|
Hi David,
Thanks for information. I need to get System Ram Status, drive space status, cpu usage and cpu usage by specific process.
Can all these possible using Notification method. Please also send any link of usage the Notification.
Thanks & Regards,
John
|
|
|
|
|
john5632 wrote: Thanks for information. I need to get System Ram Status, drive space status, cpu usage and cpu usage by specific process.
I'm thinking the performance counters might be what you are after.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
john5632 wrote: Thanks for information. I need to get System Ram Status, drive space status, cpu usage and cpu usage by specific process.
Assuming (from your previous posts) that you already have working code for the 'system RAM status' and 'drive space status'... then it would seem that you now need something for per-process cpu usage:
As I have stated before... (and what David Crow suggests...) is that you could potentially use the PDH Functions to Consume Counter Data[^]
If you are looking for something light-weight... then you could investigate using the GetProcessTimes function[^].
The lpKernelTime and lpUserTime from GetProcessTimes[^] can be used to calculate cpu usage for an individual process.
At a lower level I believe what GetProcessTimes is doing internally is calling NtQuerySystemInformation[^] with the SystemProcessInformation index and returning the times stored within the SYSTEM_PROCESS_INFORMATION structure[^] associated with the process.
Best Wishes,
-David Delaune
|
|
|
|
|
Can I use CreateMemoryResourceNotification for user defined memory size, if yes please let me know how to do?
|
|
|
|
|
john5632 wrote: Can I use CreateMemoryResourceNotification for user defined memory size, if yes please let me know how to do?
No, you cannot. The function is designed to notify an application of a low physical memory condition.
|
|
|
|
|
Thanks for your reply....
Can you suggest any way to get Network Utilization such like as Task Manager...?
|
|
|
|
|
john5632 wrote: Can you suggest any way to get Network Utilization such like as Task Manager...?
Hmmm, Interesting question...
Are you having any problems with the technique I suggested in this reply? Measuring Network Utilization[^]
Best Wishes,
-David Delaune
|
|
|
|
|
It gives Network Link Speed but I need Network Utilization like Task Manager...
|
|
|
|
|
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:
#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:
GetNetworkTraffic(network_device_sample_1);
Sleep(1000); 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.
|
|
|
|
|
I'm having trouble with cating 2 values together, one of the buffers is not null terminated.
So I made a buffer to a pointer, that I passed to a function.
int iNSN = wcslen(pzAddServerName)*sizeof(WCHAR);
szAddServerName = new WCHAR[iNSN+1];
wcsncpy_s(szAddServerName, iNSN, pzAddServerName, wcslen(pzAddServerName) );
I learned that the wcsncpy will automatically null terminate the szAddServerName, which is a copy of the pointer, which is not null terminated.
I then made a copy of the master list, with a new buffer, and assumed that the wcsncpy will null terminate the buffer. I added extra space for the new name and the delimiter plus null terminator.
int iASN = wcslen(pzEnumeratedServerNames)+wcslen(szAddServerName);
szNewServerNames = new WCHAR[iASN+4];
wcsncpy_s(szNewServerNames, iASN, pzEnumeratedServerNames, wcslen(pzEnumeratedServerNames) );
And then if the new name is not in the list of names, tried to cat it to the list of names.
if (pzMatch == NULL) {
wcsncat_s(szNewServerNames, wcslen(szNewServerNames), szAddServerName, wcslen(szAddServerName) );
}
I know that I can walk through the code to figure out which one is not null terminated, but I really need to get a rock solid rule in my head, to avoid this in the future, where I just do it right the first time.
|
|
|
|
|
jkirkerx wrote: int iNSN = wcslen(pzAddServerName)*sizeof(WCHAR);
szAddServerName = new WCHAR[iNSN+1];
should really be:
int iNSN = wcslen(pzAddServerName);
szAddServerName = new WCHAR[iNSN+1];
the 'new' operator knows the size of the type and will allocate the correct number of bytes for iNSN +1 WCHARs, behind the scenes.
jkirkerx wrote: wcsncat_s(szNewServerNames, wcslen(szNewServerNames), szAddServerName, wcslen(szAddServerName) );
then one of your wcslen_t's will cause an access violation, since wcslen is looking for the null terminator.
if one of your strings is not null-terminated, then you must know the max length allowed for it. you can use that for your length to wcnscat_s
|
|
|
|
|
That's right, it's the max size,
int iASN = wcslen(pzEnumeratedServerNames)+wcslen(szAddServerName)+1;
szNewServerNames = new WCHAR[iASN];
wcsncpy_s(szNewServerNames, iASN, pzEnumeratedServerNames, wcslen(pzEnumeratedServerNames) );
// Cat the new value to the new buffer if not already present
if (pzMatch == NULL) {
// one of these is not null terminated
wcsncat_s(szNewServerNames, iASN, szAddServerName, wcslen(szAddServerName) );
}
|
|
|
|
|
Hello Experts,
Is there any way to find out Network Utilization?
|
|
|
|
|
What do you mean by Network Utilization?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Hi,
Have a look at this older post:
How to get the network adapter connection speed using IP Helper Library.[^]
Using the code snippet above you could get the maximum speed available. (Always read the MSDN article! If you want to support gigabit speeds... you will need to modify my code to use the MIB_IF_ROW2 structure[^])
If you wanted to know what percentage of the available network capacity is being currently utilized... just modify the code sample above and obtain the:
MIB_IFROW[^].dwInOctets for incoming
MIB_IFROW[^].dwOutOctets for outgoing
1.) Save these values...
2.) Wait 1 second. (Or wait a smaller amount such as 250 milliseconds)
3.) Read the values again... and subtract current value from last value. (if you waited a short amount of time such as 250 milliseconds then multiply by 4 to estimate 1000 milliseconds)
Actually the MIB_IF_ROW2 structure[^] looks like a better candidate... it uses unsigned 64 bit integers for the counters. So I suggest that you update the function to use that instead.
Also... If you look closely at the link I gave you above... David Crow and mikey_eff[^] also shows how to get these same values using PDH (Performance Counters). So you have two choices.
I often have people ask me why I always choose the lower level functions... and PDH is a perfect example. Users have the ability to disable performance counters. So if you write an application that reads cpu/network statistics using Performance Counters... and a user named Bob disabled his performance counters... then your application will fail on Bob's computer. Microsoft Windows is somewhat like an Onion[^] and the PDH network counters are actually reading the same values accessed by the IP Helper[^] library that are tracked by the NT modular kernel. The Performance Data Helper is continually reading all these values and stores them in the registry...
Anyway it looks like I am rambling again, hope it makes sense. I better get back to eating pumpkin pie.
Best Wishes,
-David Delaune
|
|
|
|
|
hello everyone..
i searched Google but still cant find a sample code for my needs
i need a sample code for counting key-press
im going to use this code to detect a human or macro key-press
human key-press should have a millisecond delay,while a lot of macro programs dont have a delay,some sort of anti key-press spam.
c++ would be nice
sorry for my bad english
|
|
|
|
|
You cannot reach the 1 millisecond resolution on Windows (at least in user code).
Sorry, I misunderstood your requirements.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
modified 24-Dec-11 10:44am.
|
|
|
|
|
CPallini wrote: You cannot reach the 1 millisecond resolution on Windows (at least in user code).
Of course you can. You won't get it in each and every way you could try, however there are ways to get pretty good timing resolution and accuracy (could be close to the microsecond). Here are two of them:
- multimedia timers, with timeSetEvent() from winMM.dll;
- performance counters, with QueryPerformanceCounter() from kernel32.dll
|
|
|
|
|
You cannot process key state with a sampling period of 1 msec.
Ok, it looks I need more coffee...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
modified 24-Dec-11 10:45am.
|
|
|
|
|
the OP isn't going to sample key states, he asked for sample code. He could attach a keyboard hook and note the time, with better than millisecond resolution, and then, when text comes in too fast to his liking, decide it is a bot rather than a human entrant.
|
|
|
|
|
OK, I see, you are right.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Have you considered SetWindowsHookEx() with WH_KEYBOARD ?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
Use SetWindowsHookEx with WM_KEYBOARD_LL
if bit LLKHF_INJECTED in KBDLLHOOKSTRUCT.flags is set,then this key press is injected by SendInput function.
if the key press is simulated by posting WM_KEYDOWN directly,the hook procedure wont be called at all
|
|
|
|
|
Ths code does just what you asked for.
Ok, the timing is not well..... just press the keys down rapidly to get a good time measure.
#include <windows.h>
#include <iostream>
#include <time.h>
#include <cmath>
using namespace std;
int main(int argc, char *argv[])
{
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD ir;
DWORD dwRead = 0;
bool bLoop = true;
clock_t timer_start, timer_stop;
int Number_of_Keypresses=0;
ZeroMemory(&ir,sizeof(INPUT_RECORD));
timer_start = clock ();
timer_stop = clock ();
while(bLoop)
{
if( ReadConsoleInput( hInput,&ir,1,&dwRead) )
{
if(ir.EventType == KEY_EVENT )
{
if(ir.Event.KeyEvent.bKeyDown )
{
timer_start = clock ();
Number_of_Keypresses++;
switch(ir.Event.KeyEvent.wVirtualKeyCode)
{
case VK_DOWN:
cout << "DOWN" << endl;
break;
case VK_UP:
cout << "UP" << endl;
break;
case VK_RIGHT:
cout << "RIGHT" << endl;
break;
case VK_LEFT:
cout << "LEFT" << endl;
break;
case VK_ESCAPE:
bLoop = false;
default:
break;
}
}
else
{
timer_stop = clock ();timer_start = clock ();
}
cout <<"Number_of_Keypresses = "<< Number_of_Keypresses<<"\n";
cout <<"Millisecond delay = "<<abs(timer_start - timer_stop ) <<" (from KEY_EVENT_keydown to KEY_EVENT_KeyUp)" << endl;
cout <<"______________________________________________________\n";
}
}
}
return 0;
}
|
|
|
|
|