faithe/internal/
process.rs

1#![allow(missing_docs)]
2
3use crate::{
4    types::{ListEntry, UnicodeString},
5    FaitheError,
6};
7use windows::{Win32::{
8    Foundation::{HANDLE, HWND},
9    System::{Console, Threading},
10    UI::WindowsAndMessaging::{MessageBoxW, MESSAGEBOX_STYLE},
11}, core::PCWSTR};
12
13/// Returns a handle to the current process.
14pub fn get_current_process() -> HANDLE {
15    HANDLE(usize::MAX as isize)
16}
17
18/// Returns the id of the current process.
19pub fn get_current_process_id() -> u32 {
20    unsafe { Threading::GetCurrentProcessId() }
21}
22
23/// Allocates console windows ig.
24pub fn alloc_console() -> crate::Result<()> {
25    if unsafe { Console::AllocConsole().0 == 0 } {
26        Err(FaitheError::last_error())
27    } else {
28        Ok(())
29    }
30}
31
32/// Frees console.
33pub fn free_console() -> crate::Result<()> {
34    if unsafe { Console::FreeConsole().0 == 0 } {
35        Err(FaitheError::last_error())
36    } else {
37        Ok(())
38    }
39}
40
41/// Creates new message box.
42pub fn message_box(
43    hwnd: Option<HWND>,
44    text: impl AsRef<str>,
45    caption: impl AsRef<str>,
46    style: MESSAGEBOX_STYLE,
47) -> crate::Result<()> {
48    if unsafe {
49        MessageBoxW(
50            hwnd,
51            PCWSTR(
52                format!("{}\x00", text.as_ref())
53                    .encode_utf16()
54                    .collect::<Vec<_>>()
55                    .as_mut_ptr(),
56            ),
57            PCWSTR(
58                format!("{}\x00", caption.as_ref())
59                    .encode_utf16()
60                    .collect::<Vec<_>>()
61                    .as_mut_ptr(),
62            ),
63            style,
64        )
65        .0 == 0
66    } {
67        Err(FaitheError::last_error())
68    } else {
69        Ok(())
70    }
71}
72
73/// Process Environmental Block.
74#[repr(C)]
75pub struct Peb {
76    _pad0x2: [u8; 0x2],
77    /// If process is being debugged.
78    pub being_debugged: bool,
79    pad0x10: [u8; 0xD],
80    /// Base address of loaded image.
81    pub image_base_address: *const (),
82    /// Ldr data
83    pub ldr_data: &'static PebLdrData,
84}
85
86#[repr(C)]
87pub struct LdrDataTableEntry {
88    _pad0x10: [u8; 0x10],
89    pub in_memory_order_links: ListEntry,
90    _pad0x30: [u8; 0x10],
91    pub dll_base: *mut (),
92    pub entry_point: *mut (),
93    pub image_size: u32,
94    pub full_dll_name: UnicodeString,
95    pub base_dll_name: UnicodeString,
96}
97
98#[repr(C)]
99pub struct PebLdrData {
100    pub len: u32,
101    _pad0x20: [u8; 0x1C],
102    pub in_memory_order_links: ListEntry,
103}
104
105/// Returns an address of PEB(Process Environmental Block).
106#[cfg(feature = "nightly")]
107#[inline(always)]
108pub fn get_peb() -> &'static Peb {
109    use super::get_teb;
110
111    get_teb().process_environmental_block
112}