最近需要对接硬件,调用dll库,记录一下
用JNative实现此功能,
1.项目导入JNative.jar包;
2.声明dll文件里面的函数,我这用的dll名称是Dcrf32.dll;
public interface Dcrf32 extends StdCallLibrary { //有的继承是Library,看dll文件类型
Dcrf32 INSTANCE_Dcrf32 = (Dcrf32) Native.loadLibrary("D:\\Dcrf32",Dcrf32.class);// 加载dll动态库文件,绝对路径
void LibMain(int flag, byte[] version);
//声明dll里面的函数,可以多个...
//打开端口
int dc_init(short port, int baud);
}
3.调用
public static void main(String args[]) throws UnsupportedEncodingException {
int icdev = -1;
icdev = Dcrf32.INSTANCE_Dcrf32.dc_init((short)100,0);
if (icdev < 0) {
System.out.println("dc_init error!");
return;
}
System.out.println("dc_in