ahmd0
ahmd0

Reputation: 17293

c++ set monitor timeout on Windows

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

Upvotes: 1

Views: 912

Answers (2)

Willy K.
Willy K.

Reputation: 432

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;
}

Upvotes: 1

ahmd0
ahmd0

Reputation: 17293

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.

Upvotes: 2

Related Questions