.DLL from VBA: C++ strings

.DLL from VBA: C++ strings

Anonymous
Not applicable
846 Views
1 Reply
Message 1 of 2

.DLL from VBA: C++ strings

Anonymous
Not applicable
Hi

I'm experimenting with a .DLL file, exposing different functions and trying to call them from VBA. Eventually, this should become an add-in.

I wrote a simple function in C++ that returns a char *.
I exposed the function in my .def file, and I can call it from VBA. However, I only get the first two letters.

My functions returns "test" into a textbox but only "te" shows up. I told VBA the output of the function should be a string, and in C++ it's a char *.

Any suggestions?

/p6
0 Likes
847 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
My 2 cents:
Firstly, a char* is not the same as a vb string.

char* - could represent a pointer to character, pointer to character array
or pointer to null terminated character array (C-style string). Also, since
you explicitly said char*, each element in the array will represent 1byte
character.

String (vb) - represents a length pre-fixed, null terminated 2byte character
array (for UNICODE compliance).

VB does not have a direct notion of a character array. It only understands
strings. So, its dangerous to assume that char* will be implicitly converted
to strings which is what I think is happening in your case (VB is assuming
its getting a 2byte character array when in fact you are passing a 1byte
character array). For more information, you can read online articles about
the string representations in different languages.

For exactly problems like this, COM defines a BSTR data type that maps to
the correct string representations in the different languages. So, if you
can use a BSTR, use it especially if you want to create a string in C/C++
and want it to to be used in VB also. A BSTR will directly map to a VB
String data type. (Note that in the case of using Win32 API functions in VB,
the conversions between the strings in different langauges is automatically
done. But, this would be cumbersome if you were to do this for your exported
functions).

If you don't use a BSTR, you should atleast consider returning a LPCTSTR
(typedef'ed to LPCSTR under ANSI and LPCWSTR under UNICODE). But, note that
a LPCTSTR is not the same as BSTR (BSTR is length pre-fixed, LPCTSTR is
not). Also, another thing that you might want to check is whether the
allocated string is deallocated after it is used in VB. The VB runtime
automatically calls SysFreeString if the string represnets a BSTR, but not
sure what happens if it represents other types. I'm guessing you should be
alright, but you might want to make sure.

Secondly, you could consider using COM instead. For simple cases where you
just export functions, you will probably be alright as long as you are able
to account for issues like the usage of strings. But, if you want to export
classes, then you wouldn't be able to do so without using COM.

-Venkatesh.

wrote in message news:5921984@discussion.autodesk.com...
Hi

I'm experimenting with a .DLL file, exposing different functions and trying
to call them from VBA. Eventually, this should become an add-in.

I wrote a simple function in C++ that returns a char *.
I exposed the function in my .def file, and I can call it from VBA. However,
I only get the first two letters.

My functions returns "test" into a textbox but only "te" shows up. I told
VBA the output of the function should be a string, and in C++ it's a char *.

Any suggestions?

/p6
0 Likes