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:

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

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.exe4.0 => C:\Windows\Microsoft.NET\Framework\v4.0.30319\ilasm.exe

Comments and Discussions

The DllExport parser should detect "DllMain" method and add ".entrypoint" directive in ".il" file.
the ".entrypoint" is present but when the 3rd part exe (not .Net code) try to load dll it generate a loading dll error.
Profiling the exe with Dependency Walker it seem that call to DllMain return null.

i already try the tutorial but when i try to exportdll.exe i got this message could not load file or assembly 'ExportDllAttribute version 1.0.0.0 ...' or one of its depedencies. the system cannot find the file specified

The first problem I encountered is usum static object to mantain state. Using the Dll en a c# program It works as expected. Using it oc C++ it assert on caling the exported function.

Also if in the exported funcion I call a second function (not exported) that uses .Net imported classes as parameter, the function assert oc calling. If i put the code on the body of the functio It works correcly.

Last on function assert it is very hard to understand the motive, because it not catch by try catch.

There seems to be an issue with changing the calling convention. It does not (always) seem to work, at least not in my case.
Whatever I put as parameter 2 of ExportDllAttribute, it always generates StdCall.

I took a look at your code and had to add one statement to make it work:
In ExportDll, line 315 of file Program.cs contained

I spent a full day searching the internet for answer to this question. There were so many dead ends, but this was EXACTLY what I was looking for. Your solution works flawlessly. Thank you so very much.

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;