Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control power button press shutdown?

I'm working on a kiosk style application where I need to control the shutdown/restart of the PC when the power button is pressed. Thanks to this post, I'm about 90% of the way there.

  1. In control panel set the acpi power button press action to shutdown.
  2. Listen for the WndProc message WM_QUERYENDSESSION
  3. When received issue the completely undocumented:

    [DllImport("user32.dll", SetLastError = true)]
    static extern int CancelShutdown();
    
  4. Return from the WndProc and bring up my own message box asking the user to Shutdown / Restart or Cancel, and respond to their action.

Everything works well if I do a start / shutdown from the task bar (I can issue theses as fast as I want). Everything also works well the first time I press the power button. On subsequent power button presses though I see a minute or so delay before I receive the WM_QUERYENDSESSION message.

Is there a setting or registry entry about how often windows will issue an ACPI event? I know it's not the hardware because under linux the same machine will fire the ACPI event as fast as I can press the button.

Thanks.

like image 386
Mark Avatar asked Apr 23 '13 21:04

Mark


1 Answers

Calling in some favors at work, I was able to take this question directly to Microsoft support. On my third support engineer, I was essentially told this is not possible at an application level. It was his belief that calling the undocumented CancelShutdown() "confuses" the power manager or acpi driver which leads to the WM_QUERYENDSESSION message delay. Since the CancelShutdown() is undocumented, MS is not willing investigate further.

So, how do you hook power button presses? You need to write a device driver, specifically an ACPI Filter Driver. We are investigating this now.

like image 123
Mark Avatar answered Oct 31 '22 04:10

Mark