Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting BSTR's to char *

Status
Not open for further replies.

MarkRuse

Programmer
Aug 11, 1999
29
GB
Hi,<br>
<br>
I am currently trying to write a ATL object which takes in a BSTR and convert it to a char *. I then put this char * into a Roguewave RWCString object to create a string object.<br>
<br>
fn(BSTR *Source)<br>
{<br>
unsigned DataLen;<br>
BSTR BstrData;<br>
<br>
RWCString str_one((char*) *Source);<br>
DataLen = str_one.length();<br>
SysReAllocString (Source, (BSTR) str_one.data());<br>
}<br>
<br>
This code would take in the Source string, allow me to change it, then return it back to the calling code.<br>
<br>
This worked under NT3.51, but now on NT4 it seems as if the BSTR is a Multibyte Character set.<br>
<br>
Does anybody know how I can simply take in a BSTR convert it to a char *, work with it, then convert it back to a BSTR for returning to the calling code ?<br>
<br>
I have spent all day with this, and I am about to pull my hair out!<br>
<br>

 
Try something like:<br>
<br>
USING_CONVERSION;<br>
TCHAR* myStr = W2A(yourBSTR);<br>
<br>
Look up W2A and other ATL &quot;string conversion macros&quot; under the MSDN.
 
You can wrap your incoming BSTR in the _bstr_t class, which has a char* operator. You could use this constructor:<br>
<br>
(following clipped from the MSDEV help file)<br>
<br>
_bstr_t( BSTR bstr, bool fCopy ) Constructs a _bstr_t object from an existing BSTR (as opposed to a wchar_t* string). If fCopy is false, the supplied BSTR is attached to the new object without making a new copy via SysAllocString. This is the method used by the wrapper functions in the type library headers to encapsulate and take ownership of a BSTR, returned by an interface method, in a _bstr_t object. <br>
<br>
<p>Pat Gleason<br><a href=mailto:gleason@megsinet.net>gleason@megsinet.net</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top