#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


$Info_1 = _NetworkAdapterInfo()

Func _ipset()	
$IPset = GUICreate("更改IP地址", 248, 247, -1, -1, -1, -1, $window)
GUISetBkColor(0xEFF0F2)
$IPBut_1 = GUICtrlCreateButton("修改", 8, 208, 75, 25)
$IPBut_2 = GUICtrlCreateButton("返回", 160, 208, 75, 25)
$IPbel_1 = GUICtrlCreateLabel("选择网卡：", 8, 16, 64, 17)
$IPCombo_1 = GUICtrlCreateCombo("", 72, 12, 161, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
$IPbel_2 = GUICtrlCreateLabel("I P地址:", 8, 48, 52, 17)
$IPbel_3 = GUICtrlCreateLabel("子网掩码：", 8, 80, 64, 17)
$IPbel_4 = GUICtrlCreateLabel("默认网关：", 8, 112, 64, 17)
$IPbel_5 = GUICtrlCreateLabel("首选DNS：", 8, 144, 63, 17)
$IPbel_6 = GUICtrlCreateLabel("备用DNS：", 8, 176, 63, 17)
$IPput_1 = GUICtrlCreateInput("", 72, 44, 161, 21)
$IPput_2 = GUICtrlCreateInput("", 72, 76, 161, 21)
$IPput_3 = GUICtrlCreateInput("", 72, 108, 161, 21)
$IPput_4 = GUICtrlCreateInput("", 72, 140, 161, 21)
$IPput_5 = GUICtrlCreateInput("", 72, 172, 161, 21)
GUISetState(@SW_SHOW)

For $i = 1 To $Info_1[0][0]
	GUICtrlSetData($IPCombo_1,$Info_1[$i][2],$Info_1[$i][2])
	GUICtrlSetData($IPput_1,$Info_1[$i][5])
	GUICtrlSetData($IPput_2,$Info_1[$i][7])
	GUICtrlSetData($IPput_3,$Info_1[$i][6])
	GUICtrlSetData($IPput_4,$info_1[$i][8])
	GUICtrlSetData($IPput_5,$Info_1[$i][9])
Next

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			GUIDelete($IPset)
			ExitLoop
		Case $IPCombo_1
			For $i = 1 To $Info_1[0][0]
				If $Info_1[$i][2] = GUICtrlRead($IPCombo_1) Then
					GUICtrlSetData($IPput_1,$Info_1[$i][5])
					GUICtrlSetData($IPput_2,$Info_1[$i][7])
					GUICtrlSetData($IPput_3,$Info_1[$i][6])
					GUICtrlSetData($IPput_4,$info_1[$i][8])
					GUICtrlSetData($IPput_5,$Info_1[$i][9])
				EndIf
			Next
		Case $IPBut_1
			For $i = 1 To $Info_1[0][0]
				If GUICtrlRead($IPCombo_1) = "" Then
					MsgBox(16,"提示","没有读取到相应的网卡，程序出错！")
				ElseIf GUICtrlRead($IPCombo_1) = $info_1[$i][2] Then
					_SetCmpNameAndIP($info_1[$i][10],GUICtrlRead($IPput_1),GUICtrlRead($IPput_2),GUICtrlRead($IPput_3),GUICtrlRead($IPput_4)&","&GUICtrlRead($IPput_5))
					MsgBox(64,"提示","修改完成,需要重启生效！")
				EndIf
			Next
		Case $IPBut_2
			GUIDelete($IPset)
			ExitLoop
			

	EndSwitch
WEnd
EndFunc

Func _NetworkAdapterInfo()
    Local $colItem
    Local $objItem
    Local $colItems 
    Local $objItems
    Local $objWMIService 
    Local $Adapters[1][12]
    $Adapters[0][0] = 0
    $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
    $colItem  = $objWMIService.ExecQuery("Select * FROM Win32_NetworkAdapter Where NetConnectionStatus >0", "WQL", 0x30)
    If IsObj($colItem) Then
        For $objItem In $colItem
            If $objItem.MACAddress = "00:00:00:00:00:00" Then ContinueLoop
            $Adapters[0][0] += 1
            ReDim $Adapters[UBound($Adapters) + 1][12]
            $Adapters[$Adapters[0][0]][0] += $Adapters[0][0]
            $Adapters[$Adapters[0][0]][1] = $objItem.NetConnectionStatus
            $Adapters[$Adapters[0][0]][2] = $objItem.Description
            $Adapters[$Adapters[0][0]][3] = $objItem.NetConnectionID
            $Adapters[$Adapters[0][0]][4] = $objItem.MACAddress 
                        $Adapters[$Adapters[0][0]][11] = $objItem.PNPDeviceID 
            $colItems  = $objWMIService.ExecQuery('Select * FROM Win32_NetworkAdapterConfiguration Where MACAddress = "'&$Adapters[$Adapters[0][0]][4]&'" And IPEnabled = True ' , "WQL", 0x30)                     
            If IsObj($colItems) Then
                For $objItems In $colItems
                    $Adapters[$Adapters[0][0]][5] = $objItems.IPAddress(0)
                    $Adapters[$Adapters[0][0]][6] = $objItems.DefaultIPGateway(0)
                    $Adapters[$Adapters[0][0]][7] = $objItems.IPSubnet(0)
                    $Adapters[$Adapters[0][0]][8] = $objItems.DNSServerSearchOrder(0)
                    $Adapters[$Adapters[0][0]][9] = $objItems.DNSServerSearchOrder(1)
                    $Adapters[$Adapters[0][0]][10] = $objItems.SettingID
                Next    
            EndIf
        Next
    EndIf
Return $Adapters
EndFunc

Func _SetCmpNameAndIP($LanID,$setIP,$setZW,$setWG,$setDNS) 
$SetKey1 = "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\"
$SetKey2 = "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\"
$CtrlKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\"
$LanReg1 = $SetKey1 & "Services\Tcpip\Parameters\Interfaces\" & $LanID
$LanReg2 = $SetKey2 & "Services\Tcpip\Parameters\Interfaces\" & $LanID
$LanReg3 = $CtrlKey & "Services\Tcpip\Parameters\Interfaces\" & $LanID
RegWrite($LanReg1,"IPAddress","REG_MULTI_SZ",$setIP) 
RegWrite($LanReg2,"IPAddress","REG_MULTI_SZ",$setIP) 
RegWrite($LanReg3,"IPAddress","REG_MULTI_SZ",$setIP)
RegWrite($LanReg1,"SubnetMask","REG_MULTI_SZ",$setZW) 
RegWrite($LanReg2,"SubnetMask","REG_MULTI_SZ",$setZW) 
RegWrite($LanReg3,"SubnetMask","REG_MULTI_SZ",$setZW)
RegWrite($LanReg1,"DefaultGateway","REG_MULTI_SZ",$setWG) 
RegWrite($LanReg2,"DefaultGateway","REG_MULTI_SZ",$setWG) 
RegWrite($LanReg3,"DefaultGateway","REG_MULTI_SZ",$setWG)
RegWrite($LanReg1,"NameServer","REG_SZ",$setDNS) 
RegWrite($LanReg2,"NameServer","REG_SZ",$setDNS) 
RegWrite($LanReg3,"NameServer","REG_SZ",$setDNS)
RegWrite($LanReg3,"EnableDHCP","REG_DWORD","0")
EndFunc

