Skip to content

Commit

Permalink
Issues #130 Fixed 1024 character limit for GetWindowText function
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderPro committed May 14, 2024
1 parent 77b7e4e commit 38691d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions SmartSystemMenu/Native/User32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 11 additions & 5 deletions SmartSystemMenu/Utils/WindowUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 38691d9

Please sign in to comment.