Skip to content

Commit

Permalink
Merge pull request #807 from hamflx/master
Browse files Browse the repository at this point in the history
Fix the problem that Process::exe() sometimes returns an empty PathBuf under windows
  • Loading branch information
GuillaumeGomez committed Aug 6, 2022
2 parents bfc49ac + e9bec81 commit e5a8510
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
14 changes: 4 additions & 10 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ unsafe fn get_h_mod(process_handler: &HandleWrapper, h_mod: &mut *mut c_void) ->
) != 0
}

unsafe fn get_exe(process_handler: &HandleWrapper, h_mod: *mut c_void) -> PathBuf {
unsafe fn get_exe(process_handler: &HandleWrapper) -> PathBuf {
let mut exe_buf = [0u16; MAX_PATH + 1];
GetModuleFileNameExW(
**process_handler,
h_mod as _,
std::ptr::null_mut(),
exe_buf.as_mut_ptr(),
MAX_PATH as DWORD + 1,
);
Expand Down Expand Up @@ -318,7 +318,7 @@ impl Process {
String::new()
};

let exe = get_exe(&process_handler, h_mod);
let exe = get_exe(&process_handler);
let mut root = exe.clone();
root.pop();
let (cmd, environ, cwd) = match get_process_params(&process_handler) {
Expand Down Expand Up @@ -372,14 +372,8 @@ impl Process {
refresh_kind: ProcessRefreshKind,
) -> Process {
if let Some(handle) = get_process_handler(pid) {
let mut h_mod = null_mut();

unsafe {
let exe = if get_h_mod(&handle, &mut h_mod) {
get_exe(&handle, h_mod)
} else {
PathBuf::new()
};
let exe = get_exe(&handle);
let mut root = exe.clone();
root.pop();
let (cmd, environ, cwd) = match get_process_params(&handle) {
Expand Down
1 change: 0 additions & 1 deletion tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ fn test_process() {
return;
}
assert!(!s.processes().is_empty());
#[cfg(not(windows))]
assert!(s
.processes()
.values()
Expand Down

0 comments on commit e5a8510

Please sign in to comment.