Encontre o tamanho total de uma matriz de objetos com apenas um ponteiro de referência?

StackOverflow https://stackoverflow.com/questions/6332359

  •  27-10-2019
  •  | 
  •  

Pergunta

Então, como uma visão geral, estou trabalhando com o Wlanapi e sou bastante novo nele (apis nativas em geral).Estou tendo um problema ao converter uma estrutura de c ++ para c #.Agora eu tenho:

Original:

typedef struct _WLAN_BSS_LIST {
    DWORD          dwTotalSize;
    DWORD          dwNumberOfItems;
    WLAN_BSS_ENTRY wlanBssEntries[1];
} WLAN_BSS_LIST, *PWLAN_BSS_LIST;

Conversão:

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

Só não sei como obter o tamanho total da matriz referenciada por ppBssList :(

Como observador, ficarei extremamente desapontado se alguém me indicar uma biblioteca existente.

Editado para adicionar estrutura original

Foi útil?

Solução 2

Então eu descobri, não sei o que estava 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;
}

Outras dicas

Não tenho certeza se seu twTotalSize reflete a quantidade de memória alocada para as entradas em wlanBssEntries, se sim, um cálculo simples seria suficiente,

 sizeof(typeof(WLAN_BSS_ENTRY)) * dwNumberOfItems + 8

caso contrário, sugiro que você poste a estrutura de dados nativa original, talvez haja uma alternativa muito melhor para empacotá-la a partir do bloco de memória.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top