See DllExport.
IOW: The exact opposite of how DllImport
works.
https://github.com/3F/DllExport
It has support for Windows, with cross-platform support in the works.
C# code (which we call from C++):
[DllExport]
public static int _add(int a, int b)
{
return a + b;
}
[DllExport]
public static bool saySomething()
{
DialogResult dlgres = MessageBox.Show(
"Hello from managed environment !",
".NET clr",
MessageBoxButtons.OKCancel
);
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!