【问题标题】:Using Java to access a third party DLL使用 Java 访问第三方 DLL
【发布时间】:2015-08-03 19:44:47
【问题描述】:

我正在尝试为科学研究设备编写一个 Java 程序,该程序使用用 C 编写的 National Instruments 驱动程序 (DLL)。目前我对这些 DLL 一无所知。如果需要,我可以通过我的客户联系 NI 以获取详细信息。

我的 C/C++ 技能很古老,所以我宁愿避免任何需要编写 C/C++ 代码的事情。

寻找包括指导我教程的建议。我的 Java 技能非常出色,而目前只有我的 C/C++ 已经有 10 年历史了。

【问题讨论】:

标签: java dll java-native-interface jna


【解决方案1】:

在您的情况下,最简单的选项可能是JNA

这里是一个简单的Hello World example,向您展示映射C库的printf函数所涉及的内容:

package com.sun.jna.examples;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {

    // This is the simplest way of mapping, which supports extensive
    // customization and mapping of Java to native types.

    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)
            Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
                               CLibrary.class);

        void printf(String format, Object... args);
    }

    // And this is how you use it
    public static void main(String[] args) {
        CLibrary.INSTANCE.printf("Hello, World\n");
        for (int i=0;i < args.length;i++) {
            CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
        }
    }
}

JNA 的 JavaDocgithub project 包含适用于各种用例的示例和教程。

【讨论】:

    猜你喜欢
    • 2011-05-13
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多