tags:

views:

37

answers:

1

Anyone know how to convert BSTR to char* ?

update:

I try to do this, but dont know either it is right or wrong..

char *p= _com_util::ConvertBSTRToString(URL->bstrVal);
strcpy(testDest,p );
+1  A: 

Your code is okay. ConvertBSTRToString does just that. As for the strcpy, testDest should be large enough to hold the string pointed to by p. Note that since allocates a new string you will need to free it somewhere down the line. Once you are done make sure you do:

delete[] p; 

A couple of caveats though (as you can see from BSTR documentation on MSDN):

  • On Microsoft Windows, consists of a string of Unicode characters (wide or double-byte characters).
  • May contain multiple embedded null characters.

So, your strcpy may not always work as expected.

dirkgently