Convert BSTR to char*(将 BSTR 转换为 char*)
问题描述
有人知道如何将 BSTR 转换为 char* 吗?
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 );
推荐答案
你的代码没问题.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 字符(宽或双字节字符).
- 可能包含多个嵌入的空字符.
因此,您的 strcpy 可能不会总是按预期工作.
So, your strcpy may not always work as expected.
这篇关于将 BSTR 转换为 char*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 BSTR 转换为 char*
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
