JPadley
JPadley

Reputation: 343

Capture events from other windows applications

I have a third party application which I did not create myself. I need to create an application which is able to listen for button clicks and read data from tables in that application. I believe that the third party application is made in C# but I do not know for sure. Is there a way to know when UI buttons are pressed and to harvest data from the application? I do not mind which programming language the solution must be written in, as long as it fulfils the above tasks.

Upvotes: 2

Views: 663

Answers (1)

Little Fox
Little Fox

Reputation: 1252

You can use few dlls like user32.dll, to get data from other apps. Find parent handle of a window:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

public static IntPtr FindWindow(string windowName)
{
    var hWnd = FindWindow(windowName, null); 
    return hWnd;
}

After that, find handle of a child window

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string windowTitle);

private IntPtr FindSomeElement(IntPtr parent)
{
    IntPtr childHandle;

childHandle = FindWindowEx(
    parent,    
    IntPtr.Zero,    
    "WindowsForms10.EDIT.app21", 
    IntPtr.Zero);   
return childHandle;}

and get text from it:

private static string GetText(IntPtr childHandle)
{
    const uint WM_GETTEXTLENGTH = 0x000E;
    const uint WM_GETTEXT = 0x000D;

    var length = (int)SendMessage(handle, WM_GETTEXTLENGTH, IntPtr.Zero, null);
    var sb = new StringBuilder(length + 1);
    SendMessage(handle, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
    return sb.ToString();
}

//I didnt test this code, just gave an idea. Visit www.pinvoke.net/default.aspx/ for more information. You can find alot about user32.dll

Upvotes: 1

Related Questions