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!

Bear of the Year
  • 333
  • 1
  • 2
  • 8
  • 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 '10 at 03:36
  • 3
    http://stackoverflow.com/questions/3355606/detect-laptop-lid-closure-and-opening – Samuel Dec 20 '10 at 03:47

2 Answers2

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

Stefan
  • 71
  • 1
  • 7
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.

Community
  • 1
  • 1
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • Good answer. For a very small concrete example of how to use this stuff in practice, take a look at [LapLock](https://github.com/dechamps/laplock/). – Etienne Dechamps Aug 13 '16 at 09:30