Windows API Reference

Microsoft Developer Network

IP Helper API (IPHlpAPI)

The IP Helper API (IPHlpAPI) is a set of functions that provide access to information about the TCP/IP protocol stack and network adapters on a local computer. This API allows you to retrieve and modify network configuration settings, manage routing tables, and obtain information about network interfaces, IP addresses, ARP caches, and more.

Key Functionality

  • Retrieving information about network adapters (e.g., name, description, MAC address).
  • Getting and setting IP addresses, subnet masks, and default gateways.
  • Managing the ARP cache.
  • Accessing and modifying the routing table.
  • Obtaining information about TCP and UDP connections.
  • Monitoring network statistics.

Core Concepts

Understanding the following concepts is crucial when working with the IP Helper API:

  • Network Adapters: Physical or virtual network interfaces on the system.
  • IP Addresses: Logical addresses assigned to network interfaces.
  • Subnet Masks: Used to divide an IP address into network and host portions.
  • Default Gateway: The router used to forward packets to other networks.
  • ARP Cache: Stores mappings between IP addresses and MAC addresses.
  • Routing Table: Contains information about how to route network traffic.

Common Use Cases

  • Developing network diagnostic tools.
  • Implementing custom network monitoring solutions.
  • Automating network configuration tasks.
  • Gathering network performance data.

Key Functions and Structures

The following are some of the most frequently used functions and data structures within the IP Helper API:

Functions

Structures

  • IP_ADAPTER_INFO: Contains information about a network adapter.
  • MIB_IPADDRROW: Represents an entry in the IP address table.
  • MIB_IPFORWARDROW: Represents an entry in the routing table.
  • MIB_TCPROW: Represents a TCP connection entry.

Note: The IP Helper API is primarily a C-based API and is suitable for use in C, C++, and other languages that can interoperate with Win32 DLLs.

Getting Started with IPHlpAPI

To use the IP Helper API, you typically need to include the iphlpapi.h header file and link against the iphlpapi.lib library.

Example: Retrieving Adapter Information


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

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

int main() {
    IP_ADAPTER_INFO* pAdapterInfo;
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);

    // Allocate memory for the adapter information
    pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));

    if (pAdapterInfo == NULL) {
        printf("Error allocating memory\n");
        return 1;
    }

    // Call GetAdaptersInfo to get adapter information
    if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
        // Buffer was too small, reallocate
        free(pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen);
        if (pAdapterInfo == NULL) {
            printf("Error allocating memory for expanded buffer\n");
            return 1;
        }
    }

    if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR) {
        IP_ADAPTER_INFO* pAdapter = pAdapterInfo;
        while (pAdapter) {
            printf("Adapter Name: %s\n", pAdapter->AdapterName);
            printf("  Description: %s\n", pAdapter->Description);
            printf("  MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
                   pAdapter->Address[0], pAdapter->Address[1], pAdapter->Address[2],
                   pAdapter->Address[3], pAdapter->Address[4], pAdapter->Address[5]);
            printf("  IP Address: %s\n", pAdapter->IpAddressList.IpAddress.String);
            printf("  Subnet Mask: %s\n", pAdapter->IpAddressList.SubnetMask.String);
            printf("  Default Gateway: %s\n", pAdapter->GatewayList.IpAddress.String);
            printf("\n");
            pAdapter = pAdapter->Next;
        }
    } else {
        printf("Error calling GetAdaptersInfo\n");
    }

    // Free allocated memory
    if (pAdapterInfo) {
        free(pAdapterInfo);
    }

    return 0;
}