Fourth step to implement IADD method
We will provide the implementation of all methods of interface IADD in this step.
Create a new file (addObj.cpp), add the following implementation code in this file.
///
//
//Addobj.cpp
// Contains the Method Implementations of the add interface
// Interfaces
//
#include
#include "addobj.h"
#include "Iadd_i.c"
HRESULT __STDCALL CADDOBJ :: setFirstNumber (long NX1)
{
m_nx1 = nx1;
IF (m_bislogenabled) Writetolog ("Junk");
Return S_OK;
}
HRESULT __STDCALL CADDOBJ :: setsecondnumber (long NX2)
{
m_nx2 = nx2;
Return S_OK;
}
HRESULT __STDCALL CADDOBJ :: DOTHEADDITION (long * pbuffer)
{
* pBuffer = m_nx1 m_nx2;
Return S_OK;
}
/
Step 5 Realize IUNKNOWN
The IunkNown method needs to be implemented. In file addobj.cpp, we will implement it three forced methods (addRef, release, and queryinterface). Private member variable m_nrefcount is used to maintain an object life, he is not directly self-added / minus, but we use thread safety methods to use the API function InterlockedIncrement and InterlocKedDecrement.
/
HRESULT __STDCALL CADDOBJ :: Queryinterface
Refiid RIID,
void ** ppobj)
{
IF (riid == iid_iunknown)
{
* ppobj = static_cast (this);
AddRef ();
Return S_OK;
}
IF (riid == iid_iadd)
{
* ppobj = static_cast (this);
AddRef ();
Return S_OK;
}
//
// if Control Reaches Here Then, Let the Client Know That
// We do not satisfy the Required Interface
//
* ppobj = NULL;
Return E_NOINTERFACE;
} // queryinterface method
Ulong __stdcall Caddobj :: AddRef ()
{
Return InterlockedIncrement (& M_NREFCOUNT);
}
Ulong __stdcall Caddobj :: release ()
{
Long nrefcount = 0;
NREFCOUNT = InterlockedDecrement (& m_nrefcount);
IF (NREFCOUNT == 0) delete this;
Return NREFCOUNT;
}
/
Sixth step factory
We have completed some of the features of the Add COM object. As a criterion for each COM, each COM object must have a separate ICLASSFActory implementation. Customers will use this interface to get an instance of our IADD interface implementation. The IclassFactory interface, like all other COM interfaces, inherits from IunkNown. So we must also provide an IUNKNOWN implementation, as well as the iClassFactory method (LockServer and CreateInstance). Create a new file (named addObjfactory.h). Declare a class CaddFactory and inherit itself from iClassFactory. ///
//Addobjfactory.h
// Contains the C Class Declarations for the iClassFactory Implementations
//
Class CaddFactory: Public ICLASSFACTORY
{
PUBLIC:
// Interface iunknown methods
HRESULT __STDCALL QueryInterface
Refiid RIID,
void ** ppobj);
Ulong __stdcall addref ();
Ulong __stdcall release ();
// Interface iClassFactoryMETHODS
HRESULT __STDCALL CREATEINSTANCE (IUNKNOWN * PunkNownouter,
Const IID & IID,
void ** ppv);
HRESULT __STDCALL LOCKSERVER (BOOL Block);
Private:
Long m_nrefcount;
}
/
COM Guide - STEP BY Step Com Tutorial - Shim (1) COM Guide (Step By Step CoM Tutorial - Shim (2)