We help IT Professionals succeed at work.

How to convert BSTR?

White8472
White8472 asked
on
How do you convert BSTR to (char or CString)?
How do you convert char or CString to BSTR?
Comment
Watch Question

AxterSenior Software Engineer

Commented:
Use _bstr_t object
Senior Software Engineer
Commented:
Example:
BSTR bstrVal = SysAllocString (_bstr_t(Link.operator LPCTSTR()));

BSTR innerHTML_bstrVal;
pElement->get_innerHTML(&innerHTML_bstrVal);
CString innerHTMLData = _bstr_t(innerHTML_bstrVal).operator const char*();

You can use it to convert BSTR to char, and char to BSTR.
> CString innerHTMLData = _bstr_t
> (innerHTML_bstrVal).operator const char*();

this syntax looks a little verbose, it could be written more simply like:

CString innerHTMLData = static_cast<const char*>(_bstr_t(innerHTML_bstrVal));

unless I am missing something...

Author

Commented:
Thanks Axter and Jasonclarke

Explore More ContentExplore courses, solutions, and other research materials related to this topic.