Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Visual Basic string is Unicode?

Hi Everyone.

I am making a DLL in C++ so as to call it from VB.

In VB:
Private Declare Sub test Lib "../testdll.dll" (a as string, b as string) as long

In C++:
long __stdcall test(BSTR *a, BSTR *b) {
.......
.......
return 1;
}

When I test the DLL I find that the string passed to it, the variable who receives it is *a, is not a UNICODE string: if you check memory you find a simple ANSI array of characters.

For Example in VB a = "bob"

Shows in Memory:

42 = 'b'
6F = 'o'
62 = 'b'
00 = NULL

the ANSI of "bob", instead of the UNICODE representation:

42 = 'b'
00 = NULL
6F = 'o'
00 = NULL
62 = 'b'
00 = NULL
00 = NULL
00 = NULL


I would appreciate so much if someone can explain to me this issue.

After all, strings in VB are not UNICODE?

Thanks so much.

FededS
[1008 byte] By [FededS] at [2007-11-11 9:52:59]
# 1 Re: Visual Basic string is Unicode?
FedEdS:
- I know approx nothing about C (and I plan to keep it that way!) but I can tell you for sure that 32 bit versions of VB do use Unicode strings internally. Any routine that exposes the string to the "external" world first converts the string to ASCII, but internally, they are all Unicode.
- If you were to use, for example, the CopyMemory API call against a VB string, you would see this very clearly...
Sigid at 2007-11-11 17:23:33 >
# 2 Re: Visual Basic string is Unicode?
VB automatically makes an ANSI copy of the string and passes the copy to the DLL. You may use the StrPtr function to work around this behavior: http://vb.mvps.org/tips/varptr.asp
Phil Weber at 2007-11-11 17:24:33 >