reorganize process related sources into a process/ directory
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use windows::Win32::Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE};
|
||||
use windows::Win32::System::Threading::{
|
||||
OpenProcess, PROCESS_ALL_ACCESS, PROCESS_VM_READ, PROCESS_VM_WRITE,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Handle(pub HANDLE);
|
||||
|
||||
impl Deref for Handle {
|
||||
type Target = HANDLE;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Handle {
|
||||
fn drop(&mut self) {
|
||||
if self.0 != INVALID_HANDLE_VALUE {
|
||||
unsafe {
|
||||
if let Err(why) = CloseHandle(**self) {
|
||||
eprintln!("couldn't close handle: {why}");
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Handle {
|
||||
pub fn from_pid(pid: u32) -> Result<Self, crate::Error> {
|
||||
let handle = unsafe {
|
||||
OpenProcess(PROCESS_ALL_ACCESS, false, pid)
|
||||
.or_else(|_| OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid))
|
||||
.map_err(|why| {
|
||||
crate::Error::HandleError(format!(
|
||||
"couldn't open process with needed access: {why}",
|
||||
))
|
||||
})?
|
||||
};
|
||||
if handle == INVALID_HANDLE_VALUE {
|
||||
return Err(crate::Error::HandleError(
|
||||
"couldn't get a valid handle".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(Self(handle))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
mod handle;
|
||||
mod process;
|
||||
mod snapshot;
|
||||
|
||||
pub use process::{Identifier, Process};
|
||||
@@ -0,0 +1,50 @@
|
||||
use super::{handle::Handle, snapshot};
|
||||
|
||||
use derive_more::derive::Display;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Process {
|
||||
name: String,
|
||||
id: u32,
|
||||
base_address: usize,
|
||||
handle: Handle,
|
||||
}
|
||||
|
||||
#[derive(Display)]
|
||||
pub enum Identifier {
|
||||
Id(u32),
|
||||
Name(String),
|
||||
}
|
||||
|
||||
impl Process {
|
||||
pub fn from(identifier: &Identifier) -> Result<Self, crate::Error> {
|
||||
let snapshot = match identifier {
|
||||
Identifier::Id(pid) => snapshot::Process::get_processes()?
|
||||
.into_iter()
|
||||
.find(|snapshot| snapshot.id == *pid),
|
||||
Identifier::Name(ref name) => snapshot::Process::get_processes()?
|
||||
.into_iter()
|
||||
.find(|snapshot| snapshot.name == *name),
|
||||
};
|
||||
let Some(snapshot) = snapshot else {
|
||||
return Err(crate::Error::ProcessError(format!(
|
||||
"couldn't find a process with identifier `{identifier}`",
|
||||
)));
|
||||
};
|
||||
let process = Self {
|
||||
name: snapshot.name,
|
||||
id: snapshot.id,
|
||||
base_address: 0,
|
||||
handle: Handle::from_pid(snapshot.id)?,
|
||||
};
|
||||
Ok(process)
|
||||
}
|
||||
|
||||
pub fn with_pid(pid: u32) -> Result<Self, crate::Error> {
|
||||
Self::from(&Identifier::Id(pid))
|
||||
}
|
||||
|
||||
pub fn with_name(name: &str) -> Result<Self, crate::Error> {
|
||||
Self::from(&Identifier::Name(name.to_string()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
use windows::Win32::System::Diagnostics::ToolHelp::{
|
||||
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Process {
|
||||
pub id: u32,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Process {
|
||||
pub fn get_processes() -> Result<Vec<Self>, crate::Error> {
|
||||
let snapshot = unsafe {
|
||||
match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) {
|
||||
Ok(snapshot) => snapshot,
|
||||
Err(why) => {
|
||||
return Err(crate::Error::SnapshotError(format!(
|
||||
"couldn't create snapshot: {why}"
|
||||
)))
|
||||
}
|
||||
}
|
||||
};
|
||||
let mut process_entry_32_w = PROCESSENTRY32W {
|
||||
dwSize: u32::try_from(size_of::<PROCESSENTRY32W>())?,
|
||||
..Default::default()
|
||||
};
|
||||
unsafe {
|
||||
match Process32FirstW(snapshot, &mut process_entry_32_w) {
|
||||
Ok(()) => {}
|
||||
Err(why) => {
|
||||
return Err(crate::Error::SnapshotError(format!(
|
||||
"couldn't get first process from snapshot: {why}"
|
||||
)))
|
||||
}
|
||||
};
|
||||
}
|
||||
let mut processes = Vec::new();
|
||||
loop {
|
||||
let name = String::from_utf16_lossy(&process_entry_32_w.szExeFile)
|
||||
.trim_end_matches('\0')
|
||||
.to_string();
|
||||
let process = Self {
|
||||
id: process_entry_32_w.th32ProcessID,
|
||||
name,
|
||||
};
|
||||
processes.push(process);
|
||||
unsafe {
|
||||
if Process32NextW(snapshot, &mut process_entry_32_w).is_err() {
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
Ok(processes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user