Adding Run-Time Type Library Support to MFC OLE Automation Servers

OLE Automation servers created with the MFC AppWizard and ClassWizard do not make type library information available at run-time. This is a shame, since the code and data are quite available. This technical tip describes the steps required to add run-time type library support.

The Step-by-Step How-To

This assumes you have already created a DLL or EXE with AppWizard, then created one or more OLE Automation classes derived from CCmdTarget using the ClassWizard. To add run-time type library support, do the following

  1. Add the GetDispatchIID member function to the CCmdTarget-derived classes.
  2. Add the DECLARE_OLETYPELIB to the CCmdTarget-derived class declarations.
  3. Use the IMPLEMENT_OLETYPELIB macros to provide definitions for the member functions declared in the previous step.
  4. Add a call to EnableTypeLib in the constructor of the CCmdTarget-derived classes.

It is also very helpful to register the type library when the server registers, since many development tools use the statically registered type library information to optimize access to the server classes. In particular, parameter and return value types can be checked at design time, and dual interfaces can be used. (For more information on adding support for dual interfaces to MFC OLE Automation servers, see Visual C++ TN065 Dual-Interface Support for OLE Automation Servers.)

  1. Add a call to AfxOleRegisterTypeLib from the DllRegisterServer function created by the AppWizard for a DLL, or from the InitInstance member function of the application class created for an EXE. In the latter case, only call this when the /register command-line argument has been specified.
  2. Add VERINFO VALUE "OLESelfRegister", "\0" to the resource file.

To increase the usefulness of the type library, you might want to add version and helpstring clauses to the interfaces and classes defined in the ODL file.


Copyright © 1996 Scott Nichol.
27-Dec-96