How will I be able to record how manny bytes been send/recived to/from Network? RRS feed

  • Question

  • Hi

    I work with Visual Studio.Net 2003. (MFC)
    (Sorry my English is realy bad.)

    I want to write a little program which show the user how many bytes been send/recived to/from Network since the connection been created.

    Could someone help me to find informations about thiss topic?

    Wednesday, February 28, 2007 4:45 PM

All replies

  • Hi,


    Depending upon your needs you may be able to use the following approach which utilises the IP Helper APIs. This uses a function called GetIfEntry which is documented at http://msdn2.microsoft.com/en-us/library/ms908433.aspx


    #include "Iphlpapi.h"


    static int ListInterfaceTrafficStats()


         MIB_IFROW row;

         DWORD dwNumInterfaces = 0;


         // Determine the number of interfaces currently available on this device

         if (GetNumberOfInterfaces(&dwNumInterfaces) != NO_ERROR)

              return -1;


         // Loop over all interfaces

         for (DWORD i = 1; i <= dwNumInterfaces; i++)


              // Request the details for the next interface

              memset(&row, 0, sizeof(row));

              row.dwIndex = i;

              if (GetIfEntry(&row) != NO_ERROR)

                   return -1;


              // Print out a message for the interface describing the number of bytes sent/recevied

              // NOTICE: row.bDescr is an ANSI text string, rather than a WCHAR (Unicode) string

              //               hence the need for %hs as the format specifier instead of %s or %ls.

              TCHAR szBuffer[MAX_PATH];

              _stprintf(szBuffer, TEXT("%hs: in %lu bytes, out %lu bytes\n"), row.bDescr, row.dwInOctets, row.dwOutOctets);




         return 0;



    If ran on an emulator or device conneced to Visual Studio's debugger, a call to ListInterfaceTrafficStats() should list to the debug output window a line for each network interface available, outlining the number of bytes transmitted and received via it.


    Hopefully this example shows you how to obtain the information you desire, and you can modify the example to fit your needs (for instance to obtain the data for a specific interface etc etc).


    Hope this helps,

    Christopher Fairbairn

    Saturday, April 21, 2007 12:10 PM