objectcomiunknown

COM IUnknown and do I need a pointer to it first before calling CoGetClassObject?


In COM, when you want to create an instance of some COM Server object, do you first need to get a pointer to it's IUnknown interface and only then create a class object using CoGetClassObject?

As far as I understand it, IUnknown is used to manage object lifetimes, so from my understanding, whatever object the client wants to create, one needs a pointer to it's IUnknown interface implementation first.

Sound correct? If not, can anyone tell me how it works?


Solution

  • IUnknown manages the lifespan of an existing COM object. Before the object is created, there is no IUnknown pointer to speak of.

    CoGetClassObject is used to get the IUnknown interface for an object that is expected to create the objects of interest. I.e., it is an object factory, an usually implements IClassFactory, which declares the CreateInstance method that you use to create objects.

    Instead of using class factories explicitly, it is often simpler to just call CoCreateInstance.