5,427,813 members and growing! (16,667 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate

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

By JonathanLivingstone

Using Iphlpapi.dll to retrieve network adapters info
VC6, C++Windows, Win2K, MFC, VS6, Visual Studio, Dev

Posted: 29 Oct 2003
Updated: 29 Oct 2003
Views: 46,764
Bookmarked: 15 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
21 votes for this Article.
Popularity: 5.12 Rating: 3.88 out of 5
1 vote, 4.8%
1
1 vote, 4.8%
2
1 vote, 4.8%
3
7 votes, 33.3%
4
11 votes, 52.4%
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

JonathanLivingstone


hi Smile

Occupation: Web Developer
Location: Italy Italy

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionSame on C# .NETmembersarunvij23:34 27 Jul '08  
GeneralA small mistake in allocationg memory ; - )memberIngenious0011:14 12 Apr '07  
GeneralIn JavamemberAenema5:53 23 Feb '07  
Generalactually, this is not bytes...sussAnonymous4:26 9 Feb '05  
GeneralReceived & Sent bytesmemberZbsfojwf8:27 20 Oct '04  
GeneralVB.NETPort?memberandrewPP6:42 14 Sep '04  
Generalabout 4GB maxmemberbionicman45213:19 9 May '04  
GeneraldwInOctets RolloversussTom Garratt5:17 21 Apr '04  
GeneralShould consider using GetIfEntrymembersixsixseven3:48 12 Jan '04  
GeneralAttention NT4 < SP6memberc2j25:11 7 Nov '03  
GeneralThis thing >> which I am looking for >> thanksmember_skidrow_vn_9:03 4 Nov '03  
GeneralJust the thing I needed ...memberwhizer9:02 30 Oct '03  
GeneralWhich OSes?memberdog_spawn6:28 30 Oct '03  
GeneralRe: Which OSes?memberJonathanLivingstone6:56 30 Oct '03  
GeneralRe: Which OSes?memberc2j25:10 7 Nov '03  
GeneralmodemsmemberNGS 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 JonathanLivingstone
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project