#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <file.au3>
Local $DataArray,$Data[9]
$AForm1 = GUICreate("本机开放端口查看", 730, 400, -1, -1)
$Listview1 = GUICtrlCreateListView("协议| 本地主机 |本地端口 | 远程主机 | 远程端口 | 连接状态 |进程", 0, 0, 730, 400)
GUICtrlSendMsg($Listview1, 0x101E, 6, 259)
GUICtrlSendMsg(-1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
$Menu = GUICtrlCreateContextMenu($Listview1)
$Menu1 = GUICtrlCreateMenuItem("刷新", $Menu)
GUISetState(@SW_SHOW)
_ReNetstat()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Menu1
_ReNetstat()
EndSwitch
WEnd
Func _ReNetstat($LoReIP = True, $LoReIP1 = True)
GUICtrlSendMsg($Listview1, $LVM_DeleteALLITEMS, 0, 0)
RunWait(@ComSpec & ' /c netstat -ano > ' & @TempDir & '\NetSOT.tmp', "", @SW_HIDE)
If _FileReadToArray(@TempDir & '\NetSOT.tmp', $DataArray) <> 0 Then
For $I = 5 To $DataArray[0] - 1
$StatColor = 1
$Data[0] = StringReplace(StringMid($DataArray[$I], 1, 9), " ", "")
$Data[1] = StringReplace(StringMid($DataArray[$I], 10, 23), " ", "")
$Prot = StringSplit($Data[1], ":")
If $Prot[0] = 1 Then ExitLoop
$Data[1] = $Prot[1] ;
If $LoReIP = False Then
If $Data[1] = "127.0.0.1" Then ContinueLoop
EndIf
If $LoReIP1 = False Then
If $Data[1] = "0.0.0.0" Then ContinueLoop
EndIf
$Data[2] = $Prot[2] ;
$Data[3] = StringReplace(StringReplace(StringMid($DataArray[$I], 33, 23), " ", ""), "*:*", "0.0.0.0:0");data2
$Prot = StringSplit($Data[3], ":")
If $Prot[0] = 1 Then
$Data[3] = "" ;
$Data[4] = "" ;
Else
$Data[3] = $Prot[1] ;
$Data[4] = $Prot[2] ;
EndIf
$Data[5] = StringReplace(StringMid($DataArray[$I], 56, 16), " ", "")
If $Data[5] = "" Then
$Data[5] = "侦听"
$StatColor = 0
Else
$Data[5] = StringReplace(StringReplace(StringReplace(StringReplace($Data[5], "TIME_WAIT", "连接结束"), "LISTENING", "侦听"), "ESTABLISHED", "已连接"), "CLOSE_WAIT", "正在关闭");data3
EndIf
$Data[6] = StringReplace(StringMid($DataArray[$I], 72, 10), " ", "");data4
$list = ProcessList()
For $J = 1 To $list[0][0]
If $list[$J][1] = $Data[6] Then
$Path = "System"
If $Data[6] = 0 Then
$Path = "System"
Else
If $list[$J][0] <> "System" Then
$Path = _Processpath($Data[6])
If $Path = "" Then $Path = $list[$J][0]
EndIf
EndIf
$DisplayT = GUICtrlCreateListViewItem($Data[0] & "|" & $Data[1] & "|" & $Data[2] & "|" & $Data[3] & "|" & $Data[4] & "|" & $Data[5] & "|" & $Path, $Listview1)
If $StatColor = 1 Then GUICtrlSetBkColor(-1, 0xFFFED8)
EndIf
Next
Next
EndIf
FileDelete(@TempDir & "\NetSOT.tmp")
EndFunc ;==>_ReNetstat
Func _Processpath($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 ;==>_Processpath