OOPortal OOPortal


Basic COM   «Prev 

Call CoCreateInstance

Clients that need to create only one instance of a particular COM object call CoCreateInstance. To support calling
  1. CoGetClassObject and
  2. IClassFactory::CreateInstance,
  3. CoCreateInstance
combines parameters used in both calls:

STDAPI CoCreateInstance(
           REFCLSID rclsid,     
           LPUNKNOWN pUnkOuter, 
           DWORD dwClsContext,  
           REFIID riid,         
           LPVOID * ppv);

CoCreateInstance has one limitation when compared to CoGetClassObject. It does not take an IID to specify which object creation interface to get. Recall that a class factory is a "type of" class object that implements interface IClassFactory.
CoCreateInstance assumes the client wants an IClassFactory interface and the object's class object is a class factory. Although this is OK for the majority of COM objects, occasionally a COM object supports a different object creation interface. You must use CoGetClassObject to get an object creation interface for class objects that do not implement IClassFactory.
For this course, all our class objects will be class factories, that is they support IClassFactory.