Skip to content

Commit 38691d9

Browse files
committed
Issues #130 Fixed 1024 character limit for GetWindowText function
1 parent 77b7e4e commit 38691d9

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

SmartSystemMenu/Native/User32.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ static class User32
3232
[DllImport("user32.dll", CharSet = CharSet.Auto)]
3333
public static extern int GetWindowText(IntPtr handle, StringBuilder title, int size);
3434

35+
[DllImport("user32.dll", CharSet = CharSet.Auto)]
36+
public static extern int GetWindowTextLength(IntPtr handle);
37+
3538
[DllImport("user32.dll", CharSet = CharSet.Auto)]
3639
public static extern int GetClassName(IntPtr handle, StringBuilder className, int size);
3740

SmartSystemMenu/Utils/WindowUtils.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,19 @@ public static void SetOpacity(IntPtr hWnd, byte opacity)
307307
SetLayeredWindowAttributes(hWnd, 0, opacity, LWA_ALPHA);
308308
}
309309

310-
311310
public static string GetWindowText(IntPtr hWnd)
312311
{
313-
var builder = new StringBuilder(1024);
314-
User32.GetWindowText(hWnd, builder, builder.Capacity);
315-
var windowText = builder.ToString();
316-
return windowText;
312+
var length = GetWindowTextLength(hWnd);
313+
if (length > 0)
314+
{
315+
var builder = new StringBuilder(length + 1);
316+
User32.GetWindowText(hWnd, builder, builder.Capacity);
317+
return builder.ToString();
318+
}
319+
else
320+
{
321+
return string.Empty;
322+
}
317323
}
318324

319325
public static string GetClassName(IntPtr hWnd)

0 commit comments

Comments
 (0)