PDH 无法通过 PdhAddCounter() 访问总 CPU 时间

PDH cant access total CPU time through PdhAddCounter()

本文关键字:访问 CPU 时间 PdhAddCounter PDH      更新时间:2023-10-16

我目前正在尝试为我的DirectX程序制作某种CPU使用情况概述,但似乎我无法通过PdhAddCounter()获取此信息。我的代码如下所示:

status = PdhOpenQuery(NULL, 0, &m_queryHandle);
    if(status != ERROR_SUCCESS)
    {
        m_canReadCpu = false;
    }
status = PdhAddCounter(m_queryHandle, TEXT("\Processor(_Total)\% processor time"), 0, &m_counterHandle);
    if(status != ERROR_SUCCESS)
    {
        m_canReadCpu = false;
    }

在 PdhAddCounter 调用后,我的状态是 = -1073738824,这会导致程序失败。

我使用的是 Windows 7 64 位系统,我是否必须在 64 位环境中做出一些不同的东西?感谢您的任何帮助。

请记住:PdhAddCounter是区域设置明智的。

响应 -1073738824 表示 PDH_CSTATUS_NO_OBJECT=0xC0000BB8。API 未找到该字符串。您使用的是非英语操作系统吗?

如果您需要执行与Windows XP兼容的应用程序,则需要使用如下解决方法:http://en.verysource.com/code/3604946_1/platforminfo.cpp.html。

适用于 Vista 和 Windows 7、8,...你可以改用PdhAddEnglishCounter。

您可能还想使用 GetSystemTimes kernel32 API,它将使您摆脱对 pdh.dll 的依赖。

在这里看到我的答案。