Calling C# code from C++

C#C++ CliInterop

C# Problem Overview

I need to be able to invoke arbitrary C# functions from C++. suggests using ICLRRuntimeHost::ExecuteInDefaultAppDomain() but this only allows me to invoke methods having this format: int method(string arg)

What is the best way to invoke arbitrary C# functions?

C# Solutions

Solution 1 - C#

Solution 2 - C#

Compile your C++ code with the /clr flag. With that, you can call into any .NET code with relative ease.

For example:

#include <tchar.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
	System::DateTime now = System::DateTime::Now;
	printf("%d:%d:%d\n", now.Hour, now.Minute, now.Second);

	return 0;

Does this count as "C++"? Well, it's obviously not Standard C++ ...

Solution 3 - C#

See DllExport.

IOW: The exact opposite of how DllImport works.

It has support for Windows, with cross-platform support in the works.

C# code (which we call from C++):

public static int _add(int a, int b)
    return a + b;

public static bool saySomething()
    DialogResult dlgres = MessageBox.Show(
        "Hello from managed environment !",
        ".NET clr",

    return dlgres == DialogResult.OK;

C++ code (which calls previous C# code):

typedef int(__cdecl *_add)(int a, int b);
typedef bool(__cdecl *saySomething)();

auto pAdd = (_add)GetProcAddress(lib, "_add");
int c = pAdd(5, 7);

auto pSaySomething = (saySomething)GetProcAddress(lib, "saySomething");
bool dlgres = pSaySomething();

And a YouTube video with a demo at Managed & Unmanaged; PInvoke; [ Conari vs DllExport]. To be honest, the documentation is a cut below perfect, but don't let that put you off: the YouTube videos are excellent.

This project is inspired by another project from Robert Giesecke which has 220,000 downloads on NuGet.

Fun fact: some Python libraries have used this to implement functionality in a mix of C++ and C#.

And finally, thank you Robert Giesecke and Denis Kuzmin, brilliant, brilliant work!

Solution 4 - C#

If you don't care if your C++ program (or a portion of it) gets compiled with the /clr, you can use C++/CLI to simply call any .NET code (as long as you add a reference to it). Try out this article.

EDIT: Here is a nice tutorial

The other route is to make your C# code be exposed as COM.

Solution 5 - C#

The easiest way is to use COM interop.

Solution 6 - C#

You could use a COM callable wrapper around your C# code compiled into a DLL.

Solution 7 - C#

As an alternate approach, you could use Lua to instantiate the CLR objects, execute, and return the results.

Solution 8 - C#

From Microsoft: Write a custom .NET Core host to control the .NET runtime from your native code.

IOW: Call C# from from C++ on both Windows and Linux.

There is sample code on GitHub.

This sample code is cross platform, it allows C# code in .NET Core to be called from any C++ application on both Linux and Windows.

In all honesty, this solution seems to be quite complicated compared to the other DllExport answer. The C++ code is doing a lot of heavy lifting to scan for resouces and entry points, etc. One argument for this answer could be that it is cross-platform. However, the other DllExport answer is also cross-platform as well, and a lot simpler to use.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGiliView Question on Stackoverflow
Solution 1 - C#cdigginsView Answer on Stackoverflow
Solution 2 - C#ÐаnView Answer on Stackoverflow
Solution 3 - C#ContangoView Answer on Stackoverflow
Solution 4 - C#Filip FrączView Answer on Stackoverflow
Solution 5 - C#laktakView Answer on Stackoverflow
Solution 6 - C#Dan VintonView Answer on Stackoverflow
Solution 7 - C#David RobbinsView Answer on Stackoverflow
Solution 8 - C#ContangoView Answer on Stackoverflow