简体   繁体   English

使用Java访问第三方DLL

[英]Using Java to access a third party DLL

I am trying to write a Java program for scientific research equipment that uses National Instruments drivers (DLL) that are written in C. I know nothing about these DLLs at the moment. 我正在尝试编写一个用于科研设备的Java程序,该程序使用以C语言编写的National Instruments驱动程序(DLL)。目前我对这些DLL一无所知。 I can contact NI through my client to get details if required. 如果需要,我可以通过客户与NI联系以获取详细信息。

My C/C++ skills are ancient so would prefer avoiding anything that requires writing C/C++ code. 我的C / C ++技能很古老,因此希望避免使用任何需要编写C / C ++代码的东西。

Looking for advice that includes pointing me to tutorials. 寻找建议,包括向我推荐教程。 My Java skills are excellent and current it is just my C/C++ that is like a decade old. 我的Java技能非常出色,目前只有十年的C / C ++。

The simplest option in your case would likely be JNA . 在您的情况下,最简单的选择可能是JNA

Here is a simple Hello World example to show you what is involved in mapping the C library's printf function: 这是一个简单的Hello World示例 ,向您展示映射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's JavaDoc and github project include examples and tutorials for a wide range of use cases. JNA的JavaDocgithub项目包括适用于各种用例的示例和教程。

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

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