Even with #![windows_subsystem = "windows"] at the top of my application, using std::process::Command creates a console window for 0.1 to 0.3 seconds. Is there a way to use std::process::Command in an hidden way?
In C#, I could've used p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; — is there something similar for Rust?
Example usage of std::process::command that creates a console window:
Command::new("cmd").args(&["/C", "start", &exe_path]);
exePath is the path to a Windows GUI Application.
You could use std::os::windows::process::CommandExt::creation_flags. Please refer to the documentation page for the Process Creation Flags.
You wrote that this is a GUI application, so I assume you don't need the console output on this one. DETACHED_PROCESS does not create conhost.exe, but if you want to process the output you should use CREATE_NO_WINDOW.
I would also recommend using start as the command because otherwise you will have to use cmd.exe and this will probably delay the start by a few milliseconds.
use std::process::Command;
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x08000000;
const DETACHED_PROCESS: u32 = 0x00000008;
let mut command = Command::new("cmd").args(&["/C", "start", &exe_path]);
command.creation_flags(DETACHED_PROCESS); // Be careful: This only works on windows
// If you use DETACHED_PROCESS you could set stdout, stderr, and stdin to Stdio::null() to avoid possible allocations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With