• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

Detecting Windows shutdown

Hi!

On Windows shutdown I'd like to terminate an Delphi application which has opened an MS-DOS Window. How can I detect the Windows shutdown, and how can i unconditionally close the MS-DOS window?

Thanx
0
janez
Asked:
janez
1 Solution
 
XANCommented:
The system sends the WM_QUERYENDSESSION message to each window. So you can hook it and do what do you want ;)
If you know handle to the process you need terminate, you can use the next
BOOL TerminateProcess(
  HANDLE hProcess, // handle to the process
  UINT uExitCode   // exit code for the process
);
0
 
janezAuthor Commented:
Hi XAN!

Since I'm almost a beginner in Delphi, could you please tell me how to use these handles to terminate a process(application). I would be very grateful to you.
By the way...Is the procedure to close an MS-DOS window the same as the one to close an windows application and if not do you maybe know how does it look like?

Thanx
0
 
intheCommented:
Hi
this is example of terminate process.
nte you need to know the window handle of the dos window (or its caption instead as in this example)

procedure TForm1.Button2Click(Sender: TObject);
var
ProcessID,cpid : Cardinal;
h : thandle;
//h := findwindow('some dos box handle',nil);
//or
h := findwindow(nil,'some dos box caption');
begin
ProcessID := GetWindowThreadProcessID(h,@cpid);
TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,cpid),1);
end;


do you need example of wm_query..?
i am unsure if your application is of type console or type forms/windows
do you start the dos program from a normal delphi app?

console apps do not get or use wm_query... they use CTRL_SHUTDOWN_EVENT instead so if your app is a console app that is the function you need example for..
Regards Barry
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
janezAuthor Commented:
Hi inthe!

Well, my delphi application has some forms, but a dos program also starts from it. So i guess it's a kind of mix between the two types. I would be very grateful to you, if you could give me an example of how to detect the windows shutdown (now even more because you said that there are different types of messages).

Thanx
0
 
intheCommented:
Hi Janez,
oh good im glad you have forms :o) so you can use normal windows wm_query.. message for shutdown.
ok here demo ,note the global boolean "ok_to_close " ,you can probably skip using it, but its handy to have this extra functionality .when we get the query message we check state of boolean and if true then we can let windows shutdown ,else we can stop windows from shuting down by setting it too false;
by the sound of your app i would set ok_to_close to true on form create then when you execute the dos box set ok_to_close to false.when the query message is recieved and you have checke k_to_clsoe state if its false then means dos box is running so you do your terminateprocess stuff then let window continue shuttong down..
ive commented out a couple of lines that i normally use and added where roughly your code would go to check and close dos window:

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
   procedure WMQueryEndSession(var Msg: TWMQueryEndSession) ; message WM_QUERYENDSESSION ;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  ok_to_close : boolean;

implementation

{$R *.DFM}

procedure TForm1.WMQueryEndSession(var Msg: TWMQueryEndSession);
const
  CAN_SHUTDOWN = 0 ;
  DONT_SHUTDOWN = 1 ;
begin
  inherited ;
  if ok_to_close then {ok_to_close is the global boolean}
  Msg.Result := CAN_SHUTDOWN
  else
 begin
  // terminate dos window code then;
  Msg.Result := CAN_SHUTDOWN;
 end;
//else
//  Msg.Result := DONT_SHUTDOWN ;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ok_to_close := true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//execute the dos window;
ok_to_close := false;
end;

Regards Barry
0
 
intheCommented:
mmm.. i have idea that may be better for closing dos box
 (note it is untested but i am confident it may work)

add another global var and use this function to
start the dos process keeping the handle of this
 process in var h
then can try closing using sendmessage instead of
 terminate process..


var
tform1 : tform;
h : thandle;

implementation

function CreateDosProcess(filename : string ): string;
  var
   p : TProcessInformation;
   s : TStartupInfo;
 begin
   FillMemory( @s, sizeof(s), 0 );
   s.cb := sizeof(s);
    CreateProcess(Nil,PChar(filename ),
                  Nil,Nil,False,NORMAL_PRIORITY_CLASS,
                  Nil,Nil,s,p);
  h := p.hProcess;
  end;


now on formclose event and in wmquery.. message handler if
 ok_to_close = false do:

SendMessage(h,WM_DESTROY,0,0);
// or WM_CLOSE or even WM_QUIT be better
CloseHandle( p.hProcess ); //close handles to clean up
CloseHandle( p.hThread ); // and again..


id be interested in wether this works or not,so let me know results if you do test it.
cheers Barry ;-)
0
 
craig_capelCommented:
hmm, confident?... understatement, if he ever answers any of mine questions, i dont even bother (anymore) testing his code, hes a true expert.... :)

well his code looks good..... god i am bored today

Craig C. (not to mentiom the 1 1/2 liters of wine i have had to drink)
0
 
janezAuthor Commented:
Hi inthe!

Your code works fine. I used the first procedure you gave me and the WMQueryEndSession procedure...it worked ok. I have also quickly tried your second method for closing a ms-dos window, but it didn't work. Maybe i've done something wrong. Anyway, would you like that i evaluate your answer?

Thanx a lot
0
 
intheCommented:
cheers ;-)

shame about the second method i was sure it might work but it was untested ..maybe i test it more when fre time allows :o)
0
 
intheCommented:
ps
craig stop drinking it bad for your heath .. hic ..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now