Retrieves detailed information about network adapters installed on the local computer.
ULONG GetAdaptersInfo(
IP_ADAPTER_INFO *pAdapterInfo,
PULONG Size
);
pAdapterInfo
IP_ADAPTER_INFO
structure. The structure contains information about the adapters. If the buffer is not large enough, the function returns ERROR_BUFFER_OVERFLOW
and the value of the Size
parameter is updated to the required buffer size.
Size
ULONG
variable that specifies the size, in bytes, of the buffer pointed to by pAdapterInfo
.
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_BUFFER_OVERFLOW |
The buffer provided is not large enough to hold the adapter information. The Size parameter is updated with the required size. |
ERROR_INVALID_PARAMETER |
One or more of the parameters are invalid. |
ERROR_NOT_SUPPORTED |
This function is not supported on the operating system. |
The GetAdaptersInfo
function retrieves information about network adapters, including their IP addresses, MAC addresses, subnet masks, default gateways, and DHCP information.
The function populates a linked list of IP_ADAPTER_INFO
structures. Each structure describes a network adapter.
It is recommended to call GetAdaptersInfo
twice. The first call should be with a NULL
pointer for pAdapterInfo
to get the required buffer size. The second call uses the allocated buffer size to retrieve the actual adapter information.
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
int main() {
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
IP_ADAPTER_INFO *pAdapterInfo = NULL;
IP_ADAPTER_INFO *pAdapter = NULL;
DWORD dwRetVal = 0;
pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
printf("Error allocating memory for IP_ADAPTER_INFO\n");
return 1;
}
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory for IP_ADAPTER_INFO (resized)\n");
return 1;
}
}
dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
if (dwRetVal == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
printf("Adapter Name: %s\n", pAdapter->AdapterName);
printf(" Description: %s\n", pAdapter->Description);
printf(" IP Address: %s\n", pAdapter->IpAddressList.IpAddress.String);
printf(" Subnet Mask: %s\n", pAdapter->IpAddressList.IpMask.String);
if (pAdapter->GatewayList) {
printf(" Default Gateway: %s\n", pAdapter->GatewayList->IpAddress.String);
}
printf(" MAC Address: ");
for (int i = 0; i < pAdapter->PhysicalAddressLength; i++) {
if (i == pAdapter->PhysicalAddressLength - 1)
printf("%.2X\n", pAdapter->PhysicalAddress[i]);
else
printf("%.2X-", pAdapter->PhysicalAddress[i]);
}
printf("\n");
pAdapter = pAdapter->Next;
}
} else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
}
if (pAdapterInfo) {
free(pAdapterInfo);
}
return 0;
}