Code : Tout sélectionner
#NoTrayIcon
Break(0)
#RequireAdmin
#include <GUIConstants.au3>
#include <GuiIPAddress.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
$title = "MiniLanConf"
Global $networkList
If NOT IsAdmin() Then
MsgBox(16, $title, "Vous ne disposez pas des droits nécessaires pour exécuter ce programme.")
Exit
EndIf
$window = GUICreate($title, 400, 180)
GUICtrlCreateLabel("Interface réseau à configurer :", 10, 15, 200)
_ListeCNX() ; Création de la liste des connexions réseau
$ip = @IPAddress1
$gw = StringRegExpReplace($ip, "(.*)\.(.*)\.(.*)\.(.*)", "$1.$2.$3.1")
GUICtrlCreateGroup ("Configuration IP manuelle", 10, 45, 380, 95)
GUICtrlCreateLabel("Adresse IP :", 20, 65, 150)
$adresseIP = _GUICtrlIpAddress_Create ($window, 220, 60, 150, 20)
_GUICtrlIpAddress_Set($adresseIP, $ip)
GUICtrlCreateLabel("Masque de sous-réseau :", 20, 90, 150)
$masque = _GUICtrlIpAddress_Create ($window, 220, 85, 150, 20)
_GUICtrlIpAddress_Set($masque, "255.255.255.0")
GUICtrlCreateLabel("Passerelle par défaut :", 20, 115, 150)
$passerelle = _GUICtrlIpAddress_Create ($window, 220, 110, 150, 20)
_GUICtrlIpAddress_Set($passerelle, $gw)
$OK = GUICtrlCreateButton("Appliquer", 10, 150, 180, 25)
$quit = GUICtrlCreateButton("Annuler", 210, 150, 180, 25)
GUISetState()
While 1
$msg = GUIGetMsg()
If $msg == $GUI_EVENT_CLOSE OR $msg == $quit Then ExitLoop
If $msg == $OK Then _Apply()
Wend
Func _Apply()
$nw = GUICtrlRead($networkList)
$IP = _Read($adresseIP, "IP")
$netMask = _Read($masque, "IP")
$gw = _Read($passerelle, "IP")
If $IP == "0.0.0.0" OR $netMask == "0.0.0.0" Then
GUISetState( @SW_DISABLE )
MsgBox(16, $title, "La configuration est incorrecte.")
GUISetState( @SW_ENABLE )
Return 0
EndIf
if $gw == "0.0.0.0" Then $gw = ""
$cmd1 = 'netsh interface ip set address name="' & $nw & '" source=static ' & $IP & ' ' & $netMask & ' ' & $gw
$pos = WinGetPos ( $title )
$x = $pos[0]
$y = $pos[1]
GUISetState( @SW_DISABLE )
GUICtrlSetstate ( $OK , $GUI_DISABLE )
GUICtrlSetstate ( $quit , $GUI_DISABLE )
SplashTextOn ( $title, "Configuration en cours, veuillez patienter ..." , 400 , 50 , $x, $y + 50)
RunWait ( @ComSpec & " /c " & _AnsiToOEM($cmd1) , @SystemDir , @SW_HIDE )
SplashOff()
GUIDelete()
MsgBox(0, $title, "La configuration réseau a été appliquée")
Exit
EndFunc
;Lecture d'un champs de saisie
Func _Read($champ, $type = "default")
Switch $type
Case "default"
Return GUICtrlRead ($champ)
Case "IP"
Return _GUICtrlIpAddress_Get($champ)
EndSwitch
EndFunc
Func _ListeCNX()
; Rercherche les connexion réseau existantes
Global $racine
$racine = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
$i = 1
$n = 0
While 1
$var = RegEnumKey($racine, $i)
If @error Then ExitLoop
If $var <> "Descriptions" Then
$pnp = RegRead($racine & "\" & $var & "\Connection", "PnpInstanceID")
$name = RegRead($racine & "\" & $var & "\Connection", "Name")
If $pnp <> "" Then
$n = $n + 1
If $n == 1 Then
$networkList = GUICtrlCreateCombo($name, 180,10, 210, 20, $CBS_DROPDOWNLIST )
Else
GUICtrlSetData($networkList, $name)
EndIf
EndIf
EndIf
$i = $i + 1
Wend
If $n == 0 Then
MsgBox(16, $title, "Aucune interface réseau n'a été détectée.")
Exit
EndIf
EndFunc
Func _AnsiToOEM($sAnsi)
Local $a_OEMFName = DllCall('user32.dll', 'Int', 'CharToOem', 'str', $sAnsi, 'str', '')
If @error = 0 Then Return $a_OEMFName[2]
EndFunc ;==>_AnsiToOEM