I would like my program to display a message to the user, when he shuts down Windows, but not, when he logs off or restarts Windows.
My current code for detecting those events looks like this:
type
Tf_PreventShutdown = class(TForm)
private
procedure WMQueryEndSession(var _Msg: TWMQueryEndSession); message WM_QUERYENDSESSION;
end;
[...]
procedure Tf_PreventShutdown.WMQueryEndSession(var _Msg: TWMQueryEndSession);
begin
_Msg.Result := LParam(True);
if (_Msg.Unused and ENDSESSION_CRITICAL) = 0 then begin
if (_Msg.Unused and ENDSESSION_LOGOFF) <> 0 then begin
Caption := 'PreventShutdown - Logoff';
_Msg.Result := LParam(False);
end else begin
Caption := 'PreventShutdown - Shutdown or Reboot';
_Msg.Result := LParam(False);
end;
end;
end;
(Delphi XE2)
This works fine to distinguish between log off and Shutdown/Restart but I found no way to distinguish between Shutdown and Restart. Is it possible at all? And if yes, how?
There is this answer that suggests reading from the registry, but a comment states that it won't work for Windows 7 and also not, if the shutdown was initiated by other means than through the Windows explorer. My tests found that this value doesn't exist in Windows 8.1 (haven't tried Windows 7).
EDIT: What I am trying to achieve:
Some colleagues of mine need to do something just before they go home at certain days (not every day). For this they must not shut down their computer but restart it instead.
The idea is to block the shutdown showing a message, but only under certain conditions, one of them being that the computer is being shut down and not restarted. Of course they are still free to ignore this message and shut down anyway. This is just a reminder.
Since at least one of these colleagues does not have regular working hours and regularly forgets emails and other notifications, the time he shuts down his computer is the best/easiest I could come up with. (And of course, it's a fun way to do it.)