From d15b8070ec5d330ec7ac1844f0f5140d8dbe17ee Mon Sep 17 00:00:00 2001 From: futile Date: Sat, 11 Oct 2025 03:32:39 +0100 Subject: [PATCH] improve strings in `util::system_info` --- src/util/system_info.rs | 46 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/util/system_info.rs b/src/util/system_info.rs index 51459b2..a9b5efd 100644 --- a/src/util/system_info.rs +++ b/src/util/system_info.rs @@ -1,13 +1,13 @@ use std::{ - ffi::c_char, - os::windows::process::CommandExt, + ffi::{OsStr, OsString}, + os::windows::{ffi::OsStringExt, process::CommandExt}, path::{Path, PathBuf}, process::Command, }; use windows::{ Win32::System::{ Diagnostics::ToolHelp::{ - CreateToolhelp32Snapshot, PROCESSENTRY32, Process32First, Process32Next, + CreateToolhelp32Snapshot, PROCESSENTRY32W, Process32FirstW, Process32NextW, TH32CS_SNAPPROCESS, }, Threading::{ @@ -21,7 +21,7 @@ use windows::{ #[derive(Clone, Debug)] pub struct Process { pid: u32, - name: String, + name: OsString, exe: Option, } @@ -30,7 +30,7 @@ impl Process { self.pid } - pub fn name(&self) -> &str { + pub fn name(&self) -> &OsStr { &self.name } @@ -58,22 +58,22 @@ impl SystemInfo { pub fn refresh(&mut self) { let mut processes = Vec::new(); let snapshot_handle = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) }.unwrap(); - let mut process_entry = PROCESSENTRY32 { - dwSize: size_of::() as u32, + let mut process_entry = PROCESSENTRY32W { + dwSize: size_of::() as u32, ..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); processes.push(Process { 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, }); - 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); processes.push(Process { 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, }); } @@ -85,7 +85,7 @@ impl SystemInfo { } } -fn get_exe_full_path(process_entry: &PROCESSENTRY32) -> Option { +fn get_exe_full_path(process_entry: &PROCESSENTRY32W) -> Option { let process_handle_result = unsafe { OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION, @@ -94,22 +94,24 @@ fn get_exe_full_path(process_entry: &PROCESSENTRY32) -> Option { ) }; process_handle_result.map_or(None, |process_handle| { - let mut exename = [0u16; 260]; - let mut dwsize = exename.len() as u32; - let exename = PWSTR(exename.as_mut_ptr()); + let mut exe_name = [0u16; 260]; + let mut dw_size = exe_name.len() as u32; 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 { - Ok(()) => Some(PathBuf::from(unsafe { exename.to_string() }.unwrap())), + Ok(()) => Some(PathBuf::from(wide_array_to_os_string(&exe_name))), Err(_) => None, } }) } -fn c_char_arr_to_string(arr: &[c_char]) -> String { - arr.iter() - .take_while(|&&b| b != 0) - .map(|&b| b as u8 as char) - .collect() +fn wide_array_to_os_string(wide: &[u16]) -> OsString { + let null_pos = wide.iter().position(|&x| x == 0).unwrap_or(wide.len()); + OsString::from_wide(&wide[..null_pos]) }