c# - 从封闭源代码的第三方 Win32 应用程序中的窗口捕获数据

标签 c# winforms winapi interop hook

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

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

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

  1. 您如何确定事件 当用户点击某个按钮时 Hook ?
  2. 你如何获得值(value) 单击按钮时由 Win32 控件显示?

最佳答案

例如,我为您创建了一个名为“Example”的应用程序。然后添加了一个文本框。 它是一个 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;
        }

    }
}

希望这对您有所帮助。

关于c# - 从封闭源代码的第三方 Win32 应用程序中的窗口捕获数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2551705/

相关文章:

winapi - 为多个监视器处理 WM_GETMINMAXINFO

c# - Windows Phone - 裁剪位图图像

c# - Prism v4,MEF WPF 数据绑定(bind)

c# - 是否有等效于 C# 成员大括号初始化功能的 Objective-C/C++?

c# - 分发 clickonce 应用程序的文件

c# - 如何通过单击按钮增加 datagridview 中的项目数?

c# - 部分类的 ReportDiagnostic

.net - 对 CheckedListbox 进行自然排序

命令行的 C++ Winapi CreateProcess 问题

c++ - 使用 WinApi 可靠地知道卷是否可移动