So while in NY, it came to pass that I would have to try to screen scrape a running application and get some data off of it to use in an external app (will explain later). Firstly, let me just say how annoying it is that I couldn't find jack about what I wanted to do out there in web-land. I found the world of info about scraping the web (like that's difficult) and scraping a mainframe app running in a terminal session (ditto), but nothing on scraping a Windows app.
So I started to look into the Windows API. I had done a lot of API programming before, but none yet in .NET. I just never had a reason to do it. Well let me be the one to tell you, it could not be easier. And before I go further, if you are going to do any .NET (C# and VB.NET) Windows API programming, go get the
Pinvoke Addin, which is awesome to use, and bookmark
www.pinvoke.net right this second.
So on to some code. This is about as far as I got - getting the title from a running form:
using System.Text;
using System.Runtime.InteropServices;
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
foreach(System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses())
{
txtProcs.Text += proc.ProcessName;
try
{
StringBuilder sb = new StringBuilder();
txtProcs.Text += " :: " + proc.Handle.ToString();
int hWnd = GetWindowText(proc.MainWindowHandle,sb,256);
txtProcs.Text += " :: " + sb.ToString();
}
catch //Because this is a quick mockup, you might get a proc with no window
{}
}
So there you have it. That will get you the window title. Now, I don't need to do the screen scraping, as it turns out, but I still have an academic interest, so if anyone knows anything or could point me in the right direction, I would very much appreciate it.
Happy p/Invoking!