Advertisement

Array from VB to VC?

Started by March 27, 2001 07:27 AM
8 comments, last by KaneBlackflame 23 years, 10 months ago
How do you pass an array in VB to a C dll function? I need to pass an array of strings to a sort function. How does C reicieve an array?
"Victims...aren't we all?" -Brandon Lee, the Crow
When passing arrays, C/C++ will always treat them as a pointer to the first element of the array. I''m not sure if that helps you any though .

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://www.gdarchive.net/druidgames/
Advertisement
I think I know how to pass an array, but an array of stings.. not sure about that.

Anyway, pass by reference the first element of your array.
For example:

Sort( myString(1), 5)

In your Declare of Sort, make sure it passes the first parameter by reference, not by value. If you got the Declare from somewhere I''m sure they knew what they were doing. What function is it? Might help me find an answer. Or, let me see the Declare you''re using.
Ok, after a tiny bit of research, strings in VB are passed ByVal, not ByRef (at least from our point of view). So, a function taking a string should be declared as taking it ByVal. But arrays are taken ByRef. Now... if you have your Declare already done for you, this should be simple.



Dim ArrayOfStrings(1 to 10) as String

Sort(ArrayOfStrings(1), 10)



should work. Maybe, sort of. Who knows?
Hope I''ve been some help.
Strings from VB to C++ can be tricky, it has to do with the way VB stores a string in memory. Unlike C/C++, VB does not end their strings with a null character (which is why you can have a null character in the string). Instead, VB uses a mechanism called a BSTR (basic string). The first byte in a BSTR contains the length of the string, and the remaining bytes are the string data. So passing a pointer to a VB string (BSTR) will do you no good. You are passing from VB to a C++ dll, are you using COM?
I have never tested this, but my sources say BSTR do terminate with a null, and the pointer to the string points to the start of the actual string, not the length. To get the length, you go BACK(!) from the pointer...


[length](here is where pointer points)[H][E][L][L][O][\0]

BSTR''s can have null''s before the end of the string (ie more than 1 null) and the organization might be more like this..

[H][ ][E][ ][L][ ][L][ ][O][ ][\0]

but, like I said, I don''t really know what I''m talking about.

Advertisement
I can''t believe all the replys to this. Thanks for your help. I have looked into memory while trying to debug this problem and found

[H][ ][E][ ][L][ ][L][ ][O][ ]
(replace this string with something pertaining to my app of course)

in the VB section of RAM ...I''m not sure if it was NULL terminated or not. I didn''t see a count, but maybe I just overlooked it.
So, if I try and pass a string back to VB from C, I need to format it as each char as a WORD with the value in the first byte, and the second byte zeroed out? And then precede this string with a WORD containing the length of the string, and point the pointer to the string itself, before I return it to VB? Does this sound about right?
"Victims...aren't we all?" -Brandon Lee, the Crow
The BSTR is actually a 32 bit pointer variable that points to a Unicode character array. You are correct Thrump, a BSTR does terminate with a null and can also contain nulls before the end. The Unicode character array that a BSTR points to is preceded by 4 bytes that specifies the length of the character array in bytes (not the number of characters). It's important to point out that the BSTR points to the beginning of the character array, not the 4 byte length value, and null characters will be 16 bit zeros.

Edited by - noparity on March 28, 2001 12:16:14 PM
Try using the _bstr_t class in VC++, it encapsulates a BSTR type string. Are you using a COM interface for your VB and C applications to communicate?
I am not using any interface between VB and C. VB simply imports the dll functions using:

Public Declare Function VBFooDLL Lib "xxxx.dll"...

This works well enough, it''s just that strings are turning out to be tough.
"Victims...aren't we all?" -Brandon Lee, the Crow

This topic is closed to new replies.

Advertisement