简体   繁体   English

来自 dll 的 Java 调用函数

[英]Java call function from a dll

I have this python script that imports a zkemkeeper dll and connects to a time attendance device( ZKTeco ).我有这个 python 脚本,它导入zkemkeeper dll 并连接到考勤设备( 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))

This works fine as expected.这按预期工作正常。 However I want to achieve the same using java.但是我想使用java实现相同的目标。 How can I call that Connect_Net method?我如何调用Connect_Net方法? I tried the following in java but didn't work:我在java中尝试了以下但没有用:

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));
    }

}

The two choices for calling native code from Java are JNI (Java Native Interface) and JNA (Java Native Access)从 Java 调用本机代码的两种选择是JNI(Java 本机接口)JNA(Java 本机访问)

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). Java 运行时可以开箱即用地执行 JNI,但您需要使用专门为 JNI 制作的函数创建一个包装库(仅放入native关键字是不够的)。

JNA is a 3rd party library that uses libffi to make native code accessible from Java. JNA 是第 3 方库,它使用libffi使本机代码可从 Java 访问。

You have to see for yourself which approach better suits your needs.您必须亲自查看哪种方法更适合您的需求。

Edit: looking at your example code again, is that a COM call?编辑:再次查看您的示例代码,这是一个 COM 调用吗? While COM can be done with JNA (doing that myself), it's quite complicated.虽然 COM 可以用 JNA 来完成(我自己做),但它相当复杂。 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).您最好的选择可能是执行实际调用的包装器 C 库或像JACOB这样的 Java/COM 桥接产品(但是从未使用过它)。

Although this is an old post, I am finalizing a pure Java ZKEM: https://github.com/mkhoudary/ZKTeco4J虽然这是一篇旧帖子,但我正在敲定纯 Java ZKEM: https : //github.com/mkhoudary/ZKTeco4J

I was using ZKemKeeper DLL with COM4J in the past: https://github.com/mkhoudary/ZKEMJavaPort我过去在 COM4J 中使用 ZKemKeeper DLL: https : //github.com/mkhaudary/ZKEMJavaPort

But the pure Java one is really robust, you may watch the repo, I am in the final stages但是纯 Java 的真的很健壮,你可以看 repo,我在最后阶段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM