Step 7 Implementation of ICLASSFACTORY
Implement the method of CADDFACTORY. Create a new file (AddObjFactory.cpp). Provides method implementation of class iunknown and iClassFactory. AddRef, Release, and QueryInterface method implementations are basically the same as those in previous class Caddobj. In method CreateInstance, class CADDOBJ is instantiated and transmits back to its interface pointer. The LockServer method does not give the implementation of the details.
HRESULT __STDCALL CADDFAACTORY :: CreateInstance (IUNKNOWN * PUNKNOWNOUTER,
Const IID & IID,
void ** ppv)
{
//
// this Method Lets The Client Manufacture Components EN MASSE
// The class factory provodes a mechanism to control the way
// The component is created. within the class factory the
// author of the component allowing to selective enable
// or disable code as per license agreements
//
//
// canNot Aggregate.
IF (PunkNownouter! = NULL)
{
RETURN CLASS_E_NOAGGREGATION;
}
//
// CREATE AN Instance of The Component.
//
CADDOBJ * POBJECT = New CADDOBJ;
IF (POBJECT == NULL)
{
Return E_OUTOFMEMORY;
}
//
// Get The Requested Interface.
//
Return POBJECT-> QueryInterface (IID, PPV);
}
HRESULT __STDCALL CADDFAACTORY :: LOCKSERVER (Bool Block)
{
RETURN E_NOTIMPL;
}
/
Eighth step to realize the method of dllgetClassObject
A COM object in a process is just a simple Win32DLL, and he follows the established agreement. Each COM DLL must have an exit function through the name DllgetClassObject. The client will call this function to get the interface of the class factory (IUNKNOWN or ICLASSFAACTORY), and then call the CreateInstance method. Create a new file (exports.cpp) to implement DllgetClassObject. (code show as below)
Stdapi DllgetClassObject (Const CLSID & CLSID,
Const IID & IID,
void ** ppv)
{
//
// Check if the request COM Object Is Implement in this DLL
// there can be more Than 1 Com Object Implement in A DLL
//
IF (CLSID == CLSID_ADDOBJECT)
{
//
// iid specifies the Requested Interface for the factory Object
// The Client Can Request for iunknown, iClassFactory,
// iClassFactory2 //
CADDFACTORY * PADDFACT = New CaddFactory;
IF (paddfact == null)
Return E_OUTOFMEMORY;
Else
{
Return PaddFact-> QueryInterface (IID, PPV);
}
}
//
// if Control Reaches Here That Implies That The Object
// specified by the user is not implement in this DLL
//
Return Class_e_classNotavailable;
}
/
Ninth step to achieve DLLCanunloadNow
Customers need to know when COM DLL can be uninstalled from memory. Further, a COM object displayed in a process is displayed in the process space of the client program in the memory by calling the API function loadingLibrary. This display loaded DLL can also be uninstalled using FreeELibrary. COM customers must know when DLL can be securely uninstalled. A customer must determine that there is no instance from a COM object from especially in the DLL is still in the life. To make this possible calculation, in a COM DLL, we have a global variable (g_ncomobjsinuse) in the constructor of CADDOBJ and CaddFactory. Similarly, in the destructor, the global variable is reduced.
We output another function of the other COM: DLLCanunloadedNow, implemented as follows:
StDAPI DLLCanunloadNow ()
{
//
// a DLL IS NO LONGERIIN Uses Uses US Not Managing Any EXIXI OBJECTS
// (The Reference Count On All of Its Objects IS 0).
// We will Examine the value of g_ncomobjsinuse
//
IF (g_ncomobjsinuse == 0)
{
Return S_OK;
}
Else
{
Return s_false;}
}
Step by step com tutorial - STEP BY Step Com Tutorial - STEP BY Step COM TUTORALIAL - Under (2)