Skip to content

Commit 6b45a50

Browse files
committed
fix(Window::Name): Fix possible crash when trying to get the name of a Window
Fixes #1095
1 parent 6689f8c commit 6b45a50

File tree

1 file changed

+30
-21
lines changed
  • SoundSwitch.Audio.Manager/Interop/Com/User

1 file changed

+30
-21
lines changed

SoundSwitch.Audio.Manager/Interop/Com/User/User32.cs

+30-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.ComponentModel;
3-
using System.Diagnostics;
43
using System.Runtime.InteropServices;
54
using System.Text;
65

@@ -109,48 +108,58 @@ public bool Equals(HWND other)
109108
}
110109
}
111110

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

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

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

119121
[DllImport("user32", EntryPoint = "GetWindowTextA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
120122
public static extern int GetWindowText(HWND hwnd, StringBuilder lpString, int cch);
123+
121124
[DllImport("user32", EntryPoint = "GetWindowTextLengthA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
122125
public static extern int GetWindowTextLength(HWND hwnd);
123126

124127
[DllImport("user32", EntryPoint = "GetClassNameA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
125128
public static extern int GetClassName(HWND hWnd, StringBuilder text, int count);
126-
127-
[DllImport("user32.dll", CharSet =CharSet.Auto)]
128-
public static extern bool IsWindow( HWND hwnd );
129+
130+
[DllImport("user32.dll", CharSet = CharSet.Auto)]
131+
public static extern bool IsWindow(HWND hwnd);
129132

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

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

135-
internal const uint WINEVENT_OUTOFCONTEXT = 0;
136-
internal const int EVENT_OBJECT_DESTROY = 0x8001;
137-
internal const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
138+
internal const uint WINEVENT_OUTOFCONTEXT = 0;
139+
internal const int EVENT_OBJECT_DESTROY = 0x8001;
140+
internal const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
138141
internal const uint EVENT_SYSTEM_MINIMIZEEND = 0x0017;
139-
internal const int MAX_PATH = 260;
142+
internal const int MAX_PATH = 260;
143+
//
144+
// Window text
145+
//
146+
147+
internal const uint WM_GETTEXTLENGTH = 0x000E;
148+
internal const uint WM_GETTEXT = 0x000D;
140149

141150

142151
//
143152
// IAccessible / OLEACC / WinEvents
144153
//
145154

146-
public const int CHILDID_SELF = 0;
155+
public const int CHILDID_SELF = 0;
147156
public const int STATE_SYSTEM_UNAVAILABLE = 0x00000001;
148-
public const int STATE_SYSTEM_FOCUSED = 0x00000004;
149-
public const int OBJID_CARET = -8;
150-
public const int OBJID_CLIENT = -4;
151-
public const int OBJID_MENU = -3;
152-
public const int OBJID_SYSMENU = -1;
153-
public const int OBJID_WINDOW = 0;
157+
public const int STATE_SYSTEM_FOCUSED = 0x00000004;
158+
public const int OBJID_CARET = -8;
159+
public const int OBJID_CLIENT = -4;
160+
public const int OBJID_MENU = -3;
161+
public const int OBJID_SYSMENU = -1;
162+
public const int OBJID_WINDOW = 0;
154163
}
155164

156165
public static uint ForegroundProcessId
@@ -168,9 +177,9 @@ public static uint ForegroundProcessId
168177
/// </summary>
169178
public static string GetWindowText(NativeMethods.HWND window)
170179
{
171-
var length = NativeMethods.GetWindowTextLength(window) + 1;
172-
var sb = new StringBuilder(length);
173-
var result = NativeMethods.GetWindowText(window, sb, NativeMethods.MAX_PATH);
180+
var length = (int)NativeMethods.SendMessage(window, NativeMethods.WM_GETTEXTLENGTH, IntPtr.Zero, null);
181+
var sb = new StringBuilder(length+1);
182+
var result = NativeMethods.SendMessage(window, NativeMethods.WM_GETTEXT, sb.Capacity, sb);
174183
var lastWin32Error = Marshal.GetLastWin32Error();
175184

176185
if (result == 0)
@@ -186,8 +195,8 @@ public static string GetWindowText(NativeMethods.HWND window)
186195
/// </summary>
187196
public static string GetWindowClass(NativeMethods.HWND window)
188197
{
189-
var sb = new StringBuilder(NativeMethods.MAX_PATH);
190-
var result = NativeMethods.GetClassName(window, sb, NativeMethods.MAX_PATH);
198+
var sb = new StringBuilder(NativeMethods.MAX_PATH);
199+
var result = NativeMethods.GetClassName(window, sb, NativeMethods.MAX_PATH);
191200
var lastWin32Error = Marshal.GetLastWin32Error();
192201

193202
if (result == 0)

0 commit comments

Comments
 (0)