views:

445

answers:

2

Hi,
I want to prevent Windows XP from powering down as long as my Delphi app is running, and I tried the following:

procedure TForm1.FormCreate(Sender: TObject);
begin
  SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, nil, 0);
  SystemParametersInfo(SPI_SETPOWEROFFACTIVE, 0, nil, 0);
  SystemParametersInfo(SPI_SETLOWPOWERACTIVE, 0, nil, 0);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, nil, 0);
  SystemParametersInfo(SPI_SETPOWEROFFACTIVE, 1, nil, 0);
  SystemParametersInfo(SPI_SETLOWPOWERACTIVE, 1, nil, 0);
end;

but that doesn't seem to work. Other suggestions anyone?
TIA
Steven

+1  A: 

I have no Delphi knwoledge at all, but simply from a user's point of view: I don't want programs to be able to that and I would be seriously mad at microsoft if they allowed it.

Just think about it, with that ability any malicious software would disallow you to shut down as long as it is running, and it won't shut down - ouch. From now on we will never again shut down windows, we'll always have to power off the computer, which is not good for hardware or software.

I am not saying it is not possible, but if I were making an OS, I would actively make it hard or even impossible for you to do so, as it has simply too much potential for abuse. As such, you may well find that even microsoft was of this opinion and did indeed not make possible.

Jasper
I'm not sure this is an entirely reasonable point of view...There are already a number of Microsoft programs that throw up a dialog on shutdown, asking if you want to save. If you click cancel, it cancels the shutdown.
Robert Harvey
As far as I know, that's not the case. Basically, windows first tells the program (let's say word) to shut down. Word then asks the user if he or she wants to save. When you click cancel the shutting down of word will be cancelled, meaning that it effectively ignores the command it got. However, you have a while before the next phase starts, but by the time it does, windows simply starts killing the processes if they are ignoring the command to shut down. As such, Windows will shut down. At least, I haven't ever seen it any different, though I may be wrong of course.
Jasper
I'm pretty sure that running MSSQL process can, in fact, prevent windows from shutting down... Not 100% sure, but pretty sure...
Paulius Maruška
Well, as I said, I have never seen it and would find it logical for it not to be possible, but that by no means means it can't be done; I never claimed that.
Jasper
A task preventing Windows from being shut down doesn't prevent killing the offending task from the task manager.
Loren Pechtel
That's true, but that's not exactly the hardest thing to get around (and you do first have what the offending process(es) are)
Jasper
Not all applications are standard desktop applications. For example WinXP Embedded based industrial operation panels are one place where this kind of feature could be useful.
Harriv
+1 I agree with Jasper here. Of course it should be possible for applications to react to a shutdown, but prevent a shutdown? If I want to shutdown my computer, I want to shut it down without some application preventing it.
Smasher
+13  A: 

Here is the answer to your question:

Detecting and preventing Windows shut down http://delphi.about.com/cs/adptips2000/a/bltip0500_4.htm

Robert Harvey
+1 for that link - the code you've linked to looks remarkably similar to how I've done it for a long time, keeping legacy Windows machines from going to sleep or closing down when I don't want them to.
robsoft