4

Is there a method for setting up a listener/watcher to know when an unrelated application window is closed?

I can check to see if the window is still open, but that seems silly to continually do.

Meep
  • 254
  • 1
  • 15

2 Answers2

5

The Accessibility API's SetWinEventHook filtering for EVENT_OBJECT_DESTROY.

This is notification based so no polling and unlike a CBT hook does not require injection.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
4

You can:

  1. poll for HWND validity (esp. if you don't need timing precision)
  2. subclass the window to intercept its WM_DESTROY and then forward the notification to your app
  3. install a window message hook to be able to intercept the message otherwise
  4. create a hidden child window to get it destroyed with the parent destruction, to also have your message handler called on time for you
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • I should have said it is an external application, I just need to know when it closes, but I cannot edit it. – Meep Mar 08 '14 at 19:03
  • 1
    Like any other handle type, `HWND`s get reused. Checking the validity of a `HWND` does not answer the question whether a window that had a particular `HWND` at the time of retrieval is still alive. – IInspectable Jan 29 '15 at 15:47
  • @IInspectable: It is a good point and in fact polling is not a reliable solution. Still API to check `HWND` value validity is here, it is simple and straightforward, and then sometimes one can accept the chance of mistake and prefer simplicity. – Roman R. Jan 29 '15 at 16:15