diff --git a/SmartContextMenu/Native/User32.cs b/SmartContextMenu/Native/User32.cs index bd83111..b53bd72 100644 --- a/SmartContextMenu/Native/User32.cs +++ b/SmartContextMenu/Native/User32.cs @@ -33,6 +33,9 @@ static class User32 [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowText(IntPtr handle, StringBuilder title, int size); + [DllImport("user32.dll", CharSet = CharSet.Auto)] + public static extern int GetWindowTextLength(IntPtr handle); + [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int GetClassName(IntPtr handle, StringBuilder className, int size); diff --git a/SmartContextMenu/Utils/WindowUtils.cs b/SmartContextMenu/Utils/WindowUtils.cs index 06cf86b..d37b5f4 100644 --- a/SmartContextMenu/Utils/WindowUtils.cs +++ b/SmartContextMenu/Utils/WindowUtils.cs @@ -324,10 +324,17 @@ public static void SetOpacity(IntPtr hWnd, byte opacity) public static string GetWindowText(IntPtr hWnd) { - var builder = new StringBuilder(1024); - User32.GetWindowText(hWnd, builder, builder.Capacity); - var windowText = builder.ToString(); - return windowText; + var length = GetWindowTextLength(hWnd); + if (length > 0) + { + var builder = new StringBuilder(length + 1); + User32.GetWindowText(hWnd, builder, builder.Capacity); + return builder.ToString(); + } + else + { + return string.Empty; + } } public static string GetClassName(IntPtr hWnd)