COM – Creating a COM Component using C++ – .def file
April 5, 2014
In previous article, we have created a test application to test our COM component. When we rant our test application, it was displaying “Class not registered” error message.
Before re-looking into our component and test application code; lets understand in what scenarios this “Class not registered” error message occurred.
One scenario is, unable to find the mapping DLL for the given CLSID which causes “Class not registered” error message. We have discussed in our previous articles that, CLSID is used to locate the component through Windows registry. It seems our test application is unable to locate the DLL through CLSID.
We need to re-check the registry entries for our COM component. Lets open our “HelloComponent.reg” file what we have created in our previous articles. The content of the file looks like below:
Windows Registry Editor Version 5.00
There is a problem here. If we run our application on 32-bit systems, it will work fine. But if we run it on 64-bit OS it will not work. Because on 64-bit systems, we need to place our CLSID into different location. So, now create another .reg file and name it as “HelloComponent6432.reg”. Place the below registry entries into the file:
Windows Registry Editor Version 5.00
Type HelloComponent6432.reg at command prompt. So, these entries will be added into registry. Before update registry; it is always a good idea to take a copy of your Windows registry. If anything went wrong, the backup will be useful to restore the Windows registry.
Now run our test application “TestApp.exe”. The test application is throwing an error “Error in the DLL”. It seems there is a problem in the DLL what we have created.
Lets open our DLL file into Microsoft’s Dependency Walker Tool. This tool is useful to display all the dependent DLLs and the DLL details like exported functions etc.,. You can download this tool from Microsoft’s web-site. Once we open our “HelloComponent.dll” in Dependency Walker tool, it will show the DLL entries like this:
Observe that none of the functions are exported from our “HelloComponent.dll”. All the functions defined in our DLL became private and these functions can be accessible within the DLL only. We should export some functions from our DLL. Otherwise other modules can’t communicate with our DLL.
But here is the question; what functions we need to export from our DLL? We need to export DllGetClassObject and DllCanUnloadNow functions. Because COM library use these functions to create an instance of COM component and release the DLL from memory.
Now its time to export the functions from “HelloComponent.dll”. To export the functions, we no need to modify our “HelloComponent.dll”. Instead of modifying our DLL code, we can put all our exported functions in .def file and re-generate the “HelloComponent.dll” including .def file.
Create “HelloComponent.def” file and add the following lines into it.
Above command generates “HelloComponent.dll” with exported functions information. Lets open this new DLL in Microsoft’s Dependency Walker Tool. It looks like:
Now our DLL is ready to call from other modules.
Lets try running our test application. Our test application, will pick the new DLL to test the COM component is working fine or not.
After running our test application, from command prompt; this time it is showing “No such interface supported”. This time it is showing different error, other than “Error in the DLL” error. It seems the issue “Error in the DLL” was resolved; we need to look at this new error “No such interface supported”.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.