[fix] powershell black screens and try to solve update re-start not working

This commit is contained in:
2025-11-08 19:14:18 +08:00
parent c50fedd305
commit 11afc6dc9e
12 changed files with 357 additions and 281 deletions

View File

@@ -377,14 +377,23 @@ pub fn install_app_update(installer_path: String) -> Result<(), String> {
#[cfg(target_os = "windows")]
pub async fn get_computer_info() -> Result<serde_json::Value, String> {
use tokio::process::Command;
#[cfg(windows)]
use std::os::windows::process::CommandExt;
#[cfg(windows)]
const CREATE_NO_WINDOW: u32 = 0x08000000;
// 异步执行 PowerShell 命令获取计算机信息并转换为 JSON
let output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-ComputerInfo | Select-Object OsName, OSDisplayVersion, BiosSMBIOSBIOSVersion, CsManufacturer, CsName | ConvertTo-Json -Compress"
])
let mut cmd = Command::new("powershell");
cmd.args(&[
"-NoProfile",
"-WindowStyle",
"Hidden",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-ComputerInfo | Select-Object OsName, OSDisplayVersion, BiosSMBIOSBIOSVersion, CsManufacturer, CsName | ConvertTo-Json -Compress"
]);
#[cfg(windows)]
cmd.creation_flags(CREATE_NO_WINDOW);
let output = cmd
.output()
.await
.map_err(|e| format!("执行 PowerShell 命令失败: {}", e))?;
@@ -423,14 +432,17 @@ pub async fn get_computer_info() -> Result<serde_json::Value, String> {
// 如果没有从 OSDisplayVersion 获取到版本代码,尝试从注册表获取 ReleaseId
if !json.get("ReleaseId").and_then(|v| v.as_str()).is_some() {
let release_id_output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; try { (Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion').ReleaseId } catch { $null }"
])
.output()
.await;
let mut release_cmd = Command::new("powershell");
release_cmd.args(&[
"-NoProfile",
"-WindowStyle",
"Hidden",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; try { (Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion').ReleaseId } catch { $null }"
]);
#[cfg(windows)]
release_cmd.creation_flags(CREATE_NO_WINDOW);
let release_id_output = release_cmd.output().await;
if let Ok(release_output) = release_id_output {
if release_output.status.success() {
@@ -552,14 +564,23 @@ pub struct MotherboardInfo {
#[cfg(target_os = "windows")]
pub async fn get_memory_info() -> Result<Vec<MemoryInfo>, String> {
use tokio::process::Command;
#[cfg(windows)]
use std::os::windows::process::CommandExt;
#[cfg(windows)]
const CREATE_NO_WINDOW: u32 = 0x08000000;
// 执行 PowerShell 命令获取内存信息
let output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-WmiObject Win32_PhysicalMemory | Select-Object Capacity, Manufacturer, ConfiguredClockSpeed, Speed | ConvertTo-Json -Compress"
])
let mut cmd = Command::new("powershell");
cmd.args(&[
"-NoProfile",
"-WindowStyle",
"Hidden",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-WmiObject Win32_PhysicalMemory | Select-Object Capacity, Manufacturer, ConfiguredClockSpeed, Speed | ConvertTo-Json -Compress"
]);
#[cfg(windows)]
cmd.creation_flags(CREATE_NO_WINDOW);
let output = cmd
.output()
.await
.map_err(|e| format!("执行 PowerShell 命令失败: {}", e))?;
@@ -635,14 +656,23 @@ pub async fn get_memory_info() -> Result<Vec<MemoryInfo>, String> {
#[cfg(target_os = "windows")]
pub async fn get_monitor_info() -> Result<Vec<MonitorInfo>, String> {
use tokio::process::Command;
#[cfg(windows)]
use std::os::windows::process::CommandExt;
#[cfg(windows)]
const CREATE_NO_WINDOW: u32 = 0x08000000;
// 执行 PowerShell 命令获取显示器信息
let output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorID | ForEach-Object { [PSCustomObject]@{ Manufacturer = [System.Text.Encoding]::ASCII.GetString($_.ManufacturerName) -replace \"`0\"; Model = [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName) -replace \"`0\" } } | ConvertTo-Json -Compress"
])
let mut cmd = Command::new("powershell");
cmd.args(&[
"-NoProfile",
"-WindowStyle",
"Hidden",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorID | ForEach-Object { [PSCustomObject]@{ Manufacturer = [System.Text.Encoding]::ASCII.GetString($_.ManufacturerName) -replace \"`0\"; Model = [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName) -replace \"`0\" } } | ConvertTo-Json -Compress"
]);
#[cfg(windows)]
cmd.creation_flags(CREATE_NO_WINDOW);
let output = cmd
.output()
.await
.map_err(|e| format!("执行 PowerShell 命令失败: {}", e))?;
@@ -673,34 +703,43 @@ pub async fn get_monitor_info() -> Result<Vec<MonitorInfo>, String> {
}
// 尝试获取刷新率和分辨率信息
let _display_output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorBasicDisplayParams | Select-Object MaxHorizontalImageSize, MaxVerticalImageSize | ConvertTo-Json -Compress"
])
.output()
.await;
let mut _display_cmd = Command::new("powershell");
_display_cmd.args(&[
"-NoProfile",
"-WindowStyle",
"Hidden",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorBasicDisplayParams | Select-Object MaxHorizontalImageSize, MaxVerticalImageSize | ConvertTo-Json -Compress"
]);
#[cfg(windows)]
_display_cmd.creation_flags(CREATE_NO_WINDOW);
let _display_output = _display_cmd.output().await;
// 获取刷新率信息
let _refresh_output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorListedSupportedSourceModes | Select-Object -First 1 -ExpandProperty ModeTimings | Select-Object -First 1 -ExpandProperty RefreshRate | ConvertTo-Json -Compress"
])
.output()
.await;
let mut _refresh_cmd = Command::new("powershell");
_refresh_cmd.args(&[
"-NoProfile",
"-WindowStyle",
"Hidden",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorListedSupportedSourceModes | Select-Object -First 1 -ExpandProperty ModeTimings | Select-Object -First 1 -ExpandProperty RefreshRate | ConvertTo-Json -Compress"
]);
#[cfg(windows)]
_refresh_cmd.creation_flags(CREATE_NO_WINDOW);
let _refresh_output = _refresh_cmd.output().await;
// 获取当前显示器的分辨率和刷新率
let current_display_output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Add-Type -AssemblyName System.Windows.Forms; $screens = [System.Windows.Forms.Screen]::AllScreens; $screens | ForEach-Object { [PSCustomObject]@{ Width = $_.Bounds.Width; Height = $_.Bounds.Height; Primary = $_.Primary } } | ConvertTo-Json -Compress"
])
.output()
.await;
let mut current_display_cmd = Command::new("powershell");
current_display_cmd.args(&[
"-NoProfile",
"-WindowStyle",
"Hidden",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Add-Type -AssemblyName System.Windows.Forms; $screens = [System.Windows.Forms.Screen]::AllScreens; $screens | ForEach-Object { [PSCustomObject]@{ Width = $_.Bounds.Width; Height = $_.Bounds.Height; Primary = $_.Primary } } | ConvertTo-Json -Compress"
]);
#[cfg(windows)]
current_display_cmd.creation_flags(CREATE_NO_WINDOW);
let current_display_output = current_display_cmd.output().await;
// 合并显示器信息
if let Ok(display_result) = current_display_output {
@@ -783,14 +822,23 @@ pub async fn get_monitor_info() -> Result<Vec<MonitorInfo>, String> {
#[cfg(target_os = "windows")]
pub async fn get_motherboard_info() -> Result<MotherboardInfo, String> {
use tokio::process::Command;
#[cfg(windows)]
use std::os::windows::process::CommandExt;
#[cfg(windows)]
const CREATE_NO_WINDOW: u32 = 0x08000000;
// 执行 PowerShell 命令获取主板信息
let output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance -ClassName Win32_BaseBoard | Select-Object Manufacturer, Product, Version | ConvertTo-Json -Compress"
])
let mut cmd = Command::new("powershell");
cmd.args(&[
"-NoProfile",
"-WindowStyle",
"Hidden",
"-Command",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance -ClassName Win32_BaseBoard | Select-Object Manufacturer, Product, Version | ConvertTo-Json -Compress"
]);
#[cfg(windows)]
cmd.creation_flags(CREATE_NO_WINDOW);
let output = cmd
.output()
.await
.map_err(|e| format!("执行 PowerShell 命令失败: {}", e))?;