I have put together a demo for you that uses registration-free COM from a native C++ application to instantiate a COM object hosted in a .Net Framework 4.8 C# class library. The demo is contained in CSRegFreeComServer.zip and it can be downloaded from https://1drv.ms/u/s!AmnqrCFBv4nDhBoobsLB-8I5_MSE?e=HXGeHI
Before you extract the contents of the CSRegFreeComServer.zip file make sure to unblock it -
The demo solution contains a .Net Framework 4.8 C# class library project that exposes a COM object (CSRegFreeComServer.csproj). The project has been configured so that the manifest required for registration-free COM will automatically be embedded in the resultant DLL. The DLL is built using the AnyCPU platform so that it can be loaded by a 32-bit or 64-bit COM client application. DO NOT USE RegAsm with this DLL. The C++ project described below will handle type library creation.
The project contains the following Registration-Free COM Manifest -
The solution also contains a C++ project for a COM client application (CppClient.vcxproj). This project has been customized to accomplish several tasks that are specific to the sample:
- Automatically create the manifest entries needed for registration-free COM to load CSRegFreeComServer.dll.
- Copy the C# class library (CSRegFreeComServer.dll) to the same folder that contains the executable produced by the C++ project.
- Run the .Net Framework Type Library Exporter (TlbExp.exe) to export the type libraries needed for the C++ project's use of the #import directive. Because managed code can use polymorphic types like IntPtr that have different sizes between 32-bit and 64-bit code a 32-bit type library is exported for 32-bit build and a 64-bit type library is exported for a 64-bit build. This ensures that the header files created by the #import directive use the proper C++ data types.
- Delete the copied CSRegFreeComServer.dll file when the C++ project is cleaned.
The registration-free COM manifest entries needed for the C++ project are provided directly in the project properties as seen here -