Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C# from C

Tags:

c

c#

Has anyone worked on calling a C# module from C module. I tried searching on internet but didn't find good examples. Though lot of sites say something like using COM interop but couldn't find a proper example or article explaining it though.

If someone can help me on this, it would be great

Thanks, Sveerap

like image 568
sveerap Avatar asked Dec 13 '10 11:12

sveerap


1 Answers

There is more than just COM interop if you want to call into managed code from C or C++. The are also the following lesser known methods (taken from MSDN FAQ):

How do I call a .NET assembly from native Visual C++?

There are basically four methods to call .NET assembly from native VC++ code:

  1. CLR Hosting API: Native VC++ module calls CLR Hosting APIs to host CLR, load and call the .NET assembly (sample code: CppHostCLR).

  2. COM Interop: If the .NET assembly can be exposed as a COM component, native VC++ module can call into the .NET assembly through .NET – COM interop (sample code: CppCOMClient).

  3. Reverse PInvoke: The managed code calls native code passing a delegate that the native code can call back (sample code: CSPInvokeDll).

  4. C++/CLI: If the module containing native VC++ code is allowed to enable CLR, the native VC++ code can call .NET assembly directly (sample code: Consuming C# Library in native C or C++ using C++/CLI)

like image 98
Dirk Vollmar Avatar answered Sep 20 '22 08:09

Dirk Vollmar