dll的Java调用功能 [英] Java call function from a dll
问题描述
我有这个python脚本,可以导入 zkemkeeper
dll并连接到考勤设备( ZKTeco
)。这是我正在使用的脚本:
I have this python script that imports a zkemkeeper
dll and connects to a time attendance device(ZKTeco
). Here is is a script I'm using:
from win32com.client import Dispatch
zk = Dispatch("zkemkeeper.ZKEM")
zk.Connect_Net("192.168.0.17", 4370)
print(zk.StartIdentify())
print(zk.StartEnrollEx(7, 2, 1))
这可以正常工作。但是我想使用java实现相同的目的。如何调用 Connect_Net
方法?
我在Java中尝试了以下操作,但没有起作用:
This works fine as expected. However I want to achieve the same using java. How can I call that Connect_Net
method?
I tried the following in java but didn't work:
public class ZKEM {
static {
System.loadLibrary("zkemkeeper");
}
ZKEM() {
}
public static native boolean Connect_Net(String IPAdd, int Portl);
}
public class Main {
public static void main(String[] args) {
System.err.println(ZKEM.Connect_Net("192.168.0.17", 4370));
}
}
推荐答案
从Java调用本机代码的两个选择是 JNI(Java本机接口)和 JNA(Java本机访问)
The two choices for calling native code from Java are JNI (Java Native Interface) and JNA (Java Native Access)
Java运行时可以开箱即用地执行JNI,但是您需要创建一个包装库,其中包含专门为JNI设计的功能(只需放入 native
关键字是不够的。)
The Java runtime can do JNI out of the box, but you need to create a wrapper library with functions specifically made for JNI (just putting in a native
keyword is not enough).
JNA是使用 libffi 以便可以从Java访问本机代码。
JNA is a 3rd party library that uses libffi to make native code accessible from Java.
您必须亲自了解哪种方法更好满足您的需求。
You have to see for yourself which approach better suits your needs.
编辑:再次查看示例代码,这是COM调用吗?虽然可以使用JNA来完成COM(自己动手做),但它相当复杂。最好的选择可能是包装C库,它可以进行实际的调用或Java / COM桥产品,例如 JACOB (不过,从未使用过)。
looking at your example code again, is that a COM call? While COM can be done with JNA (doing that myself), it's quite complicated. Your best bet is probably a wrapper C library that does the actual calls or a Java/COM bridge product like JACOB (have never used it, however).
这篇关于dll的Java调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!