1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#![allow(non_camel_case_types, non_snake_case)]
use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::{ffi, privs::*};
handle! { HPROCESS;
/// Handle to a
/// [process](https://learn.microsoft.com/en-us/windows/win32/procthread/processes-and-threads).
/// Originally just a `HANDLE`.
}
impl HPROCESS {
/// [`CheckRemoteDebuggerPresent`](https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-checkremotedebuggerpresent)
/// function.
#[must_use]
pub fn CheckRemoteDebuggerPresent(&self) -> SysResult<bool> {
let mut present = 0;
BoolRet(unsafe { ffi::CheckRemoteDebuggerPresent(self.ptr(), &mut present) })
.to_sysresult()
.map(|_| present != 0)
}
/// [`FlushInstructionCache`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-flushinstructioncache)
/// function.
pub fn FlushInstructionCache(
&self,
base_address: *mut std::ffi::c_void,
size: usize,
) -> SysResult<()> {
BoolRet(unsafe { ffi::FlushInstructionCache(self.ptr(), base_address, size) })
.to_sysresult()
}
/// [`GetCurrentProcess`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess)
/// function.
#[must_use]
pub fn GetCurrentProcess() -> HPROCESS {
HPROCESS(unsafe { ffi::GetCurrentProcess() })
}
/// [`GetExitCodeProcess`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess)
/// function.
#[must_use]
pub fn GetExitCodeProcess(&self) -> SysResult<u32> {
let mut exit_code = 0u32;
BoolRet(unsafe { ffi::GetExitCodeProcess(self.ptr(), &mut exit_code) })
.to_sysresult()
.map(|_| exit_code)
}
/// [`GetGuiResources`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getguiresources)
/// function.
#[must_use]
pub fn GetGuiResources(&self, flags: co::GR) -> SysResult<u32> {
match unsafe { ffi::GetGuiResources(self.ptr(), flags.raw()) } {
0 => Err(GetLastError()),
count => Ok(count),
}
}
/// [`GetPriorityClass`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getpriorityclass)
/// function.
#[must_use]
pub fn GetPriorityClass(&self) -> SysResult<co::PRIORITY_CLASS> {
match unsafe { ffi::GetPriorityClass(self.ptr()) } {
0 => Err(GetLastError()),
pc => Ok(unsafe { co::PRIORITY_CLASS::from_raw(pc) }),
}
}
/// [`GetProcessHandleCount`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesshandlecount)
/// function.
#[must_use]
pub fn GetProcessHandleCount(&self) -> SysResult<u32> {
let mut count = 0u32;
BoolRet(unsafe { ffi::GetProcessHandleCount(self.ptr(), &mut count) })
.to_sysresult()
.map(|_| count)
}
/// [`GetProcessId`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocessid)
/// function.
#[must_use]
pub fn GetProcessId(&self) -> SysResult<u32> {
match unsafe { ffi::GetProcessId(self.ptr()) } {
0 => Err(GetLastError()),
id => Ok(id),
}
}
/// [`GetProcessTimes`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes)
/// function.
///
/// Returns, respectively:
///
/// 1. creation time;
/// 2. exit time;
/// 3. kernel time;
/// 4. user time.
///
/// # Examples
///
/// ```no_run
/// use winsafe::{self as w, prelude::*, co};
///
/// let hprocess: w::HPROCESS; // initialized somewhere
/// # let hprocess = w::HPROCESS::NULL;
///
/// let (creation, exit, kernel, user) = hprocess.GetProcessTimes()?;
/// # w::SysResult::Ok(())
/// ```
pub fn GetProcessTimes(&self) -> SysResult<(FILETIME, FILETIME, FILETIME, FILETIME)> {
let (mut creation, mut exit, mut kernel, mut user) =
(FILETIME::default(), FILETIME::default(), FILETIME::default(), FILETIME::default());
BoolRet(unsafe {
ffi::GetProcessTimes(
self.ptr(),
pvoid(&mut creation),
pvoid(&mut exit),
pvoid(&mut kernel),
pvoid(&mut user),
)
})
.to_sysresult()
.map(|_| (creation, exit, kernel, user))
}
/// [`IsProcessCritical`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocesscritical)
/// function.
#[must_use]
pub fn IsProcessCritical(&self) -> SysResult<bool> {
let mut critical = 0;
BoolRet(unsafe { ffi::IsProcessCritical(self.ptr(), &mut critical) })
.to_sysresult()
.map(|_| critical != 0)
}
/// [`IsWow64Process`](https://learn.microsoft.com/en-us/windows/win32/api/wow64apiset/nf-wow64apiset-iswow64process)
/// function.
#[must_use]
pub fn IsWow64Process(&self) -> SysResult<bool> {
let mut wow64 = 0;
match unsafe { ffi::IsWow64Process(self.ptr(), &mut wow64) } {
0 => Err(GetLastError()),
_ => Ok(wow64 != 0),
}
}
/// [`OpenProcess`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess)
/// function.
///
/// This method will return
/// [`ERROR::INVALID_PARAMETER`](crate::co::ERROR::INVALID_PARAMETER) if you
/// try to open a system process.
#[must_use]
pub fn OpenProcess(
desired_access: co::PROCESS,
inherit_handle: bool,
process_id: u32,
) -> SysResult<CloseHandleGuard<HPROCESS>> {
unsafe {
PtrRet(ffi::OpenProcess(desired_access.raw(), inherit_handle as _, process_id))
.to_sysresult_handle()
.map(|h| CloseHandleGuard::new(h))
}
}
/// [`QueryFullProcessImageName`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-queryfullprocessimagenamew)
/// function.
#[must_use]
pub fn QueryFullProcessImageName(&self, flags: co::PROCESS_NAME) -> SysResult<String> {
let mut buf = WString::new_alloc_buf(MAX_PATH + 1);
let mut sz = buf.buf_len() as u32;
BoolRet(unsafe {
ffi::QueryFullProcessImageNameW(self.ptr(), flags.raw(), buf.as_mut_ptr(), &mut sz)
})
.to_sysresult()
.map(|_| buf.to_string())
}
/// [`QueryProcessAffinityUpdateMode`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-queryprocessaffinityupdatemode)
/// function.
#[must_use]
pub fn QueryProcessAffinityUpdateMode(&self) -> SysResult<co::PROCESS_AFFINITY> {
let mut affinity = co::PROCESS_AFFINITY::default();
BoolRet(unsafe { ffi::QueryProcessAffinityUpdateMode(self.ptr(), affinity.as_mut()) })
.to_sysresult()
.map(|_| affinity)
}
/// [`QueryProcessCycleTime`](https://learn.microsoft.com/en-us/windows/win32/api/realtimeapiset/nf-realtimeapiset-queryprocesscycletime)
/// function.
#[must_use]
pub fn QueryProcessCycleTime(&self) -> SysResult<u64> {
let mut t = 0u64;
BoolRet(unsafe { ffi::QueryProcessCycleTime(self.ptr(), &mut t) })
.to_sysresult()
.map(|_| t)
}
/// [`ReadProcessMemory`](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-readprocessmemory)
/// function.
///
/// Reads at most `buffer.len()` bytes. Returns how many bytes were actually
/// read.
#[must_use]
pub fn ReadProcessMemory(
&self,
base_address: *mut std::ffi::c_void,
buffer: &mut [u8],
) -> SysResult<usize> {
let mut bytes_read = 0usize;
BoolRet(unsafe {
ffi::ReadProcessMemory(
self.ptr(),
base_address,
buffer.as_ptr() as _,
buffer.len(),
&mut bytes_read,
)
})
.to_sysresult()
.map(|_| bytes_read)
}
/// [`SetPriorityClass`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass)
/// function.
pub fn SetPriorityClass(&self, prority_class: co::PRIORITY_CLASS) -> SysResult<()> {
BoolRet(unsafe { ffi::SetPriorityClass(self.ptr(), prority_class.raw()) }).to_sysresult()
}
/// [`SetProcessAffinityUpdateMode`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setprocessaffinityupdatemode)
/// function.
pub fn SetProcessAffinityUpdateMode(&self, flags: co::PROCESS_AFFINITY) -> SysResult<()> {
BoolRet(unsafe { ffi::SetProcessAffinityUpdateMode(self.ptr(), flags.raw()) })
.to_sysresult()
}
/// [`SetProcessPriorityBoost`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setprocesspriorityboost)
/// function.
pub fn SetProcessPriorityBoost(&self, disable_priority_boost: bool) -> SysResult<()> {
BoolRet(unsafe { ffi::SetProcessPriorityBoost(self.ptr(), disable_priority_boost as _) })
.to_sysresult()
}
/// [`TerminateProcess`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminateprocess)
/// function.
pub fn TerminateProcess(&self, exit_code: u32) -> SysResult<()> {
BoolRet(unsafe { ffi::TerminateProcess(self.ptr(), exit_code) }).to_sysresult()
}
/// [`VirtualQueryEx`](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualqueryex)
/// function.
#[must_use]
pub fn VirtualQueryEx(
&self,
address: Option<*mut std::ffi::c_void>,
) -> SysResult<MEMORY_BASIC_INFORMATION> {
let mut mbi = MEMORY_BASIC_INFORMATION::default();
match unsafe {
ffi::VirtualQueryEx(
self.ptr(),
address.unwrap_or(std::ptr::null_mut()),
pvoid(&mut mbi),
std::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
)
} {
0 => Err(GetLastError()),
_ => Ok(mbi),
}
}
/// [`WaitForSingleObject`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject)
/// function.
pub fn WaitForSingleObject(&self, milliseconds: Option<u32>) -> SysResult<co::WAIT> {
unsafe { HEVENT::from_ptr(self.ptr()) }.WaitForSingleObject(milliseconds)
}
/// [`WriteProcessMemory`](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-writeprocessmemory)
/// function.
///
/// Returns how many bytes were actually written.
pub fn WriteProcessMemory(
&self,
base_address: *mut std::ffi::c_void,
buffer: &[u8],
) -> SysResult<usize> {
let mut bytes_written = 0usize;
BoolRet(unsafe {
ffi::WriteProcessMemory(
self.ptr(),
base_address,
buffer.as_ptr() as _,
buffer.len(),
&mut bytes_written,
)
})
.to_sysresult()
.map(|_| bytes_written)
}
}