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.

Reply via email to