vb.net连接到WIFI网络

时间:2016-09-12 15:07:35

标签: vb.net

我试图找出如何通过vb.net连接到WIFI网络但是我遇到了问题。我正在走过下面的链接但是我被困在连接上。我认为我的Connect例程会尝试连接但是它没有做任何意味着它通过WlanConnect(ClientHandle,pInterfaceGuid,wlanConnectionParameters,IntPtr.Zero)但是并没有尝试连接。执行语句后,它返回按钮单击。不应该这行代码然后尝试连接到该网络吗?

http://www.pinvoke.net/default.aspx/wlanapi/WlanConnect.html

Imports System.IO
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Net.NetworkInformation
Imports System.Text
Imports System.ComponentModel

Public Class Form1

<DllImport("wlanapi.dll", SetLastError:=True)> _
Public Shared Function WlanConnect( _
    <[In]()> ByVal hClientHandle As IntPtr, _
    <[In]()> ByVal pInterfaceGuid As IntPtr, _
    <[In]()> ByVal pConnectionParameters As IntPtr, _
    <[In]()> ByRef pReserved As IntPtr) As Int32
End Function
''----------------------------------------------------------------------------------
''--------------------------------Native WiFi Structures----------------------------
''----------------------------------------------------------------------------------
<StructLayout(LayoutKind.Sequential)> _
Public Structure WLAN_CONNECTION_PARAMETERS
    Public wlanConnectionMode As WLAN_CONNECTION_MODE
    Public strProfile As String
    Public pDot11Ssid As IntPtr
    Public pDesiredBssidList As IntPtr
    Public dot11BssType As DOT11_BSS_TYPE
    Public dwFlags As Int32
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure DOT11_BSSID_LIST
    Public Header As NDIS_OBJECT_HEADER
    Public uNumOfEntries As UInt32
    Public uTotalNumOfEntries As UInt32
    Public BSSIDs As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure DOT11_MAC_ADDRESS
    <MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.U8, SizeConst:=6)> Public ucDot11MacAddress() As SByte
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure NDIS_OBJECT_HEADER
    Public Type As SByte
    Public Revision As SByte
    Public Size As SByte
End Structure
Public Enum WLAN_CONNECTION_MODE
    wlan_connection_mode_profile
    wlan_connection_mode_temporary_profile
    wlan_connection_mode_discovery_secure
    wlan_connection_mode_discovery_unsecure
    wlan_connection_mode_auto
    wlan_connection_mode_invalid
End Enum
Public Enum DOT11_BSS_TYPE
    dot11_BSS_type_infrastructure
    dot11_BSS_type_independent
    dot11_BSS_type_any
End Enum

Function HasConnectivity() As Boolean
    Dim hostInfo As System.Net.IPHostEntry
    Try 'to look for the logon server
        Dim sServer As String = Environment.GetEnvironmentVariable("logonserver")
        hostInfo = System.Net.Dns.GetHostEntry(sServer.Remove(0, 2))
        Return True
    Catch 'theres no network connection
        Return False
    End Try
End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Connect()
End Sub

Declare Function WlanConnect Lib "wlanapi.dll" (ByVal hClientHandle As IntPtr, _
                     ByRef pInterfaceGuid As Guid, _
                     ByRef pConnectionParameters As WLAN_CONNECTION_PARAMETERS, _
                     ByVal pReserved As IntPtr) As UInteger



Public Shared Sub Connect()
    Dim clienthandle As IntPtr = IntPtr.Zero
    Dim pInterfaceGuid As Guid
    Dim wlanConnectionParameters As New WLAN_CONNECTION_PARAMETERS
    wlanConnectionParameters.dot11BssType = DOT11_BSS_TYPE.dot11_BSS_type_any
    wlanConnectionParameters.dwFlags = 0
    wlanConnectionParameters.strProfile = "NetGear"
    wlanConnectionParameters.wlanConnectionMode = WLAN_CONNECTION_MODE.wlan_connection_mode_profile
    WlanConnect(ClientHandle, pInterfaceGuid, wlanConnectionParameters, IntPtr.Zero)

End Sub
End Class

1 个答案:

答案 0 :(得分:1)

如果您在PC中保存了WLAN配置文件,这种方法很简单。

Sub connectTo(ByVal name As String)
    Dim p = "netsh.exe"
    Dim sInfo As New ProcessStartInfo(p, "wlan connect " & name)
    sInfo.CreateNoWindow = True
    sInfo.WindowStyle = ProcessWindowStyle.Hidden
    Process.Start(sInfo)
End Sub

'use the sub to connect to your AP. connectTo("myAP")

否则,使用ManagedWifiSimpleWifi dll库会更容易。这是我的code,我使用SimpleWifi.dll连接到带有密钥的网络。