CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

COM – Creating a COM Component using C++ – Summary of component development

Finally, we have successfully developed our COM component HelloComponent using C++. Let’s summarize how we developed our COM component in this article.

  • Microsoft’s Component Object Model (COM) is a specification to allow to write of reusable components and enables them to communicate with each other.
  • COM depends on interfaces to achieve its goals. One of the main interfaces 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 the IUnknown interface; responsible for querying the interface using its QueryInterface method and maintaining object reference counting to decide when to remove the component from the memory.
  • COM component housed into a DLL file or an EXE file. If it is housed in a DLL file, that must be exported DllGetClassObject & DllCanUnloadNow functions.
  • COM Component must be registered into the 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 the CoCreateInstance function.
    • If CoGetClassObject finds the location of the DLL associated with the CLSID, it calls the CoLoadLibrary function to load the DLL into memory.
    • After the DLL is loaded into the memory, CoGetClassObject will get the address of the DLL’s exported function DllGetClassObject to get the pointer to the IClassFactory interface. The IClassFactory interface is responsible to create instances of classes.
    • The CoCreateInstance function calls, IClassFactory‘s CreateInstance function to create a 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.

**

COM – Creating a COM Component using C++ – Summary of component development

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top
Exit mobile version