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

看起来像这里:

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);

09-26 21:51
查看更多