Unicode String Parameters in DLL's

U

Unknown

We are converting our application to work with Unicode. The Word macro in
our application "communicates" with our main application via a C DLL. This
DLL includes several functions which return Unicode strings back to the
macro as input parameters. My question is, what is the correct declaration
for the DLL functions in VBA?

I currently have it working like this:

Private Declare Function GetUserName Lib "MyDLL.dll" (ByVal lpUser As Long)
As Integer

GetUserName simply copies the user name into a Unicode string.

I call this function from VBA code with something like this:

Dim sUser as String
sUser = String(255, Asc(" "))

GetUserName StrPtr(sUser)

This seems to work, but I've only tested it with standard characters (i.e.
nothing outside of the normal Latin character set). I'm concerned about
VBA's automatic converting to ANSI, although I don't really understand what
that means... Does it mean that VBA will convert the sUser string to ANSI
when I try to access it, and so any characters outside of the ANSI set will
be converted to question marks or something like that?

Any help you can give me on this would be greatly appreciated.
 
T

Tom Winter

You've probably heard something wrong about "VBA's automatic converting to
ANSI". Strings in VB6 and VBA are Unicode. (The forms package in VB6 it NOT
Unicode, so you might have heard some stuff about that. VBA's forms package
is Unicode.) I do this exact same kind of thing when calling Windows Unicode
APIs. VB/VBA will see the Unicode characters put in your "buffer" by your
DLL just fine. It doesn't so any kind of conversion before or after the DLL
call.

One thing to watch out for is if your DLL routine puts a zero byte at the
end of the string. To VBA, a zero byte is just another character in the
string (Chr$(0)); it does not terminate the string. You'll need a routine to
scan where the zero byte is and then only take the text before that zero
byte. I can give you a function for that if you need.
 
U

Unknown

Cool. Thanks Tom. I have a routine that grabs the text before the trailing
zero.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top