大网数据广告活动
网络与域名解析

Managing IP Addresses Using GetIpAddrTable

· 52CE 我爱测

The GetIpAddrTable function fills a pointer to an MIB_IPADDRTABLE structure with information about the current IP addresses associated with the system.

The GetIpAddrTable function fills a pointer to an MIB_IPADDRTABLE structure with information about the current IP addresses associated with the system.

To use GetIpAddrTable

  1. Declare a pointer to an MIB_IPADDRTABLE object called pIPAddrTable, and a DWORD object called dwSize. These variables are passed as parameters to the GetIpAddrTable function. Also create a DWORD variable called dwRetVal (used for error checking).

    MIB_IPADDRTABLE  *pIPAddrTable;
    DWORD            dwSize = 0;
    DWORD            dwRetVal;
    
    
  2. Allocate memory for the structure.

    [!Note]
    The size of dwSize is not sufficient to hold the information. See the next step.

    pIPAddrTable = (MIB_IPADDRTABLE*) malloc( sizeof(MIB_IPADDRTABLE) );
    
    
  3. Make an initial call to GetIpAddrTable to get the size needed into the dwSize variable.

    [!Note]
    This call to the function is meant to fail, and is used to ensure that the dwSize variable specifies a size sufficient for holding all the information returned to pIPAddrTable. This is a common programming model for data structures and functions of this type.

    if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
        free( pIPAddrTable );
        pIPAddrTable = (MIB_IPADDRTABLE *) malloc ( dwSize );
    }
    
    
  4. Make a second call to GetIpAddrTable with general error checking and return its value to the DWORD variable dwRetVal (for more advanced error checking).

    if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR ) { 
        printf("GetIpAddrTable call failed with %d\n", dwRetVal);
    }
    
    
  5. If the call was successful, access the data from the pIPAddrTable data structure.

    printf("IP Address:         %ld\n", pIPAddrTable->table[0].dwAddr);
    printf("IP Mask:            %ld\n", pIPAddrTable->table[0].dwMask);
    printf("IF Index:           %ld\n", pIPAddrTable->table[0].dwIndex);
    printf("Broadcast Addr:     %ld\n", pIPAddrTable->table[0].dwBCastAddr);
    printf("Re-assembly size:   %ld\n", pIPAddrTable->table[0].dwReasmSize);
    
    
  6. Free any memory allocated for the pIPAddrTable structure.

    if (pIPAddrTable)
            free(pIPAddrTable);
    
    

Note:
The DWORD objects dwAddr and dwMask are returned as numerical values in host byte order, not network byte order. These values are not dotted IP addresses.

Next Step: Managing DHCP Leases Using IpReleaseAddress and IpRenewAddress

Previous Step: Managing Interfaces Using GetInterfaceInfo

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。