我只想知道ComPtr和CComPtr到底有什么不同,ComPtr::As()是否类似于CComPtr::QueryInterface()?我看了这两份文件,但问题没有明确的答案.
发布于 2020-03-06 17:57:58
ComPtr和CComPtr到底有什么区别?
它们只是来自不同框架的COM接口智能包装器。ComPtr是Windows C++模板库(WRL)的一部分。CComPtr是活动模板库(ATL)的一部分。它们为各自的框架提供了类似的用途--提供自动引用计数和重新计数--安全的类型转换。但你不应该把它们混在一起。如果您正在编写WRL代码,请使用ComPtr。如果您正在编写ATL代码,请使用CComPtr。
CComPtr::As()是否类似于CComPtr::QueryInterface()?
是的,因为As()只是在内部调用QueryInterface()。
发布于 2020-03-06 09:25:07
这些类的好处是您有了C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\winrt\wrl\client.h中的源代码(适应上下文和Visual版本):
template <typename T>
class ComPtr
{
public:
typedef T InterfaceType;
...
// query for U interface
template<typename U>
HRESULT As(_Inout_ Details::ComPtrRef<ComPtr<U>> p) const throw()
{
return ptr_->QueryInterface(__uuidof(U), p);
}
// query for U interface
template<typename U>
HRESULT As(_Out_ ComPtr<U>* p) const throw()
{
return ptr_->QueryInterface(__uuidof(U), reinterpret_cast<void**>(p->ReleaseAndGetAddressOf()));
}
// query for riid interface and return as IUnknown
HRESULT AsIID(REFIID riid, _Out_ ComPtr<IUnknown>* p) const throw()
{
return ptr_->QueryInterface(riid, reinterpret_cast<void**>(p->ReleaseAndGetAddressOf()));
}
...
};所以,是的,As基本上是在下面调用QueryInterface。
https://stackoverflow.com/questions/60560832
复制相似问题