ActiveX/Automation

In Process, MFC

jjryu 2008. 12. 20. 00:33
1. [MFC AppWizard (dll)]로 프로젝트 생성한다. 이때 [Automation]을 체크

2. IDispatch 인터페이스를 구현할 클래스 생성
[Insert | New Class] 메뉴를 선택해서 [Class Type]은 'MFC Class', [Base class]는 CCmdTarget 그리고 [Automation] 항목에서 [Creatable by type ID]를 선택해서 생성

3. 생성된 인터페이스의 컨텍스트 메뉴에서 [Add Method]를 선택해서 메소드 추가


BOOL CCalcServerApp::InitInstance()
{
    // Register all OLE server (factories) as running.  This enables the
    //  OLE libraries to create objects from other applications.
    COleObjectFactory::RegisterAll();

    return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// Special entry points required for inproc servers

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    return AfxDllGetClassObject(rclsid, riid, ppv);
}

STDAPI DllCanUnloadNow(void)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    return AfxDllCanUnloadNow();
}

// by exporting DllRegisterServer, you can use regsvr.exe
STDAPI DllRegisterServer(void)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    COleObjectFactory::UpdateRegistryAll();
    return S_OK;
}


    //  Primary dispatch interface for CCalculator
   
    [ uuid(DDACE705-44F7-40E5-A991-A9137293DC36) ]
    dispinterface ICalculator
    {
        properties:
            // NOTE - ClassWizard will maintain property information here.
            //    Use extreme caution when editing this section.
            //{{AFX_ODL_PROP(CCalculator)
            //}}AFX_ODL_PROP
           
        methods:
            // NOTE - ClassWizard will maintain method information here.
            //    Use extreme caution when editing this section.
            //{{AFX_ODL_METHOD(CCalculator)
            [id(1)] long Add(long a, long b);
            ...
            //}}AFX_ODL_METHOD

    };

    //  Class information for CCalculator

    [ uuid(95061FF7-14A4-4F6E-823D-DA2DC929B113) ]
    coclass Calculator
    {
        [default] dispinterface ICalculator;
    };


class CCalculator : public CCmdTarget
{
    DECLARE_DYNCREATE(CCalculator)

    CCalculator();           // protected constructor used by dynamic creation

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CCalculator)
    public:
    virtual void OnFinalRelease();
    //}}AFX_VIRTUAL

// Implementation
protected:
    virtual ~CCalculator();

    // Generated message map functions
    //{{AFX_MSG(CCalculator)
        // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
    DECLARE_OLECREATE(CCalculator)

    // Generated OLE dispatch map functions
    //{{AFX_DISPATCH(CCalculator)
    afx_msg long Add(long a, long b);
    //}}AFX_DISPATCH
    DECLARE_DISPATCH_MAP()
    DECLARE_INTERFACE_MAP()
};


CCalculator::CCalculator()
{
    EnableAutomation();
   
    // To keep the application running as long as an OLE automation
    //    object is active, the constructor calls AfxOleLockApp.
   
    AfxOleLockApp();
}

CCalculator::~CCalculator()
{
    // To terminate the application when all objects created with
    //     with OLE automation, the destructor calls AfxOleUnlockApp.
   
    AfxOleUnlockApp();
}

void CCalculator::OnFinalRelease()
{
    // 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();
}

...
BEGIN_DISPATCH_MAP(CCalculator, CCmdTarget)
    //{{AFX_DISPATCH_MAP(CCalculator)
    DISP_FUNCTION(CCalculator, "Add", Add, VT_I4, VTS_I4 VTS_I4)
    //}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
...
static const IID IID_ICalculator =
{ 0xe3a7e4c9, 0x77c1, 0x414a, { 0x9c, 0xc4, 0x8f, 0x36, 0x77, 0x9f, 0x58, 0x63 } };

BEGIN_INTERFACE_MAP(CCalculator, CCmdTarget)
    INTERFACE_PART(CCalculator, IID_ICalculator, Dispatch)
END_INTERFACE_MAP()

// {16E66ECA-E097-41C8-9816-7E4187F02FC1}
IMPLEMENT_OLECREATE(CCalculator, "CalcServer.Calculator", 0x16e66eca, 0xe097, 0x41c8, 0x98, 0x16, 0x7e, 0x41, 0x87, 0xf0, 0x2f, 0xc1)
...
long CCalculator::Add(long a, long b)
{
    // TODO: Add your dispatch handler code here
    return a + b;
//    return 0;
}