Link to home
Start Free TrialLog in
Avatar of Nightjar25
Nightjar25

asked on

Null Characters...and strings.....

Hi,

I am currently implementing an iterface to a COM object through VC++ 6, and MFC.  As part of this job I am interfacing to get and recieve streams (BLOBs)of data from the COM object.  The problem is that we are typically sending and recieving image data, and the COM object is recieving and sending strings.  Images obviously have a lot of NULL terminators in the data, which creates a problem.  I can put the data into a char* (or char far*), successfully, but then place it in a _bstr_t so I can pass it to a _variant_t for the COM object, this tends to corrupt the data, and therefore doesn't solve the problem...need help on what else I can do??  
Avatar of sboucher789
sboucher789

Have you tried passing your data through char array (VT_UI1 | VT_ARRAY)? These types are valid in COM I believe, or at least in a variant. Seek for help on VARTYPE in the MSDN.
Avatar of Nightjar25

ASKER

Yes but the COM object is expecting a VT_BSTR type, not a VT_U1 | VT_ARRAY
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use this.

BSTR SysAllocStringByteLen( LPCSTR  psz, unsigned int len );

If psz is Null, a string of the requested length is allocated, but not initialized. The string psz can contain embedded null characters, and does not need to end with a Null.

If this works please accept this Comment as answer.

Gunnar Valur
By the way.  You could have some problem extracting the stuff from the string on the other end.

When you send a BSTR the length is included.  It is a 32-bit value that is preceeding the string it self.  I am not sure if BSTR will point to this 32-bit value or the first byte in the string.   If it points to the first byte in the string you can do like this.

DWORD* pointer;
DWORD length;

pointer = (DWORD*)bStr;

// IF THE 32-BIT value is at front.
length = *pointer;

// IF THE 32-BIT value before front.
length--;
length = *pointer;

Now you know the size of the buffer so you can type cast it and you know its length.

Gunnar Valur
Thanks gvg, that was very helpful, FYI...it's before the front, and your code should read pointer--;
Opps....I screwed up, it was GVG's answer (SysAllocStringByteLen) that lead me to solving this.......sorry guys
If you can afford it you can create a new question to give me points but it is ofcourse up to you.

Gunnar Valur
If you can't afford it--which would not be surprising--post a request in the customer service topic area to have this straighened out.  Be sure to list this question's ID in your request.