Click here to Skip to main content
Click here to Skip to main content

How to Automate Exporting .NET Function to Unmanaged Programs

, 21 Nov 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Post-build tool which can automate exporting .NET function to unmanaged programs


Exporting functions to unmanaged programs - why not? I don't want to change *.il files by hand. I need to automate this. So here we go...


A few days ago, I was writing a program (Terminal Services addin). The first problem was that I couldn't write this in C# ... Ok. I started writing it in C++/CLI. But there was another problem - mstsc.exe doesn't like to load such a library. Ok. No problem. I started another project (in C++/CLI - the first I change to pure C++) which exports one function. The first library loaded the second one and everything worked. But for dependencies, I needed easy deployment without installing VC80 runtimes... So I tried to find a way to export the function from C#. And I found it. While I was working with a project, I compiled it often and changing *.il files by hand was really annoying. That's why I made this tool. (Anyway later I found that the first library can be written in C# but it is a topic for another article - "How to write Terminal Services addin in pure C#" .)

Using the Code

Well the code is just the code ... it's a home-grown parser with some other functionality... So I'll rather write how to use it (and sometimes how it works).


What you need to use it:

  • Framework 2.0 SDK
  • Visual Studio 2005 (optional)

First you should download the code (there are two projects ExportDll.exe and ExportDllAttribute.dll) and build it.

Then you should know how to use it... After creating the library project in C# or VB.NET, you need to add dependency to ExportDllAttribute. "Oh no, not another dependency" - you'll say. Easy ... just wait ... This library contains an attribute which tells ExportDll.exe that some function needs to be exported.

How It Works

As I said, ExportDll.exe needs to know which functions need to be exported. So you'll write something like this:

public class SomeClassName
    //second parameter of attribute is calling convention,
    //default is StdCall but u can use others fx.:
    public static void SomeFunctionName()
        System.Console.WriteLine("I really do nothing");

After compiling, you need to use ExportDll tool to export this:

ExportDll.exe full_path_to_yours_dll\yoursdll.dll [/Release|/Debug]

Note: /Release flag is default. What exactly does ExportDll do:

  • Reads DLL as assembly and makes a dictionary of exported function
  • Decompiles DLL
  • Searches and replaces some stuff in IL code (If you want to know what, read the article that I mentioned in the "Background" section.)
  • Deletes ExportDllAttribute dependency

    .assembly extern ExportDllAttribute
      .ver x:x:x:x


    .custom instance void
        [ExportDllAttribute]ExportDllAttribute.ExportDllAttribute ...
  • Compiles modified IL code again

Post-building in Visual Studio 2005

How? It's easy... Just add in Project property named "Post-build event command".

"path_to_tools\ExportDll.exe" "$(TargetPath)" /$(ConfigurationName)

OMG! It's Not Working

Hmm... Yes, forgot to mention about ExportDll.exe.cofig ...

<?xml version="1.0" encoding="utf-8" ?>
        <sectionGroup name="applicationSettings" 
        System, Version=, Culture=neutral, 
        PublicKeyToken=b77a5c561934e089" >
            <section name="ExportDll.Properties.Settings"
        System, Version=, Culture=neutral,
        PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <setting name="ildasmpath" serializeAs="String">
                <value>C:\Program Files\
                Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm.exe</value>
            <setting name="ilasmpath" serializeAs="String">

You need to configure path to ilasm and ildasm.


New version

  • now accept MarshalAsAtrribute
  • new commands in command line support: first had to be TargetPath then you can add:
    /release or /debug - /release id default
    /AnyCPU or /x64 - /AnyCPU is default its for 32/64 bit (for itanium add /PE64 /ITANIUM)
    ... any valid command for ildasm.exe
  • support fo x64
    "path_to_tools\ExportDll.exe" "$(TargetPath)" /$(ConfigurationName) /$(PlatformName)
    if PlatformName == "AnyCPU" then compiles for 32 bit
    if PlatformName == "x64" then compiles for 64 bit
  • now its compiled for runtime 4.0(ExportDll.exe) and 2.0 (ExportDllAttribut.dll) so it should be working for frameworks 2.0 - 4.0
  • it can compile for 2.0 or 4.0 runtime (runtime its not framework - frm 3.0 and 3.5 are using 2.0 runtime)
    it can be change by changing ilasmpath setting so:
    2.0 => C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ilasm.exe
    4.0 => C:\Windows\Microsoft.NET\Framework\v4.0.30319\ilasm.exe



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Systems / Hardware Administrator
Poland Poland
No Biography provided

Comments and Discussions

QuestionRe: Export using struct as a parameter Pinmembermla15427-Feb-13 11:01 
GeneralMy vote of 5 Pinmemberdhsc4-Dec-12 3:57 
GeneralRe: My vote of 5 PinmemberSelvin4-Dec-12 3:59 
GeneralMy vote of 5 PinmemberggxPrince24-Feb-12 17:12 
QuestionMantain DLL state Pinmembervss4-Jan-12 13:25 
AnswerRe: Mantain DLL state PinmemberSelvin5-Jan-12 5:35 
QuestionCompile Error PinmemberMember 835485227-Oct-11 18:29 
QuestionTarget should be dll PinmemberMember 835485227-Oct-11 16:50 
AnswerRe: Target should be dll PinmemberMember 835485227-Oct-11 18:28 
SuggestionChanging Calling Convention Issue Pinmemberheinfrisch15-Sep-11 18:59 
QuestionArguments to DLLExport Pinmembercarl looper15-Aug-11 18:08 
QuestionHow about c# dll for Pocket PC Pinmemberalfwolf_ss8-Aug-11 21:45 
AnswerRe: How about c# dll for Pocket PC PinmemberSelvin8-Aug-11 21:52 
GeneralRe: How about c# dll for Pocket PC Pinmemberalfwolf_ss9-Aug-11 18:44 
GeneralRe: How about c# dll for Pocket PC PinmemberSelvin9-Aug-11 22:55 
GeneralAnother Vote o 5 PinmemberKeith Lockwood20-Jul-11 7:00 
QuestionTrouble passing strings Pinmemberbkovacic24-Jun-11 7:13 
GeneralProblem with ExportDll function in ExportDllAttribute PinmemberMember 45233015-Jun-11 23:01 
I am getting problem with the solution you mentioned.What i did is downloaded your project and converted it into VS2010 ,successfully build it and Add the exportDllAttribute reference in my C# dll project.But when i compile the solution it gave me the following errors

error CS0426: The type name 'ExportDll' does not exist in the type 'ExportDllAttribute'
error CS0426: The type name 'ExportDllAttribute' does not exist in the type 'ExportDllAttribute'

Below is the code i used

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

using System.Text;
using System.Runtime.CompilerServices;

namespace DotNetLibrary
public class DotNetClass

[return: MarshalAs(UnmanagedType.AsAny)]
public static string DotNetMethod(string input)
return "Hello " + input;

Kindly let me know if i am doing something wrong.

Hassan Tariq
GeneralRe: Problem with ExportDll function in ExportDllAttribute PinmemberMember 45233015-Jun-11 23:08 
GeneralDeclare someclass where? Pinmemberocean_blue417-May-11 22:04 
GeneralRe: Declare someclass where? PinmemberSelvin17-May-11 22:22 
GeneralRe: Declare someclass where? Pinmemberocean_blue417-May-11 22:35 
GeneralRe: Declare someclass where? PinmemberSelvin17-May-11 22:45 
GeneralMy vote of 5 Pinmemberjoffreyb11-May-11 18:51 
GeneralN0 exported functions... PinmemberDonald Neisler15-Mar-11 11:34 
GeneralRe: N0 exported functions... PinmemberSelvin18-Mar-11 17:39 
GeneralTAB Key not working Pinmembervss15-Mar-11 7:23 
GeneralRe: TAB Key not working PinmemberSelvin18-Mar-11 17:34 
GeneralMarshalling Object Pinmembersaplpete24-Jan-11 8:46 
GeneralRe: Marshalling Object PinmemberSelvin25-Jan-11 0:18 
GeneralRe: Marshalling Object Pinmembersaplpete25-Jan-11 11:35 
GeneralRe: Marshalling Object PinmemberSelvin25-Jan-11 12:34 
GeneralRe: Marshalling Object Pinmembersaplpete26-Jan-11 8:22 
GeneralRe: Marshalling Object PinmemberSelvin26-Jan-11 8:27 
GeneralRe: Marshalling Object PinmemberSelvin26-Jan-11 8:47 
GeneralRe: Marshalling Object [modified] Pinmembersaplpete27-Jan-11 8:38 
GeneralRe: Marshalling Object Pinmembersaplpete13-Feb-11 5:59 
GeneralRe: Marshalling Object Pinmembersaplpete15-Feb-11 11:07 
GeneralMy vote of 5 PinmemberSAKryukov6-Jan-11 10:54 
GeneralRegister for COM interop or RegAsm Problem PinmemberMember 25330798-Dec-10 1:59 
GeneralRe: Register for COM interop or RegAsm Problem PinmemberSelvin8-Dec-10 3:09 
GeneralRe: Register for COM interop or RegAsm Problem PinmemberMember 25330798-Dec-10 13:05 
GeneralRe: Register for COM interop or RegAsm Problem PinmemberSelvin9-Dec-10 2:06 
GeneralRe: Register for COM interop or RegAsm Problem PinmemberMember 253307915-Dec-10 20:42 
GeneralRe: Register for COM interop or RegAsm Problem PinmemberMember 253307916-Dec-10 23:57 
GeneralRe: Register for COM interop or RegAsm Problem PinmemberSelvin21-Dec-10 1:18 
Generalstdcall decoration Pinmembersquidward20-Oct-10 0:04 
GeneralRe: stdcall decoration PinmemberSelvin22-Oct-10 7:23 
GeneralBetter solution!! Pinmembergorallo27-Aug-10 23:13 
GeneralRe: Better solution!! PinmemberSelvin28-Aug-10 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 22 Nov 2006
Article Copyright 2006 by Selvin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid