FedyuninV
FedyuninV

Reputation: 161

Performance counters indexes windows pdh c

I'm writing an utility, which should get current CPU load. At the moment It's working and using \Processor(_Total)\% process time in my localization. For multi-lingual support I'm getting counter name from registry by PdhLookupPerfNameByIndex.

Now code looks like

PdhLookupPerfNameByIndex(NULL, 6, processorTime, &cbPathSize);
PdhLookupPerfNameByIndex(NULL, 238, processor, &cbPathSize);
PDH_COUNTER_PATH_ELEMENTS elements = {NULL, processor, "_Total", NULL, NULL, processorTime};
PdhMakeCounterPath(&elements, fullPath, &cbPathSize, 0);

and I wanna remove hard-coded constants 6 and 238.

Are there some constants which means index for Processor and % process time?

Upvotes: 2

Views: 1787

Answers (1)

Helge Klein
Helge Klein

Reputation: 9085

The indexes differ between systems, you have to determine them dynamically. The procedure is hinted at in the MSDN documentation of PdhLookupPerfNameByIndex:

  1. Retrieve the REG_MULTI_SZ data of the the registry value HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009\Counter

Note: "009" stands for English. That key always exists, even on machines with different language versions of Windows.

  1. Look for your (English) counter/object name in the returned data. Format: index followed by English counter/object name, e.g.:

    6
    % Processor Time

There is your index. Just convert from string to DWORD and use it with PdhLookupPerfNameByIndex.

Upvotes: 2

Related Questions