-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathuninstall.cpp
More file actions
90 lines (77 loc) · 2.65 KB
/
uninstall.cpp
File metadata and controls
90 lines (77 loc) · 2.65 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
#include <windows.h>
#include <iostream>
#include <string>
bool IsElevated()
{
BOOL isElevated = FALSE;
HANDLE token = NULL;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
{
TOKEN_ELEVATION elevation;
DWORD size;
if (GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &size))
{
isElevated = elevation.TokenIsElevated;
}
CloseHandle(token);
}
return isElevated;
}
void RunAsAdmin(const char* path, const char* params)
{
SHELLEXECUTEINFOA sei = { sizeof(sei) };
sei.lpVerb = "runas";
sei.lpFile = path;
sei.lpParameters = params;
sei.hwnd = NULL;
sei.nShow = SW_SHOWNORMAL;
if (!ShellExecuteExA(&sei))
{
DWORD dwError = GetLastError();
if (dwError == ERROR_CANCELLED)
{
std::cerr << "The operation was cancelled by the user." << std::endl;
}
else
{
std::cerr << "Error running as admin: " << dwError << std::endl;
}
exit(1);
}
}
bool DeleteRegistryKey(HKEY hKeyParent, LPCSTR subKey) {
LONG result = RegDeleteTreeA(hKeyParent, subKey);
if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) {
std::cerr << "Error deleting registry key: " << result << std::endl;
return false;
}
return true;
}
bool UninstallContextMenu() {
bool success = true;
// Remove context menu for right-clicking inside a folder
std::string insideFolderSubKey = "Directory\\Background\\shell\\Open with Cursor";
success &= DeleteRegistryKey(HKEY_CLASSES_ROOT, insideFolderSubKey.c_str());
// Remove context menu for right-clicking on a folder
std::string onFolderSubKey = "Directory\\shell\\Open with Cursor";
success &= DeleteRegistryKey(HKEY_CLASSES_ROOT, onFolderSubKey.c_str());
// Remove context menu for all files
std::string allFilesSubKey = "*\\shell\\Open with Cursor";
success &= DeleteRegistryKey(HKEY_CLASSES_ROOT, allFilesSubKey.c_str());
return success;
}
int main(int argc, char* argv[]) {
if (!IsElevated())
{
RunAsAdmin(argv[0], (argc > 1) ? argv[1] : "");
return 0;
}
if (UninstallContextMenu()) {
std::string successMsg = "Context menu items uninstalled successfully.";
MessageBoxA(NULL, successMsg.c_str(), "Success", MB_OK | MB_ICONINFORMATION);
} else {
std::string errorMsg = "Failed to uninstall some or all context menu items.";
MessageBoxA(NULL, errorMsg.c_str(), "Error", MB_OK | MB_ICONERROR);
}
return 0;
}