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!

BSTR <-> char* conversion(s) (implemented as C++ class)

Status
Not open for further replies.

alexregistry

Programmer
May 13, 2000
6
CA
Hi all,<br><br>I tried to find the article where Don Box discusses many variations of <br>BSTR &lt;-&gt; char* conversion. I desperately need it.<br><br>Can any one send me that article from MSJ August 1995 Interfaces and the Registry/GetActiveObject Vs. MFC?<br><br>Anybody has code on this topic?<br><br>Thanks a lot.
 
I use a COM support class called _bstr_t which is defined in COMDEF.H. It wraps the BSTR type.&nbsp;&nbsp;You can construct a _bstr_t object passing it a BSTR and use a built-in =operator function to extract the data as char*, and vice versa.<br><br>Hope this helps! <p>Pat Gleason<br><a href=mailto:gleason@megsinet.net>gleason@megsinet.net</a><br><a href= > </a><br>
 
If you use MFC you can convert the BSTR in the CStriong constructor!<br><br>CString Str(&lt;BSTR parameter&gt;);
 
If you use MFC you can convert the BSTR in the CString constructor!<br><br>CString Str(&lt;BSTR parameter&gt;);
 
BSTR AnsiToBSTR( LPTSTR pchData )<br>{<br>&nbsp;&nbsp;&nbsp;// Helper function derived from source of CString::AllocSysString()<br>&nbsp;&nbsp;&nbsp;BSTR bstr = NULL;<br>#if defined(_UNICODE) ¦¦ defined(OLE2ANSI)<br>&nbsp;&nbsp;&nbsp;bstr = ::SysAllocStringLen(pchData, wcslen( pchData ) );<br>#else<br>&nbsp;&nbsp;&nbsp;int nLen = MultiByteToWideChar(CP_ACP, 0, pchData,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strlen( pchData ), NULL, NULL);<br>&nbsp;&nbsp;&nbsp;bstr = ::SysAllocStringLen(NULL, nLen);<br>&nbsp;&nbsp;&nbsp;if (bstr == NULL)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NULL;<br>&nbsp;&nbsp;&nbsp;MultiByteToWideChar(CP_ACP, 0, pchData, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strlen( pchData ), bstr, nLen);<br>#endif<br><br>&nbsp;&nbsp;&nbsp;return bstr;<br>}<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top