Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Windows shutdown with custom message

VMWare Workstation does something quite cool when I try to shutdown Windows while a Virtual Machine is running:

VMWare prevents Windows shutdown with a custom message

Normally, we'd see a "This program is preventing Windows from shutting down" message instead of the new "1 Virtual Machine is in use".

How does VMWare do this? I haven't been able to find any APIs about it on Google.

like image 814
foxy Avatar asked Sep 24 '11 08:09

foxy


People also ask

How do I stop Windows from shutting down in 10 minutes?

Enter the following command: shutdown -a . This command will cancel the scheduled shutdown for one time. You'll have to repeat this process if you want to cancel other scheduled shutdowns. Click OK (in the Run window) or ↵ Enter (in Command Prompt or PowerShell).

How do you prevent a user from shutting down the computer?

Prevent specific users from shutting down WindowsComputer Configuration > Windows Settings > Security Settings > Local Policies > User Rights Assignment > Shut Down the System. Double click on it > Select Users > Press Remove > Apply/OK.


1 Answers

You can read all about the changes introduced in Vista here. You really should read that article very carefully.

The APIs you are looking for are ShutdownBlockReasonCreate, ShutdownBlockReasonDestroy and ShutdownBlockReasonQuery. Remember that these APIs are only available on Vista/2008 server up. You'll have to implement fall back behaviour on 2000/XP.

If you need to block shutdown you call ShutdownBlockReasonCreate passing the handle to your main window and the reason as a string. This string is what is displayed in the shutdown blocked dialog, i.e. "1 virtual machine is in use" in your screenshot.

If the operation that blocks shutdown completes then you call ShutdownBlockReasonDestroy.

Note that you must still implement WM_QUERYENDSESSION to make all the pieces fit together. This is the part that actually blocks the shutdown.

On XP you should also respond to WM_ENDSESSION and if your app blocked shutdown it is polite to show a message indicating why. If you don't do so then the user is left scratching his/her head as to why the computer is ignoring the instruction to shutdown.

like image 136
David Heffernan Avatar answered Sep 20 '22 21:09

David Heffernan