Skip to content

taylorh140/Calling-NET-from-D

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calling-NET-from-D

An example of how I set up a call to .NET on windows.

If you do the following you might be able to:

  • Create a .NET DLL that can be called from D.
  • Use function calls to move data between .NET and D

First think to make sure of is that you have the correct environment to get this set up.

  1. Install the latest VS Community Edition. I'm using 2013, but It also works with 2015.

  2. Install or Re-install the DMD compiler.

    NOTE: This step is important because when dmd installs it checks the environment for VS and if it finds it compiles the runtime/phobos for 32/64 bit coff.

  3. Start a new Project-> Class Library

    Nuget

    Nuget

  4. Open the Nuget Console

    NOTE: This step uses a nuget library Unmanaged exports

    Nuget

  5. Enter the following command into the PM prompt

    Install-Package UnmanagedExports
    

    Nuget

  6. Set the target for the dll, I use x86 in the example. To do so Open the Configuration Manager

    Nuget

  7. Add the static functions and includes

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using RGiesecke.DllExport;             //For DllExport
    using System.Runtime.InteropServices;  //For CallingConvention
    
    namespace D_TO_NET
    {
        public class Class1
        {
            [DllExport("csHello", CallingConvention = CallingConvention.Cdecl)]
            static string Hello()
            {
                string message = "Hello, MessageBox!";
                // Show message box
                return message;
            }
        }
    }
  8. Compile .NET then copy the .dll and the .lib file to the same directory as your d code.

    /*caller.d*/
    import std.stdio;
    import std.string;
    
    pragma(lib, "D_TO_NET.lib"); //Use to import library
    
    extern(C) char * csHello(); //Name of the C interface function
    
    string Hello(){return cast(string) csHello().fromStringz();} //An optional Wrapper
    
    void main(){
    	writeln(Hello());
    }
  9. To compile the d code at least for 32 bit use:

    dmd caller.d -m32mscoff
  10. If everything worked you should get the a responce from .NET

    Hello, MessageBox!
    

Troubleshooting

if you get a strange error such as the following:

error : syntax error at token '{' in: {

it may be due to a language setting in windows. According to http://stackoverflow.com/questions/20288469/cant-create-unmanaged-dll-using-c-sharp-and-robert-gieseckes-unmanaged-exports

setting the default language of non-unicode programs may be of help. "Control Panel > Language and Regional Settings > Additional > Change language for applications that dont support Unicode"

About

An example of how I set up a call to .NET

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published