Using _bstr_t::_bstr_t(const char*)
is not exactly a good idea in production code:
Constructs a
_bstr_t
object by callingSysAllocString
to create a newBSTR
object and encapsulate it. This constructor first performs a multibyte to Unicode conversion. Ifs2
is too large, you [sic] may generate a stack overflow error. In such a situation, convert yourchar*
to awchar_t
withMultiByteToWideChar
and then call thewchar_t *
constructor.
Besides that _bstr_t::operator wchar_t*() const throw()
seems barely useful. It's just for struct member extraction, so you're constrained to a const
:
These operators can be used to extract raw pointers to the encapsulated Unicode or multibyte BSTR object. The operators return the pointer to the actual internal buffer, so the resulting string cannot be modified.
So _bstr_t
is just a helper object for encapsulating BSTR
s, and a mediocre one at that. Conversion using MultiByteToWideChar
and WideCharToMultiByte
is a much better choice, for multiple reasons:
- It's much less prone to crash.
- You don't get a
const
buffer in return, because you provide your own. - The names of those functions are self-descriptive. Conversion through a constructor and casting operator of an unrelated type is not.