Results 1 to 4 of 4

Thread: Unable to get text from window

Threaded View

  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.

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