执行cmd命令的时候隐藏cmd窗口

上代码:

// #![windows_subsystem = "windows"]

use std::process::{Command,Stdio};
use std::os::windows::process::CommandExt;

fn main(){
	let output = Command::new("cmd").creation_flags(0x08000000).arg("/c").arg("ping baidu.com")
	.stdout(Stdio::piped()).output().expect("cmd exec error!");
	println!("{}", String::from_utf8_lossy(&output.stdout));
}

解决过程

主要原理是,通过函数creation_flags设置CreateProcess函数的creation flags,设置为 隐藏窗口即可。

image.png https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.creation_flags

image.png

https://docs.microsoft.com/en-us/windows/win32/procthread/process-creation-flags

image.png