因此,作为一个概述,我正在与Wlanapi合作,我对它很新(一般的原生apis)。我遇到了将结构从c ++转换为c#的问题。现在我有:
原文:
typedef struct _WLAN_BSS_LIST {
DWORD dwTotalSize;
DWORD dwNumberOfItems;
WLAN_BSS_ENTRY wlanBssEntries[1];
} WLAN_BSS_LIST, *PWLAN_BSS_LIST;
转换:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct WLAN_BSS_LIST
{
internal uint dwTotalSize;
internal uint dwNumberOfItems;
internal WLAN_BSS_ENTRY[] wlanBssEntries;
internal WLAN_BSS_LIST(IntPtr ppBssList)
{
dwNumberOfItems = (uint)Marshal.ReadInt32(ppBssList);
//I need to set the value of dwTotalSize but I dunno how
wlanBssEntries = new WLAN_BSS_ENTRY[dwNumberOfItems];
for (int i = 0; i < dwNumberOfItems; i++)
{
IntPtr pWlanBssEntry = new IntPtr(ppBssList.ToInt32() + i *
Marshal.SizeOf(typeof(WLAN_BSS_ENTRY)) + 8);
wlanBssEntries[i] = (WLAN_BSS_ENTRY)Marshal.
PtrToStructure(pWlanBssEntry,
typeof(WLAN_BSS_ENTRY));
}
}
}
我只是不知道如何获得ppBssList
引用的数组的总大小:(
如果有人指我到现有的图书馆,我会非常失望。
编辑添加原始结构
答案 0 :(得分:0)
我不确定你的twTotalSize是否反映了为wlanBssEntries中的条目分配的内存量,如果是这样,一个简单的计算就足够了,
sizeof(typeof(WLAN_BSS_ENTRY)) * dwNumberOfItems + 8
否则,我建议您发布原始的本机数据结构,也许,有更好的替代方法可以从内存块中编组它。
答案 1 :(得分:0)
所以我想通了,我不知道我在想什么......
[StructLayout(LayoutKind.Sequential, Pack=1)]
struct WLAN_BSS_LIST
{
internal uint dwTotalSize;
internal uint dwNumberOfItems;
internal WLAN_BSS_ENTRY[] wlanBssEntries;
internal WLAN_BSS_LIST(IntPtr ppBssList)
{
dwTotalSize = (uint)Marshal.ReadInt32(ppBssList);
dwNumberOfItems = (uint)Marshal.ReadInt32(ppBssList, 4);
wlanBssEntries = new WLAN_BSS_ENTRY[dwNumberOfItems];
for (int i = 0; i < dwNumberOfItems; i++)
{
IntPtr pWlanBssEntry = new IntPtr(ppBssList.ToInt32() + i *
Marshal.SizeOf(typeof(WLAN_BSS_ENTRY)) + 8);
wlanBssEntries[i] = (WLAN_BSS_ENTRY)Marshal.
PtrToStructure(pWlanBssEntry, typeof(WLAN_BSS_ENTRY));
}
}
}
和
[StructLayout(LayoutKind.Sequential)]
public struct WLAN_BSS_ENTRY
{
public DOT11_SSID dot11Ssid;
public uint uPhyId;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] dot11Bssid;
public DOT11_BSS_TYPE dot11BssType;
public DOT11_PHY_TYPE dot11BssPhyType;
public int lRssi;
public uint uLinkQuality;
public bool bInRegDomain;
public UInt16 usBeaconPeriod;
public UInt64 ullTimestamp;
public UInt64 ullHostTimestamp;
public UInt16 usCapabilityInformation;
public uint ulChCenterFrequency;
public WLAN_RATE_SET wlanRateSet;
public uint ulIeOffset;
public uint ulIeSize;
}