Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 425
Denes Tarjan Avatar asked Oct 21 '08 08:10

Denes Tarjan


2 Answers

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.

like image 94
Tomalak Avatar answered Sep 30 '22 21:09

Tomalak


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.
like image 33
Roddy Avatar answered Sep 30 '22 20:09

Roddy