How can I access Ethernet statistics from C/C++ code like netstat -e?
Interface Statistics
Received Sent
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");
...