Re: api call for tchar*
From: Gale Green (gale_at_databeat.fsnet.co.uk)
Date: 07/03/04
- Next message: Randy Birch: "Re: Right align menu picture"
- Previous message: J French: "Re: api call for tchar*"
- In reply to: Tony Proctor: "Re: api call for tchar*"
- Next in thread: Gale Green: "Re: api call for tchar*"
- Reply: Gale Green: "Re: api call for tchar*"
- Reply: Gale Green: "Re: api call for tchar*"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 03 Jul 2004 12:19:47 +0100
On Fri, 2 Jul 2004 17:13:03 +0100, "Tony Proctor"
<tony_proctor@aimtechnology_NOSPAM_.com> wrote:
> There's a difference if you specify a ByRef String argument in the API's
> Declare statement as opposed to a ByVal String argument. I believe the ByVal
> String passes an ANSI version while a ByRef passes a Unicode version.
VB stores a String variable internally as a BSTR.
A BSTR is a Long.
It contains the address of the first character in a row of 2-byte
Unicode characters. The row of characters is preceded by a Long
containing the length of the row of characters, but the BSTR points to
the first character in the row of characters, not to the Length field.
When passing a String to an external procedure such as a Windows API
function, the BSTR is converted to an ANSI BSTR (ABSTR, some say) in a
new location. An ABSTR is, again, a Long containing the address of a
row of 1-byte ANSI characters, preceded as before by a Long containing
the length of the row.
A ByRef argument is passed as the address of the ABSTR and, as one
might expect, a ByVal argument is passed as the value of the ABSTR,
which is the address of the first ANSI character in the string.
The called function doesn't usually know that it is being passed an
ABSTR, so it knows nothing about the Length field. So, if a value is
passed back in a ByRef parameter, VB ignores the Length field and
relies on the fact that the returned value will be null-terminated.
The returned (ANSI) string is then converted to Unicode, and the
address of the first character in the Unicode string is passed back to
your BSTR. Thus, your BSTR will stay in the same place (i.e., your
String variables don't move), but the array of Unicode characters to
which they point can, and will, move, depending on what functions you
call. So, you can save and reuse a VarPtr to a String variable, but
not a StrPtr. A StrPtr should be obtained afresh each time you want to
use it, if you are calling external functions with ByRef String
parameters.
Even more jiggery-pokery has to be performed in Windows NT and above
when you call an "A", rather than a "W", function:
VB converts your BSTR to an ABSTR and passes it to the "A" function.
The "A" function converts the ANSI to Unicode and passes it to its
sister "W" function. The Unicode value returned from the "W" function
to the "A" function is then converted back to ANSI and returned to VB,
which converts the ANSI to Unicode and sets the address of the first
character of the Unicode string back in your BSTR.
Gale.
- Next message: Randy Birch: "Re: Right align menu picture"
- Previous message: J French: "Re: api call for tchar*"
- In reply to: Tony Proctor: "Re: api call for tchar*"
- Next in thread: Gale Green: "Re: api call for tchar*"
- Reply: Gale Green: "Re: api call for tchar*"
- Reply: Gale Green: "Re: api call for tchar*"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|