Archived

This topic is now archived and is closed to further replies.

KaneBlackflame

Passing VB String array to C DLL?!?!?!?

Recommended Posts

I have been trying to pass a VB array of Strings to a C Dll function for days now. M$ knowledge base was no help, and I keep hearing to pass a reference to the first element in the array, but it isn''t working. Can someone correct my code? Here is the VB declare and implementation as well as the function header of the C++ function : //////// Public Declare Function CreateCode Lib "THREADDLL.dll" (ByVal Nam As String, ByRef sojfjCode As String) As Long //////// x = CreateCode(EditNameVB, VBTemp(0)) //////// long APIENTRY CreateCode(char *EditName,char **Code) //////// When I access Code[0], I get the first element. When I access Code [1], I get garbage or other data, but not the second string in the array. What am I missing? I have heard HAD mentioned as well as HLSTR types that need to be used with VBArrays, but I can''t find documentation anywhere on them. I think they were custom defines in a sample I saw. Anyone know what is wrong with the above code? "Victims...aren''t we all?" -Brandon Lee, the Crow

Share this post


Link to post
Share on other sites
It will be a SAFEARRAY of OLESTRINGS/System String/BSTRs (use ATL's _bstr_t)

ohh, you're not writing the dll....
Make a... um... I don'
I don't think it's possible to pass an array of VB strings as an array of char*'s.

You can pass A string, but I don't think it's possible to pass an array of them.

To pass A VB string into a C char*, dim it as String*255 (byRef). And make certain it has a null when you pass it. Same thing when receiving strings from C APIs.

[edited by - Magmai Kai Holmlor on May 1, 2002 8:24:17 PM]

Share this post


Link to post
Share on other sites
You **May** be able to create a Type with several strings and pass it instead of the string array, if you''re the one allocating the array and the number of strings is constant...

You know, I never wanted to be a programmer...

Alexandre Moura

Share this post


Link to post
Share on other sites
VB strings I think are stored like as a pointer to a string, then the 4 bytes before the string is a (signed I think) 4 byte integer saying how long the string is. The string is null terminated.

For arrays, look up the safearray structure.

Alternatively, I would probably cram the whole thing into a fixed length string (or a byte array) and pass a pointer to the first character in that.

Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
I think I should probably clear things up.
1) I am a C Programmer. I specialize in time critical data processing. The only reason I have to touch a language like VB is because my company uses a VB as an interface. Then they expect me to make that childish language work with C. I have very little VB experience, and therein lay my problem. I would prefer to do it all in C, but the Boss " an experianced " VB coder, who can''t help me with this by the way, wants to maintain control over the interface, probably so he can change the colors whenever he wants...you know, cornflower blue on Tuesdays. ((Hope the MPAA does not nail me for that use of reference))

2) I am writing both the DLL and the VB interaction portion of it. The code I previously posted is how I was passing a string, and was also what someone suggested to do in a previous post. I am looking for someone to correct my mis information on either side. I have heard to pass a pointer to the first element of the array, which is what the char** was for. But that does not work. I have heard safe arrays mentioned also, anyone have more detail?

((I hate VB, but don''t tell my boss))

"Victims...aren''t we all?"
-Brandon Lee, the Crow

Share this post


Link to post
Share on other sites
Try this:


Dim TempString() As Byte

TempString = StringToPass

FunctionCall ByVal (VarPtr(TempString(0))


This will pass a pointer to the first character in StringToPass
Remember that VB strings are unicode too thus you need to jump in 2 byte increments to read it back.

How exactly would a string array be stored in C++, would it be a rectangular block of memory (ie, fixed length strings one after each other) or would it be an array of pointers to the strings?

Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
I should probably add that that code above will pass a pointer to a copy of the string, not the actual string... just in case you didn''t notice

Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
"AND THEN THERE WAS LIGHT..."

Well, between a mixture of luck and someones previous post mentioning SAFEARRAYs, I was able to piece together a working DLL. VB does use SAFEARRAY structures. I had to make my own version of the structure because of the old FAR and such used in the original, but I can now pass arrays of strings to a C DLL from VB.
Also, on the C side, plain VB strings are single wide BSTR. Quasi-BSTR if you will. There are 4 bytes detailing the length of the string, and the string itself, but the string is a plain C string. Also, you must pass VB back the same single wide string. If you pass a true BSTR back to VB, you have a string with NULL characters between each letter. (i.e. You only see the first letter) At least, this is how they behave for me. Thanks for all your help everyone!

"Victims...aren''t we all?"
-Brandon Lee, the Crow

Share this post


Link to post
Share on other sites
VB strings are true BSTRs it is just the automatic conversion that happens when working with DLLs. VB will convert from Unicode to MBCS when strings are passed to a DLL.

Share this post


Link to post
Share on other sites