Click to See Complete Forum and Search --> : Scraping text from outside application's non-standard window


nightmares
October 5th, 2008, 02:20 PM
Hey Everyone

I'm writing an application, and part of it includes scraping the text from a chat box in another application currently running on the computer. So, if the chat box in another running program contains something like
Me: Hi
You: Hi
Me: lol
I want to get "Me: Hi You: Hi Me: lol" into a string variable in my application so that I can parse it as needed. I have tried using some win32 api calls as follows:


IntPtr handle = new IntPtr(11111); //note: i can get the window handle, this isn't the issue
int nChars = GetWindowTextLength(handle); //win32 function
StringBuilder Buff = new StringBuilder(nChars);

if (GetWindowText(handle, Buff, nChars) > 0) //also win32 function
{
Console.WriteLine(Buff.ToString());
Console.WriteLine(handle.ToString());
}
else
{
Console.WriteLine("fail");
}


I also tried:



IntPtr window_hwnd = 313312; //I can find the window handle, this isn't the problem
Int32 txtlen;

txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);

if (txtlen == 0)

return "";



txtlen = txtlen + 1;

StringBuilder txt = new StringBuilder(txtlen);

SendMessage(window_hwnd, WM_GETTEXT, txtlen, txt);

Console.WriteLine(txt.ToString());


Note that above these code fragments are all of the dllimports for the windows functions. Also, the code fragments work for other windows programs, like notepad. From what I understand, GetWindowText and sending a WM_GETTEXT message only work if the target window is a standard windows control. Because they aren't returning anything when I know there is text in the chat window, I have concluded that the chat window is not a standard windows control. Does anyone know how to get the text out of a non-standard windows control? Note: I'm trying to use the windows api but if I can't, that's ok.

Thanks for any comments/suggestions :)

Arjay
October 5th, 2008, 02:37 PM
Use the Spy++ tool to find out if you can grab the window text. If you can, then the window api's should work. If not, search this site for Active Accessibility. There are multiple pointers to its tools. Try the AccExplorer32 tool. If it works, then the active accessibility interfaces will work.

If neither of these tools work, you are out of luck.

Btw, grabbing text out of chat message window sounds a bit creepy. Why do you need to do this?

nightmares
October 5th, 2008, 03:55 PM
Thanks for the reply. I've tried spy++ and it can get 1 line of the chat as the window caption. However when I send a WM_GETTEXT to the window, it returns an empty string so I'm not sure how spy++ is getting this one line of the chat in its "Window Caption" field.

Could this be an issue related to the fact that I'm using Vista 64 bit? I forgot to mention this in the original post.

I'll look into active accessibility, thanks for the tip.

I want to scrape the text out of a chat window in an online poker client, which contains updates about the status of a hand. So, I can create a database of hands which allows me to estimate player tendencies better/get a better read on players. It's kind of like Poker Tracker (a commercially available piece of software) but I wanted to write one on my own for fun/learning.

nightmares
October 15th, 2008, 09:29 AM
Just as a quick follow-up, I wasn't able to solve this problem using c#. I ended up using c++ and dll injection to detect when the non-standard control was opened. Then, I subclassed the control so that I could intercept messages being sent to it. So, I never actually ended up scraping text from the control, as I intercepted it before it got there.

Thanks for the help anyways :)