views:

262

answers:

1

I'm developing a C# WPF application that reparents the main window of another application using a call to Win32 SetParent(). The handle to this out-of-process child window is wrapped by a class named FormHost which is derived from HwndHost. All is working well except for one thing: messages for the reparented window are not delivered to FormHost. MSDN documentation clearly states that the HwndHost window procedure WndProc() cannot be used with out-of-process windows. The alternative, MessageHook doesn't work either. I also tried calling AttachThreadInput() to combine the input processing of the two windows. No luck. Any suggestions?

A: 

The process that is reparenting the window cannot directly subclass the message procedure of an out-of-process window. It would have to inject its own window procedure code into the address space of the window's owning process, subclass the window within that address space, and then finally use an IPC mechanism of your choosing to communicate back to the reparenting process as needed.

For the actual injection, you can either:

1) put the actual window procedure code into a DLL, use CreateRemoteThread() to load the DLL into the window's owning process, and then have the DLL's DllEntryPoint() subclass the window (you would have to store the HWND handle in global memory somewhere so the DLL can find it).

2) put the actual window procedure code into a block of memory allocated within the address space of the window's owning process by using VirtualAllocEx() and WriteProcessMemory(), then use CreateRemoteThread() to perform the actual subclass of the window using that memory block as the window procedure.

Remy Lebeau - TeamB