Func _GetExtendedTcpTable()
Local $aCall = DllCall($hIPHLPAPI, "dword", "GetExtendedTcpTable", _
"ptr*", 0, _
"dword*", 0, _
"int", 1, _
"dword", 2, _
"dword", 5, _
"dword", 0)
If @error Then
Return SetError(1, 0, 0)
EndIf
If $aCall[0] <> 122 Then
Return SetError(2, 0, 0)
EndIf
Local $iSize = $aCall[2]
Local $tByteStructure = DllStructCreate("byte[" & $iSize & "]")
$aCall = DllCall($hIPHLPAPI, "dword", "GetExtendedTcpTable", _
"ptr", DllStructGetPtr($tByteStructure), _
"dword*", $iSize, _
"int", 1, _
"dword", 2, _
"dword", 5, _
"dword", 0)
If @error Or $aCall[0] Then
Return SetError(3, 0, 0)
EndIf
Local $tMIB_TCPTABLE_OWNER_PID_DWORDS = DllStructCreate("dword[" & Ceiling($iSize / 4) & "]", DllStructGetPtr($tByteStructure))
Local $iTCPentries = DllStructGetData($tMIB_TCPTABLE_OWNER_PID_DWORDS, 1)
Local $tMIB_TCPTABLE_DWORDS = DllStructCreate("dword[" & Ceiling($iSize / 4) & "]", DllStructGetPtr($tByteStructure))
Local $aTCPTable[$iTCPentries + 1][7]
$aTCPTable[0][0] = "Connection state"
$aTCPTable[0][1] = "Local IP"
$aTCPTable[0][2] = "Local Port"
$aTCPTable[0][3] = "Remote IP"
$aTCPTable[0][4] = "Remote port"
$aTCPTable[0][5] = "PID"
$aTCPTable[0][6] = "Process Name"
Local $aProcesses = ProcessList()
Local $aState[12] = ["CLOSED", "LISTENING", "SYN_SENT", "SYN_RCVD", "ESTABLISHED", "FIN_WAIT1", "FIN_WAIT2", "CLOSE_WAIT", "CLOSING", "LAST_ACK", "TIME_WAIT", "DELETE_TCB"]
Local $iOffset
Local $iIP
TCPStartup()
For $i = 1 To $iTCPentries
$iOffset = ($i - 1) * 6 + 1
$aTCPTable[$i][0] = $aState[DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, $iOffset + 1) - 1]
$iIP = DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, $iOffset + 2)
If $iIP = 16777343 Then
$aTCPTable[$i][1] = "127.0.0.1"
ElseIf $iIP = 0 Then
$aTCPTable[$i][1] = @IPAddress1
Else
$aTCPTable[$i][1] = BitOR(BinaryMid($iIP, 1, 1), 0) & "." & BitOR(BinaryMid($iIP, 2, 1), 0) & "." & BitOR(BinaryMid($iIP, 3, 1), 0) & "." & BitOR(BinaryMid($iIP, 4, 1), 0)
$aTCPTable[$i][1] = $aTCPTable[$i][1]
EndIf
$aTCPTable[$i][2] = Dec(Hex(BinaryMid(DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, $iOffset + 3), 1, 2)))
If DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, $iOffset + 1) < 3 Then
$aTCPTable[$i][4] = "-"
$aTCPTable[$i][3] = "-"
Else
$iIP = DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, $iOffset + 4)
$aTCPTable[$i][3] = BitOR(BinaryMid($iIP, 1, 1), 0) & "." & BitOR(BinaryMid($iIP, 2, 1), 0) & "." & BitOR(BinaryMid($iIP, 3, 1), 0) & "." & BitOR(BinaryMid($iIP, 4, 1), 0)
$aTCPTable[$i][4] = Dec(Hex(BinaryMid(DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, $iOffset + 5), 1, 2)))
EndIf
$aTCPTable[$I][5] = DllStructGetData($tMIB_TCPTABLE_OWNER_PID_DWORDS, 1, $iOffset + 6)
If Not $aTCPTable[$I][5] Then
$aTCPTable[$I][6] = "System Idle Process"
Else
For $J = 1 To $aProcesses[0][0]
If $aProcesses[$J][1] = $aTCPTable[$I][5] Then
$aTCPTable[$I][6] = $aProcesses[$J][0]
If Not $aTCPTable[$I][6] Then $aTCPTable[$I][6] = $aProcesses[$J][0]
ExitLoop
EndIf
Next
EndIf
Next
TCPShutdown()
Return $aTCPTable
EndFunc