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

时间:2021-08-03 22:15:52

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

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

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

7 个解决方案

#1


2  

A good place to start for network statistics would be the GetIpStatistics call 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.
  • SNMP。需要在计算机上启用SNMP,但显然也可用于检索远程计算机的统计信息。

  • Pipe the output of 'netstat' into your application, and unpick the values from the text.
  • 将'netstat'的输出传递到应用程序中,然后从文本中取消值。

#2


6  

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 them in the MSDN.

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

#3


1  

Szia,

from 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

#4


1  

You might find a feasable WMI performance counter, e.g. Win32_PerfRawData_Tcpip_NetworkInterface.

您可能会找到一个可行的WMI性能计数器,例如Win32_PerfRawData_Tcpip_NetworkInterface。

#5


1  

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");
        ...

#6


0  

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

查看Google网上论坛,原始netstats源代码已多次发布(win32 api)

#7


0  

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

#1


2  

A good place to start for network statistics would be the GetIpStatistics call 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.
  • SNMP。需要在计算机上启用SNMP,但显然也可用于检索远程计算机的统计信息。

  • Pipe the output of 'netstat' into your application, and unpick the values from the text.
  • 将'netstat'的输出传递到应用程序中,然后从文本中取消值。

#2


6  

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 them in the MSDN.

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

#3


1  

Szia,

from 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

#4


1  

You might find a feasable WMI performance counter, e.g. Win32_PerfRawData_Tcpip_NetworkInterface.

您可能会找到一个可行的WMI性能计数器,例如Win32_PerfRawData_Tcpip_NetworkInterface。

#5


1  

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");
        ...

#6


0  

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

查看Google网上论坛,原始netstats源代码已多次发布(win32 api)

#7


0  

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