fix(windows): 内存探测改用 GetProcessMemoryInfo,不再启动 PowerShell

This commit is contained in:
qiqi committed 2026-11-23 00:57:46 +08:00
1 parent d2bef9f348
commit bc77f007a0
6 files changed
+79 -81

No files matched your search

+7 -14
View File
@@ -171,20 +171,13 @@ fn app_memory_mb() -> u64 {
}
#[cfg(windows)]
{
let script = format!("(Get-Process -Id {}).WorkingSet64", pid);
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
let out = std::process::Command::new("powershell")
.args(["-NoProfile", "-NonInteractive", "-Command", &script])
.creation_flags(CREATE_NO_WINDOW)
.output();
if let Ok(o) = out {
if o.status.success() {
let txt = String::from_utf8_lossy(&o.stdout);
if let Ok(bytes) = txt.trim().parse::<u64>() {
return bytes / (1024 * 1024);
}
}
use windows_sys::Win32::System::ProcessStatus::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
use windows_sys::Win32::System::Threading::GetCurrentProcess;
let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { std::mem::zeroed() };
pmc.cb = std::mem::size_of::<PROCESS_MEMORY_COUNTERS>() as u32;
let ok = unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) };
if ok != 0 {
return (pmc.WorkingSetSize as u64) / (1024 * 1024);
}
0
}