从闭源第三方Win32应用程序中的窗口捕获数据

我打算创建一个C#Windows Forms应用程序作为第三方Win32应用程序的扩展,但我现在对如何执行此操作感到困惑。 我得到的最远的是知道它涉及Win32 Hooking,并且有一个名为EasyHook的开源项目应该允许我这样做。

我想知道如何从文本框中获取文本或从第三方Win32应用程序中的控件获取其他数据。 当用户按下按钮时,将从外部应用程序的运行窗口捕获控件中的文本/数据。

我想问题可归纳如下:

  1. 当用户点击某个按钮时,如何确定要挂钩的事件?
  2. 如何在单击按钮时获取Win32控件显示的值?

例如,我为你创建了一个名为’Example’的应用程序。然后添加了一个文本框。 它是ac#应用程序,但您也可以将此方法用于所有Win32应用程序。 首先创建一个名为Example的C#应用​​程序,然后向其中添加一个文本框。 你需要使用textbox的类名来使用这个应用程序。然后创建一个应用程序并粘贴这些代码。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam); [DllImport("user32.dll")] public static extern IntPtr FindWindowEx(IntPtr Parent,IntPtr child, string classname, string WindowTitle); const int WM_GETTEXT = 0x00D; const int WM_GETTEXTLENGTH = 0x00E; private void Form1_Load(object sender, EventArgs e) { Process procc = GetProcByName("Example"); // You can use spy++ to get main window handle so you don't need to use this code if (procc != null) { IntPtr child = FindWindowEx(procc.MainWindowHandle, IntPtr.Zero, "WindowsForms10.EDIT.app.0.2bf8098_r16_ad1", null); // Use Spy++ to get textbox's class name. for me it was "WindowsForms10.EDIT.app.0.2bf8098_r16_ad1" int length = SendMessage(child, WM_GETTEXTLENGTH, 0, 0); StringBuilder text = new StringBuilder(); text = new StringBuilder(length + 1); int retr2 = SendMessage(child, WM_GETTEXT, length + 1, text); MessageBox.Show(text.ToString()); // now you will see value of the your textbox on another application } } public Process GetProcByName(string Name) { Process proc = null; Process[] processes = Process.GetProcesses(); for (int i = 0; i < processes.Length; i++) { if (processes[i].ProcessName == Name)proc = processes[i]; } return proc; } } } 

希望这有帮助。