Funzione GetTcpStatistics (iphlpapi.h)

La funzione GetTcpStatistics recupera le statistiche TCP per il computer locale.

Sintassi

IPHLPAPI_DLL_LINKAGE ULONG GetTcpStatistics(
  [out] PMIB_TCPSTATS Statistics
);

Parametri

[out] Statistics

Puntatore a una struttura di MIB_TCPSTATS che riceve le statistiche TCP per il computer locale.

Valore restituito

Se la funzione ha esito positivo, il valore restituito è NO_ERROR.

Se la funzione ha esito negativo, il valore restituito è uno dei codici di errore seguenti.

Codice restituito Descrizione
ERROR_INVALID_PARAMETER
Il parametro pStats è NULL o GetTcpStatistics non è in grado di scrivere nella memoria a cui punta il parametro pStats.
Altri
Utilizzare la funzione FormatMessage per ottenere la stringa di messaggio per l'errore restituito.

Commenti

La funzione GetTcpStatistics restituisce le statistiche TCP per IPv4 nel computer corrente. In Windows XP e versioni successive, è possibile usare GetTcpStatisticsEx per ottenere le statistiche TCP per IPv4 o IPv6.

Esempio

Nell'esempio seguente vengono recuperate le statistiche TCP per il computer locale e vengono stampati alcuni valori dai dati restituiti.

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

#pragma comment(lib, "iphlpapi.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) 
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

/* Note: could also use malloc() and free() */

int main()
{
    PMIB_TCPSTATS pTCPStats;
    DWORD dwRetVal = 0;

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

    if ((dwRetVal = GetTcpStatistics(pTCPStats)) == NO_ERROR) {
      printf("\tActive Opens: %ld\n", pTCPStats->dwActiveOpens);
      printf("\tPassive Opens: %ld\n", pTCPStats->dwPassiveOpens);
      printf("\tSegments Recv: %ld\n", pTCPStats->dwInSegs);
      printf("\tSegments Xmit: %ld\n", pTCPStats->dwOutSegs);
      printf("\tTotal # Conxs: %ld\n", pTCPStats->dwNumConns);
    }
    else {
      printf("GetTcpStatistics failed with error: %ld\n", dwRetVal);
      
      LPVOID lpMsgBuf;
      if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM | 
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dwRetVal,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPTSTR) &lpMsgBuf,
        0,
        NULL )) {
        printf("\tError: %s", lpMsgBuf);
      }
      LocalFree( lpMsgBuf );
    }

    if (pTCPStats)
        FREE (pTCPStats);
}

Requisiti

   
Client minimo supportato Windows 2000 Professional [solo app desktop]
Server minimo supportato Windows 2000 Server [solo app desktop]
Piattaforma di destinazione Windows
Intestazione iphlpapi.h
Libreria Iphlpapi.lib
DLL Iphlpapi.dll

Vedi anche

GetIcmpStatistics

GetIpStatistics

GetTcpStatisticsEx

GetUdpStatistics

Informazioni di riferimento sulla funzione helper IP

Pagina iniziale dell'helper IP

MIB_TCPSTATS