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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
nietodCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.