A preliminary study of COM Technology (III): a real com

Source: Internet
Author: User
Tags count implement cmath

First, implement Ismiplemath,iadvancedmath interface and DllGetClassObject ()

1.1 Implement Ismiplemath and Iadvancedmath interface

Let's change the original Cmath class (Cmath is actually "COM technology (ii) COM basics" of that Cmath Class) to implement Ismiplemath interface and Iadvancedmath interface.

The modifications are as follows:

1) Math.h File/*@**#---2003-10-29 21:33:44 (tulip)---#**@
#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);
};
2) Math.cpp file/*@**#---2003-10-29 21:32:35 (tulip)---#**@
#include "math.h"
STDMETHODIMP Cmath::queryinterface (REFIID riid, void **PPV)
{//Here This is the functionality to implement dynamic_cast, but because dynamic_cast is related to the compiler.
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;
}
This is because the reference count is for the component
Reinterpret_cast<iunknown *> (*PPV)->addref ();
return S_OK;
}
Stdmethodimp_ (ULONG) cmath::addref ()
{
return ++m_cref;
}
Stdmethodimp_ (ULONG) cmath::release ()
{
Use a temporary variable to cache the modified reference count value
ULONG res =--m_cref;
Because it is illegal to refer to the object's data after the object has been destroyed.
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::D ivide (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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.