Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / Win32
Tip/Trick

How to disable the Sleep button while your code is running

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
6 Nov 2012CPOL 18.8K   2   4
I needed to disable the Sleep button on my keyboard, here's how.

Introduction

I am writing a small app that will allow my 2 year old daughter to play with my computer and make it so she can not change anything on it. I used a keyboard hook to capture all relevant keystrokes, but the one I was not able to capture was the Sleep key. Every time she hit the Sleep key my computer would go to sleep. Here is some code that will ensure the Sleep button is disabled while my code is running, but will restore the user settings when my code is finished running.

The CDisableSleepButton class

cplusplus
#include <windows.h>
#include <powrprof.h>
#pragma comment(lib, "powrprof")

class CDisableSleepButton
{
private:
    GUID *pCurrentActivePowerScheme;
    GUID *pDuplicatePowerScheme;
    bool SleepButtonDisabled;

public:
    CDisableSleepButton()
    {
        pCurrentActivePowerScheme = NULL;
        pDuplicatePowerScheme = NULL;
        SleepButtonDisabled = false;

        DWORD Result = PowerGetActiveScheme(NULL, &pCurrentActivePowerScheme);
        if (SUCCEEDED(Result))
        {
            Result = PowerDuplicateScheme(NULL, pCurrentActivePowerScheme, &pDuplicatePowerScheme);
            if (SUCCEEDED(Result))
            {
                Result = PowerWriteACValueIndex(NULL,
                                                pDuplicatePowerScheme,
                                                &GUID_SYSTEM_BUTTON_SUBGROUP,
                                                &GUID_SLEEPBUTTON_ACTION,
                                                0); // Do Nothing

                if (SUCCEEDED(Result))
                {
                    Result = PowerSetActiveScheme(NULL, pDuplicatePowerScheme);
                    if (SUCCEEDED(Result))
                    {
                        SleepButtonDisabled = true;
                    }
                }

                if (!SleepButtonDisabled)
                {
                    PowerDeleteScheme(NULL, pDuplicatePowerScheme);
                    LocalFree(pDuplicatePowerScheme);
                    pDuplicatePowerScheme = NULL;
                }
            }
        }
    }

    ~CDisableSleepButton()
    {
        if (SleepButtonDisabled && NULL != pCurrentActivePowerScheme)
        {
            DWORD Result = PowerSetActiveScheme(NULL, pCurrentActivePowerScheme);
            if (SUCCEEDED(Result))
            {
                PowerDeleteScheme(NULL, pDuplicatePowerScheme);
                LocalFree(pDuplicatePowerScheme);
                pDuplicatePowerScheme = NULL;
            }
        }
            
        if (NULL != pCurrentActivePowerScheme)
        {
            LocalFree(pCurrentActivePowerScheme);
            pCurrentActivePowerScheme = NULL;
        }
    }
};

Using the code

Simply declare an instance of the CDisableSleepButton class when you want the Sleep button disabled. Delete the class object, or let it go out of scope to restore the default user setting. This code only works with Windows Vista and above, so be sure to set _WIN32_WINNT to 0x0600 in your stdafx header file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
Canada Canada
Father of two, brother of two, child of two.
Spouse to one, uncle to many, friend to lots.
Farmer, carpenter, mechanic, electrician, but definitely not a plumber.
Likes walks with the wife, board games, card games, travel, and camping in the summer.
High school graduate, college drop-out.
Hobby programmer who knows C++ with MFC and the STL.
Has dabbled with BASIC, Pascal, Fortran, COBOL, C#, SQL, ASM, and HTML.
Realized long ago that programming is fun when there is nobody pressuring you with schedules and timelines.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Igor-846-Apr-24 15:41
Igor-846-Apr-24 15:41 
QuestionDemo application please. Pin
M10DOS8-Nov-12 3:19
M10DOS8-Nov-12 3:19 
AnswerRe: Demo application please. Pin
PJ Arends8-Nov-12 5:37
professionalPJ Arends8-Nov-12 5:37 
Questionhi Pin
refinaa6-Nov-12 22:03
refinaa6-Nov-12 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.