Click here to Skip to main content
15,868,292 members
Articles / Programming Languages / XML

How to Automate Exporting .NET Function to Unmanaged Programs

Rate me:
Please Sign up or sign in to vote.
4.90/5 (44 votes)
21 Nov 2006MIT3 min read 454K   4.5K   94   143
Post-build tool which can automate exporting .NET function to unmanaged programs

Introduction

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...

Background

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).

Beginning

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:

C#
public class SomeClassName
{
    //second parameter of attribute is calling convention,
    //default is StdCall but u can use others fx.:
    [ExportDllAttribute.ExportDll("ExportNameOfFunction",
    System.Runtime.InteropServices.CallingConvention.Cdecl)]
    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

    MSIL
    .assembly extern ExportDllAttribute
    {
      .ver x:x:x:x
    }

    and

    MSIL
    .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
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" 
        type="System.Configuration.ApplicationSettingsGroup, 
        System, Version=2.0.0.0, Culture=neutral, 
        PublicKeyToken=b77a5c561934e089" >
            <section name="ExportDll.Properties.Settings"
        type="System.Configuration.ClientSettingsSection,
        System, Version=2.0.0.0, Culture=neutral,
        PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <ExportDll.Properties.Settings>
            <setting name="ildasmpath" serializeAs="String">
                <value>C:\Program Files\
                Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm.exe</value>
            </setting>
            <setting name="ilasmpath" serializeAs="String">
                <value>C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ilasm.exe
                </value>
            </setting>
        </ExportDll.Properties.Settings>
    </applicationSettings>
</configuration>

You need to configure path to ilasm and ildasm.

Updates

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

References

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Systems / Hardware Administrator
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNot working Pin
Tim Osborn27-May-10 19:26
Tim Osborn27-May-10 19:26 
GeneralRe: Not working Pin
Tim Osborn27-May-10 20:26
Tim Osborn27-May-10 20:26 
GeneralRe: Not working Pin
Selvin31-May-10 20:50
Selvin31-May-10 20:50 
GeneralFantastic Pin
vbfengshui30-Oct-09 17:05
vbfengshui30-Oct-09 17:05 
GeneralRe: Fantastic Pin
Member 25330798-Nov-10 20:36
Member 25330798-Nov-10 20:36 
GeneralProblem with debugging Pin
Steven12345678907-Oct-09 21:02
Steven12345678907-Oct-09 21:02 
GeneralFixed some mistakes, updated source Pin
icestudent_r31-Aug-09 17:40
icestudent_r31-Aug-09 17:40 
GeneralRe: Fixed some mistakes, updated source Pin
_marber_25-Sep-09 2:35
_marber_25-Sep-09 2:35 
GeneralRe: Fixed some mistakes, updated source Pin
icestudent_r25-Sep-09 2:50
icestudent_r25-Sep-09 2:50 
GeneralRe: Fixed some mistakes, updated source Pin
_marber_25-Sep-09 4:34
_marber_25-Sep-09 4:34 
GeneralRe: Fixed some mistakes, updated source Pin
Selvin25-Sep-09 5:03
Selvin25-Sep-09 5:03 
GeneralRe: Fixed some mistakes, updated source Pin
_marber_25-Sep-09 5:14
_marber_25-Sep-09 5:14 
GeneralRe: Fixed some mistakes, updated source Pin
maitpg17-May-10 5:06
maitpg17-May-10 5:06 
GeneralRe: Fixed some mistakes, updated source Pin
Member 308635131-May-10 0:54
Member 308635131-May-10 0:54 
QuestionEvents Pin
fayring6-Jun-09 22:37
fayring6-Jun-09 22:37 
GeneralExporting Errors in VB.NET, prolly my stupidity... Pin
Juvenal1-Apr-09 15:58
Juvenal1-Apr-09 15:58 
GeneralUnable to load one or more of the requested types Pin
normanr25-Jan-09 4:55
normanr25-Jan-09 4:55 
GeneralRe: Unable to load one or more of the requested types Pin
normanr25-Jan-09 5:21
normanr25-Jan-09 5:21 
QuestionExporting problem using the "DLLExport"-Function Pin
saplpete1-Dec-08 10:15
saplpete1-Dec-08 10:15 
AnswerRe: Exporting problem using the "DLLExport"-Function Pin
Selvin3-Dec-08 5:45
Selvin3-Dec-08 5:45 
GeneralRe: Exporting problem using the "DLLExport"-Function Pin
saplpete7-Dec-08 0:24
saplpete7-Dec-08 0:24 
GeneralRe: Exporting problem using the "DLLExport"-Function Pin
saplpete10-Dec-08 7:54
saplpete10-Dec-08 7:54 
GeneralRe: Exporting problem using the "DLLExport"-Function Pin
Selvin10-Dec-08 11:24
Selvin10-Dec-08 11:24 
GeneralRe: Exporting problem using the "DLLExport"-Function Pin
saplpete11-Dec-08 10:17
saplpete11-Dec-08 10:17 
GeneralRe: Exporting problem using the "DLLExport"-Function Pin
Selvin11-Dec-08 12:14
Selvin11-Dec-08 12:14 

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

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