Figure 1    Dispatch Map Macros and Comments
 // This goes in the header file...
 class AClassWithAutomation : public CCmdTarget {
 •
 •
 •

DECLARE_DISPATCH_MAP() • • •
// Note from George Shepherd: ClassWizard looks for these comments: // Generated OLE dispatch map functions //{{AFX_DISPATCH(AClassWithAutomation) //}}AFX_DISPATCH }; // This goes in the source code file // Note: ClassWizard looks for these comments: BEGIN_DISPATCH_MAP(AClassWithAutomation, CCmdTarget) //{{AFX_DISPATCH_MAP(AClassWithAutomation) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_DISPATCH_MAP END_DISPATCH_MAP()

Figure 2   Adding Code to Handle QueryInterface


 // This goes in the header file:
 class AClassWithAutomation : public CCmdTarget {
 •
 •
 •
   // Declares the interface map 
    DECLARE_INTERFACE_MAP()
 •
 •
 •
};
 
 // This goes in the CPP file
 // Get the GUID from GUIDGEN
 // {160D011D-D22C-11D1-8CAA-E023E7FB752B}
 static const IID IID_IAClassWithAutomation =
 { 0x160d011d, 0xd22c, 0x11d1, { 0x8c, 0xaa, 0xe0, 0x23, 0xe7, 0xfb, 0x75, 0x2b } };
 
 BEGIN_INTERFACE_MAP(AClassWithAutomation, CCmdTarget)
    INTERFACE_PART(AClassWithAutomation, IID_IAClassWithAutomation, Dispatch)
 END_INTERFACE_MAP()

Figure 3   Adding the Class Object


 // This goes in the header...
 class AClassWithAutomation : public CCmdTarget {
 •
 •
 •
   DECLARE_DYNCREATE(AClassWithAutomation) 
    DECLARE_OLECREATE(AClassWithAutomation)
 •
 •
 •
};
 
 // This goes in the C++ source file...
 IMPLEMENT_DYNCREATE(AClassWithAutomation, CCmdTarget)
 IMPLEMENT_OLECREATE(AClassWithAutomation, 
                     "AClassWithAutomation", 
                     0x160d011e, 0xd22c, 
                     0x11d1, 0x8c, 
                     0xaa, 0xe0, 0x23, 
                     0xe7, 0xfb, 0x75, 0x2b)

Figure 4   Connecting to the DocTemplate


 // {160D0106-D22C-11D1-8CAA-E023E7FB752B}
 static const CLSID clsid =
 { 0x160d0106, 0xd22c, 0x11d1, { 0x8c, 0xaa, 0xe0, 0x23, 0xe7, 0xfb, 0x75, 0x2b } };
 BOOL CHasautoApp::InitInstance()
 {
 •
 •
 •
   CSingleDocTemplate* pDocTemplate;
    pDocTemplate = new CSingleDocTemplate(

       IDR_MAINFRAME,
       RUNTIME_CLASS(CHasautoDoc),
       RUNTIME_CLASS(CMainFrame),       // main SDI frame window
       RUNTIME_CLASS(CHasautoView));
    AddDocTemplate(pDocTemplate);
 
    // Connect the COleTemplateServer to the document template.
    // The COleTemplateServer adds class object support
    // to a document template...
    m_server.ConnectTemplate(clsid, pDocTemplate, TRUE);
 •
 •
 •
}


Figure 5   Registering Class Objects


 // If COM class is in an application
 BOOL CHasautoApp::InitInstance()
 {
    // Check to see if launched as OLE server
    if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
    {
       // Register all OLE server (factories) as running.  
       // This calls CoRegisterClassObject under the hood.
       COleTemplateServer::RegisterAll();
 
       // Application was run with /Embedding or /Automation.  Don't show the
       // main window in this case.
       return TRUE;
    }
 
    // When a server application is launched stand-alone, it is a good idea
    // to update the system registry in case it has been damaged.
    m_server.UpdateRegistry(OAT_DISPATCH_OBJECT);
    COleObjectFactory::UpdateRegistryAll();
 
    return TRUE;
 }

Figure 6   ODL File for Producing a Type Library


 [ uuid(160D0107-D22C-11D1-8CAA-E023E7FB752B), version(1.0) ]
 library Hasauto
 {
    importlib("stdole32.tlb");
 
    // Primary dispatch interface for AClassWithAutomation
    // Make sure this is the same GUID as is used in the INTERFACE MAP
    [ uuid(160D011D-D22C-11D1-8CAA-E023E7FB752B) ]
    dispinterface IAClassWithAutomation
    {
       properties:
          // NOTE - ClassWizard will maintain property information here.
          // Use extreme caution when editing this section.
          // {{AFX_ODL_PROP(AClassWithAutomation)
          // }}AFX_ODL_PROP
 
       methods:
          // NOTE - ClassWizard will maintain method information here.
          // Use extreme caution when editing this section.
          // {{AFX_ODL_METHOD(AClassWithAutomation)
          // }}AFX_ODL_METHOD
   };
 
    //  Class information for AClassWithAutomation
 
    // ATTENTION: This GUID must match the class ID as stated
    // in the IMPLEMENT_OLECREATE macro in the CPP file...
    [ uuid(160D011E-D22C-11D1-8CAA-E023E7FB752B) ]
    coclass AClassWithAutomation
    {
       [default] dispinterface IAClassWithAutomation;
    };
 
    //{{AFX_APPEND_ODL}}
    //}}AFX_APPEND_ODL}}
 };