tags:

views:

249

answers:

2

I have a c++ console application that launches another application and communicates with it via com. I have the spawned window's hWnd and I want the console app to kill itself if the COM app is no longer open. How could I go about doing this?

+3  A: 

Since you are already communicating between the applications, you should set up a signal, when the window is closed it sends a 'I'm dead' message over to the console App. Your console app can then close appropriately.

If you want to do this by checking the hWnd, you could simply use the 'IsWindow()' function which will let you know if the hWnd is no longer valid. You would have to do this via a polling construct.

Another option, this one more useful if the other App wasn't yours is to install a hook and watch for the window to be destroyed. If you want to do this go have a look at windows hooks, a CBT hook would be appropriate, you can easily watch for windows being destroyed.

DeusAduro
Sorry, this is a race condition waiting to happen. A HWND is a simple number and will be reused. If that happens between polls, you lose.
MSalters
A: 

Call GetWindowThreadProcessId() and then OpenProcess(). You can now test if the process handle is signalled, or Wait() for that to happen.

MSalters