-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOculus Killer.cpp
286 lines (191 loc) · 13.5 KB
/
Oculus Killer.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
// TITLE: Oculus Killer
// VERSION: 3.1.1
// CREATED BY: Owen Sullivan
// PURPOSE: This program forces the termination of Oculus software processes that run in the background after the Oculus software is exited.
// COMPILE: Oculus Killer is compiled using Visual Studio 2022. Open the .sln project in Visual Studio and build against the Release x64 target.
// DEPENDENCIES: Oculus Killer depends on the use of the windows-kill-library library from: https://github.com/ElyDotDev/windows-kill/tree/master/windows-kill-library
// SOURCES: https://github.com/ElyDotDev/windows-kill/tree/master/windows-kill-library
// https://stackoverflow.com/questions/865152/how-can-i-get-a-process-handle-by-its-name-in-c
// https://stackoverflow.com/questions/57066929/how-would-i-link-a-lib-library-from-a-different-solution-in-visual-studio-201
// https://stackoverflow.com/questions/246806/i-want-to-convert-stdstring-into-a-const-wchar-t
// https://www.unknowncheats.me/forum/c-and-c-/267237-std-vector-std-wstring-usage.html
// https://stackoverflow.com/questions/44985451/how-to-convert-wstring-to-wchar-t-c
// https://stackoverflow.com/questions/7956519/how-to-kill-processes-by-name-win32-api
// https://docs.microsoft.com/en-us/cpp/build/reference/manifestuac-embeds-uac-information-in-manifest?view=msvc-170
// https://stackoverflow.com/questions/6418791/requesting-administrator-privileges-at-run-time
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox
// https://stackoverflow.com/questions/6705751/c-win32-api-message-box-button-clicked
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postquitmessage
// https://stackoverflow.com/questions/62616855/how-to-specify-an-application-icon-for-c-visual-studio-2019
// https://stackoverflow.com/questions/8788057/how-to-initialize-and-print-a-stdwstring
// https://stackoverflow.com/questions/4053918/how-to-portably-write-stdwstring-to-file
// https://stackoverflow.com/questions/3884124/convert-a-console-app-to-a-windows-app
// https://stackoverflow.com/questions/495795/how-do-i-use-a-third-party-dll-file-in-visual-studio-c
// Includes
#include <iostream> // For std::wcout
#include <vector> // For std::vector
#include <cstdio> // For I/O
#include <windows.h> // For winapi
#include <tlhelp32.h> // Additional winapi
#include <stdexcept> // For exception handling
#include <WinUser.h> // For MessageBox()
#include <fstream> // For file I/O
// Custom headers
#include "dependencies/windows-kill-library.h" // windows-kill-library
// Link windows-kill-library library
#pragma comment(lib, "dependencies/windows-kill-library.lib")
// windows-kill-library macros
using WindowsKillLibrary::sendSignal;
using WindowsKillLibrary::SIGNAL_TYPE_CTRL_C;
using WindowsKillLibrary::SIGNAL_TYPE_CTRL_BREAK;
// Function prototypes
int displayMessageBox(const bool& anyProcessesFound, const bool& processKillFailure); // Display winapi MessageBox based on existence and/or success of process termination
// Main
int main(int argc, char** argv) {
// Vector to hold process names to be iterated through and killed
std::vector<std::wstring> processesToKill = { L"OVRServiceLauncher.exe", L"OVRRedir.exe", L"OVRServer_x64.exe" }; // Formatted as wstring for use with szExeFile
// Boolean to hold status of whether or not any processes were found running (used in MessageBox)
bool anyProcessesFound = false; // False by default
// Boolean to hold status of any process termination failures
bool processKillFailure = false; // False by default
// Open "lastRun.log" in program directory for output logs of last program run
std::wofstream lastRunLog;
lastRunLog.open(L"lastRun.log", std::ios::out | std::ios::trunc); // Truncate existing logs
// Loop through each process and terminate
for (int numProcess = 0; numProcess < processesToKill.size(); numProcess++) {
// Boolean to hold status of whether or not processes are found running
bool processFound = false;
// Create child process to iterate through list of running processes
PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
// If child process is still open (not closed from within loop)
if (Process32First(snapshot, &processEntry) == TRUE) {
// If child process is still iterating through running processes
while (Process32Next(snapshot, &processEntry) == TRUE) {
// If process name matches currently iterated process in running process list
if (wcscmp(processEntry.szExeFile, processesToKill.at(numProcess).c_str()) == 0) {
// Set anyProcessesFound status to true
anyProcessesFound = true;
// Set processFound status to true
processFound = true;
// Create a child process with which to find the PID of the matching process
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processEntry.th32ProcessID);
// Try to terminate the process by PID
try {
// Log status to command-line and "lastRun.log" file
std::wcout << "INFO: Found process matching " << processesToKill.at(numProcess).c_str() << " with PID " << processEntry.th32ProcessID << "." << std::endl;
lastRunLog << "INFO: Found process matching " << processesToKill.at(numProcess).c_str() << " with PID " << processEntry.th32ProcessID << "." << std::endl;
// Use windows-kill-library function sendSignal() to send POSIX-like exit signal
sendSignal(processEntry.th32ProcessID, SIGNAL_TYPE_CTRL_C);
// Log status to command-line and "lastRun.log" file
std::wcout << "INFO: Terminated process with PID " << processEntry.th32ProcessID << "." << std::endl;
lastRunLog << "INFO: Terminated process with PID " << processEntry.th32ProcessID << "." << std::endl;
} catch (const std::invalid_argument& exception) { // Catch error when an argument for sendSignal() is invalid
if (strcmp(exception.what(), "ESRCH") == 0) { // If PID does not exist
// Log status to command-line and "lastRun.log" file
std::wcout << "ERROR: Invalid PID; specified process ID does not exist." << std::endl;
lastRunLog << "ERROR: Invalid PID; specified process ID does not exist." << std::endl;
// Set processKillFailure status to true
processKillFailure = true;
} else if (strcmp(exception.what(), "EINVAL") == 0) { // If signal type is invalid
// Log status to command-line and "lastRun.log" file
std::wcout << "ERROR: Invalid windows-kill signal type." << std::endl;
lastRunLog << "ERROR: Invalid windows-kill signal type." << std::endl;
// Set processKillFailure status to true
processKillFailure = true;
} else { // All other errors
// Log status to command-line and "lastRun.log" file
std::wcout << "ERROR: InvalidArgument: windows-kill-library:" << exception.what() << std::endl;
lastRunLog << "ERROR: InvalidArgument: windows-kill-library:" << exception.what() << std::endl;
// Set processKillFailure status to true
processKillFailure = true;
}
} catch (const std::system_error& exception) { // Catch system errors
// Log status to command-line and "lastRun.log" file
std::wcout << "ERROR: SystemError " << exception.code() << ": " << exception.what() << std::endl;
lastRunLog << "ERROR: SystemError " << exception.code() << ": " << exception.what() << std::endl;
// Set processKillFailure status to true
processKillFailure = true;
} catch (const std::runtime_error& exception) { // Catch runtime errors
if (strcmp(exception.what(), "EPERM") == 0) { // If permissions are not elevated (somehow)
// Log status to command-line and "lastRun.log" file
std::wcout << "ERROR: Missing required permissions to send specified signal." << std::endl;
lastRunLog << "ERROR: Missing required permissions to send specified signal." << std::endl;
// Set processKillFailure status to true
processKillFailure = true;
} else { // All other errors
// Log status to command-line and "lastRun.log" file
std::wcout << "ERROR: RuntimeError: windows-kill-library:" << exception.what() << std::endl;
lastRunLog << "ERROR: RuntimeError: windows-kill-library:" << exception.what() << std::endl;
// Set processKillFailure status to true
processKillFailure = true;
}
} catch (const std::exception& exception) { // Catch all other exceptions
// Log status to command-line and "lastRun.log" file
std::wcout << "ERROR: windows-kill-library:" << exception.what() << std::endl;
lastRunLog << "ERROR: windows-kill-library:" << exception.what() << std::endl;
// Set processKillFailure status to true
processKillFailure = true;
}
// Close process used to get PID of matching process
CloseHandle(hProcess);
}
}
}
// Close child process
CloseHandle(snapshot);
// If a process has not been found matching the specified name
if (!(processFound)) {
// Log status to command-line and "lastRun.log" file
std::wcout << "INFO: No process found matching " << processesToKill.at(numProcess).c_str() << "." << std::endl;
lastRunLog << "INFO: No process found matching " << processesToKill.at(numProcess).c_str() << "." << std::endl;
}
}
// Display MessageBox based on whether or not any processes were found
if (anyProcessesFound) { // If any processes were found
// Display MessageBox based on whether or not there were failures terminating processes
if (processKillFailure) { // If some processes failed to terminate
displayMessageBox(true, true);
} else { // If all processes terminated successfully
displayMessageBox(true, false);
}
} else { // If no processes were found
displayMessageBox(false, false);
}
// Close "lastRun.log" file
lastRunLog.close();
}
// Display winapi MessageBox based on success of process termination
int displayMessageBox(const bool& anyProcessesFound, const bool& processKillFailure) {
if (anyProcessesFound) { // If any processes were found running
if (processKillFailure) { // If there were termination failures
// Create MessageBox with error message and error icon
int msgboxID = MessageBox(NULL, (LPCWSTR)L"Oculus Killer failed to kill one or more process.\nCheck the \"lastrun.log\" file for more information.", (LPCWSTR)L"Oculus Killer", MB_ICONERROR | MB_OK | MB_DEFBUTTON1);
switch (msgboxID) {
case IDOK:
PostQuitMessage(0); // Close program when "OK" button is clicked
return 0;
}
return msgboxID; // Generate MessageBox
} else { // If there were no termination failures
// Create MessageBox with success message and informational icon
int msgboxID = MessageBox(NULL, (LPCWSTR)L"Oculus Killer terminated all running Oculus processes successfully.", (LPCWSTR)L"Oculus Killer", MB_ICONINFORMATION | MB_OK | MB_DEFBUTTON1);
switch (msgboxID) {
case IDOK:
PostQuitMessage(0); // Close program when "OK" button is clicked
return 0;
}
return msgboxID; // Generate MessageBox
}
} else { // If no processes were found running
// Create MessageBox with message and informational icon
int msgboxID = MessageBox(NULL, (LPCWSTR)L"Oculus Killer did not find any running Oculus processes.", (LPCWSTR)L"Oculus Killer", MB_ICONINFORMATION | MB_OK | MB_DEFBUTTON1);
switch (msgboxID) {
case IDOK:
PostQuitMessage(0); // Close program when "OK" button is clicked
return 0;
}
return msgboxID; // Generate MessageBox
}
}