通过winreg找到perfmon计数器id [英] Finding perfmon counter id via winreg
问题描述
我有一个应用程序通过在winreg.h暴露的API收集Perfmon计数器值 - 为了收集Perfmon计数器值我必须调用 RegQueryValueExW
传入我感兴趣的Perfmon计数器的id,为了获得该ID,我需要查询注册表的Perfmon计数器名称列表,并通过寻找我感兴趣的一个
C ++不是我选择的语言,所以下面是一个可疑的例子,可能有很多语法错误,但你得到的想法:
DWORD IdProcessIndex = 0;
WCHAR * RawStrings = new WCHAR [len];
WCHAR * pCurrent;
DWORD nLenInChars;
//获取ID进程计数器的名称ID
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,LID Process)== 0)
{
IdProcessIndex = _wtoi(pCurrent);
}
pCurrent = pName + wcslen(pName)+ 1;
}
//获取ID进程计数器的数据
WCHAR strIdProcessIndex [32];
_itow(nIdProcessIndex,strIdProcessIndex,10);
RegQueryValueExW(HKEY_PERFORMANCE_DATA,strIdProcessIndex,NULL,NULL,(PBYTE)pData,& len)
b $ b
麻烦的是,在一些机器(安装了Windows CE开发套件的机器),有一个名为ID进程的第二个perfmon计数器,因此上面的查找错误的计数器的ID。 p>
我看不到任何方法来区分两者,而不是他们的顺序 - 在这一刻,我认为我最好的赌注是采取我找到的第一个计数器一个匹配的名字,有更好的选择吗?
(不可能迁移到.Net或类似的东西)
我意识到这是旧的,但如果它有帮助:
- Tim是对的,自己解析二进制数据很困难。准备自己一个痛苦的世界。我建议PDH(封装你的注册表访问),或者如果失败,WMI(虽然注意WMI是更慢)。
- 你不能获取数据只是一个性能计数器(
ID Process
,索引784
)。您需要获取整个对象(Process
,索引230
)。 - 内置对象的ID在所有Windows安装中均为保证相同。所以如果这是你需要的唯一计数器,只需使用
230
。 :)
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)
I realize that this is old, but in case it helps:
- Tim is right, parsing the binary data yourself is difficult. Prepare yourself for a world of pain. I'd recommend PDH (encapsulates the registry accesses for you), or if that fails, WMI (though note that WMI is much slower).
- You cannot get data for just a performance counter (
ID Process
, with index784
). You need to get it for the whole object (Process
, with index230
). - The IDs for built in objects are guaranteed to be the same on all Windows installations. So if this is the only counter you need, just use
230
. :)
这篇关于通过winreg找到perfmon计数器id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!