Jump to content
Sign in to follow this  
Docfxit

Disable/Enable Local Area Connection

Recommended Posts

Docfxit

I would like to Disable/Enable a Local Area Connection.

I found this script that isn't working in Win_7 SP1 64bit.

I'm getting an error information window saying false.

NetCCExample.au3

#include "NetCC.au3"

AutoItSetOption("TrayIconDebug", 1) ;0-off

;If bEnabled Then
;   oDisalbeVerb.DoIt()
;Else
;   oEnableVerb.DoIt
;EndIf

;  Activation of all network cards.
;    _NetCC (" All ")

; Activation of all network cards the name of which contains "place ".
;    _NetCC ("place ", 1, 0)

; Deactivation of connection 1394.
    _NetCC (" local ", 0)

; Deactivation of a nonexistent connection and return of error.
;    $Res _NetCC ("Connection XYZ", 0)
    MsgBox (0, "Information ", "$Res " and $Res and " @Error " and @error and " @Extented " and @extended)

NetCC.au3

#cs ===================================================================================================================
Network Connection Control (NetCC.au3) By Tlem

Aviable Functions :
_NetCC() = Change state or get state of network connections
_NetCCGetList() = Get list of network connections.


Some parts of this code is inspired from other codes.
So tanks to : SvenP, Danny35d, smashly.
- http://www.autoitscript.com/forum/index.php?showtopic=12645&view=findpost&p=87000
- http://www.autoitscript.com/forum/index.php?showtopic=72165&view=findpost&p=528862
- http://www.autoitscript.com/forum/index.php?showtopic=74074&view=findpost&p=538835

#ce ===================================================================================================================
#include-once
#RequireAdmin
Global Const $NETWORK_FOLDER = 0x31
Global $strEnableVerb, $strDisableVerb, $strRepairVerb, $strStateVerb, $strFolderName

;====================================================================================================================
; Name           : _NetCC
; Description    : Activate or deactivate a Network Interface Controler (NIC)
; Syntax         : _NetCC($oLanConnection, [$iFlag, , [$iFullName]])
; Parameter(s)   : $oLanConnection - The name of the Lan connection.
; If equal to 'All' then change state of all NIC.
;                    $iFlag - 0 = Disable connection.
; 1 = Enable connection (default)
; 2 = Toggle connection
; 3 = Repair connection
; 4 = Check State of a connection.
;                    $iFullName - 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 @error 1~5.
; @error 1 = Not Win32_NT OS
; @error 2 = Network folder not find.
; @error 3 = Lan connection not find.
; @error 4 = Shell Application object creation error.
; @error 5 = Verb or function not available.
; Author         : Tlem <tlem at tuxolem dot net>
; http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=1092
; Note(s)        :
;
; To Do : Get individual errors for changing state of multiple NIC (Perhaps with array).
; ===================================================================================================================
Func _NetCC($oLanConnection, $iFlag = 1, $iFullName = 1)
; Control OS type.
If @OSTYPE <> "WIN32_NT" Then
Return SetError(1, 0, 0)
EndIf

; W2K/XP/W2K3
If StringInStr("WIN_2000,WIN_XP,WIN_2003,WIN_7", @OSVersion) Then
; Change all NIC state.
If StringLower($oLanConnection) = "all" Then
$Res = _NetCCAll($iFlag, 0)
; Change NIC state by partial name.
ElseIf $iFullName = 0 Then
$Res = _NetCCPartial($oLanConnection, $iFlag, 0)
; Change NIC state for $oLanConnection.
Else
$Res = _NetCCWin32($oLanConnection, $iFlag, $iFullName)
EndIf
Else
; Vista/W2K8
; Option repair not exits in Vista/2008
If $iFlag = 3 Then Return SetError(5, 0, 0)
; Change all NIC state.
If StringLower($oLanConnection) = "all" Then
$Res = _NetCCAll($iFlag, 1)
; Change NIC state by partial name.
ElseIf $iFullName = 0 Then
$Res = _NetCCPartial($oLanConnection, $iFlag, 1)
; Change NIC state for $oLanConnection.
Else
$Res = _NetCCWin32New($oLanConnection, $iFlag, $iFullName)
EndIf
EndIf
Return SetError(@error, @extended, $Res)
EndFunc ;==>_NetCC

; Only for W2K/XP/W2K3
Func _NetCCWin32($oLanConnection, $iFlag = 1, $iFullName = 1)
Local $objShell, $objFolder

$objShell = ObjCreate("Shell.Application")
If Not IsObj($objShell) Then Return SetError(2, 0, 0)

$objFolder = $objShell.NameSpace($NETWORK_FOLDER)
If Not IsObj($objFolder) Then Return SetError(3, 0, 0)

; Find the collection of the network connection name.
For $LanItem In $objFolder.Items
If StringLower($LanItem.Name) = StringLower($oLanConnection) Then
$oLanConnection = $LanItem
ExitLoop
EndIf
Next
; If no network connection name then return error.
If Not IsObj($oLanConnection) Then Return SetError(3, 0, 0)

$oEnableVerb = ""
$oDisableVerb = ""

; Find the state of the network connection.
For $Verb In $oLanConnection.Verbs
If $Verb.Name = $strEnableVerb Then
$oEnableVerb = $Verb
If Not IsObj($oEnableVerb) Then Return SetError(5, 0, 0)
$State = 0
EndIf
If $Verb.Name = $strDisableVerb Then
$oDisableVerb = $Verb
If Not IsObj($oDisableVerb) Then Return SetError(5, 0, 0)
$State = 1
EndIf
If $Verb.Name = $strRepairVerb Then
$oRepairVerb = $Verb
If Not IsObj($oRepairVerb) Then Return SetError(5, 0, 0)
EndIf
Next

; Return State
If $iFlag = 4 Then
Return $State
; Repair Net Connection
ElseIf $iFlag = 3 And $State = 1 Then
$oRepairVerb.DoIt
; Toggle Net Connection
ElseIf $iFlag = 2 Then
If $State = 1 Then
$oDisableVerb.DoIt
Else
$oEnableVerb.DoIt
EndIf
; Enable Net Connection
ElseIf $iFlag = 1 And $State = 0 Then
$oEnableVerb.DoIt
; Disable Net Connection
ElseIf $iFlag = 0 And $State = 1 Then
$oDisableVerb.DoIt
; Else there is an error
Else
Return SetError(5, 0, 0)
EndIf

; Loop to wait change state (for changing state of more than one connection).
$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 $iFlag = 3 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop
If $iFlag = 2 And $State = 1 And _NetCCWin32($oLanConnection, 4) = 0 Then ExitLoop
If $iFlag = 2 And $State = 0 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop
If $iFlag = 1 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop
If $iFlag = 0 And _NetCCWin32($oLanConnection, 4) = 0 Then ExitLoop
Sleep(100)
WEnd

; Set the return value of the function.
$Res = _NetCCWin32($oLanConnection, 4)
If $iFlag = 4 And $Res = 1 Then
Sleep(3000)
Return 1
ElseIf $iFlag = 1 And $Res = 0 Then
Return 0
ElseIf $iFlag = 0 And $Res = 1 Then
Return 0
Else
Return 1
EndIf
EndFunc ;==>_NetCCWin32

; Only for Vista/W2K8
Func _NetCCWin32New($oLanConnection, $iFlag = 1, $iFullName = 1)
Local $objWMIService, $colItems, $iModState = 0

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x30)
If Not IsObj($colItems) Then Return SetError(4, 0, 0)

; Change NIC state of $oLanConnection
; Check name of the connection
For $objItem In $colItems
If $objItem.NetConnectionID = $oLanConnection Then
; Check State.
If $iFlag = 4 Then
If $objItem.NetEnabled = True Then
Return 1
Else
Return 0
EndIf
Else
; Change the State of Net Connection.
; Disable Net Connection
If $iFlag = 0 And $objItem.NetEnabled = True Then
$objItem.Disable
; Enable Net Connection
ElseIf $iFlag = 1 And $objItem.NetEnabled = False Then
$objItem.Enable
; Toggle Net Connection
ElseIf $iFlag = 2 Then
If $objItem.NetEnabled = True Then
$objItem.Disable
$State = 1
Else
$objItem.Enable
$State = 0
EndIf
EndIf
; One change be done.
$iModState = 1
EndIf
ExitLoop
EndIf
Next
; If no connection then error.
If $iModState = 0 Then Return SetError(3, 0, 0)


; Loop to wait state change.
If $iFlag = 4 Then
While $iFlag <> _NetCCWin32New($oLanConnection, 4)
Sleep(250)
WEnd
Else
Sleep(1000)
If $iFlag = 2 Then
If $State = 0 And _NetCCWin32New($oLanConnection, 4) = 1 Then
Return 1
ElseIf $State = 1 And _NetCCWin32New($oLanConnection, 4) = 0 Then
Return 1
Else
Return 0
EndIf
ElseIf $iFlag = 1 And _NetCCWin32New($oLanConnection, 4) = 1 Then
Return 1
ElseIf $iFlag = 0 And _NetCCWin32New($oLanConnection, 4) = 0 Then
Return 1
Else
Return 0
EndIf
EndIf
EndFunc ;==>_NetCCWin32New

;====================================================================================================================
; Name           : _NetCCGetList()
; Description    : Get list of network connections.
; Syntax         : _NetCCGetList()
; Parameter(s)   : NA.
; Requirement(s) : Win32_NT OS
; Return value(s) : Array of network connections.
;                    On Failure - Return 0 and @error 3.
; @error 3 = Lan connection not find.
; Author         : Tlem <tlem at tuxolem dot net>
; http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=1092
; Note(s)        :
;
; ===================================================================================================================
Func _NetCCGetList()
; W2K/XP/W2K3
If StringInStr("WIN_2000,WIN_XP,WIN_2003", @OSVersion) Then
Local $objShell, $objFolder, $sTmp = ''

_NetCCLang()

$objShell = ObjCreate("Shell.Application")
If Not IsObj($objShell) Then Return SetError(4, 0, 0)
$objFolder = $objShell.NameSpace($NETWORK_FOLDER)
If Not IsObj($objFolder) Then Return SetError(3, 0, 0)

For $objItem In $objFolder.Items
For $oVerb In $objItem.Verbs
If StringInStr($oVerb.Name, $strStateVerb) Then $sTmp &= $objItem.Name & "|"
Next
Next
Else
; Vista/W2K8
Local $objWMIService, $colItems, $sTmp = ''
$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x30)
If Not IsObj($colItems) Then Return SetError(4, 0, 0)

For $objItem In $colItems
If $objItem.NetConnectionID Then
$sTmp &= $objItem.NetConnectionID & "|"
EndIf
Next
EndIf

If $sTmp = '' Then Return SetError(3, 0, 0)
$aNetList = StringSplit(StringTrimRight($sTmp, 1), "|")

Return $aNetList
EndFunc ;==>_NetCCGetList

Func _NetCCAll($iFlag, $Func = 0)
$iModState = 0
$aNetCon = _NetCCGetList()
For $i = 1 To $aNetCon[0]
If $Func = 1 Then
_NetCCWin32New($aNetCon[$i], $iFlag)
Else
_NetCCWin32($aNetCon[$i], $iFlag)
EndIf
$iModState = 1
Next
; If no connection find.
If $iModState = 0 Then
Return SetError(3, 0, 0)
Else
Return 1
EndIf
EndFunc ;==>_NetCCAll

Func _NetCCPartial($oLanConnection, $iFlag, $Func)
$iModState = 0
$aNetCon = _NetCCGetList()
For $i = 1 To $aNetCon[0]
If StringInStr(StringLower($aNetCon[$i]), StringLower($oLanConnection)) Then
If $Func = 1 Then
_NetCCWin32New($aNetCon[$i], $iFlag)
Else
_NetCCWin32($aNetCon[$i], $iFlag)
EndIf
$iModState = 1
EndIf
Next
; If no connection find.
If $iModState = 0 Then
Return SetError(3, 0, 0)
Else
Return 1
EndIf
EndFunc ;==>_NetCCPartial

Func _NetCCLang()
; 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"
$strRepairVerb = "Re&pair"
If @OSVersion = "WIN_2000" Then
$strStateVerb = "St&ate" ; Windows 2000 (Not sur if correct)
Else
$strStateVerb = "Stat&us"; Windows XP
EndIf

; Français (France)
Case StringInStr("040c,080c,0c0c,100c,140c,180c", @OSLang)
$strEnableVerb = "&Activer"
$strDisableVerb = "&Désactiver"
$strRepairVerb = "&Réparer"
If @OSVersion = "WIN_2000" Then
$strStateVerb = "É&tat" ; Windows 2000
Else
$strStateVerb = "Sta&tut"; Windows XP
EndIf

; Add here the correct Verbs for your Operating System Language
EndSelect
EndFunc ;==>_NetCCLang

Thanks for looking at it,

Docfxit

Edited by Docfxit

Share this post


Link to post
Share on other sites
Docfxit

Thanks for the reply...

If you follow that post all the way to the end the author has posted the latest and greatest code. That is the code I started from in this thread. I'd like to get it working.

Thanks,

Docfxit

Share this post


Link to post
Share on other sites
BrewManNH

It's because that was supposed to be used as an #include, and you put the whole thing inside your script. That means the Global Const's at the top of the include file never get seen, so it errors out. Either make the file an include to your main script, or move the constants to the top of your script before any of your code.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
JLogan3o13

I realize the OP's main question was how to get the script working, but I'm curious - if enabling/disabling the interface is all you want to do, why not just do a netsh call? I use this on Win7 pretty regularly.

#RequireAdmin
ShellExecuteWait("netsh.exe", 'int set interface "Local Area Connection" disable', "", "", @SW_HIDE)

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Docfxit

It's because that was supposed to be used as an #include, and you put the whole thing inside your script. That means the Global Const's at the top of the include file never get seen, so it errors out. Either make the file an include to your main script, or move the constants to the top of your script before any of your code.

Thanks for the reply...

I created this:

NetCCExample.au3

#include "NetCC.au3"

AutoItSetOption("TrayIconDebug", 1) ;0-off
; Activation of all network cards.
; _NetCC (" All ")

; Activation of all network cards the name of which contains "place ".
; _NetCC ("place ", 1, 0)

; Deactivation of connection 1394.
_NetCC (" local ", 0)

; Deactivation of a nonexistent connection and return of error.
; $Res _NetCC ("Connection XYZ", 0)
MsgBox (0, "Information ", "$Res " and $Res and " @Error " and @error and " @Extented " and @extended)

NetCC.au3

#cs ===================================================================================================================
Network Connection Control (NetCC.au3) By Tlem

Aviable Functions :
_NetCC() = Change state or get state of network connections
_NetCCGetList() = Get list of network connections.


Some parts of this code is inspired from other codes.
So tanks to : SvenP, Danny35d, smashly.
- http://www.autoitscript.com/forum/index.php?showtopic=12645&view=findpost&p=87000
- http://www.autoitscript.com/forum/index.php?showtopic=72165&view=findpost&p=528862
- http://www.autoitscript.com/forum/index.php?showtopic=74074&view=findpost&p=538835

#ce ===================================================================================================================
#include-once
#RequireAdmin
Global Const $NETWORK_FOLDER = 0x31
Global $strEnableVerb, $strDisableVerb, $strRepairVerb, $strStateVerb, $strFolderName

;====================================================================================================================
; Name           : _NetCC
; Description    : Activate or deactivate a Network Interface Controler (NIC)
; Syntax         : _NetCC($oLanConnection, [$iFlag, , [$iFullName]])
; Parameter(s)   : $oLanConnection - The name of the Lan connection.
; If equal to 'All' then change state of all NIC.
;                    $iFlag - 0 = Disable connection.
; 1 = Enable connection (default)
; 2 = Toggle connection
; 3 = Repair connection
; 4 = Check State of a connection.
;                    $iFullName - 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 @error 1~5.
; @error 1 = Not Win32_NT OS
; @error 2 = Network folder not find.
; @error 3 = Lan connection not find.
; @error 4 = Shell Application object creation error.
; @error 5 = Verb or function not available.
; Author         : Tlem <tlem at tuxolem dot net>
; http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=1092
; Note(s)        :
;
; To Do : Get individual errors for changing state of multiple NIC (Perhaps with array).
; ===================================================================================================================
Func _NetCC($oLanConnection, $iFlag = 1, $iFullName = 1)
; Control OS type.
If @OSTYPE <> "WIN32_NT" Then
Return SetError(1, 0, 0)
EndIf

; W2K/XP/W2K3
If StringInStr("WIN_2000,WIN_XP,WIN_2003,WIN_7", @OSVersion) Then
; Change all NIC state.
If StringLower($oLanConnection) = "all" Then
$Res = _NetCCAll($iFlag, 0)
; Change NIC state by partial name.
ElseIf $iFullName = 0 Then
$Res = _NetCCPartial($oLanConnection, $iFlag, 0)
; Change NIC state for $oLanConnection.
Else
$Res = _NetCCWin32($oLanConnection, $iFlag, $iFullName)
EndIf
Else
; Vista/W2K8
; Option repair not exits in Vista/2008
If $iFlag = 3 Then Return SetError(5, 0, 0)
; Change all NIC state.
If StringLower($oLanConnection) = "all" Then
$Res = _NetCCAll($iFlag, 1)
; Change NIC state by partial name.
ElseIf $iFullName = 0 Then
$Res = _NetCCPartial($oLanConnection, $iFlag, 1)
; Change NIC state for $oLanConnection.
Else
$Res = _NetCCWin32New($oLanConnection, $iFlag, $iFullName)
EndIf
EndIf
Return SetError(@error, @extended, $Res)
EndFunc ;==>_NetCC

; Only for W2K/XP/W2K3
Func _NetCCWin32($oLanConnection, $iFlag = 1, $iFullName = 1)
Local $objShell, $objFolder

$objShell = ObjCreate("Shell.Application")
If Not IsObj($objShell) Then Return SetError(2, 0, 0)

$objFolder = $objShell.NameSpace($NETWORK_FOLDER)
If Not IsObj($objFolder) Then Return SetError(3, 0, 0)

; Find the collection of the network connection name.
For $LanItem In $objFolder.Items
If StringLower($LanItem.Name) = StringLower($oLanConnection) Then
$oLanConnection = $LanItem
ExitLoop
EndIf
Next
; If no network connection name then return error.
If Not IsObj($oLanConnection) Then Return SetError(3, 0, 0)

$oEnableVerb = ""
$oDisableVerb = ""

; Find the state of the network connection.
For $Verb In $oLanConnection.Verbs
If $Verb.Name = $strEnableVerb Then
$oEnableVerb = $Verb
If Not IsObj($oEnableVerb) Then Return SetError(5, 0, 0)
$State = 0
EndIf
If $Verb.Name = $strDisableVerb Then
$oDisableVerb = $Verb
If Not IsObj($oDisableVerb) Then Return SetError(5, 0, 0)
$State = 1
EndIf
If $Verb.Name = $strRepairVerb Then
$oRepairVerb = $Verb
If Not IsObj($oRepairVerb) Then Return SetError(5, 0, 0)
EndIf
Next

; Return State
If $iFlag = 4 Then
Return $State
; Repair Net Connection
ElseIf $iFlag = 3 And $State = 1 Then
$oRepairVerb.DoIt
; Toggle Net Connection
ElseIf $iFlag = 2 Then
If $State = 1 Then
$oDisableVerb.DoIt
Else
$oEnableVerb.DoIt
EndIf
; Enable Net Connection
ElseIf $iFlag = 1 And $State = 0 Then
$oEnableVerb.DoIt
; Disable Net Connection
ElseIf $iFlag = 0 And $State = 1 Then
$oDisableVerb.DoIt
; Else there is an error
Else
Return SetError(5, 0, 0)
EndIf

; Loop to wait change state (for changing state of more than one connection).
$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 $iFlag = 3 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop
If $iFlag = 2 And $State = 1 And _NetCCWin32($oLanConnection, 4) = 0 Then ExitLoop
If $iFlag = 2 And $State = 0 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop
If $iFlag = 1 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop
If $iFlag = 0 And _NetCCWin32($oLanConnection, 4) = 0 Then ExitLoop
Sleep(100)
WEnd

; Set the return value of the function.
$Res = _NetCCWin32($oLanConnection, 4)
If $iFlag = 4 And $Res = 1 Then
Sleep(3000)
Return 1
ElseIf $iFlag = 1 And $Res = 0 Then
Return 0
ElseIf $iFlag = 0 And $Res = 1 Then
Return 0
Else
Return 1
EndIf
EndFunc ;==>_NetCCWin32

; Only for Vista/W2K8
Func _NetCCWin32New($oLanConnection, $iFlag = 1, $iFullName = 1)
Local $objWMIService, $colItems, $iModState = 0

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x30)
If Not IsObj($colItems) Then Return SetError(4, 0, 0)

; Change NIC state of $oLanConnection
; Check name of the connection
For $objItem In $colItems
If $objItem.NetConnectionID = $oLanConnection Then
; Check State.
If $iFlag = 4 Then
If $objItem.NetEnabled = True Then
Return 1
Else
Return 0
EndIf
Else
; Change the State of Net Connection.
; Disable Net Connection
If $iFlag = 0 And $objItem.NetEnabled = True Then
$objItem.Disable
; Enable Net Connection
ElseIf $iFlag = 1 And $objItem.NetEnabled = False Then
$objItem.Enable
; Toggle Net Connection
ElseIf $iFlag = 2 Then
If $objItem.NetEnabled = True Then
$objItem.Disable
$State = 1
Else
$objItem.Enable
$State = 0
EndIf
EndIf
; One change be done.
$iModState = 1
EndIf
ExitLoop
EndIf
Next
; If no connection then error.
If $iModState = 0 Then Return SetError(3, 0, 0)


; Loop to wait state change.
If $iFlag = 4 Then
While $iFlag <> _NetCCWin32New($oLanConnection, 4)
Sleep(250)
WEnd
Else
Sleep(1000)
If $iFlag = 2 Then
If $State = 0 And _NetCCWin32New($oLanConnection, 4) = 1 Then
Return 1
ElseIf $State = 1 And _NetCCWin32New($oLanConnection, 4) = 0 Then
Return 1
Else
Return 0
EndIf
ElseIf $iFlag = 1 And _NetCCWin32New($oLanConnection, 4) = 1 Then
Return 1
ElseIf $iFlag = 0 And _NetCCWin32New($oLanConnection, 4) = 0 Then
Return 1
Else
Return 0
EndIf
EndIf
EndFunc ;==>_NetCCWin32New

;====================================================================================================================
; Name           : _NetCCGetList()
; Description    : Get list of network connections.
; Syntax         : _NetCCGetList()
; Parameter(s)   : NA.
; Requirement(s) : Win32_NT OS
; Return value(s) : Array of network connections.
;                    On Failure - Return 0 and @error 3.
; @error 3 = Lan connection not find.
; Author         : Tlem <tlem at tuxolem dot net>
; http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=1092
; Note(s)        :
;
; ===================================================================================================================
Func _NetCCGetList()
; W2K/XP/W2K3
If StringInStr("WIN_2000,WIN_XP,WIN_2003", @OSVersion) Then
Local $objShell, $objFolder, $sTmp = ''

_NetCCLang()

$objShell = ObjCreate("Shell.Application")
If Not IsObj($objShell) Then Return SetError(4, 0, 0)
$objFolder = $objShell.NameSpace($NETWORK_FOLDER)
If Not IsObj($objFolder) Then Return SetError(3, 0, 0)

For $objItem In $objFolder.Items
For $oVerb In $objItem.Verbs
If StringInStr($oVerb.Name, $strStateVerb) Then $sTmp &= $objItem.Name & "|"
Next
Next
Else
; Vista/W2K8
Local $objWMIService, $colItems, $sTmp = ''
$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x30)
If Not IsObj($colItems) Then Return SetError(4, 0, 0)

For $objItem In $colItems
If $objItem.NetConnectionID Then
$sTmp &= $objItem.NetConnectionID & "|"
EndIf
Next
EndIf

If $sTmp = '' Then Return SetError(3, 0, 0)
$aNetList = StringSplit(StringTrimRight($sTmp, 1), "|")

Return $aNetList
EndFunc ;==>_NetCCGetList

Func _NetCCAll($iFlag, $Func = 0)
$iModState = 0
$aNetCon = _NetCCGetList()
For $i = 1 To $aNetCon[0]
If $Func = 1 Then
_NetCCWin32New($aNetCon[$i], $iFlag)
Else
_NetCCWin32($aNetCon[$i], $iFlag)
EndIf
$iModState = 1
Next
; If no connection find.
If $iModState = 0 Then
Return SetError(3, 0, 0)
Else
Return 1
EndIf
EndFunc ;==>_NetCCAll

Func _NetCCPartial($oLanConnection, $iFlag, $Func)
$iModState = 0
$aNetCon = _NetCCGetList()
For $i = 1 To $aNetCon[0]
If StringInStr(StringLower($aNetCon[$i]), StringLower($oLanConnection)) Then
If $Func = 1 Then
_NetCCWin32New($aNetCon[$i], $iFlag)
Else
_NetCCWin32($aNetCon[$i], $iFlag)
EndIf
$iModState = 1
EndIf
Next
; If no connection find.
If $iModState = 0 Then
Return SetError(3, 0, 0)
Else
Return 1
EndIf
EndFunc ;==>_NetCCPartial

Func _NetCCLang()
; 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"
$strRepairVerb = "Re&pair"
If @OSVersion = "WIN_2000" Then
$strStateVerb = "St&ate" ; Windows 2000 (Not sur if correct)
Else
$strStateVerb = "Stat&us"; Windows XP
EndIf

; Français (France)
Case StringInStr("040c,080c,0c0c,100c,140c,180c", @OSLang)
$strEnableVerb = "&Activer"
$strDisableVerb = "&Désactiver"
$strRepairVerb = "&Réparer"
If @OSVersion = "WIN_2000" Then
$strStateVerb = "É&tat" ; Windows 2000
Else
$strStateVerb = "Sta&tut"; Windows XP
EndIf

; Add here the correct Verbs for your Operating System Language
EndSelect
EndFunc ;==>_NetCCLang

The error I'm getting is:

C:DnloadNetCCExample.au3(15,49) : WARNING: $Res: possibly used before declaration.

MsgBox (0, "Information ", "$Res " and $Res and

Is it ok if I change this:

Global $strEnableVerb, $strDisableVerb, $strRepairVerb, $strStateVerb, $strFolderName

To this:

Global $strEnableVerb, $strDisableVerb, $strRepairVerb, $strStateVerb, $strFolderName, $Res

Thanks,

Docfxit

Edited by Docfxit

Share this post


Link to post
Share on other sites
Docfxit

Sorry Docfxit, I mis-read your post.

No need to be sorry. I'm happy to receive any and all attempts to fix it.

Thanks,

Docfxit

Share this post


Link to post
Share on other sites
Docfxit

I realize the OP's main question was how to get the script working, but I'm curious - if enabling/disabling the interface is all you want to do, why not just do a netsh call? I use this on Win7 pretty regularly.

#RequireAdmin
ShellExecuteWait("netsh.exe", 'int set interface "Local Area Connection" disable', "", "", @SW_HIDE)

Thanks for the reply...

This does work for this one case I'm working on now. I was hoping to find a solution that would work in all cases.

I can use this for now and re-visit this when It doesn't work.

Thanks a bunch,

Docfxit

Share this post


Link to post
Share on other sites
JLogan3o13

JLogan3o13, I'm not familiar with netsh, do you know if there is a way to delete a local area connection?

Are you talking about ghost interfaces that are no longer active, or the main Local Area Connection? If you mean the former, I believe a netsh int ipv4 (or ipv6) reset all will do the trick. For actually deleting the Local Area Connection I am unaware of a way to do this.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
JLogan3o13

Thanks for the reply...

This does work for this one case I'm working on now. I was hoping to find a solution that would work in all cases.

I can use this for now and re-visit this when It doesn't work.

Thanks a bunch,

Docfxit

You are correct, this will not work in all cases, as the Netsh disable/enable does not work in XP. For XP, you have to script the Network Connections control panel applet. Below is an old vbscript I had that will do it, though I've never taken the time to convert to AutoIt. You should be able to do so pretty easily.

Const ssfCONTROLS = 3

sConnectionName = "Local Area Connection"

sEnableVerb = "En&able"
sDisableVerb = "Disa&ble"

set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)
set oNetConnections = nothing

for each folderitem in oControlPanel.items
     if folderitem.name = "Network Connections" then
     set oNetConnections = folderitem.getfolder: exit for
     end if
next

if oNetConnections is nothing then
     msgbox "Couldn't find 'Network Connections' folder"
     wscript.quit
end if

set oLanConnection = nothing

for each folderitem in oNetConnections.items
     if lcase(folderitem.name) = lcase(sConnectionName) then
         set oLanConnection = folderitem: exit for
     end if
next

if oLanConnection is nothing then
     msgbox "Couldn't find '" & sConnectionName & "' item"
     wscript.quit
end if

bEnabled = true
set oEnableVerb = nothing
set oDisableVerb = nothing

for each verb in oLanConnection.verbs
     if verb.name = sEnableVerb then
         set oEnableVerb = verb
         bEnabled = false
     end if
     if verb.name = sDisableVerb then
         set oDisableVerb = verb
     end if
next

if bEnabled then
     oDisableVerb.DoIt()
else
     oEnableVerb.DoIt
end if

wscript.sleep 500

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
JohnOne

Are you talking about ghost interfaces that are no longer active, or the main Local Area Connection? If you mean the former, I believe a netsh int ipv4 (or ipv6) reset all will do the trick. For actually deleting the Local Area Connection I am unaware of a way to do this.

Cheers, I did mean 'ghosts', I'll give it a pop.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
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
Sign in to follow this  

×