GetAdaptersAddresses Function

Retrieves the adapter addresses for the local computer.

Syntax


    _Must_inspect_
    ULONG GetAdaptersAddresses(
      [in]      AF_ADDRESSFAMILY Family,
      [in]      ULONG Flags,
      [in, out] PVOID Reserved,
      [in, out] PIP_ADAPTER_ADDRESSES AdapterAddresses,
      [in, out] PULONG SizePointer
    );
                    

Parameters

Parameter Description
Family The address family for which to retrieve adapter addresses. Possible values are AF_UNSPEC, AF_INET (IPv4), or AF_INET6 (IPv6).
Flags Flags that control the retrieval of adapter addresses. This parameter can be zero or a combination of the following values:
  • GAA_FLAG_INCLUDE_PREFIX: Include prefix information.
  • GAA_FLAG_SKIP_DNS_SERVER: Skip DNS server addresses.
  • GAA_FLAG_SKIP_FRIENDLY_NAME: Skip friendly names.
Reserved Reserved for future use. This parameter must be NULL.
AdapterAddresses A pointer to a buffer that receives a linked list of IP_ADAPTER_ADDRESSES structures. Each structure contains information about an adapter. The caller must allocate this buffer.
SizePointer On input, a pointer to a ULONG that specifies the size, in bytes, of the buffer pointed to by the AdapterAddresses parameter. On output, if the buffer is not large enough, this parameter receives the required buffer size in bytes.

Return Value

If the function succeeds, the return value is ERROR_SUCCESS. If the buffer is not large enough, the return value is ERROR_BUFFER_OVERFLOW, and the value pointed to by SizePointer is updated with the required buffer size.

Remarks

The GetAdaptersAddresses function is used to retrieve adapter information, including IP addresses, MAC addresses, and gateway information.

Example


    #include <winsock2.h>
    #include <iphlpapi.h>
    #include <stdio.h>
    #include <netioapi.h> // For AF_INET6

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

    int main() {
        ULONG buflen = 0;
        PIP_ADAPTER_ADDRESSES pAddresses = NULL;
        ULONG family = AF_UNSPEC;
        DWORD retVal = ERROR_SUCCESS;

        // Allocate a buffer to hold the adapter addresses.
        // Start with a reasonable size and grow if needed.
        buflen = sizeof(IP_ADAPTER_ADDRESSES);

        do {
            pAddresses = (PIP_ADAPTER_ADDRESSES)malloc(buflen);
            if (pAddresses == NULL) {
                printf("Error allocating memory\n");
                return 1;
            }

            retVal = GetAdaptersAddresses(family, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &buflen);

            if (retVal == ERROR_BUFFER_OVERFLOW) {
                free(pAddresses);
                pAddresses = NULL;
            } else {
                break;
            }

            buflen += 1024; // Increase buffer size if overflow
        } while (retVal == ERROR_BUFFER_OVERFLOW);

        if (retVal == ERROR_SUCCESS) {
            PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses;

            while (pCurrAddresses) {
                printf("Adapter Name: %s\n", pCurrAddresses->FriendlyName);

                if (pCurrAddresses->OperStatus == IfOperStatusUp) {
                    printf("  Operational Status: Up\n");
                } else {
                    printf("  Operational Status: Down\n");
                }

                PIP_UNICAST_ADDRESS_IPV4 pUnicast = pCurrAddresses->FirstUnicastAddress;
                if (pUnicast) {
                    SOCKADDR_IN* pSockAddr = (SOCKADDR_IN*)pUnicast->Address.lpSockaddr;
                    char ipstr[INET_ADDRSTRLEN);
                    printf("  IPv4 Address: %s\n", inet_ntop(AF_INET, &pSockAddr->sin_addr, ipstr, sizeof(ipstr)));
                }
                
                if (pCurrAddresses->PhysicalAddressLength > 0) {
                    printf("  MAC Address: ");
                    for (int i = 0; i < pCurrAddresses->PhysicalAddressLength; i++) {
                        printf("%02x%c", pCurrAddresses->PhysicalAddress[i], (i == pCurrAddresses->PhysicalAddressLength - 1) ? ' ' : '-');
                    }
                    printf("\n");
                }
                
                printf("\n");
                pCurrAddresses = pCurrAddresses->Next;
            }
        } else {
            printf("Call to GetAdaptersAddresses failed with error: %ld\n", retVal);
        }

        if (pAddresses) {
            free(pAddresses);
        }

        return 0;
    }
                    

Requirements

Product Minimum supported client Minimum supported server
Windows 7 Windows 7 [desktop apps only] Windows Server 2008 R2 [desktop apps only]
Windows Vista Windows Vista [desktop apps only] Windows Server 2008 [desktop apps only]
Windows XP Windows XP [desktop apps only] Windows Server 2003 [desktop apps only]

See Also