是否可以使用标准库生成进程而不在 Windows 中显示控制台窗口? [英] Is it possible to use the standard library to spawn a process without showing the console window in Windows?
问题描述
这就是我现在所拥有的:
This is what I have right now:
Command::new("/path/to/application")
.args("-param")
.spawn()
看起来 Rust 使用 CreateProcessW
来运行 Windows 进程,它允许创建标志.也许有一个标志可以满足我的需要?
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?
推荐答案
你可以使用 std::os::windows::process::CommandExt::creation_flags
.请参阅进程创建标志<的文档页面/a> 或理想情况下使用 winapi中的常量一>.
您写道这是一个 GUI 应用程序,因此我假设您不需要此应用程序的控制台输出.DETACHED_PROCESS
不会创建 conhost.exe,但如果您想处理输出,您应该使用 CREATE_NO_WINDOW
.
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
.
我还建议使用 start
作为命令,否则您将不得不使用 cmd.exe
,这可能会使启动延迟几毫秒.>
示例
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.
这篇关于是否可以使用标准库生成进程而不在 Windows 中显示控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!