我有一个通过 winreg.h 中公开的 API 收集 Perfmon 计数器值的应用程序 - 为了收集 Perfmon 计数器值,我必须调用 RegQueryValueExW
并传入 Perfmon 计数器的 ID我对此感兴趣,为了获取该 ID,我需要在注册表中查询 Perfmon 计数器名称列表,并仔细查找我感兴趣的 ID
C++ 不是我选择的语言,因此以下是一个不稳定的示例,可能有很多语法错误,但您明白了:
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)
问题在于,在某些机器(安装了 Windows CE 开发工具包的机器)上有第二个名为“ID Process”的 perfmon 计数器,因此上面的代码找到了错误计数器的 ID。
除了它们所处的顺序之外,我看不出有什么方法可以区分两者 - 目前我认为我最好的选择是采用我找到的具有匹配名称的第一个计数器,是否有更好的选择?
(不可能将其迁移到.Net 或类似的东西)
最佳答案
我意识到这已经过时了,但万一它有帮助:
- 蒂姆是对的,自己解析二进制数据很困难。为痛苦的世界做好准备。我建议使用 PDH(为您封装注册表访问),或者如果失败,则使用 WMI(但请注意,WMI 速度要慢得多)。
- 您无法仅获取性能计数器的数据(
ID Process
,索引为784
)。您需要获取整个对象(Process
,索引为230
)。 - 内置对象的 ID 为 guaranteed to be the same在所有 Windows 安装上。因此,如果这是您需要的唯一计数器,只需使用
230
。 :)
关于c++ - 通过 winreg 查找 perfmon 计数器 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1119220/