0

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.)

Community
  • 1
  • 1
dummzeuch
  • 10,975
  • 4
  • 51
  • 158
  • Thomas did mention that other question. And stated, correctly so, that it does not provide the needed answer...IMHO – Sherlock70 Jan 06 '16 at 10:23
  • @Sherlock70 http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled From there, *Basically, questions are duplicates if they have the same answers.* When two questions are identical, the questions are duplicates. That the asker may not like the answers very much is a different problem. If the asker wants better answers then he/she could add a bounty to the original question. – David Heffernan Jan 06 '16 at 10:44
  • 6
    Sometimes I am shutting down a system but hit `Restart` by mistake and then just wait for the system to bounce off the bios and then kill it with the power button. No manner of detection would do anything for you in this case. I can't imagine a situation where you would need to know the difference here except, perhaps, if you're trying to solve a bigger problem in the wrong way... would help to know what you are trying to achieve. – J... Jan 06 '16 at 11:58
  • @J... see my edit to the question. – dummzeuch Jan 06 '16 at 15:34
  • I'd set those machines to wake on LAN and have a server ping them regularly. That way if they end up shut down they will promptly be restarted. – J... Jan 06 '16 at 15:42
  • This is terribly wrong way of getting thing done. – Free Consulting Jan 06 '16 at 17:23
  • @J... doesnt't work. It's not about simply restarting those computers. The colleagues need to do something as well. – dummzeuch Jan 06 '16 at 17:54
  • @FreeConsulting Why is it wrong? It is a way of reminding somebody to do something exactly where (at his computer) and when (at shutdown + additional criteria) he needs to do it. What do you propose instead? – dummzeuch Jan 06 '16 at 17:57
  • 1
    @dummzeuch It is difficult to propose a sensible solution when we don't know what it is that you are trying to remind people to do. Certainly, hooking the system shutdown routine as a means of generating a reminder seems ridiculous. What if people don't shut their systems down? I certainly don't... – J... Jan 06 '16 at 18:06
  • 1
    The reopen votes make no sense to me. One would like them to explain how this question differs. – David Heffernan Jan 06 '16 at 19:37
  • @Thomas On Google+ you said that you didn't like the other answers, the ones that actually, you know, work, because they were not completely trivial to implement. That's seriously uncool. – David Heffernan Jan 06 '16 at 19:38
  • @DavidHeffernan I gues you mean this comment: "Haven't tried it yet. The answer, which was accepted, does not work (as I stated in my question). The other answers using the event log seem complicated. I'm not sure that I am motivated enough to try them. I could just check for date and time and assume that every end session after 17:00 hours on the day in question is a shutdown." So, weighting effort and gain is uncool? You are probably right, but I never claimed to be cool. – dummzeuch Jan 06 '16 at 20:49
  • @J... I would like a way to remind people to do something at their computer that requires them to reboot Windows instead of shutting it down. It has to do with maintenance and cannot be automated (or at least not simple/cheaply enough). The people in question do usually shut down their system and even turn off the power. – dummzeuch Jan 06 '16 at 20:54
  • 3
    What difference does it make if it reboots or if it shuts down, then? It's going to start up again when they next come in - why does the reboot have to happen that moment? What you're talking about doesn't make any sense at all. – J... Jan 06 '16 at 21:32
  • 1
    @J... Take windows update for example. It will install updates only on reboot, not on shutdown. Unless i'm terribly wrong this behaviour was introduced in Win8. – Sherlock70 Jan 07 '16 at 07:41
  • @dummzeuch, disallow non-power users system shutdown privilege. Do disruptive maintenance tasks outside business hours. – Free Consulting Jan 07 '16 at 07:47
  • This is not about changing company policies, which I am not at liberty to discuss. Lets just say, it's a matter of high hardware and service costs vs. comparalby cheap employee time in a small company. So: Some people regularly need to do something at the end of their work hours which involves rebooting their computer rather than shutting it down. It can't be fully automated for the reasons stated above. Since they keep forgetting this, I would like to have a simple way to remind them of this task. They will start this program voluntarily (or not, their choice). – dummzeuch Jan 07 '16 at 08:21
  • @Sherlock70 If this is about windows updates, then the solution is to update (at least) to windows 8.1 and use "install updates automatically" - this will complete during a shutdown or restart cycle. – J... Jan 07 '16 at 09:12
  • This is not about Windows Update, but the issue is similar (and as stated above: It's not possible to fully automate it without high costs). – dummzeuch Jan 07 '16 at 10:00
  • 1
    I admit to being very curious about what this problem actually is, then... either it's not as hard as you think it is or, perhaps, our definition of "high costs" is radically different. – J... Jan 07 '16 at 15:17
  • @J... Indeed. I find the phrase "it's a matter of high hardware and service costs vs. comparalby cheap employee time in a small company" extremely puzzling. Usually I'd expect hardware + software licensing for a whole year to be a small fraction of the employee's _monthly_ salary who uses it. (nevermind depreciation over multiple years). Unfortunately with OP's insistence on being coy about the mystery task, we are but left to speculate. – Disillusioned Aug 31 '16 at 11:56

0 Answers0