Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Anyone know how to convert BSTR to char* ?

Update: I tried to do this, but don't know if it is right or wrong.

char *p= _com_util::ConvertBSTRToString(URL->bstrVal);
strcpy(testDest,p );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
424 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your code is okay. ConvertBSTRToString does just that. As for the strcpy, testDest needs to be large enough to hold the string pointed to by p. Note that since ConvertBSTRToString 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...