How to control power button press shutdown?

3.2k views Asked by At

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.

2

There are 2 answers

0
Mark On BEST ANSWER

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.

2
juFo On

I don't think it is possible unless you speak with your hardware manufacturer or hardwire the start-button so that it doesn't send a signal to the hardware which handles this. You can only delay it but even that would not give you 100% guarantee I guess.

Windows 8.1 will (maybe) bring a Kiosk Mode. Maybe that is what you are looking for ;-)