From 7677615bae5b22a2deff675b623910fbd1c28b0e Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Tue, 26 May 2020 20:17:47 +0200 Subject: [PATCH] Adapt new process API to windows. --- src/main.rs | 8 ++++++-- src/process.rs | 25 +++++++------------------ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index d472795..78e3267 100644 --- a/src/main.rs +++ b/src/main.rs @@ -384,7 +384,8 @@ fn daemon_main(config_set: ConfigSet) { let mut child = crate::process::spawn_process( &espanso_path.to_string_lossy().to_string(), &vec!["worker".to_owned()], - ).expect("unable to create worker process"); + ) + .expect("unable to create worker process"); // Create a monitor thread that will exit with the same non-zero code if // the worker thread exits @@ -395,7 +396,10 @@ fn daemon_main(config_set: ConfigSet) { if let Ok(status) = result { if let Some(code) = status.code() { if code != 0 { - error!("worker process exited with non-zero code: {}, exiting", code); + error!( + "worker process exited with non-zero code: {}, exiting", + code + ); std::process::exit(code); } } diff --git a/src/process.rs b/src/process.rs index 6737b07..7a03222 100644 --- a/src/process.rs +++ b/src/process.rs @@ -18,27 +18,16 @@ */ use log::warn; -use std::process::{Command, Stdio, Child}; -use widestring::WideCString; use std::io; +use std::process::{Child, Command, Stdio}; #[cfg(target_os = "windows")] -pub fn spawn_process(cmd: &str, args: &Vec) { - // TODO: modify with https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html - let quoted_args: Vec = args.iter().map(|arg| format!("\"{}\"", arg)).collect(); - let quoted_args = quoted_args.join(" "); - let final_cmd = format!("\"{}\" {}", cmd, quoted_args); - unsafe { - let cmd_wstr = WideCString::from_str(&final_cmd); - if let Ok(string) = cmd_wstr { - let res = crate::bridge::windows::start_process(string.as_ptr()); - if res < 0 { - warn!("unable to start process: {}", final_cmd); - } - } else { - warn!("unable to convert process string into wide format") - } - } +pub fn spawn_process(cmd: &str, args: &Vec) -> io::Result { + use std::os::windows::process::CommandExt; + Command::new(cmd) + .creation_flags(0x00000008) // Detached Process + .args(args) + .spawn() } #[cfg(not(target_os = "windows"))]