我正在尝试为科研设备编写一个Java程序,它使用国家仪器驱动程序(DLL),这些驱动程序是用C编写的。目前我对这些DLL一无所知。如果需要,我可以通过我的客户端联系NI以获取详细信息。
我的C/C++技能是古老的,所以我倾向于避免任何需要编写C/C++代码的事情。
寻找建议,包括给我指点教程。我的Java技能非常优秀,目前只是我的C/C++已经有十年历史了。
发布于 2015-08-03 19:44:47
在您的情况下,最简单的选择可能是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的JavaDoc和github project包含大量用例的示例和教程。
https://stackoverflow.com/questions/31778178
复制相似问题