Basic COM   «Prev  Next»
Lesson 7In-process COM servers and DllGetClassObject
ObjectiveDescribe what DllGetClassObject does.

In-process COM Servers and DllGetClassObject

In-process servers provide class factories via function DllGetClassObject:
STDAPI DllGetClassObject(const CLSID&
rclsid, const IID& riid, VOID ** ppv);

COM calls DllGetClassObject on behalf of a COM client passing in a CLSID, an IID, and a VOID ** output parameter.
The CLSID is the CLSID of the COM object we want a class factory for; the IID is IID_IClassFactory. If the server supports the requested COM object, it creates a class factory and calls QueryInterface in the new class factory. The following code demonstrates an implementation of DllGetClassObject.

CClass Factory
  1. 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.
  2. 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 *.
  3. 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.
  4. An instance of the class factory for the requested COM class is instantiated.
  5. 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.
  6. A check of the return status of the IClassFactory::QueryInterface call.
  7. 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.
  8. 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.

In-process COM Servers and DllGetClassObject

DllGetClassObject
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".