www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling DLL coded in D from Java

reply "DlangLearner" <Bystander gmail.com> writes:
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.
Jun 16 2015
parent reply bitwise <bitwise.pvt gmail.com> writes:
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
Jun 16 2015
parent Rikki Cattermole <alphaglosined gmail.com> writes:
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.
Jun 16 2015