Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 COM, write BSTR to text file

views
     
TSV3i HoN6
post Feb 21 2006, 04:56 PM, updated 19y ago

ǝlıɥdouɥɔɐɹɐ
******
Senior Member
1,602 posts

Joined: Mar 2005



I have this COM program,
in one of the method


STDMETHODIMP CCMSHdl::RecvMessage(BSTR msg)
{
FILE *fptr;
fptr=fopen( "Status.txt", "a+" );

if ( NULL != fptr )
{
fprintf( fptr, "%s" , msg );
}

return S_OK;
}

Then use another VB program to send in a string say "Hello".

But the textfile only written "H", or whatever first character of the string i sent.

Can anyone tell me what's wrong with it?
could it be this line -- fprintf(fptr, "%s", msg );
where i should put something else instead of "%s" for BSTR?

This post has been edited by V3i HoN6: Feb 21 2006, 04:57 PM
anthony_yio
post Feb 21 2006, 05:59 PM

........
Group Icon
Elite
1,828 posts

Joined: Jan 2003


BSTR is actually LPOLESTR with count of char in it structure.

You should use fwprintf instead for wchar. (in the case you would only read and no write)

Further note is that you can't use CRT string operation to treat BSTR. It would failed. Always convert to LPOLESTR and then re-create a new BSTR using SysAllocString or use some wrapper library to do the operation.


anthony_yio
post Feb 21 2006, 06:00 PM

........
Group Icon
Elite
1,828 posts

Joined: Jan 2003


QUOTE(anthony_yio @ Feb 21 2006, 05:59 PM)
BSTR is actually LPOLESTR with count of char in it structure.

You should use fwprintf instead for wchar. (in the case you would only read and no write)

Further note is that you can't use CRT string operation to treat BSTR. It would failed. Always convert to LPOLESTR and then re-create a new BSTR using SysAllocString or use some wrapper library to do the operation.
*
You can only read if you insist on CRT wchar operation that is.
KLKS
post Feb 22 2006, 07:52 AM

Getting Started
**
Junior Member
292 posts

Joined: Jan 2003


http://www.codeproject.com/string/bstrsproject1.asp

a BSTR is in unicode so "Hello" would look like this in memory

48 00 65 00 6C 00 6C 00 6F 00 00

fprintf(fptr, "%s", msg ); will print till it reaches a terminator

hope the ref site helps smile.gif
hoekeat
post Feb 23 2006, 01:01 AM

New Member
*
Junior Member
45 posts

Joined: Jan 2003
QUOTE(V3i HoN6 @ Feb 21 2006, 04:56 PM)
I have this COM program,
in one of the method
STDMETHODIMP CCMSHdl::RecvMessage(BSTR msg)
{
FILE *fptr;
fptr=fopen( "Status.txt", "a+" );

if ( NULL != fptr )
{
      fprintf( fptr,  "%s" , msg );
}

return S_OK;
}

Then use another VB program to send in a string say "Hello".

But the textfile only written "H", or whatever first character of the string i sent.

Can anyone tell me what's wrong with it?
could it be this line --  fprintf(fptr, "%s",  msg );
where i should put something else instead of "%s" for BSTR?
*
it is 1am now, not sure if my brain still works properly, anyway, try this
fprintf( fptr, "%s" , (char*)msg );


anthony_yio
post Feb 23 2006, 11:30 AM

........
Group Icon
Elite
1,828 posts

Joined: Jan 2003


QUOTE(hoekeat @ Feb 23 2006, 01:01 AM)
it is 1am now, not sure if my brain still works properly, anyway, try this
fprintf( fptr,  "%s" , (char*)msg );
*
Your code won't work cause msg is a wide char. You can't just cast it.

Please read the previous posts by me and KLKS before posting yours.


dstl1128
post Feb 23 2006, 01:01 PM

Look at all my stars!!
*******
Senior Member
4,463 posts

Joined: Jan 2003
COM/OLE speaks in Unicode character (wchar_t), so you can't use any standard CRT function directly on them. Instead, use unicode equivalent or use wcstombs() (or mbstowcs_s() for secure version) for conversion.



 

Change to:
| Lo-Fi Version
0.0132sec    0.32    5 queries    GZIP Disabled
Time is now: 28th March 2024 - 10:37 PM