Skip to main content

Process

Struct Process 

Source
pub struct Process { /* private fields */ }
Expand description

A handle to a Windows process.

Implementations§

Source§

impl Process

Source

pub fn inject_dll(&self, dll_path: &Path) -> Result<()>

Inject a DLL into this process.

Opens the process with the necessary rights, allocates memory for the DLL path, and creates a remote thread running LoadLibraryA in the target process.

Returns Err(ProcessError::AlreadyInjected) if the DLL filename is already loaded by the target process.

§Remarks
  • The DLL path must be valid UTF-8 and contain no embedded null characters.
  • The calling process must have the privileges required to open the target process with PROCESS_CREATE_THREAD | PROCESS_VM_WRITE | PROCESS_VM_OPERATION.
  • Injecting a 32-bit DLL into a 64-bit process (or vice versa) will silently fail; ensure bitness matches.
§Example
use std::path::Path;
use windows_erg::process::{Process, ProcessId};

let process = Process::open(ProcessId::new(1234))?;
process.inject_dll(Path::new("C:\\path\\to\\my.dll"))?;
Source

pub fn is_dll_loaded(&self, dll_name: &str) -> Result<bool>

Check whether a DLL with the given name (case-insensitive, filename only) is currently loaded in this process.

§Example
use windows_erg::process::{Process, ProcessId};

let process = Process::open(ProcessId::new(1234))?;
if process.is_dll_loaded("kernel32.dll")? {
    println!("kernel32 is loaded");
}
Source§

impl Process

Source

pub fn list() -> Result<Vec<ProcessInfo>>

List all processes in the system.

Source

pub fn list_with_buffer(out_processes: &mut Vec<ProcessInfo>) -> Result<usize>

List all processes using a reusable output buffer.

Returns the number of processes found and added to the buffer.

Source

pub fn list_with_filter<F>( out_processes: &mut Vec<ProcessInfo>, filter: F, ) -> Result<usize>
where F: Fn(&ProcessInfo) -> bool,

List all processes matching a filter using a reusable output buffer.

The filter function is called for each process. Only processes where the filter returns true are added to the buffer. This is more efficient than listing all and filtering afterwards as it avoids unnecessary buffer operations.

Returns the number of matching processes found and added to the buffer.

Source

pub fn parent_id(&self) -> Result<Option<ProcessId>>

Get the parent process ID.

Source

pub fn children(&self) -> Result<Vec<ProcessId>>

Get all immediate child processes.

Source

pub fn children_with_buffer( &self, buffer: &mut Vec<ProcessInfo>, ) -> Result<usize>

Get all immediate child processes using a reusable buffer.

Source§

impl Process

Source

pub fn memory_info(&self) -> Result<MemoryInfo>

Get memory usage information for this process.

Source§

impl Process

Source

pub fn cpu_times(&self) -> Result<ProcessCpuTimes>

Get cumulative process CPU time counters.

Returned values are cumulative since process start in 100ns units.

Source

pub fn memory_metrics(&self) -> Result<ProcessMemoryMetrics>

Get extended memory metrics for this process.

Source

pub fn metrics(&self) -> Result<ProcessMetrics>

Get point-in-time CPU and memory metrics for this process.

Source

pub fn cpu_usage(&self, interval: Duration) -> Result<f64>

Calculate process CPU usage percentage over a sampling interval.

The returned value is normalized to whole-machine CPU usage scale [0.0, 100.0].

Source§

impl Process

Source

pub fn modules(&self) -> Result<Vec<ModuleInfo>>

Enumerate all modules (DLLs) loaded in this process.

Source

pub fn modules_with_buffer( &self, out_modules: &mut Vec<ModuleInfo>, work_buffer: &mut Vec<u8>, ) -> Result<usize>

Enumerate modules using reusable output and work buffers.

§Arguments
  • out_modules: Output buffer to store ModuleInfo results
  • work_buffer: Work buffer for module handles and string data (reused across calls)
Source

pub fn modules_with_filter<F>( &self, out_modules: &mut Vec<ModuleInfo>, work_buffer: &mut Vec<u8>, filter: F, ) -> Result<usize>
where F: Fn(&ModuleInfo) -> bool,

Enumerate modules matching a filter using reusable output and work buffers.

The filter function is called for each module. Only modules where the filter returns true are added to the output buffer. This is more efficient than enumerating all and filtering afterwards.

§Arguments
  • out_modules: Output buffer to store ModuleInfo results
  • work_buffer: Work buffer for module handles and string data (should be reused)
  • filter: Predicate function to filter modules

Returns the number of matching modules found and added to the buffer.

Source§

impl Process

Source

pub fn command_line(&self) -> Result<String>

Get the command line of the process.

This reads the command line from the Process Environment Block (PEB).

Source

pub fn command_line_with_buffer( &self, out_buffer: &mut Vec<u8>, ) -> Result<String>

Get the command line using a reusable output buffer.

Source

pub fn environment(&self) -> Result<HashMap<String, String>>

Get the environment variables of the process.

Source

pub fn environment_with_buffer( &self, out_buffer: &mut Vec<u8>, ) -> Result<HashMap<String, String>>

Get the environment variables using a reusable output buffer.

Source

pub fn parameters(&self) -> Result<ProcessParameters>

Get all process parameters (command line, current directory, image path).

Source

pub fn parameters_with_buffer( &self, out_buffer: &mut Vec<u8>, ) -> Result<ProcessParameters>

Get all process parameters using a reusable output buffer.

Source§

impl Process

Source

pub fn open(pid: ProcessId) -> Result<Self>

Open a process with default access (query information).

Source

pub fn open_with_access(pid: ProcessId, access: ProcessAccess) -> Result<Self>

Open a process with specific access rights.

Source

pub fn current() -> Self

Get a pseudo-handle to the current process.

This handle does not need to be closed and is valid for the lifetime of the process.

Source

pub fn with_access(&self, access: ProcessAccess) -> Result<Self>

Open the same process with additional access rights.

This is useful when you have a process handle but need higher privileges (e.g., to read/write memory or terminate the process).

§Example
let process = Process::open(pid)?;
// Need to read memory - upgrade to VmRead access
let process_with_vm_read = process.with_access(ProcessAccess::VmRead)?;
Source

pub fn id(&self) -> ProcessId

Get the process ID.

Source

pub fn name(&self) -> Result<String>

Get the process name (executable file name without path).

Source

pub fn name_with_buffer(&self, out_buffer: &mut Vec<u8>) -> Result<String>

Get the process name using a reusable output buffer.

Source

pub fn path(&self) -> Result<PathBuf>

Get the full path to the process executable.

Source

pub fn path_with_buffer(&self, out_buffer: &mut Vec<u8>) -> Result<PathBuf>

Get the full path to the process executable using a reusable output buffer.

Source

pub fn is_running(&self) -> Result<bool>

Check if the process is still running.

Source

pub fn exit_code(&self) -> Result<Option<u32>>

Get the exit code of the process, if it has exited.

Returns None if the process is still running.

Source

pub fn wait_for_exit(&self) -> Result<u32>

Wait until this process exits and return its final exit code.

Source

pub fn wait_for_exit_timeout(&self, timeout: Duration) -> Result<Option<u32>>

Wait until this process exits or timeout elapses.

Returns Ok(Some(code)) when the process exits, Ok(None) on timeout.

Source

pub fn as_wait(&self) -> Wait

Borrow this process handle as a Wait object.

The returned wait object does not own the process handle and will not close it on drop.

Source

pub fn kill(&self) -> Result<()>

Terminate the process with exit code 1.

Source

pub fn terminate(&self, exit_code: u32) -> Result<()>

Terminate the process with a specific exit code.

Source

pub fn kill_by_id(pid: ProcessId) -> Result<()>

Kill a process by ID (convenience method).

Source

pub unsafe fn as_raw_handle(&self) -> HANDLE

Get the raw Windows handle.

§Safety

The handle must not outlive the Process instance.

Source§

impl Process

Source

pub fn threads(&self) -> Result<Vec<ThreadInfo>>

Enumerate all threads in this process.

Source

pub fn threads_with_buffer( &self, out_threads: &mut Vec<ThreadInfo>, ) -> Result<usize>

Enumerate threads using a reusable output buffer.

Source

pub fn threads_with_filter<F>( &self, out_threads: &mut Vec<ThreadInfo>, filter: F, ) -> Result<usize>
where F: Fn(&ThreadInfo) -> bool,

Enumerate threads matching a filter using a reusable output buffer.

The filter function is called for each thread. Only threads where the filter returns true are added to the output buffer. This is more efficient than enumerating all and filtering afterwards.

Returns the number of matching threads found and added to the buffer.

Source§

impl Process

Source

pub fn kill_tree(&self) -> Result<()>

Kill this process and all its descendants.

This will recursively kill all child processes first, then the parent.

Source

pub fn kill_tree_by_id(pid: ProcessId) -> Result<()>

Kill a process and all its descendants by process ID.

Source

pub fn kill_tree_by_id_with_buffer( pid: ProcessId, out_processes: &mut Vec<ProcessInfo>, ) -> Result<()>

Kill a process tree using a reusable output buffer.

Source

pub fn kill_tree_from_root(pid: ProcessId) -> Result<()>

Find the root ancestor of a process and kill the entire tree.

This walks up the parent chain to find the topmost process, then kills that entire tree.

Source

pub fn kill_tree_from_root_with_buffer( pid: ProcessId, out_processes: &mut Vec<ProcessInfo>, ) -> Result<()>

Kill tree from root using a reusable output buffer.

Trait Implementations§

Source§

impl Drop for Process

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Process

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.