43

In VS C/C++ you could use extern "C" __declspec(dllexport) -function declaration-.

How do I accomplish this in a C# dll? Is there C# code equivalent to the above code?

Edit: More info

I am trying to create an add in for Notepad++ and I want to use C#, but the common way I've seen so far is to use legacy C++ code with the above call to export a few of the functions that Notepad++ expects to import and call. There is an example app using C#, but this still requires a loader DLL, which I assume from the comments/answers below is the only way for C#.

Mike Webb
  • 8,855
  • 18
  • 78
  • 111

4 Answers4

39

Unmanaged Exports => https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

DLLExport => https://github.com/3F/DllExport

How does it work?

Create a new classlibrary or proceed with an existing one. Then add the UnmanagedExports Nuget package.

This is pretty much all setup that is required.

Now you can write any kind of static method, decorate it with [DllExport] and use it from native code.
It works just like DllImport, so you can customize the marshalling of parameters/result with MarshalAsAttribute.

During compilation, my task will modify the IL to add the required exports...

hB0
  • 1,977
  • 1
  • 27
  • 33
  • 2
    The DllExports project is now on [GitHub](https://github.com/3F/DllExport). – R.J. Dunnill Apr 23 '19 at 22:11
  • After downloading the 3F/DllExport example [here](https://github.com/3F/Examples/tree/master/DllExport) and trying to build with VS 2017 CE, the tool does not export functions for some reason. Hoping [this question](https://stackoverflow.com/questions/71441509/why-is-dllexport-not-exporting-functions-in-3f-dllexport-example-using-vs-2017-c) might lead to a solution. – John Yost Mar 22 '22 at 21:58
  • I wish I had seen this before I went to the pain of creating this for myself using CLR hosting. – Jamie May 27 '22 at 20:02
20

I've seen people do this before, but it required ildasm, adding the MSIL .export directive, and then reassembling. A program named dll_tool can do these steps for you.

If you want to build a mixed-mode DLL with both native and managed exports, you should be using C++/CLI, which is specially designed for this purpose.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
9

Yes, it is possible to export functions from a C# dll in much the same way that C++ does it! You need a little help from an add-in Unmanaged Exports (DllExport for .Net) that facilitates this process, or from a similar method such as Exporting functions in C#/VB.NET to native code.

Please see Code to Export C# DLL to Metatrader Build 600+ for a working example using Robert Giesecke's C# Project Template for Unmanaged Exports to export a C# dll to a legacy application (Metatrader) that has a great deal of similarity to C++.

Additionally, you might find Native and .NET Interopability interesting though it is mostly geared toward accessing native code from within .NET rather than the other way around.

Patrick White
  • 231
  • 3
  • 7
2

No, you cannot do that in the same sense as you do in C and C++.

But you can create COM API to achieve that which then you can use in C and C++ code.

See these articles

Nawaz
  • 353,942
  • 115
  • 666
  • 851