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.
- In control panel set the acpi power button press action to shutdown.
- Listen for the WndProc message WM_QUERYENDSESSION
When received issue the completely undocumented:
[DllImport("user32.dll", SetLastError = true)] static extern int CancelShutdown();
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.
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 theWM_QUERYENDSESSION
message delay. Since theCancelShutdown()
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.