mem_rs/process/process_name.rs
1// This file is part of the mem-rs distribution (https://github.com/FrankvdStam/mem-rs).
2// Copyright (c) 2022 Frank van der Stam.
3// https://github.com/FrankvdStam/mem-rs/blob/main/LICENSE
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, version 3.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17use std::mem::size_of;
18use windows::Win32::Foundation::{CloseHandle, MAX_PATH};
19use windows::Win32::System::ProcessStatus::{K32EnumProcesses, K32GetModuleFileNameExW};
20use windows::Win32::System::Threading::{GetCurrentProcess, OpenProcess, PROCESS_QUERY_INFORMATION, PROCESS_VM_OPERATION, PROCESS_VM_READ, PROCESS_VM_WRITE};
21use crate::helpers::{get_file_name_from_string, w32str_to_string};
22use crate::process::Process;
23
24impl Process
25{
26 /// Returns the current process name, in which this very code is running.
27 /// Does NOT return the name of the target attachment process.
28 ///
29 /// # Examples
30 ///
31 /// ```
32 /// use mem_rs::prelude::*;
33 ///
34 /// let name = Process::get_current_process_name()?;
35 /// ```
36 pub fn get_current_process_name() -> Result<String, ()>
37 {
38 unsafe
39 {
40 let handle = GetCurrentProcess();
41 let mut mod_name = [0; MAX_PATH as usize];
42 if K32GetModuleFileNameExW(Some(handle), None, &mut mod_name) != 0
43 {
44 let file_path = w32str_to_string(&mod_name.to_vec());
45 let file_name = get_file_name_from_string(&file_path);
46 return Ok(file_name);
47 }
48 Err(())
49 }
50 }
51
52 /// Returns all the processes that are currently running
53 ///
54 /// # Examples
55 ///
56 /// ```
57 /// use mem_rs::prelude::*;
58 ///
59 /// let names = Process::get_running_process_names();
60 /// ```
61 pub fn get_running_process_names() -> Vec<String>
62 {
63 unsafe
64 {
65 let mut process_names = Vec::new();
66 let mut process_ids = [0u32; 2048];
67 let mut bytes_needed = 0u32;
68 let _ = K32EnumProcesses(process_ids.as_mut_ptr(), (process_ids.len() * size_of::<u32>()) as u32, &mut bytes_needed);
69 let count = bytes_needed as usize / std::mem::size_of::<u32>();
70
71 for i in 0..count
72 {
73 let pid = process_ids[i];
74
75 let mut mod_name = [0; MAX_PATH as usize];
76
77 if let Ok(handle) = OpenProcess(
78 PROCESS_QUERY_INFORMATION
79 | PROCESS_VM_READ
80 | PROCESS_VM_WRITE
81 | PROCESS_VM_OPERATION,
82 false,
83 pid,
84 )
85 {
86 if K32GetModuleFileNameExW(Some(handle), None, &mut mod_name) != 0
87 {
88 let file_path = w32str_to_string(&mod_name.to_vec());
89 let file_name = get_file_name_from_string(&file_path);
90 process_names.push(file_name);
91 }
92 let _ = CloseHandle(handle);
93 }
94 }
95 return process_names;
96 }
97 }
98}