Finally we have successfully developed our COM component HelloComponent using C++. Lets summarize how we developed our COM component in this article.
- Microsoft’s Component Object Model (COM) is a specification to allow to write re-usable components and enables them to communicate each other.
- COM depends on interfaces to achieve its goals. One of the main interface is IUnknown and all COM based interfaces must be inherited from this root interface IUnknown.
- All interfaces going to use by the COM components, must be declared in an Interface Definition Language (IDL) file.
- All COM components must implement IUnknown interface; responsible for query the interface using its QueryInterface method and maintains object reference counting to decide when to remove component from memory.
- COM component housed into a DLL file or an EXE file. If it is housed into a DLL file, that must be exported DllGetClassObject & DllCanUnloadNow functions.
- COM Component must be registered into Windows registry with proper CLSID.
- CoCreateInstance is the function to create an instance of a COM component.
- Internally CoCreateInstance calls another function CoGetClassObject.
- CoGetClassObject checks the registry entries for CLSID which is passed through CoCreateInstance function.
- If CoGetClassObject finds the location of the DLL associated with the CLSID, it calls CoLoadLibrary function to load the DLL into memory.
- After the DLL is loaded into the memory, CoGetClassObject will get the address of DLL’s exported function DllGetClassObject to get the pointer to IClassFactory interface. IClassFactory interface is responsible to create instances of classes.
- CoCreateInstance function calls, IClassFactory‘s CreateInstance function to create class instance.
- Unloads the COM library, once the program exits.
We have created one working COM component. Do you think this will work with any other client application? What about the application developed using Visual Basic? Let us check this in our next article.