ComBSTR的使用
用 CComBSTR 进行编程
ATL 类 CComBSTR 提供对 BSTR 数据类型的包装。尽管 CComBSTR 是一个有用的工具,但有一些情况需要特别小心。
转换问题
虽然一些 CComBSTR 方法自动将 ANSI 字符串参数转换为 Unicode,但这些方法总是返回 Unicode 格式的字符串。若要将输出字符串转换回 ANSI,请使用 ATL 转换类。有关 ATL 转换类的更多信息,请参见 ATL 和 MFC 字符串转换宏。
示例
// Declare a CComBSTR object. Although the argument is ANSI,
// the constructor converts it into UNICODE.
CComBSTR bstrMyString( "Hello World" );
// Convert the string into an ANSI string
CW2CT szMyString( bstrMyString );
// Display the ANSI string
MessageBox( NULL, szMyString, _T("String Test"), MB_OK );
如果使用字符串来修改 CComBSTR 对象,请使用宽字符字符串。这会减少不必要的转换。
示例
// The following converts the ANSI string to Unicode
CComBSTR bstr("Test");
// The following uses a Unicode string at compile time
CComBSTR bstr(L"Test");
范围问题
与任何功能完善的类一样,CComBSTR 在超出范围时将释放其资源。如果函数返回指向 CComBSTR 字符串的指针,这会引起问题,因为指针将引用已经释放的内存。在这种情况下,请使用Copy 方法,如下所示。
示例
// The wrong way to do it
BSTR * MyBadFunction()
{
// Define a pointer to a BSTR
BSTR * bstrStringPtr;
// Create the CComBSTR object
CComBSTR bstrString("Hello World");
// Convert the string to uppercase
bstrString.ToUpper();
// Assign the pointer
* bstrStringPtr = bstrString;
// Return the pointer. ** Bad thing to do **
return bstrStringPtr;
}
// The correct way to do it
HRESULT MyGoodFunction(/*[out]*/ BSTR* bstrStringPtr)
{
// Create the CComBSTR object
CComBSTR bstrString("Hello World");
// Convert the string to uppercase
bstrString.ToUpper();
// Return a copy of the string.
return bstrString.CopyTo(bstrStringPtr);
}
显式释放 CComBSTR 对象
在对象超出范围之前,可以显式释放包含在 CComBSTR 对象中的字符串。如果字符串被释放,则 CComBSTR 对象无效。
示例
// Declare a CComBSTR object
CComBSTR bstrMyString( "Hello World" );
// Free the string explicitly
::SysFreeString(bstrMyString);
// The string will be freed a second time
// when the CComBSTR object goes out of scope,
// which is unnecessary.
在循环中使用 CComBSTR 对象
在 CComBSTR 类分配缓冲区来执行某些运算时,如 += 运算符或 Append 方法,建议不要在紧密型循环内执行字符串操作。在这种情况下,CStringT 可提供更好的性能。
示例
// This is not an efficient way
// to use a CComBSTR object.
CComBSTR bstrMyString;
while (bstrMyString.Length()<1000)
bstrMyString.Append(L"*");
内存泄漏问题
将已初始化的 CComBSTR 的地址作为 [out] 参数传递到函数会导致内存泄漏。
在下面的示例中,在函数 OutString 替换为了保存字符串 "Initialized" 而分配的字符串时,该字符串被泄漏。
CComBSTR bstrLeak(L"Initialized");
HRESULT hr = OutString(&bstrLeak);
若要避免泄漏,请在作为 [out] 参数传递地址之前,对现有的 CComBSTR 对象调用 Empty 方法。
请注意,如果函数的参数是 [in, out],则同样的代码将不会导致泄漏
ComBSTR的使用的更多相关文章
- MSXML使用教程
在DOM接口规范中,有四个基本的接口:Document,Node,NodeList以及NamedNodeMap.在这四个基本接口中,Document接口是对文档进行操作的入口,它是从Node接口继承过 ...
- 欧拉工程第51题:Prime digit replacements
题目链接 题目: 通过置换*3的第一位得到的9个数中,有六个是质数:13,23,43,53,73和83. 通过用同样的数字置换56**3的第三位和第四位,这个五位数是第一个能够得到七个质数的数字,得到 ...
- sql查询行转列
昨天下午碰到一个需求,一个大约30万行的表,其中有很多重复行,在这些行中某些字段值是不重复的. 比如有ID,NAME,CONTRACT_id,SALES,PRODUCT等,除了PRODUCT字段,其余 ...
随机推荐
- css笔记 - 张鑫旭css课程笔记之 padding 篇
[padding地址](https://www.imooc.com/learn/710) 一.padding与容器尺寸之间的关系 padding会影响元素的尺寸(通常情况下是通过增加/挤压内容区域) ...
- 常见的mysql 进程state<转自网络>
Analyzing 线程是对MyISAM 表的统计信息做分析(例如, ANALYZE TABLE ). checking permissions 线程是检查服务器是否具有所需的权限来执行该语句. Ch ...
- 安装cnpm
使用淘宝镜像的cnpm $ npm install -g cnpm --registry=https://registry.npm.taobao.org
- async 与await
一. async 与await (https://segmentfault.com/a/1190000007535316) 1.async 是“异步”的简写,而 await 可以认为是 async w ...
- js的mime类型有哪些?
js中的mime类型 常见类型 扩展名 类型/子类型 txt text/plain doc application/msword exe application/octet-stream pdf ap ...
- python开发环境搭建(python3.3.2+wing IDE4.1)
1.下载python http://www.wingide.com/downloads下载最新版python 2.下载Wing IDE http://wingware.com/downloads/wi ...
- php时间戳和日期转换,以及时间戳和星期转换
$this->created_at为时间戳值,转换日期如下 date('m.d',$this->created_at) : y 代表年的后两位如 17 ,Y 代表 2017 , m 代 ...
- HOJ 2156 &POJ 2978 Colored stones(线性动规)
Colored stones Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1759 Accepted: 829 Descrip ...
- poj3264 balanced lineup【线段树】
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- ResourceManager High Availability
Introduction This guide provides an overview of High Availability of YARN’s ResourceManager, and det ...