Factory design

All COM/DCOM component class follow factory design pattern. Constructor in factory design is private can new operator gives compilation error and object is not allocated directly using new operator by client. Factory design suggests a call to a creation function. Server allocates and returns the object and object memory management of allocation and deallocation is done in server side.

IClassFactory::CreateInstance

CreateInstance() is the factory provider function. A com component class should provide an IClassFactory interface for creating different objects from the component. Client indirectly calls this function with argument as the identity of the interface class. Com server creates and initializes an instance of the given co-class and returns the instance to the client as a return argument.

HRESULT CreateInstance(
  IUnknown * pUnkOuter,
                     //Pointer to whether object is or isn't part of
                     // an aggregate
  REFIID riid,       //Reference to the identifier of the interface
  void ** ppvObject  //Address of output variable that receives the
                     // interface pointer requested in riid
);

CreateInstance Parameters

pUnkOuter [in] - If the object is being created as part of an aggregate, pointer to the controlling IUnknown interface of the aggregate. Otherwise, pUnkOuter must be NULL.

riid [in] - Reference to the identifier of the interface to be used to communicate with the newly created object. If pUnkOuter is NULL, this parameter is frequently the IID of the initializing interface; if pUnkOuter is non-NULL, riid must be IID_IUnknown (defined in the header as the IID for Unknown).

ppvObject [out] - Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, *ppvObject contains the requested interface pointer. If the object does not support the interface specified in riid, the implementation must set *ppvObject to NULL.

CreateInstance Return Values

This method supports the standard return values E_UNEXPECTED, E_OUTOFMEMORY, and E_INVALIDARG, as well as the following:

  • S_OK - The specified object was created.
  • CLASS_E_NOAGGREGATION - The pUnkOuter parameter was non-NULL and the object does not support aggregation.
  • E_NOINTERFACE - The object that ppvObject points to does not support the interface identified by riid.

Calling CreateInstance

CoGetClassObject function can be called to get an IClassFactory interface pointer to the class object, call the CreateInstance method of this interface to create new uninitialised objects.

However is is not always necessary to go through this process to create an object. To create a single uninitialized object, programmer can just call CoCreateInstance. OLE also provides numerous helper functions (with names of the form OleCreateXxx) to create compound document objects.

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

Learn on Youtube

#