What's new

VB to C++ strings

Doomulation

?????????????????????????
I find it not easy to pass strings to c++ from vb. Multiple, that is. Arrays.
It ends up that c++ only recieves the first element of it. And vb recieves no data at all from char* arrays.
Is there some good way to pass arrays of strings to c++ without using a string class?

Oh and, i found that the second element passed in a string array passed from vb to c++ is approximetly 28 before the first element. Also, each character seem to be splitted by 1 byte.

Eg, "t" "." "e" "." "s" . "t" . "2"

Is there a way to get this data thru c++ code or assembler code? If so, how?
 

Cyberman

Moderator
Moderator
Doom it's in UNICODE format or wchar.. I suggest StringToWideChar conversion to move from C++ strings to VB's strings. Going from that to C++ you will need to convert multibyte format to a C type string array.

Cyb
 

tooie

New member
VB actually uses BSTR which is similar to a wide char string ..

have a look at
SysAllocString
SysAllocStringLen
SysFreeString
SysStringLen
etc...

if you create a c dll and export the function and link to VB that way it should automaticly try and convert the paramaters to an ansii string
 
OP
Doomulation

Doomulation

?????????????????????????
? Be a little more specific there.
Link to vb, did you say? The problem's mostly that the first index or string arrives, but c++ cannot get the other indexes.
 

tooie

New member
I am not sure what your saying .. is it an export .. if so what is your declaration in vb for the function ..

if your passing it straight through as a BSTR .. then it will be unicode, basicly ansi character followed by a 0 .. then the next ansi character .. use SysStringLen since VB strings can contain nulls .. where stings in C++ are useally NULL terminated.
 

Cyberman

Moderator
Moderator
And because VB strings contain NULLS your will only get the first character each time. See? :)

Example

VB string
'C' \0 'y' \0 'b' \0 \0 \0 of type WORD

To C/C++ that has only one character yet to VB it has 3
So.. you have to convert from UNICODE to ANSI encoding (IE word sized to byte sized).
In C/C++ it would be
"Cyb" \0 of type char

Cyb
 

tooie

New member
when I mean VB can have nulls in the string it can have:
'C' \0 'y' \0 'b' \0 \0 \0 'b' \0 'y' \0 'C' \0 \0 \0

that is a valid VB string.. you can contain binary data in to VB strings ..

you can not just assume it is null terminated.
 

Cyberman

Moderator
Moderator
Does VB store the length of the string in a structure?
If it does then it's probably more like

Code:
typdef struct
{
  unsigned long Size;
  WORD *Data;
} VBstring;
where the data holds the string information and the structure holds the size and a reference to the data. Is it using the ATOM type in windows or what?

Cyb
 

tooie

New member
cyb: it is basicly like you say .. the length is the dword before the string .. so it is in a structure .. this is from the sysAlloc functions ..

if they changed then VB would change where that value is stored .. tho I have not found much or anything that says that, I know that just from reversing and looking around at memory ..

so len = *((DWORD *)string - 1);

it does not use atoms .. for that matter not that much really uses atoms that I know of.
 
OP
Doomulation

Doomulation

?????????????????????????
So...what would you do to pass a vb string-array without problems to c++? A BSTR pointer will recieve no data. And I'm not sure how typedef structures works as it won't compile.
 

Top