Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: windows process name in 0.29.6 are broken #1027

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use winapi::um::errhandlingapi::GetLastError;
use winapi::um::handleapi::CloseHandle;
use winapi::um::heapapi::{GetProcessHeap, HeapAlloc, HeapFree};
use winapi::um::memoryapi::{ReadProcessMemory, VirtualQueryEx};
use winapi::um::minwinbase::LMEM_FIXED;
use winapi::um::minwinbase::{LMEM_FIXED, LMEM_ZEROINIT};
use winapi::um::processthreadsapi::{
GetProcessTimes, GetSystemTimes, OpenProcess, OpenProcessToken, ProcessIdToSessionId,
};
Expand Down Expand Up @@ -293,7 +293,10 @@ unsafe fn get_process_name(pid: Pid) -> Option<String> {
};

for i in 0.. {
info.ImageName.Buffer = LocalAlloc(LMEM_FIXED, info.ImageName.MaximumLength as _) as *mut _;
info.ImageName.Buffer = LocalAlloc(
LMEM_FIXED | LMEM_ZEROINIT,
info.ImageName.MaximumLength as _,
) as *mut _;
if info.ImageName.Buffer.is_null() {
sysinfo_debug!("Couldn't get process infos: LocalAlloc failed");
return None;
Expand Down Expand Up @@ -336,10 +339,17 @@ unsafe fn get_process_name(pid: Pid) -> Option<String> {
return None;
}

let s = std::slice::from_raw_parts(info.ImageName.Buffer, info.ImageName.Length as _);
let name = String::from_utf16_lossy(s);
let s = std::slice::from_raw_parts(
info.ImageName.Buffer,
// The length is in bytes, not the length of string
info.ImageName.Length as usize / std::mem::size_of::<u16>(),
);
let os_str = OsString::from_wide(s);
let name = Path::new(&os_str)
.file_name()
.map(|s| s.to_string_lossy().to_string());
LocalFree(info.ImageName.Buffer as _);
Some(name)
name
}

unsafe fn get_exe(process_handler: &HandleWrapper) -> PathBuf {
Expand Down
Loading