我是JNI的新手。并提出几个问题: JNI可以处理windows中存在的每种类型的DLL吗?我想链接一个库,但它给了我错误。有可能JNI和dll不兼容吗?
摘自VB .NET(可行)
Private Declare Function ConnectReader Lib "rfidhid.dll" () As Integer
Private Declare Function DisconnectReader Lib "rfidhid.dll" () As Integer
Private Declare Function SetAntenna Lib "rfidhid.dll" (ByVal mode As Integer) As Integer
Java的完整代码
公共类MainForm {
/**
* @param args
*/
public native int ConnectReader();
public static void main(String[] args) {
// TODO Auto-generated method stub
MainForm mf = new MainForm();
System.out.println(mf.ConnectReader());
}
static {
System.loadLibrary("rfidhid");
}
}
显示错误代码
Exception in thread "main" java.lang.UnsatisfiedLinkError: MainForm.ConnectReader()I
at MainForm.ConnectReader(Native Method)
at MainForm.main(MainForm.java:13)
有人能指出我可能做错的地方吗?
编辑:
结果我应该这样做(并且有效)
import com.sun.jna.Library;
import com.sun.jna.Native;
public class HelloWorld {
public interface MyLibrary extends Library {
public int ConnectReader();
}
public static void main(String[] args) {
Kernel32 lib = (MyLibrary) Native.loadLibrary("rfidhid", MyLibrary.class);
System.out.println(lib.ConnectReader());
}
}
答案 0 :(得分:3)
JNI访问使用损坏的名称;您需要使用正确的名称和参数将系统API包装在JNI存根中。您无法直接调用系统API。
我建议彻底研究JNI,或使用像JNA之类的东西来减轻一些苦差事。
例如,给定Java:
package com.mycompany.jni;
public MyClass
{
native boolean someMethod(String arg)
...
}
链接的本地方法类似于:
JNIEXPORT jboolean JNICALL Java_com_mycompany_jni_MyClass_someMethod(JNIEnv *jep, jobject thsObj, jstring arg)