我有一个结构,它包含其他结构指针的数组,以及数组长度的字段。此结构由本机方法通过“out”参数返回。
原始“C”结构:
typedef struct MMAL_COMPONENT_T
{
uint32_t input_num; /**< Number of input ports */
MMAL_PORT_T **input; /**< Array of input ports */
} MMAL_COMPONENT_T;
JNAerator生成的相应Java类:
public class MMAL_COMPONENT_T extends Structure {
public int input_num;
public MMAL_PORT_T.ByReference[] input;
}
“C”方法签名:
MMAL_STATUS_T mmal_component_create(const char *name, MMAL_COMPONENT_T **component);
Java用法:
PointerByReference ref = new PointerByReference();
status = mmal.mmal_component_create("<component-name>", ref);
MMAL_COMPONENT_T component = new MMAL_COMPONENT_T(ref.getValue());
这将生成一条JNA错误消息,指出“数组字段必须初始化”。
当前我正在使用
Pointer
替换阵列并从中手动构建阵列:
public class MMAL_COMPONENT_T extends Structure {
public int input_num;
public Pointer input;
}
使用方法:
Pointer[] array = component.input.getPointerArray(0, component.input_num);
MMAL_PORT_T port = new MMAL_PORT_T(array[0]);
port.read();
但这种方法似乎不令人满意,因为它冗长,而且使用的是指针,而不是实际的结构类型。
那么,使用JNA处理这一问题的标准方法是什么?