#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
#include <file.au3>

Global $DataArray,$Data[9]
Global $TCPFORM_1,$TCPList_1,$TCPmenu1,$TCPmenuC1,$TCPmenuC2

Func _NetTcp()
$TCPFORM_1 = GUICreate("网络连接查看", 698, 377, -1, -1, -1, -1, $window)
$TCPList_1 = GUICtrlCreateListView("", 0, 0, 698, 377)
_GUICtrlListView_SetExtendedListViewStyle(-1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSelect, $LVS_EX_SUBITEMIMAGES))
_GUICtrlListView_AddColumn(-1,"进程",100)
_GUICtrlListView_AddColumn(-1,"Pid",50)
_GUICtrlListView_AddColumn(-1,"协议",50)
_GUICtrlListView_AddColumn(-1,"本地端口",115)
_GUICtrlListView_AddColumn(-1,"远程端口",100)
_GUICtrlListView_AddColumn(-1,"连接状态",80)
_GUICtrlListView_AddColumn(-1,"程序路径",130)
GUICtrlSetImage($TCPList_1, "shell32.dll", -3)
$TCPmenu1 = GUICtrlCreateContextMenu($TCPList_1)
$TCPmenuC1 = GUICtrlCreateMenuItem("刷新列表", $TCPmenu1)
$TCPmenuC2 = GUICtrlCreateMenuItem("结束进程", $TCPmenu1)
GUISetState(@SW_SHOW)

_read()

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			GUIDelete($TCPFORM_1)
			ExitLoop
		Case $TCPmenuC1
			_read()
		Case $TCPmenuC2
			$TCPdu = GUICtrlRead(GUICtrlRead($TCPList_1))
			$TCPdu_1 = StringSplit($TCPdu,"|")
			If GUICtrlRead($TCPList_1) <> 0 Then
				ProcessClose($TCPdu_1[1])
				_read()
			EndIf			

	EndSwitch
WEnd
EndFunc

Func _read()
    GUICtrlSendMsg($TCPList_1, $LVM_DeleteALLITEMS, 0, 0)
    _ReNet()
EndFunc


Func _ReNet($LoReIP = True, $LoReIP1 = True)
    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), " ", ""), "*:*", "*:*");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", "TIME_WAIT"), "LISTENING", "LISTENING"), "ESTABLISHED", "ESTABLISHED"), "CLOSE_WAIT", "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($list[$j][0] & "|" &$list[$j][1] & "|" & $Data[0] & "|" & $Data[1]&":"&$Data[2] & "|" & $Data[3] &":"&$Data[4]& "|" & $Data[5] & "|" & $Path, $TCPList_1)
					GUICtrlSetImage($DisplayT, $Path, 0)
                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