Error trying to call a C# DLL from a C++ console app

DonBaechtel-7903 191 Reputation points
2025-06-16T17:00:35.6566667+00:00

I am on Windows 10 with VS 2022.

I want to call a C# DLL from a C++ app.

I am trying to register the C# DLL using regasm.

I am getting the following error:

C:\Users\dbaec\OneDrive\Forex\C++_App\C#_DLL\obj\x64\Debug\net6.0-windows7.0>regasm.exe /register "C:\Users\dbaec\OneDrive\Forex\C++_App\C#_DLL\obj\x64\Debug\net6.0-windows7.0\c#_DLL.dll"

Microsoft .NET Framework Assembly Registration Utility version 4.8.9037.0

for Microsoft .NET Framework version 4.8.9037.0

Copyright (C) Microsoft Corporation. All rights reserved.

RegAsm : error RA0000 : Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

How do I fix this?

How do I call a C# DLL from a C++ app?

Thanks.

Developer technologies | C++
{count} votes

Accepted answer
  1. RLWA32 49,636 Reputation points
    2025-06-18T09:13:42.87+00:00

    I created a small demo solution in VS2022 that contains 3 projects. The NetServerTypes project uses an IDL file to create a type library. The NetServerTest project builds the C# COM Server, embeds created type library as a resource and also uses a post-build event to register the server and the type library. COM registration requires elevated privileges so run Visual Studio as Administrator when you build the solution. The CppClient project is a minimal C++ console application that imports the registered type library to generate required headers. It instantiates the COM object and calls methods and gets a property.

    The demo is at https://1drv.ms/u/c/c389bf4121acea69/EfUvHqqc66tNjGT-AJOGPBEBfDHhtc3IfbfbnjZTRF9elw?e=oRkujI


4 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2025-06-19T15:07:31.2833333+00:00

    unlike the old .net framework, a .net core dll, can not be directly loaded by a C/C++ program. it needs a wrapper code. if you follow the the steps to build a COM library in .net core, you will see that a wrapper C project is created that supports loading the c# dll, and marshaling the calls.

    Only windows supports com, if you want cross platform, then you probably want the C# code to expose C entry points callable from C/C++. You C/C++ code must use a net core host to load the dll.

    https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting

    defining methods to be callable from C/C++

    https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute?view=net-9.0

    your other option is to create a native library, which is directly callable from C/C++. this uses the AOT feature and has some restriction on supported C# code:

    https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/libraries

    2 people found this answer helpful.
    0 comments No comments

  2. DonBaechtel-7903 191 Reputation points
    2025-06-17T22:59:52.84+00:00

    According to the post, I added the following content to the project.cprog file

    <ItemGroup> <ComHostTypeLibrary Include="Server.tlb" Id="1" /> </ItemGroup>

    When I built the project, I encountered an error stating that the Server.tlb file could not be found.

    Even though I had a Server : IServer in the source.

    After adding the content, I kept getting a periodic loud BONG sound that continued until after the content was removed.

    I want the Server.tlb file. How do I get it?

    Attached *.csproj file:

    <Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>

    <TargetFramework>net8.0</TargetFramework>
    
    <ImplicitUsings>enable</ImplicitUsings>
    
    <Nullable>enable</Nullable>
    
    <EnableComHosting>true</EnableComHosting>
    
    <EnableRegFreeCom>true</EnableRegFreeCom>
    

    </PropertyGroup>

    <ItemGroup>

    <ComHostTypeLibrary Include="Server.tlb" Id="1" />
    

    </ItemGroup>

    </Project>

    1 person found this answer helpful.

  3. DonBaechtel-7903 191 Reputation points
    2025-06-26T02:09:14.3466667+00:00

    Updated project at https://1drv.ms/u/c/c389bf4121acea69/EaV-4dxmGhhJrcFAnQJ1cnUB19zVLWrhWjdVWRQOee8n-g?e=U3gWn3

    The project is working really well now.

    Thank you very much.


  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    2 deleted comments

    Comments have been turned off. Learn more

Your answer

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