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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
use once_cell::sync::OnceCell;
use std::collections::HashSet;
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use winapi::shared::minwindef::{DWORD, FILETIME};
use winapi::shared::windef::HWND;
use winapi::um::handleapi::CloseHandle;
use winapi::um::memoryapi::ReadProcessMemory;
use winapi::um::processthreadsapi::{GetProcessTimes, OpenProcess};
use winapi::um::psapi::GetProcessImageFileNameW;
use winapi::um::winnt::{HANDLE, PROCESS_QUERY_INFORMATION, PROCESS_VM_READ};
use winapi::um::winuser::{EnumWindows, GetWindowThreadProcessId};
pub mod cli;
pub mod gui;
pub mod hwnd;
pub mod ps;
static INITIAL_HWND_SET: OnceCell<HashSet<isize>> = OnceCell::new();
static INITIAL_PID_SET: OnceCell<HashSet<u32>> = OnceCell::new();
pub fn snapshot_initial_hwnds() {
let mut hwnd_set = HashSet::new();
unsafe extern "system" fn enum_proc(
hwnd: HWND,
lparam: winapi::shared::minwindef::LPARAM,
) -> i32 {
unsafe {
let set = &mut *(lparam as *mut HashSet<isize>);
set.insert(hwnd as isize);
1
}
}
unsafe {
EnumWindows(
Some(enum_proc),
&mut hwnd_set as *mut _ as winapi::shared::minwindef::LPARAM,
);
}
INITIAL_HWND_SET.set(hwnd_set).ok();
}
pub fn is_hwnd_new(hwnd: HWND) -> bool {
if let Some(hwnd_set) = INITIAL_HWND_SET.get() {
!hwnd_set.contains(&(hwnd as isize))
} else {
false
}
}
pub fn snapshot_initial_pids() {
use winapi::um::tlhelp32::{
CreateToolhelp32Snapshot, PROCESSENTRY32, Process32First, Process32Next, TH32CS_SNAPPROCESS,
};
let mut pid_set = HashSet::new();
unsafe {
let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snapshot.is_null() {
return;
}
let mut entry: PROCESSENTRY32 = std::mem::zeroed();
entry.dwSize = std::mem::size_of::<PROCESSENTRY32>() as u32;
if Process32First(snapshot, &mut entry) != 0 {
loop {
pid_set.insert(entry.th32ProcessID);
if Process32Next(snapshot, &mut entry) == 0 {
break;
}
}
}
CloseHandle(snapshot);
}
INITIAL_PID_SET.set(pid_set).ok();
}
pub fn is_pid_new(pid: u32) -> bool {
if let Some(pid_set) = INITIAL_PID_SET.get() {
!pid_set.contains(&pid)
} else {
false
}
}
// Example usage: find the oldest matching GUI app(s)
// Usage: find_oldest_recent_apps(&file.to_string_lossy(), 1)
// Returns the oldest (least recent) matching app(s)
pub fn find_oldest_recent_apps(
program_name: &str,
num_oldest: usize,
parent_pid: Option<DWORD>,
launching_pid: Option<DWORD>,
) -> Vec<(HWND, u32, String, (i32, i32, i32, i32))> {
unsafe {
struct EnumData {
windows: Vec<(HWND, u32, u64, String, (i32, i32, i32, i32))>,
target_program_name: String,
launching_pid: Option<DWORD>,
parent_time: Option<u64>,
launching_time: Option<u64>,
}
extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
let data = unsafe { &mut *(lparam as *mut EnumData) };
if !is_hwnd_new(hwnd) {
// Skip if hwnd existed at program start
return 1; // Continue enumeration
}
// Check if the window has the WS_VISIBLE style
let style = unsafe {
winapi::um::winuser::GetWindowLongW(hwnd, winapi::um::winuser::GWL_STYLE)
};
if (style & winapi::um::winuser::WS_VISIBLE as i32) == 0 {
return 1;
}
// Check if the window is a top-level window
let parent_hwnd = unsafe { winapi::um::winuser::GetParent(hwnd) };
if !parent_hwnd.is_null() {
// skip non-top-level
}
// Get the process ID for the window
let mut process_id = 0;
unsafe {
GetWindowThreadProcessId(hwnd, &mut process_id);
}
// Skip if process_id matches launching_pid
if let Some(launching_pid) = data.launching_pid {
if process_id == launching_pid {
return 1;
}
}
// Open the process to get its creation time and executable name
let process_handle =
unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, process_id) };
struct CleanupHandle {
handle: HANDLE,
}
impl Drop for CleanupHandle {
fn drop(&mut self) {
if !self.handle.is_null() {
unsafe { CloseHandle(self.handle) };
}
}
}
let _cleanup = CleanupHandle {
handle: process_handle,
};
if process_handle.is_null() {
return 1;
}
// Get the executable name
let mut exe_path = [0u16; 260];
let exe_len = unsafe {
GetProcessImageFileNameW(
process_handle,
exe_path.as_mut_ptr(),
exe_path.len() as u32,
)
};
if exe_len == 0 {
return 1;
}
let exe_name = OsString::from_wide(&exe_path[..exe_len as usize])
.to_string_lossy()
.to_string();
// Get the class name of the window
let mut class_name = [0u16; 256];
let class_name_len = unsafe {
winapi::um::winuser::GetClassNameW(
hwnd,
class_name.as_mut_ptr(),
class_name.len() as i32,
)
};
let class_name_str = if class_name_len > 0 {
OsString::from_wide(&class_name[..class_name_len as usize])
.to_string_lossy()
.to_string()
} else {
String::new()
};
// Check if the executable name contains the target program name
if !exe_name
.to_ascii_lowercase()
.contains(&data.target_program_name.to_ascii_lowercase())
{
// If the target program name has no extension, try adding .exe or .com
if !data.target_program_name.contains('.') {
let exe_name_with_ext = format!("{}.exe", data.target_program_name);
let com_name_with_ext = format!("{}.com", data.target_program_name);
if exe_name
.to_ascii_lowercase()
.contains(&exe_name_with_ext.to_ascii_lowercase())
|| exe_name
.to_ascii_lowercase()
.contains(&com_name_with_ext.to_ascii_lowercase())
{
// match
} else {
return 1;
}
} else {
return 1;
}
}
// Get the process creation time
let mut creation_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut exit_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut kernel_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut user_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let success = unsafe {
GetProcessTimes(
process_handle,
&mut creation_time,
&mut exit_time,
&mut kernel_time,
&mut user_time,
)
};
if success == 0 {
return 1;
}
// Convert creation time to UNIX timestamp
let creation_time_unix = filetime_to_unix_time(creation_time);
// Filter out if creation_time_unix < parent_time or < launching_time
if let Some(parent_time) = data.parent_time {
if creation_time_unix < parent_time {
return 1;
}
}
if let Some(launching_time) = data.launching_time {
if creation_time_unix < launching_time {
return 1;
}
}
// Get the window bounds
let mut rect = unsafe { std::mem::zeroed() };
if unsafe { winapi::um::winuser::GetWindowRect(hwnd, &mut rect) } == 0 {
return 1;
}
let bounds = (
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
);
if bounds.2 == 0 || bounds.3 == 0 {
let parent_hwnd = unsafe { winapi::um::winuser::GetParent(hwnd) };
if !parent_hwnd.is_null() {
let mut parent_rect = unsafe { std::mem::zeroed() };
if unsafe { winapi::um::winuser::GetWindowRect(parent_hwnd, &mut parent_rect) }
!= 0
{
let parent_bounds = (
parent_rect.left,
parent_rect.top,
parent_rect.right - parent_rect.left,
parent_rect.bottom - parent_rect.top,
);
data.windows.push((
parent_hwnd,
process_id,
creation_time_unix,
class_name_str,
parent_bounds,
));
}
}
return 1;
}
data.windows
.push((hwnd, process_id, creation_time_unix, class_name_str, bounds));
1
}
// Helper to get process creation time by pid
fn get_process_creation_time(pid: DWORD) -> Option<u64> {
unsafe {
let handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pid);
if handle.is_null() {
return None;
}
let mut creation_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut exit_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut kernel_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut user_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let ok = GetProcessTimes(
handle,
&mut creation_time,
&mut exit_time,
&mut kernel_time,
&mut user_time,
);
CloseHandle(handle);
if ok == 0 {
None
} else {
Some(filetime_to_unix_time(creation_time))
}
}
}
let program_name = if program_name.starts_with('"') && program_name.ends_with('"') {
program_name
.trim_matches('"')
.rsplit('\\')
.next()
.unwrap_or(program_name)
.to_string()
} else {
program_name
.rsplit('\\')
.next()
.unwrap_or(program_name)
.to_string()
};
let parent_time = parent_pid.and_then(get_process_creation_time);
let launching_time = launching_pid.and_then(get_process_creation_time);
let mut data = EnumData {
windows: Vec::new(),
target_program_name: program_name.to_string(),
launching_pid,
parent_time,
launching_time,
};
EnumWindows(Some(enum_windows_proc), &mut data as *mut _ as isize);
// Sort windows by creation time (oldest first)
data.windows
.sort_by_key(|&(_, _, creation_time, _, _)| creation_time);
// Return the top `num_oldest` windows
data.windows
.into_iter()
.take(num_oldest)
.map(|(hwnd, pid, _, class_name, bounds)| (hwnd, pid, class_name, bounds))
.collect::<Vec<_>>()
}
}
// Converts a Windows FILETIME to a Unix timestamp (seconds since 1970-01-01)
pub fn filetime_to_unix_time(ft: FILETIME) -> u64 {
// FILETIME is in 100-nanosecond intervals since January 1, 1601 (UTC)
// UNIX epoch is January 1, 1970
let windows_to_unix_epoch: u64 = 11644473600; // seconds
let ticks = ((ft.dwHighDateTime as u64) << 32) | (ft.dwLowDateTime as u64);
let seconds = ticks / 10_000_000;
if seconds < windows_to_unix_epoch {
0
} else {
seconds - windows_to_unix_epoch
}
}
pub fn find_most_recent_gui_apps(
program_name: &str,
num_recent: usize,
parent_pid: Option<DWORD>,
_launching_pid: Option<DWORD>,
) -> Vec<(HWND, u32, String, (i32, i32, i32, i32))> {
unsafe {
struct EnumData {
windows: Vec<(HWND, u32, u64, String, (i32, i32, i32, i32))>,
target_program_name: String,
parent_pid: Option<DWORD>,
}
extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
let data = unsafe { &mut *(lparam as *mut EnumData) };
if !is_hwnd_new(hwnd) {
// Skip if hwnd existed at program start
return 1; // Continue enumeration
}
// println!("Enumerating HWND: {:?}", hwnd);
// // Check if the window is visible
// if unsafe { IsWindowVisible(hwnd) } == 0 {
// println!("HWND {:?} is not visible. Skipping.", hwnd);
// return 1; // Continue enumeration
// }
// Check if the window has the WS_VISIBLE style
let style = unsafe {
winapi::um::winuser::GetWindowLongW(hwnd, winapi::um::winuser::GWL_STYLE)
};
if (style & winapi::um::winuser::WS_VISIBLE as i32) == 0 {
// println!("HWND {:?} does not have WS_VISIBLE style. Skipping.", hwnd);
return 1; // Continue enumeration
}
// Check if the window is a top-level window
let parent_hwnd = unsafe { winapi::um::winuser::GetParent(hwnd) };
if !parent_hwnd.is_null() {
println!("HWND {:?} is not a top-level window. Skipping.", hwnd);
// return 1; // Skip non-top-level windows
}
// Get the process ID for the window
let mut process_id = 0;
unsafe {
GetWindowThreadProcessId(hwnd, &mut process_id);
}
// println!("HWND {:?} belongs to process ID: {}", hwnd, process_id);
// Open the process to get its creation time and executable name
let process_handle =
unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, process_id) };
let _cleanup = CleanupHandle {
handle: process_handle,
};
struct CleanupHandle {
handle: HANDLE,
}
//oh rust, how I love thee...
impl Drop for CleanupHandle {
fn drop(&mut self) {
// println!("Cleaning up handle: {:?}", self.handle);
if !self.handle.is_null() {
unsafe { CloseHandle(self.handle) };
}
}
}
if process_handle.is_null() {
// println!("Failed to open process for PID {}. Skipping HWND {:?}.", process_id, hwnd);
return 1; // Continue enumeration
}
// Get the executable name
let mut exe_path = [0u16; 260];
let exe_len = unsafe {
GetProcessImageFileNameW(
process_handle,
exe_path.as_mut_ptr(),
exe_path.len() as u32,
)
};
if exe_len == 0 {
// println!("Failed to get executable name for PID {}. Skipping HWND {:?}.", process_id, hwnd);
return 1; // Continue enumeration
}
let exe_name = OsString::from_wide(&exe_path[..exe_len as usize])
.to_string_lossy()
.to_string();
// println!("Executable name for PID {}: {}", process_id, exe_name);
// Get the class name of the window
let mut class_name = [0u16; 256];
let class_name_len = unsafe {
winapi::um::winuser::GetClassNameW(
hwnd,
class_name.as_mut_ptr(),
class_name.len() as i32,
)
};
let class_name_str = if class_name_len > 0 {
OsString::from_wide(&class_name[..class_name_len as usize])
.to_string_lossy()
.to_string()
} else {
// eprintln!("Failed to get class name for HWND {:?}", hwnd);
String::new()
};
// println!("Class name for HWND {:?}: {}", hwnd, class_name_str);
// If the executable name is "cmd.exe", check the command-line arguments
if exe_name.to_ascii_lowercase().ends_with("cmd.exe") {
println!("Executable name is cmd.exe. Checking command-line arguments...");
let mut cmdline = [0u16; 32768];
let cmdline_len = unsafe {
{
let cmdline_ptr = winapi::um::processenv::GetCommandLineW();
if cmdline_ptr.is_null() {
0
} else {
let mut len = 0;
while *cmdline_ptr.add(len) != 0 {
cmdline[len] = *cmdline_ptr.add(len);
len += 1;
}
len as u32
}
}
};
if cmdline_len > 0 {
let cmdline_str = OsString::from_wide(&cmdline[..cmdline_len as usize])
.to_string_lossy()
.to_string();
// Check if the command-line arguments contain the target program name
if !cmdline_str
.to_ascii_lowercase()
.contains(&data.target_program_name.to_ascii_lowercase())
{
return 1; // Continue enumeration
}
println!("Command-line arguments for cmd.exe: {}", cmdline_str);
} else {
// If we can't retrieve the command-line arguments, skip this window
return 1; // Continue enumeration
}
}
// Check if the executable name contains the target program name
if !exe_name
.to_ascii_lowercase()
.contains(&data.target_program_name.to_ascii_lowercase())
{
// If the target program name has no extension, try adding .exe or .com
if !data.target_program_name.contains('.') {
let exe_name_with_ext = format!("{}.exe", data.target_program_name);
let com_name_with_ext = format!("{}.com", data.target_program_name);
if exe_name
.to_ascii_lowercase()
.contains(&exe_name_with_ext.to_ascii_lowercase())
|| exe_name
.to_ascii_lowercase()
.contains(&com_name_with_ext.to_ascii_lowercase())
{
println!(
"Executable name '{}' matches target with extension '{}'.",
exe_name, data.target_program_name
);
if let Some(cmdline_str) = get_cmdline_for_pid(process_id) {
println!(
"Command-line arguments for PID {}: {}",
process_id, cmdline_str
);
}
} else {
return 1; // Continue enumeration
}
} else {
return 1; // Continue enumeration
}
return 1; // Continue enumeration
} else {
println!(
"Executable name '{}' matches target '{}'.",
exe_name, data.target_program_name
);
// Get the command line of the target process, not the current process
if let Some(cmdline_str) = get_cmdline_for_pid(process_id) {
println!(
"Command-line arguments for PID {}: {}",
process_id, cmdline_str
);
}
// Filter by command-line arguments: keep only if it contains "--w-pool-manager" and our parent_pid
if let Some(cmdline_str) = get_cmdline_for_pid(process_id) {
if cmdline_str.contains("--w-pool-manager") {
if let Some(parent_pid) = data.parent_pid {
if cmdline_str.contains(&parent_pid.to_string()) {
println!(
"PID {} has --w-pool-manager and parent_pid {} in command line: {}",
process_id, parent_pid, cmdline_str
);
// } else {
// // Does not contain parent_pid, skip this window
// return 1;
}
} else {
// No parent_pid specified, just keep if --w-pool-manager is present
println!(
"PID {} has --w-pool-manager in command line: {}",
process_id, cmdline_str
);
}
}
}
}
// Get the process creation time
let mut creation_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut exit_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut kernel_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut user_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let success = unsafe {
GetProcessTimes(
process_handle,
&mut creation_time,
&mut exit_time,
&mut kernel_time,
&mut user_time,
)
};
if success == 0 {
println!(
"Failed to get process times for PID {}. Skipping HWND {:?}.",
process_id, hwnd
);
return 1; // Continue enumeration
}
// Convert creation time to UNIX timestamp
let creation_time_unix = filetime_to_unix_time(creation_time);
println!(
"Creation time for PID {}: {}",
process_id, creation_time_unix
);
// Get the window bounds
let mut rect = unsafe { std::mem::zeroed() };
if unsafe { winapi::um::winuser::GetWindowRect(hwnd, &mut rect) } == 0 {
eprintln!("Failed to get window rect for HWND {:?}", hwnd);
return 1; // Continue enumeration
}
let bounds = (
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
);
println!("Bounds for HWND {:?}: {:?}", hwnd, bounds);
// If the bounds are zero, try to get the parent window
if bounds.2 == 0 || bounds.3 == 0 {
let parent_hwnd = unsafe { winapi::um::winuser::GetParent(hwnd) };
if !parent_hwnd.is_null() {
println!(
"Bounds are zero for HWND {:?}. Using parent HWND {:?} instead.",
hwnd, parent_hwnd
);
// Get the bounds of the parent window
let mut parent_rect = unsafe { std::mem::zeroed() };
if unsafe { winapi::um::winuser::GetWindowRect(parent_hwnd, &mut parent_rect) }
!= 0
{
let parent_bounds = (
parent_rect.left,
parent_rect.top,
parent_rect.right - parent_rect.left,
parent_rect.bottom - parent_rect.top,
);
println!(
"Bounds for parent HWND {:?}: {:?}",
parent_hwnd, parent_bounds
);
// Add the parent window to the list instead
data.windows.push((
parent_hwnd,
process_id,
creation_time_unix,
class_name_str,
parent_bounds,
));
} else {
eprintln!(
"Failed to get bounds for parent HWND {:?}. Skipping.",
parent_hwnd
);
}
} else {
eprintln!("No parent HWND found for HWND {:?}. Skipping.", hwnd);
}
return 1; // Continue enumeration
}
// Add the window to the list
data.windows
.push((hwnd, process_id, creation_time_unix, class_name_str, bounds));
1 // Continue enumeration
}
let program_name = if program_name.starts_with('"') && program_name.ends_with('"') {
program_name
.trim_matches('"')
.rsplit('\\')
.next()
.unwrap_or(program_name)
.to_string()
} else {
program_name
.rsplit('\\')
.next()
.unwrap_or(program_name)
.to_string()
};
let mut data = EnumData {
windows: Vec::new(),
target_program_name: program_name.to_string(),
parent_pid: parent_pid,
};
println!("Starting enumeration for program name: {}", program_name);
EnumWindows(Some(enum_windows_proc), &mut data as *mut _ as isize);
// Sort windows by creation time (most recent first)
println!("Sorting windows by creation time...");
data.windows
.sort_by_key(|&(_, _, creation_time, _, _)| std::cmp::Reverse(creation_time));
// Return the top `num_recent` windows
let result = data
.windows
.into_iter()
.take(num_recent)
.map(|(hwnd, pid, _, class_name, bounds)| (hwnd, pid, class_name, bounds))
.collect::<Vec<_>>();
println!(
"Found {} recent GUI apps matching '{}': {:?}",
result.len(),
program_name,
result
);
result
}
}
pub fn kill_process_and_children(parent_pid: u32) {
use winapi::um::handleapi::CloseHandle;
use winapi::um::processthreadsapi::OpenProcess;
use winapi::um::processthreadsapi::TerminateProcess;
use winapi::um::winnt::PROCESS_TERMINATE;
// 1. Get all child PIDs recursively
let mut pids = get_child_pids(parent_pid);
// 2. Add the parent itself
pids.push(parent_pid);
// 3. Kill each process
for pid in pids {
unsafe {
let handle = OpenProcess(PROCESS_TERMINATE, 0, pid);
if !handle.is_null() {
println!("Killing PID {}", pid);
TerminateProcess(handle, 1);
CloseHandle(handle);
} else {
println!("Failed to open PID {} for termination", pid);
}
}
}
}
#[allow(dead_code)]
pub fn get_child_pids(parent_pid: u32) -> Vec<u32> {
use winapi::shared::minwindef::FALSE;
use winapi::um::handleapi::CloseHandle;
use winapi::um::tlhelp32::{
CreateToolhelp32Snapshot, PROCESSENTRY32, Process32First, Process32Next, TH32CS_SNAPPROCESS,
};
let mut child_pids = Vec::new();
let mut stack = vec![parent_pid];
unsafe {
// Take a snapshot of all processes
let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snapshot.is_null() {
eprintln!("Failed to create process snapshot");
return child_pids;
}
let mut entry: PROCESSENTRY32 = std::mem::zeroed();
entry.dwSize = std::mem::size_of::<PROCESSENTRY32>() as u32;
// Collect all process entries into a Vec for easier traversal
let mut all_entries = Vec::new();
if Process32First(snapshot, &mut entry) != FALSE {
loop {
all_entries.push(entry);
if Process32Next(snapshot, &mut entry) == FALSE {
break;
}
}
}
CloseHandle(snapshot);
// Use a stack for DFS to collect all descendants
while let Some(pid) = stack.pop() {
for e in all_entries.iter() {
if e.th32ParentProcessID == pid && !child_pids.contains(&e.th32ProcessID) {
child_pids.push(e.th32ProcessID);
stack.push(e.th32ProcessID);
}
}
}
}
child_pids
}
use winapi::shared::ntdef::UNICODE_STRING;
/// Get the command line of a process by PID using winapi and direct memory reading.
/// Returns None if not accessible.
pub fn get_cmdline_for_pid(pid: u32) -> Option<String> {
unsafe {
// Open the process
let h_process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pid);
if h_process.is_null() {
return None;
}
// Get PROCESS_BASIC_INFORMATION (undocumented struct)
#[repr(C)]
struct PROCESS_BASIC_INFORMATION {
Reserved1: *mut std::ffi::c_void,
PebBaseAddress: *mut std::ffi::c_void,
Reserved2: [*mut std::ffi::c_void; 2],
UniqueProcessId: *mut std::ffi::c_void,
Reserved3: *mut std::ffi::c_void,
}
// NtQueryInformationProcess is not in winapi, so we declare it manually
type NtQueryInformationProcessType = unsafe extern "system" fn(
winapi::um::winnt::HANDLE,
u32,
*mut std::ffi::c_void,
u32,
*mut u32,
) -> i32;
let ntdll = winapi::um::libloaderapi::GetModuleHandleA(b"ntdll.dll\0".as_ptr() as _);
if ntdll.is_null() {
CloseHandle(h_process);
return None;
}
let proc_addr = winapi::um::libloaderapi::GetProcAddress(
ntdll,
b"NtQueryInformationProcess\0".as_ptr() as _,
);
if proc_addr.is_null() {
CloseHandle(h_process);
return None;
}
let nt_query_information_process: NtQueryInformationProcessType =
std::mem::transmute(proc_addr);
let mut pbi: PROCESS_BASIC_INFORMATION = std::mem::zeroed();
let mut return_len = 0u32;
let status = nt_query_information_process(
h_process,
0, // ProcessBasicInformation
&mut pbi as *mut _ as *mut _,
std::mem::size_of::<PROCESS_BASIC_INFORMATION>() as u32,
&mut return_len,
);
if status != 0 {
CloseHandle(h_process);
return None;
}
// Read PEB
#[repr(C)]
struct PEB {
Reserved1: [u8; 2],
BeingDebugged: u8,
Reserved2: [u8; 1],
Reserved3: [*mut std::ffi::c_void; 2],
Ldr: *mut std::ffi::c_void,
ProcessParameters: *mut RTL_USER_PROCESS_PARAMETERS,
}
#[repr(C)]
struct RTL_USER_PROCESS_PARAMETERS {
Reserved1: [u8; 16],
Reserved2: [*mut std::ffi::c_void; 10],
ImagePathName: UNICODE_STRING,
CommandLine: UNICODE_STRING,
}
let mut peb: PEB = std::mem::zeroed();
let mut bytes_read = 0;
if winapi::um::memoryapi::ReadProcessMemory(
h_process,
pbi.PebBaseAddress as *mut winapi::ctypes::c_void,
&mut peb as *mut _ as *mut _,
std::mem::size_of::<PEB>(),
&mut bytes_read,
) == 0
{
CloseHandle(h_process);
return None;
}
// Read RTL_USER_PROCESS_PARAMETERS
let mut upp: RTL_USER_PROCESS_PARAMETERS = std::mem::zeroed();
if ReadProcessMemory(
h_process,
peb.ProcessParameters as *mut _,
&mut upp as *mut _ as *mut _,
std::mem::size_of::<RTL_USER_PROCESS_PARAMETERS>(),
&mut bytes_read,
) == 0
{
CloseHandle(h_process);
return None;
}
// Read the command line UNICODE_STRING buffer
let len = upp.CommandLine.Length as usize / 2;
let mut buffer = vec![0u16; len];
if ReadProcessMemory(
h_process,
upp.CommandLine.Buffer as *mut _,
buffer.as_mut_ptr() as *mut _,
upp.CommandLine.Length as usize,
&mut bytes_read,
) == 0
{
CloseHandle(h_process);
return None;
}
CloseHandle(h_process);
Some(String::from_utf16_lossy(&buffer))
}
}
pub fn find_matching_env_gui_apps(
env_name: &str,
env_value: Option<&str>,
num_results: usize,
) -> Vec<(HWND, u32, String, (i32, i32, i32, i32))> {
use winapi::um::tlhelp32::{
CreateToolhelp32Snapshot, PROCESSENTRY32, Process32First, Process32Next, TH32CS_SNAPPROCESS,
};
// 1. Enumerate all processes and filter by is_pid_new and env var
let mut matching_pids = Vec::new();
unsafe {
let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snapshot.is_null() {
return Vec::new();
}
let mut entry: PROCESSENTRY32 = std::mem::zeroed();
entry.dwSize = std::mem::size_of::<PROCESSENTRY32>() as u32;
if Process32First(snapshot, &mut entry) != 0 {
loop {
let pid = entry.th32ProcessID;
if is_pid_new(pid)
&& crate::ps::process_has_env_var(pid, env_name, env_value).unwrap_or(false)
{
matching_pids.push(pid);
}
if Process32Next(snapshot, &mut entry) == 0 {
break;
}
}
}
CloseHandle(snapshot);
}
// 2. For each matching pid, enumerate windows and collect GUI windows
let mut results = Vec::new();
for pid in matching_pids {
// Enumerate all windows, filter by pid
unsafe {
struct EnumData {
pid: u32,
windows: Vec<(HWND, u32, u64, String, (i32, i32, i32, i32))>,
}
extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
let data = unsafe { &mut *(lparam as *mut EnumData) };
let mut process_id = 0;
unsafe { GetWindowThreadProcessId(hwnd, &mut process_id) };
if process_id != data.pid {
return 1;
}
if !is_hwnd_new(hwnd) {
return 1;
}
let style = unsafe {
winapi::um::winuser::GetWindowLongW(hwnd, winapi::um::winuser::GWL_STYLE)
};
if (style & winapi::um::winuser::WS_VISIBLE as i32) == 0 {
return 1;
}
let parent_hwnd = unsafe { winapi::um::winuser::GetParent(hwnd) };
if !parent_hwnd.is_null() {
// skip non-top-level
}
// Get process creation time
let process_handle = unsafe {
OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, process_id)
};
struct CleanupHandle {
handle: HANDLE,
}
impl Drop for CleanupHandle {
fn drop(&mut self) {
if !self.handle.is_null() {
unsafe { CloseHandle(self.handle) };
}
}
}
let _cleanup = CleanupHandle {
handle: process_handle,
};
if process_handle.is_null() {
return 1;
}
// Get the class name of the window
let mut class_name = [0u16; 256];
let class_name_len = unsafe {
winapi::um::winuser::GetClassNameW(
hwnd,
class_name.as_mut_ptr(),
class_name.len() as i32,
)
};
let class_name_str = if class_name_len > 0 {
OsString::from_wide(&class_name[..class_name_len as usize])
.to_string_lossy()
.to_string()
} else {
String::new()
};
// Get the process creation time
let mut creation_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut exit_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut kernel_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let mut user_time = FILETIME {
dwLowDateTime: 0,
dwHighDateTime: 0,
};
let success = unsafe {
GetProcessTimes(
process_handle,
&mut creation_time,
&mut exit_time,
&mut kernel_time,
&mut user_time,
)
};
if success == 0 {
return 1;
}
let creation_time_unix = filetime_to_unix_time(creation_time);
// Get the window bounds
let mut rect = unsafe { std::mem::zeroed() };
if unsafe { winapi::um::winuser::GetWindowRect(hwnd, &mut rect) } == 0 {
return 1;
}
let bounds = (
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
);
if bounds.2 == 0 || bounds.3 == 0 {
let parent_hwnd = unsafe { winapi::um::winuser::GetParent(hwnd) };
if !parent_hwnd.is_null() {
let mut parent_rect = unsafe { std::mem::zeroed() };
if unsafe {
winapi::um::winuser::GetWindowRect(parent_hwnd, &mut parent_rect)
} != 0
{
let parent_bounds = (
parent_rect.left,
parent_rect.top,
parent_rect.right - parent_rect.left,
parent_rect.bottom - parent_rect.top,
);
data.windows.push((
parent_hwnd,
process_id,
creation_time_unix,
class_name_str,
parent_bounds,
));
}
}
return 1;
}
data.windows
.push((hwnd, process_id, creation_time_unix, class_name_str, bounds));
1
}
let mut data = EnumData {
pid,
windows: Vec::new(),
};
EnumWindows(Some(enum_windows_proc), &mut data as *mut _ as isize);
results.extend(data.windows);
}
}
// Sort by creation time (most recent first)
results.sort_by_key(|&(_, _, creation_time, _, _)| std::cmp::Reverse(creation_time));
results
.into_iter()
.take(num_results)
.map(|(hwnd, pid, _, class_name, bounds)| (hwnd, pid, class_name, bounds))
.collect()
}