GetAdaptersInfo

The GetAdaptersInfo function retrieves information about the network adapters installed on the local computer.

Syntax


ULONG GetAdaptersInfo(
  PIP_ADAPTER_INFO pAdapterInfo,
  PULONG           pSize
);
                

Parameters

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.

Return Value

If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is one of the system error codes.

Remarks

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:

Note: Applications should call 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;
}
                

See Also