Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call external dll function from java code

Tags:

java

dll

call

jna

I need to call external DLL library function from Java code. I use Netbeans 7.2. My dll's functions are:

Boolean  isValid(string  word)
List<String> getWords(String  word)

I'm following this example. But I don't know how declare my dll functions. And I found another link. But it doesn't work for me.

like image 269
Temp Temp Avatar asked Feb 21 '13 08:02

Temp Temp


1 Answers

I stumbled upon the same problem of "calling DLL from Java" and first was frustrated about the complexity. Yet, there is an elegant solution (might also be interesting for the people over there in the processing.org habitat..) Given the rather "general" form of the question (maybe, downrating is not justified for that), I suppose, a rather easy-going solution would be indicated. In other words, a solution that avoids messing aronud with header files, extra conversions, etc., just as the source code is not necessarily available.

My recommendation for that would be JNA (https://github.com/twall/jna), which basically is a simplifying wrapper around JNI. It works great, type mapping is straightforward (e.g. pchar = lpcstr buffer -> string), though I am using it only towards Windows DLLs and my own C-style DLLs created using Delphi-Pascal. The only thing to consider is that return values should be exported through functions rather than "out" flagged reference variables. The question already points to a linked source that provides an example for that (so, answers around JNI may be misplaced here). Note that the link I provided also contains axamples for the transfer of arrays and pointers.

like image 183
monnoo Avatar answered Sep 29 '22 16:09

monnoo