JNI调用C++dll动态库如何转换struct结构体为java实体类

JNI调用C++dll动态库如何转换struct结构体为java实体类

需求:使用java对接第三方c++程序,调用c++方法

一、JNI和JNA简介

JNI(Java Native Interface)是一种技术,通过这种技术可以做到以下两点:

  • Java程序中的函数可以调用Native语言写的函数,Native一般指的是C/C++编写的函数。
  • Native程序中的函数可以调用Java层的函数,也就是在C/C++程序中可以调用Java的函数。

我们都知道承载Java世界的虚拟机是用Native语言写的,而虚拟机又运行在具体平台上,所以虚拟机本身无法做到平台无关。然而,有了JNI技术,可以对Java层屏蔽具体的虚拟机实现上的差异了。这样,就能实现Java本身的平台无关特性。其实Java一直在使用JNI技术,只是我们平时较少用到罢了。

JNI的使用并不简单,如果已有一个编译好的.dll/.so文件,如果使用JNI技术调用,我们首先需要使用C语言另外写一个.dll/.so共享库,使用SUN规定的数据结构替代C语言的数据结构,调用已有的 dll/so中公布的函 数。然后再在Java中载入这个库dll/so,然后编写Java native函数作为链接库中函数的代理。经过这些繁琐的步骤才能在Java中调用本地代码。

JNA(Java Native Access)是建立在JNI技术基础之上的一个Java类库,它使我们可以方便地使用java直接访问动态链接库中的函数。我们不需要重写我们的动态链接库文件,而是有直接调用的API,大大简化了我们的工作量。但是JNA一般只适用于较为简单的C/C++库,如果接口、数据结构复杂的话就不推荐。而且JNA也只提供了C/C++对Java的接口转化。

二、Java和C类型对照表

下表是JNA官网 (https://jna.java.net/javadoc/overview-summary.html) 给出的基本类型的C语言和Java类型对照表:

C Type Representation Java Type
char 8-bit integer byte
wchar_t platform-dependent char
short 16-bit integer short
int 32-bit integer int
int boolean flag boolean
enum enumeration type int(usually)
long long, __int64 64-bit integer long
float 32-bit floating point float
double 64-bit floating point double
pointer (e.g. void*) platform-dependent (32- or 64-bit pointer to memory) BufferPointer
pointer (e.g. void*) array 32- or 64-bit pointer to memory (argument/return)contiguous memory (struct member) [] (array of primitive type)
long platform-dependent (32- or 64-bit integer) NativeLong
const char* NUL-terminated array (native encoding or jna.encoding) String
const wchar_t* NUL-terminated array (unicode) WString
char** NULL-terminated array of C strings String[]
wchar** NULL-terminated array of wide C strings String[]
void** NULL-terminated array of pointers Pointer[]
struct* struct pointer to struct (argument or return) (or explicitly) struct by value (member of struct) (or explicitly) Structure
union same as Structure Union
struct[] array of structs, contiguous in memory Structure[]
void (*FP)() function pointer (Java or native) Callback
pointer (*) same as Pointer PointerType
other integer type IntegerType
other custom mapping, depends on definition NativeMapped

这张对照表一般已经能够满足跨平台、跨语言调用的数据类型转换需求,因为如果我们要做跨语言调用,应当尽量使用基本和简单的数据类型,而不要过多使用复杂结构体传递数据,因为C语言的结构体中,每个成员会执行对齐操作与前一个成员保持字节对齐,也就是说,成员的书写顺序会影响结构体占用的空间大小,因此会在Java端定义结构体时会造成不小的麻烦。比如,如果跨语言调用的函数中参数包含stat这个结构体:我们都知道,stat这个结构体是用来描述linux文件系统的文件元数据的基本结构,但麻烦的是,这个结构体的成员定义次序在不同的机型上并不相同,如果我们在Java端重写这个结构体,会产生兼容性问题。

三、结构体定义

有时候我们需要在Java端访问某个C/C++结构体中的成员,我们就需要在Java端复写这个结构体,在复写的时候需要注意两点:

  • 需要在结构体定义中定义2个内部类ByReference和ByValue,来实现指针类型接口和值类型接口
  • 重写getFieldOrder()来告诉C/C++的成员取值次序

下面我们通过一个栗子来看一下在Java只不过怎么模拟定义一个C/C++结构体:

C/C++代码

typedef struct A {
 B* b; 
 void* args;
 int len;
};

typedef struct B { 
 int obj_type;
};

Java代码


/* 结构体A定义 */
public static A extends Structure {

//构造函数定义
 public A() {
 super();
 }

 public A(Pointer _a) {
 super(_a);
 }

 //结构体成员定义
 public B.ByReference b;
 PointerByReference args;
 int len;

 //添加2个内部类,分别实现指针类型接口、值类型接口
 public static class ByReference extends A implements Structure.ByReference {}
 public static class ByValue extends A implements Structure.ByValue{}

 //定义取值次序,需要与C/C++中对齐,不然会出现NoSuchFieldError
 @Override 
 protected List getFieldOrder() {
 return Arrays.asList(new String[]{"b", "args", "len"});
 }
}

/* 结构体B定义 */
public static B extends Structure {
 public B() {
 super();
 }

 public B(Pointer _b) {
 super(_b);
 }

 int obj_type;

 public static class ByReference extends B implements Structure.ByReference {}
 public static class ByValue extends B implements Structure.ByValue{}

@Override 
 protected List getFieldOrder() {
 return Arrays.asList(new String[]{"obj_type"});
 }
}

四、结构体传递

如果需要在Java端访问某个结构体的成员,需要使用ByReference(指针、引用)或是ByValue(拷贝参数);如果只是起到数据传递,不关心具体内部结构,可以使用PointerByReference和Pointer。

test_myFun(struct A a)
test_myFun(A.ByValue a)

test_myFun(struct A *a)
test_myFun(A.ByReference a)

test_myFun(struct A **a)
test_myFun(A.ByReference[] a)

test_myFun(A a)
test_myFun(Pointer a)

test_myFun(A *a)
test_myFun(PointerByReference a)

test_myFun(A **a)
test_myFun(PointerByReference[] a)

五、回调函数

我们有时候会在C/C++中顶一个一个带回调函数的函数,例如:

typedef bool (*test_cb)(const char *name);

int test_myFun(test_cb cb, const char *name, uint32_t flag);

如果我们需要在Java端调用test_myFun函数,则我们需要在Java端定义一个与test_cb相同的回调接口:

public interface testCallback extends Callback { 
 //invoke对应test_cb,注意参数顺序需要保持一致
 boolean invoke(String name); 
}

定义该回调接口的实现:

public class testCallbackImpl implements testCallback {
 @Override 
 public int invoke(String name) { 
 System.out.printf("Invoke Callback " + name + " successfully!"); 
 return true; 
 } 
}

test_myFun函数Java端定义:

int testMyFun(Callback cb, String name, int flag);
调用实例:

int rc = testMyFun(new testCallbackImpl(), "helloworld", 1);

参考文档:

http://blog.umcloud.com/java-sdk/#comment-879

原文地址:https://www.cnblogs.com/cnsyear/p/12732083.html