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
#![allow(non_camel_case_types, non_snake_case)]
use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::{ffi, privs::*};
handle! { HTHREAD;
/// Handle to a
/// [thread](https://learn.microsoft.com/en-us/windows/win32/procthread/processes-and-threads).
/// Originally just a `HANDLE`.
}
impl HTHREAD {
/// [`CreateThread`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread)
/// function.
///
/// Returns the thread handle and its ID.
#[must_use]
pub fn CreateThread(
thread_attrs: Option<&mut SECURITY_ATTRIBUTES>,
stack_size: usize,
start_addr: *mut std::ffi::c_void,
parameter: *mut std::ffi::c_void,
flags: co::THREAD_CREATE,
) -> SysResult<(CloseHandleGuard<HTHREAD>, u32)> {
let mut thread_id = 0u32;
unsafe {
PtrRet(ffi::CreateThread(
pvoid_or_null(thread_attrs),
stack_size,
start_addr,
parameter,
flags.raw(),
&mut thread_id,
))
.to_sysresult_handle()
.map(|h| (CloseHandleGuard::new(h), thread_id))
}
}
/// [`GetCurrentThread`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentthread)
/// function.
#[must_use]
pub fn GetCurrentThread() -> HTHREAD {
HTHREAD(unsafe { ffi::GetCurrentThread() })
}
/// [`GetExitCodeThread`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodethread)
/// function.
#[must_use]
pub fn GetExitCodeThread(&self) -> SysResult<u32> {
let mut exit_code = 0u32;
BoolRet(unsafe { ffi::GetExitCodeThread(self.ptr(), &mut exit_code) })
.to_sysresult()
.map(|_| exit_code)
}
/// [`GetProcessIdOfThread`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocessidofthread)
/// function.
#[must_use]
pub fn GetProcessIdOfThread(&self) -> SysResult<u32> {
match unsafe { ffi::GetProcessIdOfThread(self.ptr()) } {
0 => Err(GetLastError()),
id => Ok(id),
}
}
/// [`GetThreadId`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadid)
/// function.
#[must_use]
pub fn GetThreadId(&self) -> SysResult<u32> {
match unsafe { ffi::GetThreadId(self.ptr()) } {
0 => Err(GetLastError()),
id => Ok(id),
}
}
/// [`GetThreadIdealProcessorEx`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadidealprocessorex)
/// function.
#[must_use]
pub fn GetThreadIdealProcessorEx(&self) -> SysResult<PROCESSOR_NUMBER> {
let mut pi = PROCESSOR_NUMBER::default();
BoolRet(unsafe { ffi::GetThreadIdealProcessorEx(self.ptr(), pvoid(&mut pi)) })
.to_sysresult()
.map(|_| pi)
}
/// [`GetThreadIOPendingFlag`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadiopendingflag)
/// function.
#[must_use]
pub fn GetThreadIOPendingFlag(&self) -> SysResult<bool> {
let mut io = 0;
BoolRet(unsafe { ffi::GetThreadIOPendingFlag(self.ptr(), &mut io) })
.to_sysresult()
.map(|_| io != 0)
}
/// [`GetThreadPriorityBoost`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadpriorityboost)
/// function.
#[must_use]
pub fn GetThreadPriorityBoost(&self) -> SysResult<bool> {
let mut pb = 0;
BoolRet(unsafe { ffi::GetThreadPriorityBoost(self.ptr(), &mut pb) })
.to_sysresult()
.map(|_| pb != 0)
}
/// [`GetThreadTimes`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes)
/// 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 hthread: w::HTHREAD; // initialized somewhere
/// # let hthread = w::HTHREAD::NULL;
///
/// let (creation, exit, kernel, user) = hthread.GetThreadTimes()?;
/// # w::SysResult::Ok(())
/// ```
pub fn GetThreadTimes(&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::GetThreadTimes(
self.ptr(),
pvoid(&mut creation),
pvoid(&mut exit),
pvoid(&mut kernel),
pvoid(&mut user),
)
})
.to_sysresult()
.map(|_| (creation, exit, kernel, user))
}
/// [`QueryThreadCycleTime`](https://learn.microsoft.com/en-us/windows/win32/api/realtimeapiset/nf-realtimeapiset-querythreadcycletime)
/// function.
#[must_use]
pub fn QueryThreadCycleTime(&self) -> SysResult<u64> {
let mut t = 0u64;
BoolRet(unsafe { ffi::QueryThreadCycleTime(self.ptr(), &mut t) })
.to_sysresult()
.map(|_| t)
}
/// [`ResumeThread`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-resumethread)
/// function.
pub fn ResumeThread(&self) -> SysResult<u32> {
minus1_as_error(unsafe { ffi::ResumeThread(self.ptr()) })
}
/// [`SetThreadIdealProcessor`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadidealprocessor)
/// function.
///
/// Returns the previous ideal processor.
pub fn SetThreadIdealProcessor(&self, ideal_processor: u32) -> SysResult<u32> {
minus1_as_error(unsafe { ffi::SetThreadIdealProcessor(self.ptr(), ideal_processor) })
}
/// [`SetThreadIdealProcessorEx`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadidealprocessorex)
/// function.
///
/// Returns the previous ideal processor.
pub fn SetThreadIdealProcessorEx(
&self,
ideal_processor: PROCESSOR_NUMBER,
) -> SysResult<PROCESSOR_NUMBER> {
let mut prev = PROCESSOR_NUMBER::default();
BoolRet(unsafe {
ffi::SetThreadIdealProcessorEx(self.ptr(), pcvoid(&ideal_processor), pvoid(&mut prev))
})
.to_sysresult()
.map(|_| prev)
}
/// [`SetThreadPriorityBoost`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadpriorityboost)
/// function.
pub fn SetThreadPriorityBoost(&self, disable_priority_boost: bool) -> SysResult<()> {
BoolRet(unsafe { ffi::SetThreadPriorityBoost(self.ptr(), disable_priority_boost as _) })
.to_sysresult()
}
/// [`SuspendThread`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-suspendthread)
/// function.
pub fn SuspendThread(&self) -> SysResult<u32> {
minus1_as_error(unsafe { ffi::SuspendThread(self.ptr()) })
}
/// [`TerminateThread`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminatethread)
/// function.
pub fn TerminateThread(&self, exit_code: u32) -> SysResult<()> {
BoolRet(unsafe { ffi::TerminateThread(self.ptr(), exit_code) }).to_sysresult()
}
}