views:

50

answers:

1

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?

+3  A: 

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.

Marcelo Cantos
So only object implementing IUnknown can be used in COM by a client?
Tony
Tony: All objects must implement `IUnknown`. This interface is not very useful in its own right, but it declares the `QueryInterface` method, which lets you discover the other interfaces supported by the object.
Marcelo Cantos
@Marcelo Cantos: `IUnknown` *is* useful on its own since its helps track object lifetime.
sharptooth
@sharptooth: I didn't say it isn't useful on its own.
Marcelo Cantos