12

I have a class library written in C#, and I want to call it from a legacy native C++ application. The host application is truly native, compiled on Windows and Linux, and it’s a console application. So how can I make it call the C# class library, assuming using Microsoft .NET on Windows, and Mono on Linux?

I have looked at SWIG and wrapping with COM interfaces on Windows, but is there a standard recognized solution that works cross platform? I.e., that is generic, works with both Microsoft .NET and Mono. A write-once-use-everywhere implementation.

Solutions should expose the full class interfaces from the C# domain to the C++ domain.

Similar questions focus only on the Windows solutions, for example -

Call C# methods from C++ without using COM

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
titanae
  • 2,259
  • 2
  • 21
  • 31

2 Answers2

7

If you want to do this cross platform, I would recommend going with a 100% Mono approach.

Mono has a clean Embedding API which works on Linux and Windows.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Good answer, embedding Mono on Windows not really what I was looking for also it all looks very manual. You would have to write a lot of boiler plate code to expose an full interface to a library, it looks time consuming and fragile, reminds me of JNI. I was wondering if there is some sort of automated way, like SWIG or just exposing a COM interface. – titanae Aug 22 '09 at 00:51
  • COM works well, but not so well on linux. SWIG is only good for going the other way... Sorry. – Reed Copsey Aug 22 '09 at 16:35
3

With .NET 5.0 (the successor of .NET Core) this is now possible to call C# from C++ in a cross-platform way without using Mono. Please see the solution explained in this GitHub issue using DNNE to generate a shared library and GCHandles to access C# objects.

With this you get a shared library that can be used from C or C++. Note that this will give a C-like API (no objects, like when using extern C in C++), in the future there may be tools like SWIG for C++ to overcome this limitation.

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
  • 2
    Please post the code inline. If not possible, please link directly to the code snippet instead of a github issue containing multiple comments. – Gili Aug 02 '20 at 12:47
  • @Gili here is a snippet demonstrating a C# interface that can be called using this method: https://github.com/dotnet/docs/issues/18174#issuecomment-642124735 People should refer to the [DNNE documentation](https://github.com/AaronRobinsonMSFT/DNNE) for how to create a DLL. This is a very new feature (.NET 5.0 is still in beta) but as it was not mentioned anywhere on SO I assumed it would be ok to not have more inline content yet. Moreover the other answer was found helpful (+5) despite not having inlined code. – Gabriel Devillers Aug 02 '20 at 20:01
  • What is "DNNE" for? ".NET native exports"? – Peter Mortensen Jun 01 '22 at 19:34
  • @PeterMortensen I would guess like you: "Dot Net Native Export" – Gabriel Devillers Nov 03 '22 at 08:21