NetDllExport

Exports static methods in a managed DLL as library functions that can be called from an unmanaged Windows application.

Archived content: This application is currently inactive and may be outdated or no longer maintained or functional.

It really works: The otherwise separate .NET world allows the direct invocation of managed functions from unmanaged applications. This means that you can for instance call functions in a class library (DLL) programmed with C# from native C++, Delphi or VB programs without the help of COM just like with any other DLL. This allows you to code DLLs for unmanaged programs in C# or to make C# code accessible to native applications.

The trick works by manipulating the IL code of the .NET DLL. Actually, .NET is already prepared to start up the CLR upon loading the DLL from an unmanaged application. There is just no directly accessible switch for that. So you would need to do some hand work first. The compiled DLL must be converted to IL code with the IL disassembler ildasm (part of the Windows SDK), then a few modifications are required, and finally the DLL is re-assembled with ilasm. As this work needs to be repeated after every build, I wrote this helper program for a project once to get the work done quicker.

Simply calling NetDllExport.exe with the file name of the DLL file to be processed, all public static methods of the class DllExport are exported under their function name and the new DLL is written to the same file name. More options like selecting a different class name, output file, or a debug build are available through command line parameters. An overview is displayed with NetDllExport --help.

A CodeProject article describes the background in greater detail. Unlike the proposed solution there, Net­Dll­Export does not require any additional references or attribute definitions because simply all public static methods of the specified class are exported.

Compatibility: .NET Version 2.0 Windows 10 Windows 8 Windows 7 Windows XP

A similar program that I’ve found is the NuGet package Unmanaged Exports (DllExport for .Net) (also supports the x64 platform).

Download

NetDllExport.exe20.0 KiBProgram file, version 1.3, .NET 4.0

master.zipLatest source code directly from GitHub

Previous versions

There’s a public Git repository of NetDllExport on GitHub.

Usage notes

NetDllExport was only tested with DLLs for the framework 4.0 (Visual Studio 2010) and only for 32 bit DLLs. Support for 64 bit DLLs needs to be added in the source code by setting the correct .corflags. The older version 1.2.1 only supports .NET 2.0 (Visual Studio 2005) which is unsupported from version 1.3 on due to the lack of a testing environment.

The processed DLL may currently be built for x86 or Any CPU. NetDllExport always changes this setting to x86.

Licence and terms of use

This software is released under the terms of the simplified BSD licence. You can find the detailed terms and conditions in the download.

Statistic data

  • Created on 2010-04-05, updated on 2016-02-22.
  • Ca. 290 lines of code, estimated development costs: 290 - 1 200 €