Skip to content

Commit dc07e76

Browse files
committed
Add error handling for Get & SetWindowLong failure & Combine get style function
1 parent 829dbaa commit dc07e76

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

Flow.Launcher/Helper/WindowsInteropHelper.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,17 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
151151

152152
#region Alt Tab
153153

154-
private static IntPtr SetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong)
154+
private static int SetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong)
155155
{
156156
PInvoke.SetLastError(WIN32_ERROR.NO_ERROR); // Clear any existing error
157157

158-
return PInvoke.SetWindowLong(hWnd, nIndex, dwNewLong);
159-
}
158+
var result = PInvoke.SetWindowLong(hWnd, nIndex, dwNewLong);
159+
if (result == 0 && Marshal.GetLastPInvokeError() != 0)
160+
{
161+
throw new Win32Exception(Marshal.GetLastPInvokeError());
162+
}
160163

161-
private static int IntPtrToInt32(IntPtr intPtr)
162-
{
163-
return unchecked((int)intPtr.ToInt64());
164+
return result;
164165
}
165166

166167
/// <summary>
@@ -169,13 +170,12 @@ private static int IntPtrToInt32(IntPtr intPtr)
169170
/// <param name="window">To hide a window</param>
170171
public static void HideFromAltTab(Window window)
171172
{
172-
var helper = new WindowInteropHelper(window);
173-
var exStyle = PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
173+
var exStyle = GetCurrentWindowStyle(window);
174174

175175
// Add TOOLWINDOW style, remove APPWINDOW style
176176
var newExStyle = ((uint)exStyle | (uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) & ~(uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
177177

178-
SetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
178+
SetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
179179
}
180180

181181
/// <summary>
@@ -184,24 +184,27 @@ public static void HideFromAltTab(Window window)
184184
/// <param name="window">To restore the displayed window</param>
185185
public static void ShowInAltTab(Window window)
186186
{
187-
var helper = new WindowInteropHelper(window);
188-
var exStyle = PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
187+
var exStyle = GetCurrentWindowStyle(window);
189188

190189
// Remove the TOOLWINDOW style and add the APPWINDOW style.
191190
var newExStyle = ((uint)exStyle & ~(uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) | (uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
192191

193-
SetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
192+
SetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
194193
}
195194

196195
/// <summary>
197196
/// To obtain the current overridden style of a window.
198197
/// </summary>
199198
/// <param name="window">To obtain the style dialog window</param>
200199
/// <returns>current extension style value</returns>
201-
public static int GetCurrentWindowStyle(Window window)
200+
private static int GetCurrentWindowStyle(Window window)
202201
{
203-
var helper = new WindowInteropHelper(window);
204-
return PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
202+
var style = PInvoke.GetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
203+
if (style == 0 && Marshal.GetLastPInvokeError() != 0)
204+
{
205+
throw new Win32Exception(Marshal.GetLastPInvokeError());
206+
}
207+
return style;
205208
}
206209

207210
#endregion

0 commit comments

Comments
 (0)