Skip to content

Commit 00b61f3

Browse files
authored
Merge pull request #3581 from Flow-Launcher/get_window_long
Fix crash on ×32 devices
2 parents ae76c41 + b2f5713 commit 00b61f3

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

Flow.Launcher.Infrastructure/NativeMethods.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ SystemParametersInfo
2222

2323
SetForegroundWindow
2424

25-
GetWindowLong
25+
WINDOW_LONG_PTR_INDEX
2626
GetForegroundWindow
2727
GetDesktopWindow
2828
GetShellWindow

Flow.Launcher.Infrastructure/PInvokeExtensions.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
namespace Windows.Win32;
66

7-
// Edited from: https://github.com/files-community/Files
87
internal static partial class PInvoke
98
{
9+
// SetWindowLong
10+
// Edited from: https://github.com/files-community/Files
11+
1012
[DllImport("User32", EntryPoint = "SetWindowLongW", ExactSpelling = true)]
11-
static extern int _SetWindowLong(HWND hWnd, int nIndex, int dwNewLong);
13+
private static extern int _SetWindowLong(HWND hWnd, int nIndex, int dwNewLong);
1214

1315
[DllImport("User32", EntryPoint = "SetWindowLongPtrW", ExactSpelling = true)]
14-
static extern nint _SetWindowLongPtr(HWND hWnd, int nIndex, nint dwNewLong);
16+
private static extern nint _SetWindowLongPtr(HWND hWnd, int nIndex, nint dwNewLong);
1517

1618
// NOTE:
1719
// CsWin32 doesn't generate SetWindowLong on other than x86 and vice versa.
@@ -22,4 +24,22 @@ public static unsafe nint SetWindowLongPtr(HWND hWnd, WINDOW_LONG_PTR_INDEX nInd
2224
? _SetWindowLong(hWnd, (int)nIndex, (int)dwNewLong)
2325
: _SetWindowLongPtr(hWnd, (int)nIndex, dwNewLong);
2426
}
27+
28+
// GetWindowLong
29+
30+
[DllImport("User32", EntryPoint = "GetWindowLongW", ExactSpelling = true)]
31+
private static extern int _GetWindowLong(HWND hWnd, int nIndex);
32+
33+
[DllImport("User32", EntryPoint = "GetWindowLongPtrW", ExactSpelling = true)]
34+
private static extern nint _GetWindowLongPtr(HWND hWnd, int nIndex);
35+
36+
// NOTE:
37+
// CsWin32 doesn't generate GetWindowLong on other than x86 and vice versa.
38+
// For more info, visit https://github.com/microsoft/CsWin32/issues/882
39+
public static unsafe nint GetWindowLongPtr(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex)
40+
{
41+
return sizeof(nint) is 4
42+
? _GetWindowLong(hWnd, (int)nIndex)
43+
: _GetWindowLongPtr(hWnd, (int)nIndex);
44+
}
2545
}

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,17 @@ public static void DisableControlBox(Window window)
194194
SetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE, style);
195195
}
196196

197-
private static int GetWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex)
197+
private static nint GetWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex)
198198
{
199-
var style = PInvoke.GetWindowLong(hWnd, nIndex);
199+
var style = PInvoke.GetWindowLongPtr(hWnd, nIndex);
200200
if (style == 0 && Marshal.GetLastPInvokeError() != 0)
201201
{
202202
throw new Win32Exception(Marshal.GetLastPInvokeError());
203203
}
204204
return style;
205205
}
206206

207-
private static nint SetWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong)
207+
private static nint SetWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, nint dwNewLong)
208208
{
209209
PInvoke.SetLastError(WIN32_ERROR.NO_ERROR); // Clear any existing error
210210

0 commit comments

Comments
 (0)