首页 > 解决方案 > 确认我在 Java JNA 中正确发送指向 int[] 的指针

问题描述

我正在传递一个指向 int[] 的指针,如下所示,它位于我的 java 结构/类中:

        int[] initialDynamicSize = new int[]{1, initialDynamicAllocatedSize};
        int[] allFloorsSize = new int[]{1, allFloorsAllocatedSize};

        Pointer PTRinitialDynamicSize = new Memory(2*Native.getNativeSize(Integer.TYPE));
        PTRinitialDynamicSize.setInt(0*Native.getNativeSize(Integer.TYPE),1);
        PTRinitialDynamicSize.setInt(1*Native.getNativeSize(Integer.TYPE),initialDynamicAllocatedSize);

        Pointer PTRallFloorsSize = new Memory(2*Native.getNativeSize(Integer.TYPE));
        PTRinitialDynamicSize.setInt(0*Native.getNativeSize(Integer.TYPE),1);
        PTRallFloorsSize.setInt(1*Native.getNativeSize(Integer.TYPE),allFloorsAllocatedSize);

结构如下所示:

@Structure.FieldOrder({ "str", "size", "allocatedSize", "numDimensions", "canFreeData"})
public class test extends Structure implements Structure.ByReference{


    public Pointer str;
    //private int numVals;
    public Pointer size;
    public int allocatedSize;
    public int numDimensions;
    public boolean canFreeData;

    //Static
    public test(Pointer str, Pointer size) {
        this.str = str;
        this.size = size;
    }

    //Dynamic
    public test(Pointer str, Pointer size, int allocatedSize, int numDimensions, boolean canFreeData) {
        this.str = str;
        this.size = size;
        this.allocatedSize = allocatedSize;
        this.numDimensions = numDimensions;
        this.canFreeData = canFreeData;
    }

    public test() {
    }
}

我从我的 C 代码中收到错误,我想排除此代码的任何错误。接收数据的 C 代码结构是:

struct emxArray_char_T
{
  char *data;
  int *size; // This is the string array that i am sending
  int allocatedSize;
  int numDimensions;
  bool canFreeData;
};

这是一个 MatLab 结构https://uk.mathworks.com/help/coder/ug/use-c-arrays-in-the-generated-function-interfaces.html#mw_0cae9a4a-b74d-43f9-aae0-ed192db73d22

标签: javaandroidcjna

解决方案


推荐阅读