求仅具有引用指针的对象数组的总大小?

发布于 2024-11-15 17:03:13 字数 1317 浏览 5 评论 0原文

总而言之,我正在使用 Wlanapi,而且我对它相当陌生(一般来说是原生 api)。我在将结构从 C++ 转换为 C# 时遇到问题。现在我有:

Original:

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

Conversion:

[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 引用的数组的总大小:(

仅供参考,如果有人指出我,我会非常失望一个现有的库。

编辑以添加原始结构

So as an overview, I am working with the Wlanapi and I am fairly new to it (native apis in general). I am running into a problem converting a structure from c++ to c#. Right now I have:

Original:

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

Conversion:

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

I just don't know how to get the total size of the array referenced by ppBssList :(

As an fyi, I will be extremely disappointed if someone points me to an existing library.

Edited to add original struct

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-11-22 17:03:13

我不确定你的 twTotalSize 是否反映了为 wlanBssEntries 中的条目分配的内存量,如果是这样,一个简单的计算就足够了,

 sizeof(typeof(WLAN_BSS_ENTRY)) * dwNumberOfItems + 8

否则,我建议你发布原始的本机数据结构,也许,有更好的从内存块编组它的替代方法。

I'm not sure if your twTotalSize reflects the amount of memory allocated for the entries in wlanBssEntries, if so, a simple calculation would be sufficient,

 sizeof(typeof(WLAN_BSS_ENTRY)) * dwNumberOfItems + 8

otherwise, I suggest that you post the original native data structure, maybe, there is a much better alternative for marshaling it from the memory block.

留一抹残留的笑 2024-11-22 17:03:13

所以我明白了,我不知道我在想什么......

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

So I figured it out, I dunno what I was thinking...

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

And

[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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文