处理来自其他进程的窗口消息 [英] Handling messages for window from other process

查看:170
本文介绍了处理来自其他进程的窗口消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C#WPF应用程序,该应用程序使用对Win32 SetParent()的调用来重新显示另一个应用程序的主窗口.进程外子窗口的句柄由一个名为FormHost的类包装,该类派生自HwndHost.除了一件事之外,其他所有程序都运行良好:父窗口的消息未传递到FormHost. MSDN文档明确指出,HwndHost窗口过程WndProc()不能与进程外窗口一起使用.另一种方法是MessageHook也不起作用.我还尝试调用AttachThreadInput()来结合两个窗口的输入处理.没运气.有什么建议?

解决方案

正在重定窗口的进程无法直接对进程外窗口的消息过程进行子类化.它将不得不将自己的窗口过程代码注入到窗口拥有过程的地址空间中,在该地址空间内对窗口进行子类化,然后最终根据需要使用您选择的IPC机制与返回父过程进行通信.

对于实际注射,您可以:

1)将实际的窗口过程代码放入DLL中,使用CreateRemoteThread()将DLL加载到窗口拥有的进程中,然后使DLL的DllEntryPoint()成为窗口的子类(您必须将HWND句柄存储在全局内存,以便DLL可以找到它.)

2)通过使用VirtualAllocEx()和WriteProcessMemory()将实际的窗口过程代码放入分配在窗口拥有进程的地址空间内的内存块中,然后使用CreateRemoteThread()使用以下命令执行窗口的实际子类该内存块作为窗口过程.

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?

解决方案

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.

这篇关于处理来自其他进程的窗口消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆