Click here to Skip to main content
15,885,365 members
Articles / Desktop Programming / MFC
Article

Retrieve the number of bytes sent/received (and other useful infos) for any network adapter

Rate me:
Please Sign up or sign in to vote.
4.56/5 (23 votes)
29 Oct 2003CPOL1 min read 102.1K   2K   26   16
Using Iphlpapi.dll to retrieve network adapters info

Sample Image - BytesSentReceived.jpg

Introduction

This sample snippet shows how to retrieve the list of all network adapters on your machine, plus a lot of infos about each of them. The information you can retrieve are all the members of the structure MIB_IFROW as shown in this MSDN snippet

typedef struct _MIB_IFROW {
  WCHAR   wszName[MAX_INTERFACE_NAME_LEN];
  DWORD   dwIndex;    // index of the interface
  DWORD   dwType;     // type of interface
  DWORD   dwMtu;      // max transmission unit 
  DWORD   dwSpeed;    // speed of the interface 
  DWORD   dwPhysAddrLen;    // length of physical address
  BYTE    bPhysAddr[MAXLEN_PHYSADDR]; // physical address of adapter
  DWORD   dwAdminStatus;    // administrative status
  DWORD   dwOperStatus;     // operational status
  DWORD   dwLastChange;     // last time operational status changed 
  DWORD   dwInOctets;       // octets received
  DWORD   dwInUcastPkts;    // unicast packets received 
  DWORD   dwInNUcastPkts;   // non-unicast packets received 
  DWORD   dwInDiscards;     // received packets discarded 
  DWORD   dwInErrors;       // erroneous packets received 
  DWORD   dwInUnknownProtos;  // unknown protocol packets received 
  DWORD   dwOutOctets;      // octets sent 
  DWORD   dwOutUcastPkts;   // unicast packets sent 
  DWORD   dwOutNUcastPkts;  // non-unicast packets sent 
  DWORD   dwOutDiscards;    // outgoing packets discarded 
  DWORD   dwOutErrors;      // erroneous packets sent 
  DWORD   dwOutQLen;        // output queue length 
  DWORD   dwDescrLen;       // length of bDescr member 
  BYTE    bDescr[MAXLEN_IFDESCR];  // interface description 
} MIB_IFROW,*PMIB_IFROW;
After wasted lot of time trying to reach the performance counter stuff I found this DLL. And now you can use it without having to look around anymore :)

Using the code

In my working environment I don't have the right headers and libs to include in the project, so I used the functions inside the DLL using the LoadLibrary and GetProcAddress, like this:

typedef DWORD (_stdcall *TGetIfTable) (
  MIB_IFTABLE *pIfTable,  // buffer for interface table 
  ULONG *pdwSize,         // size of buffer
  BOOL bOrder             // sort the table by index?
);
...
TGetIfTable pGetIfTable;
...
// not so safe here...
pGetIfTable=(TGetIfTable)GetProcAddress(LoadLibrary("Iphlpapi.dll"),
    "GetIfTable");
/* always remember to test the result of LoadLibrary 
and GetProcAddress against null return values... */

The key function of the DLL  is GetIfTable. You have to call this function twice, first time to know how much memory space allocate, and then to fill the structure with the return values. In the OnInitDialog I get the right size of the adapters-table and I can fill the combo box with all the adapters descriptions

...
m_pTable=NULL;
m_dwAdapters=0;
ULONG uRetCode=pGetIfTable(m_pTable,&m_dwAdapters,TRUE);
if (uRetCode == 122 /* The data area passed to a 
    system call is too small.*/)
{
    // now we know how much memory allocate
    m_pTable=new MIB_IFTABLE[m_dwAdapters];
    pGetIfTable(m_pTable,&m_dwAdapters,TRUE);
    for (UINT i=0;i<m_pTable->dwNumEntries;i++)
    {
        MIB_IFROW Row=m_pTable->table[i];
        char szDescr[MAXLEN_IFDESCR];
        memcpy(szDescr,Row.bDescr,Row.dwDescrLen);
        szDescr[Row.dwDescrLen]=0;
        m_ctlCBAdapters.AddString(szDescr);
    }
}
...

if everything has gone right a timer starts, and the UpdateCounters get called every 100ms

void CP01Dlg::UpdateCounters()
{
    int nIndex=m_ctlCBAdapters.GetCurSel();
    if (nIndex!=CB_ERR)
    {
        pGetIfTable(m_pTable,&m_dwAdapters,TRUE);
        MIB_IFROW Row=m_pTable->table[nIndex];
        m_strInfo.Format("Received %d, Sent %d",Row.dwInOctets,
            Row.dwOutOctets);
        UpdateData(FALSE);
    }
}

That's all

I hope this snippet can save you some time. See you in my next article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Leonardo
Italy Italy
Hi Smile | :)
I was born in 1970 (Augusta - Italy).
I live in Taranto - Italy.
I work in Taranto - Italy.
I like computer science!!!
That's all!

Comments and Discussions

 
QuestionSame on C# .NET Pin
sarunvij27-Jul-08 22:34
sarunvij27-Jul-08 22:34 
GeneralA small mistake in allocationg memory ; - ) Pin
Ingenious00112-Apr-07 0:14
Ingenious00112-Apr-07 0:14 
GeneralIn Java Pin
Aenema23-Feb-07 4:53
Aenema23-Feb-07 4:53 
Generalactually, this is not bytes... Pin
Anonymous9-Feb-05 3:26
Anonymous9-Feb-05 3:26 
GeneralReceived &amp; Sent bytes Pin
Zbb20-Oct-04 7:27
Zbb20-Oct-04 7:27 
QuestionVB.NETPort? Pin
andrew|14-Sep-04 5:42
andrew|14-Sep-04 5:42 
Generalabout 4GB max Pin
bionicman45219-May-04 2:19
bionicman45219-May-04 2:19 
GeneraldwInOctets Rollover Pin
TomGarratt21-Apr-04 4:17
TomGarratt21-Apr-04 4:17 
GeneralShould consider using GetIfEntry Pin
sixsixseven12-Jan-04 2:48
sixsixseven12-Jan-04 2:48 
Hi out there!
Just wanted to add:

It's not very efficient to re-read the
whole interface-table every time you
encounter a timer event. You should
consider using GetIfEntry (see Platform
SDK documentation).

Regards,

Christian Blichmann
(codeproject AT blichmann DOT de)
GeneralAttention NT4 &lt; SP6 Pin
c2j27-Nov-03 4:11
c2j27-Nov-03 4:11 
GeneralThis thing >> which I am looking for >> thanks Pin
_skidrow_vn_4-Nov-03 8:03
_skidrow_vn_4-Nov-03 8:03 
GeneralJust the thing I needed ... Pin
whizer30-Oct-03 8:02
whizer30-Oct-03 8:02 
QuestionWhich OSes? Pin
dog_spawn30-Oct-03 5:28
dog_spawn30-Oct-03 5:28 
AnswerRe: Which OSes? Pin
Massimiliano Conte30-Oct-03 5:56
Massimiliano Conte30-Oct-03 5:56 
GeneralRe: Which OSes? Pin
c2j27-Nov-03 4:10
c2j27-Nov-03 4:10 
Generalmodems Pin
NGS 54967230-Oct-03 5:28
NGS 54967230-Oct-03 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.