Convert BSTR to char*

☆樱花仙子☆ 提交于 2019-12-18 04:35:19

问题


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 );

回答1:


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.



来源:https://stackoverflow.com/questions/3648848/convert-bstr-to-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!