improve strings in util::system_info

This commit is contained in:
2025-10-11 03:32:39 +01:00
parent efccd6ed50
commit d15b8070ec
+24 -22
View File
@@ -1,13 +1,13 @@
use std::{ use std::{
ffi::c_char, ffi::{OsStr, OsString},
os::windows::process::CommandExt, os::windows::{ffi::OsStringExt, process::CommandExt},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command, process::Command,
}; };
use windows::{ use windows::{
Win32::System::{ Win32::System::{
Diagnostics::ToolHelp::{ Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, PROCESSENTRY32, Process32First, Process32Next, CreateToolhelp32Snapshot, PROCESSENTRY32W, Process32FirstW, Process32NextW,
TH32CS_SNAPPROCESS, TH32CS_SNAPPROCESS,
}, },
Threading::{ Threading::{
@@ -21,7 +21,7 @@ use windows::{
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Process { pub struct Process {
pid: u32, pid: u32,
name: String, name: OsString,
exe: Option<PathBuf>, exe: Option<PathBuf>,
} }
@@ -30,7 +30,7 @@ impl Process {
self.pid self.pid
} }
pub fn name(&self) -> &str { pub fn name(&self) -> &OsStr {
&self.name &self.name
} }
@@ -58,22 +58,22 @@ impl SystemInfo {
pub fn refresh(&mut self) { pub fn refresh(&mut self) {
let mut processes = Vec::new(); let mut processes = Vec::new();
let snapshot_handle = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) }.unwrap(); let snapshot_handle = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) }.unwrap();
let mut process_entry = PROCESSENTRY32 { let mut process_entry = PROCESSENTRY32W {
dwSize: size_of::<PROCESSENTRY32>() as u32, dwSize: size_of::<PROCESSENTRY32W>() as u32,
..Default::default() ..Default::default()
}; };
unsafe { Process32First(snapshot_handle, &raw mut process_entry) }.unwrap(); unsafe { Process32FirstW(snapshot_handle, &raw mut process_entry) }.unwrap();
let exe_full_path = get_exe_full_path(&process_entry); let exe_full_path = get_exe_full_path(&process_entry);
processes.push(Process { processes.push(Process {
pid: process_entry.th32ProcessID, pid: process_entry.th32ProcessID,
name: c_char_arr_to_string(&process_entry.szExeFile), name: wide_array_to_os_string(&process_entry.szExeFile),
exe: exe_full_path, exe: exe_full_path,
}); });
while unsafe { Process32Next(snapshot_handle, &raw mut process_entry) }.is_ok() { while unsafe { Process32NextW(snapshot_handle, &raw mut process_entry) }.is_ok() {
let exe_full_path = get_exe_full_path(&process_entry); let exe_full_path = get_exe_full_path(&process_entry);
processes.push(Process { processes.push(Process {
pid: process_entry.th32ProcessID, pid: process_entry.th32ProcessID,
name: c_char_arr_to_string(&process_entry.szExeFile), name: wide_array_to_os_string(&process_entry.szExeFile),
exe: exe_full_path, exe: exe_full_path,
}); });
} }
@@ -85,7 +85,7 @@ impl SystemInfo {
} }
} }
fn get_exe_full_path(process_entry: &PROCESSENTRY32) -> Option<PathBuf> { fn get_exe_full_path(process_entry: &PROCESSENTRY32W) -> Option<PathBuf> {
let process_handle_result = unsafe { let process_handle_result = unsafe {
OpenProcess( OpenProcess(
PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_QUERY_LIMITED_INFORMATION,
@@ -94,22 +94,24 @@ fn get_exe_full_path(process_entry: &PROCESSENTRY32) -> Option<PathBuf> {
) )
}; };
process_handle_result.map_or(None, |process_handle| { process_handle_result.map_or(None, |process_handle| {
let mut exename = [0u16; 260]; let mut exe_name = [0u16; 260];
let mut dwsize = exename.len() as u32; let mut dw_size = exe_name.len() as u32;
let exename = PWSTR(exename.as_mut_ptr());
let image_name_result = unsafe { let image_name_result = unsafe {
QueryFullProcessImageNameW(process_handle, PROCESS_NAME_WIN32, exename, &raw mut dwsize) QueryFullProcessImageNameW(
process_handle,
PROCESS_NAME_WIN32,
PWSTR(exe_name.as_mut_ptr()),
&raw mut dw_size,
)
}; };
match image_name_result { match image_name_result {
Ok(()) => Some(PathBuf::from(unsafe { exename.to_string() }.unwrap())), Ok(()) => Some(PathBuf::from(wide_array_to_os_string(&exe_name))),
Err(_) => None, Err(_) => None,
} }
}) })
} }
fn c_char_arr_to_string(arr: &[c_char]) -> String { fn wide_array_to_os_string(wide: &[u16]) -> OsString {
arr.iter() let null_pos = wide.iter().position(|&x| x == 0).unwrap_or(wide.len());
.take_while(|&&b| b != 0) OsString::from_wide(&wide[..null_pos])
.map(|&b| b as u8 as char)
.collect()
} }