Skip to content

Commit e534fff

Browse files
committed
Issues #13 Fixed 2000 character limit for some types of windows
1 parent 16139b9 commit e534fff

File tree

4 files changed

+69
-58
lines changed

4 files changed

+69
-58
lines changed
Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Text;
3-
using System.Runtime.InteropServices;
4-
using System.ComponentModel;
5-
using System.Windows.Automation;
6-
using WindowTextExtractor.Native;
7-
using WindowTextExtractor.Native.Structs;
1+
using System.Windows.Automation;
82

93
namespace WindowTextExtractor.Extensions
104
{
@@ -28,51 +22,5 @@ public static string GetTextFromWindow(this AutomationElement element)
2822
return element.Current.Name;
2923
}
3024
}
31-
32-
public static string GetTextFromConsole(this AutomationElement element)
33-
{
34-
try
35-
{
36-
Kernel32.FreeConsole();
37-
var result = Kernel32.AttachConsole(element.Current.ProcessId);
38-
if (!result)
39-
{
40-
var error = Marshal.GetLastWin32Error();
41-
throw new Win32Exception(error);
42-
}
43-
var handle = Kernel32.GetStdHandle(Constants.STD_OUTPUT_HANDLE);
44-
if (handle == IntPtr.Zero)
45-
{
46-
var error = Marshal.GetLastWin32Error();
47-
throw new Win32Exception(error);
48-
}
49-
ConsoleScreenBufferInfo binfo;
50-
result = Kernel32.GetConsoleScreenBufferInfo(handle, out binfo);
51-
if (!result)
52-
{
53-
var error = Marshal.GetLastWin32Error();
54-
throw new Win32Exception(error);
55-
}
56-
57-
var buffer = new char[binfo.srWindow.Right];
58-
var textBuilder = new StringBuilder();
59-
for (var i = 0; i < binfo.dwSize.Y; i++)
60-
{
61-
uint numberOfCharsRead;
62-
if (Kernel32.ReadConsoleOutputCharacter(handle, buffer, (uint)buffer.Length, new Coord(0, (short)i), out numberOfCharsRead))
63-
{
64-
textBuilder.AppendLine(new string(buffer));
65-
}
66-
}
67-
68-
var text = textBuilder.ToString().TrimEnd();
69-
return text;
70-
}
71-
catch
72-
{
73-
Kernel32.FreeConsole();
74-
return null;
75-
}
76-
}
7725
}
7826
}

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,23 @@ public bool PreFilterMessage(ref Message m)
867867
User32.SetCursor(cursorHandle);
868868
var cursorPosition = Cursor.Position;
869869
var element = AutomationElement.FromPoint(new System.Windows.Point(cursorPosition.X, cursorPosition.Y));
870-
if (element != null && element.Current.ProcessId != _processId)
870+
var windowProcessId = element.Current.ProcessId;
871+
if (element != null && windowProcessId != _processId)
871872
{
872873
var windowHandle = new IntPtr(element.Current.NativeWindowHandle);
873-
windowHandle = windowHandle == IntPtr.Zero ? User32.WindowFromPoint(new Point(cursorPosition.X, cursorPosition.Y)) : windowHandle;
874+
if (windowHandle == IntPtr.Zero)
875+
{
876+
windowHandle = User32.WindowFromPoint(new Point(cursorPosition.X, cursorPosition.Y));
877+
if (windowHandle != IntPtr.Zero)
878+
{
879+
User32.GetWindowThreadProcessId(windowHandle, out windowProcessId);
880+
var windowStyle = User32.GetWindowLong(windowHandle, Constants.GWL_STYLE);
881+
if (((windowStyle & Constants.WS_CAPTION) != 0 || (windowStyle & Constants.WS_SYSMENU) != 0 || (windowStyle & Constants.WS_POPUP) != 0))
882+
{
883+
element = AutomationElement.FromHandle(windowHandle);
884+
}
885+
}
886+
}
874887

875888
var previousHandle = IntPtr.Zero;
876889
var previousProcessId = 0;
@@ -898,7 +911,7 @@ public bool PreFilterMessage(ref Message m)
898911
return false;
899912
}
900913

901-
using var process = Process.GetProcessById(element.Current.ProcessId);
914+
using var process = Process.GetProcessById(windowProcessId);
902915
if (element.Current.IsPassword)
903916
{
904917
if (process.ProcessName.ToLower() == "iexplore")
@@ -933,7 +946,7 @@ public bool PreFilterMessage(ref Message m)
933946
}
934947
else
935948
{
936-
var text = element.GetTextFromConsole() ?? element.GetTextFromWindow();
949+
var text = WindowUtils.ExtractTextFromConsoleWindow(windowProcessId) ?? element.GetTextFromWindow();
937950
text = text == null ? "" : text.TrimEnd().TrimEnd(Environment.NewLine);
938951
if (_settings.ShowEmptyItems || (!_settings.ShowEmptyItems && !string.IsNullOrEmpty(text)))
939952
{
@@ -947,7 +960,7 @@ public bool PreFilterMessage(ref Message m)
947960
lock (_lockObject)
948961
{
949962
_windowHandle = windowHandle;
950-
_windowProcessId = element.Current.ProcessId;
963+
_windowProcessId = windowProcessId;
951964
scale = _settings.Scale;
952965
captureCursor = _settings.CaptureCursor;
953966
}

WindowTextExtractor/Native/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ static class Constants
2929
public const int DWL_DLGPROC = 4;
3030
public const int DWL_USER = 8;
3131

32+
// WindowStyle
33+
public const long WS_CAPTION = 0x00C00000L;
34+
public const long WS_SYSMENU = 0x00080000L;
35+
public const long WS_POPUP = 0x80000000L;
36+
3237
// LayeredWindowAttributes
3338
public const int LWA_ALPHA = 0x00000002;
3439

WindowTextExtractor/Utils/WindowUtils.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Drawing;
77
using System.Diagnostics;
88
using System.Runtime.InteropServices;
9+
using System.ComponentModel;
910
using System.Management;
1011
using System.Windows.Forms;
1112
using mshtml;
@@ -53,6 +54,50 @@ public static IList<string> GetPasswordsFromHtmlPage(IntPtr handle)
5354
return result;
5455
}
5556

57+
public static string ExtractTextFromConsoleWindow(int processId)
58+
{
59+
try
60+
{
61+
Kernel32.FreeConsole();
62+
var result = Kernel32.AttachConsole(processId);
63+
if (!result)
64+
{
65+
var error = Marshal.GetLastWin32Error();
66+
throw new Win32Exception(error);
67+
}
68+
var handle = Kernel32.GetStdHandle(Constants.STD_OUTPUT_HANDLE);
69+
if (handle == IntPtr.Zero)
70+
{
71+
var error = Marshal.GetLastWin32Error();
72+
throw new Win32Exception(error);
73+
}
74+
result = Kernel32.GetConsoleScreenBufferInfo(handle, out var binfo);
75+
if (!result)
76+
{
77+
var error = Marshal.GetLastWin32Error();
78+
throw new Win32Exception(error);
79+
}
80+
81+
var buffer = new char[binfo.srWindow.Right];
82+
var textBuilder = new StringBuilder();
83+
for (var i = 0; i < binfo.dwSize.Y; i++)
84+
{
85+
if (Kernel32.ReadConsoleOutputCharacter(handle, buffer, (uint)buffer.Length, new Coord(0, (short)i), out var numberOfCharsRead))
86+
{
87+
textBuilder.AppendLine(new string(buffer));
88+
}
89+
}
90+
91+
var text = textBuilder.ToString().TrimEnd();
92+
return text;
93+
}
94+
catch
95+
{
96+
Kernel32.FreeConsole();
97+
return null;
98+
}
99+
}
100+
56101
public static WindowInformation GetWindowInformation(IntPtr handle, Point cursorPosition)
57102
{
58103
var text = GetWindowText(handle);

0 commit comments

Comments
 (0)