text von fremden programmen holen



  • hallo,
    ich habe ein fremdes programm das hat 2 text fenster.
    ist es möglich mit meinem programm den text aus diesen 2 edit controls zu holen? wenn ja wie?



  • WM_GETTEXT



  • ok aber wie komme ich an das handle der edit box?



  • spy++ EnumChildWindows etc.



  • ascda schrieb:

    spy++ EnumChildWindows etc.

    blödsinn probiers mal mit GetDlgitem



  • und dazu brauch er spy++ du mongochild ➡



  • wtf ist spy++?
    kann dazu nix finden in google



  • spy++ wird mit dem visual studio mitgeliefert. das ist ein programm, womit du WindowsMessages überwachen kannst! Da kannst du verfolgen, welche Ereigenisse in all deinen momentan laufenden Programmen auftreten und welche weitere Objekte (Window) in einer anwendung instanziiert wurden.

    Ich war grade dabei, so ein proggy zu programmieren. Hier die Klasse dazu:

    (Geschrieben in C# 2008)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace GetHWNDText
    {
        class hwnd
        {
            [DllImport("user32")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
    
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern int GetWindowText(int hwnd, StringBuilder s, int nMaxCount);
    
            [DllImport("user32")]
            public static extern int GetForegroundWindow();
    
            [DllImport("user32.dll", EntryPoint = "SendMessage")]
            private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder sb);
    
            public const int WM_GETTEXTLENGTH = 0x000E;
            public const int WM_GETTEXT = 0x000D;
            //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            private int hwnd_old;
            public bool enabled;
    
            private List<string> collect = new List<string>();
    
            public hwnd()
            {
                enabled = true;
                hwnd_old=GetForegroundWindow();
            }
    
            /// <summary>
            /// Main Function
            /// </summary>
            /// <returns>return a List with all strings of the foreground window</returns>
            public List<string> get_list()
            {
    
                int hwnd = GetForegroundWindow();
                if (hwnd != hwnd_old)
                {
                    collect.Clear();
                    hwnd_old = hwnd;
                    collect.Add("Fixed Window: " + ActiveApplTitle(hwnd_old));
                    analyze_wnd((IntPtr)hwnd,0);
                    return collect;
                }
                return null;
            }
    
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            /// <summary>
            /// Readout the Title of an window
            /// </summary>
            /// <returns></returns>
            private static string ActiveApplTitle(int hwnd)
            {
                StringBuilder sbTitle = new StringBuilder(1024);
                int intLength = GetWindowText(hwnd, sbTitle, sbTitle.Capacity);
    
                if ((intLength <= 0) || (intLength > sbTitle.Length)) 
                    return "";
                return sbTitle.ToString();
            }
    
            private void analyze_wnd(IntPtr hwnd, int layer)
            {
                List<IntPtr> childs = GetChildWindows(hwnd);
                foreach (IntPtr chl in childs)
                {
                    int lenght = 100;
                    StringBuilder sb = new StringBuilder(lenght + 1);
                    SendMessage(chl, WM_GETTEXT, lenght + 1, sb);
                    if (sb.Length > 0 && collect.Contains(sb.ToString())==false)
                        collect.Add(sb.ToString());
                    analyze_wnd(chl,layer++);
                }
            }
    
            //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            /// <summary>
            /// Get the handler of all child windows
            /// </summary>
            /// <param name="parent">parent child window</param>
            /// <returns></returns>
            private static List<IntPtr> GetChildWindows(IntPtr parent)
            {
                List<IntPtr> result = new List<IntPtr>();
                GCHandle listHandle = GCHandle.Alloc(result);
                try
                {
                    EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
                    EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
                }
                finally
                {
                    if (listHandle.IsAllocated)
                        listHandle.Free();
                }
                return result;
            }
    
            /// <summary>
            /// Callback method to be used when enumerating windows.
            /// </summary>
            /// <param name="handle">Handle of the next window</param>
            /// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
            /// <returns>True to continue the enumeration, false to bail</returns>
            private static bool EnumWindow(IntPtr handle, IntPtr pointer)
            {
                GCHandle gch = GCHandle.FromIntPtr(pointer);
                List<IntPtr> list = gch.Target as List<IntPtr>;
                if (list == null)
                {
                    throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
                }
                list.Add(handle);
                //  You can modify this to check to see if you want to cancel the operation, then return a null here
                return true;
            }
    
            /// <summary>
            /// Delegate for the EnumChildWindows method
            /// </summary>
            /// <param name="hWnd">Window handle</param>
            /// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
            /// <returns>True to continue enumerating, false to bail.</returns>
            public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter); 
    
        }
    }
    

    Funktionsweise:
    Nach der initalizierung über den Konstruktor wird von der form aus get_list() aufgerufen. Sie ermittelt den handle des im Vordergrund befinden Fensters, dazu den Titel des Fensters. Dieser Handle wird dann an analyze_wnd() übergeben. analyze_wnd() hohlt sich alle in einer ebene befinden hendles der in ihr befinden objekte und ruft sich beim durchgehen dieser rekrusiv auf um auf die dadrunter leigenden zuzugreifen.

    Falls ein Fehler auftreten sollte, dann die public variable "enabled" auf false setzen. bis jetzt wird sie aber nicht sonderlich gebraucht.

    Mit get_list() wird eine Liste mit dem Text aller texttragender Objekten des fixierten Fensters zurückgegeben. Dann nur noch in der Form mit einem timer aufrufen und über einer foreach schleife auswerten und nach passenden Daten suchen!

    Viel spaß,
    Garry

    P.S. zu ergänzen wäre natürlich, dass die Klasse irgendwie die hwnd der anwendung in der sie ausgeführt wird, rauskriegt und das fenster nicht auswertet. aber wie das geht, hab ich erstmal keine ahnung. könnte jemand ergänzen?


Anmelden zum Antworten