I'm recently exploring how to use COM in c++ and come across with a simple ribbon code.
Code has:
HRESULT hr = CoCreateInstance(CLSID_UIRibbonFramework, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&g_pFramework));
So, it's based on COM, right? There should be some information about this component in Component Service, but I can't find it anyhow.
My question is how windows manages to find the COM component with corresponding CLSID provided in the header file(UIRibbon.h
)?
how windows manages to find the COM component with corresponding CLSID
The CoCreateInstance function provides a convenient shortcut by connecting to the class object associated with the specified CLSID, creating an uninitialized instance, and releasing the class object. As such, it encapsulates the following functionality:
CoGetClassObject
...
A call to CoGetClassObject creates, initializes, and gives the caller access (through a pointer to an interface specified with the riid parameter) to the class object. The class object is the one associated with the CLSID that you specify in the rclsid parameter. The details of how the system locates the associated code and data within a computer are transparent to the caller, as is the dynamic loading of any code that is not already loaded.
[...]
There are two places to find a CLSID for a class: (1) The registry holds an association between CLSIDs and file suffixes, and between CLSIDs and file signatures for determining the class of an object. (2) When an object is saved to persistent storage, its CLSID is stored with its data.
As MSDN says the lookup is transparent to caller and it embeds multiple sources: some CLSIDs might be explicitly registered by servers and are priority for lookup, then there is per-user registry of COM classes, machine wide registry, "Treat As" records etc.
Presumably, the most frequent scenario is the following. COM server (application or DLL) registers its CLSIDs with system registry as a part of its installation. Then system registry holds a record under HKEY_CLASSES_ROOT
which holds information on specific CLSID
and how to locate code associated with it. Then CoCreateInstance
and friends look this information up on caller request and do the magic letting the caller obtain callable interface pointer for the requested CLSID.
This is in particular the case of CLSID_UIRibbonFramework
, with its related information in registry under HKEY_CLASSES_ROOT\CLSID\{926749fa-2615-4987-8845-c33e65f2b957}
.