Func _GetTcpTable()
Local $aCall = DllCall($hIPHLPAPI, "dword", "GetTcpTable", _
"ptr*", 0, _
"dword*", 0, _
"int", 1)
If @error Then
Return SetError(1, 0, 0)
EndIf
If $aCall[0] <> 122 Then ; ERROR_INSUFFICIENT_BUFFER
Return SetError(2, 0, 0)
EndIf
Local $iSize = $aCall[2]
Local $tByteStructure = DllStructCreate("byte[" & $iSize & "]")
$aCall = DllCall($hIPHLPAPI, "dword", "GetTcpTable", _
"ptr", DllStructGetPtr($tByteStructure), _
"dword*", $iSize, _
"int", 1)
If @error Or $aCall[0] Then
Return SetError(3, 0, 0)
EndIf
Local $tMIB_TCPTABLE_DWORDS = DllStructCreate("dword[" & Ceiling($iSize / 4) & "]", DllStructGetPtr($tByteStructure))
Local $iTCPentries = DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, 1) ; number of entries
Local $aTCPTable[$iTCPentries + 1][5]
$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"
#cs
$tMIB_TCPROW = DllStructCreate("dword State;" & _
"dword LocalAddr;" & _
"dword LocalPort;" & _
"dword RemoteAddr;" & _
"dword RemotePort")
#ce
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) * 5 + 1 ; going thru array of dwords
$aTCPTable[$i][0] = $aState[DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, $iOffset + 1) - 1] ; connection state text
$iIP = DllStructGetData($tMIB_TCPTABLE_DWORDS, 1, $iOffset + 2)
If $iIP = 16777343 Then
$aTCPTable[$i][1] = "localhost (127.0.0.1)"
ElseIf $iIP = 0 Then
$aTCPTable[$i][1] = "Any local address"
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] = _IpToName($iIP) & " (" & $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
Next
TCPShutdown()
Return $aTCPTable
EndFunc ;==>_GetTcpTable
Func _GetUdpTable()
Local $aCall = DllCall($hIPHLPAPI, "dword", "GetUdpTable", _
"ptr*", 0, _
"dword*", 0, _
"int", 1)
If @error Then
Return SetError(1, 0, 0)
EndIf
If $aCall[0] <> 122 Then ; ERROR_INSUFFICIENT_BUFFER
Return SetError(2, 0, 0)
EndIf
Local $iSize = $aCall[2]
Local $tByteStructure = DllStructCreate("byte[" & $iSize & "]")
$aCall = DllCall($hIPHLPAPI, "dword", "GetUdpTable", _
"ptr", DllStructGetPtr($tByteStructure), _
"dword*", $iSize, _
"int", 1)
If @error Or $aCall[0] Then
Return SetError(3, 0, 0)
EndIf
Local $tMIB_UDPTABLE_DWORDS = DllStructCreate("dword[" & Ceiling($iSize / 4) & "]", DllStructGetPtr($tByteStructure))
Local $iUDPentries = DllStructGetData($tMIB_UDPTABLE_DWORDS, 1, 1) ; number of entries
Local $aTCPTable[$iUDPentries + 1][2]
$aTCPTable[0][0] = "Local IP "
$aTCPTable[0][1] = "Local Port"
#cs
$tMIB_UDPROW = DllStructCreate("dword LocalAddr;" & _
"dword LocalPort")
#ce
Local $iOffset
Local $iIP
UDPStartup()
For $i = 1 To $iUDPentries
$iOffset = ($i - 1) * 2 + 1 ; going thru array of dwords
$iIP = DllStructGetData($tMIB_UDPTABLE_DWORDS, 1, $iOffset + 1)
If $iIP = 16777343 Then
$aTCPTable[$i][0] = "localhost (127.0.0.1)"
ElseIf $iIP = 0 Then
$aTCPTable[$i][0] = "Any local address"
Else
$aTCPTable[$i][0] = 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][0] = _IpToName($iIP) & " (" & $aTCPTable[$i][0] & ")"
EndIf
$aTCPTable[$i][1] = Dec(Hex(BinaryMid(DllStructGetData($tMIB_UDPTABLE_DWORDS, 1, $iOffset + 2), 1, 2)))
Next
UDPShutdown()
Return $aTCPTable
EndFunc ;==>_GetUdpTable