Handling messages for window from other process

只谈情不闲聊 提交于 2019-12-10 11:35:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/1364152/handling-messages-for-window-from-other-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!