Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to get notified when another application terminates Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-May-03
Category
Win API
Language
Delphi 2.x
Views
69
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			 

My application is starting processes invoking ShellExecute API calls. These 
processes are manipulating files stored in a database BLOB field. I'd like to be 
notified when the external application terminates in order to save shanges made on 
the file. My idea was to use windows hooks, but I didn't guess exactly how. 
ShellExecute returns the Instance Handle of the application that was run (or DDE 
handle).

Answer:

Solve 1:

How about using the WndProc procedure and listening for the WM_QUERYENDSESSION in 
the main application, and broadcasting another message to the other one. Then your 
applications do what they need to do when they receive the message. For example:

1   {MainApp:}
2   
3   const
4     EndAppMsg = $FFF8B;
5   
6     public
7   
8     procedure WndProc(var Msg: TMessage); override;
9   
10  procdure TMainForm.WndProc(var Msg: TMessage);
11  begin
12    if Msg.Msg = WM_QUERYENDSESSION then
13    begin
14      BroadcastSystemMessage(BSF_POSTMESSAGE, BSM_ALLCOMPONENTS, EndAppMsg, 0, 0)
15    end
16    else
17      inherited;
18  end;


And then use the WndProc again in the other applications that the main application 
calls, and listen for the EndAppMsg message. When it is recived execute the code 
that you need to execute. For example:

19  {Client/Worker App}
20  
21  procedure WndProc(var Msg: Tmessage);
22  const
23    EnaAppMsg = $FFF8B;
24  begin
25    if Msg.Msg = EndAppMsg then
26    begin
27      {Execute your code here or call another procedure to execute the code}
28    end
29    else
30      inherited;
31  end;



Solve 2:

You might try using ShellExecuteEx instead, then using the returned hProcess in a 
WaitForSingleObject call. Here's an example I used in a small demo application:

32  uses
33    Windows, ShellApi;
34  
35  var
36    Info: TShellExecuteInfo;
37  begin
38    FillChar(Info, SizeOf(Info), 0);
39    Info.cbSize := SizeOf(Info);
40    Info.fMask := SEE_MASK_NOCLOSEPROCESS;
41    Info.Wnd := Handle;
42    Info.lpVerb := 'open';
43    Info.lpFile := 'C:\SomeTextFile.Text'; {Change me!}
44    Info.lpParameters := nil;
45    Info.lpDirectory := nil;
46    Info.nShow := SW_SHOW;
47    if (ShellExecuteEx(@Info)) then
48    begin
49      WaitForSingleObject(Info.hProcess, INFINITE);
50      MessageBox(Handle, 'You closed the app I launched!', 'Finished!', MB_OK);
51      CloseHandle(Info.hProcess);
52    end;
53  end;


I'm not sure if you want your entire application becoming unresponsive while the 
launched application is running (the WaitForSingleObject call doesn't return until 
the application is closed), but if you don't, you might consider launching 
individual threads for each ShellExecuteEx, then using WaitForSingleObject in those 
threads (and writing an OnTerminate handler for the thread object to determine when 
the application finally finished).




			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC