找回密码
 加入
搜索
查看: 2512|回复: 4

[网络通信] WIN7下 正常,WIN10下出错的问题,附代码

[复制链接]
发表于 2017-4-21 15:23:15 | 显示全部楼层 |阅读模式
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#PRE_Outfile=D:\Users\grubb\Desktop\监控\PING2.exe
#PRE_UseX64=n
#PRE_Res_requestedExecutionLevel=None
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <TabConstants.au3>

Opt("TrayMenuMode", 1)
$g_szVersion = "网络检测"
If WinExists($g_szVersion) Then Exit ; 此脚本已经运行了
AutoItWinSetTitle($g_szVersion)



$Form1 = GUICreate("网络检测", 320, 380, -1, -1)
$Button1 = GUICtrlCreateButton("开始检测", 80, 330, 73, 25)
$Button2 = GUICtrlCreateButton("停止检测", 184, 330, 73, 25)
$Input1 = GUICtrlCreatelabel("本地网关", 26, 34, 105, 21,BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY,$WS_BORDER))
GUICtrlSetBkColor($Input1,0x6CA6CD)
GUICtrlSetFont($Input1, 11, 800, 0, "微软雅黑")
$Input2 = GUICtrlCreatelabel("ERP地址", 26, 64, 105, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
GUICtrlSetBkColor($Input2,0x6CA6CD)
GUICtrlSetFont($Input2, 11, 800, 0, "微软雅黑")
$Input3 = GUICtrlCreatelabel("内网DNS", 26, 94, 105, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
GUICtrlSetBkColor($Input3,0x6CA6CD)
GUICtrlSetFont($Input3, 11, 800, 0, "微软雅黑")
$Input4 = GUICtrlCreatelabel("京东主页", 26, 124, 105, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
GUICtrlSetBkColor($Input4,0x6CA6CD)
GUICtrlSetFont($Input4 ,11, 800, 0, "微软雅黑")
$Input5 = GUICtrlCreatelabel("CRM地址", 26, 154, 105, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
GUICtrlSetBkColor($Input5,0x6CA6CD)
GUICtrlSetFont($Input5, 11, 800, 0, "微软雅黑")

$Input5 = GUICtrlCreateInput("本机网关地址:", 66, 280, 180, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))

$Input9 = GUICtrlCreateInput("", 151, 34, 41, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
$Input10 = GUICtrlCreateInput("", 151, 64, 41, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
$Input11 = GUICtrlCreateInput("", 151, 94, 41, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
$Input12 = GUICtrlCreateInput("", 151, 124, 41, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
$Input13 = GUICtrlCreateInput("", 151, 154, 41, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))

$Input17 = GUICtrlCreateInput('延迟:', 210, 34, 80, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
$Input18 = GUICtrlCreateInput("延迟:", 210, 64, 80, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
$Input19 = GUICtrlCreateInput("延迟:", 210, 94, 80, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
$Input20 = GUICtrlCreateInput("延迟:", 210, 124, 80, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))
$Input21 = GUICtrlCreateInput("延迟:", 210, 154, 80, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY, $WS_BORDER))


GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
$ip_gateway = _Gateway()
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        GUICtrlSetData($Input5, "本机网关地址:")
                        ;Call(stop)
                        Call(_Gateway)
                        If $ip_gateway = 0 Then
                                $msg = MsgBox(0, "ERROR", "无法获取本地网关,请检查网线是否连接且已通过认证")
                                GUICtrlSetData($Input5, "未获取到网络地址")
                       
                        Else
                                GUICtrlSetData($Input5, "本机网关地址:" & $ip_gateway)
                                Call(ping1)
                        EndIf
                Case $Button2 ;停止PING
                        Call(stop)
                        ;AdlibUnRegister(ping1)
                        ;GUICtrlSetData($Input17, "延迟:")
                        ;GUICtrlSetData($Input9, "")
                        ;GUICtrlSetData($Input18, "延迟:")
                        ;GUICtrlSetData($Input10, "")
                        ;GUICtrlSetData($Input19, "延迟:")
                        ;GUICtrlSetData($Input11, "")
                        ;GUICtrlSetData($Input20, "延迟:")
                        ;GUICtrlSetData($Input12, "")
                        ;GUICtrlSetData($Input21, "延迟:")
                        ;GUICtrlSetData($Input13, "")
                        ;GUICtrlSetData($Input5, "本机网关地址:")
        EndSwitch
WEnd

$ip_gateway = _Gateway()
Dim $IPGateway
Dim $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
Dim $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE", "WQL", 0x10 + 0x20)
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   ;==>_Gateway
Func stop()
                        AdlibUnRegister(ping1)
                        GUICtrlSetData($Input17, "延迟:")
                        GUICtrlSetData($Input9, "")
                        GUICtrlSetData($Input18, "延迟:")
                        GUICtrlSetData($Input10, "")
                        GUICtrlSetData($Input19, "延迟:")
                        GUICtrlSetData($Input11, "")
                        GUICtrlSetData($Input20, "延迟:")
                        GUICtrlSetData($Input12, "")
                        GUICtrlSetData($Input21, "延迟:")
                        GUICtrlSetData($Input13, "")
                        GUICtrlSetData($Input5, "本机网关地址:")
EndFunc
Func Ping1()
        $p1 = Ping($ip_gateway, 4000)
        If $p1 > 0 And $p1 < 30 Then
                GUICtrlSetData($Input17, "延迟:" & $p1 & " MS")
                GUICtrlSetData($Input9, "良好")
                GUICtrlSetColor($Input9, 0x32CD32)
        ElseIf $p1 >= 30 And $p1 < 60 Then
                GUICtrlSetData($Input17, "延迟:" & $p1 & " MS")
                GUICtrlSetData($Input9, "正常")
                GUICtrlSetColor($Input9, 0x436EEE)
        ElseIf $p1 >= 60 And $p1 < 120 Then
                GUICtrlSetData($Input17, "延迟:" & $p1 & " MS")
                GUICtrlSetData($Input9, "较差")
                GUICtrlSetColor($Input9, 0xEEB422)
        ElseIf $p1 >= 120 Then
                GUICtrlSetData($Input17, "延迟:" & $p1 & " MS")
                GUICtrlSetData($Input9, "异常")
                GUICtrlSetColor($Input9, 0xEE0000)
        ElseIf $p1 = 0 Then
                GUICtrlSetData($Input17, "延迟:超时")
                GUICtrlSetData($Input9, "异常")
                GUICtrlSetColor($Input9, 0xEE0000)
               
        EndIf
        $p2 = Ping("erp.jd.com", 4000)
        If $p2 > 0 And $p2 < 30 Then
                GUICtrlSetData($Input18, "延迟:" & $p2 & " MS")
                GUICtrlSetData($Input10, "良好")
                GUICtrlSetColor($Input10, 0x32CD32)
        ElseIf $p2 >= 30 And $p2 < 60 Then
                GUICtrlSetData($Input18, "延迟:" & $p2 & " MS")
                GUICtrlSetData($Input10, "正常")
                GUICtrlSetColor($Input10, 0x436EEE)
        ElseIf $p2 >= 60 And $p2 < 120 Then
                GUICtrlSetData($Input18, "延迟:" & $p2 & " MS")
                GUICtrlSetData($Input10, "较差")
                GUICtrlSetColor($Input10, 0xEEB422)
        ElseIf $p2 >= 120 Then
                GUICtrlSetData($Input18, "延迟:" & $p2 & " MS")
                GUICtrlSetData($Input10, "异常")
                GUICtrlSetColor($Input10, 0xEE0000)
        ElseIf $p2 = 0 Then
                GUICtrlSetData($Input18, "延迟:超时")
                GUICtrlSetData($Input10, "异常")
                GUICtrlSetColor($Input10, 0xEE0000)
               
        EndIf
        $p3 = Ping("172.17.23.148", 4000)
        If $p3 > 0 And $p3 < 30 Then
                GUICtrlSetData($Input19, "延迟:" & $p3 & " MS")
                GUICtrlSetData($Input11, "良好")
                GUICtrlSetColor($Input11, 0x32CD32)
        ElseIf $p3 >= 30 And $p3 < 60 Then
                GUICtrlSetData($Input19, "延迟:" & $p3 & " MS")
                GUICtrlSetData($Input11, "正常")
                GUICtrlSetColor($Input11, 0x436EEE)
        ElseIf $p3 >= 60 And $p3 < 120 Then
                GUICtrlSetData($Input19, "延迟:" & $p3 & " MS")
                GUICtrlSetData($Input11, "较差")
                GUICtrlSetColor($Input11, 0xEEB422)
        ElseIf $p3 >= 120 Then
                GUICtrlSetData($Input19, "延迟:" & $p3 & " MS")
                GUICtrlSetData($Input11, "异常")
                GUICtrlSetColor($Input11, 0xEE0000)
        ElseIf $p3 = 0 Then
                GUICtrlSetData($Input19, "延迟:超时")
                GUICtrlSetData($Input11, "异常")
                GUICtrlSetColor($Input11, 0xEE0000)
               
               
        EndIf
        $p4 = Ping("www.jd.com", 4000)
        If $p4 > 0 And $p4 < 30 Then
                GUICtrlSetData($Input20, "延迟:" & $p4 & " MS")
                GUICtrlSetData($Input12, "良好")
                GUICtrlSetColor($Input12, 0x32CD32)
        ElseIf $p4 >= 30 And $p4 < 60 Then
                GUICtrlSetData($Input20, "延迟:" & $p4 & " MS")
                GUICtrlSetData($Input12, "正常")
                GUICtrlSetColor($Input12, 0x436EEE)
        ElseIf $p4 >= 60 And $p4 < 120 Then
                GUICtrlSetData($Input20, "延迟:" & $p4 & " MS")
                GUICtrlSetData($Input12, "较差")
                GUICtrlSetColor($Input12, 0xEEB422)
        ElseIf $p4 >= 120 Then
                GUICtrlSetData($Input20, "延迟:" & $p4 & " MS")
                GUICtrlSetData($Input12, "异常")
                GUICtrlSetColor($Input12, 0xEE0000)
        ElseIf $p4 = 0 Then
                GUICtrlSetData($Input20, "延迟:超时")
                GUICtrlSetData($Input12, "异常")
                GUICtrlSetColor($Input12, 0xEE0000)
               
               
        EndIf
        $p5 = Ping("crm.jd.com", 4000)
        If $p5 > 0 And $p5 < 30 Then
                GUICtrlSetData($Input21, "延迟:" & $p5 & " MS")
                GUICtrlSetData($Input13, "良好")
                GUICtrlSetColor($Input13, 0x32CD32)
        ElseIf $p5 >= 30 And $p5 < 60 Then
                GUICtrlSetData($Input21, "延迟:" & $p5 & " MS")
                GUICtrlSetData($Input13, "正常")
                GUICtrlSetColor($Input13, 0x436EEE)
        ElseIf $p5 >= 60 And $p5 < 120 Then
                GUICtrlSetData($Input21, "延迟:" & $p5 & " MS")
                GUICtrlSetData($Input13, "较差")
                GUICtrlSetColor($Input13, 0xEEB422)
        ElseIf $p5 >= 120 Then
                GUICtrlSetData($Input21, "延迟:" & $p5 & " MS")
                GUICtrlSetData($Input13, "异常")
                GUICtrlSetColor($Input13, 0xEE0000)
        ElseIf $p5 = 0 Then
                GUICtrlSetData($Input21, "延迟:超时")
                GUICtrlSetData($Input13, "异常")
                GUICtrlSetColor($Input13, 0xEE0000)
               
               
        EndIf
EndFunc   ;==>Ping1




代码如下,问题1:WIN10环境下,只是断开网络连接,程序崩溃。
问题2:断开网络连接,按我的思路,执行65 66 67行,但是实际情况只执行了66行,且跳到了69行去执行了,请问问题出在哪。感谢
发表于 2017-4-21 16:19:51 | 显示全部楼层
本帖最后由 229989799 于 2017-4-21 16:28 编辑

win10加入管理权限试试看。
#RequireAdmin-----------------------------
win7下测试拔了网线。win10没环境。。


_Gateway()这个自定义函数已经包含检测到网络不通就提示错误了。。(看102 ,103行)
所以65,66,67行没有必要再写了。。
 楼主| 发表于 2017-4-24 11:38:35 | 显示全部楼层
回复 2# 229989799


    首先感谢你的回复
1。我在代码前加了管理员权限,但是WIN10在不连接网络的情况下,程序依然报错
2。65 66 67行,我的目的是在未获取IP地址的情况下,,弹出提示,并且清除$INPUT5 里面的信息,弹出提示没问题,但是不会清除显示的信息
发表于 2017-4-24 23:09:12 | 显示全部楼层
用32位编译试试,
我的一些程序在win2008下,64位编译运行有问题,32位的没问题
 楼主| 发表于 2017-4-25 10:48:20 | 显示全部楼层
只要网络正常程序运行是没有问题的,,
但是当网络断开后,程序要么就相当卡,要么就直接崩溃。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-28 19:57 , Processed in 0.074449 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表