Re: file delete routine is intermittent
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Wed, 20 Apr 2005 16:18:48 -0400
"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
.
- Follow-Ups:
- Re: file delete routine is intermittent
- From: Bob Butler
- Re: file delete routine is intermittent
- From: Jonathan Wood
- Re: file delete routine is intermittent
- References:
- file delete routine is intermittent
- From: Steve Easton
- Re: file delete routine is intermittent
- From: MikeD
- Re: file delete routine is intermittent
- From: Jonathan Wood
- Re: file delete routine is intermittent
- From: MikeD
- Re: file delete routine is intermittent
- From: Jonathan Wood
- file delete routine is intermittent
- Prev by Date: Re: file delete routine is intermittent
- Next by Date: Re: file delete routine is intermittent
- Previous by thread: Re: file delete routine is intermittent
- Next by thread: Re: file delete routine is intermittent
- Index(es):
Relevant Pages
|