How can I access Ethernet statistics like netstat from windows program

How to 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 
+4
source share
7 answers

A good place to start network statistics is to call GetIpStatistics in Windows IPHelper functions.

There are several other approaches that are perhaps more portable: -

  • SNMP SNMP is required to be enabled on the computer, but can also be used to retrieve statistics for remote computers.
  • Output "netstat" to your application and print the values ​​from the text.
+2
source

WMI will provide these 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 later. You may need to resign with the appropriate Win32_PerfRawData classes in Windows 2000 and do a little more math before you can display the output.

Find documentation about all of them on MSDN.

+6
source

Szia,

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

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

Example MFC in CodeProject: http://www.codeproject.com/KB/applications/wnetstat.aspx

+1
source

You can find a feasible WMI performance counter , for example. Win32_PerfRawData_Tcpip_NetworkInterface .

+1
source

Let me answer myself, as I asked the same thing in another forum.

WMI is good, but IpHlpApi is easier to use 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"); ... 
+1
source

See Google Groups, the source code for netstats has been published many times (win32 api)

0
source

As the answers above show, WMI performance counters contain some data. Just keep in mind that in later versions of windows, perf counters are broken on 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

0
source

All Articles