Sign in to follow this  
Mari_p

Checking if an specific application was closed/killed

Recommended Posts

Hi, Suppose you have two window applications running at same time. Lets call them: A and B. Consider the following: - B knows the handle of A, so B could send messages to A. - B should make an action when A doesn't exist more. Suppose, in this case, A could be closed by the user or killed by an uncommon error. OK, I'm coding something like that. I'm calling GetWindowLong function in the message loop. It works, but I don't know if this would be a good way to accomplish that. The relevant piece of code is:
{
   ...
   ...
   ...

   while( GetMessage( &msg,NULL,0,0) > 0 )
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);

      if ( GetWindowLong( A_hWnd, GWL_HINSTANCE ) == 0 )   // check if A exists
          goto here;

   }

here:

   ReleaseObjects();
	
   return 0;
}

Please, could anybody tell me what is the best way of doing that? Thanks in advance

Share this post


Link to post
Share on other sites
[edit]
i would use message hooking. hook the other window and when it receive a WM_NCDESTROY you pretty much know the window is gone (assuming you have the right window handle to start with).

[crap old response]
hmmm something like that i dunno what i'd do, but i would either try hooking WM_DESTROY or do an IsWindow check (probably a bad idea... as you better have some other test -- like a caption, window class, thread ID check -- as well because Windows recycles window handles). but without any communication between the apps, barring hooking the windows and testing for WM_DESTROY, it's still kinda iffy. speaking of hooks, i would try that as well, probably even first as i'm pretty sure it can be done that way.

Share this post


Link to post
Share on other sites
You are using "handle" ambiguously. You have a window handle and not, say, a process handle.

The most correct way to do this would be to use GetWindowThreadProcessId to get the process id of the process that owns the window, followed by OpenProcess to get a process handle for the that process. You can then use WaitForSingleObject to check if the process is still running. This method is guaranteed to work, assuming you have the appropriate permissions.

Your method will probably work as well but can hit a race condition if the app closes and the a new app reuses the window handle before you notice.

Share this post


Link to post
Share on other sites
Thanks for the replies.

Quote:
Your method will probably work as well but can hit a race condition if the app closes and the a new app reuses the window handle before you notice.

You are right. Sorry, I didn't exactly explain how the applications work. In true, B is a DLL that is called by A.

When A is started, it initialises B and passes its handle to B. When A is terminated, B is closed (by that method). Sorry, I should have mentioned that the action to be executed was closing B when A is terminated.

My main concern about my method would be if it would contribute to a significant reduction in performance, since I'm calling it in a 'uninterrupted' loop.

Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mari_p
My main concern about my method would be if it would contribute to a significant reduction in performance, since I'm calling it in a 'uninterrupted' loop.
GetMessage() will block and free processor time back to the OS, so there shouldn't be a large performance hit at all. Likewise, WaitForSingleObject() performs a blocking wait, and will free processor time to the OS.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this