从另一个应用程序窗口中的文本字段读取 [英] Reading from a text field in another application's window

查看:606
本文介绍了从另一个应用程序窗口中的文本字段读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让Windows应用程式存取其他应用程式资料,更准确地说是GUI中的文字输入栏位,并撷取文字以便在我们自己的应用程式中处理?

Is there a way for a Windows application to access another applications data, more specifically a text input field in GUI, and grab the text there for processing in our own application?

如果可能,有没有办法屏蔽你的应用程序以防止它?

If it is possible, is there a way to "shield" your application to prevent it?

编辑:三个第一个答案似乎是关于获得另一个应用程序窗口标题,而不是该窗口中的特定文本输入字段。

The three first answers seem to be about getting the another applications window title, not a specific text input field in that window.

我没有Windows API期望,所以你可以更确切地说如何在该窗口中找到某个文本字段,它的前提条件是什么知道一个窗口句柄需要的东西,它需要知道文本字段句柄吗?我如何得到?等...)

I'm no Windows API expect, so could you be more exact how do I find a certain text field in that window, what are the prequisites for it (seems like knowing a window handle something is required, does it require knowing the text field handle as well? How do I get that? etc...)

C ++中的代码片段真的将真正感谢。 MSDN帮助很难浏览,因为Win32-API有这样可怕的命名约定。

Code snippets in C++ really would be really appreciated. MSDN help is hard to browse since Win32-API has such horrible naming conventions.

完成!查看我的答案下面的一个how-to C ++。

Completed! See my answer below for a how-to in C++.

推荐答案

要从另一个应用程序的文本框中读取文本内容,您需要以某种方式获取该文本框控件的窗口句柄。根据应用程序UI的设计方式(如果它有一个UI),有几种不同的方法可以用来获取此句柄。你可以使用FindWindow/FindWindowEx找到你的控件或使用WindowFromPoint,如果这是有道理的。无论如何,一旦你有了文本控件的句柄,你可以发送一个WM_GETTEXT消息来检索它的内容(假设它是一个标准的文本框控件)。这是一个精心编写的示例(无错误检查):

For reading text content from another application's text box you will need to get that text box control's window handle somehow. Depending on how your application UI is designed (if it has a UI that is) there are a couple of different ways that you can use to get this handle. You might use "FindWindow"/"FindWindowEx" to locate your control or use "WindowFromPoint" if that makes sense. Either way, once you have the handle to the text control you can send a "WM_GETTEXT" message to it to retrieve its contents (assuming it is a standard text box control). Here's a concocted sample (sans error checks):

HWND hwnd = (HWND)0x00310E3A;
char szBuf[2048];
LONG lResult;

lResult = SendMessage( hwnd, WM_GETTEXT, sizeof( szBuf ) / sizeof( szBuf[0] ), (LPARAM)szBuf );
printf( "Copied %d characters.  Contents: %s\n", lResult, szBuf );

我使用Spy ++获取一个文本框窗口的句柄。

I used "Spy++" to get the handle to a text box window that happened to be lying around.

为了保护自己的文本框不被这样检查,你可以总是子类化你的文本框(参见SetWindowLong用GWL_WNDPROC为nIndex 参数),并对WM_GETTEXT消息进行一些特殊处理,以确保只有来自同一进程的请求得到服务。

As for protecting your own text boxes from being inspected like this, you could always sub-class your text box (see "SetWindowLong" with "GWL_WNDPROC" for the "nIndex" parameter) and do some special processing of the "WM_GETTEXT" message to ensure that only requests from the same process are serviced.

这篇关于从另一个应用程序窗口中的文本字段读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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