如何从 Windows 程序访问类似 netstat 的以太网统计信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/221181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:29:51  来源:igfitidea点击:

How can I access netstat-like Ethernet statistics from a Windows program

cwindowsnetworkingethernetnetwork-monitoring

提问by Denes Tarjan

How can I access Ethernet statistics from C/C++ code like netstat -e?

如何从 C/C++ 代码(如netstat -e )访问以太网统计信息?

Interface Statistics

                       Received            Sent

Bytes                      21010071        15425579
Unicast packets               95512           94166
Non-unicast packets           12510               7
Discards                          0               0
Errors                            0               3
Unknown protocols                 0

采纳答案by Roddy

A good place to start for network statistics would be the GetIpStatisticscall in the Windows IPHelper functions.

Windows IPHelper 函数中的GetIpStatistics调用是开始网络统计的好地方。

There are a couple of other approaches that are possibly more portable:-

还有一些其他方法可能更便携:-

  • SNMP. Requires SNMP to be enabled on the computer, but can obviously be used to retrieve statistics for remote computers also.
  • Pipe the output of 'netstat' into your application, and unpick the values from the text.
  • 网络管理协议。需要在计算机上启用 SNMP,但显然也可用于检索远程计算机的统计信息。
  • 将“netstat”的输出通过管道传输到您的应用程序中,并从文本中取消选择值。

回答by Tomalak

The WMI will provide those readings:

WMI 将提供这些读数:

SELECT * FROM Win32_PerfFormattedData_Tcpip_IP
SELECT * FROM Win32_PerfFormattedData_Tcpip_TCP
SELECT * FROM Win32_PerfFormattedData_Tcpip_UDP
SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMP
SELECT * FROM Win32_PerfFormattedData_Tcpip_Networkinterface

These classes are available on Windows XP or newer. You may have to resign to the matching "Win32_PerfRawData" classes on Windows 2000, and do a little bit more of math before you can display the output.

这些类在 Windows XP 或更新版本上可用。您可能不得不放弃 Windows 2000 上匹配的“Win32_PerfRawData”类,并在显示输出之前做更多的数学运算。

Find documentation on all of themin the MSDN.

在 MSDN 中查找有关所有这些的文档

回答by Denes Tarjan

Let me answer to myself, as I asked the same on another forum.

让我自己回答,因为我在另一个论坛上问过同样的问题。

WMI is good, but it's easier to use IpHlpApi instead:

WMI 很好,但使用 IpHlpApi 更容易:

#include <winsock2.h>
#include <iphlpapi.h>

int main(int argc, char *argv[])
{

PMIB_IFTABLE pIfTable;
MIB_IFROW ifRow;
PMIB_IFROW pIfRow = &ifRow;
DWORD dwSize = 0;

// first call returns the buffer size needed
DWORD retv = GetIfTable(pIfTable, &dwSize, true);
if (retv != ERROR_INSUFFICIENT_BUFFER)
    WriteErrorAndExit(retv);
pIfTable = (MIB_IFTABLE*)malloc(dwSize);

retv = GetIfTable(pIfTable, &dwSize, true);
if (retv != NO_ERROR)
    WriteErrorAndExit(retv);

// Get index
    int i,j;
    printf("\tNum Entries: %ld\n\n", pIfTable->dwNumEntries);
    for (i = 0; i < (int) pIfTable->dwNumEntries; i++)
    {
        pIfRow = (MIB_IFROW *) & pIfTable->table[i];
        printf("\tIndex[%d]:\t %ld\n", i, pIfRow->dwIndex);
        printf("\tInterfaceName[%d]:\t %ws", i, pIfRow->wszName);
        printf("\n");
        printf("\tDescription[%d]:\t ", i);
        for (j = 0; j < (int) pIfRow->dwDescrLen; j++)
            printf("%c", pIfRow->bDescr[j]);
        printf("\n");
        ...

回答by Ilya Kochetov

Szia,

西娅,

from http://en.wikipedia.org/wiki/Netstat

来自http://en.wikipedia.org/wiki/Netstat

On the Windows platform, netstat information can be retrieved by calling the GetTcpTable and GetUdpTable functions in the IP Helper API, or IPHLPAPI.DLL. Information returned includes local and remote IP addresses, local and remote ports, and (for GetTcpTable) TCP status codes. In addition to the command-line netstat.exe tool that ships with Windows, there are GUI-based netstat programs available. On the Windows platform, this command is available only if the Internet Protocol (TCP/IP) protocol is installed as a component in the properties of a network adapter in Network Connections.

在Windows 平台上,可以通过调用IP Helper API 或IPHLPAPI.DLL 中的GetTcpTable 和GetUdpTable 函数来检索netstat 信息。返回的信息包括本地和远程 IP 地址、本地和远程端口以及(对于 GetTcpTable)TCP 状态代码。除了 Windows 附带的命令行 netstat.exe 工具之外,还有基于 GUI 的 netstat 程序可用。在 Windows 平台上,仅当 Internet 协议 (TCP/IP) 协议作为组件安装在网络连接的网络适配器属性中时,此命令才可用。

MFC sample at CodeProject: http://www.codeproject.com/KB/applications/wnetstat.aspx

CodeProject 中的 MFC 示例:http: //www.codeproject.com/KB/applications/wnetstat.aspx

回答by VolkerK

回答by Laisvis Lingvevicius

As above answers suggest, WMI performance counters contains some data. Just be aware that in later versions of windows the perf counters are broken down in v4 vs v6 so the queries are:

如上所述,WMI 性能计数器包含一些数据。请注意,在更高版本的 Windows 中,性能计数器在 v4 与 v6 中被分解,因此查询是:

SELECT * FROM Win32_PerfFormattedData_Tcpip_IPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_IPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_TCPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_TCPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_UDPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_UDPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMP

SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMP

SELECT * FROM Win32_PerfFormattedData_Tcpip_IPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_IPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_TCPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_TCPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_UDPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_UDPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMPv6

回答by Laisvis Lingvevicius

See Google Groups, original netstats source code has been posted many times (win32 api)

看Google Groups,原来的netstats源码已经贴了很多次了(win32 api)