启动后退出控制台

This commit is contained in:
zqm
2026-04-07 13:02:22 +08:00
parent d74f0bcafb
commit 1231e95a70
3 changed files with 121 additions and 15 deletions

View File

@@ -42,24 +42,68 @@ fn main() {
Err(e) => println!("Rename failed: {:?}", e),
}
// 读取 Updater 的 debug_mode 配置,决定启动方式
let updater_debug_mode = load_updater_debug_mode(&appdata_dir);
println!("Updater debug_mode: {}", updater_debug_mode);
// 启动 Updater.exe
println!("Starting Updater.exe...");
println!("Updater.exe path: {:?}", updater_exe);
println!("Updater.exe exists: {:?}", updater_exe.exists());
let started = spawn_updater(&updater_exe, updater_debug_mode);
match Command::new(updater_exe).spawn() {
Ok(child) => {
println!("Updater.exe started successfully with PID: {}", child.id());
// 等待一点时间,确保 Updater 有足够的时间启动
sleep(Duration::from_secs(1));
// 检查 Updater 进程是否仍在运行
if is_process_running("Updater.exe") {
println!("Updater.exe is running");
} else {
println!("Updater.exe may have exited immediately");
}
if started {
println!("Updater.exe started successfully");
// 等待一点时间,确保 Updater 有足够的时间启动
sleep(Duration::from_secs(1));
// 检查 Updater 进程是否仍在运行
if is_process_running("Updater.exe") {
println!("Updater.exe is running");
} else {
println!("Updater.exe may have exited immediately");
}
} else {
println!("Failed to start Updater.exe");
}
}
/// 读取 Updater 的配置文件,获取 debug_mode
fn load_updater_debug_mode(appdata_dir: &std::path::Path) -> bool {
let config_path = appdata_dir.join("Updater").join("config.json");
if let Ok(content) = fs::read_to_string(&config_path) {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) {
return json.get("debug_mode").and_then(|v| v.as_bool()).unwrap_or(false);
}
}
false
}
/// 根据 debug_mode 以不同方式启动 Updater.exe
/// - debug_mode = true → CREATE_NEW_CONSOLEUpdater 获得独立的新终端窗口
/// - debug_mode = false → DETACHED_PROCESSUpdater 完全后台运行,无控制台
fn spawn_updater(updater_exe: &std::path::Path, debug_mode: bool) -> bool {
use std::os::windows::process::CommandExt;
// Windows 进程创建标志
const CREATE_NEW_CONSOLE: u32 = 0x00000010;
const DETACHED_PROCESS: u32 = 0x00000008;
let creation_flags = if debug_mode {
CREATE_NEW_CONSOLE // 新建独立控制台窗口
} else {
DETACHED_PROCESS // 完全脱离控制台,后台静默运行
};
match Command::new(updater_exe)
.creation_flags(creation_flags)
.spawn()
{
Ok(child) => {
drop(child);
true
}
Err(e) => {
println!("Failed to start Updater.exe: {:?}", e);
false
}
Err(e) => println!("Failed to start Updater.exe: {:?}", e),
}
}