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

I plan to create a C # Windows Forms application as an extension for a third-party Win32 application, but I'm at a dead end on how to do it right now. The farthest I got is to know that this is related to Win32 Hooking and that there is an open source project called EasyHook that should allow me to do this.

I would like to know how I can get text from a text field or some other data from a control in a third-party Win32 application. The text / data in the control should be captured from the launch window of the external application at the moment the user clicks the button.

I think the question can be summarized as follows:

  • How do you define an event for pushing a button when a user clicks on a specific button?
  • How do you get the value displayed by the Win32 control at the click of a button?
+3
source share
1 answer

for example, I created an application called "Example" for you.then added a text box. this is a C # application, but you can also use this method for all Win32 applications. First create a C # application called Example, and then add a text box to it. you need to specify the class name of the text field in order to use this application. Then create the 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 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;
        }

    }
}

I hope for this help.

+1
source

Source: https://habr.com/ru/post/1739279/


All Articles