【问题标题】:Capturing data from a window in a closed-source third-party Win32 application从闭源第三方 Win32 应用程序中的窗口捕获数据
【发布时间】:2012-02-14 22:18:15
【问题描述】:

我正计划创建一个 C# Windows 窗体应用程序作为第三方 Win32 应用程序的扩展,但我现在不知道如何执行此操作。我所知道的最远的是知道它涉及 Win32 Hooking,并且有一个名为 EasyHook 的开源项目应该允许我这样做。

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

我想这个问题可以总结如下:

  1. 如何确定事件 当用户点击某个按钮时挂钩?
  2. 如何获得价值 单击按钮时由 Win32 控件显示?

【问题讨论】:

    标签: c# winforms winapi interop hook


    【解决方案1】:

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

    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;
            }
    
        }
    }
    

    希望对您有所帮助。

    【讨论】:

    • 没有。不不不!使用UI Automation。它是 officialsupporteddocumented 接口,用于 - 嗯 - 自动化 3rd 方应用程序。您为按钮设置了一个事件监听器,并在它被触发时检索文本框内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多