Mike Webb Mike Webb - 1 year ago 99
C++ Question

Is is possible to export functions from a C# DLL like in VS C++?

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

hB0 hB0
Answer Source

Unmanaged Exports

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download