Solved

Execute program, on windows shutdown/restart

Posted on 2003-11-17
15
330 Views
Last Modified: 2010-04-05
I want to execute program just before windows shutdown or restart. I can trap WM_QUERYENDSESSION, but windows prevents executing programs at this point. I could prevent stutting down, wait a while, then execute the program and post shutdown message again, but I don't know exactly what was the event (shutdown, restart, end task...)
All I want to do is execute something(PostgreSQL stop bat file in this case) before restart or shutdown.
0
Comment
Question by:Excessmind
  • 6
  • 5
  • 2
15 Comments
 
LVL 8

Accepted Solution

by:
gmayo earned 65 total points
ID: 9762503
You can determine whether it's a logoff or a shutdown but it appears you cannot determine what *type* of shutdown (restart or switch off). It's in the lparam of the WM_QUERYENDSESSION message, but I suspect you already know that.

There is an (untested) example: http://www.nl.freepascal.org/lists/fpc-pascal/2001-July/001711.html

One other possibility is not responding to the message until the other app is done. This will cause your program to "hang" and Windows will time out and ask if you want to shut down anyway. Not a problem if the other app is quick but if it takes more than a few seconds then this is probably not the route to go down.

Geoff M.
0
 

Author Comment

by:Excessmind
ID: 9762566
But I can't start another app, when shutdown is initiated, windows gives error message.
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9762595
You can't execute even between WM_QUERYENDSESSION and WM_ENDSESSION? Sorry, I misinterpreted you.

How about having the other app running continuously from when Windows starts, but standing idle? Then when it gets the message to shutdown, it performs the tasks you need, and then allows shutdown?

Geoff M.
0
 

Author Comment

by:Excessmind
ID: 9762766
I wish I could do this.... as far as I know, postgresql starts with one bat file and stops with another. I want to start it and hide its console window when windows starts and end it when shutdowns or restarts. My App monitors database status, start, stop etc. similar to InterBase Guardian.

I've been trying to do this in different ways, but unfortunately nothing works. I'm completely out of idas now. Looks like I have to try different approach...

P.S. I have already tried something similar to that example posted above and it didn't work.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9764681
post your statup nd shutdown batch files. You should be able to write a Delphi service exexcutable to do this for you.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9764690
oh, btw, please explain a little further what you want to happen when there is a shutdown request.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Excessmind
ID: 9769550
OK, I'm posting files:
start.bat
del C:\base\pgdata\postmaster.pid
C:
cd C:\base\bin
set HOME=C:\base\bin
set PATH=C:\base\bin;C:\base\data;C:\base\usr\local\bin;
umount -A
mount C:\base /
mount C:\base\pgdata /data
bash -c "ipc-daemon & postmaster -D /data -i --shutdown"
exit

stop.bat
C:
cd C:\base\bin
set HOME=C:\base\bin
set PATH=C:\base\bin;C:\base\data;C:\base\usr\local\bin;
umount -A
mount C:\base /
mount C:\base\pgdata /data
bash -c "pg_ctl stop -D /data"
bash pg_stop
exit

these aren't the best bat files you've seen :) but I've never tried to make them better and I have to work with this database.... So, the first one starts postgres and stays hidden while database is running. The second one executes another exe and stops postgres. I want to do this before windows shutdowns.
I can't make this as service, because it is intended to run on win98 also.
I apologize for the lengthy post and thank you for your goodwill.
0
 
LVL 26

Assisted Solution

by:EddieShipman
EddieShipman earned 65 total points
ID: 9771030
What is the location (directory containing) of the files mount, unmount and bash?

But, you can make this a tray application. This would mean that it could be setup to run at
startup and it would also close itself upon shutdown. You place the code to stop in a function
and call it either upon shutdown or via a menu or button. Place the start code in a function
and call it in the form's create method or via a menu or button.

Once I get the directories, I'll creat one for you to show that it can work and you can do all of it
using Delphi.
0
 

Author Comment

by:Excessmind
ID: 9771109
All executables are in C:\base\bin.
I agree about tray app. This is what I've started to do.

Thank you in advance.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9773269
OK, use this and any trayicon component you want....

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Menus, StdCtrls, ShellAPI;

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    btnStart: TButton;
    btnStop: TButton;
    mnuStart: TMenuItem;
    mnuStop: TMenuItem;
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
    bPGStarted: Boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure Run(App, CommandLine, Directory: String);
var
  sei: TShellExecuteInfo;
begin
  ZeroMemory(@sei, sizeof(sei));
  try
    with sei do
    begin
      cbSize := SizeOf(sei);
      fMask  := SEE_MASK_NOCLOSEPROCESS;
      Wnd   := Form1.Handle;
      lpVerb := 'open';
      lpFile := PChar(App);
      lpParameters := PChar(CommandLine);
      lpDirectory := PChar(Directory);
      nShow := SW_SHOWHIDE;
    end;
  finally
    ShellExecuteEx(@sei);
  end;
end;

procedure TForm1.btnStartClick(Sender: TObject);
begin
{
Start Batch File:
del C:\base\pgdata\postmaster.pid
C:
cd C:\base\bin
set HOME=C:\base\bin
set PATH=C:\base\bin;C:\base\data;C:\base\usr\local\bin;
umount -A
mount C:\base /
mount C:\base\pgdata /data
bash -c "ipc-daemon & postmaster -D /data -i --shutdown"
}
  DeleteFile('c:\base\pgdata\postmaster.pid');
  SetCurrentDir('c:\base\bin');
  SetEnvironmentVariable(PChar('HOME'), PChar('c:\base\bin'));
  SetEnvironmentVariable(PChar('PATH'), PChar('C:\base\bin;C:\base\data;C:\base\usr\local\bin;'));
  Run('c:\base\bin\unmount.exe', '-A', 'c:\base\bin');
  Run('c:\base\bin\mount.exe', 'c:\base /', 'c:\base\bin');
  Run('c:\base\bin\mount.exe', 'c:\base\pgdata /data', 'c:\base\bin');
  Run('c:\base\bin\bash.exe', '-c "ipc-daemon & postmaster -D /data -i --shutdown"', 'c:\base\bin');
  bPGStarted := True;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
{
Stop Batch File:
cd C:\base\bin
set HOME=C:\base\bin
set PATH=C:\base\bin;C:\base\data;C:\base\usr\local\bin;
umount -A
mount C:\base /
mount C:\base\pgdata /data
bash -c "pg_ctl stop -D /data"
bash pg_stop
}
  SetCurrentDir('c:\base\bin');
  SetEnvironmentVariable(PChar('HOME'), PChar('c:\base\bin'));
  SetEnvironmentVariable(PChar('PATH'), PChar('C:\base\bin;C:\base\data;C:\base\usr\local\bin;'));
  Run('c:\base\bin\unmount.exe', '-A', 'c:\base\bin');
  Run('c:\base\bin\mount.exe', 'c:\base /', 'c:\base\bin');
  Run('c:\base\bin\bash.exe', '-c "pg_ctl stop -D /data"', 'c:\base\bin');
  Run('c:\base\bin\bash.exe', 'pg_stop', 'c:\base\bin');
  bPGStarted := False;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if bPGStarted then
    btnStopClick(nil);
end;

end.
0
 

Author Comment

by:Excessmind
ID: 9795700
I've been busy these days so I din't have time to test this, but now I've tested it and it doesn't work again. You have offered me a way to ged rid of the bat files and I thank you for this(though it is somewhat slower this way), but not a solution to my problem. When windows starts shutting down, or restarting, my application receives QUERYENSESSION, at this time I can't execute any other application. ShellExecute fails and gives error message.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9799598
I don't know what else to suggest, then.
0
 

Author Comment

by:Excessmind
ID: 9823286
Thank you anyway
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now