How to call external dll function from java code

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.


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 (eg 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.


You will need to use the Java Native Interface (JNI), which is a set of C/C++ functions that allow native code to interface with java code (ie receiving parameters from java function calls, returning results, etc). Write a wrapper C library that receive JNI calls and then call your external library.

For instance, the following function invokes a method updateHandlers on a native object (that is stored as long in the Java side).

class MyImpl {
  void updateHandlers(JNIEnv *env) {
    this->contentHandler = ....;
  }
}

JNIEXPORT void JNICALL Java_package_Classname_updateHandlers0
  (JNIEnv *env, jobject obj, jlong ptr) 
{
  ((MyImpl*)ptr)->updateHandlers(env);
}

The corresponding declarations in package.ClassName are:

private long ptr; //assigned from JNI
public void updateHandlers() {
   if (ptr==0) throw new NullPointerException(); 
   updateHandlers0(ptr);
}
private native void updateHandlers0(long ptr);

static {
    try {
          /*try preloading the library external.dll*/
      System.loadLibrary("external");
    } catch (UnsatisfiedLinkError e) {
      /*library will be resolved when loading myjni*/
    }
    System.loadLibrary("myjni"); //load myjni.dll
}

I did write some time ago sample tutorial, maybe it will help.

http://wendro.blogspot.com/2010/03/jni-example-eclipse-dev-cpp.html

链接地址: http://www.djcxy.com/p/96836.html

上一篇: onmousemove事件不会从外部源中触发?

下一篇: 如何从java代码中调用外部dll函数