Jump to content

Enable / Disable Network Adapters


Recommended Posts

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!

Link to comment
Share on other sites

Hi,

maybe just use Devcon.exe.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee 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

Link to comment
Share on other sites

I don't know Delphi, but this code will enable or disable network adapters.

#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
Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
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...