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字段,其余 ...
随机推荐
- 怎样用SQL语句查看查询的性能指标
一.SET STATISTICS IO (有关TSQL语句查询所产生的磁盘活动量) 扫描计数:在查询中涉及到的表被访问的次数: 逻辑读取:从数据缓冲中读取的数据页数: 物理读取:从物理磁盘中往缓冲读 ...
- mariadb修改root密码的方法
mariadb安装好后,root密码为空,可以先使用HeidiSQL链接到数据库,执行以下sql,就可以修改root的密码了 update mysql.user set password=passwo ...
- 监控redis服务器执行的命令--类似于tomcat的local-access.log
一.问题由来 一般程序启动时会去拉必要的缓存存进去redis. 由于我们这边开发可直连开发和测试环境,有时候会发生,开发同学本地直连了测试环境,本地ide一启动,可能会导致重新覆盖了测试环境上的缓存. ...
- 在Linux下面的某一个文件的查找命令
借鉴文章:https://www.kafan.cn/edu/60044166.html Linux查找包含特定字符串的文件名的方法:http://www.jbxue.com/LINUXjishu/97 ...
- 如何在Computer下添加System Folder
1.创建一个GUID标识该系统目录,比如:{7854FF7A-470F-4D04-9FC5-4CFC7B2A0E89}.下面的操作步骤将全部使用这个示例GUID. 2.打开注册表编辑器(Registr ...
- Apache服务器301重定向去掉.html和.php
在做优化网站的时候,会考虑到网站整站的集权: 考虑到网站可以生成静态,首先,让网站优先访问 index.html 之后考虑:去掉 .html 和 .php. 利用 .htaccess <IfMo ...
- PAT甲1038 Recover the smallest number
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- HDU 4734 - F(x) - [数位DP][memset优化]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 Time Limit: 1000/500 MS (Java/Others) Memory Lim ...
- Ubuntu:14.04.2 安装多个Linux内核
http://blog.csdn.net/ddk3001/article/details/47340119 安装Ubuntu 14.04.2 后,内核是 3.16.0-30-generic 1.虚 ...
- python数据结构之堆栈
本篇我以堆栈的数据类型和操作方法两个方面总结学习笔记 堆栈(Stack) 一种后进先出(LIFO)的线性数据结构,对堆栈的插入和删除操作都只能在栈顶(top)进行. 堆栈可以通过数组和链表轻松实现 一 ...