CallNtPowerInformation(SystemPowerInfomation...) 在 WIndows XP 上工作吗?

发布于 2024-08-15 20:25:08 字数 1036 浏览 8 评论 0原文

我正在尝试访问电源管理空闲计数器的“TimeRemaining”值。谷歌搜索表明很多人(包括我)都可以获得一次值,但随后的每次调用都会给出相同的结果。没有倒计时,CurIdle 值没有变化...

这是相关代码的简短版本:

#include <windows.h>
#include <tchar.h>
#include <powrprof.h>
typedef struct _SYSTEM_POWER_INFORMATION {
  ULONG MaxIdlenessAllowed;
  ULONG Idleness;
  ULONG TimeRemaining;
  UCHAR CoolingMode;
}SYSTEM_POWER_INFORMATION, *PSYSTEM_POWER_INFORMATION;

int _tmain(int argc, _TCHAR* argv[])
{
    SYSTEM_POWER_INFORMATION spin;
    CallNtPowerInformation(SystemPowerInformation,NULL,0,&spin,sizeof(SYSTEM_POWER_INFORMATION));
    _tprintf(_T("MaxIdleness\t%d\t"),spin.MaxIdlenessAllowed);
    _tprintf(_T("CurIdle\t%d\t"),spin.Idleness);
    _tprintf(_T("TimeRemaining\t%d\n"),spin.TimeRemaining);
    return 0;
}

当我从命令行运行此代码时,我总是得到相同的 MaxIdleness 值(这是预期的)、CurIdle(应该更改)和 TimeRemaining (始终处于最大值...可以理解,因为我是从命令行执行的,但是如果我将其放入批处理文件中,其中 sleep.exe 位于其间或程序中,我会得到相同的结果在每次调用 CallNtPowerInformation 之间进行“睡眠”迭代)。

谁能给我发送一份显示不同 TimeRemaining 和 CurIdle 值的示例代码副本?

I'm trying to access the "TimeRemaining" value for the power management idle counter. Google search indicates that lots of folks (including me) can get a value once, but every subsequent call gives the same results. No countdown, no change in CurIdle value...

Here's a short version of the code in question:

#include <windows.h>
#include <tchar.h>
#include <powrprof.h>
typedef struct _SYSTEM_POWER_INFORMATION {
  ULONG MaxIdlenessAllowed;
  ULONG Idleness;
  ULONG TimeRemaining;
  UCHAR CoolingMode;
}SYSTEM_POWER_INFORMATION, *PSYSTEM_POWER_INFORMATION;

int _tmain(int argc, _TCHAR* argv[])
{
    SYSTEM_POWER_INFORMATION spin;
    CallNtPowerInformation(SystemPowerInformation,NULL,0,&spin,sizeof(SYSTEM_POWER_INFORMATION));
    _tprintf(_T("MaxIdleness\t%d\t"),spin.MaxIdlenessAllowed);
    _tprintf(_T("CurIdle\t%d\t"),spin.Idleness);
    _tprintf(_T("TimeRemaining\t%d\n"),spin.TimeRemaining);
    return 0;
}

When I run this from the command line, I always get the same values for MaxIdleness (which is to be expected), CurIdle (which should change) and TimeRemaining (which is always at the maximum value...understandable since I'm executing from the command line, but I get the same result if I put it in a batch file with sleep.exe in between or in a program that iterates with a "sleep" between each call to CallNtPowerInformation).

Can anyone send me a copy of example code that does show varying TimeRemaining and CurIdle values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2024-08-22 20:25:08

正如有时发生的那样,我解决了我的问题。以下是我为那些遇到相同行为的人学到的知识:

1)我的具体问题是,正如人们可能已经猜到的那样,启动时加载的程序之一以某种方式频繁地重置计数器......因此计数器始终等于最大值。我手动禁用(或卸载)程序,直到电脑再次挂起。

2)我在上面的问题中建议的构造(批处理文件中包含 sleep.exe 的简单控制台程序)仍然不起作用。我的猜测是 sleep.exe 或任何执行的批处理文件都会使计数器保持在最大值。

3) 我注意到的另一件事(一旦我的测试 PC 正确挂起)是计数器和其他值(MaxIdle 等)仅每 15 秒更新一次。我不知道这是否适用于所有 WinXP 安装,但这有助于解释为什么像我这样正在寻找逐秒倒计时的人无法获得它。 (这也解释了为什么我在其他正确挂起的程序上测试 CallNtPowerInformation 计时器似乎不起作用......在我每秒重复得到相同的值之后,我一定在 15 秒过去之前放弃了。)

4)所以...总结一下:
a)要非常小心...很多事情都会重置空闲计时器
b) 如果 PC 确实空闲,则不要期望它(或 CurIdle 或 MaxIdleness)更新频率超过每 15 秒(如果不是,计数器将立即重置为最大值)。

我希望这可以帮助其他尝试使用此功能的人。

德斯科特姆

As sometimes happens, I solved my problem. Here's what I learned for those who come across the same behavior:

1) My specific problem was, as one might have guessed, that one of the programs loaded on startup was somehow resetting the counter frequently...so the counter was always equal to the maximum value. I manually disabled (or uninstalled) programs until the PC would suspend again.

2) The construct I suggested in my question above (simple console program with sleep.exe in a batch file) still doesn't work. My guess is that either sleep.exe or any batch file executing keeps the counter at the maximum value.

3) The other thing I noted (once my test PC was properly suspending) was that the counter and other values (MaxIdle, etc.) are only updated every 15 seconds. I don't know if this is true for all WinXP installations, but it helps to explain why folks like me that were looking for a second-by-second countdown weren't able to get it. (It also explains why my testing my CallNtPowerInformation timer on other programs that did suspend properly didn't seem to work...after I got the same value repeated every second, I must have given up before 15 seconds had elapsed.)

4) So...to summarize:
a) be very careful...lots of things will reset the Idle timer
b) don't expect it (or CurIdle or MaxIdleness) to be updated more than every 15 seconds if the PC is really idle (if it's not, the counter is reset immediately to the maximum value).

I hope this helps others trying to use this function.

dscottm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文