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

发布于 2024-07-08 15:51:33 字数 454 浏览 9 评论 0原文

如何从 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

How can I access Ethernet statistics from C/C++ code like 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

謸气贵蔟 2024-07-15 15:51:33

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

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

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

The WMI will provide those readings:

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.

Find documentation on all of them in the MSDN.

格子衫的從容 2024-07-15 15:51:33

网络统计数据的一个不错的起点是 GetIpStatistics< /a> 调用 Windows IPHeper 函数。

还有其他几种可能更便携的方法:-

  • SNMP。 需要在计算机上启用 SNMP,但显然也可以用于检索远程计算机的统计信息。
  • 将“netstat”的输出通过管道传输到您的应用程序中,然后从文本中取消选取值。

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

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.
小鸟爱天空丶 2024-07-15 15:51:33

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

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

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

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

#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");
        ...
表情可笑 2024-07-15 15:51:33

Szia,

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

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

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

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.

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

笑忘罢 2024-07-15 15:51:33

参见Google Groups,原始netstats源代码已发布多次(win32 api)

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

握住我的手 2024-07-15 15:51:33

正如上面的答案所示,WMI 性能计数器包含一些数据。 请注意,在更高版本的 Windows 中,性能计数器在 v4 和 v6 中进行了细分,因此查询为:

SELECT * FROM Win32_PerfFormattedData_Tcpip_IPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_TCPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_UDPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMP

选择

SELECT * FROM Win32_ PerfFormattedData_Tcpip_IPv6 选择 * 从 Win32_PerfFormattedData_Tcpip_TCPv6

*从 Win32_PerfFormattedData_Tcpip_UDPv6

选择 * 从 Win32_PerfFormattedData_Tcpip_ICMPv6

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:

SELECT * FROM Win32_PerfFormattedData_Tcpip_IPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_TCPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_UDPv4

SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMP

SELECT * FROM Win32_PerfFormattedData_Tcpip_IPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_TCPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_UDPv6

SELECT * FROM Win32_PerfFormattedData_Tcpip_ICMPv6

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文