I need to mute/un-mute the sound card at startup and shutdown.
I have found some code to do the work, but often Windows slams through the shutdown and the sound never gets muted.
Can someone please tell me how to pause the shutdown long enough for my app to mute the sound? I can use a simple TTimer to pause the app long enough for it to run the muting and then let Windows get on with shutting down.
How do I tell Windows to wait though?
I notice that if I leave Firefox running and try to shutdown, Windows stops with a message, "These programs are preventing windows closing..." and needs a click to force Firefox to close. I need to find that.
Since Windows Vista, if you register a shutdown reason string with the OS or if your application has a top level window, the OS will wait indefinitely for your program to return from
WM_QUERYENDSESSION
while displaying the blocking applications screen - or until the user chooses to forcefully end the program of course.The below sample code simulates a 45 seconds wait with
Sleep
. In the first five seconds of the wait the OS waits patiently, only then it displays the full screen UI. The only way to show the screen immediately is to immediately return false fromWM_QUERYENDSESSION
. But in this case you won't be able to resume shutdown.For details on shutdown behavior of the OS for Vista and later, see documentation.
You need to handle the
WM_QUERYENDSESSION
messsage. It's sent to each application before Windows starts the shutdown process. Do what you need quickly, because failure to respond rapidly enough causes the behavior you're observing in FireFox, which is usually a sign of a badly designed app (and the user may terminate it before you get a chance to finish).(Just as an aside: The enabling/disabling of sounds is a per-user setting, and you should have a very good need for interfering with the user's choice. If I were you, I'd make sure my uninstaller was well-tested, because any app that interfered with my sound preferences this way would be removed from my system very quickly.)