Click here to Skip to main content
15,899,124 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
SuggestionRe: System Query Pin
David Crow28-Dec-11 4:18
David Crow28-Dec-11 4:18 
GeneralRe: System Query Pin
Randor 28-Dec-11 11:11
professional Randor 28-Dec-11 11:11 
GeneralRe: System Query Pin
john563227-Dec-11 20:27
john563227-Dec-11 20:27 
GeneralRe: System Query Pin
Randor 28-Dec-11 11:08
professional Randor 28-Dec-11 11:08 
GeneralRe: System Query Pin
john563229-Dec-11 18:23
john563229-Dec-11 18:23 
GeneralRe: System Query Pin
Randor 29-Dec-11 18:47
professional Randor 29-Dec-11 18:47 
GeneralRe: System Query Pin
john563229-Dec-11 20:29
john563229-Dec-11 20:29 
GeneralRe: System Query Pin
Randor 29-Dec-11 21:57
professional Randor 29-Dec-11 21:57 
Hi,
john5632 wrote:
It gives Network Link Speed but I need Network Utilization like Task Manager...


I apologize for not being more clear. I will try again. This older post gives you total link-speed capability.

How to get the network adapter connection speed using IP Helper Library.[^]

You need this top number... how else would you calculate the percentage of utilization?

If you look again at the post: How to calculate the percentage of network utilization[^]

I clearly tells you that you need to obtain:

MIB_IFROW[^].dwInOctets for incoming
MIB_IFROW[^].dwOutOctets for outgoing

There are 8 bits to the byte. An Octet[^] is 8 bits. So you can accurately say that an octet is equivalent to a byte. In other words... MIB_IFROW[^].dwInOctets and MIB_IFROW[^].dwOutOctets are an accumulative count of the number of bytes that have been sent/recieved through the network device.

In the reply I gave you... I go on further to say that the MIB_IF_ROW2 structure[^] looks like a better choice because it uses unsigned 64 bit integers which would support higher speeds such as gigabit.

I am guessing that you are looking for a copy-and-paste snippit:

C++
#include "Iphlpapi.h"
#pragma comment(lib, "iphlpapi.lib")

vector<pair<string,pair<size_t,size_t>>> network_device_sample_1;
vector<pair<string,pair<size_t,size_t>>> network_device_sample_2;

VOID GetNetworkTraffic(vector<pair<string,pair<size_t,size_t>>>& sample)
{
	BYTE *pBuf=NULL;
	DWORD dwSize=0;
	DWORD dwResult=0;
	BOOL bConnected=FALSE;
	PMIB_IFTABLE pMIBTable;
	CString str;
	GetIfTable(NULL,&dwSize,FALSE);
	pBuf=new BYTE[dwSize];
	pMIBTable=reinterpret_cast<PMIB_IFTABLE>(pBuf);
 
	if(NO_ERROR == GetIfTable(pMIBTable,&dwSize,FALSE))
	{
		for(UINT i=0; i < pMIBTable->dwNumEntries; ++i)
		{
			if(MIB_IF_TYPE_ETHERNET == pMIBTable->table[i].dwType)
			{
				sample.push_back(make_pair(std::string((char *)pMIBTable->table[i].bDescr),make_pair(pMIBTable->table[i].dwInOctets,pMIBTable->table[i].dwOutOctets)));
			}
		}
	}
	delete []pBuf;
}

DWORD CalculateBytesTransferred(pair<string,pair<size_t,size_t>> device)
{
	std::string description = device.first;
	vector<pair<string,pair<size_t,size_t>>>::iterator it = network_device_sample_1.begin();
	while(it != network_device_sample_1.end())
	{
		if(it->first == device.first)
		{
			ATLTRACE ("Statistics for %s.\n",device.first.c_str());

			DWORD dwTotalIncoming = device.second.first - it->second.first;
			DWORD dwTotalOutgoing = device.second.second - it->second.second;

			ATLTRACE ("Device has recieved %ld bytes per second.\n",dwTotalIncoming);
			ATLTRACE ("Device has sent %d bytes per second.\n",dwTotalOutgoing);
		}
		++it;
	}
	return 0;
}


You would use it like this:

C++
GetNetworkTraffic(network_device_sample_1);
Sleep(1000); //You should probably replace this with something else... I am just Sleeping() for your code sample...
GetNetworkTraffic(network_device_sample_2);
for_each(network_device_sample_2.begin(), network_device_sample_2.end(), CalculateBytesTransferred);


Best Wishes,
-David Delaune

[UPDATE]
My code sample is poorly written... but I honestly do not feel like doing your work for you. I wrote this crap right here impromptu..

Some things that could be improved:
1.) The overall architecture of these functions should be redesigned... the CalculateBytesTransferred relies on a global std::list and thats just crap.
2.) The CalculateBytesTransferred function is comparing device descriptions... this is completely incorrect and it should be comparing the device GUID at: MIB_IFROW[^].wszName

Anyway you need to do the rest... it does not calculate the percentage... you need to use the other code I gave you to get the link speed. I'm done.

modified 30-Dec-11 4:29am.

QuestionJoining 2 values with wcsncat_s Pin
jkirkerx26-Dec-11 10:52
professionaljkirkerx26-Dec-11 10:52 
AnswerRe: Joining 2 values with wcsncat_s Pin
Chris Losinger26-Dec-11 10:59
professionalChris Losinger26-Dec-11 10:59 
GeneralRe: Joining 2 values with wcsncat_s Pin
jkirkerx26-Dec-11 11:13
professionaljkirkerx26-Dec-11 11:13 
QuestionNetwork Utilization Pin
john563225-Dec-11 22:13
john563225-Dec-11 22:13 
AnswerRe: Network Utilization Pin
Richard MacCutchan26-Dec-11 6:57
mveRichard MacCutchan26-Dec-11 6:57 
AnswerRe: Network Utilization Pin
Randor 26-Dec-11 12:17
professional Randor 26-Dec-11 12:17 
QuestionDetect keypress count Pin
juver escolta23-Dec-11 6:04
juver escolta23-Dec-11 6:04 
AnswerRe: Detect keypress count Pin
CPallini23-Dec-11 7:34
mveCPallini23-Dec-11 7:34 
AnswerRe: Detect keypress count Pin
Luc Pattyn24-Dec-11 2:21
sitebuilderLuc Pattyn24-Dec-11 2:21 
GeneralRe: Detect keypress count Pin
CPallini24-Dec-11 3:59
mveCPallini24-Dec-11 3:59 
AnswerRe: Detect keypress count Pin
Luc Pattyn24-Dec-11 4:06
sitebuilderLuc Pattyn24-Dec-11 4:06 
GeneralRe: Detect keypress count Pin
CPallini24-Dec-11 4:42
mveCPallini24-Dec-11 4:42 
QuestionRe: Detect keypress count Pin
David Crow23-Dec-11 9:28
David Crow23-Dec-11 9:28 
AnswerRe: Detect keypress count Pin
Gary R. Wheeler23-Dec-11 23:56
Gary R. Wheeler23-Dec-11 23:56 
AnswerRe: Detect keypress count Pin
Lactoferrin24-Dec-11 1:45
Lactoferrin24-Dec-11 1:45 
AnswerRe: Detect keypress count Pin
Software_Developer24-Dec-11 4:54
Software_Developer24-Dec-11 4:54 
QuestionHow GetObjectSchema() to work ? Pin
André Dewispelaere23-Dec-11 3:43
André Dewispelaere23-Dec-11 3:43 

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.