From c2b7810c3375137666d6e744f47e2f1295de652f Mon Sep 17 00:00:00 2001 From: 21pages Date: Sat, 18 May 2024 08:24:28 +0800 Subject: [PATCH] windows kill flutter main window when --server close (#8077) Signed-off-by: 21pages --- src/core_main.rs | 2 +- src/ipc.rs | 2 ++ src/platform/windows.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/core_main.rs b/src/core_main.rs index 5e5c36ef4..d2fbb8443 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -565,7 +565,7 @@ fn core_main_invoke_new_connection(mut args: std::env::Args) -> Option) -> Option { unsafe { let hWnd = GetForegroundWindow(); @@ -2419,3 +2421,33 @@ pub fn is_x64() -> bool { } unsafe { sys_info.u.s().wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 } } + +#[cfg(feature = "flutter")] +pub fn kill_flutter_main_window() { + // It is used to kill the hidden flutter main window process, it can also kill the install window process + log::info!("kill flutter main window"); + unsafe { + let window_name = wide_string(&crate::get_app_name()); + let class_name = wide_string(FLUTTER_RUNNER_WIN32_WINDOW_CLASS); + let hwnd = FindWindowW(class_name.as_ptr(), window_name.as_ptr()); + if hwnd.is_null() { + return; + } + let mut process_id: u32 = 0; + GetWindowThreadProcessId(hwnd, &mut process_id as *mut u32); + if process_id == 0 { + return; + } + let output = Command::new("taskkill") + .arg("/F") + .arg("/PID") + .arg(process_id.to_string()) + .output() + .expect("Failed to execute command"); + if output.status.success() { + log::info!("kill flutter main window success"); + } else { + log::error!("kill flutter main window failed"); + } + } +}