Using QueryInterface inside a COM
-
Hello I am working on a COM project in which i use Interface Inheritance. The IDL file looks like this, interface IEarthInterface : IUnknown {}; interface IOceanInterface : IEarthInterface {}; interface IPacificInterface : IOceanInterface {}; interface IAntarticInterface : IOceanInterface {}; interface IDataInterface : IUnknown {}; interface IFindInterface : IDataInterface { HRESULT GetOceanTypeInterface(IOceanInterface** pOceanInterface); }; From inside IFindInterface i need to figure out which Ocean is currently attached and then attach or typecast it was IOceanInterface and send it back to client. All i wanted to know is how to get a Interface pointer from inside another Interface. ie, can i call CoCreateInstance or QueryInterface from inside IFindInterface and get the Interface Pointer to IPacificInterface or IAntarticInterface. I been already told that CoCreateInstance will not work in this case. Any idea how to implement this? Thanks in advance.
-
Hello I am working on a COM project in which i use Interface Inheritance. The IDL file looks like this, interface IEarthInterface : IUnknown {}; interface IOceanInterface : IEarthInterface {}; interface IPacificInterface : IOceanInterface {}; interface IAntarticInterface : IOceanInterface {}; interface IDataInterface : IUnknown {}; interface IFindInterface : IDataInterface { HRESULT GetOceanTypeInterface(IOceanInterface** pOceanInterface); }; From inside IFindInterface i need to figure out which Ocean is currently attached and then attach or typecast it was IOceanInterface and send it back to client. All i wanted to know is how to get a Interface pointer from inside another Interface. ie, can i call CoCreateInstance or QueryInterface from inside IFindInterface and get the Interface Pointer to IPacificInterface or IAntarticInterface. I been already told that CoCreateInstance will not work in this case. Any idea how to implement this? Thanks in advance.
This should work:
IPacificInterface* pPacific = NULL; pOceanInterface->QueryInterface(NULL, IID_IPacificInterface, &pPacific);
If this fails, try all other possible derived interfaces until a call to QueryInterface succeeds. This is pretty much like using dynamic_cast to determine the type of object by pointer or reference to base.