From b474aa7ef98af970043993aeeabf44362b7770c7 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 21 Jan 2024 18:20:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=88=E8=AF=95=E8=AF=95=20FreeConsole?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.rs | 14 +++++--------- src/main.rs | 3 +++ src/win.rs | 30 ++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index 255fa45..327aea0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,7 +18,7 @@ pub struct Config { pub chdir: Option, pub bin: PathBuf, pub config: Option, - pub bin_arg: String, + pub bin_arg: Vec, } impl Display for Config { @@ -42,7 +42,6 @@ impl Config { let mut bin: Option = None; let dir: Option = None; let mut config: Option = None; - let mut bin_arg = "".to_string(); // -- 表示后面的参数都是可执行文件的参数 let args: Vec = std::env::args().collect(); // 先检查有没有 --help @@ -52,14 +51,11 @@ impl Config { } let index = args.iter().position(|x| x == "--"); + let bin_arg: Vec; if index.is_some() { - // 把后面的所有函数拼接一下, 作为可执行文件的参数 - for i in index.unwrap() + 1..args.len() { - bin_arg.push_str(&args[i]); - if i != args.len() - 1 { - bin_arg.push(' '); - } - } + bin_arg = args[index.unwrap() + 1..].to_vec(); + } else { + bin_arg = Vec::new(); } // 先尝试获取指定的控制台参数 // --hide 表示隐藏控制台 diff --git a/src/main.rs b/src/main.rs index 46690a3..4863c45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,4 +18,7 @@ fn main() { // 输出相关信息 println!("call {}", VERSION); println!("config: {}", config); + // 运行 + #[cfg(windows)] + win::run(&config); } diff --git a/src/win.rs b/src/win.rs index 8cc1cdf..17619e4 100644 --- a/src/win.rs +++ b/src/win.rs @@ -1,3 +1,29 @@ -pub fn hide_window() {} +use crate::config::Config; +use winapi::um::wincon; -pub fn show_window() {} +pub fn call_bin(config: &Config) { + // 先切换工作目录 + if let Some(chdir) = config.chdir.as_ref() { + std::env::set_current_dir(chdir).unwrap(); + } + // 调用可执行文件 + std::process::Command::new(&config.bin) + .args(&config.bin_arg) + .spawn() + .expect("执行失败"); +} + +pub fn run(config: &Config) { + println!("先睡两秒为敬"); + // 先睡两秒为敬 + std::thread::sleep(std::time::Duration::from_secs(2)); + println!("睡醒了"); + // 尝试 FreeConsole 看看 + unsafe { + wincon::FreeConsole(); + } + println!("FreeConsole 了"); + // 调用可执行文件 + call_bin(&config); + std::thread::sleep(std::time::Duration::from_secs(2)); +}