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 ofulOutBufLenis 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
ulOutBufLenvariable.[!Note]
This call to the function is meant to fail, and is used to ensure that theulOutBufLenvariable 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
pInfodata 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 structureAdapteris 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
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.
