Skip to content

Commit 906295c

Browse files
committed
Issues #13 Fixed 1024 character limit for GetWindowText function
1 parent f6489a1 commit 906295c

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

WindowTextExtractor/Native/User32.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ static class User32
4242

4343
[DllImport("user32.dll")]
4444
[return: MarshalAs(UnmanagedType.Bool)]
45-
public static extern bool GetClientRect(IntPtr handle, out Rect lpRect);
45+
public static extern bool GetClientRect(IntPtr hWnd, out Rect lpRect);
4646

4747
[DllImport("user32.dll", CharSet = CharSet.Auto)]
4848
public static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
4949

50+
[DllImport("user32.dll", CharSet = CharSet.Auto)]
51+
public static extern int GetWindowTextLength(IntPtr hWnd);
52+
5053
[DllImport("user32.dll")]
5154
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
5255

WindowTextExtractor/Utils/WindowUtils.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ public static void SetTransparency(IntPtr handle, int percent)
316316
SetOpacity(handle, opacity);
317317
}
318318

319-
320319
public static void SetOpacity(IntPtr handle, byte opacity)
321320
{
322321
var exStyle = User32.GetWindowLong(handle, Constants.GWL_EXSTYLE);
@@ -326,10 +325,17 @@ public static void SetOpacity(IntPtr handle, byte opacity)
326325

327326
private static string GetWindowText(IntPtr handle)
328327
{
329-
var builder = new StringBuilder(1024);
330-
User32.GetWindowText(handle, builder, builder.Capacity);
331-
var windowText = builder.ToString();
332-
return windowText;
328+
var length = User32.GetWindowTextLength(handle);
329+
if (length > 0)
330+
{
331+
var builder = new StringBuilder(length + 1);
332+
User32.GetWindowText(handle, builder, builder.Capacity);
333+
return builder.ToString();
334+
}
335+
else
336+
{
337+
return string.Empty;
338+
}
333339
}
334340

335341
private static string GetWmGettext(IntPtr handle)

0 commit comments

Comments
 (0)