c#从包含数组的内存中读取结构

时间:2013-12-05 23:49:44

标签: c# c++ arrays struct

我无法将此结构转换为c#。我的尝试大部分都是有效的。有没有办法声明它可以计算出要创建多少WLAN_PHY_FRAME_STATISTICS,如WLAN_PHY_FRAME_STATISTICS [dwNumberOfPhys]?

typedef struct _WLAN_STATISTICS {
  ULONGLONG                 ullFourWayHandshakeFailures;
  ULONGLONG                 ullTKIPCounterMeasuresInvoked;
  ULONGLONG                 ullReserved;
  WLAN_MAC_FRAME_STATISTICS MacUcastCounters;
  WLAN_MAC_FRAME_STATISTICS MacMcastCounters;
  DWORD                     dwNumberOfPhys;
  WLAN_PHY_FRAME_STATISTICS PhyCounters[1];
} WLAN_STATISTICS, *PWLAN_STATISTICS;

到目前为止我所拥有的:

[StructLayout(LayoutKind.Sequential)]
public struct WLAN_STATISTICS {
    public ulong ullFourWayHandshakeFailures;
    public ulong ullTKIPCounterMeasuresInvoked;
    public ulong ullReserved;
    public WLAN_MAC_FRAME_STATISTICS MacUcastCounters;
    public WLAN_MAC_FRAME_STATISTICS MacMcastCounters;
    public int dwNumberOfPhys;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
    public WLAN_PHY_FRAME_STATISTICS[] PhyCounters;
}

SizeConst应该等于dwNumberOfPhys,我不确定如何最好地做到这一点。

我正在使用Marshal.PtrToStructure将数据导入结构中。

我想我要问的是:是否有一种很好的方式来声明这个数组,以便数组自动正确的大小,还是我需要将其作为字节数组读取并自己管理结构内容?

0 个答案:

没有答案