Re: InStr and striping NULs

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Mike Williams" <mikea@xxxxxxxxxxxxxxxxx> wrote in message
news:%23XTZYwgIIHA.6108@xxxxxxxxxxxxxxxxxxxxxxx
How have you declared it? If it's a constant then you can't have used
Const
cNul = Chr$(0) because VB would complain about an expression being used in
a
constant declaration. In that case you might have been tempted do do
something like Const cNul = 0 or perhaps even Const cNul as String = 0. If
that's what you have done then VB will happily accept it, but VB's
automatic
type conversion will actually create a string using the Ascii code of the
"zero digit character" (Chr$(48)) which of course will cause your code to
fail to work as expected. However, if you have used Public cNul as String
(using either a standard variable length string or a fixed length one) and
if you have at some point in your startup procedure assigned Chr$(0) to
that
variable then all will be fine. In that case you need to check that your
initialisation code is being run correctly before you use the variable.
Perhaps you have declared this variable differently in this one program
that
is giving you problems, or perhaps there is another variable of the same
name, but with different contents, in scope in this specific project. It's
hard to say really what the exact problem is without looking at all your
declarations, which you haven't shown.

In the troublesome project:

1. The following are used in a public module
Public cNul As String
cNul = Chr$(0)

2. The code assigning cNul is invoked by the Form Load event before the
first use of cNul.
3. There are no other declarations of, or assignments to, cNul in the
project.

The ONLY use of cNul is in the statement in my original posting

Filename = WFD.cFileName
' k = InStr(Filename, cNul)
k = InStr(Filename, Chr$(0))

Where

WFD is defined by

Private WFD As WIN32_FIND_DATA

And the value is returned by FindFirstFileW/FindNextFileW


.