Retrieves a table that contains the IPv4 address information for the local computer.
DWORD GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder);
DWORD GetIpAddrTable(
PMIB_IPADDRTABLE pIpAddrTable,
PULONG pdwSize,
BOOL bOrder
);
| Parameter | Description |
|---|---|
pIpAddrTable |
A pointer to a |
pdwSize |
On input, specifies the size, in bytes, of the buffer pointed to by On output, if the function succeeds and the buffer is large enough, this parameter is updated with the actual size of the structure. |
bOrder |
A boolean value that specifies whether the IP address entries in the table should be sorted. If If |
If the function succeeds, the return value is NO_ERROR.
If the function fails, the return value is one of the following error codes:
ERROR_INSUFFICIENT_BUFFER: The buffer specified by pIpAddrTable is not large enough to hold the table. The size required for the buffer is returned in the pdwSize parameter.ERROR_INVALID_PARAMETER: One or more of the parameters are invalid.The GetIpAddrTable function is used to retrieve a list of IP addresses configured on the local computer. This function is available on Windows 2000 and later.
The MIB_IPADDRTABLE structure contains an array of MIB_IPADDRROW structures, each representing an IP address configuration for an interface.
To get the most accurate information, it is recommended to call GetIpAddrTable twice. The first call is to get the required buffer size, and the second call with an allocated buffer fills the table.
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
int main() {
ULONG ulSize = 0;
MIB_IPADDRTABLE* pIpAddrTable = NULL;
DWORD dwRetVal = 0;
// Get the required buffer size
dwRetVal = GetIpAddrTable(NULL, &ulSize, TRUE);
if (dwRetVal == ERROR_INSUFFICIENT_BUFFER) {
pIpAddrTable = (MIB_IPADDRTABLE*) malloc(sizeof(MIB_IPADDRTABLE) + ulSize); // Allocate memory
if (pIpAddrTable == NULL) {
printf("Error allocating memory\n");
return 1;
}
} else {
printf("Error calling GetIpAddrTable (first call): %d\n", dwRetVal);
return 1;
}
// Get the IP address table
dwRetVal = GetIpAddrTable(pIpAddrTable, &ulSize, TRUE);
if (dwRetVal == NO_ERROR) {
printf("Successfully retrieved IP Address Table\n");
printf("Number of entries: %d\n\n", pIpAddrTable->dwNumEntries);
for (DWORD i = 0; i < pIpAddrTable->dwNumEntries; i++) {
char ipAddrStr[INET_ADDRSTRLEN];
char maskAddrStr[INET_ADDRSTRLEN];
// Convert IP address to string
inet_ntop(AF_INET, &(pIpAddrTable->table[i].dwAddr), ipAddrStr, INET_ADDRSTRLEN);
// Convert subnet mask to string
inet_ntop(AF_INET, &(pIpAddrTable->table[i].dwMask), maskAddrStr, INET_ADDRSTRLEN);
printf("Interface Index: %lu\n", pIpAddrTable->table[i].dwIndex);
printf("IP Address: %s\n", ipAddrStr);
printf("Subnet Mask: %s\n", maskAddrStr);
printf("Broadcast Address: %s\n", inet_ntoa(*((struct in_addr*)&pIpAddrTable->table[i].dwBCastAddr)));
printf("------------------------------------\n");
}
} else {
printf("Error calling GetIpAddrTable (second call): %d\n", dwRetVal);
}
// Free allocated memory
if (pIpAddrTable) {
free(pIpAddrTable);
pIpAddrTable = NULL;
}
return 0;
}
Header: Iphlpapi.h
Library: Iphlpapi.lib
Minimum supported client: Windows 2000 Professional
Minimum supported server: Windows 2000 Server
Header file: ws2tcpip.h (for inet_ntop)