The GetAdaptersInfo function retrieves information about the network adapters installed on the local computer.
ULONG GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pSize
);
| Parameter | Description |
|---|---|
pAdapterInfo |
A pointer to a buffer that receives an array of IP_ADAPTER_INFO structures. This structure contains information about the adapters. |
pSize |
A pointer to a ULONG variable that specifies the size of the buffer, in bytes, pointed to by pAdapterInfo. If the function fails because the buffer is too small, this parameter receives the required buffer size. |
If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is one of the system error codes.
The GetAdaptersInfo function is used to retrieve adapter-specific information, such as the adapter name, description, IP address, subnet mask, and default gateway. This information is returned as an array of IP_ADAPTER_INFO structures.
The IP_ADAPTER_INFO structure contains the following members:
Next: Pointer to the next adapter information structure in the list.ComboIndex: Adapter index.AdapterName: Null-terminated string that specifies the adapter name.Description: Null-terminated string that describes the adapter.AddressLength: Length of the hardware address.Address: Hardware address.Index: Network interface index.Type: The type of the adapter.DhcpEnabled: Specifies whether DHCP is enabled for the adapter.CurrentIpAddress: The current IP address for the adapter.IpMask: The IP subnet mask.GatewayList: Pointer to a linked list of IP_ADDR_STRING structures representing the default gateway addresses.DhcpServer: The IP address of the DHCP server.LeaseObtained: Time when the DHCP lease was obtained.LeaseExpires: Time when the DHCP lease expires.StrongLeet: Specifies whether the adapter has a strong lease.LeaseTransferred: Pointer to a linked list of IP_ADDR_STRING structures representing DHCP lease transfer addresses.AddrList: Pointer to a linked list of IP_ADDR_STRING structures representing IP addresses and subnet masks for the adapter.GetAdaptersInfo with a NULL pointer for pAdapterInfo to determine the required buffer size. The function will return the required size in the pSize parameter. Then, allocate a buffer of that size and call GetAdaptersInfo again with the allocated buffer.
Example Usage (Conceptual C++):
#include <windows.h>
#include <iprtrmib.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
void DisplayAdapterInfo() {
ULONG size = 0;
IP_ADAPTER_INFO* pAdapterInfo = NULL;
// Get the required buffer size
DWORD dwRetVal = GetAdaptersInfo(NULL, &size);
if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
pAdapterInfo = (IP_ADAPTER_INFO*)malloc(size);
if (pAdapterInfo == NULL) {
printf("Error allocating memory for adapter info.\n");
return;
}
} else {
printf("Error getting adapter info size: %lu\n", dwRetVal);
return;
}
// Get the adapter information
dwRetVal = GetAdaptersInfo(pAdapterInfo, &size);
if (dwRetVal == ERROR_SUCCESS) {
IP_ADAPTER_INFO* pCurrentAdapter = pAdapterInfo;
while (pCurrentAdapter) {
printf("Adapter Name: %s\n", pCurrentAdapter->AdapterName);
printf("Description: %s\n", pCurrentAdapter->Description);
printf("IP Address: %s\n", pCurrentAdapter->IpAddressList.IpAddress.String);
printf("Subnet Mask: %s\n", pCurrentAdapter->IpAddressList.IpMask.String);
printf("Default Gateway: %s\n", pCurrentAdapter->GatewayList.IpAddress.String);
printf("----------------------------------------\n");
pCurrentAdapter = pCurrentAdapter->Next;
}
} else {
printf("Error retrieving adapter information: %lu\n", dwRetVal);
}
if (pAdapterInfo) {
free(pAdapterInfo);
}
}
int main() {
DisplayAdapterInfo();
return 0;
}