Scraping the text from another application with win32 api

the window is non-standard, so getwindowtext isn't working!

Page 1 of 1

1 Replies - 23317 Views - Last Post: 29 March 2009 - 05:04 PM Rate Topic: -----

#1 nightmares  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-October 08

Scraping the text from another application with win32 api

Post icon  Posted 04 October 2008 - 11:48 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 :)

Is This A Good Question/Topic? 0
  • +

Replies To: Scraping the text from another application with win32 api

#2 onecsguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-March 09

Re: Scraping the text from another application with win32 api

Posted 29 March 2009 - 05:04 PM

The problem is most likely that the window is using gdi to draw the text in color, or some other proprietary text engine [non OS]. There are 4 ways that you can possibly get the text, i have implemented 2 of them myself in other projects.

Option 1 [I had a fairly reliable code for this at one time, it took me about an hour to get working]:
Screen shot screen scrape ... this is actually very simple, especially if you know the font name and font size. You simply make the program grab the screen [there is c# sample code for this using BitBlt], then use GDI+ in c# to compile a lookup based off of a bitmap of each character. This lookup shouldnt actually be a bitmap, but rather an array of coordinates for the fonts drawing area for faster results.

Option 2 [I have created a c# packet sniffer with a built in c# compiler for custom message handling(JIT scripting basically)] there are c# implementations of packet sniffers, the chat text will most likely be unencrypted, and you can simply sniff on the same port that the chat program is running on.

Option 3: Use the clipboard ... simply select the window in code, and invoke Ctrl-A then Ctrl-C, then get clipboard data from Clipboard.GetText method.

Option 4: Hook into the api calls for GDI if in fact the program is using gdi to do the text rendering, I have no idea how to do this, but here is an article on it http://www.codeproje...em/hooksys.aspx

If you have any questions email me at "bra**nt@on***e*c**sgu**y.**.com" without the *'s
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1