本帖最后由 流浪628 于 2012-1-29 14:52 编辑
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#Include <Date.au3>
#Include <WinAPI.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=E:\作品\Form1.kxf
Global Const $NetworkAdapterInfo = _NetworkAdapterInfo() ;获得网卡信息.
Global Const $ScriptDir=@ScriptDir & '\'& "ip.ini" ;获得文件存放路径。
Global $hIcons[2]
$Form1 = GUICreate("酒kkkkkkkkkk", 520, 390, 348, 85)
$Group1 = GUICtrlCreateGroup("网络信息", 16, 16, 489, 337)
$Label1 = GUICtrlCreateLabel("可用网卡:", 42, 72, 60, 17)
$Label2 = GUICtrlCreateLabel("本 地 IP:", 42, 96, 60, 17)
$Label3 = GUICtrlCreateLabel("子网掩码:", 42, 120, 60, 17)
$Label4 = GUICtrlCreateLabel("本地网关:", 42, 144, 60, 17)
$Label5 = GUICtrlCreateLabel("首选 DNS:", 42, 168, 60, 17)
$Label6 = GUICtrlCreateLabel("备用 DNS:", 42, 192, 60, 17)
$Label7 = GUICtrlCreateLabel("DHCP状态:", 42, 216, 60, 17)
$Group2 = GUICtrlCreateGroup("准备修改的网络信息", 264, 16, 241, 337)
$Label8 = GUICtrlCreateLabel("可用网卡", 296, 48, 52, 17)
$Label9 = GUICtrlCreateLabel("本 地 IP", 296, 72, 60, 17)
$Label10 = GUICtrlCreateLabel("子网掩码", 296,96, 52, 17)
$Label11 = GUICtrlCreateLabel("本地网关", 296, 120, 52, 17)
$Label12 = GUICtrlCreateLabel("首选DNS", 296, 144, 51, 17)
$Label13 = GUICtrlCreateLabel("备用DNS", 296, 168, 51, 17)
$Label16 = GUICtrlCreateLabel("apache启动", 296, 192, 64, 17)
$Button1 = GUICtrlCreateButton("启动", 312, 312, 49, 25)
$Button2 = GUICtrlCreateButton("退出", 392, 312, 49, 25)
$Label15 = GUICtrlCreateLabel("请选择本地链接", 368, 48, 88, 17)
$Label27 = GUICtrlCreateLabel("192.168.1.200", 368, 72, 109, 17)
$Label29 = GUICtrlCreateLabel("255.255.255.0", 368, 96, 105, 17)
$Label30 = GUICtrlCreateLabel("192.168.1.1", 368, 120, 109, 17)
$Label31 = GUICtrlCreateLabel("202.106.0.20", 368, 144, 115, 17)
$Label33 = GUICtrlCreateLabel("210.82.5.1", 368, 168, 111, 17)
$Label34 = GUICtrlCreateLabel("启动", 368, 192, 108, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label18 = GUICtrlCreateLabel("本地链接:", 42, 240, 60, 17)
$Label19 = GUICtrlCreateLabel("", 112, 80, 4, 4)
$Label20 = GUICtrlCreateLabel("请选择本地链接", 128, 72, 112, 17)
$Label21 = GUICtrlCreateLabel("请选择本地IP地址", 128, 96, 106, 17)
$Label22 = GUICtrlCreateLabel("请选择本地I子网掩码", 128, 120, 115, 17)
$Label23 = GUICtrlCreateLabel("请选择本地网关", 128, 144, 112, 17)
$Label24 = GUICtrlCreateLabel("请选择首选DNS", 128, 168, 111, 17)
$Label25 = GUICtrlCreateLabel("DHCP状态", 128, 216, 106, 17)
$Label26 = GUICtrlCreateLabel("请选择备用DNS", 128, 192, 111, 17)
$Label28 = GUICtrlCreateLabel("连接状", 128, 240, 104, 17)
$Label36 = GUICtrlCreateLabel("系统环境:", 42, 48, 60, 17)
$Label37 = GUICtrlCreateLabel("检测系统的运行环境", 128, 48, 112, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$hStatus = _GUICtrlStatusBar_Create ($Form1) ;If using GUICtrlCreateMenu then use _GUICtrlStatusBar_Create after GUICtrlCreateMenu
Global $aParts[2] = [350, -1]
_GUICtrlStatusBar_SetParts ($hStatus, $aParts)
_GUICtrlStatusBar_SetText ($hStatus, " qqqqqq",0)
_GUICtrlStatusBar_SetText ($hStatus, " 酒qqqqqqq部", 1)
_GUICtrlStatusBar_SetIcon ($hStatus, 0, -19, "shell32.dll")
_GUICtrlStatusBar_SetIcon ($hStatus, 1, 221, "shell32.dll")
$hIcons[0] = _WinAPI_LoadShell32Icon (-19)
$hIcons[1] = _WinAPI_LoadShell32Icon (221)
_GUICtrlStatusBar_SetIcon ($hStatus, 0, $hIcons[0])
_GUICtrlStatusBar_SetIcon ($hStatus, 1, $hIcons[1])
$pid=ProcessExists("apmxe.exe")
If $pid <> "" Then
MsgBox("1","apache","apmxe以启动")
GUICtrlSetData($Label34,"apmxe以启动")
EndIf
If $pid = "" Then
GUICtrlSetData($Label34,"apmxe没启动")
MsgBox("1","apache","请稍候apmxe正在启动")
EndIf
;检测系统环境
Global Const $OSVersion= @OSVersion
GUICtrlSetData($Label37,$OSVersion)
;保存IP地址和往输入框里填写固定IP。
For $i=1 To $NetworkAdapterInfo[0][0]
GUICtrlSetData(1,$NetworkAdapterInfo[1][3],$NetworkAdapterInfo[1][3])
GUICtrlSetData($Label20,$NetworkAdapterInfo[1][3])
If $NetworkAdapterInfo[1][2] ="7" Then
GUICtrlSetData($Label28,"不在线")
EndIf
If $NetworkAdapterInfo[1][2] ="2" Then
GUICtrlSetData($Label28,"在线")
EndIf
;获取IP、子网掩码、网关并保存在IP.INI文件里,并将要改的IP地址显示在输入框里。
If $NetworkAdapterInfo[$i][0] = 0 Then
GUICtrlSetData(1,"没有找到可用的网卡信息^_^")
Else
If $NetworkAdapterInfo[1][6] = True Then
IniWrite ( $ScriptDir, "DHCP", "dhcp值", "True")
GUICtrlSetData($Label25,"DHCP动态获得")
EndIf
If $NetworkAdapterInfo[1][6] = False then
IniWrite ( $ScriptDir, "DHCP", "dhcp值", "False")
GUICtrlSetData($Label25,"DHCP静态获得")
IniWrite ( $ScriptDir, $NetworkAdapterInfo[1][3], "ip地址", $NetworkAdapterInfo[1][7])
IniWrite ( $ScriptDir, $NetworkAdapterInfo[1][3], "子网掩码",$NetworkAdapterInfo[1][8])
IniWrite ( $ScriptDir, $NetworkAdapterInfo[1][3], "网关地址",$NetworkAdapterInfo[1][9])
IniWrite ( $ScriptDir, $NetworkAdapterInfo[1][3], "首选DNS",$NetworkAdapterInfo[1][10])
IniWrite ( $ScriptDir, $NetworkAdapterInfo[1][3], "备用DNS",$NetworkAdapterInfo[1][11])
GUICtrlSetData($Label37,$OSVersion)
GUICtrlSetData($Label21,$NetworkAdapterInfo[1][7])
GUICtrlSetData($Label22,$NetworkAdapterInfo[1][8])
GUICtrlSetData($Label24,$NetworkAdapterInfo[1][10])
GUICtrlSetData($Label23,$NetworkAdapterInfo[1][9])
GUICtrlSetData($Label26,$NetworkAdapterInfo[1][11])
GUICtrlSetData($Label15,$NetworkAdapterInfo[1][3])
MsgBox("1","设置","ip地址:"&$NetworkAdapterInfo[1][7]&@CRLF&"子网掩码:"&$NetworkAdapterInfo[1][8]&@CRLF&"网关地址:"&$NetworkAdapterInfo[1][9]&@CRLF&"首选DNS:"&$NetworkAdapterInfo[1][10]&@CRLF&"备用DNS:"&$NetworkAdapterInfo[1][11]&@CRLF&"都已保存")
EndIf
EndIf
Next
;点击按钮时的三种状态。
While 1
$nMsg = GUIGetMsg() ;捕获窗口消息
Switch $nMsg
Case $GUI_EVENT_CLOSE ;对话框(窗口)正被关闭(相关按钮被点击或系统菜单项被选中).
Exit
Case $Button1 ;更改IP地址
If GUICtrlRead($Label20) = "本地连接" Then
$ConName = $NetworkAdapterInfo[1][3]
GUICtrlSetData($Label8,$NetworkAdapterInfo[1][3])
$IpADD = GUICtrlRead($Label27)
$GateWay = GUICtrlRead($Label30)
$ConDNS2 = GUICtrlRead($Label33)
$SubMask = GUICtrlRead($Label29)
$ConDNS1 = GUICtrlRead($Label31)
MsgBox("1","更改","ip地址:"&$IpADD&@CRLF&"子网掩码:"&$SubMask&@CRLF&"网关地址:"&$GateWay&@CRLF&"首选DNS:"&$ConDNS1&@CRLF&"备用DNS:"&$ConDNS2&@CRLF&"都已更改")
If $OSVersion = "WIN_XP" Then
_CMD_SetLocalIP_XP($ConName,$IpADD,$SubMask,$GateWay,$ConDNS1,$ConDNS2)
EndIf
If $OSVersion = "WIN_7" Then
_CMD_SetLocalIP_WIN7($ConName,$IpADD,$SubMask,$GateWay,$ConDNS1,$ConDNS2)
EndIf
If GUICtrlRead($Label34) ="apmxe没启动" Then
$apache = @ScriptDir&"\ap\httpd.exe"
$conf=@ScriptDir&"\ap\conf\httpd.conf"
$appath=@ScriptDir&"\ap"
$webroot=@ScriptDir&"\htdocs"
$ggg=$apache&" -f "&'"'&$conf&'" -d "'&$appath&'" -C "DocumentRoot '&"'"&$webroot&"'"&'"'
;RunWait(@ComSpec & " /C "& $apache& " -f "&'"'&$conf&'" -d "'&$appath&'" -C "DocumentRoot '&"'"&$webroot&"'"&'"', "", @SW_HIDE)
Run(@ComSpec & " /C "& $ggg, "")
;检测apache是否启动
If ProcessExists("httpd.exe") <>0 Then
MsgBox("1","2","apache已启动")
GUICtrlSetData($Label34,"apache已启动")
EndIf
If ProcessExists("httpd.exe") = 0 Then
MsgBox("1","2",ProcessExists("httpd.exe"))
GUICtrlSetData($Label34,"apmxe没222启动")
EndIf
EndIf
EndIf
Case $Button2 ;把INI里面的值取过来。
If GUICtrlRead($Label20) = "本地连接" Then
MsgBox(1,"",IniRead($ScriptDir, "DHCP", "dhcp值", "NotFound"))
If IniRead($ScriptDir, "DHCP", "dhcp值", " ") = "True" Then
_DHCP_IP_SetLocalIP()
_DHCP_DNS_SetLocalIP()
EndIf
If IniRead($ScriptDir, "DHCP", "dhcp值", " ") = "False" Then
GUICtrlSetData($Label8,@ComputerName) ;修改指定控件的数据.
GUICtrlSetData($Label27,IniRead($ScriptDir, "本地连接", "ip地址", "NotFound"))
GUICtrlSetData($Label30,IniRead($ScriptDir, "本地连接", "网关地址", "NotFound"))
GUICtrlSetData($Label31,IniRead($ScriptDir, "本地连接", "首选DNS", "NotFound"))
GUICtrlSetData($Label29,IniRead($ScriptDir, "本地连接", "子网掩码", "NotFound"))
GUICtrlSetData($Label33,IniRead($ScriptDir, "本地连接", "备用DNS", "NotFound"))
$ConName = $NetworkAdapterInfo[1][3]
$IpADD = GUICtrlRead($Label27)
$GateWay = GUICtrlRead($Label30)
$ConDNS1 = GUICtrlRead($Label31)
$SubMask = GUICtrlRead($Label29)
$ConDNS2 = GUICtrlRead($Label33)
MsgBox("1","还原","ip地址:"&$IpADD&@CRLF&"子网掩码:"&$SubMask&@CRLF&"网关地址:"&$GateWay&@CRLF&"首选DNS:"&$ConDNS1&@CRLF&"备用DNS:"&$ConDNS2&@CRLF&"都已还原初始状态")
If $OSVersion = "WIN_XP" Then
_CMD_SetLocalIP_XP($ConName,$IpADD,$SubMask,$GateWay,$ConDNS1,$ConDNS2)
EndIf
If $OSVersion = "WIN_7" Then
_CMD_SetLocalIP_WIN7($ConName,$IpADD,$SubMask,$GateWay,$ConDNS1,$ConDNS2)
EndIf
;检查apache进程,然后查杀。
$list = ProcessList("httpd.exe")
For $i = 1 To $list[0][0]
ProcessClose($list[$i][1])
Next
Exit
EndIf
EndIf
EndSwitch
WEnd
;获取网卡信息的函数。
Func _NetworkAdapterInfo()
Local $colItems1 = ""
Local $objWMIService
Local $NetworkAdapterID = 0
Local $NetworkAdapterName = ""
Local $NetworkAdapterGateway = ""
Local $NetworkAdapterHostName = ""
Local $NetworkAdapterIPaddress = ""
Local $NetworkAdapterDNS1 = ""
Local $NetworkAdapterDNS2 = ""
Local $NetworkAdapterSubnet = ""
Local $NetworkAdapterMAC = ""
Local $NetworkAdapterNetConnectionID = ""
Local $NetworkAdapterNetConnectionStatus = ""
Local $NetworkAdapterInfo[10][12] ;最高10块网卡.
$NetworkAdapterInfo[0][0] = 0
$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems1 = $objWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus >0 ', "WQL", 0x30)
If IsObj($colItems1) Then
For $objItem In $colItems1
$NetworkAdapterName = $objItem.Description
If $NetworkAdapterName="" Then ContinueLoop
If StringInStr ($NetworkAdapterName,"vmware")=True Then ContinueLoop
$NetworkAdapterNetConnectionStatus = $objItem.NetConnectionStatus
$NetworkAdapterNetConnectionID = $objItem.NetConnectionID
If StringInStr ($NetworkAdapterNetConnectionID,"无线")=True Then ContinueLoop
$NetworkAdapterIndex = $objItem.Index
$NetworkAdapterMAC = $objItem.MACAddress
$NetworkAdapterID += 1
$NetworkAdapterInfo[0][0] = $NetworkAdapterID
$NetworkAdapterInfo[$NetworkAdapterID][0] = $NetworkAdapterID
$NetworkAdapterInfo[$NetworkAdapterID][1] = $NetworkAdapterName
$NetworkAdapterInfo[$NetworkAdapterID][2] = $NetworkAdapterNetConnectionStatus ;反回的值是2表示在线,7表示不在线。
$NetworkAdapterInfo[$NetworkAdapterID][3] = $NetworkAdapterNetConnectionID
$NetworkAdapterInfo[$NetworkAdapterID][4] = $NetworkAdapterMAC
$NetworkAdapterInfo[$NetworkAdapterID][5] = $NetworkAdapterIndex
Local $colItems2 = $objWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = "'& $NetworkAdapterIndex &'" ', "WQL", 0x30)
If IsObj($colItems2) Then
For $objItem2 In $colItems2
$NetworkAdapterIPaddress = $objItem2.IPAddress(0)
$NetworkAdapterDHCPEnabled = $objItem2.DHCPEnabled
$NetworkAdapterIPSubnet = $objItem2.IPSubnet(0)
$NetworkAdapterDefaultIPGateway = $objItem2.DefaultIPGateway(0)
$NetworkAdapterDNS1 = $objItem2.DNSServerSearchOrder(0)
$NetworkAdapterInfo[$NetworkAdapterID][6] = $NetworkAdapterDHCPEnabled
$NetworkAdapterInfo[$NetworkAdapterID][7] = $NetworkAdapterIPaddress
$NetworkAdapterInfo[$NetworkAdapterID][8] = $NetworkAdapterIPSubnet
$NetworkAdapterInfo[$NetworkAdapterID][9] = $NetworkAdapterDefaultIPGateway
$NetworkAdapterInfo[$NetworkAdapterID][10] = $NetworkAdapterDNS1
If UBound($objItem2.DNSServerSearchOrder)>1 Then
$NetworkAdapterDNS2 = $objItem2.DNSServerSearchOrder(1)
$NetworkAdapterInfo[$NetworkAdapterID][11] = $NetworkAdapterDNS2
EndIf
Next
EndIf
Next
Else
Return $NetworkAdapterInfo
EndIf
Return $NetworkAdapterInfo
EndFunc ;==>_NetworkAdapterInfo
;修改win7 IP、子网拽码、网关、DNS的函数
Func _CMD_SetLocalIP_WIN7($ConName, $IpADD, $SubMask, $GateWay, $ConDNS1,$ConDNS2)
If $SubMask <> "" Then $SubMask = "255.255.255.0"
If $ConName <> "" And $IpADD <> "" Then
RunWait(@ComSpec & ' /C netsh -c interface ip set address ' & '"' & $ConName & '"' & ' static ' & $IpADD & " " & $SubMask & " " & $GateWay, "", @SW_HIDE)
EndIf
If $ConDNS1 <> "" Then
RunWait(@ComSpec & ' /C netsh -c interface ip set dns ' & '"' & $ConName & '"' & ' static ' & $ConDNS1 & ' primary validate=no', "", @SW_HIDE)
EndIf
If $ConDNS2 <> "" Then
RunWait(@ComSpec & ' /C netsh -c interface ip add dns ' & '"' & $ConName & '" ' & $ConDNS2 & ' validate=no', "", @SW_HIDE)
EndIf
EndFunc ;==>_CMD_SetLocalIP
;修改xp IP、子网拽码、网关、DNS的函数
Func _CMD_SetLocalIP_XP($ConName, $IpADD, $SubMask, $GateWay, $ConDNS1,$ConDNS2)
If $SubMask <> "" Then $SubMask = "255.255.255.0"
If $ConName <> "" And $IpADD <> "" Then
RunWait(@ComSpec & ' /C netsh interface ip set address ' & '"' & $ConName & '"' & ' static ' & $IpADD & " " & $SubMask & " " & $GateWay & " " & ' none ',"", @SW_HIDE)
EndIf
If $ConDNS1 <> "" Then
RunWait(@ComSpec & ' /C netsh interface ip set dns ' & '"' & $ConName & '"' & ' static ' & $ConDNS1 & ' primary', "", @SW_HIDE)
EndIf
If $ConDNS2 <> "" Then
RunWait(@ComSpec & ' /C netsh interface ip add dns ' & '"' & $ConName & '" ' & $ConDNS2,"", @SW_HIDE)
EndIf
EndFunc ;==>_CMD_SetLocalIP
;设置DHCP的函数
Func _DHCP_IP_SetLocalIP()
RunWait(@ComSpec & ' /C netsh interface ip set address ' & '"' & $NetworkAdapterInfo[1][3] & '"' & ' source=dhcp ', "", @SW_HIDE)
EndFunc
Func _DHCP_DNS_SetLocalIP()
RunWait(@ComSpec & ' /C netsh interface ip set dns ' & '"' & $NetworkAdapterInfo[1][3] & '"' & ' source=dhcp ', "", @SW_HIDE)
EndFunc
;检测apache是否启动的函数
Func _ProcessGetLocation($iPID)
Local $aProc = DllCall('kernel32.dll', 'hwnd', 'OpenProcess', 'int', BitOR(0x0400, 0x0010), 'int', 0, 'int', $iPID)
If $aProc[0] = 0 Then Return SetError(1, 0, '')
Local $vStruct = DllStructCreate('int[1024]')
DllCall('psapi.dll', 'int', 'EnumProcessModules', 'hwnd', $aProc[0], 'ptr', DllStructGetPtr($vStruct), 'int', DllStructGetSize($vStruct), 'int_ptr', 0)
Local $aReturn = DllCall('psapi.dll', 'int', 'GetModuleFileNameEx', 'hwnd', $aProc[0], 'int', DllStructGetData($vStruct, 1), 'str', '', 'int', 2048)
If StringLen($aReturn[3]) = 0 Then Return SetError(2, 0, '')
Return $aReturn[3]
EndFunc
[/code]官方的帮助手册修改DNS哪块是不完整的,每次只能改一个DNS,我在它的基础上做了改进,现在可以一下修改两个DNS,
执行程序时获得本机IP和DHCP
退出时还原本地IP和DHCP
这段代码只能运行在WIN7,XP系统里运行。
图在下面,我的回复里。
附件太大上传不上去,郁闷,6.57mb |