启动后退出控制台

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

@@ -6,9 +6,22 @@ version = 4
name = "BootLoader"
version = "0.1.0"
dependencies = [
"serde_json",
"windows",
]
[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
[[package]]
name = "memchr"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
[[package]]
name = "proc-macro2"
version = "1.0.106"
@@ -27,6 +40,48 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "syn"
version = "2.0.117"
@@ -160,3 +215,9 @@ name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"

View File

@@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
windows = { version = "0.56", features = ["Win32_System_Console", "Win32_Foundation", "Win32_System_ProcessStatus"] }
serde_json = "1.0"
windows = { version = "0.56", features = ["Win32_System_Console", "Win32_Foundation", "Win32_System_ProcessStatus", "Win32_System_Threading"] }

View File

@@ -42,14 +42,16 @@ 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());
if started {
println!("Updater.exe started successfully");
// 等待一点时间,确保 Updater 有足够的时间启动
sleep(Duration::from_secs(1));
// 检查 Updater 进程是否仍在运行
@@ -58,8 +60,50 @@ fn main() {
} 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),
}
}