convert char** to BSTR

how to convert char** to BSTR
Who is Participating?
ZoppoConnect With a Mentor Commented:
Hi Sohela,

there's a function 'BSTR SysAllocString( const OLECHAR *sz );' which can be used for this.

To use it you need a OLECHAR* - to convert a 'char*' to 'const OLECHAR*' you can use the macro 'A2COLE'

So, if you have a 'char** pChar' (which is a pointer to a pointer of chars) it should be possible to do it like this:

const OLECHAR* pOleChar = A2COLE( *pChar );
BSTR str = SysAllocString( pOleChar );
// do something with the 'str'
SysFreeString( str ); // need to cleanup the allocated BSTR

Hope that helps,

SohelaAuthor Commented:
hey thanks a lot got it
Fine, that's great.
You could also use one of the BSTR wrapper classes such as CComBSTR or _bstr_t, both of which can take a char* in the constructor:

char ** ppChar;


CComBSTR myComBSTR(*ppChar);

_bstr_t my_bstr_t(*ppChar);

Then you get all of the other functionality of the classes, including the ability to detach the underlying BSTR if you need it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.