Native DLL Exports from .NET Assembly

I was amused to find out that .NET assemblies can do native DLL exports with one and easy trick:

.method static void Rainbow()
{
    .export [1]
    ldstr      "Rainbow"
    call       void [mscorlib]System.Console::WriteLine(string)
    ret
}

However there is no built-in way to do that in C# or VB. What I wanted to have is [DllExport] attribute as opposed to the existing [DllImport].

One probably could scribble some IL code manually but it would be a tough experience for one's project. That would be just counter-productive to maintain.

With the advent of loved and hated NuGet, now it is possible to do cool things during the build of your project. And yes, you can instantly have the much-wanted [DllExport] attribute thanks to Robert Giesecke and his excellent Unmanaged Exports (DllExport for .Net) package:

class Program
{
    [DllExport]
    static void Rainbow()
    {
        Console.WriteLine("Rainbow");
    }
}

It just works and it's freaking awesome.

An interesting part is that DLL exports render the resulting assembly as a mixed-mode assembly. And here is what Eazfuscator.NET said when such assembly was obfuscated:

Error: Mixed-mode assemblies obfuscation is not supported.  

Not anymore! The new Eazfuscator.NET 4.6 instantly handles mixed-mode assemblies with DLL exports:

Obfuscating assembly 'NativeExports.dll'...  
Done  

Yay!

I had one of the toughest coding sessions to deliver that feature. The intricate layouts of executable file, V‑Table fixups, .reloc section, native code analysis and generation. A pure geek joy. I am endlessly happy that I can share this achievement with you.

P.S. Looking for a way to put [DllExport] and native parts together? Download sample project

comments powered by Disqus