QueryInterface() is used to obtain an interface pointer of an another class from the exiting interface pointer.
Returns a pointer to a specified interface on an object to which a client currently holds an interface pointer. This function must call IUnknown::AddRef on the pointer it returns.

HRESULT QueryInterface(
  REFIID iid,        //Identifier of the requested interface
  void ** ppvObject  //Address of output variable that receives the
                     //interface pointer requested in iid


  • iid - [in] Identifier of the interface being requested.
  • ppvObject - [out] Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, *ppvObject contains the requested interface pointer to the object. If the object does not support the interface specified in iid, *ppvObject is set to NULL.

Return Value

  • S_OK if the interface is supported, E_NOINTERFACE if not.

QueryInterface source

STDMETHODIMP CMathLib::QueryInterface(REFIID  iid, LPVOID* ppvObject)
  if ( iid == IID_IUnknown) {
    *ppvObject = dynamic_cast( this );
  } else 
  if ( iid == IID_IDispatch) {
    *ppvObject = dynamic_cast( this );
  } else
  if ( iid == IID_IMathLib) {
    *ppvObject = dynamic_cast( this );
  }else {
    // It didn't match an interface
    *ppvObject = NULL;
    return E_NOINTERFACE;
  return S_OK;

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.