Skip to content
Open
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
32 changes: 21 additions & 11 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn kill_process_if(
process: &RunningProcess,
path: &Path,
) -> Result<(), Box<dyn error::Error>> {
use windows_sys::Win32::Foundation::{CloseHandle, MAX_PATH};
use windows_sys::Win32::Foundation::{CloseHandle, MAX_PATH, ERROR_ACCESS_DENIED, GetLastError};
use windows_sys::Win32::System::ProcessStatus::K32GetModuleFileNameExW;
use windows_sys::Win32::System::Threading::{
OpenProcess, TerminateProcess, PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE,
Expand All @@ -104,16 +104,26 @@ fn kill_process_if(
process.id,
);

if ptr::eq(handle as *mut c_void, ptr::null()) {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Failed to open process: {}",
util::get_last_error_message()?
),
)
.into());
}
if ptr::eq(handle as *mut c_void, ptr::null_mut()) {
let error_code = GetLastError();

// Check for insufficient permission
if error_code == ERROR_ACCESS_DENIED {
info!(
log,
"Insufficient permissions to open process: {}", process.id
);
return Ok(()); // Ignore the error and return Ok
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Failed to open process: {}",
util::get_last_error_message()?
),
).into());
}
}

let mut raw_path = [0u16; MAX_PATH as usize];
let len = K32GetModuleFileNameExW(handle, mem::zeroed(), raw_path.as_mut_ptr(), MAX_PATH)
Expand Down