Skip to content

Commit 4e392d6

Browse files
committed
Issues #20 Fixed 1024 character limit for GetWindowText function
1 parent 40c80e3 commit 4e392d6

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

SmartContextMenu/Native/User32.cs

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

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

SmartContextMenu/Utils/WindowUtils.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,17 @@ public static void SetOpacity(IntPtr hWnd, byte opacity)
324324

325325
public static string GetWindowText(IntPtr hWnd)
326326
{
327-
var builder = new StringBuilder(1024);
328-
User32.GetWindowText(hWnd, builder, builder.Capacity);
329-
var windowText = builder.ToString();
330-
return windowText;
327+
var length = GetWindowTextLength(hWnd);
328+
if (length > 0)
329+
{
330+
var builder = new StringBuilder(length + 1);
331+
User32.GetWindowText(hWnd, builder, builder.Capacity);
332+
return builder.ToString();
333+
}
334+
else
335+
{
336+
return string.Empty;
337+
}
331338
}
332339

333340
public static string GetClassName(IntPtr hWnd)

0 commit comments

Comments
 (0)