关于获取网关地址的问题
代码是论坛大神的,借用一下,如下:$ip_gateway = _Gateway()
Func _Gateway()
Dim $IPGateway
Dim $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
Dim $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE", "WQL", 0x10 + 0x20)
For $objItem In $colItems
$IPGateway = $objItem.DefaultIPGateway(0)
If StringInStr($IPGateway, ".") > 0 Then ExitLoop
Next
If StringRegExpReplace($IPGateway, '(\d+\.){12}', '') = "0" Then
MsgBox ( 0 , "ERROR" , "无法获取本地网关,请检查网线是否连接且已通过认证" )
Else
Return StringRegExpReplace($IPGateway, '(\d+\.){12}', '')
EndIf
但是此代码有个问题,可以获取到网关,但是如果未插网线,或者说未连网,此代码不运行,请大神看看问题出在哪 本帖最后由 229989799 于 2017-4-17 15:04 编辑
win7测试正常。改了第十行的一个“0”。。楼主漏了15行,加了个Endfunc,否则无法运行此脚本。
MsgBox(0,0,_Gateway())
Func _Gateway()
Dim $IPGateway
Dim $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
Dim $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE", "WQL", 0x10 + 0x20)
For $objItem In $colItems
$IPGateway = $objItem.DefaultIPGateway(0)
If StringInStr($IPGateway, ".") > 0 Then ExitLoop
Next
If StringRegExpReplace($IPGateway, '(\d+\.){12}', '') = "" Then
MsgBox ( 0 , "ERROR" , "无法获取本地网关,请检查网线是否连接且已通过认证" )
Else
Return StringRegExpReplace($IPGateway, '(\d+\.){12}', '')
EndIf
EndFunc
楼上大神高见
页:
[1]