将指针指向结构(StructureByReference)到C代码中,如何再次访问该数据?

问题描述 投票:0回答:1

我正在将以下测试结构从Android中的Java传递到共享库中的本机C函数:

@Structure.FieldOrder({"testDouble", "testInt", "testPointer"})
public class Test extends Structure implements Structure.ByReference {

    public double testDouble;
    public int testInt;
    public Pointer testPointer;


    public Test(double testDouble, int testInt, Pointer testPointer) {
            this.testDouble = testDouble;
            this.testInt = testInt;
            this.testPointer = testPointer;

    }

    Getters & Setters...

本机C代码将结构处理为:

    void Test(double extraDouble, Test *TestResult)

Native C代码将指针指向TestResult并填充所有字段。

然后,我需要能够以Java代码的形式访问该数据-即访问测试对象并读取数据testDoubletestInttestPointer处的数据,即double[]。该函数不会传回TestResult对象;如您所见,它只是在我假设的内存中被覆盖。

我了解如何发送数据;但是,当我尝试通过Java中的值再次访问该对象时,在应该进行填充之后,我只会得到零数据-好像它是一个全新的对象,而不是原来的对象(现在填充了该对象)数据。

如何访问我的C代码应填充的数据?即我认为再次访问该指针处的内存?

我假设在本机C代码中,它只是用新数据覆盖了该内存块,然后我必须找到某种方法以能够使用Java返回该数据块并重建我的测试结构。如果我的任何假设不正确,请更正我。

如果您需要任何澄清,请告诉我。

C结构只是:

typedef struct {
  double testDouble;
  int testInt;
  double testPointer[3];
} Test;
java android c java-native-interface jna
1个回答
0
投票

您没有正确传递双精度数组。由于数组的大小是固定的,因此您可以将其映射为结构的一部分:

© www.soinside.com 2019 - 2024. All rights reserved.