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...
I'm just speaking theoretical here, because I never used the pdh functions in depth, but ...
Are you working with a localized version of Windows (not English) ?
In that case, what happens to the names of the objects, might they be translated to the lcoalized language too ? I mean "Processor" and "% Processor Time". At least PerfMon shows them to me in my german Windows version with the german names (in the object list ...)
PerfMon on my Win2k works correctly.
But... I have a rus Windows version... so the name of a counter is russian. But I don't think that problem is in such way...
So the whole name is \\NAME_OF_MY_MACHINE\Ïðîöåññîð(_Total)\% ç*ãðóæå**îñòè ïðîöåññîð* :)
At least I don't hope so... as I said, just a theoretical question to be sure since I don't see any other failures in your code ... and I saw perfmon reporting me object names in german on my Windows version.
I asked before and perhaps I missed the answer, but... Do you build your application as Unicode ?
I don't know but.. may be problem whith szMachineName or dwInstanceIndex..?
Actually I do think that is the problem, the lookup is done in the registry, though the below states it also stores the english version also, perhaps it's a trump because of the locale. Convert the counter name to what you posted above and use that. I've only used english for the PdhXXX functions. Here is msdn's link to surf around on.Quote:
Originally posted by Punga
PerfMon on my Win2k works correctly.
But... I have a rus Windows version... so the name of a counter is russian. But I don't think that problem is in such way...
So the whole name is \\NAME_OF_MY_MACHINE\Ïðîöåññîð(_Total)\% ç*ãðóæå**îñòè ïðîöåññîð* :)
http://msdn.microsoft.com/library/de...addcounter.asp
Quote:
Retrieving Counter Names and Explanations
Object type names, counter names, object explanations, and counter explanations are not made directly available in the performance data structures. Instead, the performance data structures contain indexes you can use to locate where the names and explanations for each object and counter can be found. The ObjectNameTitleIndex and ObjectHelpTitleIndex members of the PERF_OBJECT_TYPE structure contain the indexes to the object name and explanation, respectively. The CounterNameTitleIndex and CounterHelpTitleIndex members of the PERF_COUNTER_DEFINITION structure contain the indexes to the counter name and explanation, respectively.
To access the names and explanations, read the Counter and Help values in the following registry key.
HKEY_LOCAL_MACHINE\
SOFTWARE\
Microsoft\
Windows NT\
CurrentVersion\
Perflib\
langid
The langid is the ASCII representation of the 3-digit hexadecimal language identifier. For example, the U.S. English langid is 009. In a non-English version of Windows, counters are stored in both the native language of the system and in English.
Why is it everyone can't just use english and eat at McDonalds :p
/Promoting the american stereotype...
Sorry to tell you that (and I can't tell that this is fine), but:
1) I tried your code sniplet and it didn't work for me
2) I changed the names to the german translations and it worked ...
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 = "Prozessor";
pe.szInstanceName = "_Total";
pe.szParentInstance = NULL;
pe.dwInstanceIndex = 0;
pe.szCounterName = "Prozessorzeit (%)";
pdhResult = PdhMakeCounterPath(&pe, szCounterPath, &dwPathSize, 0);
pdhResult = PdhAddCounter(hQuery, szCounterPath, 0, &hCounter);
So you have to try the russian equivalents (sorry, or is there a difference between russian and white-russian / belaruss ? Im not sure, I should know that living here not to far away from you, but I have to admit I don't) and I think you have to use unicode for that.
I need to build my application as Unicode ? Tell me plz how to do this;) And how should I write names of PDH_COUNTER_PATH_ELEMENTS structure elements, whith _T(...) or not ?
That is why on my previous posts I kept asking for him to run perfmon and look at the counter name.Quote:
Originally posted by Marco F
Sorry to tell you that (and I can't tell that this is fine), but:
1) I tried your code sniplet and it didn't work for me
2) I changed the names to the german translations and it worked ...
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 = "Prozessor";
pe.szInstanceName = "_Total";
pe.szParentInstance = NULL;
pe.dwInstanceIndex = 0;
pe.szCounterName = "Prozessorzeit (%)";
pdhResult = PdhMakeCounterPath(&pe, szCounterPath, &dwPathSize, 0);
pdhResult = PdhAddCounter(hQuery, szCounterPath, 0, &hCounter);
So you have to try the russian equivalents (sorry, or is there a difference between russian and white-russian / belaruss ? Im not sure, I should know that living here not to far away from you, but I have to admit I don't) and I think you have to use unicode for that.
I think you must use unicode or at least you ave to explizitlly call the UNICODE-version of the pdh-functions. I think your russian characters are not in the "standard-Ansi" range, but you should know that better than me.
But the program won't work under the eng. version Win2k... Or I mistake?
Forcing Unicode-PDH-Functions:
=========================
PDH_STATUS pdhResult = 0;
wchar_t szCounterPath[1024];
DWORD dwPathSize = 1024;
PDH_COUNTER_PATH_ELEMENTS_W pe;
HQUERY hQuery;
HQUERY hCounter;
pdhResult = PdhOpenQuery( NULL, 0, &hQuery );
pe.szMachineName = 0l;
pe.szObjectName = L"Prozessor";
pe.szInstanceName = L"_Total";
pe.szParentInstance = NULL;
pe.dwInstanceIndex = 0;
pe.szCounterName = L"Prozessorzeit (%)";
pdhResult = PdhMakeCounterPathW(&pe, szCounterPath, &dwPathSize, 0);
pdhResult = PdhAddCounterW(hQuery, szCounterPath, 0, &hCounter);
===================
yes, the program will not work in ANY W2K version different from russian. Sorry, I don't know a better way. Perhaps there is one, but I don't know.
Only thing you could do is to localize the strings via a string table for your different target locales.
Use my sample which helps you getting cpu usage for whole system or for a single process:
How to get CPU usage by performance counters (without PDH)
-->> dudiav
I can't extract the downloaded archive *.zip.