5

This is what I have right now:

Command::new("/path/to/application")
  .args("-param")
  .spawn()

It looks like Rust uses CreateProcessW for running Windows processes, which allows creation flags. Perhaps there is a flag which will do what I need?

2
  • crates.io/search?q=windows, gl, but I don't think it's easy, windows... is based on window, maybe you want implement a windows service ?
    – Stargateur
    Commented Jan 11, 2020 at 6:29
  • you can use or CREATE_NO_WINDOW flag (will be no window, but will be console(invisible) created for process) or DETACHED_PROCESS - will be no console at all. this more efficient compare with CREATE_NO_WINDOW flag (will be no additional conhost.exe) but application can exit, if it assume console handles exist and work
    – RbMm
    Commented Jan 11, 2020 at 10:10

2 Answers 2

14

You could use std::os::windows::process::CommandExt::creation_flags. Please refer to the documentation page for the Process Creation Flags or ideally use the constants from winapi.

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.
2

std:os::windows::process::CommandExt extends the process::Command builder with Windows-specific options when building for Windows. No constant is defined in the standard library for CREATE_NO_WINDOW, so you'd either need to define it yourself or use the raw value of 0x08000000:

let command = Command::new("my-command")
    .args("param")
    .creation_flags(0x08000000)
    .spawn();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.