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 SkipVought 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) 2

Status
Not open for further replies.

alexregistry

Programmer
May 13, 2000
6
0
0
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.&nbsp;&nbsp;<br><br>&nbsp;<br>
 
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! <br> <p>Pat Gleason<br><a href=mailto:gleason@megsinet.net>gleason@megsinet.net</a><br><a href= > </a><br>
 
char* &lt;-&gt; BSTR<br>wchar_t* &lt;-&gt; BSTR<br>wchar_t* &lt;-&gt; char*<br><br>Can I have all these conversions in one single class?
 
#include &lt;comdef.h&gt;<br><br>#ifndef _CONV_H<br>#define _CONV_H<br><br><br><br>class toBSTR <br>{<br><br>public:<br><br>&nbsp;&nbsp;&nbsp;// Foreign constructors<br>&nbsp;&nbsp;&nbsp;toBSTR (const char&nbsp;&nbsp;&nbsp;&nbsp;*p8);<br>&nbsp;&nbsp;&nbsp;toBSTR (const wchar_t *p16);<br><br>&nbsp;&nbsp;&nbsp;// Native Constructor<br>&nbsp;&nbsp;&nbsp;toBSTR (const BSTR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bstr);<br><br>&nbsp;&nbsp;&nbsp;// Non-virtual destructor (this class is concrete)<br>&nbsp;&nbsp;&nbsp;~toBSTR (void);<br><br>&nbsp;&nbsp;&nbsp;// Native conversion operator<br>&nbsp;&nbsp;&nbsp;operator const BSTR (void) const;<br><br>private:<br><br>&nbsp;&nbsp;&nbsp;// Native BSTR string<br>&nbsp;&nbsp;&nbsp;BSTR&nbsp;&nbsp;&nbsp;m_bstr;<br><br>&nbsp;&nbsp;&nbsp;// Is foreign?<br>&nbsp;&nbsp;&nbsp;BOOL&nbsp;&nbsp;&nbsp;m_bIsForeign;<br><br>&nbsp;&nbsp;&nbsp;// Protect against assignment: just declare the prototypes of the copy <br>&nbsp;&nbsp;&nbsp;// constructor and assignment operator, and DO NOT implement them<br>&nbsp;&nbsp;&nbsp;toBSTR (const toBSTR&);<br>&nbsp;&nbsp;&nbsp;toBSTR& operator= (const toBSTR&);<br><br>};<br><br><br>// Foreign constructors require allocation of a native<br>// string and conversion<br>inline toBSTR::toBSTR (const char *p8)<br>: m_bstr (SysAllocStringLen (0, strlen (p8))), m_bIsForeign (TRUE) <br>&nbsp;&nbsp;// SysAllocStringLen appends a NULL characther <br>{<br>&nbsp;&nbsp;&nbsp;// If the 1st arg. is NULL, mbstowcs() returns the required size<br>&nbsp;&nbsp;&nbsp;// of the destination string.<br>&nbsp;&nbsp;&nbsp;size_t size = mbstowcs (0, p8, strlen (p8)) + 1;<br>&nbsp;&nbsp;&nbsp;mbstowcs (m_bstr, p8, size);<br>}<br><br>inline toBSTR::toBSTR (const wchar_t *p16) <br>: m_bstr (SysAllocString(p16)), m_bIsForeign (TRUE) <br>{ <br>}<br><br><br>// Native constructor is a pass-through.<br>inline toBSTR::toBSTR (const BSTR bstr) <br>: m_bstr (bstr), m_bIsForeign (FALSE) <br>{ <br>}<br><br>// Simply give out the native wideness string. <br>inline toBSTR::eek:perator const BSTR (void) const <br>{<br>&nbsp;&nbsp;&nbsp;return m_bstr;<br>}<br><br><br>// Destructor: Delete native string only if synthesized <br>// in foreign constructor.<br>inline toBSTR::~toBSTR (void)<br>{<br>&nbsp;&nbsp;if (m_bIsForeign)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;SysFreeString(m_bstr);<br>&nbsp;&nbsp;}<br>}<br><br><br>class toCStringW <br>{<br><br>public:<br><br>&nbsp;&nbsp;&nbsp;// Native constructor<br>&nbsp;&nbsp;&nbsp;toCStringW (const wchar_t *p16);<br><br>&nbsp;&nbsp;&nbsp;// Foreign constructors<br>&nbsp;&nbsp;&nbsp;toCStringW (const char&nbsp;&nbsp;&nbsp;&nbsp;*p8);<br>&nbsp;&nbsp;&nbsp;toCStringW (const BSTR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bstr);<br><br>&nbsp;&nbsp;&nbsp;// Non-virtual destructor (this class is concrete)<br>&nbsp;&nbsp;&nbsp;~toCStringW (void);<br><br>&nbsp;&nbsp;&nbsp;// Native conversion operator<br>&nbsp;&nbsp;&nbsp;operator const wchar_t * (void) const;<br><br>private:<br><br>&nbsp;&nbsp;&nbsp;// Native wideness string: wchar_t*<br>&nbsp;&nbsp;&nbsp;wchar_t *m_sz;<br><br>&nbsp;&nbsp;&nbsp;// Is foreign?<br>&nbsp;&nbsp;&nbsp;BOOL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_bIsForeign;<br><br>&nbsp;&nbsp;&nbsp;// Protect against assignment<br>&nbsp;&nbsp;&nbsp;toCStringW (const toCStringW&);<br>&nbsp;&nbsp;&nbsp;toCStringW& operator= (const toCStringW&);<br>};<br><br><br>// Native constructor is a pass-through<br>inline toCStringW::toCStringW (const wchar_t *p16) <br>: m_sz ((wchar_t *)p16), m_bIsForeign (FALSE) <br>{ <br>}<br><br><br>// Foreign constructors require allocation of a native<br>// string and conversion<br>inline toCStringW::toCStringW (const char *p8)<br>: m_bIsForeign (TRUE) <br>{<br>&nbsp;&nbsp;&nbsp;// Calculate required buffer size (some characters may<br>&nbsp;&nbsp;&nbsp;// already occupy 16-bits under DBCS)<br>&nbsp;&nbsp;&nbsp;size_t size = mbstowcs (0, p8, strlen(p8)) + 1;<br><br>&nbsp;&nbsp;&nbsp;// Alloc native string and convert<br>&nbsp;&nbsp;&nbsp;if (m_sz = new wchar_t[size])<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mbstowcs (m_sz, p8, size);<br>&nbsp;&nbsp;&nbsp;}<br>}<br><br><br>inline toCStringW::toCStringW (const BSTR&nbsp;&nbsp;bstr)<br>: m_bIsForeign (TRUE) <br>{<br>&nbsp;&nbsp;&nbsp;// Calculate required buffer size<br>&nbsp;&nbsp;&nbsp;size_t size = wcstombs (0, bstr, wcslen (bstr)) + 1;<br><br>&nbsp;&nbsp;&nbsp;// Alloc native string and convert<br>&nbsp;&nbsp;&nbsp;if (m_sz = new wchar_t[size])<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_sz = bstr;<br>&nbsp;&nbsp;&nbsp;}<br>}<br><br><br><br>// Simply give out the native wideness string <br>inline toCStringW::eek:perator const wchar_t * (void) const <br>{<br>&nbsp;&nbsp;&nbsp;return m_sz;<br>}<br><br>// Delete w_char* string only if synthesized in foreign constructor<br>inline toCStringW::~toCStringW (void)<br>{<br>&nbsp;&nbsp;if (m_bIsForeign)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;delete[] m_sz;<br>&nbsp;&nbsp;}<br>}<br><br><br>class toCString <br>{<br><br>public:<br><br>&nbsp;&nbsp;&nbsp;// Native constructor<br>&nbsp;&nbsp;&nbsp;toCString (const char *p8);<br><br>&nbsp;&nbsp;&nbsp;// Foreign constructors<br>&nbsp;&nbsp;&nbsp;toCString (const wchar_t *p16);<br>&nbsp;&nbsp;&nbsp;toCString (const BSTR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bstr);<br><br>&nbsp;&nbsp;&nbsp;// Non-virtual destructor <br>&nbsp;&nbsp;&nbsp;~toCString (void);<br><br>&nbsp;&nbsp;&nbsp;// Native conversion operator<br>&nbsp;&nbsp;&nbsp;operator const char * (void) const;<br><br>private:<br><br>&nbsp;&nbsp;&nbsp;// Native wideness string: char*<br>&nbsp;&nbsp;&nbsp;char *m_sz;<br><br>&nbsp;&nbsp;&nbsp;// Is foreign?<br>&nbsp;&nbsp;&nbsp;BOOL&nbsp;&nbsp;m_bIsForeign;<br><br><br>&nbsp;&nbsp;&nbsp;// Protect against assignment<br>&nbsp;&nbsp;&nbsp;toCString (const toCString&);<br>&nbsp;&nbsp;&nbsp;toCString& operator= (const toCString&);<br>};<br><br><br>// Native constructor is a pass-through<br>inline toCString::toCString (const char *p8) <br>: m_sz ((char*)p8), m_bIsForeign (FALSE) <br>{ <br>}<br><br><br>// Foreign constructor requires allocation of a native<br>// string and conversion<br>inline toCString::toCString (const wchar_t *p16)<br>: m_bIsForeign(TRUE) <br>{<br><br>&nbsp;&nbsp;&nbsp;// Calculate required buffer size (some characters may<br>&nbsp;&nbsp;&nbsp;// require more than one byte under DBCS)<br>&nbsp;&nbsp;&nbsp;size_t size = wcstombs (0, p16, wcslen (p16)) + 1;<br><br>&nbsp;&nbsp;&nbsp;// Alloc native string and convert<br>&nbsp;&nbsp;&nbsp;if (m_sz = new char[size])<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wcstombs (m_sz, p16, size);<br>&nbsp;&nbsp;&nbsp;}<br>}<br><br><br><br>// Simply give out the native wideness string <br>inline toCString::eek:perator const char * (void) const <br>{<br>&nbsp;&nbsp;&nbsp;return m_sz;<br>}<br><br><br>// Delete native string only if synthesized in foreign constructor<br>inline toCString::~toCString (void) <br>{<br>&nbsp;&nbsp;&nbsp;if (m_bIsForeign) <br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete[] m_sz;<br>&nbsp;&nbsp;&nbsp;}<br>}<br><br><br>#endif&nbsp;&nbsp;// _CONV_H<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top