How an Automation Component Returns an Error Object

When a component using Automation causes an error, it can return an object to describe that error. To do this, the component makes the following calls:

  1. Calls CreateErrorInfo in the Automation DLL. This function creates an Automation error object that exposes ICreateErrorInfo and IErrorInfo.

  2. Calls the methods in ICreateErrorInfo to store information in the error object, such as the string describing the error and the GUID of the interface that caused the error.

  3. Calls QueryInterface to retrieve the IErrorInfo interface pointer on the error object. This interface pointer will identify the error object to all Automation components.

  4. Calls SetErrorInfo in the Automation DLL and passes it the IErrorInfo interface pointer. SetErrorInfo replaces its current error object, if any, with the new error object and adds a reference count to the new error object.

  5. Calls Release to release its reference count on the error object. This effectively transfers ownership of the error object from the component that caused the error to the Automation DLL.

The following code shows an example of how an Automation component might create an Automation error object and transfer ownership of the object to the Automation DLL.

#include <oledb.h> 
int main() {
 ICreateErrorInfo *pcerrinfo;
 IErrorInfo   *perrinfo;

 // Error occurs in a method in the provider. (Not shown.) 
 //Create an Automation error object.
 CreateErrorInfo(&pcerrinfo);

 // Use the returned ICreateErrorInfo interface pointer to add error information to
 // the object. (Not shown.)

 // Retrieve an IErrorInfo interface pointer on the object and call SetErrorInfo to
 // pass the error object to the Automation DLL.
 pcerrinfo->QueryInterface(IID_IErrorInfo, (void**)&perrinfo);

 SetErrorInfo(0, perrinfo);

 // Release the interface pointers on the object to finish transferring ownership of
 // the object to the Automation DLL.
 perrinfo->Release();
 pcerrinfo->Release();
} ;