How can I get the text of another process’ window?
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
As you’ve probably found out by now, calling GetWindowText()
won’t work most of the time. The reason for this is that GetWindowText()
won’t do the necessary translation between the address spaces of the two processes. This is required because address that the calling process passes to GetWindowText()
in the lpString
parameter is not valid in the address space of the target process, so some translation is required.
However, there is one way to get around it, and that is sending a WM_GETTEXT
message to the target window. Now you might be wondering how this could work, if, after all, GetWindowText()
sends a WM_GETTEXT
message as part of its implementation.
The answer is that Windows treats some messages differently when they are sent directly across process boundaries, and provides support for address translation (which is not a translation at all. Windows uses memory mapped files to accomplish the copy). WM_GETTEXT
is one of those, as are WM_SETTEXT
and WM_COPYDATA
.
Keep in mind, however, that this will not work for all windows, for the simple reason that they do not store their text using WM_SETTEXT
and use their own buffers for it, but don’t handle the WM_GETTEXT
message appropriately.
Finally, note that GetWindowText()
will work under some circumstances, namely, when the target window passes WM_SETTEXT
messages to DefWindowProc()
. In this case, Windows holds the window text itself in internal structures, which happens to be saved in memory shared by all processes (a memory-mapped file), so GetWindowText()
will retrieve the text directly, without needing to go across process boundaries.
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=417