I'm wondering if it's possible to programmatically restart an application that has been terminated in the beginning of a shutdown that will be canceled later.
On Windows, if an application calls ShutdownBlockReasonCreate on shutdown , it can know whether the user has cancelled it or not, by checking if the ENDSESSION_CRITICAL bit is set in the lParam.
In my case I don't want to block any shutdown .
Is this possible ? Thanks for any suggestion.
I think I found a solution :
I created a hidden window using
CreateWindowExto handle theWM_QUERYENDSESSIONandWM_ENDSESSIONmessages:The
returnvalues are very important (see this).As I didn't expected, a process without a top level visible window, even if it calls
ShutdownBlockReasonCreate(with a hidden window handle), it will not be shown in the list of applications blocking the shutdown. And if there's no other application blocking the shutdown, Windows will only wait for a few seconds, and then shutdown.For the loop, I took the idea from here. I don't know if there's a better alternative.
That's it.
Please test it, and correct me if I'm wrong. Thanks.
[EDIT] Here's the program main for a ready test: