Is there a way to call c# dll from c++ unmanaged application without COM usage?
-
You could always [embed Mono](http://www.mono-project.com/Embedding_Mono)... – cdhowie Dec 16 '10 at 21:46
-
can be done via mixed mode dlls. – Sandeep Singh Rawat Dec 16 '10 at 22:28
-
possible duplicate of [Calling C# code from C++](http://stackoverflow.com/questions/778590/calling-c-sharp-code-from-c) – cdiggins Apr 20 '14 at 13:10
-
There are multiple techniques listed here: http://stackoverflow.com/a/23182821/184528 – cdiggins Apr 20 '14 at 13:12
4 Answers
You can do this using Reverse P/Invoke
- example and discussion here.

- 53,498
- 9
- 91
- 140
It is actually possible to disassemble, modify the IL, and reassemble it with exported functions. I messed with this a few years ago, and created an application that would disassemble a dll, provide a list of functions that could potentially be exported - allowing the user to select them, then re-write the IL and reassemble everything. Then, I could call directly into the dll from unmanaged code...or p-invoke into the dll from managed code (not really practical, but interesting nonetheless).
Surely there is a reason that this isn't supported in the .net languages themselves (even tho it is supported in MSIL). I wouldn't use this in production:
Dead link:
http://www.csharphelp.com/2007/03/exporting-managed-code-as-unmanaged/
Wayback Machine: https://web.archive.org/web/20140213030149/http://www.csharphelp.com/2007/03/exporting-managed-code-as-unmanaged/

- 8,252
- 4
- 29
- 50
-
1It is supported in C++/CLI, it automatically creates a thunk that will load the CLR when you __declspec(dllexport) a static C++/CLI function. Which is the Big Trick of course, can't run managed code without loading the CLR. – Hans Passant Dec 16 '10 at 22:29
-
@Hans When you add exports to your IL, ILASM will do everything that is required for your assembly to be used as if it were a native one. – Robert Giesecke Jan 05 '11 at 15:02
I might be a bit late, but check this out.
Using this little msbuild task, you can create a C# library that can be called as if it were a native DLL. (e.g. write plugins for apps that require them to be native dlls)
Oh and don't forget to use the project template, which will setup everything for you.

- 4,314
- 21
- 22
Your only option really is to either use C++.net or create a C++.net wrapper for it that exports what you need.