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.
-
Install the latest VS Community Edition. I'm using 2013, but It also works with 2015.
-
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.
-
Start a new Project-> Class Library
-
Open the Nuget Console
NOTE: This step uses a nuget library Unmanaged exports
-
Enter the following command into the PM prompt
Install-Package UnmanagedExports
-
Set the target for the dll, I use x86 in the example. To do so Open the Configuration Manager
-
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; } } }
-
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()); }
-
To compile the d code at least for 32 bit use:
dmd caller.d -m32mscoff
-
If everything worked you should get the a responce from .NET
Hello, MessageBox!
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"