Click here to Skip to main content
15,503,767 members
Articles / Desktop Programming / MFC
Posted 29 Oct 2003


26 bookmarked

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
Using Iphlpapi.dll to retrieve network adapters info

Sample Image - BytesSentReceived.jpg


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 {
  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 
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...
/* 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

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];
    for (UINT i=0;i<m_pTable->dwNumEntries;i++)
        MIB_IFROW Row=m_pTable->table[i];
        char szDescr[MAXLEN_IFDESCR];

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)
        MIB_IFROW Row=m_pTable->table[nIndex];
        m_strInfo.Format("Received %d, Sent %d",Row.dwInOctets,

That's all

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


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 23:34
sarunvij27-Jul-08 23:34 
GeneralA small mistake in allocationg memory ; - ) Pin
Ingenious00112-Apr-07 1:14
Ingenious00112-Apr-07 1:14 
GeneralIn Java Pin
Aenema23-Feb-07 5:53
Aenema23-Feb-07 5:53 
Generalactually, this is not bytes... Pin
Anonymous9-Feb-05 4:26
Anonymous9-Feb-05 4:26 
GeneralReceived &amp; Sent bytes Pin
Zbb20-Oct-04 8:27
Zbb20-Oct-04 8:27 
QuestionVB.NETPort? Pin
andrew|14-Sep-04 6:42
andrew|14-Sep-04 6:42 
Generalabout 4GB max Pin
bionicman45219-May-04 3:19
bionicman45219-May-04 3:19 
GeneraldwInOctets Rollover Pin
TomGarratt21-Apr-04 5:17
TomGarratt21-Apr-04 5:17 
GeneralShould consider using GetIfEntry Pin
sixsixseven12-Jan-04 3:48
sixsixseven12-Jan-04 3:48 
GeneralAttention NT4 &lt; SP6 Pin
c2j27-Nov-03 5:11
c2j27-Nov-03 5:11 
GeneralThis thing >> which I am looking for >> thanks Pin
_skidrow_vn_4-Nov-03 9:03
_skidrow_vn_4-Nov-03 9:03 
GeneralJust the thing I needed ... Pin
whizer30-Oct-03 9:02
whizer30-Oct-03 9:02 
QuestionWhich OSes? Pin
dog_spawn30-Oct-03 6:28
dog_spawn30-Oct-03 6:28 
AnswerRe: Which OSes? Pin
Massimiliano Conte30-Oct-03 6:56
Massimiliano Conte30-Oct-03 6:56 
GeneralRe: Which OSes? Pin
c2j27-Nov-03 5:10
c2j27-Nov-03 5:10 
Generalmodems Pin
NGS 54967230-Oct-03 6:28
NGS 54967230-Oct-03 6: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.