C# "Unmanaged Exports" [closed]

I would recommend you do this the documented way instead of relying on a undocumented hack from an author who doesn't provide support. Let's do it with an example:

namespace Publics {
    public class Class1 {
        public static void Run() { 
            // Stuff...
        }
    }
}

Add a new C++/CLI class library to your project. Right-click the solution, Add, New Project. Open the "Other Languages" node, Visual C++, CLR, and pick the "Class Library" project template. Right-click the new project, Properties, Common Properties, Framework and References, click the Add New Reference button. From the Projects tab, pick the C# project whose method(s) you want to export.

Delete the pre-generated empty class with the //TODO comment and write this kind of code:

extern "C" __declspec(dllexport)
void __stdcall Example() 
{
    Publics::Class1::Run();
}

Build your solution. Check that the Example function got exported by running dumpbin.exe /exports on the DLL. You should see something similar to this:

      1    0 00001020 _Example@0 = _Example@0

Beyond the name and the calling convention, you now also have lots of choices to tweak the exported function. If you want to export an instance method instead of a static method you could write the function like this for example:

extern "C" __declspec(dllexport)
void __stdcall Example() 
{
    Publics::Class1^ obj = gcnew Publics::Class1;
    obj->Run();
}

Etcetera, some familiarity with the C++/CLI language is required if you are going to make this elaborate. Last but not least, you are also likely to find out what went wrong in your original attempt to make Giesecke's IL rewriter work. It otherwise uses the exact same technique that the C++/CLI compiler uses to export the managed method.


I've been using version 1.1.3 and see there is now a newer version with NuGet support. I just did a test with that.

Can I somehow check if the MSBuild task that does all the magic is actually run or not?

You can get more detail from MSBuild using the command line or adjust the verbosity that Visual Studio requests: Tools > Options > Project and Solutions > Build and Run > MSBuild project build output verbosity [VS 2010]. You'll probably want to reset it as soon as you are done troubleshooting.

I saw the target and task were being called but didn't see any results until I switched the project platform to x86. Then I see various, relevant log entries including Adding .vtentry:0 .export....

What should the project file contain (it seems to be suspiciously empty to me?)

There isn't much needed in the project file. NuGet does it all: A reference to the DllExport assembly and an include for the target file.

A couple things that I can think of that might be tripping you up:

  1. Make sure you are actually building the project. The Solution Build Manager can have some projects not set to build for the selected solution configuration.
  2. Make sure you are checking the right DLL. The build task writes the path into the build log. The line starts with Assembling.