将BSTR转换为char * [英] Convert BSTR to char*

查看:792
本文介绍了将BSTR转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人知道如何将BSTR转换为char *?

Anyone know how to convert BSTR to char* ?

更新:我试图这样做,但不知道是对还是错。 p>

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


推荐答案

ConvertBSTRToString 就是这样。对于 strcpy testDest 需要足够大以容纳 p 。注意,因为ConvertBSTRToString分配一个新的字符串,你将需要释放它在某处下。一旦你完成,确保你做:

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; 

有几个注意事项(从 BSTR MSDN上的文档):

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



  • 在Microsoft Windows上,由一串Unicode字符(宽字符或
    双字节字符)组成。 li>
  • 可能包含多个嵌入的空字符。

strcpy 总是可以按预期工作。

So, your strcpy may not always work as expected.

这篇关于将BSTR转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆