Java JNA 调用dll库

JNA

JNA(Java Native Access )提供一组Java工具类用于在运行期间动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与结构,JNA将自动实现Java接口到native function的映射。

优点

JNA可以让你像调用一般java方法一样直接调用本地方法。就和直接执行本地方法差不多,而且调用本地方法还不用额外的其他处理或者配置什么的,也不需要多余的引用或者编码,使用很方便。

JNA描述

JNA类库使用一个很小的本地类库sub 动态的调用本地代码。程序员只需要使用一个特定的java接口描述一下将要调用的本地代码的方法的结构和一些基本属性。这样就省了为了适配多个平台而大量的配置和编译代码。因为调用的都是JNA提供的公用jar 包中的接口。

注意

要根据dll库文件编译版本来选择jdk版本,jdk版本与编译dll的一致,同为32位或64位。

JNA jar包

jna-x.x.x.jar jna-platform-x.x.x.jar

下面我使用JNA简单做了个demo,大致包含了一下dll的调用以及各类型的参数传递。

java的pom:


      net.java.dev.jna
      jna
      5.5.0
    
    
      net.java.dev.jna
      jna-platform
      5.5.0
    

dll中h:

#ifndef UNTITLEDDLL_H
#define UNTITLEDDLL_H

/*导出设置*/
#define UNTITLEDDLLSHARED_EXPORT __declspec(dllexport)

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 class GeoPos extends Structure {
    //  public static class ByReference extends GeoPos implements Structure.ByReference {}
    //  public static class ByValue extends GeoPos implements Structure.ByValue{}
    //必须使用public,不然报错
    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());
  }
}

最终结果:


image.png

自定义结构数组调用时

package com.exampleweb.demo.callink;
 
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
import lombok.Data;
 
@Data
@FieldOrder(value={"lon","lat","elev"})
public class GeoPos extends Structure {
 
  //必须使用public
  public double lon;
  public double lat;
  public double elev;
}

定义结构体数组时注意循环定义

GeoPos[] geoposos=new GeoPos[5];
for (int i = 0; i < 5; i++) {
  geoposos[i] = new GeoPos();
  geoposos[i].lon=(10.00);
  geoposos[i].lat=(20.00);
  geoposos[i].elev=(30.00);
}

但在调用dll程序时会报错,显示结构数组元素必须使用连续内存


image.png

改为toArray,可以运行

GeoPos geopos=new GeoPos();
GeoPos[] geoposos= (GeoPos[])geopos.toArray(5);
 
for (int i = 0; i < 5; i++) {
  geoposos[i].lon=(10.00);
  geoposos[i].lat=(20.00);
  geoposos[i].elev=(30.00);
}

常见错误:

  1. 注意dll名称与位置,否则报错

java.lang.UnsatisfiedLinkError: 找不到指定的模块。

  1. 注意jdk版本与编译dll的一致,否则报错

java.lang.UnsatisfiedLinkError: %1 不是有效的 Win32 应用程序

  1. 定义结构体时,必须使用public,否则报错,直接赋值或者.set()都可以

java.lang.Error: Structure.getFieldOrder() on class com.exampleweb.demo.callink.CallLinkDemo$GeoPos returns names ([elev, lat, lon]) which do not match declared field names ([])

  1. 注意声明函数

  2. 注意参数类型保持一致

你可能感兴趣的:(Java JNA 调用dll库)