pub struct Process { /* private fields */ }Expand description
A handle to a Windows process.
Implementations§
Source§impl Process
impl Process
Sourcepub fn inject_dll(&self, dll_path: &Path) -> Result<()>
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"))?;Sourcepub fn is_dll_loaded(&self, dll_name: &str) -> Result<bool>
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
impl Process
Sourcepub fn list() -> Result<Vec<ProcessInfo>>
pub fn list() -> Result<Vec<ProcessInfo>>
List all processes in the system.
Sourcepub fn list_with_buffer(out_processes: &mut Vec<ProcessInfo>) -> Result<usize>
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.
Sourcepub fn list_with_filter<F>(
out_processes: &mut Vec<ProcessInfo>,
filter: F,
) -> Result<usize>
pub fn list_with_filter<F>( out_processes: &mut Vec<ProcessInfo>, filter: F, ) -> Result<usize>
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.
Sourcepub fn children_with_buffer(
&self,
buffer: &mut Vec<ProcessInfo>,
) -> Result<usize>
pub fn children_with_buffer( &self, buffer: &mut Vec<ProcessInfo>, ) -> Result<usize>
Get all immediate child processes using a reusable buffer.
Source§impl Process
impl Process
Sourcepub fn memory_info(&self) -> Result<MemoryInfo>
pub fn memory_info(&self) -> Result<MemoryInfo>
Get memory usage information for this process.
Source§impl Process
impl Process
Sourcepub fn cpu_times(&self) -> Result<ProcessCpuTimes>
pub fn cpu_times(&self) -> Result<ProcessCpuTimes>
Get cumulative process CPU time counters.
Returned values are cumulative since process start in 100ns units.
Sourcepub fn memory_metrics(&self) -> Result<ProcessMemoryMetrics>
pub fn memory_metrics(&self) -> Result<ProcessMemoryMetrics>
Get extended memory metrics for this process.
Sourcepub fn metrics(&self) -> Result<ProcessMetrics>
pub fn metrics(&self) -> Result<ProcessMetrics>
Get point-in-time CPU and memory metrics for this process.
Source§impl Process
impl Process
Sourcepub fn modules(&self) -> Result<Vec<ModuleInfo>>
pub fn modules(&self) -> Result<Vec<ModuleInfo>>
Enumerate all modules (DLLs) loaded in this process.
Sourcepub fn modules_with_buffer(
&self,
out_modules: &mut Vec<ModuleInfo>,
work_buffer: &mut Vec<u8>,
) -> Result<usize>
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 resultswork_buffer: Work buffer for module handles and string data (reused across calls)
Sourcepub fn modules_with_filter<F>(
&self,
out_modules: &mut Vec<ModuleInfo>,
work_buffer: &mut Vec<u8>,
filter: F,
) -> Result<usize>
pub fn modules_with_filter<F>( &self, out_modules: &mut Vec<ModuleInfo>, work_buffer: &mut Vec<u8>, filter: F, ) -> Result<usize>
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 resultswork_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
impl Process
Sourcepub fn command_line(&self) -> Result<String>
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).
Sourcepub fn command_line_with_buffer(
&self,
out_buffer: &mut Vec<u8>,
) -> Result<String>
pub fn command_line_with_buffer( &self, out_buffer: &mut Vec<u8>, ) -> Result<String>
Get the command line using a reusable output buffer.
Sourcepub fn environment(&self) -> Result<HashMap<String, String>>
pub fn environment(&self) -> Result<HashMap<String, String>>
Get the environment variables of the process.
Sourcepub fn environment_with_buffer(
&self,
out_buffer: &mut Vec<u8>,
) -> Result<HashMap<String, String>>
pub fn environment_with_buffer( &self, out_buffer: &mut Vec<u8>, ) -> Result<HashMap<String, String>>
Get the environment variables using a reusable output buffer.
Sourcepub fn parameters(&self) -> Result<ProcessParameters>
pub fn parameters(&self) -> Result<ProcessParameters>
Get all process parameters (command line, current directory, image path).
Sourcepub fn parameters_with_buffer(
&self,
out_buffer: &mut Vec<u8>,
) -> Result<ProcessParameters>
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
impl Process
Sourcepub fn open(pid: ProcessId) -> Result<Self>
pub fn open(pid: ProcessId) -> Result<Self>
Open a process with default access (query information).
Sourcepub fn open_with_access(pid: ProcessId, access: ProcessAccess) -> Result<Self>
pub fn open_with_access(pid: ProcessId, access: ProcessAccess) -> Result<Self>
Open a process with specific access rights.
Sourcepub fn current() -> Self
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.
Sourcepub fn with_access(&self, access: ProcessAccess) -> Result<Self>
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)?;Sourcepub fn name_with_buffer(&self, out_buffer: &mut Vec<u8>) -> Result<String>
pub fn name_with_buffer(&self, out_buffer: &mut Vec<u8>) -> Result<String>
Get the process name using a reusable output buffer.
Sourcepub fn path_with_buffer(&self, out_buffer: &mut Vec<u8>) -> Result<PathBuf>
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.
Sourcepub fn is_running(&self) -> Result<bool>
pub fn is_running(&self) -> Result<bool>
Check if the process is still running.
Sourcepub fn exit_code(&self) -> Result<Option<u32>>
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.
Sourcepub fn wait_for_exit(&self) -> Result<u32>
pub fn wait_for_exit(&self) -> Result<u32>
Wait until this process exits and return its final exit code.
Sourcepub fn wait_for_exit_timeout(&self, timeout: Duration) -> Result<Option<u32>>
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.
Sourcepub fn as_wait(&self) -> Wait
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.
Sourcepub fn terminate(&self, exit_code: u32) -> Result<()>
pub fn terminate(&self, exit_code: u32) -> Result<()>
Terminate the process with a specific exit code.
Sourcepub fn kill_by_id(pid: ProcessId) -> Result<()>
pub fn kill_by_id(pid: ProcessId) -> Result<()>
Kill a process by ID (convenience method).
Sourcepub unsafe fn as_raw_handle(&self) -> HANDLE
pub unsafe fn as_raw_handle(&self) -> HANDLE
Source§impl Process
impl Process
Sourcepub fn threads(&self) -> Result<Vec<ThreadInfo>>
pub fn threads(&self) -> Result<Vec<ThreadInfo>>
Enumerate all threads in this process.
Sourcepub fn threads_with_buffer(
&self,
out_threads: &mut Vec<ThreadInfo>,
) -> Result<usize>
pub fn threads_with_buffer( &self, out_threads: &mut Vec<ThreadInfo>, ) -> Result<usize>
Enumerate threads using a reusable output buffer.
Sourcepub fn threads_with_filter<F>(
&self,
out_threads: &mut Vec<ThreadInfo>,
filter: F,
) -> Result<usize>
pub fn threads_with_filter<F>( &self, out_threads: &mut Vec<ThreadInfo>, filter: F, ) -> Result<usize>
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
impl Process
Sourcepub fn kill_tree(&self) -> Result<()>
pub fn kill_tree(&self) -> Result<()>
Kill this process and all its descendants.
This will recursively kill all child processes first, then the parent.
Sourcepub fn kill_tree_by_id(pid: ProcessId) -> Result<()>
pub fn kill_tree_by_id(pid: ProcessId) -> Result<()>
Kill a process and all its descendants by process ID.
Sourcepub fn kill_tree_by_id_with_buffer(
pid: ProcessId,
out_processes: &mut Vec<ProcessInfo>,
) -> Result<()>
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.
Sourcepub fn kill_tree_from_root(pid: ProcessId) -> Result<()>
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.
Sourcepub fn kill_tree_from_root_with_buffer(
pid: ProcessId,
out_processes: &mut Vec<ProcessInfo>,
) -> Result<()>
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.