JNA(十五)Double指针与数组

前言

关于环境和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.查看输出

总结

  1. 指针是通过引用(DoubleByReference)来定义的,而数组则是:PointerByReference
  2. 数组的写入和读取都是通过偏移实现,其他数据类型应该也一样
  3. Native.getNativeSize可以获取指定类型占用的字节数
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值