Managing Interfaces Using GetInterfaceInfo
The GetInterfaceInfo function fills a pointer to an IP_INTERFACE_INFO structure with information about the interfaces associated with the system.
To use GetInterfaceInfo
Declare a pointer to an IP_INTERFACE_INFO object called
pInfo
, and a ULONG object calledulOutBufLen
. Also declare a DWORD object calleddwRetVal
(used for error checking).ULONG ulOutBufLen; DWORD dwRetVal; unsigned int i; IP_INTERFACE_INFO* pInterfaceInfo;
Allocate memory for the structures.
Note
The size of
ulOutBufLen
is not sufficient to hold the information. See the next step.pInterfaceInfo = (IP_INTERFACE_INFO *) malloc(sizeof (IP_INTERFACE_INFO)); ulOutBufLen = sizeof(IP_INTERFACE_INFO);
Make an initial call to GetInterfaceInfo to get the size needed into the
ulOutBufLen
variable.Note
This call to the function is meant to fail, and is used to ensure that the
ulOutBufLen
variable specifies a size sufficient for holding all the information returned topInfo
. This is a common programming model in IP Helper for data structures and functions of this type.if (GetInterfaceInfo(pInterfaceInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER) { free(pInterfaceInfo); pInterfaceInfo = (IP_INTERFACE_INFO *) malloc(ulOutBufLen); }
Make a second call to GetInterfaceInfo with general error checking and return its value to the DWORD variable
dwRetVal
(for more advanced error checking).if ((dwRetVal = GetInterfaceInfo(pInterfaceInfo, &ulOutBufLen)) != NO_ERROR) { printf(" GetInterfaceInfo failed with error: %d\n", dwRetVal); }
If the call was successful, access the data from the
pInfo
data structure.printf(" GetInterfaceInfo succeeded.\n"); printf(" Num Adapters: %ld\n\n", pInterfaceInfo->NumAdapters); for (i = 0; i < (unsigned int) pInterfaceInfo->NumAdapters; i++) { printf(" Adapter Index[%d]: %ld\n", i, pInterfaceInfo->Adapter[i].Index); printf(" Adapter Name[%d]: %ws\n\n", i, pInterfaceInfo->Adapter[i].Name); } }
Note
The %ws in the first line denotes a wide string. This is used because the Name attribute of the IP_ADAPTER_INDEX_MAP structure
Adapter
is a WCHAR, which is a Unicode string.Free any memory allocated for the pInfo structure.
if (pInterfaceInfo) { free(pInterfaceInfo); pInterfaceInfo = NULL; }
Next Step: Managing IP Addresses Using GetIpAddrTable
Previous Step: Managing Network Adapters Using GetAdaptersInfo