HQUERY hQuery;
HCOUNTER hCounter;
if(ERROR_SUCCESS == PdhOpenQuery(0, 0, &hQuery))
{
if(ERROR_SUCCESS == PdhAddCounter (hQuery,TEXT("\\Processor(0)\\% Processor Time"),0,&hCounter))
{
int i = 0;
while(i < 5)
{
++i;
if(ERROR_SUCCESS == PdhCollectQueryData(hQuery))
{
PDH_FMT_COUNTERVALUE val;
if(ERROR_SUCCESS == PdhGetFormattedCounterValue(hCounter,PDH_FMT_LONG|PDH_FMT_NOSCALE,0, &val))
{
printf("\nCPU is at %d %%",val.longValue);
}
}
Sleep(500); //wait for some time(not absolutely needed though, but better to wait some time atleast!)
}
PdhRemoveCounter(hCounter);
}
PdhCloseQuery(hQuery);
}
Bikram Singh