#include #include #include #Include #include #include #Include #RequireAdmin Opt("GuiOnEventMode", 1) Opt('MustDeclareVars', 1) Global Const $SS_RIGHT = 2 Global Const $SS_LEFT = 0 ;~ Global $WinShell= ObjCreate("shell.application") Global $Configs[50][7] Global $gui, $ConfigList, $ProfilName, $CARD, $IPWS, $MASK, $GATE, $DNS1, $DNS2, $ToolTip, $IsDHCP Dim $Apply, $GetCurrent, $DeleteProfil, $SaveProfil, $About Global $NetCardDescriptors[10][3] ;0 Number ;1 Name ;2 UID Descriptor GUIRegisterMsg($WM_COMMAND, "WM_COMMAND") _CreateGUI() _GetInfoReseau() _ImportProfils() While 1 Sleep(60000) WEnd ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _About() MsgBox(64 + 262144, "A Propos", "IP_Profil_Manager by Arek", 0, $gui) EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _Apply() Local $i, $j, $pid, $line Local $NetWorkName, $address If _GUICtrlComboBox_GetEditText($CARD) = "" Then MsgBox(48, "Carte réseau", "Selectionnez une carte réseau!") Return EndIf ;recherche de la carte reseau choisie for $i = 1 to $NetCardDescriptors[0][0] If _GUICtrlComboBox_GetEditText($CARD) = $NetCardDescriptors[$i][1] Then ExitLoop EndIf Next ;récupération du nom de connexion $NetWorkName = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" & $NetCardDescriptors[$i][2] & "\Connection","Name") If GUICtrlRead($ProfilName) = "DHCP" Then ProgressOn ( "Configuration IP", "Application des nouveaux paramètres...", "Configuration de l'adresse IP...") RunWait ('netsh interface ip set address name="' & $NetWorkName & '" source=dhcp', "", @SW_HIDE ) ProgressSet (50, "Configuration du DNS...") RunWait ('netsh interface ip set dns name="' & $NetWorkName & '" source=dhcp', "", @SW_HIDE ) ProgressSet (100, "Terminé") sleep(750) ProgressOff ( ) $IsDHCP = True Else ProgressOn ( "Configuration IP", "Application des nouveaux paramètres...", "Configuration de l'adresse IP...") ;~ $address = 'netsh interface ip set address name="' & $NetWorkName & '" source=static addr=' & _GUICtrlIpAddress_Get($IPWS) & ' mask=' & _GUICtrlIpAddress_Get($MASK) & ' gateway=' & _GUICtrlIpAddress_Get($GATE) & ' gwmetric=0' ;~ InputBox("test", "set address", $address) RunWait ('netsh interface ip set address name="' & $NetWorkName & '" source=static addr=' & _GUICtrlIpAddress_Get($IPWS) & ' mask=' & _GUICtrlIpAddress_Get($MASK) & ' gateway=' & _GUICtrlIpAddress_Get($GATE) & ' gwmetric=0', "", @SW_HIDE ) ProgressSet (33, "Configuration du DNS...") RunWait ('netsh interface ip set dns name="' & $NetWorkName & '" source=static addr=' & _GUICtrlIpAddress_Get($DNS1) & ' register=PRIMARY', "", @SW_HIDE) ProgressSet (66, "Configuration du DNS...") RunWait ('netsh interface ip add dns name="' & $NetWorkName & '" addr=' & _GUICtrlIpAddress_Get($DNS1), "", @SW_HIDE) ProgressSet (100, "Terminé") sleep(750) ProgressOff ( ) $IsDHCP = False EndIf MsgBox(48, "Changement", "La configuration a été appliquée", 2) EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _CreateGUI() $gui = GUICreate("IP_Profil_Manager", 525, 180) GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit") GUICtrlCreateGroup("Profils", 05, 05, 200, 170) $ConfigList = GUICtrlCreateList("", 15, 20, 180, 145, $LBS_NOINTEGRALHEIGHT) GUICtrlSetOnEvent($ConfigList, "_Fill") GUICtrlCreateGroup("", -99, -99, 1, 1) GUICtrlCreateGroup("Configuration" , 210, 05, 225, 170) GUICtrlCreateLabel("Nom de profil: " , 220, 22, 80, 20, $SS_RIGHT) GUICtrlCreateLabel("Carte : " , 220, 47, 50, 20, $SS_LEFT) GUICtrlCreateLabel("IP : " , 220, 72, 80, 20, $SS_RIGHT) GUICtrlCreateLabel("Masque : " , 220, 97, 80, 20, $SS_RIGHT) GUICtrlCreateLabel("Passerelle : " , 220, 122, 80, 20, $SS_RIGHT) GUICtrlCreateLabel("DNS : " , 220, 147, 80, 20, $SS_RIGHT) ;~ GUICtrlCreateLabel("DNS 2 : " , 220, 172, 80, 20, $SS_RIGHT) $ProfilName = GUICtrlCreateInput("Nom" , 300, 20, 125, 20) $CARD = GUICtrlCreateCombo("" , 250, 45, 175, 20) $IPWS = _GUICtrlIpAddress_Create ($gui , 300, 70, 125, 20) $MASK = _GUICtrlIpAddress_Create ($gui , 300, 95, 125, 20) $GATE = _GUICtrlIpAddress_Create ($gui , 300, 120, 125, 20) $DNS1 = _GUICtrlIpAddress_Create ($gui , 300, 145, 125, 20) ;~ $DNS2 = _GUICtrlIpAddress_Create ($gui , 300, 170, 125, 20) GUICtrlCreateGroup("", -99, -99, 1, 1) GUICtrlCreateGroup("Actions", 440, 05, 80, 170) $Apply = GUICtrlCreateButton("Appliquer" , 445, 20, 70, 20) $GetCurrent = GUICtrlCreateButton("Lire" , 445, 45, 70, 20) $SaveProfil = GUICtrlCreateButton("Sauvegarder" , 445, 70, 70, 20) $DeleteProfil = GUICtrlCreateButton("Supprimer" , 445, 95, 70, 20) $About = GUICtrlCreateButton("A Propos" , 445, 145, 70, 20) ;~ GUICtrlSetState ($GetCurrent, $GUI_DISABLE) GUICtrlSetOnEvent($Apply, "_Apply") GUICtrlSetOnEvent($GetCurrent, "_GetCurrent") GUICtrlSetOnEvent($SaveProfil, "_SaveProfil") GUICtrlSetOnEvent($DeleteProfil, "_DeleteProfil") GUICtrlSetOnEvent($About, "_About") GUICtrlCreateGroup("", -99, -99, 1, 1) GUISetState(@SW_SHOW) EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _CreateINI() Local $result If MsgBox(52, "Erreur", "Le fichier de configuration est absent ou corrompu." & @CRLF & "Voulez vous en créer un autre ?" & @CRLF & "(S'il existe, l'ancien fichier sera écrasé)") = 6 Then If FileExists("profils.dat") Then FileDelete("profils.dat") EndIf $Result = IniWrite ( "profils.dat", "Profils", "DHCP", ";000.000.000.000;000.000.000.000;000.000.000.000;000.000.000.000") If $Result = 1 Then MsgBox(64, "Création Fichier de Profil", "Le nouveau fichier de configuration a corectement été créé."); & @CRLF & "Merci de redémarrer le programme pour prendre en compte les modifications") Else MsgBox(32, "Création Fichier de Profil", "Une erreur est survenue lors de la création." & @CRLF & "Le programme ne peut pas continuer." & @CRLF & "Nous vous prions de bien vouloir nous excuser pour ce désagrément.") Exit EndIf Else MsgBox(32, "Fichier de Profil absent ou coromppu", "Le programme ne peut pas continuer sans fichier de profil." & @CRLF & "Nous vous prions de bien vouloir nous excuser pour ce désagrément.") Exit EndIf EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _DeleteProfil() If MsgBox(36, "Confirmation", "Le profil - " & GUICtrlRead($ProfilName) & " - sera définitivement supprimé" & @CRLF & "Êtes-vous sur ?") = 6 Then IniDelete ( "profils.dat", "Profils", GUICtrlRead($ProfilName)) _ImportProfils() EndIf EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _Fill() Local $Index ;~ $CARD, $IPWS, $MASK, $GATE, $DNS1, $DNS2 $Index = _GUICtrlListBox_GetCurSel ($ConfigList) + 1 GUICtrlSetData($ProfilName, $Configs[$Index][0]) _GUICtrlComboBox_SelectString ($CARD, $Configs[$Index][1]) _GUICtrlIpAddress_Set($IPWS, $Configs[$Index][2]) _GUICtrlIpAddress_Set($MASK, $Configs[$Index][3]) _GUICtrlIpAddress_Set($GATE, $Configs[$Index][4]) _GUICtrlIpAddress_Set($DNS1, $Configs[$Index][5]) ;~ _GUICtrlIpAddress_Set($DNS2, $Configs[$Index][6]) $IsDHCP = False EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _GetCurrent() Local $ReadIP, $ReadMask, $ReadGateway, $ReadDNS, $DHCP, $i If _GUICtrlComboBox_GetEditText($CARD) = "" Then MsgBox(48, "Carte réseau", "Selectionnez une carte réseau!") Return EndIf For $i = 1 to $NetCardDescriptors[0][0] If _GUICtrlComboBox_GetEditText($CARD) = $NetCardDescriptors[$i][1] Then ExitLoop EndIf Next $DHCP = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2] , 'EnableDHCP') ;~ MsgBox(0, "", $DHCP) If $DHCP = 0 Then $IsDHCP = False $ReadIP = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2], 'IPAddress') $ReadMask = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2], 'SubnetMask') $ReadGateway = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2], 'DefaultGateway') $ReadDNS = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2], 'NameServer') Else $IsDHCP = True $ReadIP = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2], 'DhcpIPAddress') $ReadMask = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2], 'DhcpSubnetMask') $ReadGateway = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2], 'DhcpDefaultGateway') $ReadDNS = RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' & $NetCardDescriptors[$i][2], 'DhcpNameServer') EndIf GUICtrlSetData($ProfilName, "Configuration #" & _GUICtrlListBox_GetCount($ConfigList) + 1) _GUICtrlIpAddress_Set($IPWS, $ReadIP) _GUICtrlIpAddress_Set($MASK, $ReadMask) _GUICtrlIpAddress_Set($GATE, $ReadGateway) _GUICtrlIpAddress_Set($DNS1, StringLeft($ReadDNS, 15)) ;~ _GUICtrlIpAddress_Set($hWnd, $sAddress) EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _GetInfoReseau() Global $IniConfigs, $IniNetCard, $Network Local $i $i=0 While 1 $i+=1 $NetCardDescriptors[$i][0] = RegEnumKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards", $i) $NetCardDescriptors[$i][1] = StringReplace(RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" & $NetCardDescriptors[$i][0] , "Description"),"/","") $NetCardDescriptors[$i][2] = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" & $NetCardDescriptors[$i][0] , "ServiceName") If @error <> 0 Then ExitLoop EndIf $NetCardDescriptors[0][0] = $i _GUICtrlComboBox_AddString($CARD, $NetCardDescriptors[$i][1]) _GUICtrlComboBox_SetCurSel($CARD, 0) WEnd ;~ _ArrayDisplay($NetCardDescriptors) EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _ImportProfils() Local $i, $j, $r, $HaveDHCP = False Local $String, $Profils $Profils = IniReadSection ( "profils.dat", "Profils" ) if @Error = 1 Then _CreateINI() $Profils = IniReadSection ( "profils.dat", "Profils" ) EndIf ;~ _ArrayDisplay($Profils) While _GUICtrlListBox_DeleteString($ConfigList, 0) <> -1 WEnd For $i = 1 To $Profils[0][0] $String = StringSplit($Profils[$i][1], ";") if $Profils[$i][0] = "DHCP" Then $HaveDHCP = True IniWrite ( "profils.dat", "Profils", "DHCP", ";000.000.000.000;000.000.000.000;000.000.000.000;000.000.000.000") EndIf $r = _GUICtrlListBox_InsertString($ConfigList, $Profils[$i][0], $i-1) $Configs[$i][0] = $Profils[$i][0] For $j = 1 To 5 $Configs[$i][$j] = $String[$j] Next Next ;~ _ArrayDisplay($Profils) If $HaveDHCP = False Then _CreateINI() _GUICtrlListBox_SetCurSel($ConfigList, 0) _Fill() return $Profils[0][0] EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _Quit() Exit EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _SaveProfil() Local $Result, $CanSave = False If $IsDHCP = True Then If MsgBox(36, "Configuration automatique DHCP", "Cette configuration réseau a été attribué automatiquement par DHCP" & @CRLF & "Elle sera convertie en configuration statique à l'enregistrement"& @CRLF & "La configuration pourrait être atribuée à une autre machine sur le réseau" & @CRLF & "Voulez vous continuer ?" ) = 7 Then Return 0 EndIf EndIf If GUICtrlRead($ProfilName) = "DHCP" Then MsgBox(48, "Enregistrement...", "Ce profil ne doit pas être modifié." & @CRLF & "Saisissez un autre nom") return 0 EndIf If GUICtrlRead($ProfilName) = "" Then MsgBox(48, "Enregistrement...", "Saisissez un nom pour le profil") return 0 EndIf If _GUICtrlListBox_FindString($ConfigList, GUICtrlRead($ProfilName), True) <> -1 Then if MsgBox(52, "Enregistrement...", "Le nom de profil existe déjà, voulez vous l'écraser ?") = 7 Then return 0 EndIf EndIf $Result = IniWrite ( "profils.dat", "Profils", GUICtrlRead($ProfilName), _GUICtrlComboBox_GetEditText($CARD) & ";" & _GUICtrlIpAddress_Get($IPWS) & ";" & _GUICtrlIpAddress_Get($MASK) & ";" & _GUICtrlIpAddress_Get($GATE) & ";" & _GUICtrlIpAddress_Get($DNS1)) _ImportProfils() If $Result <> 1 Then MsgBox(48, "Erreur de sauvegarde", "Impossible d'enregistrer le profil actif."); & @CRLF & "Merci de redémarrer le programme pour prendre en compte les modifications") EndIf EndFunc Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg Local $hWndFrom, $iIDFrom, $iCode, $hWndListBox If Not IsHWnd($ConfigList) Then $hWndListBox = GUICtrlGetHandle($ConfigList) $hWndFrom = $ilParam $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word $iCode = BitShift($iwParam, 16) ; Hi Word Switch $hWndFrom Case $ConfigList, $hWndListBox Switch $iCode Case $LBN_SELCHANGE ; Sent when the selection in a list box has changed _Fill() EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_COMMAND ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;