Managing IP Addresses Using GetIpAddrTable
To use GetIpAddrTable
- 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).
- Allocate memory for the structure.
Note The size of dwSize is not sufficient to hold the information. See the next step.
- 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.
- 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 the call was successful, access the data from the pIPAddrTable data structure.
printf("IP Address: %ld\n", pIPAddrTable->table.dwAddr); printf("IP Mask: %ld\n", pIPAddrTable->table.dwMask); printf("IF Index: %ld\n", pIPAddrTable->table.dwIndex); printf("Broadcast Addr: %ld\n", pIPAddrTable->table.dwBCastAddr); printf("Re-assembly size: %ld\n", pIPAddrTable->table.dwReasmSize);
- Free any memory allocated for the pIPAddrTable structure.
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.
Previous Step: Managing Interfaces Using GetInterfaceInfo
Build date: 11/16/2013