COM – Creating a COM Component using C++ – .def file

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:

Open HelloComponent.dll using Microsoft's Dependency Walker Tool
HelloComponent.dll – Microsoft’s Dependency Walker Tool

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.

; HelloComponent.def : Export module functions.

LIBRARY "HelloComponent.dll"

	DllGetClassObject	PRIVATE
	DllCanUnloadNow	PRIVATE

Now re-generate our “HelloComponent.dll”. Make sure that you use .def file while generating the DLL. Below is the command:

cl /LD /EHsc HelloComponent.cpp /link comsuppw.lib /def:HelloComponent.def

Above command generates “HelloComponent.dll” with exported functions information. Lets open this new DLL in Microsoft’s Dependency Walker Tool. It looks like:

DLL with exported functions showing in Microsoft's Dependency Walker Tool
DLL with exported functions showing in Microsoft’s Dependency Walker Tool

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”.

We will check this in our next article.


Add a Comment

Your email address will not be published.