Calling C# class libraries via COM in C++ requires registration to use them, but I don't want to register the DLL.

RL Chen 260 Reputation points
2023-09-27T12:26:15.99+00:00

In C++, I use #import ". \PptInterface.tlb" to call the functions in the C# class library I generated, provided that PptInterface.dll and PptInterface.tlb are available in the current program directory.I am confused, however, in that when going to a new computer to use this function, it is necessary to reuse RegAsm.exe to do the same thing to the DLL. RegAsm.exe to register the DLL so that the COM interface of the program can call the function properly. I don't want to have to register the DLL every time I go to a new computer, is there a good way to do this?

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. RLWA32 51,361 Reputation points
    2023-10-02T12:24:49.4066667+00:00

    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 -

    Unblock

    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 -

    CsharpManifest

    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:

    1. Automatically create the manifest entries needed for registration-free COM to load CSRegFreeComServer.dll.
    2. Copy the C# class library (CSRegFreeComServer.dll) to the same folder that contains the executable produced by the C++ project.
    3. 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.
    4. 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 -

    CppManifest


1 additional answer

Sort by: Most helpful
  1. RLWA32 51,361 Reputation points
    2023-09-27T13:18:27.0833333+00:00

    If you are deploying an in-process COM server (DLL) you can use registration-free COM to avoid having to register the DLL on systems to which it is deployed.

    Refer to How to: Configure .NET Framework-Based COM Components for Registration-Free Activation


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.