Results 1 to 4 of 4

Thread: Unable to get text from window

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    43

    Unable to get text from window

    I've been trying to get text from a certain window. I'm fairly certain I'm really close. So maybe someone can give me a little push in the right direction.

    Code:
    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 MW2KICKER
    {
    
        public partial class MW2KICKER : Form
        {
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
            [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
            public static extern IntPtr FindWindow(string className, string windowName);
            [DllImport("user32.dll", SetLastError = true)]
            public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, StringBuilder lParam);
            [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
            static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            static extern int GetWindowTextLength(IntPtr hWnd);
            
    
    
    
            private const int WM_SETTEXT = 0x000C;
            private const int VK_RETURN = 0x0D;
            private const int WM_KEYDOWN = 0x100;
            private const int WM_KEYUP = 0x101;
            private const int WM_GETTEXTLENGTH = 0x000E;
            private const int GW_CHILD = 5;
            private const int GW_HWNDNEXT = 2;
    
            public MW2KICKER()
            {
                InitializeComponent();
            }
    
            private void bStatus_Click(object sender, EventArgs e)
            {
                //Find the console edit box aswell as send the status command - works fine.
                string message = "status";
                StringBuilder sb = new StringBuilder(message);
                IntPtr MW4CONSOLE = FindWindow("IW4 WinConsole", null);
                IntPtr MW4EDIT = FindWindowEx(MW4CONSOLE, IntPtr.Zero, "Edit", IntPtr.Zero);
                SendMessage(MW4EDIT, WM_SETTEXT, IntPtr.Zero, sb);
                PostMessage(MW4EDIT, WM_KEYDOWN, VK_RETURN, IntPtr.Zero);
                PostMessage(MW4EDIT, WM_KEYUP, VK_RETURN, IntPtr.Zero);
    
                //Get the console text - works and finds the correct window - cross checked with api spy
                IntPtr MW4TEXT = FindWindowEx(MW4CONSOLE, MW4EDIT, "Edit", IntPtr.Zero);
    
                //does not work
                int length = GetWindowTextLength(MW4TEXT);
                StringBuilder sbtext = new StringBuilder(length + 1);
    
                GetWindowText(MW4TEXT, sbtext, sbtext.Capacity);
                lbUserNames.Items.Add(sbtext.ToString());
    
            }
           
    
        }
    }
    Here is a picture of the program running. Note, that I can send text fine to said window.

    Name:  1z72dxi.jpg
Views: 1111
Size:  390.3 KB

    I have the handle of the window I want to extract text from. But, like I said it isn't working. Any ideas?
    Last edited by fattysc; Jul 20th, 2010 at 08:30 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Unable to get text from window

    As someone who views a lot of threads, i can tell you that many people don't want to have to click links and download things unnecessarily. You could just as easily posted just the relevant code right here and you could also have attached the image. Then we wouldn't have to waste time going beyond this page. The easier you make it for us, the more likely you'll get the help you want.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    43

    Re: Unable to get text from window

    Edited, for people who don't like to click links.

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    43

    Re: Unable to get text from window

    Well I figured it out by myself. Not that anybody seemed interested. Here is the fixed code.

    Code:
    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;
    using ManagedWinapi.Windows.Contents;
    namespace MW2KICKER
    {
    
        public partial class MW2KICKER : Form
        {
            [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
            public static extern IntPtr FindWindow(string className, string windowName);
            [DllImport("user32.dll", SetLastError = true)]
            public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder sb);
            [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
            public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
    
            public const int WM_SETTEXT = 0x000C;
            public const int VK_RETURN = 0x0D;
            public const int WM_KEYDOWN = 0x100;
            public const int WM_KEYUP = 0x101;
            public const int WM_GETTEXTLENGTH = 0x000E;
            public const int WM_GETTEXT = 0x000D; 
    
            public MW2KICKER()
            {
                InitializeComponent();
                
            }
    
            private void bStatus_Click(object sender, EventArgs e)
            {
                //Find the console edit box aswell as send the status command - works fine.
                string message = "status";
                StringBuilder sb = new StringBuilder(message);
                IntPtr MW4CONSOLE = FindWindow("IW4 WinConsole", null);
                IntPtr MW4EDIT = FindWindowEx(MW4CONSOLE, IntPtr.Zero, "Edit", IntPtr.Zero);
                SendMessage(MW4EDIT, WM_SETTEXT, 0, sb);
                PostMessage(MW4EDIT, WM_KEYDOWN, VK_RETURN, IntPtr.Zero);
                PostMessage(MW4EDIT, WM_KEYUP, VK_RETURN, IntPtr.Zero);
    
            
                IntPtr MW4TEXT = FindWindowEx(MW4CONSOLE, MW4EDIT, "Edit", IntPtr.Zero);
    
                int length = SendMessage(MW4TEXT, WM_GETTEXTLENGTH, 0, null);
                StringBuilder sbtext = new StringBuilder(length + 1);
    
    
                SendMessage(MW4TEXT, WM_GETTEXT, sbtext.Capacity, sbtext);
                lbUserNames.Items.Add(sbtext.ToString());
    
            }
    
    
    
    
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width