Adding Automation To do to the App ================ Step 1) In InitInstance add: // Initialize OLE libraries if (!AfxOleInit()) { AfxMessageBox("Failed to initialize OLE"); return FALSE; } // Parse the command line to see if launched as OLE server if (RunEmbedded() || RunAutomated()) { // Register all OLE server (factories) as running. This enables the // OLE libraries to create objects from other applications. COleTemplateServer::RegisterAll(); } else { // When a server application is launched stand-alone, it is a good idea // to update the system registry in case it has been damaged. COleObjectFactory::UpdateRegistryAll(); } Step 2) Derive a class from CCmdTarget. In the .h file add the following maps: DECLARE_DYNCREATE( ) DECLARE_MESSAGE_MAP() DECLARE_OLECREATE( ) // Generated OLE dispatch map functions //{{AFX_DISPATCH( ) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_DISPATCH DECLARE_DISPATCH_MAP() DECLARE_INTERFACE_MAP() In the *.cpp file flesh out the maps with: IMPLEMENT_DYNCREATE( , CCmdTarget) BEGIN_MESSAGE_MAP( , CCmdTarget) //{{AFX_MSG_MAP( ) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP() BEGIN_DISPATCH_MAP(, CCmdTarget) //{{AFX_DISPATCH_MAP() // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_DISPATCH_MAP END_DISPATCH_MAP() // {6DDC0788-3886-4185-A6C5-A5A52284EEA6} static const IID IID_IRep = { 0x6ddc0788, 0x3886, 0x4185, { 0xa6, 0xc5, 0xa5, 0xa5, 0x22, 0x84, 0xee, 0xa6 } }; BEGIN_INTERFACE_MAP( , CCmdTarget) INTERFACE_PART( , IID_IRep, Dispatch) END_INTERFACE_MAP() // The IMPLEMENT_OLECREATE2 macro is defined in StdAfx.h of this project // {7BF0E457-3B7F-4A4C-A871-B43496A47945} IMPLEMENT_OLECREATE2( , ".Application", 0x7bf0e457, 0x3b7f, 0x4a4c, 0xa8, 0x71, 0xb4, 0x34, 0x96, 0xa4, 0x79, 0x45) In the constructor add: EnableAutomation(); // To keep the application running as long as an automation // object is active, the constructor calls AfxOleLockApp. AfxOleLockApp(); In the destructor add: AfxOleUnlockApp(); Add the function: //virtual void OnFinalRelease();/* *.h file def.*/ void ::OnFinalRelease()/* *.cpp file def.*/ { // When the last reference for an automation object is released // OnFinalRelease is called. The base class will automatically // deletes the object. Add additional cleanup required for your // object before calling the base class. CCmdTarget::OnFinalRelease(); } In stdafx.h define the macro: #ifndef IMPLEMENT_OLECREATE2 #define IMPLEMENT_OLECREATE2(class_name, external_name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ AFX_DATADEF COleObjectFactory class_name::factory(class_name::guid, \ RUNTIME_CLASS(class_name), TRUE, _T(external_name)); \ const AFX_DATADEF GUID class_name::guid = \ { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }; #endif // IMPLEMENT_OLECREATE2 Step 3) Add a *.odl file: // rep.odl : type library source for rep.exe // This file will be processed by the MIDL compiler to produce the // type library (rep.tlb). [ uuid(8D12A9D8-D0E2-429A-8AAE-DBE7D53F3176), version(1.0) ] library Rep { importlib("stdole32.tlb"); importlib("stdole2.tlb"); // Primary dispatch interface for CRepDoc [ uuid(6DDC0788-3886-4185-A6C5-A5A52284EEA6) ] dispinterface IRep { properties: // NOTE - ClassWizard will maintain property information here. // Use extreme caution when editing this section. //{{AFX_ODL_PROP(CRepDlgAutoProxy) //}}AFX_ODL_PROP methods: // NOTE - ClassWizard will maintain method information here. // Use extreme caution when editing this section. //{{AFX_ODL_METHOD(CRepDlgAutoProxy) //}}AFX_ODL_METHOD }; // Class information for CRepDoc [ uuid(7BF0E457-3B7F-4A4C-A871-B43496A47945) ] coclass Rep { [default] dispinterface IRep; }; //{{AFX_APPEND_ODL}} //}}AFX_APPEND_ODL}} }; Step 4) In the post build step add the following to register your app: "$(TargetPath)" /RegServer Step 5) It's a good idea to have a reg file so that any setup program can register your app. This is also carried out in InitInstance of the app, but needed prior to first execution: REGEDIT HKEY_CLASSES_ROOT\.Application = Application HKEY_CLASSES_ROOT\.Application\CLSID = {7BF0E457-3B7F-4A4C-A871-B43496A47945} HKEY_CLASSES_ROOT\CLSID\{7BF0E457-3B7F-4A4C-A871-B43496A47945} = Application HKEY_CLASSES_ROOT\CLSID\{7BF0E457-3B7F-4A4C-A871-B43496A47945}\ProgId = .Application HKEY_CLASSES_ROOT\CLSID\{7BF0E457-3B7F-4A4C-A871-B43496A47945}\LocalServer32 = .EXE