Skip to main content

doe/
process.rs

1///
2#[cfg(feature = "process")]
3pub mod process {
4    /// A struct representing information about a process, including its PID and name.
5    #[derive(Debug, Clone, Default)]
6    pub struct ProcessInfo {
7        /// The process ID.
8        pub pid: u64,
9        /// The process name.
10        pub name: String,
11    }
12
13    /// todo: implement
14    #[allow(warnings)]
15    #[cfg(target_os = "macos")]
16    pub fn get_all_process_name_and_pid() -> Vec<ProcessInfo> {
17        use cocoa::appkit;
18        todo!()
19    }
20    /// Retrieves information about all running processes on a Windows system.
21    ///
22    /// # Examples
23    ///
24    /// ```
25    /// let processes: Vec<ProcessInfo> = get_all_process_name_and_pid();
26    /// for process in processes {
27    ///     println!("Process ID: {}, Name: {}", process.pid, process.name);
28    /// }
29    /// ```
30    #[cfg(target_os = "windows")]
31    pub fn get_all_process_name_and_pid() -> Vec<ProcessInfo> {
32        use std::mem;
33        use winapi::shared::minwindef::TRUE;
34        use winapi::um::tlhelp32::{
35            Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
36        };
37
38        let mut res = vec![];
39        unsafe {
40            let snapshot = winapi::um::tlhelp32::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
41            if snapshot == winapi::um::handleapi::INVALID_HANDLE_VALUE {
42                panic!("Failed to create snapshot");
43            }
44
45            let mut process_entry: PROCESSENTRY32W = mem::zeroed();
46            process_entry.dwSize = mem::size_of::<PROCESSENTRY32W>() as u32;
47
48            if Process32FirstW(snapshot, &mut process_entry) == TRUE {
49                loop {
50                    res.push(ProcessInfo {
51                        pid: process_entry.th32ProcessID as u64,
52                        name: String::from_utf16_lossy(&process_entry.szExeFile)
53                            .trim()
54                            .to_string()
55                            .replace("\0", ""),
56                    });
57                    // println!("Process ID: {}, Name: {}", process_entry.th32ProcessID, String::from_utf16_lossy(&process_entry.szExeFile));
58                    if Process32NextW(snapshot, &mut process_entry) != TRUE {
59                        break;
60                    }
61                }
62            }
63            winapi::um::handleapi::CloseHandle(snapshot);
64        }
65        res
66    }
67    #[cfg(target_os = "windows")]
68    /// Attempts to terminate a process with the specified PID on a Windows system.
69    ///
70    /// # Arguments
71    ///
72    /// * `pid` - The process ID of the process to be terminated.
73    ///
74    /// # Examples
75    ///
76    /// ```
77    /// let pid_to_kill = 12345;
78    /// kill_process_by_pid(pid_to_kill);
79    /// ```
80    pub fn kill_process_by_pid(pid: u64) {
81        use winapi::shared::minwindef::{DWORD, FALSE};
82        use winapi::um::handleapi::CloseHandle;
83        use winapi::um::processthreadsapi::{OpenProcess, TerminateProcess};
84        use winapi::um::winnt::PROCESS_TERMINATE;
85        unsafe {
86            let process_handle = OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD);
87            if process_handle.is_null() {
88                eprintln!("Failed to open process with PID: {}", pid);
89                return;
90            }
91            if TerminateProcess(process_handle, 0) == FALSE {
92                eprintln!("Failed to terminate process with PID: {}", pid);
93            }
94            CloseHandle(process_handle);
95        }
96    }
97}
98
99#[cfg(feature = "process")]
100pub use process::*;