directedition
directedition

Reputation: 11705

Kill self if hWnd doesn't exist

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?

Upvotes: 0

Views: 818

Answers (2)

MSalters
MSalters

Reputation: 180020

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

Upvotes: 0

DeusAduro
DeusAduro

Reputation: 6076

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.

Upvotes: 3

Related Questions