Windows获取其他进程中Edit控件的内容
最近做的MFC项目中,有个获取其他进程中Edit控件内容的需求,本来以为是个很简单的问题,但是来来回回折腾了不少时间,发博记录一下。
刚开始拿到这个问题,很自然的就想到GetDlgItemText():
UINT GetDlgItemText( HWND hDlg, // handle to dialog box int nIDDlgItem, // control identifier LPTSTR lpString, // pointer to buffer for text int nMaxCount // maximum size of string );The GetDlgItemText function retrieves the title or text associated with a control in a dialog box.
看API的描述和要实现的需求一样,自然就用起来了,可发现不管怎样这个API始终调用失败,获取不到内容。一直以为是代码哪里出问题了,调试好久。后来Google之后在CSDN论坛上找到个老帖,得知这个API只有在Windows 2K之前的系统才能跨进程使用,无奈放弃。
于是查了下,还有一个GetWindowText()
int GetWindowText( HWND hWnd, // handle to window or control LPTSTR lpString, // text buffer int nMaxCount // maximum number of characters to copy );The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowTextcannot retrieve the text of a control in another application.
描述里面很清楚写着不能对其他程序使用,又一次失败。
PS:吐槽一下,为什么GetDlgItemText()里面不写这句话,浪费俺的时间。╮(╯▽╰)╭
解决方法:使用SendMessage()向进程发WM_GETTEXT消息获取。
SendMessage(handle,message,Wparam,lparam);
Handle为窗口句柄,
message为消息类型,
wparam和lparam为消息参数;
WM_GETTEXT
An application sends a WM_GETTEXT message to copy the text that corresponds to a window into a buffer provided by the caller.
其实使用这个消息等于用GetwindowText()。。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix