Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hide the console window for a process started with std::process::Command? [duplicate]

Tags:

windows

cmd

rust

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.

like image 909
Fredol Avatar asked Aug 08 '26 16:08

Fredol


1 Answers

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.

Example

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.
like image 132
Robin Lindner Avatar answered Aug 10 '26 06:08

Robin Lindner