Joe Tam
Joe Tam

Reputation: 622

Setting a HWND's lpfnWndProc from another process

I have a win32 main application that spawns a child process.

At the moment I use CreateWindowEx to create a HWND in the main application followed by spawning a child process. The child process then looks up the HWND using FindWindow. I would like to handle the Window messages (e.g. WM_SIZE, WM_SETFOCUS etc) for this HWND in the child process, therefore I tried setting the GWLP_WNDPROC attribute in the child process but I'm getting an access denied error, which is reasonable.

I thought about creating a HWND directly in the child process instead but when the window is clicked on, the main application loses focus which is not acceptable for my use case.

Does anyone have a suggestion on what to do to keep the focus on the main application, while having the child process handling the messages?

Upvotes: 3

Views: 2063

Answers (2)

marcinj
marcinj

Reputation: 49986

In your child process you can call ShowWindow with WS_NOACTIVATE. When calling CreateProcess function you can specify STARTUPINFO as follows:

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNOACTIVATE;

You can even explicitly give back focus in your child application to main window.

I suppose you want to have similar design to what chromium does. If you look closely at their design docs:

http://www.chromium.org/developers/design-documents/inter-process-communication

you will see that they do implement IPC between processes but they do not have single message processing function in main process.

As last hint - not related to question, read on jobs under windows - this mechanism allows to take a closer control on child processes.

Upvotes: 1

user1773602
user1773602

Reputation:

Use SetWindowsHookEx with one of the windows message hooks (e.g. WH_CALLWNDPROC) to intercept the messages going to other windows, then filter by the HWND of the window in question.

Upvotes: 1

Related Questions