Convert BSTR to const char *

Possible duplicates:
What code is better for converting BSTR parameters to ANSI in C / C ++?
How to convert char * to BSTR?

Hi guys, I am very new to C ++, anyone has an idea how I can convert BSTR to const char *?

+7
c ++
source share
3 answers

A BSTR is actually a WCHAR* with a length prefix. A BSTR value indicates the beginning of a line, not a length prefix (which is stored in bytes only "to" the location pointed to by BSTR ).

In other words, you can consider BSTR as if it were const WCHAR* . No conversion required.

So your question really is: "How to convert a Unicode string ( WCHAR* ) to char* ?" and the answer is to use the API ::WideCharToMultiByte function as described here . Or, if you use MFC / ATL in your application, use ATL and MFC Conversion Macros .

+6
source share
 #include "comutil.h" BSTR bstrVal; _bstr_t interim(bstrVal, false); // or use true to get original BSTR released through wrapper const char* strValue((const char*) bstrVal); 

This handles all Wide Char to multibyte conversions.

+4
source share
 typedef OLECHAR FAR * BSTR; typedef WCHAR OLECHAR; 

From here .

WCHAR *. This is a wide char, not a char. This means that it is Unicode, not ASCII and does not need to be converted. If you want to use const char *, do not use wide types.

Although it is possible to use some hackers, you may lose data, as you will convert text from Unicode to ASCII.

0
source share

All Articles