Click here to Skip to main content
6,596,602 members and growing! (20,254 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate License: The Code Project Open License (CPOL)

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

By Massimiliano Conte

Using Iphlpapi.dll to retrieve network adapters info
VC6Win2K, MFC, Dev
Posted:29 Oct 2003
Views:55,265
Bookmarked:20 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 5.35 Rating: 3.93 out of 5
1 vote, 4.3%
1
1 vote, 4.3%
2
1 vote, 4.3%
3
7 votes, 30.4%
4
13 votes, 56.5%
5

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)

About the Author

Massimiliano Conte


Member
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!
Occupation: Software Developer (Senior)
Company: ElsagDatamat
Location: Italy Italy

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
QuestionSame on C# .NET Pinmembersarunvij23:34 27 Jul '08  
GeneralA small mistake in allocationg memory ; - ) PinmemberIngenious0011:14 12 Apr '07  
GeneralIn Java PinmemberAenema5:53 23 Feb '07  
Generalactually, this is not bytes... PinsussAnonymous4:26 9 Feb '05  
GeneralReceived & Sent bytes PinmemberZbsfojwf8:27 20 Oct '04  
GeneralVB.NETPort? PinmemberandrewPP6:42 14 Sep '04  
Generalabout 4GB max Pinmemberbionicman45213:19 9 May '04  
GeneraldwInOctets Rollover PinsussTom Garratt5:17 21 Apr '04  
GeneralShould consider using GetIfEntry Pinmembersixsixseven3:48 12 Jan '04  
GeneralAttention NT4 < SP6 Pinmemberc2j25:11 7 Nov '03  
GeneralThis thing >> which I am looking for >> thanks Pinmember_skidrow_vn_9:03 4 Nov '03  
GeneralJust the thing I needed ... Pinmemberwhizer9:02 30 Oct '03  
GeneralWhich OSes? Pinmemberdog_spawn6:28 30 Oct '03  
GeneralRe: Which OSes? PinmemberJonathanLivingstone6:56 30 Oct '03  
GeneralRe: Which OSes? Pinmemberc2j25:10 7 Nov '03  
Generalmodems PinmemberNGS 5496726:28 30 Oct '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Oct 2003
Editor: Nishant Sivakumar
Copyright 2003 by Massimiliano Conte
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project