是否可以使用标准库生成进程而不在 Windows 中显示控制台窗口?

Jam*_*mes 4 windows rust

这就是我现在所拥有的:

Command::new("/path/to/application")
  .args("-param")
  .spawn()
Run Code Online (Sandbox Code Playgroud)

看起来 Rust 用于CreateProcessW运行 Windows 进程,它允许创建标志。也许有一个标志可以满足我的需要?

Rob*_*ner 12

你可以使用std::os::windows::process::CommandExt::creation_flags. 请参阅进程创建标志的文档页面,或者最好使用winapi中的常量。

您写道这是一个 GUI 应用程序,因此我假设您不需要此应用程序的控制台输出。DETACHED_PROCESS不会创建 conhost.exe,但如果您想处理输出,您应该使用CREATE_NO_WINDOW.

我还建议使用startas 命令,因为否则您将不得不使用cmd.exe,这可能会延迟启动几毫秒。

例子

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.
Run Code Online (Sandbox Code Playgroud)