diff --git a/SmartSystemMenu/Native/User32.cs b/SmartSystemMenu/Native/User32.cs index 893bed0..127dfd0 100644 --- a/SmartSystemMenu/Native/User32.cs +++ b/SmartSystemMenu/Native/User32.cs @@ -32,6 +32,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/SmartSystemMenu/Utils/WindowUtils.cs b/SmartSystemMenu/Utils/WindowUtils.cs index 24b5af9..dcea1e3 100644 --- a/SmartSystemMenu/Utils/WindowUtils.cs +++ b/SmartSystemMenu/Utils/WindowUtils.cs @@ -307,13 +307,19 @@ public static void SetOpacity(IntPtr hWnd, byte opacity) SetLayeredWindowAttributes(hWnd, 0, opacity, LWA_ALPHA); } - 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)