Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Execute program, on windows shutdown/restart

Posted on 2003-11-17
15
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
15 Comments
 
LVL 8

Accepted Solution

by:
gmayo earned 260 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 260 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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