Calling C# from native C++, without /clr or COM?

雨燕双飞 提交于 2020-08-16 19:12:49

问题


This question has been asked before, but I never found a truly satisfying solution -

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 & Linux, its 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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1314769/calling-c-sharp-from-native-c-without-clr-or-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!