3

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?

Joe Tam
  • 564
  • 5
  • 16
  • Even if you could change the window procedure from another process, it wouldn't help you because the function pointer in the child application is invalid in the main process, so the main process will simply crash the next time it receives a message. Why not have the child process send the main process's window a message to say "Please change your WM_SIZE and WM_SETFOCUS behavior"? – Raymond Chen Nov 07 '12 at 15:07
  • Because then all the message handling will be done in the main application. The child process is meant to be self contained and always have a HWND associated with it. The main application can spawn multiple child processes. My ideal scenario would be to have the child process to create the HWND but clicking on it does not lose focus on the main application. – Joe Tam Nov 07 '12 at 15:14
  • 1
    I suspect that you're not going to be able to make this work to your satisfaction. What is the problem you are trying to solve by doing this whole scheme, it may be that there is a better way? – jcoder Nov 07 '12 at 15:51
  • Please refer to my comment to marcin_j's answer for what I'm trying to do specifically. – Joe Tam Nov 08 '12 at 10:49

2 Answers2

1

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.

1

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.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Yes my design is similar to Chromium's. Specifically, I have a main application (like Chrome) that can spawn multiple child process. You can imagine each of these child processes as the tabs in Chrome. If the main application gets resized, the content in the tabs need to be resized too. Two solutions that I tried (and failed): 1. Child process creates the HWND- this leads to the main application losing focus when a "tab" is in focus 2. Main application creates the HWND, child process uses this HWND for rendering into. I cannot intercept events (e.g. WM_SIZE) in the child process. – Joe Tam Nov 08 '12 at 10:46
  • You can create window(s) in you main app but do all the processing in child processes. When something needs to be visualized then child process sends raw image to main app to be shown in its window. Actually you can use mapped memory here and let main process read it when it is needed - so no copying would be required. This way it simplifies any double buffering that whould be required anyway to avoid flickering. Otherwise you would have to track child process window each time main window moves, also keep z-order correct, focus, etc. – marcinj Nov 08 '12 at 11:42
  • How do I get the child processes to do the processing? Do I need to forward these events from the main application onto the children, since the main application currently owns the Window Procedure? – Joe Tam Nov 08 '12 at 15:13