Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 897
Mike Webb Avatar asked Jan 27 '11 16:01

Mike Webb


People also ask

Is export a function in C?

C code export provides an option to generate C code for a selected subsystem within a schematic model.

What is export in C++?

C++'s export keyword was originally meant to permit the separation of the definition of a template from its usages. It proved to be incredibly difficult to implement, and the “ export templates” C++ feature was dropped in C++11, having never seen widespread usage.

What does __ Declspec Dllexport do?

__declspec(dllexport) adds the export directive to the object file so you do not need to use a . def file. This convenience is most apparent when trying to export decorated C++ function names.


2 Answers

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

like image 78
hB0 Avatar answered Sep 22 '22 03:09

hB0


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.

like image 37
Ben Voigt Avatar answered Sep 23 '22 03:09

Ben Voigt