ComBSTR的使用

用 CComBSTR 进行编程

Visual Studio .NET 2003
 
3(共 3)对本文的评价是有帮助 评价此主题
 

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],则同样的代码将不会导致泄漏

原文地址:https://www.cnblogs.com/Dennis-mi/p/3461439.html