1

I was wondering if there's a way to enable or disable monitor/screen timeout from a C++ program on a global scale? (I have one catch though, it must be backward compatible with Windows XP SP3.)

I'm talking about this global setting:

Windows 8 screenshot

of this one for XP:

XP screenshot

ahmd0
  • 16,633
  • 33
  • 137
  • 233
  • If you want to programatically change that setting... you really shouldn't do that. It's very user-unfriendly. If you just want to stop the display from sleeping while your program runs here's a sort of similar question: http://stackoverflow.com/questions/463813/programmatically-prevent-windows-screensaver-from-starting. – bames53 Aug 24 '12 at 22:04
  • Yeah I heard about it, unfortunately most of those APIs like SetThreadExecutionState are very finicky. In my experience, specifying ES_DISPLAY_REQUIRED has absolutely no effect. – ahmd0 Aug 24 '12 at 22:07
  • I think I got it: CallNtPowerInformation(SystemPowerPolicyAc), and use SYSTEM_POWER_POLICY::VideoTimeout – ahmd0 Aug 25 '12 at 06:26
  • Then you should post that as an answer and accept it, otherwise the question appears unanswered and won't benefit those in the future with the same question. – Carey Gregory Aug 26 '12 at 19:07

2 Answers2

2

For those who's interested, here's how to fix this:

Call CallNtPowerInformation(SystemPowerPolicyAc) API to get or set the display timeout value, and use SYSTEM_POWER_POLICY::VideoTimeout member.

ahmd0
  • 16,633
  • 33
  • 137
  • 233
1

A complete sample:

bool applyVideoTimeout(DWORD newtimeOut)
{
    SYSTEM_POWER_POLICY powerPolicy;
    DWORD ret;
    DWORD size = sizeof(SYSTEM_POWER_POLICY);

    ret = CallNtPowerInformation(SystemPowerPolicyAc, nullptr, 0, &powerPolicy, size);

    if ((ret != ERROR_SUCCESS) || (size != sizeof(SYSTEM_POWER_POLICY)))
    {
        return false;
    }

    powerPolicy.VideoTimeout = newtimeOut
    ret = CallNtPowerInformation(SystemPowerPolicyAc, &powerPolicy, size, nullptr, 0);

    if ((ret != ERROR_SUCCESS))
    {
        return false;
    }

    return true;
}
Willy K.
  • 397
  • 2
  • 14