Click to See Complete Forum and Search --> : CPU-Load in percent?


vander
July 24th, 2003, 03:53 AM
How can I get the CPU-Load (in percent) for any thread/process, like in the Powertoys 'WinTop'? I've read a lot in the forum, but I can't find a solution for this special purpose.
I want coding in VB, but I can understand examples in C++ if it's not to difficult :rolleyes:

Lee Peart
July 24th, 2003, 10:02 AM
Hi,

I'm not sure if there is maybe a better way than this, but you could try using GetProcessTimes() to do what you want.

Have Fun,
Lee

mailtokannan
July 24th, 2003, 01:37 PM
Use the following code, you can get any performance data using this helper functions...
Refer MSDN "Windows NT Performance Counters" section to know about various available performance counter objects...

-----------------------------------------------------------------------------
#include <afxtempl.h>
#include <pdh.h>
#include <pdhmsg.h>

BOOL Initialize(void);
void Uninitialize(void);

//// COUNTERS ////
int AddCounter(const char *pszCounterName,HCOUNTER *hCounter);
BOOL RemoveCounter(HCOUNTER hCounter);
long GetCounterValue(HCOUNTER hCounter);
//// DATA ////
BOOL CollectQueryData(void);

-----------------------------------------------------------------------------

#include "perfmon.h"

HQUERY hQuery; // the query to the PDH

BOOL Initialize()
{
if (PdhOpenQuery(NULL, 1, &hQuery) != ERROR_SUCCESS) return false;
return true;
}

void Uninitialize()
{
PdhCloseQuery(&hQuery);
}

int AddCounter(const char *pszCounterName,HCOUNTER *pCounter)
{
// add to current query
HCOUNTER temp;
if (PdhAddCounter(hQuery, pszCounterName, (DWORD)pCounter, &temp) != ERROR_SUCCESS)
{
delete pCounter; // clean mem
return 0;
}

*pCounter=temp;
return 1;
}

BOOL RemoveCounter(HCOUNTER pCounter)
{
if (PdhRemoveCounter(pCounter) != ERROR_SUCCESS) return false;
return true;
}

BOOL CollectQueryData()
{
if (PdhCollectQueryData(hQuery) != ERROR_SUCCESS) return false;
return true;
}


long GetCounterValue(HCOUNTER pCounter)
{
PDH_FMT_COUNTERVALUE pdhFormattedValue;

// get the value from the PDH
if (PdhGetFormattedCounterValue(pCounter, PDH_FMT_LONG, NULL, &pdhFormattedValue) != ERROR_SUCCESS)
return false;

// test the value for validity
if (pdhFormattedValue.CStatus != ERROR_SUCCESS)
return false;
// return the value
return pdhFormattedValue.longValue;
}

-----------------------------------------------------------------------------
#include "perfmon.h"

#define COUNTER_NAME "\\\\chn-dcid17\\Processor(_TOTAL)\\% Processor Time"

int main(int argc, char* argv[])
{
HCOUNTER *hcounter;
hcounter=new HCOUNTER[10];
int a_nCnt=0;
long result;
BOOL bReturnValue = FALSE;

Initialize();
AddCounter(COUNTER_NAME, &hcounter[a_nCnt++]);

//if u receive begin
while(1)
{
for(int x=0;x<a_nCnt;x++)
{
bReturnValue = CollectQueryData();
result = GetCounterValue(hcounter[x]);
printf("The Result is%d\n",result);
}
printf("*****************************************************\n");
Sleep(1000);
}

for(int x=0;x<a_nCnt;x++)
RemoveCounter(hcounter[x]);

Uninitialize();
return 1;
}

vander
July 25th, 2003, 09:03 AM
Thanks to mailtokannan and Lee Peart. I forgot to refer that I use Win9x. I already found some Sources about doing this under WinNT/WinXP. In mbm5cpuidle-1.0.zip (Plugin for Motherboard Monitor) is the source for a working cpuusage class, but it don't work with Win9x.

Edit:

I've get a closer look at wintop from the M$ powertoys and at seems to be an vxd for these function. This is the call for getting the information I need 'GetThreadExecTime '. Unfortunately it's only possible from an vxd, so I have to write one or explore the wintop.vxd calling format :(
Why it's so hard to get this info under Win9x? :mad: