Re: file delete routine is intermittent




"Jonathan Wood" <jwood@xxxxxxxxxxxxxxxx> wrote in message
news:O8FqjvcRFHA.3444@xxxxxxxxxxxxxxxxxxxxxxx
> MikeD,
>
> > > This is not necessary as Visual Basic automatically appends a single
> null
> > > terminator character to every string.
> >
> > Only when you pass a string as a parameter (and pass it ByVal). In this
> > case, you're passing a structure. VB does NOT automatically append a
null
> > character to string members of structures so you must append the null
> > character yourself. I'm very surprised you weren't aware of that.
>
> Virtually every API function uses the C standard of having strings
> terminated with a null character. Therefore, strings within structures
being
> passed to Windows would not work unless you manually appended the
> terminator. After years of passing structures to Windows, I can tell you
> with confidence that this is not necessary.


I agree it's not necessary with all structures that you pass to API
functions. In fact, the FILEOPSTRUCT is the only one I can think of right
off-hand in which you must do this....and it's clearly documented that it
must be terminated with *2* null characters. Therefore, at the very least,
you should explicitly append 1 of them yourself.


>
> I browsed the file VB4DLL.TXT that ships with Visual Basic. In section 4,
it
> includes the following items:
>
> "Visual Basic maintains variable-length strings internally as BSTRs.
> BSTRs are defined in the OLE header files as OLECHAR FAR *. An
> OLECHAR is a UNICODE character in 32-bit OLE and an ANSI character in
> 16-bit OLE. A BSTR can contain NULL values because a length is also
> maintained with the BSTR. BSTRs are also NULL terminated so they can
> be treated as an LPSTR."

Yes, but that is passing a string, not a structure, so it's not an
apples-to-apples comparison.

>
> and in section 5 it includes an example of passing a UDT to a DLL and does
> not manually append a terminator.

As I said, it's not often that I have found it absolutely necessary to
explicitly null-terminate a string member of a structure. My experience has
been, however, that it's best to always do so.

>
> What is your information based on?

Mostly just my own experience, but I do recall reading something (a KB
article perhaps) that stated when passing a structure containing string
elements, the strings were not automatically null-terminated. It's entirely
possible that article was referring to VB3. I just know that one of the
first times that I ever called SHFileOperation under Win2000, it didn't
"work" (it didn't crash, it just did nothing). Appending the 2 null
characters solved it and I've never had a problem with this function since.


--
Mike
Microsoft MVP Visual Basic


.



Relevant Pages

  • Re: file delete routine is intermittent
    ... >> terminator character to every string. ... > Only when you pass a string as a parameter. ... VB does NOT automatically append a null ...
    (microsoft.public.vb.winapi)
  • Re: file delete routine is intermittent
    ... > This is not necessary as Visual Basic automatically appends a single null ... > terminator character to every string. ... Only when you pass a string as a parameter. ...
    (microsoft.public.vb.winapi)
  • Re: Append characters to file name to establish uniqueness
    ... I am trying to keep a set of files online and append a character ... string to each file in order to establish uniqueness to each file name ...
    (comp.os.vms)
  • Re: Pound symbol in a string
    ... > When I use system.IO.StreamWriter to write append a string to file that ... > contains the GBP pound symbol, I notice that it also appends an extra ... Yet when I examine the string character ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Unicode: ugh!
    ... a string is defined as a pointer to char in the ... by and including the first null character. ... the null terminator is not a NULL character (NULL ...
    (comp.lang.c)