Capturing data from a window in a closed-source third-party Win32 application

三世轮回 提交于 2019-11-29 23:06:50

问题


I'm planning on creating a C# Windows Forms app as an extension for a third-party Win32 application but I'm stumped as to how to do this right now. The farthest I've gotten is knowing it involves Win32 Hooking and that there's this open source project called EasyHook that's supposed to allow me to do this.

I'd like to know how I can get the text from a textbox or some other data from a control in a third-party Win32 application. The text/data in a control is to be captured from the external application's running window the moment the user presses a button.

I guess the question can be summed up as follows:

  1. How do you determine the event to hook to when the user clicks a certain button?
  2. How do you get the value displayed by a Win32 control at the time the button is clicked?

回答1:


for example I created a application named 'Example' for you.then added a textbox. it's a c# application but you can use this method for all Win32 applications too. First create a C# application named Example then add a textbox to it. you need to textbox's class name to use this application.then create a application and paste these codes.

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

    }
}

Hope this help.



来源:https://stackoverflow.com/questions/2551705/capturing-data-from-a-window-in-a-closed-source-third-party-win32-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!