Click here to Skip to main content
6,822,613 members and growing! (19,350 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

How to Automate Exporting .NET Function to Unmanaged Programs

By Selvin

Post-build tool which can automate exporting .NET function to unmanaged programs
C#, VB, Windows, .NET, Visual-Studio, Dev
Posted:9 Nov 2006
Updated:21 Nov 2006
Views:51,881
Bookmarked:49 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 5.48 Rating: 4.02 out of 5
2 votes, 8.7%
1
2 votes, 8.7%
2

3
2 votes, 8.7%
4
17 votes, 73.9%
5

Introduction

Exporting functions to unmanaged programs - why not?
But 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:

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

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

    and

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

References

License

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

About the Author

Selvin


Member

Occupation: Systems / Hardware Administrator
Location: Poland Poland

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 60 (Total in Forum: 60) (Refresh)FirstPrevNext
GeneralFantastic Pinmembervbfengshui18:05 30 Oct '09  
GeneralProblem with debugging PinmemberSteven123456789022:02 7 Oct '09  
GeneralFixed some mistakes, updated source Pinmembericestudent_r18:40 31 Aug '09  
GeneralRe: Fixed some mistakes, updated source Pinmember_marber_3:35 25 Sep '09  
GeneralRe: Fixed some mistakes, updated source Pinmembericestudent_r3:50 25 Sep '09  
GeneralRe: Fixed some mistakes, updated source Pinmember_marber_5:34 25 Sep '09  
GeneralRe: Fixed some mistakes, updated source PinmemberSelvin6:03 25 Sep '09  
GeneralRe: Fixed some mistakes, updated source Pinmember_marber_6:14 25 Sep '09  
QuestionEvents Pinmemberfayring23:37 6 Jun '09  
GeneralExporting Errors in VB.NET, prolly my stupidity... PinmemberJuvenal16:58 1 Apr '09  
GeneralUnable to load one or more of the requested types Pinmembernormanr5:55 25 Jan '09  
GeneralRe: Unable to load one or more of the requested types Pinmembernormanr6:21 25 Jan '09  
QuestionExporting problem using the "DLLExport"-Function Pinmembersaplpete11:15 1 Dec '08  
AnswerRe: Exporting problem using the "DLLExport"-Function PinmemberSelvin6:45 3 Dec '08  
GeneralRe: Exporting problem using the "DLLExport"-Function Pinmembersaplpete1:24 7 Dec '08  
GeneralRe: Exporting problem using the "DLLExport"-Function Pinmembersaplpete8:54 10 Dec '08  
GeneralRe: Exporting problem using the "DLLExport"-Function PinmemberSelvin12:24 10 Dec '08  
GeneralRe: Exporting problem using the "DLLExport"-Function Pinmembersaplpete11:17 11 Dec '08  
GeneralRe: Exporting problem using the "DLLExport"-Function PinmemberSelvin13:14 11 Dec '08  
GeneralRe: Exporting problem using the "DLLExport"-Function Pinmembersaplpete11:59 12 Dec '08  
QuestionRe: Exporting problem using the "DLLExport"-Function PinmemberFilip Vande Weghe2:22 15 Jul '09  
AnswerRe: Exporting problem using the "DLLExport"-Function PinmemberFilip Vande Weghe5:57 16 Jul '09  
Questionmissing dependencies PinmemberMagick9310:39 23 Sep '08  
AnswerRe: missing dependencies PinmemberSelvin1:23 24 Sep '08  
Generalhow can I build it in vc6++? PinmemberK.O.6:25 17 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 21 Nov 2006
Editor: Deeksha Shenoy
Copyright 2006 by Selvin
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project