I just came across a subtle point, an error, in the Crash Course (and I was only on page 2). I see it said this way all the time. (You don't think people are coppying each other, do you?) It stated "When an application asks COM to create an object, COM creates an instance of the object and returns a pointer to the objectís IUnknown interface." Not quite true. Say we have an object with a single interface ISomething. You can look all day thru the code for where the IUnknown comes from... it just ain't there as a seperate interface. It's the first three methods of ISomething. Now, when making objects such as this (single interface), one normally refers to the "Something" class (or object) having it's ISomething interface. One rairly asks COM to get you an IUnknown interface. That's a rare special case when aggregating an object, something you may almost ignore (unless constructing the whole object framework itself like CoLib). One typically asks COM to get a ISomething interface from the Something coclass. Thats exactly what the CoGetClassObject method does. Now, internally, each class has a ClassFactory that the class uses to have itself created. Class Factory uses the three IUnknown methods get used in a quite optimal fashion. For example, since QueryInterface will already know how to look up which interfaces are supported. So this method is used to determine if your newborn object supports the interface you want. The big question here is, WHICH object are you asking? Does the ClassFactory have intimate details of every class it instances? No, it doesn't. Mostly, it has a list of "If asking for this coclass, make this thing." It has zero idea if that class supports any interface at all (with one importaint exception). Now each object created may have LOTS of interfaces. The ClassFactory really has no idea if it's the correct object. A good name for something when you don't know what it is in UNKNOWN. A ClassFactory thus creates an Unknown object, and it may be said it has an IUnknown interface. Once it has this Unknown object, the Class Factory calls, you guessed it, Unknown::QueryInterface to see if it has the interface you want. If it does, the Class Factory returns you this new interface, and then calls Unknown::Release on the orgionally created object. And in the middle, QueryInterface itself has called AddRef to count this new pointer to itself. So the reference count of a newborn object quickly starts at 1, changes to 2, then back to 1. I bet now you know lots more about the internals of COM then you wanted to. And now you know why the ONLY thing you may be sure about every interface is called UNKNOWN.
Posted on 2001-02-28 12:26:00 by Ernie
No wondering why I confused! Thanks, bitRAKE
Posted on 2001-02-28 16:20:00 by bitRAKE