Jump to content

QuickNIC


spudw2k
 Share

Recommended Posts

A co-worker of mine was working remotely yesterday and asked if I knew of a way to easy and quickly enable/disable his wireless card rather than the tediousness of opening Network Connections, right-click and (En/Dis)able. I found a vbscript that uses a Shell object to do essentially the same thing, but I took the liberty of improving it. I converted the script to AutoIt to increase functionality and add a gui.

I designed it to run the in systemtray for ease. Take a look and hopefully someone else will find it useful.

---update---

Code has been updated. Download version 2 below on my second post. The original code is left available for reference.

#include <Array.au3>
Opt("TrayMenuMode",1)
TraySetIcon("Shell32.dll",-150)
TraySetToolTip("QuickNIC")

Func GetNetworkConnections()
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    
    If not IsObj($objWMIService) Then return 0
    
    $colItems = $objWMIService.ExecQuery("SELECT NetConnectionID FROM Win32_NetworkAdapter")
    
    $strData = ""
    For $objItem in $colItems
        If $objItem.NetConnectionID <> "" Then $strData = $strData & $objItem.NetConnectionID & "|"
    Next
    
    If StringLen($strData) > 2 Then
        $strData = StringLeft($strData,StringLen($strData)-1)
    Else
        msgbox(0,"","No Networks Found.")
        return 0
    EndIf
    
    Return $strData
EndFunc

Func ToggleNetworkInterface($strConn)
    $objShell = ObjCreate("Shell.Application")
    
    $strNetConn = "Network Connections"
    
    $strEnable = "En&able"
    $strDisable = "Disa&ble"

    $objCP = $objShell.Namespace(3)
    
    $colNetwork = ""
    For $clsConn In $objCP.Items
        If $clsConn.Name = $strNetConn Then $colNetwork = $clsConn.GetFolder
    Next
    
    If $colNetwork = "" Then
        msgbox(0,"","Failed to Find Network Connections")
        Exit
    EndIf
    
    $clsLANConn = ""
    For $clsConn In $colNetwork.Items
        If StringLower($clsConn.Name) = StringLower($strConn) Then $clsLANConn = $clsConn
    Next
        
    If $clsLANConn = "" Then
        Msgbox(0,"","Cannot find Network by the name of: " & $strConn)
        Exit
    EndIf
    
    For $clsVerb in $clsLANConn.verbs
        If $clsVerb.name = $strDisable Then 
            $clsVerb.DoIt
            sleep(100)
            Return 0
        EndIf
        If $clsVerb.name = $strEnable Then 
            $clsVerb.DoIt
            sleep(100)
            Return 1
        EndIf
    Next
EndFunc

Func LoadInterface()
    $arrData = StringSplit(GetNetworkConnections(),"|")
    _ArrayDelete($arrData,0)
    _ArraySort($arrData)

    Dim $NetConn[1]
    
    For $i = 0 to UBound($arrData) - 1
        $NetConn[$i] = TrayCreateItem($arrData[$i])
        ReDim $NetConn[$i + 2]
    Next
    
    $separator = TrayCreateItem("")
    $trayabout = TrayCreateItem("About")
    $trayexit = TrayCreateItem("Exit")
    
    While 1
        $msg = TrayGetMsg()
            
        If $msg > 0 and $msg <> $trayabout and $msg <> $trayexit Then 
            ToggleNetworkInterface(TrayItemGetText($msg))
        EndIf
        
        If $msg = $trayabout Then 
            SplashTextOn("About...","QuickNIC is a tool that provides a quick and easy method to toggle (enable/disable) your Network Connections." & @CRLF & @CRLF & "version 0.1b",300,120)
            sleep(3500)
            SplashOff()
        EndIf
        
        If $msg = $trayexit Then ExitLoop
    WEnd
EndFunc

LoadInterface()
Edited by spudw2k
Link to comment
Share on other sites

  • 2 weeks later...

I should be able to do that fairly easily with checkboxes, but I'm just trying to find the best way to minimize my code. I'll post the updated version when it's working best.

---update---

Ok, here is the updated version with status of each NIC. Enjoy.

---update---

Found a bug. Remove Wizards. Fixed and reposted v2.

---update---

Checked code against version 3.2.10.0

Fixed trayicon

edit:

WARNING! this script does not work with Windows 7 (at least not 64-bit but I doubt x86 works either).

I may update it eventually.

QuickNICV2.au3

Edited by spudw2k
Link to comment
Share on other sites

@spudw2k

Nice application !!

But your latest version is bringing up lots of errors of non decl. vars.

Regards

ptrex

What version of AutoItv3 are you using? It works fine for me using v3.2.4.9

If you give me the error and line# I'd take alook.

Link to comment
Share on other sites

  • 3 months later...

Just verified. Code is working with version 3.2.10.0. Compiled Code uploaded.

It does indeed. Very useful - thanks spudw2k.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Nice.

Here is what I have been using for a while. VBS, but same difference. This is for XP, I have a different one for 2k, small differences.

Sul.

'~ Toggle a SPECIFIED NIC on or off
Option Explicit
Dim objShell
Dim objCP, objEnable, objDisable, colNetwork
Dim clsConn, clsLANConn, clsVerb
Dim strNetConn, strConn, strEnable, strDisable
Dim bEnabled, bDisabled

strNetConn = "Network Connections"
strConn = "name of your network adapter here"

strEnable = "En&able"
strDisable = "Disa&ble"

Set objShell = CreateObject("Shell.Application")
Set objCP = objShell.Namespace(3) 'Control Panel

Set colNetwork = Nothing
For Each clsConn in objCP.Items
    If clsConn.Name = strNetConn Then
        Set colNetwork = clsConn.getfolder
        Exit For
    End If
Next

If colNetwork is Nothing Then
    WScript.Echo "Network folder not found"
    WScript.Quit
End If

Set clsLANConn = Nothing
For Each clsConn in colNetwork.Items
'~ ‘In case the LAN is named "connection 2", etc.

    If Instr(LCase(clsConn.name),LCase(strConn)) Then
        Set clsLANConn = clsConn
        Exit For
    End If
Next

If clsLANConn is Nothing Then
    WScript.Echo "Network Connection not found"
    WScript.Quit
End If

bEnabled = True
Set objEnable = Nothing
Set objDisable = Nothing
For Each clsVerb in clsLANConn.verbs
    If clsVerb.name = strEnable Then 
        Set objEnable = clsVerb 
        bEnabled = False
    End If
    If clsVerb.name = strDisable Then 
        Set objDisable = clsVerb 
    End If
Next

If bEnabled Then
    objDisable.DoIt
Else
    objEnable.DoIt
End If

'~ Give the connection time to stop/start
WScript.Sleep 1000 
WScript.Quit
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...