WlanGetNetworkBssList with c# on windows 10

本文关键字:windows on with WlanGetNetworkBssList | 更新日期: 2023-09-27 18:07:25

我试图从Windows 10下的c#桌面应用程序互操作WlanGetNetworkBssList。正确检索WLAN_BSS_LIST有一些问题。不知道问题在哪里,我得到的列表是错误的。第一项似乎是正确的,但其他的都不对。我希望有人能帮助我。

这些是我使用的结构。我认为问题是错误的结构定义。

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct WLAN_BSS_LIST
{
    internal Int32 dwTotalSize;
    internal Int32 dwNumberOfItems;
    internal WLAN_BSS_ENTRY[] wlanBssEntries;
    internal WLAN_BSS_LIST(IntPtr ppWlanBssList)
    {
        dwTotalSize = (Int32)Marshal.ReadInt32(ppWlanBssList);
        dwNumberOfItems = (Int32)Marshal.ReadInt32(ppWlanBssList + 4);
        wlanBssEntries = new WLAN_BSS_ENTRY[dwNumberOfItems];
        int WLAN_BSS_ENTRY_SIZE = Marshal.SizeOf(typeof(WLAN_BSS_ENTRY));
        for (int i = 0; i < dwNumberOfItems; i++)
        {
            IntPtr pItemList = new IntPtr(ppWlanBssList.ToInt32() + i * Marshal.SizeOf(typeof(WLAN_BSS_ENTRY)) + 8);
            wlanBssEntries[i] = (WLAN_BSS_ENTRY)Marshal.PtrToStructure(pItemList, typeof(WLAN_BSS_ENTRY));
        }
    }
}
public struct WLAN_BSS_ENTRY
{
    public DOT11_SSID dot11Ssid;
    public ulong uPhyId;
    public DOT11_MAC_ADDRESS dot11Bssid;
    public DOT11_BSS_TYPE dot11BssType;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public DOT11_PHY_TYPE[] dot11BssPhyType;
    public long lRssi;
    public ulong uLinkQuality;
    public bool bInRegDomain;
    public ushort usBeaconPeriod;
    public ulong ullTimestamp;
    public ulong ullHostTimestamp;
    public ushort usCapabilityInformation;
    public ulong ulChCenterFrequency;
    public WLAN_RATE_SET wlanRateSet;
    public ulong ulIeOffset;
    public ulong ulIeSize;
}
public struct DOT11_SSID
{
    public uint uSSIDLength;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string ucSSID;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct DOT11_MAC_ADDRESS
{
    public byte one;
    public byte two;
    public byte three;
    public byte four;
    public byte five;
    public byte six;
}
public enum DOT11_BSS_TYPE
{
    dot11_BSS_type_infrastructure = 1,
    dot11_BSS_type_independent = 2,
    dot11_BSS_type_any = 3,
}
public enum DOT11_PHY_TYPE
{
    dot11_phy_type_unknown,
    dot11_phy_type_any,
    dot11_phy_type_fhss,
    dot11_phy_type_dsss,
    dot11_phy_type_irbaseband,
    dot11_phy_type_ofdm,
    dot11_phy_type_hrdsss,
    dot11_phy_type_erp,
    dot11_phy_type_ht,
    dot11_phy_type_IHV_start,
    dot11_phy_type_IHV_end,
}
public struct WLAN_RATE_SET
{
    public ulong uRateSetLength;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 126)]
    public ushort[] usRateSet;
}

WlanGetNetworkBssList with c# on windows 10

快速解决方法是更改代码以使用Ilya Konstantinov的Managed Wifi API库。它是在MIT许可下的。我有一个分支,使用SafeHandles的所有资源管理,但原始代码是坚实的。