3

I have a method that retrieves the handle of the current fore-window on windows explorer. I want a method/procedure to be fired when this window closes or before window closes. What are the available windows APIs available for this job ? (if any ) Code examples will be appreciated. Thank you

Nilaksha Perera
  • 715
  • 2
  • 12
  • 36
  • Check this [answer](http://stackoverflow.com/a/21914783/122195). You should hook on to shell in order to intercept windows events. – thepirat000 Aug 05 '15 at 14:44
  • Look at `SetWinEventHook()` or `SetWindowsHookEx()`. You can use `GetWindowThreadProcessId()` to get the window's thread and process IDs to pass to them. – Remy Lebeau Aug 05 '15 at 15:44
  • `SetWindowsHookEx()` can set a `WH_CBT` hook to monitor for `HCBT_SYSCOMMAND`/`SC_CLOSE` events, but requires a DLL to hook another process. `SetWinEventHook()` does not require a DLL, but does not have an event specifically for window close, so maybe monitor for `EVENT_OBJECT_DESTROY` or `EVENT_OBJECT_STATECHANGE` events. – Remy Lebeau Aug 05 '15 at 15:52
  • @thepirat000: the [`RegisterShellHookWindow()` documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644989.aspx) says: "*This function is not intended for general use. It may be altered or unavailable in subsequent versions of Windows.*" Besides, it does not have an event for window close, only window destroy. Same as `SetWinEventHook()`. Notall windows are destroyed when they are closed, so I think `SetWindowsHookEx()` is probably the OP's best bet. – Remy Lebeau Aug 05 '15 at 15:55
  • `SetWindowsHookEx()` can alternatively set a `WH_CALLWNDPROC/RET` hook to monitor for `WM_CLOSE` messages. – Remy Lebeau Aug 05 '15 at 16:01

0 Answers0