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

Managing Interfaces Using GetInterfaceInfo

· 52CE 我爱测

The GetInterfaceInfo function fills a pointer to an IP_INTERFACE_INFO structure with information about the interfaces associated with the system.

The GetInterfaceInfo function fills a pointer to an IP_INTERFACE_INFO structure with information about the interfaces associated with the system.

To use GetInterfaceInfo

  1. Declare a pointer to an IP_INTERFACE_INFO object called pInfo, and a ULONG object called ulOutBufLen. Also declare a DWORD object called dwRetVal (used for error checking).

        ULONG               ulOutBufLen;
        DWORD               dwRetVal;
        unsigned int       i;
    
        IP_INTERFACE_INFO*  pInterfaceInfo;
    
  2. 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);
    
    
  3. 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 to pInfo. 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);
        }
    
  4. 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);
        }
    
  5. 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.

  6. 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.

资料来源与适用范围

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

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

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