lon91ong 发表于 2014-4-14 17:59:31

关于_WinAPI_GetTCPTable求教

我在GoogleCode的SVN上几乎翻遍了N种版本的WinAPIEx.au3文件, 都没有找到该函数
麻烦大侠给个能用的WinAPIEx.au3

lon91ong 发表于 2014-4-14 18:22:13

暂时找到了一个不错的替代方案, 原文在此
#include <Array.au3>

Global Enum $TCP_TABLE_BASIC_LISTENER, $TCP_TABLE_BASIC_CONNECTIONS, $TCP_TABLE_BASIC_ALL, $TCP_TABLE_OWNER_PID_LISTENER, $TCP_TABLE_OWNER_PID_CONNECTIONS, _
      $TCP_TABLE_OWNER_PID_ALL, $TCP_TABLE_OWNER_MODULE_LISTENER, $TCP_TABLE_OWNER_MODULE_CONNECTIONS, $TCP_TABLE_OWNER_MODULE_ALL

;    $TCP_TABLE_OWNER_MODULE_... not working for now

Global $aTcpTable = _WinAPI_GetTcpTable()
_ArrayDisplay($aTcpTable, "TCP TABLE", -1, 0, "", "|", "IDX|STATE|STATE DESC|LOCAL IP|LOCAL PORT|REMOTE IP|REMOTE PORT")

Global $aTcpTable_BL = _WinAPI_GetExtendedTcpTable($TCP_TABLE_BASIC_LISTENER)
_ArrayDisplay($aTcpTable_BL, "Extended TCP TABLE - Listening only", -1, 0, "", "|", "IDX|STATE|STATE DESC|LOCAL IP|LOCAL PORT|REMOTE IP|REMOTE PORT")

Global $aTcpTable_PID = _WinAPI_GetExtendedTcpTable($TCP_TABLE_OWNER_PID_ALL)
_ArrayDisplay($aTcpTable_PID, "Extended TCP TABLE - PID included", -1, 0, "", "|", "IDX|STATE|STATE DESC|LOCAL IP|LOCAL PORT|REMOTE IP|REMOTE PORT|PID")


;~ Global $aTcpTable_Module = _WinAPI_GetExtendedTcpTable($TCP_TABLE_OWNER_MODULE_ALL)
;~ _ArrayDisplay($aTcpTable_Module, "Extended TCP TABLE - Module All", -1, 0, "", "|", "IDX|STATE|STATE DESC|LOCAL IP|LOCAL PORT|REMOTE IP|REMOTE PORT|PID|TIMESTAMP")




Func _WinAPI_GetTcpTable()
    ;funkey 2012.12.14
    Local Const $aConnState = ["CLOSED", "LISTENING", "SYN_SENT", "SYN_RCVD", "ESTABLISHED", "FIN_WAIT1", _
            "FIN_WAIT2", "CLOSE_WAIT", "CLOSING", "LAST_ACK", "TIME_WAIT", "DELETE_TCB"]

    Local $tMIB_TCPTABLE = DllStructCreate("dword")
    Local $aRet = DllCall("Iphlpapi.dll", "DWORD", "GetTcpTable", "struct*", $tMIB_TCPTABLE, "DWORD*", 0, "BOOL", True)
    Local $dwSize = $aRet
    $tMIB_TCPTABLE = DllStructCreate("DWORD[" & $dwSize / 4 & "]")

    $aRet = DllCall("Iphlpapi.dll", "DWORD", "GetTcpTable", "struct*", $tMIB_TCPTABLE, "DWORD*", $dwSize, "BOOL", True)
    If $aRet <> 0 Then Return SetError(1)
    Local $iNumEntries = DllStructGetData($tMIB_TCPTABLE, 1, 1)
    Local $aRes[$iNumEntries]

    For $i = 0 To $iNumEntries - 1
      $aRes[$i] = DllStructGetData($tMIB_TCPTABLE, 1, 2 + $i * 5 + 0)
      $aRes[$i] = $aConnState[$aRes[$i] - 1]
      $aRet = DllCall("ws2_32.dll", "str", "inet_ntoa", "uint", DllStructGetData($tMIB_TCPTABLE, 1, 2 + $i * 5 + 1)) ; local IP / translate
      $aRes[$i] = $aRet
      $aRet = DllCall("ws2_32.dll", "ushort", "ntohs", "uint", DllStructGetData($tMIB_TCPTABLE, 1, 2 + $i * 5 + 2)) ; local port / translate
      $aRes[$i] = $aRet
      $aRet = DllCall("ws2_32.dll", "str", "inet_ntoa", "uint", DllStructGetData($tMIB_TCPTABLE, 1, 2 + $i * 5 + 3)) ; remote IP / translate
      $aRes[$i] = $aRet
      If $aRes[$i] <= 2 Then
            $aRes[$i] = 0
      Else
            $aRet = DllCall("ws2_32.dll", "ushort", "ntohs", "uint", DllStructGetData($tMIB_TCPTABLE, 1, 2 + $i * 5 + 4)) ; remote port / translate
            $aRes[$i] = $aRet
      EndIf
    Next

    Return $aRes
EndFunc   ;==>_WinAPI_GetTcpTable


Func _WinAPI_GetExtendedTcpTable($iTableClass)
    ;funkey 2012.12.14
    Local Const $aConnState = ["CLOSED", "LISTENING", "SYN_SENT", "SYN_RCVD", "ESTABLISHED", "FIN_WAIT1", _
            "FIN_WAIT2", "CLOSE_WAIT", "CLOSING", "LAST_ACK", "TIME_WAIT", "DELETE_TCB"]

    Local Const $AF_INET = 2

    Local $tTCPTABLE = 0, $iLoop = 0
    Switch Floor($iTableClass / 3)
      Case 0
            $tTCPTABLE = DllStructCreate("DWORD")
            $iLoop = 5
      Case 1
            $tTCPTABLE = DllStructCreate("DWORD")
            $iLoop = 6
      Case 2
            $tTCPTABLE = DllStructCreate("DWORD;INT64;UINT64")
            $iLoop = 40
    EndSwitch

    Local $aRet = DllCall("Iphlpapi.dll", "DWORD", "GetExtendedTcpTable", "struct*", $tTCPTABLE, "DWORD*", 0, "BOOL", True, "ULONG", $AF_INET, "INT", $iTableClass, "ULONG", 0)
    Local $dwSize = $aRet
    $tTCPTABLE = DllStructCreate("DWORD[" & $dwSize / 4 & "]")
    $aRet = DllCall("Iphlpapi.dll", "DWORD", "GetExtendedTcpTable", "struct*", $tTCPTABLE, "DWORD*", $dwSize, "BOOL", True, "ULONG", $AF_INET, "INT", $iTableClass, "ULONG", 0)
    If $aRet <> 0 Then Return SetError(1)
    Local $iNumEntries = DllStructGetData($tTCPTABLE, 1, 1)
    If $iLoop = 40 Then
      Local $aRes[$iNumEntries]
    Else
      Local $aRes[$iNumEntries][$iLoop + 1]
    EndIf

    Local $iOffset = 2, $tTemp = 0
    If $iLoop = 40 Then $iOffset = 3
    For $i = 0 To $iNumEntries - 1
      $aRes[$i] = DllStructGetData($tTCPTABLE, 1, $iOffset + $i * $iLoop + 0)
      $aRes[$i] = $aConnState[$aRes[$i] - 1]
      $aRet = DllCall("ws2_32.dll", "str", "inet_ntoa", "uint", DllStructGetData($tTCPTABLE, 1, $iOffset + $i * $iLoop + 1)) ; local IP
      $aRes[$i] = $aRet
      $aRet = DllCall("ws2_32.dll", "ushort", "ntohs", "uint", DllStructGetData($tTCPTABLE, 1, $iOffset + $i * $iLoop + 2)) ; local port
      $aRes[$i] = $aRet
      $aRet = DllCall("ws2_32.dll", "str", "inet_ntoa", "uint", DllStructGetData($tTCPTABLE, 1, $iOffset + $i * $iLoop + 3)) ; remote IP
      $aRes[$i] = $aRet
      If $aRes[$i] <= 2 Then
            $aRes[$i] = 0
      Else
            $aRet = DllCall("ws2_32.dll", "ushort", "ntohs", "uint", DllStructGetData($tTCPTABLE, 1, $iOffset + $i * $iLoop + 4)) ; remote port
            $aRes[$i] = $aRet
      EndIf
      If $iLoop = 6 Or $iLoop = 40 Then
            $aRes[$i] = DllStructGetData($tTCPTABLE, 1, $iOffset + $i * $iLoop + 5)
      EndIf
      If $iLoop = 40 Then
            $tTemp = DllStructCreate("word", DllStructGetPtr($tTCPTABLE, 1) + (($iOffset + $i * $iLoop + 6) * 4))
            $aRes[$i] = StringFormat("Date: %i.%i.%i", DllStructGetData($tTemp, 1, 1), DllStructGetData($tTemp, 1, 2), DllStructGetData($tTemp, 1, 4))
      EndIf
    Next

    Return $aRes
EndFunc   ;==>_WinAPI_GetExtendedTcpTable

afan 发表于 2014-4-14 19:37:54

这个包含该函数
页: [1]
查看完整版本: 关于_WinAPI_GetTCPTable求教