简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM