Microsoft Learn

Documentation for developers

GetIpNetTable function

Note For Windows Vista and later, use the GetIpNetTable2 function to retrieve IP routing table entries.

The GetIpNetTable function retrieves the IP routing table.

IPHLPAPI.H


Syntax


PMIB_IPNETTABLE GetIpNetTable(
  [out]     PMIB_IPNETTABLE pIpNetTable,
  [in, out] PULONG          pSize,
  [in]      BOOL            bOrder
);
            

Parameters

pIpNetTable

A pointer to a buffer that receives the IP routing table as a MIB_IPNETTABLE structure.

pSize

On input, the size in bytes of the buffer pointed to by pIpNetTable.

On output, if the buffer is not large enough to hold the returned routing table, the function sets this parameter to the required buffer size in bytes.

bOrder

A boolean value that specifies whether the routing table entries should be sorted. If this parameter is TRUE, the entries are sorted.

Return value

If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is one of the following error codes.

Return code Description
ERROR_INSUFFICIENT_BUFFER The buffer pointed to by pIpNetTable is not large enough to hold the routing table. The required buffer size is returned in the pSize parameter.
ERROR_INVALID_PARAMETER One or more parameters are invalid.
ERROR_INVALID_USER_BUFFER The user buffer is not valid.
ERROR_NOT_FOUND The routing table cannot be retrieved.

Remarks

The GetIpNetTable function is used to retrieve the IP routing table. The table contains information about the routes used by the system to forward IP packets. Each entry in the table represents a route to a specific destination network or host.

The routing table is returned as a MIB_IPNETTABLE structure, which contains an array of MIB_IPNETROW structures. Each MIB_IPNETROW structure describes a single entry in the IP routing table.

It is recommended to use the GetIpNetTable2 function on Windows Vista and later versions of Windows for retrieving IP routing table entries, as it provides more detailed information and better performance.

Tip Always allocate a sufficiently large buffer for pIpNetTable to avoid the ERROR_INSUFFICIENT_BUFFER error. You can call the function once with a NULL buffer to get the required size.

Example

The following example demonstrates how to retrieve the IP routing table and print the entries.


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

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

int main() {
    PMIB_IPNETTABLE pIpNetTable = NULL;
    ULONG ulSize = 0;
    DWORD dwRetVal = 0;

    // Allocate buffer for IP routing table
    dwRetVal = GetIpNetTable(NULL, &ulSize, TRUE);

    if (dwRetVal == ERROR_INSUFFICIENT_BUFFER) {
        pIpNetTable = (PMIB_IPNETTABLE)malloc(ulSize);
        if (pIpNetTable == NULL) {
            printf("Error allocating memory.\n");
            return 1;
        }
    } else {
        printf("Error getting IP routing table size: %d\n", dwRetVal);
        return 1;
    }

    // Retrieve the IP routing table
    dwRetVal = GetIpNetTable(pIpNetTable, &ulSize, TRUE);

    if (dwRetVal == NO_ERROR) {
        printf("Successfully retrieved IP routing table.\n");
        printf("Number of entries: %d\n", pIpNetTable->dwNumEntries);

        for (DWORD i = 0; i < pIpNetTable->dwNumEntries; i++) {
            printf("\nEntry %d:\n", i + 1);
            printf("  Destination Address: %s\n", inet_ntoa(pIpNetTable->table[i].dwDestination));
            printf("  Netmask: %s\n", inet_ntoa(pIpNetTable->table[i].dwNetmask));
            printf("  Gateway Address: %s\n", inet_ntoa(pIpNetTable->table[i].dwNextHop));
            printf("  Interface Index: %d\n", pIpNetTable->table[i].dwIndex);
            printf("  Protocol: %d\n", pIpNetTable->table[i].dwProtocol);
            printf("  Type: %d\n", pIpNetTable->table[i].dwType);
            printf("  State: %d\n", pIpNetTable->table[i].dwState);
        }
    } else {
        printf("Error retrieving IP routing table: %d\n", dwRetVal);
    }

    if (pIpNetTable != NULL) {
        free(pIpNetTable);
        pIpNetTable = NULL;
    }

    return 0;
}
            

Requirements

Tear Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Reimplemented in Windows 2000
Header Iphlpapi.h
Library Iphlpapi.lib
DLL Iphlpapi.dll