;==================================================================================================================== ; Name : _NicControl ; Description : Activate or deactivate a network controler (NIC) ; Syntax : _NicControl($oLanConnection, [$bEnable, , [$bFullName]]) ; Parameter(s) : $oLanConnection - The name of the Lan connection. ; If equal to 'All' then change state of all NIC. ; $bEnable - The state of the NIC (Default 1 for activate). ; $bFullName - Look for string $oLanConnection in NIC name (Default 1 for full name). ; Requirement(s) : Win32_NT OS ; Return value(s) : On Success - Return 1 ; On Failure - Return 0 and @Extented is set. ; @Extented = 1 : Not Win32_NT OS ; @Extented = 2 : Network folder not find. ; @Extented = 3 : Lan connection not find. ; Author : Tlem ; http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=1092 ; Note(s) : Original author : SvenP ; http://www.autoitscript.com/forum/index.php?showtopic=12645&view=findpost&p=87000 ; To Do : Get errors for changing state of multiple NIC (Perhaps with array). ; =================================================================================================================== Func _NicControl($oLanConnection, $bEnable = 1, $bFullName = 1) ; Langage selection. Select ; English (United States) Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409", @OSLang) $strEnableVerb = "En&able" $strDisableVerb = "Disa&ble" If @OSVersion = "WIN_2000" Then $strFolderName = "Network and Dial-up Connections" ; Windows 2000 Else $strFolderName = "Network Connections"; Windows XP EndIf ; Français (France) Case StringInStr("040c,080c,0c0c,100c,140c,180c", @OSLang) $strEnableVerb = "&Activer" $strDisableVerb = "&Désactiver" If @OSVersion = "WIN_2000" Then $strFolderName = "Connexions réseau et accès à distance" ; Windows 2000 Else $strFolderName = "Connexions réseau"; Windows XP EndIf ; Add here the correct Verbs for your Operating System Language EndSelect ; Control OS type. If @OSTYPE <> "WIN32_NT" Then SetError(1, 1) Return 0 EndIf ; Virtual folder containing icons for the Control Panel applications. (value = 3) Const $ssfCONTROLS = 3 $ShellApp = ObjCreate("Shell.Application") $oControlPanel = $ShellApp.Namespace($ssfCONTROLS) ; Find 'Network connections' control panel item $oNetConnections = "" For $FolderItem In $oControlPanel.Items If $FolderItem.Name = $strFolderName Then $oNetConnections = $FolderItem.GetFolder ExitLoop EndIf Next ; If no 'Network connections' folder then return error. If Not IsObj($oNetConnections) Then SetError(1, 2) Return 0 EndIf ; Change all NIC state (Do not return errors). If StringLower($oLanConnection) = "all" Then For $FolderItem In $oNetConnections.Items _NicControl($FolderItem.name, $bEnable) Next Return 1 EndIf ; Change NIC state by partial name (Do not return errors). If $bFullName = 0 Then For $FolderItem In $oNetConnections.Items If StringInStr(StringLower($FolderItem.name), StringLower($oLanConnection)) Then _NicControl($FolderItem, $bEnable) EndIf Next Return 1 EndIf ; Find the collection of the network connection name. For $FolderItem In $oNetConnections.Items If StringLower($FolderItem.Name) = StringLower($oLanConnection) Then $oLanConnection = $FolderItem ExitLoop EndIf Next ; If no network connection name then return error. If Not IsObj($oLanConnection) Then SetError(1, 3) Return 0 EndIf $oEnableVerb = "" $oDisableVerb = "" ; Find the state of the network connection. For $Verb In $oLanConnection.Verbs If $Verb.Name = $strEnableVerb Then $oEnableVerb = $Verb EndIf If $Verb.Name = $strDisableVerb Then $oDisableVerb = $Verb EndIf Next ; Enable NIC If $bEnable Then If IsObj($oEnableVerb) Then $oEnableVerb.DoIt EndIf ; Disable NIC If Not $bEnable Then If IsObj($oDisableVerb) Then $oDisableVerb.DoIt EndIf $begin = TimerInit() While 1 $dif = Int(TimerDiff($begin) / 1000) If $dif > 10 Then ExitLoop ; Control the state of the NIC to exit before the end of waiting time. If $bEnable = 1 And _GetNicState($oLanConnection, $strEnableVerb, $strDisableVerb) = 1 Then ExitLoop If $bEnable = 0 And _GetNicState($oLanConnection, $strEnableVerb, $strDisableVerb) = 0 Then ExitLoop Sleep(100) WEnd ; Set the return value of the function. $Res = _GetNicState($oLanConnection, $strEnableVerb, $strDisableVerb) If $bEnable = 1 And $Res = 0 Then Return 0 ElseIf $bEnable = 0 And $Res = 1 Then Return 0 Else Return 1 EndIf EndFunc ;==>_NicControl ; Function that give the state of the lan connection (1 = Active 0 = Not Active). Func _GetNicState($oLanConnection, $strEnableVerb, $strDisableVerb) For $Verb In $oLanConnection.Verbs If $Verb.Name = $strEnableVerb Then Return 0 Else Return 1 EndIf Next EndFunc ;==>_GetNicState