rust执行cmd命令隐藏dos窗口-打开环境变量

180it 2023-03-11 PM 485℃ 0条

https://blog.csdn.net/weixin_35894173/article/details/112282032

https://rustcc.cn/article?id=f1630b61-4637-4e80-8414-8a921af50d68

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

关键命令:

![windows_subsystem = "windows"]

use std::os::windows::process::CommandExt;

.creation_flags(0x08000000)

#![windows_subsystem = "windows"]
use std::process::Command;
use std::os::windows::process::CommandExt;

fn main() {

    let output = if cfg!(target_os = "windows") {
        Command::new("cmd")
                .creation_flags(0x08000000)
                .arg("/C")
                .arg("rundll32 sysdm.cpl,EditEnvironmentVariables")
                .output()
                .expect("failed to execute process")
    } else {
        Command::new("sh")
                .arg("-c")
                .arg("echo hello")
                .output()
                .expect("failed to execute process")
    };
    
    let hello = output.stdout;
    println!("{:?}", hello)
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

rust执行cmd命令隐藏dos窗口-打开环境变量