Getting the text contained in an external application

This is a discussion on Getting the text contained in an external application within the C# Programming forums, part of the General Programming Boards category; Hi! i'm iterating through the system processes an when i find a process that belongs to a text oriented application, ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362

    Getting the text contained in an external application

    Hi!

    i'm iterating through the system processes an when i find a process that belongs to a text oriented application, i want to read that text inside the external app. For example, when i get the process to an instance of notepad, i want to get the text contained inside notepad. i've tried to do it using Process.MainWindowHandle pointer, but with no success. How to do it?
    (This is for an "nanny" app i'm writing, which need access to text in external applications so that i can detect any unappropriated words)

    Thanks.
    Last edited by geek@02; 05-08-2012 at 10:45 AM.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    You could use some win32 functions to get the text content of an external control. You gave the example of notepad:

    Code:
    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    
    namespace gettext
    {
        class Program
        {
            static void Main(string[] args)
            {
                foreach (Process proc in Process.GetProcesses())
                    if (proc.ProcessName == "notepad")
                    {
                        IntPtr ptr = proc.MainWindowHandle;
                        ptr = FindWindowEx(ptr, IntPtr.Zero, "Edit", IntPtr.Zero);
                        Console.WriteLine(GetWindowText(ptr));
                    }
    
                Console.Read();
            }
    
            public static string GetWindowText(IntPtr hwnd)
            {
                int len = SendMessageGetTextLength(hwnd, 14, IntPtr.Zero, IntPtr.Zero);
                StringBuilder sb = new StringBuilder(len);
                SendMessageGetText(hwnd, 13, new UIntPtr((uint)len), sb);
                return sb.ToString();
            }
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr FindWindowEx(IntPtr hwndParent,
                IntPtr hwndChildAfter, string lpszClass, IntPtr lpszWindow);
            [DllImport("user32.dll", EntryPoint = "SendMessage")]
            private static extern IntPtr SendMessageGetText(IntPtr hWnd,
                uint msg, UIntPtr wParam, StringBuilder lParam);
            [DllImport("User32.dll", EntryPoint = "SendMessage")]
            private extern static int SendMessageGetTextLength(IntPtr hWnd,
                int msg, IntPtr wParam, IntPtr lParam);
        }
    }
    Notice this line:

    Code:
    ptr = FindWindowEx(ptr, IntPtr.Zero, "Edit", IntPtr.Zero);
    I got the "Edit" class name by using Spy++.

    Unfortunately I don't think there is a managed way of achieving what you require, but I'd be happy for someone to prove me wrong.

  3. #3
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362
    Thanks that helped a lot.

    also, i tried to iterate the child windows of a specific mom window using GetWindow() with GW_CHILD flag (e.g getting the handle to ms word and iterating it's child windows), though i cannot get the code to work. if you guys know a link to that kind of code, please post. Appreciate it.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Note that this approach will not work if the window is custom drawn by the application in which case it might not contain any text and would be made up of linked graphical objects. Most programs will have outward facing interfaces or APIs that can be used to get information from them. If they do not then you are trying to poke around inside another process's memory which is not good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using external modules in your application
    By nonpuz in forum C Programming
    Replies: 0
    Last Post: 07-30-2011, 08:47 AM
  2. Replies: 9
    Last Post: 06-21-2009, 01:13 AM
  3. about launching external application
    By sydetys in forum Windows Programming
    Replies: 16
    Last Post: 01-06-2009, 02:35 AM
  4. External Application
    By Sintax in forum C Programming
    Replies: 2
    Last Post: 02-07-2006, 12:04 AM
  5. opening an external application
    By scotland in forum C++ Programming
    Replies: 3
    Last Post: 07-06-2003, 09:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21