-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsystem.cpp
334 lines (270 loc) · 8.48 KB
/
system.cpp
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
/*
COPYRIGHT (C) 2025 ETHAN CHAN
ALL RIGHTS RESERVED. UNAUTHORIZED COPYING, MODIFICATION, DISTRIBUTION, OR USE
OF THIS SOFTWARE WITHOUT PRIOR PERMISSION IS STRICTLY PROHIBITED.
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
PROJECT NAME: DieKnow
FILENAME: src/system.cpp
DESCRIPTION: System information for DieKnow
AUTHOR: Ethan Chan
DATE: 2024-11-13
VERSION: 1.0.1
*/
#include "system.h"
namespace System {
std::unordered_map<HWND, WNDPROC> original_procedures;
WNDPROC _proc = nullptr;
const double WINDOW_DELAY = 0.7;
const std::vector<std::string> WINDOW_EXCLUDE_LIST = {
"GDI+",
"DDE Server Window",
"Default IME",
"MSCTFIME UI",
".NET-BroadcastEventWindow",
"DesktopWindowXamlSource",
"HardwareMonitorWindow",
"WISPTIS",
"SystemResourceNotifyWindow",
"DesktopWindow",
"Battery Meter",
"BluetoothNotificationAreaIconWindowClass",
"CiceroUIWndFrame",
"DesktopInfo",
"MediaContextNotificationWindow",
"TclNotifier",
"Task Switching",
"Task Host Window",
"Shell Handwriting Canvas",
"System tray overflow window.",
"SecurityHealthSystray",
"Win HCP",
"Windows Input Experience",
"OfficePowerManagerWindow",
"PopupHost",
"Progress",
"RealtekAudioAdminBackgroundProcessClass",
"CFD File Open Message Window",
"Core Sync",
"Desktop Info",
"Graphics Command Center",
"MenuWindow",
"Microsoft OneNote - Windows taskbar",
"MS_WebcheckMonitor",
"Network Flyout",
"Per Monitor Aware Window",
"Program Manager",
"Rtc Video PnP Listener",
"TtkMonitorWindow",
"Windows Push Notifications Platform",
"DWM Notification Window",
"Tooltip",
"Realtek Jack Windows"
};
bool Window::operator==(const System::Window& other) const {
return (title == other.title) &&
(class_name == other.class_name) &&
(hwnd == other.hwnd);
}
bool is_valid(const char* title) {
/*
Check if a window name is valid and not in the WINDOW_EXCLUDE_LIST.
*/
std::string caption(title);
return std::none_of(
System::WINDOW_EXCLUDE_LIST.begin(),
System::WINDOW_EXCLUDE_LIST.end(),
[&caption](const std::string& word) {
return caption.find(word) != std::string::npos;
}
);
return true;
}
std::string get_cpu_name() {
HKEY hkey;
char cpu_name[256];
DWORD buffer_size = sizeof(cpu_name);
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
0, KEY_READ, &hkey) == ERROR_SUCCESS) {
RegQueryValueExA(
hkey,
"ProcessorNameString",
NULL, NULL,
(LPBYTE)cpu_name,
&buffer_size);
RegCloseKey(hkey);
}
return std::string(cpu_name);
}
std::string get_gpu_name() {
DISPLAY_DEVICEA dd;
dd.cb = sizeof(dd);
std::string name = "Unknown GPU";
if (EnumDisplayDevicesA(NULL, 0, &dd, 0)) {
name = dd.DeviceString;
}
return name;
}
std::string get_os_info() {
/*
Retrieve OS information, such as Windows build information.
*/
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
// Get architecture
std::string arch = (
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
) ? "64-bit" : "32-bit";
OSVERSIONINFOEXA osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXA));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXA);
GetVersionExA((LPOSVERSIONINFOA)&osvi);
std::ostringstream os_info;
os_info << "Windows " << osvi.dwMajorVersion << "." << osvi.dwMinorVersion
<< " (Build " << osvi.dwBuildNumber << "), " << arch;
return os_info.str();
}
std::string get_available_ram() {
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
std::ostringstream ram_info;
ram_info << (statex.ullAvailPhys / (1024 * 1024)) << " MB available";
return ram_info.str();
}
void press(BYTE key) {
/*
Press a key.
*/
keybd_event(key, 0, 0, 0);
}
void release(BYTE key) {
/*
Release a held key.
*/
keybd_event(key, 0, KEYEVENTF_KEYUP, 0);
}
inline void push(BYTE key) {
/*
Shortcut to push and release keys.
*/
System::press(key);
System::release(key);
}
void toggle_internet() {
/*
Toggle internet by the following keypresses:
1. Press Windows-A
2. Press Space
3. Press Windows-A
4. Press Escape to close the window
*/
if (!settings.get<bool>("internet_toggler", false)) return;
System::press(0x5B);
System::press(0x41);
System::release(0x41);
System::release(0x5b);
std::this_thread::sleep_for(std::chrono::duration<double>(WINDOW_DELAY));
System::push(0x20); // Space
System::push(0x1B); // Escape
}
BOOL CALLBACK enum_windows(HWND hwnd, LPARAM lParam) {
std::vector<System::Window>* windows =
reinterpret_cast<std::vector<System::Window>*>(lParam);
char title[256];
char class_name[256];
GetWindowText(hwnd, title, sizeof(title));
GetClassNameA(hwnd, class_name, sizeof(class_name));
if (title[0] && is_valid(title)) {
windows->push_back({hwnd, title, class_name});
}
return TRUE;
}
BOOL CALLBACK enum_snapshot(HWND hwnd, LPARAM lParam) {
/*
Enumerate through a list of windows to be used as a snapshot.
The primary difference bteween `enum_windows()` is this uses the window
classname and checks if it is visible before pushing back, not if it is a
system window or not.
*/
std::vector<System::Window>* windows =
reinterpret_cast<std::vector<System::Window>*>(lParam);
char title[256];
char class_name[256];
GetWindowText(hwnd, title, sizeof(title));
GetClassNameA(hwnd, class_name, sizeof(class_name));
if ((class_name[0]) &&
(IsWindowVisible(hwnd))) {
windows->push_back({hwnd, title, class_name});
}
return TRUE;
}
LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo) {
DWORD code = ExceptionInfo->ExceptionRecord->ExceptionCode;
PVOID address = ExceptionInfo->ExceptionRecord->ExceptionAddress;
std::cerr << "Exception Code: " << code << std::endl;
std::cerr << "Exception Address: " << address << std::endl;
// Registers
const CONTEXT* context = ExceptionInfo->ContextRecord;
std::cerr << "RAX: " << context->Rax << std::endl;
std::cerr << "RBX: " << context->Rbx << std::endl;
std::cerr << "RCX: " << context->Rcx << std::endl;
std::cerr << "RDX: " << context->Rdx << std::endl;
return EXCEPTION_EXECUTE_HANDLER;
}
ErrorBuffer::ErrorBuffer(std::streambuf *original) :
original_buffer(original) {}
ErrorBuffer::~ErrorBuffer() {}
// cppcheck-suppress unusedFunction
int ErrorBuffer::overflow(int c) {
if (c != EOF) {
if (first) {
const char *message = "ERROR: ";
while (*message) {
original_buffer->sputc(*message++);
}
first = false;
}
original_buffer->sputc('\033');
original_buffer->sputc('[');
original_buffer->sputc('1');
original_buffer->sputc(';');
original_buffer->sputc('3');
original_buffer->sputc('1');
original_buffer->sputc('m');
original_buffer->sputc(c);
original_buffer->sputc('\033');
original_buffer->sputc('[');
original_buffer->sputc('0');
original_buffer->sputc('m');
}
return c;
}
// static ErrorBuffer buffer(std::cerr.rdbuf());
// static std::streambuf *const original_buffer = std::cerr.rdbuf(&buffer);
// cppcheck-suppress unusedFunction
LRESULT ShieldWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
/*
Custom Window procedure to prevent a window from being hidden.
Used by a hook to replace the existing window. All unhandled messages are
redirected to the original window.
*/
if ((uMsg == WM_SHOWWINDOW) &&
(wParam == FALSE)) {
return 0;
}
return CallWindowProc(
original_procedures[hwnd],
hwnd,
uMsg,
wParam,
lParam
);
}
} // namespace System