IP Helper API Reference

The IP Helper Application Programming Interface (API) provides functions that retrieve information about network configurations and modify network configurations. These functions are included in the IP Helper API. The functions in this API are designed to provide a higher-level abstraction over Winsock.

Overview

The IP Helper API allows applications to:

Key Concepts

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

Core Functions

Retrieving IP Address Information

These functions allow you to get details about IP addresses configured on the system.

Managing Routing Entries

Manipulate the system's routing table.

Network Configuration and Parameters

Query and set various TCP/IP parameters.

Notifications

Receive notifications when network configurations change.

Example Usage

The following C++ code snippet demonstrates how to retrieve IP address information for all adapters:


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

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

int main() {
    PIP_ADAPTER_ADDRESSES pAdapterAddresses = NULL;
    ULONG outBufLen = 0;
    DWORD dwRetVal = 0;

    // Allocate a buffer for the adapter addresses
    dwRetVal = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &outBufLen);

    if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
        pAdapterAddresses = (IP_ADAPTER_ADDRESSES *) malloc(outBufLen);
        if (pAdapterAddresses == NULL) {
            printf("Error allocating memory\n");
            return 1;
        }
    } else {
        printf("Error getting buffer size: %d\n", dwRetVal);
        return 1;
    }

    // Get adapter addresses
    dwRetVal = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAdapterAddresses, &outBufLen);

    if (dwRetVal == NO_ERROR) {
        printf("Successfully retrieved adapter addresses.\n\n");
        for (PIP_ADAPTER_ADDRESSES pAdapter = pAdapterAddresses; pAdapter != NULL; pAdapter = pAdapter->Next) {
            printf("Adapter Name: %S\n", pAdapter->FriendlyName);
            printf("  Description: %S\n", pAdapter->Description);

            for (PIP_ADAPTER_UNICAST_ADDRESS pUnicast = pAdapter->FirstUnicastAddress; pUnicast != NULL; pUnicast = pUnicast->Next) {
                if (pUnicast->Address.lpSockaddr->sa_family == AF_INET) {
                    char ipstr[INET_ADDRSTRLEN);
                    struct sockaddr_in *sin = (struct sockaddr_in *) pUnicast->Address.lpSockaddr;
                    inet_ntop(AF_INET, &sin->sin_addr, ipstr, sizeof(ipstr));
                    printf("  IPv4 Address: %s\n", ipstr);
                } else if (pUnicast->Address.lpSockaddr->sa_family == AF_INET6) {
                    char ipstr[INET6_ADDRSTRLEN);
                    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) pUnicast->Address.lpSockaddr;
                    inet_ntop(AF_INET6, &sin6->sin6_addr, ipstr, sizeof(ipstr));
                    printf("  IPv6 Address: %s\n", ipstr);
                }
            }
            printf("\n");
        }
    } else {
        printf("Error getting adapter addresses: %d\n", dwRetVal);
    }

    if (pAdapterAddresses) {
        free(pAdapterAddresses);
    }

    return 0;
}
            
Note: Ensure that you link against iphlpapi.lib and ws2_32.lib when compiling your application.