GetTcpTable2-Funktion (iphlpapi.h)

Die GetTcpTable2-Funktion ruft die IPv4-TCP-Verbindungstabelle ab.

Syntax

IPHLPAPI_DLL_LINKAGE ULONG GetTcpTable2(
  [out]     PMIB_TCPTABLE2 TcpTable,
  [in, out] PULONG         SizePointer,
  [in]      BOOL           Order
);

Parameter

[out] TcpTable

Ein Zeiger auf einen Puffer, der die TCP-Verbindungstabelle als MIB_TCPTABLE2-Struktur empfängt.

[in, out] SizePointer

Gibt bei der Eingabe die Größe des Puffers an, auf den der TcpTable-Parameter verweist.

Wenn der Puffer bei der Ausgabe nicht groß genug ist, um die zurückgegebene Verbindungstabelle zu enthalten, legt die Funktion diesen Parameter gleich der erforderlichen Puffergröße fest.

[in] Order

Ein -Wert, der angibt, ob die TCP-Verbindungstabelle sortiert werden soll. Wenn dieser Parameter TRUE ist, wird die Tabelle in der Reihenfolge sortiert:

  1. Lokale IP-Adresse
  2. Lokaler Port
  3. Remote-IP-Adresse
  4. Remoteport

Rückgabewert

Wenn die Funktion erfolgreich ist, wird der Rückgabewert NO_ERROR.

Wenn die Funktion fehlschlägt, ist der Rückgabewert einer der folgenden Fehlercodes.

Rückgabecode Beschreibung
ERROR_INSUFFICIENT_BUFFER
Der Puffer, auf den der TcpTable-Parameter verweist, ist nicht groß genug. Die erforderliche Größe wird in der PULONG-Variablen zurückgegeben, auf die der SizePointer-Parameter verweist.

Dieser Fehler wird auch zurückgegeben, wenn der pTcpTable-ParameterNULL ist.

ERROR_INVALID_PARAMETER
Der SizePointer-Parameter ist NULL, oder GetTcpTable2 kann nicht in den Speicher schreiben, auf den der SizePointer-Parameter verweist.
ERROR_NOT_SUPPORTED
Diese Funktion wird auf dem Betriebssystem, das auf dem lokalen System verwendet wird, nicht unterstützt.
Andere
Verwenden Sie FormatMessage , um die Meldungszeichenfolge für den zurückgegebenen Fehler abzurufen.

Hinweise

Die GetTcpTable2-Funktion ist unter Windows Vista und höher definiert.

Die GetTcpTable2-Funktion ist eine erweiterte Version der GetTcpTable-Funktion , die auch Informationen zum TCP-Abladezustand der TCP-Verbindung abruft.

Beispiele

Im folgenden Beispiel wird die TCP-Verbindungstabelle für IPv4 abgerufen und der Status jeder Verbindung ausgegeben.

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>

// Need to link with Iphlpapi.lib and Ws2_32.lib
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */

int main()
{

    // Declare and initialize variables
    PMIB_TCPTABLE2 pTcpTable;
    ULONG ulSize = 0;
    DWORD dwRetVal = 0;

    char szLocalAddr[128];
    char szRemoteAddr[128];

    struct in_addr IpAddr;

    int i;

    pTcpTable = (MIB_TCPTABLE2 *) MALLOC(sizeof (MIB_TCPTABLE2));
    if (pTcpTable == NULL) {
        printf("Error allocating memory\n");
        return 1;
    }

    ulSize = sizeof (MIB_TCPTABLE);
// Make an initial call to GetTcpTable2 to
// get the necessary size into the ulSize variable
    if ((dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE)) ==
        ERROR_INSUFFICIENT_BUFFER) {
        FREE(pTcpTable);
        pTcpTable = (MIB_TCPTABLE2 *) MALLOC(ulSize);
        if (pTcpTable == NULL) {
            printf("Error allocating memory\n");
            return 1;
        }
    }
// Make a second call to GetTcpTable2 to get
// the actual data we require
    if ((dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE)) == NO_ERROR) {
        printf("\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries);
        for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
            printf("\n\tTCP[%d] State: %ld - ", i,
                   pTcpTable->table[i].dwState);
            switch (pTcpTable->table[i].dwState) {
            case MIB_TCP_STATE_CLOSED:
                printf("CLOSED\n");
                break;
            case MIB_TCP_STATE_LISTEN:
                printf("LISTEN\n");
                break;
            case MIB_TCP_STATE_SYN_SENT:
                printf("SYN-SENT\n");
                break;
            case MIB_TCP_STATE_SYN_RCVD:
                printf("SYN-RECEIVED\n");
                break;
            case MIB_TCP_STATE_ESTAB:
                printf("ESTABLISHED\n");
                break;
            case MIB_TCP_STATE_FIN_WAIT1:
                printf("FIN-WAIT-1\n");
                break;
            case MIB_TCP_STATE_FIN_WAIT2:
                printf("FIN-WAIT-2 \n");
                break;
            case MIB_TCP_STATE_CLOSE_WAIT:
                printf("CLOSE-WAIT\n");
                break;
            case MIB_TCP_STATE_CLOSING:
                printf("CLOSING\n");
                break;
            case MIB_TCP_STATE_LAST_ACK:
                printf("LAST-ACK\n");
                break;
            case MIB_TCP_STATE_TIME_WAIT:
                printf("TIME-WAIT\n");
                break;
            case MIB_TCP_STATE_DELETE_TCB:
                printf("DELETE-TCB\n");
                break;
            default:
                printf("UNKNOWN dwState value\n");
                break;
            }
            IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
            strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));
            printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);
            printf("\tTCP[%d] Local Port: %d \n", i,
                   ntohs((u_short)pTcpTable->table[i].dwLocalPort));

            IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
            strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
            printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr);
            printf("\tTCP[%d] Remote Port: %d\n", i,
                   ntohs((u_short)pTcpTable->table[i].dwRemotePort));
                   
            printf("\tTCP[%d] Owning PID: %d\n", i, pTcpTable->table[i].dwOwningPid);
            printf("\tTCP[%d] Offload State: %ld - ", i,
                   pTcpTable->table[i].dwOffloadState);
            switch (pTcpTable->table[i].dwOffloadState) {
            case TcpConnectionOffloadStateInHost:
                printf("Owned by the network stack and not offloaded \n");
                break;
            case TcpConnectionOffloadStateOffloading:
                printf("In the process of being offloaded\n");
                break;
            case TcpConnectionOffloadStateOffloaded:
                printf("Offloaded to the network interface control\n");
                break;
            case TcpConnectionOffloadStateUploading:
                printf("In the process of being uploaded back to the network stack \n");
                break;
            default:
                printf("UNKNOWN Offload state value\n");
                break;
            }
                   
        }
    } else {
        printf("\tGetTcpTable2 failed with %d\n", dwRetVal);
        FREE(pTcpTable);
        return 1;
    }
    
    if (pTcpTable != NULL) {
        FREE(pTcpTable);
        pTcpTable = NULL;
    }

    return 0;    
}

Anforderungen

Anforderung Wert
Unterstützte Mindestversion (Client) Windows Vista [nur Desktop-Apps]
Unterstützte Mindestversion (Server) Windows Server 2008 [nur Desktop-Apps]
Zielplattform Windows
Kopfzeile iphlpapi.h
Bibliothek Iphlpapi.lib
DLL Iphlpapi.dll

Weitere Informationen

GetExtendedTcpTable

GetOwnerModuleFromTcpEntry

GetTcp6Table

GetTcpStatistics

GetTcpStatisticsEx

GetTcpTable

MIB_TCP6ROW2

MIB_TCP6TABLE

MIB_TCP6TABLE2

MIB_TCP6TABLE_OWNER_MODULE

MIB_TCP6TABLE_OWNER_PID

MIB_TCPROW

MIB_TCPROW2

MIB_TCPROW_OWNER_MODULE

MIB_TCPROW_OWNER_PID

MIB_TCPTABLE

MIB_TCPTABLE2

MIB_TCPTABLE_OWNER_MODULE

MIB_TCPTABLE_OWNER_PID

SetTcpEntry