如何设置结构缓冲区的正确大小?

How to set up a correct size of buffer of the structure?

我有一个 DLL 文档,我必须使用它,其中定义了一个结构,该结构是本机方法之一的参数。

看起来像这样:

typedef struct
{
UNUM32 uiModuleState;
UNUM32 uiSerialNumber;
UNUM32 uiVCIIf;
UNUM32 uiModuleType;
CHAR8 szModuleName[256];
}
VTX_RT_VCI_ITEM;
typedef struct
{
UNUM32 uiNumVCIItems;
VTX_RT_VCI_ITEM * pVCIItems;
}
VTX_RT_VCI_ITEM_LIST;
Calling Convention:
SNUM32 VtxRtGetModuleIds( IO UNUM32* puiBufferLen,
IO VTX_RT_VCI_ITEM_LIST* pVCIItemList);

我已经像这里一样在 JNA 中为该结构建模

VTX_RT_VCI_ITEM

@Structure.FieldOrder({ "uiModuleState",
                        "uiSerialNumber",
                        "uiVCIIf",
                        "uiModuleType",
                        "szModuleName" })
public class VtxRtVciItem extends Structure
{
    public int uiModuleState;

    public int uiSerialNumber;

    public int uiVCIIf;

    public int uiModuleType;

    public char[] szModuleName = new char[VciRuntimeAPI.VTX_RT_SMALL_BUF_SIZE];

    public static class ByReference extends VtxRtVciItem implements Structure.ByReference {}

    public static class ByValue extends VtxRtVciItem implements Structure.ByValue {}

    public VtxRtVciItem()
    {
        super();
        read();
    }
}

VTX_RT_VCI_ITEM_LIST

@Structure.FieldOrder({ "uiNumVCIItems",
                        "pVCIItems" })
public class VtxRtVciItemList extends Structure
{
    public int uiNumVCIItems;

    public VtxRtVciItem.ByReference pVCIItems;

    public VtxRtVciItemList()
    {
        super();

    }
}

第一个参数描述如下 puiBufferLen pVCIItemList 指向的缓冲区的大小。

如何设置该结构的正确缓冲区大小?

我正在尝试做类似此处的操作,但该结构的大小为 8,这意味着未调用 VtxRtVciItem。

VtxRtVciItemList vtxRtVciItemList = new VtxRtVciItemList();
IntByReference puiBufferLen = new IntByReference();
puiBufferLen.setValue(vtxRtVciItemList.size());

您的 vtxRtVciItemList 只是一个包含列表元素数量和指向实际列表的指针的结构。列表缓冲区本身将是列表中每个结构的大小 (new VtxRtVciItem().size()) 乘以这些元素的数量 (uiNumVCIItems)。

您没有显示实际分配该缓冲区的位置,您需要使用 Structure.toArray() 方法来完成。

我想这就是你想要做的,如果我误解了你的请求,请告诉我。

int numItems = 42; // whatever your number of list items is
VtxRtVciItem.ByReference[] vtxRtVciItemPointerArray = 
    (VtxRtVciItem.ByReference[]) new VtxRtVciItem.ByReference().toArray(numItems);
VtxRtVciItemList vtxRtVciItemList = new VtxRtVciItemList();
vtxRtVciItemList.uiNumVCIItems = numItems;
vtxRtVciItemList.pVCIItems = vtxRtVciItemPointerArray[0];

然后传递给你的函数:

IntByReference puiBufferLen = 
    new IntByReference(vtxRtVciItemList.uiNumVCIItems * vtxRtVciItemPointerArray[0].size());
VtxRtGetModuleIds(puiBufferLen, pVCIItemList);