diff --git a/.changes/skip-host-process.md b/.changes/skip-host-process.md new file mode 100644 index 0000000..e2ef4f3 --- /dev/null +++ b/.changes/skip-host-process.md @@ -0,0 +1,6 @@ +--- +"nsis_tauri_utils": "patch" +"nsis_process": "patch" +--- + +Skip processes with the same pid as the current installer's process to prevent the installer from killing itself. diff --git a/crates/nsis-process/src/lib.rs b/crates/nsis-process/src/lib.rs index b0031a0..b1a0a4b 100644 --- a/crates/nsis-process/src/lib.rs +++ b/crates/nsis-process/src/lib.rs @@ -13,7 +13,7 @@ use windows_sys::Win32::{ }, }; -/// Test if there is a running process with the given name. The input and process names are case-insensitive. +/// Test if there is a running process with the given name, skipping processes with the host's pid. The input and process names are case-insensitive. /// /// # Safety /// @@ -36,7 +36,7 @@ pub unsafe extern "C" fn FindProcess( } } -/// Kill all running process with the given name. The input and process names are case-insensitive. +/// Kill all running process with the given name, skipping processes with the host's pid. The input and process names are case-insensitive. /// /// # Safety /// @@ -71,6 +71,7 @@ fn kill(pid: u32) -> bool { } fn get_processes(name: &str) -> Vec { + let current_pid = std::process::id(); let mut processes = Vec::new(); unsafe { @@ -83,11 +84,12 @@ fn get_processes(name: &str) -> Vec { if Process32FirstW(handle, &mut process) != 0 { while Process32NextW(handle, &mut process) != 0 { - if decode_wide(&process.szExeFile) - .to_str() - .unwrap_or_default() - .to_lowercase() - == name.to_lowercase() + if current_pid != process.th32ProcessID + && decode_wide(&process.szExeFile) + .to_str() + .unwrap_or_default() + .to_lowercase() + == name.to_lowercase() { processes.push(process.th32ProcessID); }