0

As far as I know there're only sleep/hibernate and shut-down options available in the Windows Control Panel in reaction to the power button and laptop lid closing:

enter image description here

I write a Windows service and I'm curious if I can customize those options? Primarily I need to be able to log out Windows user before sending the system to sleep.

Gorpik
  • 10,940
  • 4
  • 36
  • 56
ahmd0
  • 16,633
  • 33
  • 137
  • 233

2 Answers2

2

OK, have to answer my own question. Here's what I was able to find out:

It is easy to get what power action will be performed when lid is closed. Here's the code (it must be run from a user-mode process though):

void getLidClosedAction()
{
    GUID* pGuidActivePwrSchm = NULL;
    DWORD dwR = PowerGetActiveScheme(NULL, &pGuidActivePwrSchm);
    if(dwR == ERROR_SUCCESS)
    {
        DWORD val;

        val = -1;
        dwR = PowerReadACValueIndex(NULL, pGuidActivePwrSchm, &GUID_SYSTEM_BUTTON_SUBGROUP, &GUID_LIDCLOSE_ACTION, &val);
        if(dwR == ERROR_SUCCESS)
        {
            _tprintf(L"Lid closed action: ");

            switch(val)
            {
            case 0:
                _tprintf(L"Do nothing\n");
                break;
            case 1:
                _tprintf(L"Sleep\n");
                break;
            case 2:
                _tprintf(L"Hibernate\n");
                break;
            case 3:
                _tprintf(L"Shut-down\n");
                break;
            default:
                _tprintf(L"Unknown value=%d\n", val);
                break;
            }
        }
        else
        {
            _tprintf(L"PowerReadACValueIndex error=%d\n", dwR);
        }


        if(pGuidActivePwrSchm)
        {
            LocalFree(pGuidActivePwrSchm);
            pGuidActivePwrSchm = NULL;
        }
    }
    else
    {
        _tprintf(L"PowerGetActiveScheme error=%d\n", dwR);
    }

}

And it is also easy to set the power action when the lid is closed (again the code must run in the user-mode process -- otherwise you'll need to get the power scheme GUID using means other than PowerGetActiveScheme call):

BOOL setLidClosedAction(DWORD dwVal)
{
    //'dwVal' = can be one of:
    //          0 = do nothing
    //          1 = sleep
    //          2 = hibernate
    //          3 = shut-down
    BOOL bRes = FALSE;

    GUID* pGuidActivePwrSchm = NULL;
    DWORD dwR = PowerGetActiveScheme(NULL, &pGuidActivePwrSchm);
    if(dwR == ERROR_SUCCESS)
    {
        dwR = PowerWriteACValueIndex(NULL, pGuidActivePwrSchm, &GUID_SYSTEM_BUTTON_SUBGROUP, &GUID_LIDCLOSE_ACTION, dwVal);
        if(dwR == ERROR_SUCCESS)
        {
            bRes = TRUE;
        }
        else
        {
            _tprintf(L"PowerWriteACValueIndex error=%d\n", dwR);
        }

        if(pGuidActivePwrSchm)
        {
            LocalFree(pGuidActivePwrSchm);
            pGuidActivePwrSchm = NULL;
        }
    }
    else
    {
        _tprintf(L"PowerGetActiveScheme error=%d\n", dwR);
    }

    return bRes;
}

This applies to the power/sleep button action as well. The GUIDs in GUID_SYSTEM_BUTTON_SUBGROUP are as such:

GUID_POWERBUTTON_ACTION = power button
GUID_SLEEPBUTTON_ACTION = sleep button
GUID_USERINTERFACEBUTTON_ACTION = sometimes another OEM sleep button

Unfortunately I was not able to see how to set up a custom power action for those events.

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

Just, I'd like to give some clues for system idle time and Log off.

You can get system idle time by using BOOL WINAPI GetLastInputInfo( _Out_ PLASTINPUTINFO plii ); This is a simple method to get the system idle time.

LASTINPUTINFO input_info;
input_info.cbSize = sizeof(LASTINPUTINFO);

::GetLastInputInfo(&input_info);

 //input_info.dwTime` means the value of tick count 
 //when the last input event was received. 

 //So, you can get idle time by using below code.
 DWORD currentTime = GetTickCount();
 DWORD timeElapsed = currentTime - input_info.dwTime;

Next, as far as I know, ExitWindowsEx() function can be used for Log off, Shut down system, and WM_QUERYENDSESSION is sent when an application calls one of the system shutdown functions.

ExitWindowsEx function

I hope this will help you a little.

hyun
  • 2,135
  • 2
  • 18
  • 20