shadeMe
shadeMe

Reputation: 716

Exporting functions from a C# class library

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 ?

Upvotes: 4

Views: 4765

Answers (4)

VladV
VladV

Reputation: 10349

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).

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273169

Your C++ Apllication would have to start by hosting the CLR. There is nothing special required from the .NET DLL.

Upvotes: 2

TomTom
TomTom

Reputation: 62093

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

Upvotes: 1

Adam Robinson
Adam Robinson

Reputation: 185593

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.

Upvotes: 8

Related Questions