접기
import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(1B00C2AF-80C4-442E-B55F-38700BE57A37),
dual,
helpstring("ISimpleCalc Interface"),
pointer_default(unique)
]
interface ISimpleCalc : IDispatch
{
[propput, id(1), helpstring("property xyVar")] HRESULT xyVar([in] int x, [in] int y);
[propget, id(2), helpstring("property Sum")] HRESULT Sum([out, retval] short *pVal);
...
[propget, id(5), helpstring("property Div")] HRESULT Div([out, retval] float *pVal);
};
#ifndef __SIMPLECALC_H_
#define __SIMPLECALC_H_
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CSimpleCalc
class ATL_NO_VTABLE CSimpleCalc :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CSimpleCalc, &CLSID_SimpleCalc>,
public ISupportErrorInfo,
public IDispatchImpl<ISimpleCalc, &IID_ISimpleCalc, &LIBID_CALSRVLib>
{
public:
CSimpleCalc()
{
// {
m_xVar = 0;
m_yVar = 0;
// }
}
...
BEGIN_COM_MAP(CSimpleCalc)
COM_INTERFACE_ENTRY(ISimpleCalc)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()
// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
// {
// ISimpleCalc
public:
STDMETHOD(get_Div)(/*[out, retval]*/ float *pVal);
...
STDMETHOD(get_Sum)(/*[out, retval]*/ short *pVal);
STDMETHOD(put_xyVar)(/*[in]*/ int x, /*[in]*/ int y);
protected:
int m_yVar;
int m_xVar;
// }
};
#endif //__SIMPLECALC_H_
#include "CalSrv.h"
#include "SimpleCalc.h"
...
STDMETHODIMP CSimpleCalc::InterfaceSupportsErrorInfo(REFIID riid) { static const IID* arr[] = { &IID_ISimpleCalc }; for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++) { if (InlineIsEqualGUID(*arr[i],riid)) return S_OK; } return S_FALSE; }
STDMETHODIMP CSimpleCalc::put_xyVar(int x, int y)
{
// TODO: Add your implementation code here
m_xVar = x;
m_yVar = y;
return S_OK;
}
STDMETHODIMP CSimpleCalc::get_Sum(short *pVal)
{
// TODO: Add your implementation code here
*pVal = m_xVar + m_yVar;
return S_OK;
}
...
STDMETHODIMP CSimpleCalc::get_Div(float *pVal)
{
// TODO: Add your implementation code here
if(m_yVar == 0)
{
ICreateErrorInfo *pCreateErrInfo;
IErrorInfo* pErrInfo = NULL;
HRESULT hr = ::CreateErrorInfo (&pCreateErrInfo);
if(SUCCEEDED(hr))
{
pCreateErrInfo->SetSource(OLESTR("CalSrv.SimpleCalc.1"));
pCreateErrInfo->SetDescription(OLESTR("분모가 0이 아닌 값을 입력하십시요."));
hr = pCreateErrInfo->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &pErrInfo);
if (SUCCEEDED(hr))
{
SetErrorInfo (0, pErrInfo);
pErrInfo->Release();
}
pCreateErrInfo->Release();
}
return E_INVALIDARG;
}
*pVal = (float) m_xVar / m_yVar;
return S_OK;
}
접기