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

时间:2012-08-15 12:38:02

标签: c++ pdh

我目前正在尝试为我的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位环境中制作不同的东西? 谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

请记住: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。

答案 1 :(得分:0)

您可能还想使用GetSystemTimes kernel32 API,这将使您免于依赖pdh.dll。

请参阅my answer here