【问题标题】:JNA: Pointer to a pointer to a structJNA:指向结构指针的指针
【发布时间】:2012-03-28 08:07:14
【问题描述】:

我有一个函数调用:

long foo(mystruct **list)

mystruct的定义在哪里

typedef struct {
    otherstruct *bar[64];
} mystruct;

我正在尝试获取与 bar 对应的 (JNA) Structure[]。我当前的函数定义是int foo(PointerByReference list);,因为这是指向指针的指针,但我不知道如何获取 Structure[]。

在C语言中,代码使用如下:

mystruct *list;
foo(&list);
for (i=0; i<64; i++)
    doStuff(list->bar[i]);

【问题讨论】:

    标签: pointers struct pass-by-reference jna jnaerator


    【解决方案1】:
    1. PointerByReference 合适;使用getValue() 获取Pointer 价值。
    2. 使用该值,初始化您的“mystruct”结构
    3. 由于“otherstruct *bar[64]”表示struct* 的数组,您需要显式使用Structure.ByReference[] 类型的字段来强制JNA 将数组视为指针而不是内联结构。

    代码:

    class OtherStruct extends Structure {
        class ByReference extends OtherStruct implements Structure.ByReference { }
        ...
    }
    class MyStructure extends Structure {
        public OtherStruct.ByReference[] bar = new OtherStruct.ByReference[64];
    }
    

    JNA 应在必要时围绕本机函数调用隐式调用 Structure.read() 和 Structure.write(),但除此之外,您可能需要根据您的使用情况显式地进行这些调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多