CComPtrpInterface; HRESULT hr = CoCreateInstance(CLSID_MyObject, NULL, CLSCTX_INPROC_SERVER, IID_IMyInterface, (void**)&pInterface); if (SUCCEEDED(hr)) { // Use CComQIPtr to query for another interface CComQIPtr pOtherInterface(pInterface); if (pOtherInterface) { // Interface pointer is valid, use it... } }
CComPtrIn this example, CComQIPtr is used to simplify the process of casting an IUnknown interface pointer to an IMyInterface pointer. The CComQIPtr class is defined in the atlcomcli.h header file, which is part of the ATL library in Visual C++.pUnknown; // ... CComQIPtr pInterface(pUnknown); // ...