Thread overview
Calling DLL coded in D from Java
Jun 16, 2015
DlangLearner
Jun 17, 2015
bitwise
Jun 17, 2015
Rikki Cattermole
June 16, 2015
I'd like to know if it is possible to call an DLL coded in D from Java? I don't have any knowledge on DLL calling mechanism, so I am wondering if this is possible or any special procedure should be followed.

I indeed tried a small example but didn't succeed. First I created an DLL from the template in VisualD as below:

import std.c.windows.windows;
import core.sys.windows.dll;
import std.c.stdio;

__gshared HINSTANCE g_hInst;

extern(C)

export void dllprint() {
	printf("hello dll world\n");
}

extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved)
{
    switch (ulReason)
    {
        case DLL_PROCESS_ATTACH:
            g_hInst = hInstance;
            dll_process_attach(hInstance, true);
            break;

        case DLL_PROCESS_DETACH:
            dll_process_detach(hInstance, true);
            break;

        case DLL_THREAD_ATTACH:
            dll_thread_attach(true, true);
            break;

        case DLL_THREAD_DETACH:
            dll_thread_detach(true, true);
            break;

        default:
            break;
    }
    return true;
}

and its DEF file:

LIBRARY      "z_dll.dll"
EXETYPE NT
SUBSYSTEM WINDOWS
CODE SHARED EXECUTE
DATA WRITE
EXPORTS
	dllprint

I am able to call the generated DLL from D. For calling from Java, I used JNA, and the code is:

public class TestDlangDLL {
	public interface DlangDLL extends Library {

		DlangDLL INSTANCE = (DlangDLL) Native.loadLibrary("C:\\z_dll",
				DlangDLL.class);

		void dllprint();
	}

	public static void main(String[] args) {
		DlangDLL dDLL = DlangDLL.INSTANCE;
		System.out.println(dDLL.toString()); //fine here
		dDLL.dllprint(); //crash on this call
	}
}

This test program is able to load the DLL, but unable to call the exported function dllprint.

Any guidance is appreciated.

June 17, 2015
On Tue, 16 Jun 2015 18:47:03 -0400, DlangLearner <Bystander@gmail.com> wrote:

> I'd like to know if it is possible to call an DLL coded in D from Java?

What you're looking for is JNI (Java Native Interface).

If you export your D functions correctly, as you have done(extern(C) export) then you can call them the same way you would a C DLL.

This tutorial seems like it may have what you need:
http://www.codeproject.com/Articles/2876/JNI-Basics

  Bit
June 17, 2015
On 17/06/2015 2:18 p.m., bitwise wrote:
> On Tue, 16 Jun 2015 18:47:03 -0400, DlangLearner <Bystander@gmail.com>
> wrote:
>
>> I'd like to know if it is possible to call an DLL coded in D from Java?
>
> What you're looking for is JNI (Java Native Interface).
>
> If you export your D functions correctly, as you have done(extern(C)
> export) then you can call them the same way you would a C DLL.
>
> This tutorial seems like it may have what you need:
> http://www.codeproject.com/Articles/2876/JNI-Basics
>
>    Bit

Next time I stream at https://www.livecoding.tv/alphaglosined/ I'll be working on a Java parser to make D and Java interop a lot easier in D.
Feel free to drop by and ask questions.

Normally I stream UTC+0 12pm Monday and Tuesday. But might stream tonight. Follow me there or on twitter to get a notification of when I stream.