[
Regular DLL using shared MFC DLL] 타입으로 새로운 프로젝트를 생성한다.
; SpcCom.def : Declares the module parameters for the DLL.
LIBRARY "SpcCom"
DESCRIPTION 'SpcCom Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
DllCanUnloadNow @1 PRIVATE
DllGetClassObject @2 PRIVATE
DllRegisterServer @3 PRIVATE
DllUnregisterServer @4 PRIVATE
CLSID_STR_CLASSID 는 스트링 리소스이다.
#include <afxctl.h>
BOOL CSpcComApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
// {
AfxOleLockApp();
return COleObjectFactory::RegisterAll();
// }
// return CWinApp::InitInstance();
}
int CSpcComApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
// {
AfxOleUnlockApp();
return 0;
// }
// return CWinApp::ExitInstance();
}
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return AfxDllGetClassObject(rclsid, riid, ppv);
}
STDAPI DllCanUnloadNow()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return AfxDllCanUnloadNow();
}
STDAPI DllRegisterServer()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleObjectFactory::UpdateRegistryAll();
return S_OK;
}
STDAPI DllUnregisterServer()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// Load up various strings from the resource stringtable
CString strClassID;
strClassID.LoadString(CLSID_STR_CLASSID);
CLSID CLSID_COMPONENT;
BSTR bstrClassID = strClassID.AllocSysString();
CLSIDFromString(bstrClassID, &CLSID_COMPONENT);
SysFreeString(bstrClassID);
// Unregister the class
return AfxOleUnregisterClass(CLSID_COMPONENT, "CLSID_COMPONENT");
}
import "wtypes.idl";
import "unknwn.idl";
[
object,
uuid(6B6FA1AE-26FB-4d88-A0D8-2C43ABCB6E02),
helpstring("ISimpleCal interface"),
pointer_default(unique)
]
interface ISimpleCal:IUnknown
{
HRESULT Sum ([in] int x, [in] int y, [out]int* z);
HRESULT Sub ([in] int x, [in] int y, [out]int* z);
};
//
// Class information for SimpleCal
//
[ uuid(60D754D5-DDBA-4d23-A84A-63A812AB2C49) ]
coclass COMPONENT
{
interface ISimpleCal;
};
#ifndef SIMPLECALC_H
#define SIMPLECALC_H
#include "..\..\Common\spccom2.h"
class CSimpleCalc : public CCmdTarget
{
public:
// Constructor
CSimpleCalc();
// Destructor
virtual ~CSimpleCalc();
protected:
BEGIN_INTERFACE_PART(CInnerSimpleCal, ISimpleCal)
STDMETHOD(Sum)(int x, int y, int* z);
STDMETHOD(Sub)(int x, int y, int* z);
END_INTERFACE_PART(CInnerSimpleCal)
DECLARE_INTERFACE_MAP()
DECLARE_OLECREATE(CSimpleCalc)
DECLARE_DYNCREATE(CSimpleCalc)
};
typedef CSimpleCalc* PSimpleCalc;
#endif
#include "stdafx.h"
#include "spccom1.h"
#include "..\..\Common\SpcCom2_i.c"
// Support dynamic creation
IMPLEMENT_DYNCREATE(CSimpleCalc, CCmdTarget)
// {60D754D5-DDBA-4d23-A84A-63A812AB2C49}
IMPLEMENT_OLECREATE(CSimpleCalc, "CLSID_COMPONENT",
0x60d754d5, 0xddba, 0x4d23, 0xa8, 0x4a, 0x63, 0xa8, 0x12, 0xab, 0x2c, 0x49);
BEGIN_INTERFACE_MAP(CSimpleCalc, CCmdTarget)
INTERFACE_PART(CSimpleCalc, IID_ISimpleCal, CInnerSimpleCal)
END_INTERFACE_MAP()
CSimpleCalc::CSimpleCalc()
{
AfxOleLockApp();
}
CSimpleCalc::~CSimpleCalc()
{
AfxOleUnlockApp();
}
// ISimpleCal
STDMETHODIMP_(ULONG)
CSimpleCalc::XCInnerSimpleCal::AddRef()
{
METHOD_PROLOGUE(CSimpleCalc, CInnerSimpleCal)
return pThis->ExternalAddRef();
}
STDMETHODIMP_(ULONG)
CSimpleCalc::XCInnerSimpleCal::Release()
{
METHOD_PROLOGUE(CSimpleCalc, CInnerSimpleCal)
return pThis->ExternalRelease();
}
STDMETHODIMP
CSimpleCalc::XCInnerSimpleCal::QueryInterface(REFIID riid, LPVOID* ppv)
{
METHOD_PROLOGUE(CSimpleCalc, CInnerSimpleCal)
return pThis->ExternalQueryInterface(&riid, ppv);
}
STDMETHODIMP
CSimpleCalc::XCInnerSimpleCal::Sum(int x, int y, int* z)
{
*z = x + y;
return S_OK;
}
STDMETHODIMP
CSimpleCalc::XCInnerSimpleCal::Sub(int x, int y, int* z)
{
*z = x - y;
return S_OK;
}
참조 사이트: