Click here to Skip to main content
15,912,207 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Outprocess Control Pin
Tracy Software29-Dec-11 0:35
Tracy Software29-Dec-11 0:35 
AnswerRe: Outprocess Control Pin
Tracy Software29-Dec-11 0:55
Tracy Software29-Dec-11 0:55 
QuestionTAPI Call program Pin
Hadi Dayvary28-Dec-11 22:05
professionalHadi Dayvary28-Dec-11 22:05 
QuestionHow to retrieve another cookies from InternetGetCookie() when the website sets multiple cookies. Pin
Avinash Pachar27-Dec-11 21:22
Avinash Pachar27-Dec-11 21:22 
QuestionRe: How to retrieve another cookies from InternetGetCookie() when the website sets multiple cookies. Pin
David Crow28-Dec-11 4:20
David Crow28-Dec-11 4:20 
AnswerRe: How to retrieve another cookies from InternetGetCookie() when the website sets multiple cookies. Pin
Chuck O'Toole28-Dec-11 5:16
Chuck O'Toole28-Dec-11 5:16 
QuestionRe: How to retrieve another cookies from InternetGetCookie() when the website sets multiple cookies. Pin
Avinash Pachar3-Jan-12 21:21
Avinash Pachar3-Jan-12 21:21 
QuestionRe: How to retrieve another cookies from InternetGetCookie() when the website sets multiple cookies. Pin
Avinash Pachar9-Jan-12 22:28
Avinash Pachar9-Jan-12 22:28 
QuestionWriting data on exif Pin
eduardocardoso27-Dec-11 6:09
eduardocardoso27-Dec-11 6:09 
QuestionSystem Query Pin
john563227-Dec-11 0:16
john563227-Dec-11 0:16 
AnswerRe: System Query Pin
Randor 27-Dec-11 11:46
professional Randor 27-Dec-11 11:46 
GeneralRe: System Query Pin
john563227-Dec-11 19:36
john563227-Dec-11 19:36 
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 

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.