|
发表于 2014-10-31 08:57:42
|
显示全部楼层
[au3]; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_GetTcpTable
; Description....: retrieves the IPv4 TCP connection table.
; Syntax.........: _WinAPI_GetTCPtable( [optional handle to "ws2_32.dll" [, optional handle to "iphlpapi.dll" ] ] )
; Parameters.....:
; Return values..: Success: TCPtable[][] = 2-D array
; [0][0] = number of connections for connection n:
; [n][0] = connection state (integer)
; [n][1] = local IP
; [n][2] = local port
; [n][3] = remote IP
; [n][4] = remote port
; [n][5] = connection state (informative text)
; Failure: TCPtable[0][0] = -1
; Author.........:
; Modified.......:
; Remarks........:
; Related........:
; Link...........: @@MsdnLink@@ GetTcpTable, MIB_TCPTABLE
; Example........: Yes
; ===============================================================================================================================
Func _WinAPI_GetTcpTable()
Local $TCPtable[1][1] = [[-1]] ; preset to "failed"
Local $dwSize = DllStructCreate("dword") ; for MIB_TCPTABLE buffer size
Local $MIB_TCPTABLE = DllStructCreate("dword") ; nominal struct initially
DllStructSetData($dwSize, 1, 0) ; force zero size
Local $Ret = DllCall("iphlpapi.dll", "int", "GetTcpTable", "ptr", DllStructGetPtr($MIB_TCPTABLE), "ptr", DllStructGetPtr($dwSize), "int", 1) ; get size
If @error Or $Ret[0] <> 122 Then Return $TCPtable ; dllCall error or RC is *not* ERROR_INSUFFICIENT_BUFFER = 122
$MIB_TCPTABLE = ""
For $i = 1 To DllStructGetData($dwSize, 1) / 4 ; make to requested size of buffer
$MIB_TCPTABLE &= "dword;"
Next
$MIB_TCPTABLE = DllStructCreate(StringTrimRight($MIB_TCPTABLE, 1)) ; requested struct
DllStructSetData($dwSize, 1, DllStructGetSize($MIB_TCPTABLE)) ; recheck its size
$Ret = DllCall("iphlpapi.dll", "int", "GetTcpTable", "ptr", DllStructGetPtr($MIB_TCPTABLE), "ptr", DllStructGetPtr($dwSize), "int", 1) ; get data
If @error Or $Ret[0] Then Return $TCPtable ; dllCall error or RC is Error
Local $numTCPentries = DllStructGetData($MIB_TCPTABLE, 1) ; number of entries
ReDim $TCPtable[$numTCPentries + 1][6]
For $i = 1 To $numTCPentries
Local $Offset = ($i - 1) * 5 + 1 ; dword offset into struct
$TCPtable[$i][0] = DllStructGetData($MIB_TCPTABLE, $Offset + 1) ; integer connection state
$Ret = DllCall("ws2_32.dll", "str", "inet_ntoa", "uint", DllStructGetData($MIB_TCPTABLE, $Offset + 2)) ; local IP / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i][1] = $Ret[0]
$Ret = DllCall("ws2_32.dll", "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $Offset + 3)) ; local port / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i][2] = $Ret[0]
If $TCPtable[$i][0] <= 2 Then ; CLOSED or LISTENING state
$TCPtable[$i][3] = "0.0.0.0"
$TCPtable[$i][4] = 0
Else
$Ret = DllCall("ws2_32.dll", "str", "inet_ntoa", "uint", DllStructGetData($MIB_TCPTABLE, $Offset + 4)) ; remote IP / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i][3] = $Ret[0]
$Ret = DllCall("ws2_32.dll", "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $Offset + 5)) ; remote port / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i][4] = $Ret[0]
EndIf
Next
$dwSize = 0
$MIB_TCPTABLE = 0
$TCPtable[0][0] = $numTCPentries ; success
Return $TCPtable
EndFunc ;==>_WinAPI_GetTcpTable
[/au3] |
|