tags:

views:

325

answers:

3

How would I export functions defined in a C# class library, while enabling them to be imported to and called from an unmanaged C++ application/DLL ?

+8  A: 

Strictly speaking, you can't just export functions as you would in a classic .dll, as .NET .dll's aren't really .dll's at all. Your only three options are:

  1. Use managed C++
  2. Expose your C# classes as COM objects and consume them from your C++ code
  3. Host the .NET runtime in your C++ project and interact with your C# classes through that.
Adam Robinson
I see. Many thanks for the quick answer.
shadeMe
+1  A: 

You would not. Not supported. You can pretty much only export COM objects from a C# class librarly.

TomTom
+1 - COM interop is a fairly standard technique for calling managed code from an unmanaged app.
Tim Robinson
+1  A: 

You could also make a C++ wrapper for your C# library - a simple Managed C++ DLL that would import .NET methods and export them natively. This adds an additional layer, but it might be useful if C# library is a must have.

Another option is to tweak the compiled assembly to export the functions. A C# compiler cannot do this, but it takes a slight change of MSIL code to get the things done. Have a look at this article - there're some links on how the stuff works, and a tool to automate it (though I haven't tried it myself).

VladV
A C# library isn't a requirement - As a matter of fact, I was considering to use managed C++ instead. I can't use the CLI with my primary application but I can use it to write my library. How would that change the scenario ?
shadeMe
In this case, you should use Managed C++ - just to avoid unnecessary hacking.
VladV