How an Automation Consumer Retrieves an Error Object

When an Automation consumer retrieves an error object in response to a return code it received, the consumer makes the following calls:

  1. Calls QueryInterface on the component that returned the code to retrieve an ISupportErrorInfo interface pointer on that component. This interface must be supported by all components that create error objects.

  2. Calls the InterfaceSupportsErrorInfo method in this interface and passes it the IID of the interface containing the method that returned the code. The InterfaceSupportsErrorInfo method returns S_OK if the interface supports error objects (meaning the consumer should retrieve the current error object, if one exists) and S_FALSE if the interface does not support error objects (meaning the consumer should not retrieve the current error object because it applies to another interface and method).

  3. Calls the GetErrorInfo function in the Automation DLL. This function returns an IErrorInfo interface pointer on the current error object and releases its reference count on the error object, thus transferring ownership of the error object to the consumer.

  4. Calls the methods in the IErrorInfo interface to retrieve information from the error object.

  5. Calls Release on the error object to release it.

The following code shows an example of how an Automation consumer might retrieve and use an Automation error object:

#include <oledb.h>
IUnknown *pMyObject;
extern GUID IID_IMyInterface;

int main() {
 IErrorInfo    *perrinfo;
 ISupportErrorInfo *pserrinfo;
 HRESULT    hr;

 // Error occurs when calling method. (Not shown.) 
 //Check that the current interface supports error objects.
hr = pMyObject->QueryInterface(IID_ISupportErrorInfo, (void**) &pserrinfo);
 if (SUCCEEDED(hr)) {
  hr = pserrinfo->InterfaceSupportsErrorInfo(IID_IMyInterface);
  if (hr == S_OK) {
   // Get the current error object.
   GetErrorInfo(0, &perrinfo);

   // Use the returned IErrorInfo interface to retrieve error information.
   // (Not shown).

   // Release the error object.
   perrinfo->Release();
  }
 }
} ;