A
Avi
Hi,
I need to capture visible text from a command line application for
some automation purpose. I have created functions using WIN32 API
functions GetForegroundWindow, GetWindowText, SendMessage etc.
[ Below is the functions I am using. ]
**Challenge I am facing is - when i try to get text from console
application it gives only the title message, other details are
missing. **
Can someone please help.
Thanks,
Av
[DllImport("user32.dll")]
static extern int GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(int hWnd, StringBuilder text,
int count);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int
wParam, StringBuilder lParam);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int
wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd,
StringBuilder lpClassName, int nMaxCount);
public static string GetWindowClassName(IntPtr hWnd)
{
StringBuilder buffer = new StringBuilder(128);
GetClassName(hWnd, buffer, buffer.Capacity);
return buffer.ToString();
}
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window,
EnumWindowProc callback, IntPtr i);
public Form1()
{
InitializeComponent();
}
#region GetChildWindows
/// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</
param>
/// <returns>List of child windows</returns>
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc
(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr
(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a
reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</
returns>
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException("GCHandle Target could
not be cast as List<IntPtr>");
}
list.Add(handle);
// You can modify this to check to see if you want to
cancel the operation, then return a null here
return true;
}
/// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it
for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</
returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr
parameter);
#endregion
private const int WM_GETTEXTLENGTH = 0x000E;
private const int WM_GETTEXT = 0x000D;
private void GetActiveWindow()
{
const int nChars = 256;
int handle = 0;
int length = 0;
string text = String.Empty;
StringBuilder Buff = new StringBuilder(nChars);
string sClassName = String.Empty;
handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
this.txtHandle.Text = handle.ToString();
this.txtCapturedText.Text = Buff.ToString();
}
text = this.txtCapturedText.Text + Environment.NewLine;
IntPtr winHwnd = FindWindow(null, Buff.ToString());//,
null);
List<IntPtr> lstWinHdle = GetChildWindows(winHwnd);
this.lstObjects.Items.Clear();
this.lstObjects.Items.Add(GetWindowClassName(winHwnd));
for (int i = 0; i < lstWinHdle.Count; i++ )
{
length = SendMessage(lstWinHdle, WM_GETTEXTLENGTH,
0, 0);
StringBuilder textTemp = new StringBuilder(length);
int hr = SendMessage(lstWinHdle, WM_GETTEXT, length
+1, textTemp);
text = text + textTemp.ToString() +
Environment.NewLine ;
//iGetClsName = GetClassName(lstWinHdle,
sClassName, 255);
this.lstObjects.Items.Add(GetWindowClassName(lstWinHdle
));
}
}
I need to capture visible text from a command line application for
some automation purpose. I have created functions using WIN32 API
functions GetForegroundWindow, GetWindowText, SendMessage etc.
[ Below is the functions I am using. ]
**Challenge I am facing is - when i try to get text from console
application it gives only the title message, other details are
missing. **
Can someone please help.
Thanks,
Av
[DllImport("user32.dll")]
static extern int GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(int hWnd, StringBuilder text,
int count);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int
wParam, StringBuilder lParam);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int
wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd,
StringBuilder lpClassName, int nMaxCount);
public static string GetWindowClassName(IntPtr hWnd)
{
StringBuilder buffer = new StringBuilder(128);
GetClassName(hWnd, buffer, buffer.Capacity);
return buffer.ToString();
}
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window,
EnumWindowProc callback, IntPtr i);
public Form1()
{
InitializeComponent();
}
#region GetChildWindows
/// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</
param>
/// <returns>List of child windows</returns>
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc
(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr
(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a
reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</
returns>
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException("GCHandle Target could
not be cast as List<IntPtr>");
}
list.Add(handle);
// You can modify this to check to see if you want to
cancel the operation, then return a null here
return true;
}
/// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it
for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</
returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr
parameter);
#endregion
private const int WM_GETTEXTLENGTH = 0x000E;
private const int WM_GETTEXT = 0x000D;
private void GetActiveWindow()
{
const int nChars = 256;
int handle = 0;
int length = 0;
string text = String.Empty;
StringBuilder Buff = new StringBuilder(nChars);
string sClassName = String.Empty;
handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
this.txtHandle.Text = handle.ToString();
this.txtCapturedText.Text = Buff.ToString();
}
text = this.txtCapturedText.Text + Environment.NewLine;
IntPtr winHwnd = FindWindow(null, Buff.ToString());//,
null);
List<IntPtr> lstWinHdle = GetChildWindows(winHwnd);
this.lstObjects.Items.Clear();
this.lstObjects.Items.Add(GetWindowClassName(winHwnd));
for (int i = 0; i < lstWinHdle.Count; i++ )
{
length = SendMessage(lstWinHdle, WM_GETTEXTLENGTH,
0, 0);
StringBuilder textTemp = new StringBuilder(length);
int hr = SendMessage(lstWinHdle, WM_GETTEXT, length
+1, textTemp);
text = text + textTemp.ToString() +
Environment.NewLine ;
//iGetClsName = GetClassName(lstWinHdle,
sClassName, 255);
this.lstObjects.Items.Add(GetWindowClassName(lstWinHdle
));
}
}