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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// All files in the project carrying such notice may not be copied, modified, or distributed
// except according to those terms.
//! FFI bindings to psapi.
use shared::basetsd::{SIZE_T, ULONG_PTR};
use shared::minwindef::{BOOL, DWORD, HMODULE, LPDWORD, LPVOID, PDWORD};
use um::winnt::{HANDLE, LPCSTR, LPCWSTR, LPSTR, LPWSTR, PVOID};
pub const LIST_MODULES_DEFAULT: DWORD = 0x0;
pub const LIST_MODULES_32BIT: DWORD = 0x01;
pub const LIST_MODULES_64BIT: DWORD = 0x02;
pub const LIST_MODULES_ALL: DWORD = LIST_MODULES_32BIT | LIST_MODULES_64BIT;
extern "system" {
pub fn K32EnumProcesses(
lpidProcess: *mut DWORD,
cb: DWORD,
lpcbNeeded: LPDWORD,
) -> BOOL;
pub fn K32EnumProcessModules(
hProcess: HANDLE,
lphModule: *mut HMODULE,
cb: DWORD,
lpcbNeeded: LPDWORD,
) -> BOOL;
pub fn K32EnumProcessModulesEx(
hProcess: HANDLE,
lphModule: *mut HMODULE,
cb: DWORD,
lpcbNeeded: LPDWORD,
dwFilterFlag: DWORD,
) -> BOOL;
pub fn K32GetModuleBaseNameA(
hProcess: HANDLE,
hModule: HMODULE,
lpBaseName: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetModuleBaseNameW(
hProcess: HANDLE,
hModule: HMODULE,
lpBaseName: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetModuleFileNameExA(
hProcess: HANDLE,
hModule: HMODULE,
lpFilename: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetModuleFileNameExW(
hProcess: HANDLE,
hModule: HMODULE,
lpFilename: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32EmptyWorkingSet(
hProcess: HANDLE,
) -> BOOL;
pub fn K32QueryWorkingSet(
hProcess: HANDLE,
pv: PVOID,
cb: DWORD,
) -> BOOL;
pub fn K32QueryWorkingSetEx(
hProcess: HANDLE,
pv: PVOID,
cb: DWORD,
) -> BOOL;
pub fn K32InitializeProcessForWsWatch(
hProcess: HANDLE,
) -> BOOL;
pub fn K32GetWsChanges(
hProcess: HANDLE,
lpWatchInfo: PPSAPI_WS_WATCH_INFORMATION,
cb: DWORD,
) -> BOOL;
pub fn K32GetWsChangesEx(
hProcess: HANDLE,
lpWatchInfoEx: PPSAPI_WS_WATCH_INFORMATION_EX,
cb: PDWORD,
) -> BOOL;
pub fn K32GetMappedFileNameW(
hProcess: HANDLE,
lpv: LPVOID,
lpFilename: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetMappedFileNameA(
hProcess: HANDLE,
lpv: LPVOID,
lpFilename: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32EnumDeviceDrivers(
lpImageBase: *mut LPVOID,
cb: DWORD,
lpcbNeeded: LPDWORD,
) -> BOOL;
pub fn K32GetDeviceDriverBaseNameA(
ImageBase: LPVOID,
lpFilename: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetDeviceDriverBaseNameW(
ImageBase: LPVOID,
lpFilename: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetDeviceDriverFileNameA(
ImageBase: LPVOID,
lpFilename: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetDeviceDriverFileNameW(
ImageBase: LPVOID,
lpFilename: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetPerformanceInfo(
pPerformanceInformation: PPERFORMANCE_INFORMATION,
cb: DWORD,
) -> BOOL;
pub fn K32EnumPageFilesW(
pCallBackRoutine: PENUM_PAGE_FILE_CALLBACKW,
pContext: LPVOID,
) -> BOOL;
pub fn K32EnumPageFilesA(
pCallBackRoutine: PENUM_PAGE_FILE_CALLBACKA,
pContext: LPVOID,
) -> BOOL;
pub fn K32GetProcessImageFileNameA(
hProcess: HANDLE,
lpImageFileName: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn K32GetProcessImageFileNameW(
hProcess: HANDLE,
lpImageFileName: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn EnumProcesses(
lpidProcess: *mut DWORD,
cb: DWORD,
lpcbNeeded: LPDWORD,
) -> BOOL;
pub fn K32GetProcessMemoryInfo(
Process: HANDLE,
ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
cb: DWORD,
) -> BOOL;
pub fn K32GetModuleInformation(
hProcess: HANDLE,
hModule: HMODULE,
lpmodinfo: LPMODULEINFO,
cb: DWORD,
) -> BOOL;
}
pub type LPMODULEINFO = *mut MODULEINFO;
pub type PPSAPI_WORKING_SET_INFORMATION = *mut PSAPI_WORKING_SET_INFORMATION;
pub type PPSAPI_WORKING_SET_EX_INFORMATION = *mut PSAPI_WORKING_SET_EX_INFORMATION;
pub type PPSAPI_WS_WATCH_INFORMATION = *mut PSAPI_WS_WATCH_INFORMATION;
pub type PPSAPI_WS_WATCH_INFORMATION_EX = *mut PSAPI_WS_WATCH_INFORMATION_EX;
pub type PENUM_PAGE_FILE_INFORMATION = *mut ENUM_PAGE_FILE_INFORMATION;
pub type PPERFORMANCE_INFORMATION = *mut PERFORMANCE_INFORMATION;
pub type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
pub type PPROCESS_MEMORY_COUNTERS_EX = *mut PROCESS_MEMORY_COUNTERS_EX;
FN!{stdcall PENUM_PAGE_FILE_CALLBACKA(
pContext: LPVOID,
pPageFileInfo: PENUM_PAGE_FILE_INFORMATION,
lpFilename: LPCSTR,
) -> BOOL}
FN!{stdcall PENUM_PAGE_FILE_CALLBACKW(
pContext: LPVOID,
pPageFileInfo: PENUM_PAGE_FILE_INFORMATION,
lpFilename: LPCWSTR,
) -> BOOL}
STRUCT!{struct MODULEINFO {
lpBaseOfDll: LPVOID,
SizeOfImage: DWORD,
EntryPoint: LPVOID,
}}
STRUCT!{struct ENUM_PAGE_FILE_INFORMATION {
cb: DWORD,
Reserved: DWORD,
TotalSize: SIZE_T,
TotalInUse: SIZE_T,
PeakUsage: SIZE_T,
}}
STRUCT!{struct PERFORMANCE_INFORMATION {
cb: DWORD,
CommitTotal: SIZE_T,
CommitLimit: SIZE_T,
CommitPeak: SIZE_T,
PhysicalTotal: SIZE_T,
PhysicalAvailable: SIZE_T,
SystemCache: SIZE_T,
KernelTotal: SIZE_T,
KernelPaged: SIZE_T,
KernelNonpaged: SIZE_T,
PageSize: SIZE_T,
HandleCount: DWORD,
ProcessCount: DWORD,
ThreadCount: DWORD,
}}
STRUCT!{struct PROCESS_MEMORY_COUNTERS {
cb: DWORD,
PageFaultCount: DWORD,
PeakWorkingSetSize: SIZE_T,
WorkingSetSize: SIZE_T,
QuotaPeakPagedPoolUsage: SIZE_T,
QuotaPagedPoolUsage: SIZE_T,
QuotaPeakNonPagedPoolUsage: SIZE_T,
QuotaNonPagedPoolUsage: SIZE_T,
PagefileUsage: SIZE_T,
PeakPagefileUsage: SIZE_T,
}}
STRUCT!{struct PROCESS_MEMORY_COUNTERS_EX {
cb: DWORD,
PageFaultCount: DWORD,
PeakWorkingSetSize: SIZE_T,
WorkingSetSize: SIZE_T,
QuotaPeakPagedPoolUsage: SIZE_T,
QuotaPagedPoolUsage: SIZE_T,
QuotaPeakNonPagedPoolUsage: SIZE_T,
QuotaNonPagedPoolUsage: SIZE_T,
PagefileUsage: SIZE_T,
PeakPagefileUsage: SIZE_T,
PrivateUsage: SIZE_T,
}}
STRUCT!{struct PSAPI_WORKING_SET_BLOCK {
Flags: ULONG_PTR,
}}
BITFIELD!{PSAPI_WORKING_SET_BLOCK Flags: ULONG_PTR [
Protection set_Protection[0..5],
ShareCount set_ShareCount[5..8],
Shared set_Shared[8..9],
Reserved set_Reserved[9..12],
VirtualPage set_VirtualPage[12..32],
]}
pub type PPSAPI_WORKING_SET_BLOCK = *mut PSAPI_WORKING_SET_BLOCK;
STRUCT!{struct PSAPI_WORKING_SET_EX_BLOCK {
Flags: ULONG_PTR,
}}
#[cfg(not(target_arch="x86_64"))]
BITFIELD!{PSAPI_WORKING_SET_EX_BLOCK Flags: ULONG_PTR [
Valid set_Valid[0..1],
ShareCount set_ShareCount[1..4],
Win32Protection set_Win32Protection[4..15],
Shared set_Shared[15..16],
Node set_Node[16..22],
Locked set_Locked[22..23],
LargePage set_LargePage[23..24],
Reserved set_Reserved[24..31],
Bad set_Bad[31..32],
]}
#[cfg(target_arch="x86_64")]
BITFIELD!{PSAPI_WORKING_SET_EX_BLOCK Flags: ULONG_PTR [
Valid set_Valid[0..1],
ShareCount set_ShareCount[1..4],
Win32Protection set_Win32Protection[4..15],
Shared set_Shared[15..16],
Node set_Node[16..22],
Locked set_Locked[22..23],
LargePage set_LargePage[23..24],
Reserved set_Reserved[24..31],
Bad set_Bad[31..32],
ReservedUlong set_ReservedULong[32..64],
]}
pub type PPSAPI_WORKING_SET_EX_BLOCK = *mut PSAPI_WORKING_SET_EX_BLOCK;
STRUCT!{struct PSAPI_WORKING_SET_INFORMATION {
NumberOfEntries: ULONG_PTR,
WorkingSetInfo: [PSAPI_WORKING_SET_BLOCK; 1],
}}
STRUCT!{struct PSAPI_WORKING_SET_EX_INFORMATION {
VirtualAddress: PVOID,
VirtualAttributes: PSAPI_WORKING_SET_EX_BLOCK,
}}
STRUCT!{struct PSAPI_WS_WATCH_INFORMATION {
FaultingPc: LPVOID,
FaultingVa: LPVOID,
}}
STRUCT!{struct PSAPI_WS_WATCH_INFORMATION_EX {
BasicInfo: PSAPI_WS_WATCH_INFORMATION,
FaultingThreadId: ULONG_PTR,
Flags: ULONG_PTR,
}}
extern "system" {
pub fn EmptyWorkingSet(
hProcess: HANDLE,
) -> BOOL;
pub fn EnumDeviceDrivers(
lpImageBase: *mut LPVOID,
cb: DWORD,
lpcbNeeded: LPDWORD,
) -> BOOL;
pub fn EnumPageFilesA(
pCallBackRoutine: PENUM_PAGE_FILE_CALLBACKA,
pContext: LPVOID,
) -> BOOL;
pub fn EnumPageFilesW(
pCallBackRoutine: PENUM_PAGE_FILE_CALLBACKW,
pContext: LPVOID,
) -> BOOL;
pub fn EnumProcessModules(
hProcess: HANDLE,
lphModule: *mut HMODULE,
cb: DWORD,
lpcbNeeded: LPDWORD,
) -> BOOL;
pub fn EnumProcessModulesEx(
hProcess: HANDLE,
lphModule: *mut HMODULE,
cb: DWORD,
lpcbNeeded: LPDWORD,
dwFilterFlag: DWORD,
) -> BOOL;
pub fn GetDeviceDriverBaseNameA(
ImageBase: LPVOID,
lpFilename: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetDeviceDriverBaseNameW(
ImageBase: LPVOID,
lpFilename: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetDeviceDriverFileNameA(
ImageBase: LPVOID,
lpFilename: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetDeviceDriverFileNameW(
ImageBase: LPVOID,
lpFilename: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetMappedFileNameA(
hProcess: HANDLE,
lpv: LPVOID,
lpFilename: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetMappedFileNameW(
hProcess: HANDLE,
lpv: LPVOID,
lpFilename: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetModuleBaseNameA(
hProcess: HANDLE,
hModule: HMODULE,
lpBaseName: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetModuleBaseNameW(
hProcess: HANDLE,
hModule: HMODULE,
lpBaseName: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetModuleFileNameExA(
hProcess: HANDLE,
hModule: HMODULE,
lpFilename: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetModuleFileNameExW(
hProcess: HANDLE,
hModule: HMODULE,
lpFilename: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetModuleInformation(
hProcess: HANDLE,
hModule: HMODULE,
lpmodinfo: LPMODULEINFO,
cb: DWORD,
) -> BOOL;
pub fn GetPerformanceInfo(
pPerformanceInformation: PPERFORMANCE_INFORMATION,
cb: DWORD,
) -> BOOL;
pub fn GetProcessImageFileNameA(
hProcess: HANDLE,
lpImageFileName: LPSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetProcessImageFileNameW(
hProcess: HANDLE,
lpImageFileName: LPWSTR,
nSize: DWORD,
) -> DWORD;
pub fn GetProcessMemoryInfo(
hProcess: HANDLE,
ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
cb: DWORD,
) -> BOOL;
pub fn GetWsChanges(
hProcess: HANDLE,
lpWatchInfo: PPSAPI_WS_WATCH_INFORMATION,
cb: DWORD,
) -> BOOL;
pub fn GetWsChangesEx(
hProcess: HANDLE,
lpWatchInfoEx: PPSAPI_WS_WATCH_INFORMATION_EX,
cb: PDWORD,
) -> BOOL;
pub fn InitializeProcessForWsWatch(
hProcess: HANDLE,
) -> BOOL;
pub fn QueryWorkingSet(
hProcess: HANDLE,
pv: PVOID,
cb: DWORD,
) -> BOOL;
pub fn QueryWorkingSetEx(
hProcess: HANDLE,
pv: PVOID,
cb: DWORD,
) -> BOOL;
}
|