Skip to content

Commit

Permalink
fix(Window::Name): Fix possible crash when trying to get the name of …
Browse files Browse the repository at this point in the history
…a Window

Fixes #1095
  • Loading branch information
Belphemur committed Jan 22, 2023
1 parent 6689f8c commit 6b45a50
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions SoundSwitch.Audio.Manager/Interop/Com/User/User32.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

Expand Down Expand Up @@ -109,48 +108,58 @@ public bool Equals(HWND other)
}
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr GetWindowThreadProcessId([In] HWND hWnd, [Out] out uint ProcessId);

[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern HWND GetForegroundWindow();

[DllImport("user32", EntryPoint = "GetWindowTextA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetWindowText(HWND hwnd, StringBuilder lpString, int cch);

[DllImport("user32", EntryPoint = "GetWindowTextLengthA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetWindowTextLength(HWND hwnd);

[DllImport("user32", EntryPoint = "GetClassNameA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetClassName(HWND hWnd, StringBuilder text, int count);
[DllImport("user32.dll", CharSet =CharSet.Auto)]
public static extern bool IsWindow( HWND hwnd );

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindow(HWND hwnd);

public delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, HWND hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

[DllImport("user32.dll")]
public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

internal const uint WINEVENT_OUTOFCONTEXT = 0;
internal const int EVENT_OBJECT_DESTROY = 0x8001;
internal const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
internal const uint WINEVENT_OUTOFCONTEXT = 0;
internal const int EVENT_OBJECT_DESTROY = 0x8001;
internal const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
internal const uint EVENT_SYSTEM_MINIMIZEEND = 0x0017;
internal const int MAX_PATH = 260;
internal const int MAX_PATH = 260;
//
// Window text
//

internal const uint WM_GETTEXTLENGTH = 0x000E;
internal const uint WM_GETTEXT = 0x000D;


//
// IAccessible / OLEACC / WinEvents
//

public const int CHILDID_SELF = 0;
public const int CHILDID_SELF = 0;
public const int STATE_SYSTEM_UNAVAILABLE = 0x00000001;
public const int STATE_SYSTEM_FOCUSED = 0x00000004;
public const int OBJID_CARET = -8;
public const int OBJID_CLIENT = -4;
public const int OBJID_MENU = -3;
public const int OBJID_SYSMENU = -1;
public const int OBJID_WINDOW = 0;
public const int STATE_SYSTEM_FOCUSED = 0x00000004;
public const int OBJID_CARET = -8;
public const int OBJID_CLIENT = -4;
public const int OBJID_MENU = -3;
public const int OBJID_SYSMENU = -1;
public const int OBJID_WINDOW = 0;
}

public static uint ForegroundProcessId
Expand All @@ -168,9 +177,9 @@ public static uint ForegroundProcessId
/// </summary>
public static string GetWindowText(NativeMethods.HWND window)
{
var length = NativeMethods.GetWindowTextLength(window) + 1;
var sb = new StringBuilder(length);
var result = NativeMethods.GetWindowText(window, sb, NativeMethods.MAX_PATH);
var length = (int)NativeMethods.SendMessage(window, NativeMethods.WM_GETTEXTLENGTH, IntPtr.Zero, null);
var sb = new StringBuilder(length+1);
var result = NativeMethods.SendMessage(window, NativeMethods.WM_GETTEXT, sb.Capacity, sb);
var lastWin32Error = Marshal.GetLastWin32Error();

if (result == 0)
Expand All @@ -186,8 +195,8 @@ public static string GetWindowText(NativeMethods.HWND window)
/// </summary>
public static string GetWindowClass(NativeMethods.HWND window)
{
var sb = new StringBuilder(NativeMethods.MAX_PATH);
var result = NativeMethods.GetClassName(window, sb, NativeMethods.MAX_PATH);
var sb = new StringBuilder(NativeMethods.MAX_PATH);
var result = NativeMethods.GetClassName(window, sb, NativeMethods.MAX_PATH);
var lastWin32Error = Marshal.GetLastWin32Error();

if (result == 0)
Expand Down

0 comments on commit 6b45a50

Please sign in to comment.