3

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?
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Z. Albia
  • 31
  • 1
  • 2

1 Answers1

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.

opcoder
  • 27
  • 3
  • No. No, no, NO! Use [UI Automation](http://msdn.microsoft.com/en-us/library/ms747327.aspx). It is the **official**, **supported**, and **documented** interface to - uhm - automate a 3rd party application. You set up an event listener for the button, and retrieve the text box contents when it is triggered. – IInspectable Aug 21 '14 at 14:20