_com_ptr_t 用于定义智能指针类型。例如,让我们定义 IHTMLDocument3Ptr 类型:
typedef _com_ptr_t <_com_IIID<IHTMLDocument3, &__uuidof(IHTMLDocument3)>> IHTMLDocument3Ptr;
有一个简单的宏:
_COM_SMARTPTR_TYPEDEF(IHTMLDocument3, IID_IHTMLDocument3);
这将创建 IHTMLDocument3Ptr,一个智能指针:
IHTMLDocument3Ptr htmlDocument3;
使用 CComQIPtr 这将被定义为:
CComQIPtr<IHTMLDocument3> htmlDocument3;
有一个“comdefsp.h”文件,其中包含许多 COM 接口的预定义智能指针(https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-headers/include/comdefsp.h https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-headers/include/comdefsp.h)。 “comdef.h”文件自动包含它。例如,已经定义了 IDispatch 的智能指针:
IDispatchPtr dispatch;
使用 CComPtr 这将被定义为:
CComPtr<IDispatch> dispatch;
_com_ptr_t = 无 ATL
使用的优点_com_ptr_t
over CComPtr/CComQIPtr
就是它您不必链接到 ATL 库.
其他不需要 ATL 库的智能 COM 指针是_bstr_t
(相当于CComBSTR
) and _variant_t
(相当于CComVariant
).