From: Tomasz Littau on
I have a problem to get information about cpu usage by process, i have
function but all the time have error PDH_CSTATUS_NO_OBJECT

what`s wrong ?

int main(int argc, char* argv[])
{
UINT index;
char ProcCpuUsageCounterPath[256];
HQUERY hProcCpuUsageQuery;
HCOUNTER hProcCpuUsageCounter;
PDH_FMT_COUNTERVALUE fmtValue;
PDH_STATUS pdhStatus;

strcpy( ProcCpuUsageCounterPath, "\\Process(explorer)\\\% Processor Time");
printf ("Going to query for counter %s\n", ProcCpuUsageCounterPath);

pdhStatus = PdhOpenQuery (NULL, 0, &hProcCpuUsageQuery);
switch( pdhStatus )
{
case ERROR_SUCCESS:
printf ("PdhOpenQuery SUCCESS\n");
break;
case PDH_INVALID_ARGUMENT:
printf ("PdhOpenQuery ERROR - PDH_INVALID_ARGUMENT\n");
getch();
return 0;
break;
case PDH_MEMORY_ALLOCATION_FAILURE:
printf ("PdhOpenQuery ERROR - PDH_MEMORY_ALLOCATION_FAILURE\n");
getch();
return 0;
break;
default:
printf ("PdhOpenQuery ERROR - UNKNOWN\n");
getch();
return 0;
break;
}


pdhStatus = PdhAddCounter( hProcCpuUsageQuery, ProcCpuUsageCounterPath,
0, &hProcCpuUsageCounter );
switch( pdhStatus )
{
case ERROR_SUCCESS:
printf ("PdhAddCounter SUCCESS");
break;
case PDH_CSTATUS_BAD_COUNTERNAME:
printf ("PdhAddCounter ERROR - PDH_CSTATUS_BAD_COUNTERNAME\n");
getch();
return 0;
break;
case PDH_CSTATUS_NO_COUNTERNAME:
printf ("PdhAddCounter ERROR - PDH_CSTATUS_NO_COUNTERNAME\n");
getch();
return 0;
break;
case PDH_CSTATUS_NO_MACHINE:
printf ("PdhAddCounter ERROR - PDH_CSTATUS_NO_MACHINE\n");
getch();
return 0;
break;
case PDH_CSTATUS_NO_OBJECT:
printf ("PdhAddCounter ERROR - PDH_CSTATUS_NO_OBJECT\n");
getch();
return 0;
break;
case PDH_FUNCTION_NOT_FOUND:
printf ("PdhAddCounter ERROR - PDH_FUNCTION_NOT_FOUND\n");
getch();
return 0;
break;
case PDH_INVALID_ARGUMENT:
printf ("PdhAddCounter ERROR - PDH_INVALID_ARGUMENT\n");
getch();
return 0;
break;
case PDH_INVALID_HANDLE:
printf ("PdhAddCounter ERROR - PDH_INVALID_HANDLE\n");
getch();
return 0;
break;
case PDH_MEMORY_ALLOCATION_FAILURE:
printf ("PdhAddCounter ERROR - PDH_MEMORY_ALLOCATION_FAILURE\n");
getch();
return 0;
break;
default:
printf ("PdhAddCounter ERROR - UNKNOWN\n");
getch();
return 0;
break;
}
while( !kbhit() )
{
PdhCollectQueryData (hProcCpuUsageQuery);

PdhGetFormattedCounterValue (hProcCpuUsageCounter,
PDH_FMT_LONG, 0,
&fmtValue);
printf ("CPU Usage calc.exe %ld\n", index,
fmtValue.longValue);
Sleep (1000);
}

PdhRemoveCounter (hProcCpuUsageCounter);

PdhCloseQuery (hProcCpuUsageQuery);

return 0;
}
--
_____________________________

tlen: lemkat
gg: 4891654

http:///www.lemkat.soft-pc.pl
_____________________________