7

I am looking for a way to intercept the laptop lid closing event. In windows 7, the power management allows me to select a desired behavior when the lid is closed. So there must be a way windows knows when the lid is closed.

I did my research, but only found suggestions to monitor the sleep event. I would like to be more specific to only respond to lid closing.

Does anyone have a suggestion?

Thanks!

2
  • I looked into this for ages but never found a solution. On linux it's easy to read - but on windows (up to xp), the lid switch is tied to power management which is in turn tied to user auth stuff. I came to the point where I needed to write a device driver - and stopped there.
    – sje397
    Dec 20, 2010 at 3:36
  • 3

2 Answers 2

6

The question refers to GUID_LIDSWITCH_STATE_CHANGE not to GUID_LIDCLOSE_ACTION.

GUID_LIDCLOSE_ACTION monitors if the user changes the power behavior when the lid is closing (Control Panel -> Power Settings -> Choose what the close lid does)

If you want to monitor the event of lid close/open, you need to register for GUID_LIDSWITCH_STATE_CHANGE. I used it a Windows service:

int ServiceMain(int argc, char** argv)
{
    serviceStatusHandle = RegisterServiceCtrlHandlerExA(serviceName, (LPHANDLER_FUNCTION_EX) ServiceControlHandler, 0);
    ...
    lidcloseRegHandle = RegisterPowerSettingNotification(serviceStatusHandle, &GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_SERVICE_HANDLE);
    ...
}

And in service control handler:

/**
* Event handler for windows service.
*/
void WINAPI ServiceControlHandler(DWORD controlCode, DWORD evtype, PVOID evdata, PVOID Context)
{
    switch (controlCode)
    {...
         case SERVICE_CONTROL_POWEREVENT:
         WriteToLog("Service Control: SERVICE_CONTROL_POWEREVENT builds and fwd the msg");
         msg.control = SERVICE_CONTROL_POWEREVENT;
         msg.event_type = (int) evtype;
         msg.event_data = evdata;
     ...
    }
}

evtype is PBT_POWERSETTINGCHANGE and in evdata you have the event logged: 0 for closed and 1 for opened.

More details here: https://msdn.microsoft.com/en-us/library/aa372723.aspx https://msdn.microsoft.com/en-us/library/hh769082(v=vs.85).aspx

5

You can register for notification when the lid is shut with RegisterPowerSettingNotification.

AnswerLid Close Action change notification

http://social.msdn.microsoft.com/Forums/en-US/tabletandtouch/thread/0bbf90be-9322-47fb-bfa4-016b57211b3a

In Vista you can register for a callback for when the Lid Close Action changes. This is done by calling RegisterPowerSettingNotification (see http://msdn2.microsoft.com/en-us/library/aa373196.aspx for details). The GUID for this power setting you're interested in is GUID_LIDCLOSE_ACTION. This is defined in wdm.h in the Platform SDK.

Once registered, a WM_POWERBROADCAST will be sent to your application with wParam set to PBT_POWERSETTINGCHANGE. This event is sent anytime the value for the lid close action changes. The lParam contains a pointer to a POWERBROADCAST_SETTING structure (see http://msdn2.microsoft.com/en-us/library/aa372723.aspx) containing information on the setting change.

1
  • Good answer. For a very small concrete example of how to use this stuff in practice, take a look at LapLock. Aug 13, 2016 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.