How can I Get (in '%') CPU performance? Task Manager(WinNt) shows such information.
Printable View
How can I Get (in '%') CPU performance? Task Manager(WinNt) shows such information.
NtQuerySystemInformation....
Note, that 'NtQuerySystemInformation()' does only work on systems NT or higher...
Sent by PM:
Are you sure that NtQuerySystemInforamtion returns cpu perfomance(in '%') even fo each process? Tell me please in what structure such information is contained. I didn't see anything about it...
Take a look at the following thread...
WMI will provide that information.
Tell me plz where I can find Info how to work whith WMI or may be You'll help me..;)
Look at Windows Management Instrumentation (Windows Management Instrumentation SDK: Platform SDK). Specifically, look at Monitoring Performance Data. You can use the Win32_PerfRawData_PerfProc_Process class but if you are only using 2000/XP or only using XP, there are other classes that are easier for you and the system.
In the Platform SDK samples there are a few WMI samples. One of them (I think called simple) has the basics of what you need to use WMI. It will take a little time to learn about WMI, but once you know how to use it, the knowledge can be used for many other things too.
Tell me what's wrong.
I use PDH.
PDH_STATUS pdhResult = 0;
TCHAR szCounterPath[1024];
DWORD dwPathSize = 1024;
PDH_COUNTER_PATH_ELEMENTS pe;
HQUERY hQuery;
HQUERY hCounter;
pdhResult = PdhOpenQuery( NULL, 0, &hQuery );
pe.szMachineName = 0l;
pe.szObjectName = "Processor";
pe.szInstanceName = "_Total";
pe.szParentInstance = NULL;
pe.dwInstanceIndex = 0;
pe.szCounterName = "% Processor Time";
pdhResult = PdhMakeCounterPath(&pe, szCounterPath, &dwPathSize, 0);
pdhResult = PdhAddCounter(hQuery, szCounterPath, 0, &hCounter);
And here PdhAddCounter returns PDH_CSTATUS_NO_OBJECT.
What I have done incorrectly?
I don't know; I have not used the Performance Data Helper.
Did you also check pdhResult after calling PdhMakeCounterPath? Are you sure the values you are using in pe are valid names?
Have you looked at relevant sample programs? It is essentially certain that there are sample uses of the Performance Data Helper provided by Microsoft somewhere, either in the Platform SDK or in a KB article or both. Also there might be a CodeGuru article.
Are sure the code you posted is the code your using? Just asking cause it works fine on my XP machine. Can you start the performance monitor and get a value for the processor time? Is it a different name under NT? I don't recall it being...but...how about nulling out the machinename also?
There are examples on MSDN...
I changed you code a bit, just to grab the first processors stats, see if it works, the double query is necassary to get the correct CPU % ie: fill all the values...for the query set.
Code:PDH_STATUS pdhResult = 0;
TCHAR szCounterPath[1024];
DWORD dwPathSize = 1024;
PDH_COUNTER_PATH_ELEMENTS pe = {0};
HQUERY hQuery;
HQUERY hCounter;
pdhResult = PdhOpenQuery( NULL, 0, &hQuery );
pe.szObjectName = "Processor(0)";
pe.szCounterName = "% Processor Time";
pdhResult = PdhMakeCounterPath(&pe, szCounterPath, &dwPathSize, 0);
pdhResult = PdhAddCounter(hQuery, szCounterPath, 0, &hCounter);
pdhResult = PdhCollectQueryData(hQuery);
Sleep(2000);
pdhResult = PdhCollectQueryData(hQuery);
PDH_FMT_COUNTERVALUE stFormattedValue = {0};
pdhResult = PdhGetFormattedCounterValue( hCounter
, PDH_FMT_LONG
, NULL
, &stFormattedValue
);
Just to be sure:
Are PdhOpenQuery() and PdhMakeCounterPath() returning ERROR_SUCCESS ???
Or are you working with a unicode build ? Try to encapsulate the sz... members with _T(...)
[Merged threads]
Of course I checked all return values of previous functions. I have a problem whith PdhAddCounter().
And I work under Win2000.
May be somebody can show me working code whith correctly filled structure PDH_COUNTER_PATH_ELEMENTS?
I... don't know where I mistake...
I do not have access to my W2k machines at the moment, but I highly doubt the above code fails under w2k. Again, START perfmon, what is the counter name (there is a point behind my madness here such as lanquage)? Can you display the cpu % in perfmon.Quote:
Originally posted by Punga
And I work under Win2000.
May be somebody can show me working code whith correctly filled structure PDH_COUNTER_PATH_ELEMENTS?
I... don't know where I mistake...