Jump to content

Enable/Disable connection


bybordo
 Share

Recommended Posts

Works fine compiled on my end on win7 32bit using normal stable AutoIT, not beta
 

What are you doing to confirm that it's not working.

If your computer has been setup with a static IP, this will not disable your connection.  Your releasing your DHCP lease here and then renewing it.  Sorry if this is already known to you, can't tell much from your post.

Link to comment
Share on other sites

In case you are talking about wifi connection here you go:

Global $hDLL = DllOpen("wlanapi.dll"), $tGUID = DllStructCreate("byte[16]")
OnAutoItExitRegister("_End")
Local $hClientHandle, $abGUIDs, $pGUID, $avNetworks, $sSSID = "wifi123" ; enter your profile name here

$hClientHandle = _GetClientHandle()
$abGUIDs = _GetInterfaces($hClientHandle)
$pGUID = _SelectInterface($abGUIDs[0])
$avNetworks = _GetNetworkList($hClientHandle, $pGUID, 0)
$disconnect = _Disconnect($hClientHandle, $pGUID)
ConsoleWrite("Disconnected from: " & $sSSID & @CRLF)
$connect = _Connect($sSSID)
If Not @error Then ConsoleWrite("Connected to: " & $sSSID & @CRLF)


Func _GetClientHandle()
Local $aResult = DllCall($hDLL, "dword", "WlanOpenHandle", "dword", 2, "ptr", 0, "dword*", 0, "hwnd*", 0)
If @error Or $aResult[0] Then
ConsoleWrite("WlanOpenHandle Failed" & @CRLF)
Exit
EndIf

Return $aResult[4]
EndFunc ;==>_GetClientHandle

Func _Disconnect($hClientHandle, $pGUID)
$aResult = DllCall($hDLL, "dword", "WlanDisconnect", "hwnd", $hClientHandle, "ptr", $pGUID, "ptr", 0)
If @error Then Return SetError(4, 0, 0)
If $aResult[0] Then SetError(1, 0, 0)
EndFunc ;==>_Disconnect

Func _Connect($sSSID)
Local $tSSID, $tParameters
Local Const $WLAN_CONNECTION_MODE_PROFILE = 0
Local Const $DOT11_BSS_TYPE_INFRASTRUCTURE = 1, $DOT11_BSS_TYPE_INDEPENDENT = 2
Local Const $WLAN_CONNECTION_HIDDEN_NETWORK = 1, $WLAN_CONNECTION_ADHOC_JOIN_ONLY = 2

$tSSID = DllStructCreate("WCHAR[32]")
DllStructSetData($tSSID, 1, $sSSID)
$tParameters = DllStructCreate("dword Mode; ptr SSID; ptr; ptr; dword BSSType; dword Flags")

DllStructSetData($tParameters, "Mode", $WLAN_CONNECTION_MODE_PROFILE)
DllStructSetData($tParameters, "SSID", DllStructGetPtr($tSSID))
DllStructSetData($tParameters, "BSSType", $DOT11_BSS_TYPE_INFRASTRUCTURE)
DllStructSetData($tParameters, "Flags", 0)

$aResult = DllCall($hDLL, "dword", "WlanConnect", "hwnd", $hClientHandle, "ptr", $pGUID, "ptr", DllStructGetPtr($tParameters), "ptr", 0)
If @error Or $aResult[0] Then
If @error Then
ConsoleWrite("DllCall failure - WlanConnect" & @CRLF & "ERROR: " & @error & @CRLF & "EXTENDED: " & @extended & @CRLF)
Else
ConsoleWrite("API error - WlanConnect" & @CRLF & "ERROR: " & $aResult[0] & @CRLF)
EndIf
EndIf
EndFunc ;==>_Connect


Func _GetProfile($hClientHandle, $pGUID, $SSID)
Local $XMLProfile, $Profile[11], $ProfileAttributes, $TMP

$XMLProfile = _GetProfileXML($hClientHandle, $pGUID, $SSID)
If @error Then Return SetError(@error, @extended, $XMLProfile)

$ProfileAttributes = StringSplit('<name>|<connectionType>|<connectionMode>|<authentication>|<encryption>|<useOneX>|<keyType>|<keyMaterial>|<keyIndex>|<Type xmlns="http://www.microsoft.com/provisioning/EapCommon">|<ConfigBlob>', "|")

For $i = 1 To UBound($ProfileAttributes) - 1
$TMP = StringRegExp($XMLProfile, $ProfileAttributes[$i] & "([^<]{0,})<", 1)
If IsArray($TMP) Then
$Profile[$i - 1] = $TMP[0]
If $TMP[0] == "ESS" Then $Profile[$i - 1] = "Infrastructure"
If $TMP[0] == "IBSS" Then $Profile[$i - 1] = "Ad Hoc"
If $TMP[0] == "auto" Then $Profile[$i - 1] = "Automatic"
If $TMP[0] == "manual" Then $Profile[$i - 1] = "Manual"
If $TMP[0] == "open" Then $Profile[$i - 1] = "Open"
If $TMP[0] == "shared" Then $Profile[$i - 1] = "Shared Key"
If $TMP[0] == "WPAPSK" Then $Profile[$i - 1] = "WPA-PSK"
If $TMP[0] == "WPA2PSK" Then $Profile[$i - 1] = "WPA2-PSK"
If $TMP[0] == "none" Then $Profile[$i - 1] = "Unencrypted"
If $TMP[0] == "true" Then $Profile[$i - 1] = "802.1x Enabled"
If $TMP[0] == "false" Then $Profile[$i - 1] = "802.1x Disabled"
If $TMP[0] == "networkKey" Then $Profile[$i - 1] = "Network Key"
If $TMP[0] == "passPhrase" Then $Profile[$i - 1] = "Pass Phrase"
If $TMP[0] == "13" Then $Profile[$i - 1] = "TLS"
If $TMP[0] == "25" Then $Profile[$i - 1] = "PEAP"
EndIf
Next

If Not $Profile[2] And $Profile[1] Then $Profile[2] = "Automatic"
If Not $Profile[7] Then $Profile[6] = "No Key Material"
If Not $Profile[7] Then $Profile[7] = "No Key Material"
If $Profile[8] Then $Profile[8] += 1
If Not $Profile[8] Then $Profile[8] = "No Key Index"
If Not $Profile[10] Then $Profile[9] = "No Blob"
If Not $Profile[10] Then $Profile[10] = "No Blob"
Return $Profile
EndFunc ;==>_GetProfile

Func _GetProfileXML($hClientHandle, $pGUID, $SSID)
$aResult = DllCall($hDLL, "dword", "WlanGetProfile", "hwnd", $hClientHandle, "ptr", $pGUID, "wstr", $SSID, "ptr", 0, "wstr*", 0, "ptr*", 0, "ptr*", 0)
If @error Then Return SetError(4, 0, 0)
If $aResult[0] Then Return SetError(1, $aResult[0], 0)
Return $aResult[5]
EndFunc ;==>_GetProfileXML

Func _GetInterfaces($hClientHandle)
Local $aResult, $pInterfaceList, $tInterfaceList, $iInterfaceCount, $pInterface, $tInterface

$aResult = DllCall($hDLL, "dword", "WlanEnumInterfaces", "hwnd", $hClientHandle, "ptr", 0, "ptr*", 0)
If @error Or $aResult[0] Then
ConsoleWrite("WlanEnumInterfaces Failed" & @CRLF)
Exit
EndIf

$pInterfaceList = $aResult[3]


$tInterfaceList = DllStructCreate("dword", $pInterfaceList)
$iInterfaceCount = DllStructGetData($tInterfaceList, 1)

If Not $iInterfaceCount Then
ConsoleWrite("no interfaces were found" & @CRLF)
Exit
EndIf

Local $abGUIDs[$iInterfaceCount]


For $i = 0 To $iInterfaceCount - 1
$pInterface = Ptr(Number($pInterfaceList) + ($i * 532 + 8))
$tInterface = DllStructCreate("byte GUID[16]; wchar descr[256]; int State", $pInterface)
$abGUIDs[$i] = DllStructGetData($tInterface, "GUID")
ConsoleWrite("Found Interface: " & DllStructGetData($tInterface, "descr") & @CRLF)
Next

DllCall($hDLL, "dword", "WlanFreeMemory", "ptr", $pInterfaceList) ;free memory

Return $abGUIDs
EndFunc ;==>_GetInterfaces

Func _SelectInterface($bGUID)
DllStructSetData($tGUID, 1, $bGUID)
Return DllStructGetPtr($tGUID)
EndFunc ;==>_SelectInterface

Func _GetNetworkList($hClientHandle, $pGUID, $iFlags)
Local $aResult, $pNetworkList, $tNetworkList, $iNetworkCount, $pNetworkItem, $tNetworkItem
$aResult = DllCall($hDLL, "dword", "WlanGetAvailableNetworkList", "hwnd", $hClientHandle, "ptr", $pGUID, "int", $iFlags, "ptr", 0, "ptr*", 0)
If @error Or $aResult[0] Then
ConsoleWrite("WlanGetAvailableNetworkList Failed" & @CRLF)
Exit
EndIf

$pNetworkList = $aResult[5]
$tNetworkList = DllStructCreate("dword", $pNetworkList)
$iNetworkCount = DllStructGetData($tNetworkList, 1)
If Not $iNetworkCount Then
ConsoleWrite("no networks were found" & @CRLF)
Exit
EndIf

Local $avNetworks[$iNetworkCount][20]
For $i = 0 To $iNetworkCount - 1
$pNetworkItem = Ptr(Number($pNetworkList) + ($i * 628 + 8))
$tNetworkItem = DllStructCreate("wchar ProfName[256]; dword SSIDLen; char SSID[32]; dword BSSType; dword NoBSSIDs; dword Connectable; dword RsnCode; dword NoPhyTypes; " & _
"dword PhyTypes[8]; dword MorePhyTypes; dword Signal; dword SecEnabled; dword Auth; dword Ciph; dword Flags", $pNetworkItem)
$avNetworks[$i][0] = DllStructGetData($tNetworkItem, "ProfName")
$avNetworks[$i][1] = DllStructGetData($tNetworkItem, "SSID")
$avNetworks[$i][2] = DllStructGetData($tNetworkItem, "Signal")
If DllStructGetData($tNetworkItem, "SecEnabled") Then
$avNetworks[$i][3] = "Secure"
Else
$avNetworks[$i][3] = "Not Secure"
EndIf
ConsoleWrite("Found Network: " & $avNetworks[$i][1] & " [" & $avNetworks[$i][2] & "%]" & "-" & $avNetworks[$i][3] & @CRLF)
Next

DllCall($hDLL, "dword", "WlanFreeMemory", "ptr", $pNetworkList) ;Free memory

Return $avNetworks
EndFunc ;==>_GetNetworkList

Func _End()
DllCall($hDLL, "dword", "WlanCloseHandle", "ptr", $hClientHandle, "ptr", 0)
DllClose($hDLL)
EndFunc ;==>_End
_Connect function was made by MattyD.

He actually has an entire UDF about wlan.api. Check the Example Scipt forum

Edited by AutID
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...