1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| Option Strict On
Option Explicit On
Imports System.Runtime.InteropServices
Imports WIFI_WlanAPI.NativeMethods
Imports System.ComponentModel
Public Class Form1 ' button1, textbox1
Private clientVers As ClientVersion
Private hClient As IntPtr
Private pList As IntPtr 'pointeur vers la liste de WLAN_INTERFACE_INFO_LIST
Private pInfos As IntPtr ' pointeur vers une struture WLAN_INTERFACE_INFO
Private AdapterInfo As WLAN_INTERFACE_INFO
Private result As UInteger
Private pNetList As New IntPtr
Private pNetInfos As New IntPtr ' pointeur vers une struture WLAN_AVAILABLE_NETWORK
Private pBSSIList As New IntPtr
Private pBSSIInfos As New IntPtr
Private NetworkInfo As WLAN_AVAILABLE_NETWORK
Private infos As New List(Of NativeMethods.WLAN_INTERFACE_INFO)
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
clientVers = GetClientVersion()
hClient = IntPtr.Zero
Dim negotiatedversion As Integer
' récupération du Handle de la carte Wifi
result = WlanOpenHandle(clientVers, _
Nothing, _
negotiatedversion, _
hClient)
If result <> 0 Then Throw New Win32Exception()
' récupération du pointeur vers la structure
pList = Nothing
result = WlanEnumInterfaces(hClient, Nothing, pList)
If result <> 0 Then Throw New Win32Exception()
' extraction des données
Dim list As WLAN_INTERFACE_INFO_LIST = _
DirectCast(Marshal.PtrToStructure(pList, _
GetType(WLAN_INTERFACE_INFO_LIST)), _
WLAN_INTERFACE_INFO_LIST)
If list.NumberOfItems = 0 Then
MessageBox.Show("nothing found")
CloseHClient(hClient)
FreeMemory(pList)
Exit Sub
End If
pInfos = New IntPtr(pList.ToInt32 + 8) ' 2 DWORDS
For i As Integer = 0 To list.NumberOfItems - 1
' TODO récupérer les données en utilisant (Marshal.PtrToStructure)
AdapterInfo = New WLAN_INTERFACE_INFO
AdapterInfo.InterfaceGuid = DirectCast(Marshal.PtrToStructure(pInfos, GetType(Guid)), Guid)
pInfos = New IntPtr(pInfos.ToInt32 + Marshal.SizeOf(GetType(Guid)))
AdapterInfo.InterfaceDescription = Marshal.PtrToStringUni(pInfos)
pInfos = New IntPtr(pInfos.ToInt32 + 512)
AdapterInfo.state = DirectCast(Marshal.ReadInt32(pInfos), WLAN_INTERFACE_STATE)
pInfos = New IntPtr(pInfos.ToInt32 + 4)
Me.TextBox1.Text &= AdapterInfo.InterfaceDescription
Me.TextBox1.Text &= Environment.NewLine
Me.TextBox1.Text &= AdapterInfo.InterfaceGuid.ToString
Me.TextBox1.Text &= Environment.NewLine
Me.TextBox1.Text &= AdapterInfo.state.ToString
Me.TextBox1.Text &= Environment.NewLine
infos.Add(AdapterInfo)
Next ' TODO :pour lire la carte suivante il faut déplacer le pointeur de la longueur de la structure
End Sub
Private Sub CloseHClient(ByVal hClient As IntPtr)
If hClient <> IntPtr.Zero Then
If NativeMethods.WlanCloseHandle(hClient, Nothing) <> 0 Then
Throw New System.ComponentModel.Win32Exception()
End If
End If
End Sub
Private Sub FreeMemory(ByVal ptr As IntPtr)
If ptr <> IntPtr.Zero Then
NativeMethods.WlanFreeMemory(ptr)
End If
End Sub
Public Enum ClientVersion As UInteger
XPSP2 = 1
Vista = 2
End Enum
Public Function GetClientVersion() As ClientVersion
Select Case Environment.OSVersion.Version.Major
Case 5
Return clientVersion.XPSP2
Case 6
Return clientVersion.Vista
Case Else
Throw New System.NotSupportedException( _
"Get a newer or older OS!")
End Select
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As UInt32 = 0
pNetList = Nothing
result = WlanGetAvailableNetworkList(hClient, infos(0).InterfaceGuid, 0, Nothing, pNetList) 'pNetList pointe vers une structure du type WLAN_AVAILABLE_NETWORK
If result <> 0 Then
Throw New Win32Exception(Convert.ToInt32(result), " result=" & result.ToString)
Exit Sub
End If
Dim Netlist As WLAN_AVAILABLE_NETWORK_LIST = _
DirectCast(Marshal.PtrToStructure(pNetList, _
GetType(WLAN_AVAILABLE_NETWORK_LIST)), _
WLAN_AVAILABLE_NETWORK_LIST)
If Netlist.NumberOfItems = 0 Then
MessageBox.Show("nothing found")
CloseHClient(hClient)
FreeMemory(pList)
Exit Sub
End If
pNetInfos = New IntPtr(pNetList.ToInt32 + 8) ' 2 DWORDS
For i = 0 To CUInt(Netlist.NumberOfItems - 1)
Dim APinfo As WLAN_AVAILABLE_NETWORK = _
DirectCast(Marshal.PtrToStructure(pNetInfos, _
GetType(WLAN_AVAILABLE_NETWORK)), _
WLAN_AVAILABLE_NETWORK)
' APinfo contient ici l'ensemble des données.
Dim StrutureLength As Integer = Marshal.SizeOf(APinfo)
pNetInfos = New IntPtr(pNetInfos.ToInt32 + StrutureLength) ' on déplace le pointeur vers le début de la structure suivante (AP suivant)
Next
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
FreeMemory(pList)
FreeMemory(pNetList)
CloseHClient(hClient)
End Sub
End Class |
Partager