JNA(Java Native Access )提供一组Java工具类用于在运行期间动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与结构,JNA将自动实现Java接口到native function的映射。
要根据dll库文件编译版本来选择jdk版本,jdk版本与编译dll的一致,同为32位或64位。
java的pom:
<dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>5.5.0</version> </dependency> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna-platform</artifactId> <version>5.5.0</version> </dependency>
dll中h:
#ifndef UNTITLEDDLL_H #define UNTITLEDDLL_H #include "untitleddll_global.h" class UNTITLEDDLLSHARED_EXPORT Untitleddll { public: Untitleddll(); }; struct GeoPos{ double lon; double lat; double elev; }; /**************************** 函数名:MyAdd 输 入:a 累加参数a b 累加参数b 输 出:int 计算结果 ****************************/ extern "C" UNTITLEDDLLSHARED_EXPORT int MyAdd(int a, int b); /**************************** 函数名:calLinkInfo 输 入:posList 位置信息结构体 length 数组指针长度 equipParam 传入设备参数数组 code 信道类型 dResult 返回计算结果数组 输 出:bool 计算是否完成 ****************************/ extern "C" UNTITLEDDLLSHARED_EXPORT double calLinkInfo(GeoPos*geopos,int length,double* equipParam,int code,double *dResults); #endif // UNTITLEDDLL_H
dll的cpp:
#include "untitleddll.h" Untitleddll::Untitleddll() { } int MyAdd(int a, int b) { return a + b; } double calLinkInfo(GeoPos*geopos,int length,double* equipParam,int code,double *dResults) { double numb = 0; numb = equipParam[0]; numb += equipParam[1]; numb += equipParam[2]; numb += equipParam[3]; numb += equipParam[4]; double list = geopos->lat + geopos->lon + geopos->elev; *dResults = 20.0; return list + numb; }
java调用:
package com.exampleweb.demo.callink; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Structure; import com.sun.jna.Structure.FieldOrder; import com.sun.jna.ptr.DoubleByReference; import org.junit.Test; public class CallLinkDemo { static String fileName = "untitleddll"; @FieldOrder(value={"lon","lat","elev"}) public static class GeoPos extends Structure{// public double lon; public double lat; public double elev; } public interface JnaLibrary extends Library { // fileName 为 dll 名称 JnaLibrary INSTANCE = Native.load(fileName, JnaLibrary.class); //MyAdd与calLinkInfo声明函数 int MyAdd(int a, int b); double calLinkInfo( GeoPos posList,int length, double[] equipParam, int code, DoubleByReference dResults); } @Test public void TestLink() { int max = JnaLibrary.INSTANCE.MyAdd(100, 200); System.out.println(max); System.out.println("//////////////分割线//////////////"); GeoPos geopos = new GeoPos(); geopos.lon = (10.00); geopos.lat = (20.00); geopos.elev = (30.00); double []equipParam = new double[5]; for (int i = 0; i < 5; i++) { equipParam[i] = 0.1; } int length = 10; DoubleByReference reference = new DoubleByReference();//用指针返回值 double result = JnaLibrary.INSTANCE.calLinkInfo(geopos,length,equipParam,10,reference); System.out.println(result); System.out.println(reference.getValue()); } }
最终结果:
常见错误:
1.注意dll名称与位置,否则报错
java.lang.UnsatisfiedLinkError: 找不到指定的模块。
2.注意jdk版本与编译dll的一致,否则报错
java.lang.UnsatisfiedLinkError: %1 不是有效的 Win32 应用程序
3.注意声明函数
4.注意参数类型保持一致
原文:https://www.cnblogs.com/Comma/p/14470538.html