c# - 使用 'WlanScan' 刷新 WiFi 网络列表(将 api 语法从 c# 转换为 vba...或解决方法?)

标签 c# vba winapi wifi bssid

我需要刷新 Window 的无线网络列表。

我很乐意接受任何我可以通过 VBA 直接或间接自动化的解决方法(cmdline、wmi 等)。 (我使用的是 Windows 7 家庭版 64 位和 Office 365 专业版 64 位。)

我可以通过多种方式以编程方式列出网络,包括 netsh 或下面的代码,但是列表不会刷新,除非我实际单击 net任务栏通知区域上的网络连接图标。

  • 如某些文档所述,该列表不会每 60 秒自动更新一次。
  • 断开+重新连接 NIC 不是可行/可持续的选择。​​

我想我没有得到WlanOpenHandle 的句柄根据需要,我不擅长将 C 语言转换为 VBA。

没有错误,但 WlanScan 返回未知代码 1168


Related bits:

这是 VB 的函数声明, 改编:

Public Shared Function WlanScan(ByVal hClientHandle As IntPtr, _
   ByRef pInterfaceGuid As Guid, ByVal pDot11Ssid As IntPtr, _
   ByVal pIeData As IntPtr, ByVal pReserved As IntPtr) As UInteger
End Function

...以及 C# 中的函数用法示例:

Guid g;
//wlanHndl is the handle returned previously by calling [WlanOpenHandle]
for (int i = 0; i < infoList.dwNumberOfItems; i++)
{
g = infoList.InterfaceInfo[i].InterfaceGuid;
uint resultCode=WlanScan(wlanHndl, ref g, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (resultCode != 0)
    return;
}

...和如何在 C++ 中打开句柄(来自 here ):

dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
    wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
    return 1;
    // You can use FormatMessage here to find out why the function failed
}

"Un-hidden:"

Obtain (cached) list of wireless networks:

列出网络的代码效果很好 - 除了不能自行刷新。 (之前我正在解析 netsh wlan show networks mode=bssid 的文本输出,它有同样的问题。)

我之前删除了这一部分,因为它很长而且除了刷新之外似乎工作正常。 -)

Option Explicit  'section's source: vbforums.com/showthread.php?632731
Private Const DOT11_SSID_MAX_LENGTH As Long = 32
Private Const WLAN_MAX_PHY_TYPE_NUMBER As Long = 8
Private Const WLAN_AVAILABLE_NETWORK_CONNECTED As Long = 1
Private Const WLAN_AVAILABLE_NETWORK_HAS_PROFILE As Long = 2

Private Type GUID  'from cpearson.com
    Data1 As Long: Data2 As Integer:  Data3 As Integer:  Data4(7) As Byte
End Type

Private Type WLAN_INTERFACE_INFO
    ifGuid As GUID: InterfaceDescription(255) As Byte: IsState As Long
End Type

Private Type DOT11_SSID
    uSSIDLength As Long:            ucSSID(DOT11_SSID_MAX_LENGTH - 1) As Byte
End Type

Private Type WLAN_AVAILABLE_NETWORK
    strProfileName(511) As Byte:    dot11Ssid As DOT11_SSID
    dot11BssType As Long:           uNumberOfBssids As Long
    bNetworkConnectable As Long:    wlanNotConnectableReason As Long
    uNumberOfPhyTypes As Long:      dot11PhyTypes(WLAN_MAX_PHY_TYPE_NUMBER - 1) As Long
    bMorePhyTypes As Long:          wlanSignalQuality As Long
    bSEcurityEnabled As Long:       dot11DefaultAuthAlgorithm As Long
    dot11DefaultCipherAlgorithm As Long: dwflags As Long: dwReserved As Long
End Type

Private Type WLAN_INTERFACE_INFO_LIST
    dwNumberOfItems As Long: dwIndex As Long: InterfaceInfo As WLAN_INTERFACE_INFO
End Type

Private Type WLAN_AVAILABLE_NETWORK_LIST
    dwNumberOfItems As Long:  dwIndex As Long: Network As WLAN_AVAILABLE_NETWORK
End Type

Declare PtrSafe Function WlanOpenHandle Lib "Wlanapi.dll" (ByVal dwClientVersion As Long, _
                ByVal pdwReserved As Long, ByRef pdwNegotiaitedVersion As Long, _
                ByRef phClientHandle As Long) As Long

Declare PtrSafe Function WlanEnumInterfaces Lib "Wlanapi.dll" (ByVal hClientHandle As Long, _
                ByVal pReserved As Long, ppInterfaceList As Long) As Long

Declare PtrSafe Function WlanGetAvailableNetworkList Lib "Wlanapi.dll" ( _
                ByVal hClientHandle As Long, pInterfaceGuid As GUID, ByVal dwflags As Long, _
                ByVal pReserved As Long, ppAvailableNetworkList As Long) As Long

Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
                Source As Any, ByVal Length As Long)

Declare PtrSafe Sub WlanFreeMemory Lib "Wlanapi.dll" (ByVal pMemory As Long)

Type WiFis
  ssid As String: signal As Single
End Type

Public Function GetWiFi() As WiFis()
'returns an array of custom type WiFis (1st interface only)

    Dim udtList As WLAN_INTERFACE_INFO_LIST, udtAvailList As WLAN_AVAILABLE_NETWORK_LIST, udtNetwork As WLAN_AVAILABLE_NETWORK
    Dim lngReturn As Long, lngHandle As Long, lngVersion As Long, lngList As Long, lngAvailable As Long
    Dim lngStart As Long, intCount As Integer, ssid As String, signal As Single, wifiOut() As WiFis
    n = 0

    lngReturn = WlanOpenHandle(2&, 0&, lngVersion, lngHandle) 'get handle
    If lngReturn <> 0 Then
        Debug.Print "Couldn't get wlan handle (Code " & lngReturn & ")"
        Exit Function
    End If

    lngReturn = WlanEnumInterfaces(ByVal lngHandle, 0&, lngList) 'enumerate <*first interface only*>
    CopyMemory udtList, ByVal lngList, Len(udtList)
    lngReturn = WlanGetAvailableNetworkList(lngHandle, udtList.InterfaceInfo.ifGuid, 2&, 0&, lngAvailable) 'get network list
    CopyMemory udtAvailList, ByVal lngAvailable, LenB(udtAvailList)
    intCount = 0
    lngStart = lngAvailable + 8

    Do
        CopyMemory udtNetwork, ByVal lngStart, Len(udtNetwork) ' Populate avail. network structure
        ssid = Replace(StrConv(udtNetwork.dot11Ssid.ucSSID, vbUnicode), Chr(0), "")
        If Len(ssid) < 4 Then ssid = "(Unnamed)"
        signal = CSng(udtNetwork.wlanSignalQuality) / 100
        '[Signal] = 0 to 100 which represents the signal strength (100 Signal)=(-100dBm RSSI), (100 Signal)=(-50dBm RSSI)

        If udtNetwork.dwflags = 0 Then
            n = n + 1
            ReDim Preserve wifiOut(n)
            wifiOut(n).ssid = ssid
            wifiOut(n).signal = signal
        Else
            'skipping networks with [dwflags] > 0
            'I *think* that's what I'm supposed to do
            'Returns 3 for currently connected network, 2 for networks that have profiles
        End If

        intCount = intCount + 1
        lngStart = lngStart + Len(udtNetwork)
    Loop Until intCount = udtAvailList.dwNumberOfItems
    WlanFreeMemory lngAvailable     'clean up memory
    WlanFreeMemory lngList

    GetWiFi = wifiOut   'Success! (function is populated with cached network list)

End Function

...and the problem:

Refresh network list using WlanScan?

不会生成 VBA 错误,但返回代码 1168(我无法识别)/(Source )

'Added blindly:'wlanui type library (wlanui.dll) and "wlan pref iua" (wlanconn.dll)

Public Type DOT11_SSID 
   uSSIDLength As LongPtr: ucSSID As String
End Type

Private Type GUID 'from cpearson.com/excel/CreateGUID.aspx
    Data1 As LongPtr: Data2 As Integer
    Data3 As Integer: Data4(0 To 7) As Byte
End Type

#If Win64 Then 'also new to Office-64bit, but seems okay
    Declare PtrSafe Function WlanScan Lib "Wlanapi.dll" _
        (ByVal hClientHandle As LongPtr, ByRef pInterfaceGuid As GUID, _
        ByVal pDot11Ssid As LongPtr, ByVal pIeData As LongPtr, _
        ByVal pReserved As LongPtr) As LongPtr
#Else
    Private Declare WlanScan Lib "Wlanapi.dll" _
        (ByVal hClientHandle As LongPtr, ByRef pInterfaceGuid As GUID, _
        ByVal pDot11Ssid As LongPtr, ByVal pIeData As LongPtr, _
        ByVal pReserved As LongPtr) As LongPtr
#End If

Sub test_RefreshNetworkList()
    Dim hresult As LongPtr, phClientHandle As Long, pdwNegotiatedVersion As Long
    Dim retVal As Longptr, g As GUID
    hresult = WlanOpenHandle(2&, 0&, pdwNegotiatedVersion, phClientHandle)
    retVal = WlanScan(phClientHandle, g, 0, 0, 0)
    Select Case retVal
        Case 87: Debug.Print "ERROR_INVALID_PARAMETER"
        Case 6: Debug.Print "ERROR_INVALID_HANDLE"
        Case 8: Debug.Print "ERROR_NOT_ENOUGH_MEMORY"
        Case Else: Debug.Print "RPC_STATUS : " & retVal  ' "misc errors"
    End Select
End Sub

肯定有一种迂回的方法可以从 VBA 刷新网络列表吗?我对可以自动化的变通办法很满意……什么?!

paw谢谢!


Edit:

我在适用的(我认为)位置将 Long 更改为 LongPtr。同样的错误。

这是 WlanOpenHandleWlanScan 的定义。

Declare PtrSafe Function WlanOpenHandle Lib "Wlanapi.dll" 
    (ByVal dwClientVersion As LongPtr, _
     ByVal pdwReserved As LongPtr, 
     ByRef pdwNegotiaitedVersion As LongPtr, _
     ByRef phClientHandle As LongPtr           ) As LongPtr

(...这也是我第一次尝试使用编译器常量。)

#If Win64 Then
    Declare PtrSafe Function WlanScan Lib "Wlanapi.dll" _
        (ByVal hClientHandle As LongPtr,
         ByRef pInterfaceGuid As GUID, _
         ByVal pDot11Ssid As LongPtr, 
         ByVal pIeData As LongPtr, _
         ByVal pReserved As LongPtr) As LongPtr
#Else
    Private Declare WlanScan Lib "Wlanapi.dll" _
        (ByVal hClientHandle As LongPtr, 
         ByRef pInterfaceGuid As GUID, _
         ByVal pDot11Ssid As LongPtr, 
         ByVal pIeData As LongPtr, _
         ByVal pReserved As LongPtr     ) As LongPtr
#End If

最佳答案

我认为不刷新的主要问题是您永远不会关闭打开的句柄。这可能会导致问题,因为据我所知不应该有多个打开的句柄。

您使用 WlanOpenHandle 获得接口(interface)的句柄,但在完成它并获得所需信息后,您应该调用 WlanCloseHandle 关闭它句柄和关联的连接。

Declare PtrSafe Function WlanCloseHandle Lib "Wlanapi.dll" ( _
  ByVal hClientHandle As LongPtr, _
  Optional ByVal pReserved As LongPtr) As Long

然后在你的函数结束时:

    WlanCloseHandle lngHandle 'Close handle
    GetWiFi = wifiOut   'Success! (function is populated with cached network list)
End Function

任何错误处理程序,如果您要添加一个,都应该测试句柄是否不为 0,如果不是,则关闭它。

我还更改了各种小东西,例如使用 LongPtr 作为指针以使您的代码兼容 64 位(注意:它 VBA6 兼容,这需要许多条件编译),修改您的声明以不使用可选参数,以及其他一些小事情。

我在一台设备上对以下代码进行了 10 次迭代测试,得到了 10 种不同的结果:

代码:

Public Function GetWiFi() As wifis()
'returns an array of custom type WiFis (1st interface only)

    Dim udtList As WLAN_INTERFACE_INFO_LIST, udtAvailList As WLAN_AVAILABLE_NETWORK_LIST, udtNetwork As WLAN_AVAILABLE_NETWORK
    Dim lngReturn As Long, pHandle As LongPtr, lngVersion As Long, pList As LongPtr, pAvailable As LongPtr
    Dim pStart As LongPtr, intCount As Integer, ssid As String, signal As Single, wifiOut() As wifis
    Dim n As Long
    n = 0

    lngReturn = WlanOpenHandle(2&, 0&, lngVersion, pHandle) 'get handle
    If lngReturn <> 0 Then
        Debug.Print "Couldn't get wlan handle (Code " & lngReturn & ")"
        Exit Function
    End If

    lngReturn = WlanEnumInterfaces(ByVal pHandle, 0&, pList) 'enumerate <*first interface only*>
    CopyMemory udtList, ByVal pList, Len(udtList)
    lngReturn = WlanScan(pHandle, udtList.InterfaceInfo.ifGuid)
    lngReturn = WlanGetAvailableNetworkList(pHandle, udtList.InterfaceInfo.ifGuid, 2&, 0&, pAvailable) 'get network list
    CopyMemory udtAvailList, ByVal pAvailable, LenB(udtAvailList)
    intCount = 0
    pStart = pAvailable + 8

    Do
        CopyMemory udtNetwork, ByVal pStart, Len(udtNetwork) ' Populate avail. network structure
        ssid = Replace(StrConv(udtNetwork.dot11Ssid.ucSSID, vbUnicode), Chr(0), "")
        If Len(ssid) < 4 Then ssid = "(Unnamed)"
        signal = CSng(udtNetwork.wlanSignalQuality) / 100
        '[Signal] = 0 to 100 which represents the signal strength (100 Signal)=(-100dBm RSSI), (100 Signal)=(-50dBm RSSI)

        If udtNetwork.dwflags = 0 Then
            n = n + 1
            ReDim Preserve wifiOut(n)
            wifiOut(n).ssid = ssid
            wifiOut(n).signal = signal
        Else
            'skipping networks with [dwflags] > 0
            'I *think* that's what I'm supposed to do
            'Returns 3 for currently connected network, 2 for networks that have profiles
        End If

        intCount = intCount + 1
        pStart = pStart + Len(udtNetwork)
    Loop Until intCount = udtAvailList.dwNumberOfItems
    WlanFreeMemory pAvailable     'clean up memory
    WlanFreeMemory pList
    WlanCloseHandle pHandle 'Close handle
    GetWiFi = wifiOut   'Success! (function is populated with cached network list)
End Function

类型和常量:

Private Const DOT11_SSID_MAX_LENGTH As Long = 32
Private Const WLAN_MAX_PHY_TYPE_NUMBER As Long = 8
Private Const WLAN_AVAILABLE_NETWORK_CONNECTED As Long = 1
Private Const WLAN_AVAILABLE_NETWORK_HAS_PROFILE As Long = 2

Public Type GUID
    Data(15) As Byte
End Type

Private Type WLAN_INTERFACE_INFO
    ifGuid As GUID: InterfaceDescription(255) As Byte: IsState As Long
End Type

Private Type DOT11_SSID
    uSSIDLength As Long:            ucSSID(DOT11_SSID_MAX_LENGTH - 1) As Byte
End Type

Private Type WLAN_AVAILABLE_NETWORK
    strProfileName(511) As Byte:    dot11Ssid As DOT11_SSID
    dot11BssType As Long:           uNumberOfBssids As Long
    bNetworkConnectable As Long:    wlanNotConnectableReason As Long
    uNumberOfPhyTypes As Long:      dot11PhyTypes(WLAN_MAX_PHY_TYPE_NUMBER - 1) As Long
    bMorePhyTypes As Long:          wlanSignalQuality As Long
    bSEcurityEnabled As Long:       dot11DefaultAuthAlgorithm As Long
    dot11DefaultCipherAlgorithm As Long: dwflags As Long: dwReserved As Long
End Type

Private Type WLAN_INTERFACE_INFO_LIST
    dwNumberOfItems As Long: dwIndex As Long: InterfaceInfo As WLAN_INTERFACE_INFO
End Type

Private Type WLAN_AVAILABLE_NETWORK_LIST
    dwNumberOfItems As Long:  dwIndex As Long: Network As WLAN_AVAILABLE_NETWORK
End Type

Public Type WiFis
  ssid As String: signal As Single
End Type

函数声明:

Declare PtrSafe Function WlanOpenHandle Lib "Wlanapi.dll" (ByVal dwClientVersion As Long, _
                ByVal pdwReserved As LongPtr, ByRef pdwNegotiaitedVersion As Long, _
                ByRef phClientHandle As LongPtr) As Long

Declare PtrSafe Function WlanEnumInterfaces Lib "Wlanapi.dll" (ByVal hClientHandle As LongPtr, _
                ByVal pReserved As LongPtr, ByRef ppInterfaceList As LongPtr) As Long

Declare PtrSafe Function WlanGetAvailableNetworkList Lib "Wlanapi.dll" ( _
                ByVal hClientHandle As LongPtr, ByRef pInterfaceGuid As GUID, ByVal dwflags As Long, _
                ByVal pReserved As LongPtr, ByRef ppAvailableNetworkList As LongPtr) As Long


Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
                Source As Any, ByVal Length As Long)

Declare PtrSafe Function WlanScan Lib "Wlanapi.dll" _
        (ByVal hClientHandle As LongPtr, ByRef pInterfaceGuid As GUID, _
        Optional ByVal pDot11Ssid As LongPtr, Optional ByVal pIeData As LongPtr, _
        Optional ByVal pReserved As LongPtr) As Long

Declare PtrSafe Function WlanCloseHandle Lib "Wlanapi.dll" ( _
  ByVal hClientHandle As LongPtr, _
  Optional ByVal pReserved As LongPtr) As Long


Declare PtrSafe Sub WlanFreeMemory Lib "Wlanapi.dll" (ByVal pMemory As LongPtr)

打印列表的测试调用:

Public Sub PrintWifis()
    Dim aWifis() As wifis
    aWifis = GetWiFi
    Dim l As Long
    For l = LBound(aWifis) To UBound(aWifis)
        Debug.Print aWifis(l).ssid; aWifis(l).signal
    Next
End Sub

关于c# - 使用 'WlanScan' 刷新 WiFi 网络列表(将 api 语法从 c# 转换为 vba...或解决方法?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52243831/

相关文章:

c - 在C中逐位读取整个磁盘的内容

c# - ObservableCollection.Next() 和 observablecollection.previous() 函数

c# - 如何将与特定 C# 接口(interface)匹配的所有类型放入 IDictionary 中?

excel - VBA Excel 到 Sql Server

vba - 如何避免向下转换为接口(interface)类?

vba - 单击vba中的按钮

c++ - 打印时GDI折线部分输出

c - PostQuitMessage() 是否进入 WM_DESTROY 或 WM_CLOSE?

c# - 评论会减慢我网页的执行时间吗?

c# - 如何在其他asp.net 项目中使用用C# 编写的DLL?