-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin_impl.c
More file actions
119 lines (99 loc) · 3.19 KB
/
win_impl.c
File metadata and controls
119 lines (99 loc) · 3.19 KB
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
#ifdef _WIN32
#include "../os_interface.h"
#include <psapi.h> // For memory and name info
#include <stdio.h>
#include <tlhelp32.h> // For snapshots (Freeze/Thaw logic)
#include <windows.h>
// Helper to open a process with specific permissions
HANDLE get_process_handle(int32_t pid) {
return OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
}
int32_t os_get_active_pid(void) {
HWND hwnd = GetForegroundWindow();
if (hwnd == NULL)
return -1;
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
return (int32_t)pid;
}
void os_get_process_name(int32_t pid, char *buffer, size_t size) {
// Set default in case of failure
snprintf(buffer, size, "Unknown");
HANDLE hProcess = get_process_handle(pid);
if (hProcess) {
HMODULE hMod;
DWORD cbNeeded;
// Get the first module (the executable itself)
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
GetModuleBaseNameA(hProcess, hMod, buffer, size);
}
CloseHandle(hProcess);
}
}
uint64_t os_get_memory_usage(int32_t pid) {
HANDLE hProcess = get_process_handle(pid);
if (!hProcess)
return 0;
PROCESS_MEMORY_COUNTERS pmc;
uint64_t mem_usage = 0;
if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
// WorkingSetSize is the RAM currently in use (roughly)
mem_usage = pmc.WorkingSetSize;
}
CloseHandle(hProcess);
return mem_usage;
}
// Stub for cross-platform compatibility
uint64_t os_get_swap_usage(void) { return 0; }
// Stub for cross-platform compatibility
double os_get_cpu_usage(int32_t pid) { return 0.0; }
// Stub for cross-platform compatibility
int os_get_memory_pressure(void) { return 0; }
// Stub for cross-platform compatibility
bool os_is_on_ac_power(void) { return true; }
// Stub for cross-platform compatibility
bool os_has_power_assertion(int32_t pid) { return false; }
// --- THE HARD PART: FREEZE & THAW ---
// Helper function to iterate threads and toggle them
int toggle_process_threads(int32_t pid, bool freeze) {
// 1. Take a snapshot of all threads in the system
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
return -1;
THREADENTRY32 te32;
te32.dwSize = sizeof(THREADENTRY32);
// 2. Get the first thread
if (!Thread32First(hThreadSnap, &te32)) {
CloseHandle(hThreadSnap);
return -1;
}
// 3. Loop through every thread in the system
do {
// 4. Check if this thread belongs to our target Process ID
if (te32.th32OwnerProcessID == pid) {
// Open the thread with permission to suspend/resume
HANDLE hThread =
OpenThread(THREAD_SUSPEND_RESUME, FALSE, te32.th32ThreadID);
if (hThread != NULL) {
if (freeze) {
SuspendThread(hThread);
} else {
ResumeThread(hThread);
}
CloseHandle(hThread);
}
}
} while (Thread32Next(hThreadSnap, &te32));
CloseHandle(hThreadSnap);
return 0;
}
int os_freeze_process(int32_t pid) {
// Don't freeze yourself or System processes!
if (pid <= 4)
return -1;
return toggle_process_threads(pid, true); // true = freeze
}
int os_thaw_process(int32_t pid) {
return toggle_process_threads(pid, false); // false = thaw
}
#endif // _WIN32