From 639c93b4c80857d9090ec60cb49ed1f898163945 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Wed, 18 Jan 2023 01:43:08 -0500 Subject: other: switch to windows-rs for process killing (#985) --- src/app/process_killer.rs | 55 +++++++++++++++++++++++------------------------ src/utils/error.rs | 2 +- 2 files changed, 28 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs index f3cea471..d93c6f07 100644 --- a/src/app/process_killer.rs +++ b/src/app/process_killer.rs @@ -1,41 +1,51 @@ -// Copied from SO: https://stackoverflow.com/a/55231715 -#[cfg(target_os = "windows")] -use winapi::{ - shared::{minwindef::DWORD, ntdef::HANDLE}, - um::{ - processthreadsapi::{OpenProcess, TerminateProcess}, - winnt::{PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE}, - }, -}; +//! This file is meant to house (OS specific) implementations on how to kill processes. -/// This file is meant to house (OS specific) implementations on how to kill processes. #[cfg(target_family = "unix")] use crate::utils::error::BottomError; use crate::Pid; +#[cfg(target_os = "windows")] +use windows::Win32::{ + Foundation::HANDLE, + System::Threading::{ + OpenProcess, TerminateProcess, PROCESS_QUERY_INFORMATION, PROCESS_TERMINATE, + }, +}; + +/// Based from [this SO answer](https://stackoverflow.com/a/55231715). #[cfg(target_os = "windows")] struct Process(HANDLE); #[cfg(target_os = "windows")] impl Process { - fn open(pid: DWORD) -> Result { - let pc = unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, 0, pid) }; - if pc.is_null() { - return Err("OpenProcess".to_string()); + fn open(pid: u32) -> Result { + match unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, false, pid) } { + Ok(process) => Ok(Process(process)), + Err(_) => Err("process may have already been terminated.".to_string()), } - Ok(Process(pc)) } fn kill(self) -> Result<(), String> { let result = unsafe { TerminateProcess(self.0, 1) }; - if result == 0 { - return Err("Failed to kill process".to_string()); + if result.0 == 0 { + return Err("process may have already been terminated.".to_string()); } Ok(()) } } +/// Kills a process, given a PID, for windows. +#[cfg(target_os = "windows")] +pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> { + { + let process = Process::open(pid as u32)?; + process.kill()?; + } + + Ok(()) +} + /// Kills a process, given a PID, for unix. #[cfg(target_family = "unix")] pub fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::Result<()> { @@ -65,14 +75,3 @@ pub fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::R Ok(()) } - -/// Kills a process, given a PID, for windows. -#[cfg(target_os = "windows")] -pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> { - { - let process = Process::open(pid as DWORD)?; - process.kill()?; - } - - Ok(()) -} diff --git a/src/utils/error.rs b/src/utils/error.rs index 904f1392..1473a9ae 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -21,7 +21,7 @@ pub enum BottomError { #[error("Error caused by Crossterm, {0}")] CrosstermError(String), /// An error to represent generic errors. - #[error("Generic error, {0}")] + #[error("Error, {0}")] GenericError(String), /// An error to represent errors with fern. #[error("Fern error, {0}")] -- cgit v1.2.3