OOPortal OOPortal

Basic COM   «Prev 

In-process COM Servers and DllGetClassObject

struct CCLassFactory01: public IClassFactory{
  ULONG m_refcnt;
  //IU Methods

Lines 2-16: These declare and implement a class factory for O1. IClassFactory, like all COM interfaces, must implement IUnknown as its first three methods. Following that are the methods of IClassFactory. CreateInstance and LockServer were covered earlier in this module.
Lines 18 and 19: We use the standard declaration of DllGetClassObject. REFCLSID and REFIID are macros that preprocess to "const CLSID&"and "const IID& when compiling in C++; LPVOID is VOID *.
Line 22: We check to see if this server supports the COM object by checking rclsid. If the requested COM object is not supported, CLASS_E_CLASSNOTAVAILABLE is returned (line 33).
Lines 23 and 24: An instance of the class factory for the requested COM class is instantiated.
Line 26: A call is made to IClassFactory::QueryInterface to ask the newly created class factory if it supports the requested object creation interface in parameter riid. Normally this is IID_IClassFactory. QueryInterface will also call AddRef if the requested interface is supported.
Line 27: A check of the return status of the IClassFactory::QueryInterface call.
Lines 28 nd 29: Executed if the caller (COM) asked for an object creation interface not support by the class factory. The only reason this would fail (barring bugs) is if the caller did not ask for either IID_IUnknown or IID_IClassFactory.
Line 34: Returns S_OK to indicate, to the caller, that the requested COM object is supported by the server and the object's object creation interface; IClassFactory, has been placed in output parameter ""ppv".