Solved

Execute program, on windows shutdown/restart

Posted on 2003-11-17
15
336 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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