|
-
Jul 19th, 2010, 10:10 PM
#1
Thread Starter
Member
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.

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.
-
Jul 19th, 2010, 11:58 PM
#2
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.
-
Jul 20th, 2010, 08:31 AM
#3
Thread Starter
Member
Re: Unable to get text from window
Edited, for people who don't like to click links.
-
Jul 21st, 2010, 07:01 PM
#4
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|