k0tt
k0tt

Reputation: 78

Intercept the closing message of a window opened with ShellExecuteEx

I have an annoying program that don't save his position when closed. I've made a small console program that open it and move the window to another position, now i want to save the position when the program close, how i can intercept the closing message so i can save the position before the window destroy itself ?

Upvotes: 0

Views: 478

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595392

Since you can position the window after launching its app, you obviously have the window's HWND. However, you cannot subclass a HWND across process boundaries, so your app cannot hook the window directly. You would have to either:

  1. remotely inject some code into the launched process to subclass the target window from within the context of its own process, then your subclass has direct access to all of messages that the window receives.

  2. implement a global message hook in a DLL using SetWindowsHookEx() and look at all of the messages that the target HWND receives.

Either way, when your subclass/hook detects a WM_CLLOSE and/or WM_DESTROY message being delivered to the target window, it can communicate that info back to your app using any Inter-Process Communication (IPC) mechanism of your choosing - named pipe, named event, socket, mailslot, window message, etc.

Upvotes: 1

Related Questions