前言
关于环境和dll的生成,不懂的同学可以去查看JNA(一)与JNA(二)的内容
double数组的操作漏掉了,现补上
操作
1.C语言代码
头文件(library.h)
#ifndef CDYNAMICDEMO_LIBRARY_H
#define CDYNAMICDEMO_LIBRARY_H
#include <string.h>
#include <stdlib.h>
// double指针
void setDouble(const double *value);
double *getDouble();
void cleanDouble(double *value);
//double数组
double setDoubleArr(const double *arr, int num);
void getDoubleArr(double **arr, int *num);
void cleanDoubleArr(double *arr);
#endif //CDYNAMICDEMO_LIBRARY_H
代码文件(library.c)
#include "library.h"
#include <stdio.h>
void setDouble(const double *value) {
printf("\nthe double value is: %f", *value);
}
double *getDouble() {
double *value = malloc(sizeof(double));
*value = 2.71828;
return value;
}
void cleanDouble(double *value) {
free(value);
}
double setDoubleArr(const double *arr, int num) {
int j = 0;
double total = 0;
for (; j < num; j++) {
total += arr[j];
}
return total;
}
void getDoubleArr(double **arr, int *num) {
int i = 0;
*num = 10;
*arr = malloc(sizeof(double) * 10);
memset(*arr, 0, sizeof(double) * 10);
for (; i < *num; i++) {
(*arr)[i] = (10 + i) / 100.0;
}
}
void cleanDoubleArr(double *arr) {
free(arr);
}
2.java代码
package com.dynamic.demo.base;
import com.sun.jna.*;
import com.sun.jna.ptr.DoubleByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
public interface DoublePointAndArrayLibrary extends Library {
DoublePointAndArrayLibrary INTANCE = Native.load(Platform.isWindows() ? "libCDynamicDemo" : "", DoublePointAndArrayLibrary.class);
void setDouble(DoubleByReference reference);
DoubleByReference getDouble();
void cleanDouble(Pointer value);
// double数组
double setDoubleArr(Pointer arr, int num);
void getDoubleArr(PointerByReference pointerByReference, IntByReference reference);
void cleanDoubleArr(Pointer arr);
public static void main(String[] args) {
DoubleByReference reference = new DoubleByReference(3.1415);
DoublePointAndArrayLibrary.INTANCE.setDouble(reference);
DoubleByReference aDouble = DoublePointAndArrayLibrary.INTANCE.getDouble();
System.out.println("获取double: " + aDouble.getValue());
DoublePointAndArrayLibrary.INTANCE.cleanDouble(aDouble.getPointer());
// 设置double数组
int num = 5;
int nativeSize = Native.getNativeSize(double.class);
Pointer pointer = new Memory(num * nativeSize);
for (int i = 0; i < num; i++) {
pointer.setDouble(i * nativeSize, (10.1 + i) / 3.0);
}
double value = DoublePointAndArrayLibrary.INTANCE.setDoubleArr(pointer, num);
System.out.println(value);
// 获取double数组, PointerByReference是指针的指针
PointerByReference arrReference = new PointerByReference();
IntByReference intByReference = new IntByReference();
DoublePointAndArrayLibrary.INTANCE.getDoubleArr(arrReference, intByReference);
int len = intByReference.getValue();
System.out.println("数组元素个数: " + len);
if (intByReference.getValue() > 0) {
Pointer pointer1 = arrReference.getValue();
double total = 0;
for (int i = 0; i < len; i++) {
double val = pointer1.getDouble(i * nativeSize);
total += val;
}
System.out.println("double数组的总和: " + total);
DoublePointAndArrayLibrary.INTANCE.cleanDoubleArr(pointer1);
}
}
}
3.查看输出
总结
- 指针是通过引用(DoubleByReference)来定义的,而数组则是:PointerByReference
- 数组的写入和读取都是通过偏移实现,其他数据类型应该也一样
- Native.getNativeSize可以获取指定类型占用的字节数