I have an app that collects Perfmon counter values through the API exposed in winreg.h - in order to collect Perfmon counter values I must make a call to RegQueryValueExW
passing in the id of the Perfmon counter I'm interested in, and in order to obtain that ID I need to query the registry for the list of Perfmon counter names and go through looking for the one I'm interested in
C++ isn't my language of choice, so the following is a shaky example, probably with lots of syntax errors but you get the idea:
DWORD IdProcessIndex = 0;
WCHAR* RawStrings = new WCHAR[ len ];
WCHAR* pCurrent;
DWORD nLenInChars;
// Get the name id of the "ID Process" counter
RegQueryValueExW(HKEY_PERFORMANCE_DATA, COUNTER009, 0, 0, (PBYTE)RawStrings, &len)
pCurrent = (WCHAR*)RawStrings;
while ( (nLenInChars = wcslen(pCurrent)) != 0 && IdProcessIndex == 0 )
{
WCHAR* pName;
pName = pCurrent + nLenInChars + 1;
if ( wcscmp( pName, L"ID Process" ) == 0)
{
IdProcessIndex = _wtoi( pCurrent );
}
pCurrent = pName + wcslen( pName ) + 1;
}
// Get data for the "ID Process" counter
WCHAR strIdProcessIndex[32];
_itow( nIdProcessIndex, strIdProcessIndex, 10 );
RegQueryValueExW(HKEY_PERFORMANCE_DATA, strIdProcessIndex, NULL, NULL, (PBYTE)pData, &len)
Trouble is that on some machines (ones with the Windows CE dev kit installed) there is a second perfmon counter with the name "ID Process", and so the above finds the ID of the wrong counter.
I cant see any way to differentiate between the two other than the order that they are in - at the moment I think my best bet is to take the first counter that I find with a matching name, is there a better option?
(Its not possible to migrate this to .Net or anything like that)