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??  
Nightjar25Asked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
If the COM object is expecting a string, how can you expect it to work with an image????

Anyways you can conver the image data to a NUL termianted string (and convert back again.)  Just convert it to an ASCII string that represents the date in HEX.  I.e convert each byte to 2 ASCII characters that represent the byte in HEX.  So a 0 byte will convert to "00", and will not be a NUL character.  The only NUL will be the one at the end of the string.
0
 
sboucher789Commented:
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.
0
 
Nightjar25Author Commented:
Yes but the COM object is expecting a VT_BSTR type, not a VT_U1 | VT_ARRAY
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
gvgCommented:
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
0
 
gvgCommented:
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
0
 
Nightjar25Author Commented:
Thanks gvg, that was very helpful, FYI...it's before the front, and your code should read pointer--;
0
 
Nightjar25Author Commented:
Opps....I screwed up, it was GVG's answer (SysAllocStringByteLen) that lead me to solving this.......sorry guys
0
 
gvgCommented:
If you can afford it you can create a new question to give me points but it is ofcourse up to you.

Gunnar Valur
0
 
nietodCommented:
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.
0
All Courses

From novice to tech pro — start learning today.