Jump to content



Photo

Enable / Disable Network Adapters


  • Please log in to reply
7 replies to this topic

#1 SandelPerieanu

SandelPerieanu

    Prodigy

  • Active Members
  • PipPipPip
  • 179 posts

Posted 11 December 2006 - 11:26 AM

AutoIt         
function [b]DisableNetAdapter[/b]: boolean; var   hdi:  HDEVINFO; begin   hdi := SetupDiGetClassDevs(@GUID_DEVCLASS_NET, nil, 0, DIGCF_PRESENT);   if cardinal(hdi) = INVALID_HANDLE_VALUE then   begin     Result := False;   end   else   begin     Result := StateChange(DICS_DISABLE, 0, hdi);     SetupDiDestroyDeviceInfoList(hdi);   end; end; function [b]EnableNetAdapter[/b]: boolean; var   hdi:  HDEVINFO; begin   hdi := SetupDiGetClassDevs(@GUID_DEVCLASS_NET, nil, 0, DIGCF_PRESENT);   if cardinal(hdi) = INVALID_HANDLE_VALUE then   begin     Result := False;   end   else   begin     Result := StateChange(DICS_ENABLE, 0, hdi);     SetupDiDestroyDeviceInfoList(hdi);   end; end;

I don't know how i convert this!





#2 Xenobiologist

Xenobiologist

    Xx Code~Mega xX

  • MVPs
  • 4,728 posts

Posted 11 December 2006 - 11:36 AM

Hi,

maybe just use Devcon.exe.

So long,

Mega
Scripts & functions Organize Includes Let Scite organize the include files *newYahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication)_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

#3 Danny35d

Danny35d

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 783 posts

Posted 11 December 2006 - 12:17 PM

I don't know Delphi, but this code will enable or disable network adapters.
Plain Text         
#include <GUIConstants.au3> Dim $i, $base, $key, $name ; == GUI generated with Koda == $Form1 = GUICreate("Nic", 176, 89, -1, -1, -1, $WS_EX_TOOLWINDOW) GUICtrlCreateLabel("Pick Network Connection", 8, 8, 157, 17, $SS_CENTER) $Combo1 = GUICtrlCreateCombo("", 8, 24, 161, 21) $Button1 = GUICtrlCreateButton("Enable", 8, 56, 75, 25) $Button2 = GUICtrlCreateButton("Disable", 96, 56, 75, 25) ;Get Network Connection Names $base = "HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}" While 1     $i += 1     $key = RegEnumKey($base, $i)     If @error <> 0 Then ExitLoop     $name = RegRead($base & "\" & $key & "\Connection", "Name")     If StringLeft($key, 1) = "{" Then GUICtrlSetData($Combo1, $name & "|") WEnd GUISetState(@SW_SHOW) While 1     $msg = GuiGetMsg()     Switch $msg     Case $GUI_EVENT_CLOSE         ExitLoop     Case $Button1         If GUICtrlRead($Combo1) <> '' Then NicToggle(1, GUICtrlRead($Combo1))     Case $Button2         If GUICtrlRead($Combo1) <> '' Then NicToggle(0, GUICtrlRead($Combo1))     Case Else        ;;;;;;;     EndSwitch WEnd Exit Func NicToggle($Toggle, $sConnectionName = "Local Area Connection", $sNetworkFolder = "Network Connections")        $ssfCONTROLS = 3     $sEnableVerb = "En&able"     $sDisableVerb = "Disa&ble"     $shellApp = ObjCreate("shell.application")     $oControlPanel = $shellApp.Namespace ($ssfCONTROLS)     $oNetConnections = "nothing"         For $folderitem In $oControlPanel.items         If $folderitem.name = $sNetworkFolder Then             $oNetConnections = $folderitem.getfolder             ExitLoop         EndIf     Next     If $oNetConnections = "nothing" Then         MsgBox(48, "Error", "Couldn't find " & $sNetworkFolder & " folder")         Exit     EndIf         $oLanConnection = "nothing"     For $folderitem In $oNetConnections.items         If StringLower($folderitem.name) = StringLower($sConnectionName) Then             $oLanConnection = $folderitem             ExitLoop         EndIf     Next     If $oLanConnection = "nothing" Then         MsgBox(48, "Error", "Couldn't find '" & $sConnectionName & "' item")         Exit     EndIf         $bEnabled = True     $oEnableVerb = "nothing"     $oDisableVerb = "nothing"     $s = "Verbs: " & @CRLF     For $verb In $oLanConnection.verbs         $s = $s & @CRLF & $verb.name            ;enables         If $verb.name = $sEnableVerb And $Toggle = 1 Then             $oEnableVerb = $verb             $oEnableVerb.DoIt             ExitLoop                    ;disables         ElseIf $verb.name = $sDisableVerb And $Toggle = 0 Then             $oDisableVerb = $verb             $oDisableVerb.DoIt             ExitLoop         Else             MsgBox(48, "Error", "Tried to disable when already disabled" & @CRLF & "or enable when already enabled")             Exit         EndIf     Next     Sleep(1000) EndFunc ;==>NicToggle

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

#4 Wus

Wus

    Indentured Servant

  • Active Members
  • PipPipPipPipPipPip
  • 513 posts

Posted 11 December 2006 - 02:06 PM

Yes that code works for enabling or disabling...

http://www.autoitscript.com/forum/index.php?showtopic=21229

Generally its better to link it btw...
Posted Image

#5 SandelPerieanu

SandelPerieanu

    Prodigy

  • Active Members
  • PipPipPip
  • 179 posts

Posted 12 December 2006 - 01:18 PM

i make this with DEVCON.EXE

Func NetAdapterStatus($netconnectionid, $status)  Local $objwmiservice = ObjGet('winmgmts:\\.\root\CIMV2')  Local $colitems = $objwmiservice.execquery _    ("SELECT * FROM Win32_NetworkAdapter Where NetConnectionID='" & $netconnectionid & "'", 'WQL', 0x30)  For $objitem In $colitems   Select    Case $status = 'Disable'     RunWait('DevCon.exe disable ' & StringMid(String($objitem.pnpdeviceid), 1, _       StringInStr(String($objitem.pnpdeviceid), '&') - 1), @ScriptDir, @SW_HIDE)    Case $status = 'Enable'     RunWait('DevCon.exe enable ' & StringMid(String($objitem.pnpdeviceid), 1, _       StringInStr(String($objitem.pnpdeviceid), '&') - 1), @ScriptDir, @SW_HIDE)   EndSelect  Next  $colitems = ''  $objitem = '' EndFunc   ;==>NetAdapterStatus


Ex: $netconnectionid="Local Area Connection"
$status = "Disable" ; Or $status = "Enable"

#6 Joon

Joon

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 329 posts

Posted 12 December 2006 - 03:57 PM

Another way, this way you don't need to install DevCon.exe

Func NetAdapterStatus($netconnectionid, $status)     If StringLower($status) = "enable" Then         RunWait(@ComSpec & ' /c Title Enabling "' & $netconnectionid & '"...|netsh interface set interface name="' & $netconnectionid & '" admin=ENABLED')     ElseIf StringLower($status) = "disable" Then         RunWait(@ComSpec & ' /c Title Disabling "' & $netconnectionid & '"...|netsh interface set interface name="' & $netconnectionid & '" admin=DISABLED')     Else             EndIf EndFunc  ;==>NetAdapterStatus


#7 SandelPerieanu

SandelPerieanu

    Prodigy

  • Active Members
  • PipPipPip
  • 179 posts

Posted 12 December 2006 - 05:39 PM

Another way, this way you don't need to install DevCon.exe

Func NetAdapterStatus($netconnectionid, $status)     If StringLower($status) = "enable" Then         RunWait(@ComSpec & ' /c Title Enabling "' & $netconnectionid & '"...|netsh interface set interface name="' & $netconnectionid & '" admin=ENABLED')     ElseIf StringLower($status) = "disable" Then         RunWait(@ComSpec & ' /c Title Disabling "' & $netconnectionid & '"...|netsh interface set interface name="' & $netconnectionid & '" admin=DISABLED')     Else             EndIf EndFunc ;==>NetAdapterStatus

I tried this on the Win XP and don't work, it's work only Win 2K3 R2

#8 Joon

Joon

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 329 posts

Posted 12 December 2006 - 06:15 PM

I tried this on the Win XP and don't work, it's work only Win 2K3 R2

Can you try just running following command from Command Prompt?
netsh interface set interface name="Local Area Connection" admin=DISABLED


I tried - didn't work on XP... weird. I'm doing some research now.

Edited by Joon, 12 December 2006 - 06:23 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users