Trova la dimensione totale di un array di oggetti con solo un puntatore di riferimento?
-
27-10-2019 - |
Domanda
Quindi, come panoramica, sto lavorando con Wlanapi e sono abbastanza nuovo (API native in generale).Sto riscontrando un problema durante la conversione di una struttura da c ++ a c #.In questo momento ho:
Originale:
typedef struct _WLAN_BSS_LIST {
DWORD dwTotalSize;
DWORD dwNumberOfItems;
WLAN_BSS_ENTRY wlanBssEntries[1];
} WLAN_BSS_LIST, *PWLAN_BSS_LIST;
Conversione:
[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));
}
}
}
Non so come ottenere la dimensione totale dell'array a cui fa riferimento ppBssList
:(
Come utente, sarei estremamente deluso se qualcuno mi indicasse una libreria esistente.
Modificato per aggiungere la struttura originale
Soluzione 2
Così ho capito, non so cosa stavo pensando ...
[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));
}
}
}
e
[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;
}
Altri suggerimenti
Non sono sicuro che twTotalSize rifletta la quantità di memoria allocata per le voci in wlanBssEntries, in tal caso sarebbe sufficiente un semplice calcolo,
sizeof(typeof(WLAN_BSS_ENTRY)) * dwNumberOfItems + 8
altrimenti, ti suggerisco di pubblicare la struttura dati nativa originale, forse c'è un'alternativa molto migliore per il marshalling dal blocco di memoria.