Author |
Message |
Daniel Beva #1 / 11
|
 Detecting EndProcess from Task Manager
Is there a way to detect when your program is being terminated using Task Manager. I have an application that uses CreateProcess to start a third party application with wShowWindow = WS_HIDE I would like to show the main window of the spawned application when my application is closed for whatever reason. Daniel
|
Wed, 02 Mar 2005 23:19:49 GMT |
|
 |
Larry Brasfiel #2 / 11
|
 Detecting EndProcess from Task Manager
Quote: > Is there a way to detect when your program is being terminated using Task > Manager.
Yes. A message is sent to its window. I don't happen to know what message, but if I wanted to find out, I would use the spy utility that has come with the last 6 or 7 versions of Visual C++. With spy, you can see what messages are being sent to any particular window of your choosing. -- -Larry Brasfield (address munged, s/sn/h/ to reply)
|
Wed, 02 Mar 2005 23:37:39 GMT |
|
 |
Daniel Beva #3 / 11
|
 Detecting EndProcess from Task Manager
I had another look at the message with spy++ v5.00.7022 and with all mesages logged I can't see any message sent to the application window when after the task manager is activated. (The main window is hidden at the time with a systray icon for user interaction) Perhaps the message is sent to WinMain but not dispatched to the main window. What do you think? Daniel
Quote:
> > Is there a way to detect when your program is being terminated using Task > > Manager. > Yes. A message is sent to its window. I don't happen > to know what message, but if I wanted to find out, I > would use the spy utility that has come with the last > 6 or 7 versions of Visual C++. With spy, you can see > what messages are being sent to any particular window > of your choosing. > -- > -Larry Brasfield > (address munged, s/sn/h/ to reply)
|
Thu, 03 Mar 2005 00:15:42 GMT |
|
 |
William DePalo [MVP #4 / 11
|
 Detecting EndProcess from Task Manager
Quote: > Is there a way to detect when your program is being terminated using Task > Manager.
The most reliable way to detect process termination is to open a handle to it and wait for it to become signalled. To OpenProcess() requires a handle which you can get, subject to privilege checks, if you have an ID. You can get a scan the process list for a process id with Process32First() & Process32Next() - except on NT before Windows 2000. On NT 4 use EnumProcesses() (assuming you have PSAPI installed), on NT versions prior to 4 I think you need to scour the performance data in the registry. Regards, Will
|
Thu, 03 Mar 2005 01:01:48 GMT |
|
 |
Daniel Beva #5 / 11
|
 Detecting EndProcess from Task Manager
Thankyou for the tip, it will certainly help me with another aspect of my current project but doesn't quite get me past my current problem. As I mentioned before my application spawns a 3rd party application who's main window is hidden. I hope to detect when my application is being terminated by Task Manager so that the spawned application can either be shown or terminated before my application is terminated. I fou have any further suggestions that help achieve this goal let me know. Daniel
Quote:
> > Is there a way to detect when your program is being terminated using Task > > Manager. > The most reliable way to detect process termination is to open a handle to > it and wait for it to become signalled. To OpenProcess() requires a handle > which you can get, subject to privilege checks, if you have an ID. You can > get a scan the process list for a process id with Process32First() & > Process32Next() - except on NT before Windows 2000. On NT 4 use > EnumProcesses() (assuming you have PSAPI installed), on NT versions prior to > 4 I think you need to scour the performance data in the registry. > Regards, > Will
|
Thu, 03 Mar 2005 23:24:02 GMT |
|
 |
William DePalo [MVP #6 / 11
|
 Detecting EndProcess from Task Manager
Quote: > I fou have any further suggestions that help achieve this goal let me
know. :-) In an interactive environment like Windows the user is king. If he wants to terminate a process he ought to be able to. The best you can do is catch the event and deal with it. As a previous poster mentioned you ought to look at the message flow first to see if a simple WM_CLOSE or WM_SYSCOMMAND/SC_CLOSE is what causes the exit. Failing that, when a user chooses 'End Now' button the Task Manager will call TerminateProcess() to blow away your application. This is a summary and brutal execution without chance of pardon or appeal. There is almost nothing you can do about it. It would be better to eliminate whatever cause the user has to take such a drastic measure. Regards, Will
|
Fri, 04 Mar 2005 00:24:39 GMT |
|
 |
Daniel Beva #7 / 11
|
 Detecting EndProcess from Task Manager
Well I guess that answers my question then. After I was unable to find a message that caused the program termination when the process was killed with task manager I suspected that it must use TerminateProcess or some equally drastic method. I remember being able to postpone process termination in Visual Basic through the QueryUnload event but back then I was using Windows 95 so there is a good chance that the process termination is handled in a different way. That is something that I can look into when I get a moment. I like my applications to clean up after themselves even in the most extreme of circumstances and the task manager was the only one that I wasn't able to adequately handle as unlikely as it is to occur. Thank you for your valuable advice, Daniel
Quote:
> > I fou have any further suggestions that help achieve this goal let me > know. > :-) > In an interactive environment like Windows the user is king. If he wants to > terminate a process he ought to be able to. The best you can do is catch the > event and deal with it. As a previous poster mentioned you ought to look at > the message flow first to see if a simple WM_CLOSE or > WM_SYSCOMMAND/SC_CLOSE is what causes the exit. > Failing that, when a user chooses 'End Now' button the Task Manager will > call TerminateProcess() to blow away your application. This is a summary and > brutal execution without chance of pardon or appeal. There is almost nothing > you can do about it. It would be better to eliminate whatever cause the user > has to take such a drastic measure. > Regards, > Will
|
Fri, 04 Mar 2005 22:07:24 GMT |
|
 |
#8 / 11
|
 Detecting EndProcess from Task Manager
|
Fri, 19 Jun 1992 00:00:00 GMT |
|
 |
Daniel Beva #9 / 11
|
 Detecting EndProcess from Task Manager
After some more experimenting I have found that when an aplication is terminated using the 'End Task' button in the Task Manager a WM_CLOSE message is sent to the application window. When the 'End Process' button is used to kill the process no messages are sent. As far as I can tell 'End Task' is only available for windows that have the WS_VISIBLE style set. Thankyou to everyone who has helped with this topic, Daniel
Quote: > Is there a way to detect when your program is being terminated using Task > Manager. > I have an application that uses CreateProcess to start a third party > application with wShowWindow = WS_HIDE > I would like to show the main window of the spawned application when my > application is closed for whatever reason. > Daniel
|
Fri, 04 Mar 2005 23:29:50 GMT |
|
 |
#10 / 11
|
 Detecting EndProcess from Task Manager
|
Fri, 19 Jun 1992 00:00:00 GMT |
|
 |
Igor Tandetni #11 / 11
|
 Detecting EndProcess from Task Manager
Quote:
> > Is there a way to detect when your program is being terminated using Task > > Manager. > Yes. A message is sent to its window. I don't happen > to know what message
Good old WM_CLOSE. But it is only posted when you kill from Applications tab. Processes tab just calls TerminateProcess. -- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
|
Sat, 05 Mar 2005 06:50:57 GMT |
|
|