Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What line of code could I use in C++ to disable energy saver?

Tags:

c++

winapi

I want to prevent the monitor from going to sleep (the windows setting, not the monitor setting). I am using c++. What call do I make?

like image 1000
Nathan Lawrence Avatar asked Jun 16 '09 19:06

Nathan Lawrence


1 Answers

class KeepDisplayOn
{
public:
    KeepDisplayOn()
    {
        mPrevExecState = ::SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
        ::SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &mPrevScreenSaver, 0);
        ::SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, FALSE, NULL, 0);
    }

    ~KeepDisplayOn()
    {
        ::SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, mPrevScreenSaver, NULL, 0);
        ::SetThreadExecutionState(mPrevExecState);
    }

private:
    UINT                mPrevScreenSaver;
    EXECUTION_STATE     mPrevExecState;
};
like image 176
sean e Avatar answered Oct 03 '22 14:10

sean e