How to call external dll function from java code

Temp Temp picture Temp Temp · Feb 21, 2013 · Viewed 36k times · Source

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.

Answer

monnoo picture monnoo · Sep 30, 2013

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.