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.

Leave a Comment