[英]Using Java to access a third party DLL
我正在嘗試編寫一個用於科研設備的Java程序,該程序使用以C語言編寫的National Instruments驅動程序(DLL)。目前我對這些DLL一無所知。 如果需要,我可以通過客戶與NI聯系以獲取詳細信息。
我的C / C ++技能很古老,因此希望避免使用任何需要編寫C / C ++代碼的東西。
尋找建議,包括向我推薦教程。 我的Java技能非常出色,目前只有十年的C / C ++。
在您的情況下,最簡單的選擇可能是JNA 。
這是一個簡單的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]);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.