Yes, you really can build out the dll that contains your COM server according to this guide. But the story just begins.
When you register your dll with “regsvr32 myprogrammingnotes.dll”, you will see this error:
The module “myprogrammingnotes.dll” was loaded but the call to DllRegisterSever failed with the error code 0x80040200.
For more information about this problem, search online using the error code as a search term.
Searching the error code results in all answers pointing to the reason that you did not run regsvr32 as administrator. It seems regsvr32 requires administrator privileges to do its work. Unfortunately, this is not true in this case. The same error occurs even you run cmd and regsvr32 as administrator. And the real reason is hidden well by Qt team.
If you look carefully at the compiler messages in Qt Creator, you will see the following errors despite the dll has been built successfully.
process_begin: CreateProcess(NULL, C:\Qt\bin\idc.exe debug\myprogrammingnotes.dll /idl debug/myprogrammingnotes.idl -version 1.0, …) failed.
make (e=2): The system cannot find the file specified.
mingw32-make[1]: [debug\myprogrammingnotes.dll] Error 2 (ignored)
process_begin: CreateProcess(NULL, midl debug/myprogrammingnotes.idl /nologo /tlb debug/myprogrammingnotes.tlb, …) failed.
make (e=2): The system cannot find the file specified.
mingw32-make[1]: [debug\myprogrammingnotes.dll] Error 2 (ignored)
process_begin: CreateProcess(NULL, C:\Qt\bin\idc.exe debug\myprogrammingnotes.dll /tlb debug/myprogrammingnotes.tlb, …) failed.
make (e=2): The system cannot find the file specified.
mingw32-make[1]: [debug\myprogrammingnotes.dll] Error 2 (ignored)
process_begin: CreateProcess(NULL, C:\Qt\bin\idc.exe debug\myprogrammingnotes.dll /regserver, …) failed.
make (e=2): The system cannot find the file specified.
mingw32-make[1]: [debug\myprogrammingnotes.dll] Error 2 (ignored)
These are called post-link processes, i.e., the processes after the myprogrammingnotes.dll is built. The first error is obscure. In fact, it says the system cannot find the program C:\Qt\bin\idc.exe. Yes, you indeed cannot see idc.exe in the directory C:\Qt\bin\. Why? Because Qt team intendedly does that to make money. Fortunately, the source code of idc is right in the src directory: C:\qt-everywhere-opensource-src\src\tools\idc. You can go there and run “qmake mingw32-make” to build idc.exe yourself. The built idc.exe is in C:\qt-everywhere-opensource-src\bin\. You should copy it to the Qt installation directory:C:\Qt\bin.
Now, the plot progresses to the second episode. Rebuild your project and you will see a new file myprogrammingnotes.idl is generated in the build directory. This is the result of the following post-link command:
C:\Qt\bin\idc.exe debug\myprogrammingnotes.dll /idl debug/myprogrammingnotes.idl -version 1.0
which says run idc.exe to generate the IDL file myprogrammingnotes.idl out of myprogrammingnotes.dll.
Meanwhile you will see the following errors in the compiler output window:
process_begin: CreateProcess(NULL, midl debug/myprogrammingnotes.idl /nologo /tlb debug/myprogrammingnotes.tlb, …) failed.
make (e=2): The system cannot find the file specified.
mingw32-make[1]: [debug\myprogrammingnotes.dll] Error 2 (ignored)
Couldn’t open debug\myprogrammingnotes.tlb for read
mingw32-make[1]: [debug\myprogrammingnotes.dll] Error 4 (ignored)
Failed to register server!
The first error actually says it cannot find the midl program. Yes, midl.exe is not in C:\Qt\bin\ or any place of Qt. It is the IDL compiler provided by Microsoft. Where is midl.exe? You may think it is accompanied by Visual Studio but you cannot find it in the Visual Studio installation directory. In fact midl is part of Windows SDK. You can find it in the Windows SDK installation directory like C:\Program Files (x86)\Windows Kits\10\bin\x64. Ok, you got it. How do you let Qt creator find it and use it? Well, you should set the build environment in Qt Creator. Specifically, you should modify the PATH environment variable in Qt Creator/Build Environment/Use System Environment to append that SDK path. But Microsoft does not want to make your life easier, either. You will see this error when rebuilding:
midl : command line error MIDL1005 : cannot find C preprocessor cl.exe
So, midl depends on cl.exe provided by Visual Studio. But why does it separate midl.exe from Visual Studio and put it into Windows SDK? Who knows!
Ok, let’s add the Visual Studio path to the cl.exe to the PATH environment variable too. Can we build our project without further problems? No, MS smiles smugly. You will see this error:
debug\myprogrammingnotes.idl(12): fatal error C1034: olectl.h: no include path set
This error, I think, is produced by cl.exe. As you know, you did not tell cl.exe where to find header files. You can find olectl.h and other related header files in Windows SDK or Visual Studio installation directories but here is a shortcut. Open a Visual Studio command prompt such as “x64 Native Tools Command Prompt for vs 2022” and run “set INCLUDE” to get the INCLUDE environment variable, then add this environment variable to Qt Creator Build Environment. Now, the story is over. You should be able to build a register-able COM dll. The whole post-link processes are:
C:\Qt\bin\idc.exe debug\myprogrammingnotes.dll /idl debug/myprogrammingnotes.idl -version 1.0
midl debug/myprogrammingnotes.idl /nologo /tlb debug/myprogrammingnotes.tlb
C:\Qt\bin\idc.exe debug\myprogrammingnotes.dll /tlb debug/myprogrammingnotes.tlb
C:\Qt\bin\idc.exe debug\myprogrammingnotes.dll /regserver
midl compiles myprogrammingnotes.idl to myprogrammingnotes.tlb(type library of COM), idc.exe embeds the typelib into myprogrammingnotes.dll, and register the dll finally.
The dll registration error is caused by missing typelib in the dll(b.t.w, you can use oleview.exe accompanied by Windows SDK to check the type library in a dll). Qt team returns a wrong misleading error code if it cannot find the type library when registering the dll.