I just want know what exact difference between ComPtr and CComPtr, and whether ComPtr::As() is analogue of CComPtr::QueryInterface()?
I read documentation of both, but there is no clear answer to the question...
ComPtr vs CComPtr, As vs QueryInterface
2.9k Views Asked by Olga Pshenichnikova At
2
There are 2 best solutions below
0
On
What's nice about those class is you have the source, in C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\winrt\wrl\client.h (adapt to your context and Visual Studio version):
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()));
}
...
};
So, yes, As basically calls QueryInterface underneath.
They are simply COM interface smart wrappers from different frameworks.
ComPtris part of the Windows Runtime C++ Template Library (WRL).CComPtris part of the Active Template Library (ATL) . They serve similar purposes for their respective frameworks - to provide automated reference counting and refcount-safe typecasting. But you should not mix them interchangeably. If you are writing WRL code, useComPtr. If you are writing ATL code, useCComPtr.Yes, because
As()simply callsQueryInterface()internally.