Back in the first Intro to COM article, we saw the CoCreateInstance()
API, which creates a COM object when a client
requests one. From the client's perspective, it's a black box. Just callCoCreateInstance()
with the right parameters
and BAM! you get a COM object back. Of course, there's no black magic involved; a well-defined process happens in which the COM server gets loaded, creates the requested COM object, and returns the requested interface.
Here's a quick overview of the process. There are a few unfamiliar terms here, but don't worry; I'll cover everything in the following sections.
- The client program calls
CoCreateInstance()
, passing the CLSID of the coclass and the IID of the interface it wants. - The COM library looks up the server's CLSID under
HKEY_CLASSES_ROOT\CLSID
. This key holds the server's registration information. - The COM library reads the full path of the server DLL and loads the DLL into the client's process space.
- The COM library calls the
DllGetClassObject()
function in the server to request the class factory for the requested coclass. - The server creates a class factory and returns it from
DllGetClassObject()
. - The COM library calls the
CreateInstance()
method in the class factory to create the COM object that the client program requested. CoCreateInstance()
returns an interface pointer back to the client program.