一、实现ISmipleMath,IAdvancedMath接口和DllGetClassObject()
1.1 实现ISmipleMath和IAdvancedMath接口
让我们将原来的CMath 类(CMath其实就是"COM技术初探(二)COM基础知识"里的那个CMath类)修改来实现ISmipleMath接口和IAdvancedMath接口。
修改的地方如下:
1) Math.h文件 /*@**#---2003-10-29 21:33:44 (tulip)---#**@
2) Math.cpp文件
#include "interface.h"*/
#include "MathCOM.h"//新增加的,以替换上面的东东
class CMath : public ISimpleMath,
public IAdvancedMath
{
private:
ULONG m_cRef;
private:
int calcFactorial(int nOp);
int calcFabonacci(int nOp);
public:
CMath();
//IUnknown Method
STDMETHOD(QueryInterface)(REFIID riid, void **ppv);
STDMETHOD_(ULONG, AddRef)();
STDMETHOD_(ULONG, Release)();
// ISimpleMath Method
STDMETHOD (Add)(int nOp1, int nOp2,int * pret);
STDMETHOD (Subtract)(int nOp1, int nOp2,int *pret);
STDMETHOD (Multiply)(int nOp1, int nOp2,int *pret);
STDMETHOD (Divide)(int nOp1, int nOp2,int * pret);
// IAdvancedMath Method
STDMETHOD (Factorial)(int nOp,int *pret);
STDMETHOD (Fabonacci)(int nOp,int *pret);
};/*@**#---2003-10-29 21:32:35 (tulip)---#**@
#include "math.h"
STDMETHODIMP CMath::QueryInterface(REFIID riid, void **ppv)
{// 这里这是实现dynamic_cast的功能,但由于dynamic_cast与编译器相关。
if(riid == IID_ISimpleMath)
*ppv = static_cast<ISimpleMath *>(this);
else if(riid == IID_IAdvancedMath)
*ppv = static_cast<IAdvancedMath *>(this);
else if(riid == IID_IUnknown)
*ppv = static_cast<ISimpleMath *>(this);
else {
*ppv = 0;
return E_NOINTERFACE;
}
//这里要这样是因为引用计数是针对组件的
reinterpret_cast<IUnknown *>(*ppv)->AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG) CMath::AddRef()
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) CMath::Release()
{
// 使用临时变量把修改后的引用计数值缓存起来
ULONG res = --m_cRef;
// 因为在对象已经销毁后再引用这个对象的数据将是非法的
if(res == 0)
delete this;
return res;
}
STDMETHODIMP CMath::Add(int nOp1, int nOp2,int * pret)
{
*pret=nOp1+nOp2;
return S_OK;
}
STDMETHODIMP CMath::Subtract(int nOp1, int nOp2,int * pret)
{
*pret= nOp1 - nOp2;
return S_OK;
}
STDMETHODIMP CMath::Multiply(int nOp1, int nOp2,int * pret)
{
*pret=nOp1 * nOp2;
return S_OK;
}
STDMETHODIMP CMath::Divide(int nOp1, int nOp2,int * pret)
{
*pret= nOp1 / nOp2;
return S_OK;
}
int CMath::calcFactorial(int nOp)
{
if(nOp <= 1)
return 1;
return nOp * calcFactorial(nOp - 1);
}
STDMETHODIMP CMath::Factorial(int nOp,int * pret)
{
*pret=calcFactorial(nOp);
return S_OK;
}
int CMath::calcFabonacci(int nOp)
{
if(nOp <= 1)
return 1;
return calcFabonacci(nOp - 1) + calcFabonacci(nOp - 2);
}
STDMETHODIMP CMath::Fabonacci(int nOp,int * pret)
{
*pret=calcFabonacci(nOp);
return S_OK;
}
CMath::CMath()
{
m_cRef=0;
}