From 28c7538fc3b00bc88a62336bba79d04a0b841fc3 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 15 Apr 2025 09:06:31 +0800 Subject: [PATCH] Fix possible Win32Exception --- Flow.Launcher.Infrastructure/Win32Helper.cs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index 6be024389..798501ae3 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -365,21 +365,10 @@ namespace Flow.Launcher.Infrastructure // No installed English layout found if (enHKL == HKL.Null) return; - // When application is exiting, the Application.Current will be null - if (Application.Current == null) return; - - // Get the FL main window - var hwnd = GetWindowHandle(Application.Current.MainWindow, true); + // Get the foreground window + var hwnd = PInvoke.GetForegroundWindow(); if (hwnd == HWND.Null) return; - // Check if the FL main window is the current foreground window - if (!IsForegroundWindow(hwnd)) - { - var result = PInvoke.SetForegroundWindow(hwnd); - // If we cannot set the foreground window, we can use the foreground window and switch the layout - if (!result) hwnd = PInvoke.GetForegroundWindow(); - } - // Get the current foreground window thread ID var threadId = PInvoke.GetWindowThreadProcessId(hwnd); if (threadId == 0) throw new Win32Exception(Marshal.GetLastWin32Error());