Click here to Skip to main content
12,403,978 members (76,237 online)
Click here to Skip to main content
Add your own
alternative version

Stats

7.6K views
1 bookmarked
Posted

How to disable the Sleep button while your code is running

, 6 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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

#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)

Share

About the Author

PJ Arends
President
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionDemo application please. Pin
M10DOS8-Nov-12 3:19
memberM10DOS8-Nov-12 3:19 
AnswerRe: Demo application please. Pin
PJ Arends8-Nov-12 5:37
memberPJ Arends8-Nov-12 5:37 
Questionhi Pin
refinaa6-Nov-12 22:03
memberrefinaa6-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 7 Nov 2012
Article Copyright 2012 by PJ Arends
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid