Función GetTcp6Table (iphlpapi.h)

La función GetTcp6Table recupera la tabla de conexión TCP para IPv6.

Sintaxis

IPHLPAPI_DLL_LINKAGE ULONG GetTcp6Table(
  [out]     PMIB_TCP6TABLE TcpTable,
  [in, out] PULONG         SizePointer,
  [in]      BOOL           Order
);

Parámetros

[out] TcpTable

Puntero a un búfer que recibe la tabla de conexión TCP para IPv6 como una estructura de MIB_TCP6TABLE .

[in, out] SizePointer

En la entrada, especifica el tamaño en bytes del búfer al que apunta el parámetro TcpTable .

En la salida, si el búfer no es lo suficientemente grande como para contener la tabla de conexión TCP devuelta, la función establece este parámetro igual al tamaño de búfer necesario en bytes.

[in] Order

Valor booleano que especifica si se debe ordenar la tabla de conexión TCP. Si este parámetro es TRUE, la tabla se ordena en orden ascendente, empezando por la dirección IP local más baja. Si este parámetro es FALSE, la tabla aparece en el orden en que se recuperaron.

Los valores siguientes se comparan (como se enumeran) al ordenar los puntos de conexión TCP:

  1. Dirección IPv6 local
  2. Identificador de ámbito local
  3. Puerto local
  4. Dirección IPv6 remota
  5. Identificador de ámbito remoto
  6. Puerto remoto

Valor devuelto

Si la función se realiza correctamente, el valor devuelto es NO_ERROR.

Si se produce un error en la función, el valor devuelto es uno de los siguientes códigos de error.

Código devuelto Descripción
ERROR_INSUFFICIENT_BUFFER
El búfer al que apunta el parámetro TcpTable no es lo suficientemente grande. El tamaño necesario se devuelve en la variable a la que apunta el parámetro SizePointer .
ERROR_INVALID_PARAMETER
El parámetro SizePointer es NULL o GetTcp6Table no puede escribir en la memoria a la que apunta el parámetro SizePointer .
ERROR_NOT_SUPPORTED
Esta función no se admite en el sistema operativo en uso en el sistema local.
Otros
Use FormatMessage para obtener la cadena de mensaje del error devuelto.

Comentarios

La función GetTcp6Table se define en Windows Vista y versiones posteriores.

Ejemplos

En el ejemplo siguiente se recupera la tabla de conexión TCP para IPv6 y se imprime el estado de cada conexión.

#ifndef UNICODE
#define UNICODE
#endif

#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 wmain()
{

    // Declare and initialize variables
    PMIB_TCP6TABLE pTcpTable;
    DWORD dwSize = 0;
    DWORD dwRetVal = 0;

    wchar_t ipstringbuffer[46];
    
    int i;

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

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

            if (InetNtop(AF_INET6, &pTcpTable->table[i].LocalAddr, ipstringbuffer, 46) == NULL)
                wprintf(L"  InetNtop function failed for local IPv6 address\n");
            else     
                wprintf(L"\tTCP[%d] Local Addr: %s\n", i, ipstringbuffer);
            wprintf(L"\tTCP[%d] Local Scope ID: %d \n", i,
                   ntohl (pTcpTable->table[i].dwLocalScopeId));
            wprintf(L"\tTCP[%d] Local Port: %d \n", i,
                   ntohs((u_short)pTcpTable->table[i].dwLocalPort));

            if (InetNtop(AF_INET6, &pTcpTable->table[i].RemoteAddr, ipstringbuffer, 46) == NULL)
                wprintf(L"  InetNtop function failed for remote IPv6 address\n");
            else     
                wprintf(L"\tTCP[%d] Remote Addr: %s\n", i, ipstringbuffer);
            wprintf(L"\tTCP[%d] Remote Scope ID: %d \n", i,
                   ntohl(pTcpTable->table[i].dwRemoteScopeId));
            wprintf(L"\tTCP[%d] Remote Port: %d\n", i,
                   ntohs((u_short)pTcpTable->table[i].dwRemotePort));
        }
    } else {
        wprintf(L"\tGetTcp6Table failed with %d\n", dwRetVal);
        FREE(pTcpTable);
        return 1;
    }

    if (pTcpTable != NULL) {
        FREE(pTcpTable);
        pTcpTable = NULL;
    }
    
    return 0;    
}

Requisitos

   
Cliente mínimo compatible Windows Vista [solo aplicaciones de escritorio]
Servidor mínimo compatible Windows Server 2008 [solo aplicaciones de escritorio]
Plataforma de destino Windows
Encabezado iphlpapi.h
Library Iphlpapi.lib
Archivo DLL Iphlpapi.dll

Consulte también

GetExtendedTcpTable

GetOwnerModuleFromTcp6Entry

GetTcp6Table2

GetTcpStatisticsEx

GetTcpTable2

MIB_TCP6ROW

MIB_TCP6ROW_OWNER_MODULE

MIB_TCP6ROW_OWNER_PID

MIB_TCP6TABLE

MIB_TCP6TABLE_OWNER_MODULE

MIB_TCP6TABLE_OWNER_PID