CComPtr选件类

托管COM接口指针的智能指针选件类。

template<
   class T 
>
class CComPtr

参数

  • T
    指定指针类型的COM接口将存储。

成员

ezzw7k98.collapse_all(zh-cn,VS.110).gif公共构造函数

名称

描述

CComPtr::CComPtr

构造函数。

ezzw7k98.collapse_all(zh-cn,VS.110).gif公共运算符

名称

描述

CComPtr::operator =

分配指向成员的指针。

备注

ATL使用 CComPtrCComQIPtr 管理COM接口指针。 两个从 CComPtrBase派生,因此,两个执行自动引用计数。

CComPtrCComQIPtr 选件类可帮助通过消除了内存泄漏自动引用计数。 以下功能两个执行逻辑相同操作;但是,请注意第二个版本如何可能不太容易出错的使用 CComPtr 选件类:

// Error-checking routine that performs manual lifetime management
// of a COM IErrorInfo object
HRESULT CheckComError_Manual()
{
   HRESULT hr;
   CComBSTR bstrDescription; 
   CComBSTR bstrSource; 
   CComBSTR bstrHelpFile; 

   IErrorInfo* pErrInfo = NULL; // naked COM interface pointer
   hr = ::GetErrorInfo(0, &pErrInfo);
   if(hr != S_OK)
      return hr;

   hr = pErrInfo->GetDescription(&bstrDescription); 
   if(FAILED(hr))
   {
      pErrInfo->Release();   // must release interface pointer before returning
      return hr;
   }

   hr = pErrInfo->GetSource(&bstrSource);
   if(FAILED(hr))
   {
      pErrInfo->Release();   // must release interface pointer before returning
      return hr;
   }

   hr = pErrInfo->GetHelpFile(&bstrHelpFile);
   if(FAILED(hr))
   {
      pErrInfo->Release();   // must release interface pointer before returning
      return hr;
   }

   pErrInfo->Release();      // must release interface pointer before returning
   return S_OK;
}
// Error-checking routine that performs automatic lifetime management
// of a COM IErrorInfo object through a CComPtr smart pointer object
HRESULT CheckComError_SmartPtr()
{
   HRESULT hr;
   CComBSTR bstrDescription; 
   CComBSTR bstrSource; 
   CComBSTR bstrHelpFile; 

   CComPtr<IErrorInfo> pErrInfo; 
   hr = ::GetErrorInfo(0, &pErrInfo);
   if(hr != S_OK)
      return hr;

   hr = pErrInfo->GetDescription(&bstrDescription); 
   if(FAILED(hr))
      return hr;

   hr = pErrInfo->GetSource(&bstrSource);
   if(FAILED(hr))
      return hr;

   hr = pErrInfo->GetHelpFile(&bstrHelpFile);
   if(FAILED(hr))
      return hr;

   return S_OK;
}   // CComPtr will auto-release underlying IErrorInfo interface pointer as needed

在调试版本中,代码跟踪链接atlsd.lib。

继承层次结构

CComPtrBase

CComPtr

要求

Header: atlbase.h

请参见

参考

CComPtr::CComPtr

CComQIPtr::CComQIPtr

其他资源

ATL选件类概述