Jump to content

Tray NetworkManager AutoIT that needs optimization


Recommended Posts

Hi,

I am trying to make a network tool that would allow to enable/disable the network interfaces found on a system with a tray icon.

There are two things bugging me with the following code :

1- It needs more flexibility about the amount of interfaces that can be monitored.  ( Switch/case limitations forced me to do it this way... maybe there is something more elegant )

2- CPU hogging ( I want the Network Manager tool to be refreshed and display the correct information even if the network interface is not enabled/disabled throught this tool )

I tried using TraySetOnEvent($TRAY_EVENT_MOUSEOVER, "Function") so when the user would mouseOver the tray icon it would automatically trigger a refresh of the network cards status.  I finally removed this since i wasn't able to make it work properly

So  if anyone have any idea on how to make this script more efficient :)

Note: Comments in french and you can't run it on your computer right away since it uses the RunAS function... so you will need to put your own credentials...

#requireadmin
#NoTrayIcon

#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3>
Opt("TrayMenuMode", 3) ;


$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$query = ""

Local $active_netword_cards[0][3]
;Local $active_netword_cards[0][2]
Local $Reseau[10]
Local $i = 0


;Obtenir la liste d'interfaces réseau disponibles

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
         if $objItem.NetConnectionID <> Null Then
            _arrayAdd($active_netword_cards, $objItem.NetConnectionID)
            $active_netword_cards[$i][1] = $objItem.NetConnectionStatus
            $active_netword_cards[$i][2] = $objItem.ProductName
            If $objItem.NetConnectionStatus = 2 Then
               $Reseau[$i] = TrayCreateItem("Désactiver " & $active_netword_cards[$i][0] & " ("&$active_netword_cards[$i][2] &")")
            ElseIf $objItem.NetConnectionStatus = 0 Then
               $Reseau[$i] = TrayCreateItem("Activer " & $active_netword_cards[$i][0] & " ("&$active_netword_cards[$i][2] &")")
            ElseIf $objItem.NetConnectionStatus = 7 Then
               $Reseau[$i] = TrayCreateItem("Désactiver " & $active_netword_cards[$i][0] & " ("&$active_netword_cards[$i][2] &")")
            EndIf
            $i = $i+1
         EndIf
      Next
Endif
Local $idExit = TrayCreateItem("Quitter")
TraySetState($TRAY_ICONSTATE_SHOW) ; Montrer licone dans la barre des taches
;*********************************************************************************************************************************************************
;*************************************************************************EN TEST*************************************************************************
;_ArrayDisplay($Reseau,"Tableau","5","","",4)
;_ArrayDisplay($active_netword_cards, "2D display transposed", Default, 1)

;*********************************************************************************************************************************************************
ReseauManager()
Func ReseauManager()
    ; 1 cartes réseaux détectées :
    While 1
         If UBound($active_netword_cards) = 1 Then
            Switch TrayGetMsg()
               Case $Reseau[0]
                  If TrayItemGetText($Reseau[0])= "Désactiver " &  $active_netword_cards[0][0]  & " ("&$active_netword_cards[0][2] &")" Then
                  TrayItemSetText($Reseau[0],"Activer " & $active_netword_cards[0][0]  & " ("&$active_netword_cards[0][2] &")")
                  RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[0][0]&'" admin=DISABLED',"",@SW_HIDE   )
               Else
                  TrayItemSetText($Reseau[0],"Désactiver " & $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")")
                  RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[0][0]&'" admin=ENABLED',"",@SW_HIDE   )
               EndIf
               Case $idExit ; Quitter le programme
                   $QuestionFermeture = MsgBox($MB_OKCANCEL,"Fermeture","Êtes vous vraiment certain de vouloir quitter le gestionnaire réseau?")
                  If $QuestionFermeture = 1 Then
                     Exit
                  EndIf
             EndSwitch
          EndIf
         ; 2 cartes réseaux détectées :
         If UBound($active_netword_cards) = 2 Then
            Switch TrayGetMsg()
               Case $Reseau[0]
                  If TrayItemGetText($Reseau[0])= "Désactiver " &  $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")" Then
                     TrayItemSetText($Reseau[0],"Activer " & $active_netword_cards[0][0]  & " ("&$active_netword_cards[0][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[0][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[0],"Désactiver " & $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[0][0]&'" admin=ENABLED',"",@SW_HIDE  )
                  EndIf
               Case $Reseau[1]
                  If TrayItemGetText($Reseau[1])= "Désactiver " &  $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")" Then
                     TrayItemSetText($Reseau[1],"Activer " & $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[1][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[1],"Désactiver " & $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[1][0]&'" admin=ENABLED',"",@SW_HIDE  )
                  EndIf
               Case $idExit ; Quitter le programme
                   $QuestionFermeture = MsgBox($MB_OKCANCEL,"Fermeture","Êtes vous vraiment certain de vouloir quitter le gestionnaire réseau?")
                  If $QuestionFermeture = 1 Then
                     Exit
                  EndIf
            EndSwitch
         EndIf
         ; 3 cartes réseaux détectées :
         If UBound($active_netword_cards) = 3 Then
            Switch TrayGetMsg()
               Case $Reseau[0]
                  If TrayItemGetText($Reseau[0])= "Désactiver " &  $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")" Then
                     TrayItemSetText($Reseau[0],"Activer " & $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[0][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[0],"Désactiver " & $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[0][0]&'" admin=ENABLED',"",@SW_HIDE  )
                  EndIf
               Case $Reseau[1]
                  If TrayItemGetText($Reseau[1])= "Désactiver " &  $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")" Then
                     TrayItemSetText($Reseau[1],"Activer " & $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[1][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[1],"Désactiver " & $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[1][0]&'" admin=ENABLED',"",@SW_HIDE  )
                  EndIf
               Case $Reseau[2]
                  If TrayItemGetText($Reseau[2])= "Désactiver " &  $active_netword_cards[2][0] & " ("&$active_netword_cards[2][2] &")" Then
                     TrayItemSetText($Reseau[2],"Activer " & $active_netword_cards[2][0] & " ("&$active_netword_cards[2][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[2][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[2],"Désactiver " & $active_netword_cards[2][0] & " ("&$active_netword_cards[2][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[2][0]&'" admin=ENABLED',"",@SW_HIDE  )
                  EndIf
                Case $idExit ; Quitter le programme
                   $QuestionFermeture = MsgBox($MB_OKCANCEL,"Fermeture","Êtes vous vraiment certain de vouloir quitter le gestionnaire réseau?")
                  If $QuestionFermeture = 1 Then
                     Exit
                  EndIf
            EndSwitch
         EndIf
      ; 4 cartes réseaux détectées :
         If UBound($active_netword_cards) = 4 Then
            Switch TrayGetMsg()
               Case $Reseau[0]
                  If TrayItemGetText($Reseau[0])= "Désactiver " &  $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")" Then
                     TrayItemSetText($Reseau[0],"Activer " & $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[0][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[0],"Désactiver " & $active_netword_cards[0][0] & " ("&$active_netword_cards[0][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[0][0]&'" admin=ENABLED',"",@SW_HIDE  )
                  EndIf

               Case $Reseau[1]
                  If TrayItemGetText($Reseau[1])= "Désactiver " &  $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")" Then
                     TrayItemSetText($Reseau[1],"Activer " & $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[1][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[1],"Désactiver " & $active_netword_cards[1][0] & " ("&$active_netword_cards[1][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[1][0]&'" admin=ENABLED',"",@SW_HIDE  )
                  EndIf

               Case $Reseau[2]
                  If TrayItemGetText($Reseau[2])= "Désactiver " &  $active_netword_cards[2][0] & " ("&$active_netword_cards[2][2] &")" Then
                     TrayItemSetText($Reseau[2],"Activer " & $active_netword_cards[2][0] & " ("&$active_netword_cards[2][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[2][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[2],"Désactiver " & $active_netword_cards[2][0] & " ("&$active_netword_cards[2][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[2][0]&'" admin=ENABLED',"",@SW_HIDE  )
                  EndIf
               Case $Reseau[3]
                  If TrayItemGetText($Reseau[3])= "Désactiver " &  $active_netword_cards[3][0] & " ("&$active_netword_cards[3][2] &")" Then
                     TrayItemSetText($Reseau[3],"Activer " & $active_netword_cards[3][0] & " ("&$active_netword_cards[3][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[3][0]&'" admin=DISABLED',"",@SW_HIDE  )
                  Else
                     TrayItemSetText($Reseau[3],"Désactiver " & $active_netword_cards[3][0] & " ("&$active_netword_cards[3][2] &")")
                     RunAs("Administrateur","","password","",@SystemDir & '\netsh interface set interface name="'& $active_netword_cards[3][0]&'" admin=ENABLED',"","",@SW_HIDE  )
                  EndIf
               Case $idExit ; Quitter le programme
                  $QuestionFermeture = MsgBox($MB_OKCANCEL,"Fermeture","Êtes vous vraiment certain de vouloir quitter le gestionnaire réseau?")
                  If $QuestionFermeture = 1 Then
                     Exit
                  EndIf
            EndSwitch
         EndIf
         If UBound($active_netword_cards) > 4 Then
            MsgBox("","Information!","Impossible d'utiliser ce logiciel (Trop d'interfaces réseaux)")
         EndIf
         ; Cette section sert à revalider le status des cartes réseaux
         ;sleep(1000)
         $i = 0
         $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
         $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

         If IsObj($colItems) then
            For $objItem In $colItems
               if $objItem.NetConnectionID <> Null Then
                  ;_arrayAdd($active_netword_cards, $objItem.NetConnectionID)
                  $active_netword_cards[$i][0] = $objItem.NetConnectionID
                  $active_netword_cards[$i][1] = $objItem.NetConnectionStatus
                  $active_netword_cards[$i][2] = $objItem.ProductName
                  If $objItem.NetConnectionStatus = 2 Then
                     TrayItemSetText($Reseau[$i],"Désactiver " & $active_netword_cards[$i][0] & " ("&$active_netword_cards[$i][2] &")")
                  ElseIf $objItem.NetConnectionStatus = 0 Then
                     TrayItemSetText($Reseau[$i],"Activer " & $active_netword_cards[$i][0] & " ("&$active_netword_cards[$i][2] &")")
                  ElseIf $objItem.NetConnectionStatus = 7 Then
                     TrayItemSetText($Reseau[$i],"Désactiver " & $active_netword_cards[$i][0] & " ("&$active_netword_cards[$i][2] &")")
                  EndIf
               $i = $i+1
               EndIf
      Next
Endif
   Wend
EndFunc
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...