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

Retrieving Information Using GetIpStatistics

· 52CE 我爱测

The GetIpStatistics function fills a pointer to an MIB_IPSTATS structure with information about the current IP statistics associated with the system.

The GetIpStatistics function fills a pointer to an MIB_IPSTATS structure with information about the current IP statistics associated with the system.

To use GetIpStatistics

  1. Declare some variables that are needed.

    Declare a DWORD variable dwRetval that will be using for error checking function calls. Declare a pointer to an MIB_IPSTATS variable called pStats, and allocate memory for the structure. Check that memory could be allocated.

    MIB_IPSTATS  *pStats;
    DWORD        dwRetVal = 0;
    
    pStats = (MIB_IPSTATS*) malloc(sizeof(MIB_IPSTATS));
    
    if (pStats == NULL) {
        printf("Unable to allocate memory for MIB_IPSTATS\n");
    }
    
  2. Call the GetIpStatistics function with the pStats parameter to retrieve IP statistics for the local computer. Check for errors and return the error value in the DWORD variable dwRetval. If an error occurs, the dwRetval variable can be used for more extensive error checking and reporting.

    dwRetVal = GetIpStatistics(pStats);
    if (dwRetVal != NO_ERROR) {
        printf("GetIpStatistics call failed with %d\n", dwRetVal);
    }
    
  3. If the call to GetIpStatistics was successful, print out some of the data in the MIB_IPSTATS structure pointed to by the pStats parameter.

    printf("Number of interfaces:   %ld\n", pStats->dwNumIf);
    printf("Number of IP addresses: %ld\n", pStats->dwNumAddr);
    printf("Number of received datagrams:  %ld\n", pStats->dwInReceives);
    printf("NUmber of outgoing datagrams requested to transmit:  %ld\n", pStats->dwOutRequests);
    
  4. Free the memory allocated for the MIB_IPSTATS structure pointed to by the pStats parameter. This should be done once the application no longer needs the data returned by the pStats parameter.

    if (pStats)
        free(pStats);
    

Next Step: Retrieving Information Using GetTcpStatistics

Previous Step: Managing IP Addresses Using AddIPAddress and DeleteIPAddress

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)

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

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