DllExport will create a DLL that exposes standard C style function calls which are transparently thunked to static methods in a .NET assembly.

To export a static method in a .NET class, mark each method with an attribute called DllExportAttribute (see help for an example attribute class)

Usage:
   DllExport [options] Assembly.dll -- This creates the native thunks in an
                                       assembly called $TargetAssembly.dll

                                       This is good for development--it
                                       preserves the original assembly and
                                       allows for easy debugging.
   Options:
        --merge                     -- this will generate the thunking
                                       functions and merge them into the
                                       target assembly.

                                       This is good for when you want to
                                       produce a release build.

                                       ** Warning **
                                       This overwrites the target assembly.

        --keep-temp-files           -- leaves the working files in the
                                       current directory.

        --rescan-tools              -- causes the tool to search for its
                                       dependent tools (ilasm and ildasm)
                                       instead of using the cached values.

        --no-logo                   -- suppresses informational messages.

   More Help:

        DllExport --help            -- Displays this help.

        DllExport --sampleClass     -- Displays the DllExportAttribute
                                       source code that you should include
                                       in your assembly.

        DllExport --sampleUsage     -- Displays some examples of using the
                                       DllExport attribute.


The DllExport Attribute Class:

using System;
using System.Runtime.InteropServices;

/// <summary>
/// This class is used by the DllExport utility to generate a C-style
/// native binding for any static methods in a .NET assembly.
///
/// Namespace is not important--feel free to set the namespace to anything
/// convenient for your project.
/// -----------------------------------------------------------------------
/// (c) 2009 Microsoft Corporation -- All rights reserved
/// This code is licensed under the MS-PL
/// http://www.opensource.org/licenses/ms-pl.html
/// Courtesy of the Open Source Techology Center: http://port25.technet.com
/// -----------------------------------------------------------------------
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class DllExportAttribute: Attribute {
    public DllExportAttribute(string exportName)
        : this(CallingConvention.StdCall,exportName) {
    }

    public DllExportAttribute(CallingConvention convention, string name) {
        ExportedName = name;
        this.CallingConvention = convention;
    }

    public string ExportedName { get; set; }
    public CallingConvention CallingConvention { get; set; }
}



To use the DllExport Attribute in your code, include the class in your project. Namespace is not important.

On any method you wish to export as a C-style function, simply use the attribute on any static method in a class:

...

// example 1
// note the exported function name doesn't have to match the method name
[DllExport("myFunction")]
public static int MyFunction( int age, string name ){
   // ....
}

// example 2
// On this example, we've marked set the calling convention to CDECL
[DllExport( CallingConvention.Cdecl, "myNextFunction")]
public static int MyFunctionTwo( float someParameter, string name ){
   // ....
}



Last edited Jun 20, 2009 at 3:54 PM by fearthecowboy, version 1

Comments

No comments yet.