-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.cpp
More file actions
535 lines (449 loc) · 12.8 KB
/
Copy pathsetup.cpp
File metadata and controls
535 lines (449 loc) · 12.8 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
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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <shobjidl.h>
#include <tlhelp32.h>
#include <winreg.h>
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <string>
#include <system_error>
#include <vector>
constexpr int IDR_APP_PAYLOAD = 101;
constexpr wchar_t APP_NAME[] = L"ClickGuard";
constexpr wchar_t APP_EXE_NAME[] = L"ClickGuard.exe";
constexpr wchar_t UNINSTALL_EXE_NAME[] = L"ClickGuard Uninstall.exe";
constexpr wchar_t UNINSTALL_LINK_NAME[] = L"Uninstall ClickGuard.lnk";
constexpr wchar_t UNINSTALL_REG_PATH[] =
L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\ClickGuard";
std::wstring GetKnownFolder(REFKNOWNFOLDERID folderId) {
PWSTR path = nullptr;
const HRESULT hr = SHGetKnownFolderPath(
folderId,
KF_FLAG_DEFAULT,
nullptr,
&path
);
if (FAILED(hr) || !path) {
return {};
}
std::wstring result(path);
CoTaskMemFree(path);
return result;
}
std::wstring GetModulePath() {
std::wstring path(MAX_PATH, L'\0');
const DWORD length = GetModuleFileNameW(
nullptr,
path.data(),
static_cast<DWORD>(path.size())
);
path.resize(length);
return path;
}
bool CreateShortcut(
const std::wstring& shortcutPath,
const std::wstring& targetPath,
const std::wstring& arguments,
const std::wstring& workingDirectory,
const std::wstring& description
) {
IShellLinkW* shellLink = nullptr;
const HRESULT createHr = CoCreateInstance(
CLSID_ShellLink,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IShellLinkW,
reinterpret_cast<void**>(&shellLink)
);
if (FAILED(createHr) || !shellLink) {
return false;
}
shellLink->SetPath(targetPath.c_str());
shellLink->SetArguments(arguments.c_str());
shellLink->SetWorkingDirectory(workingDirectory.c_str());
shellLink->SetDescription(description.c_str());
shellLink->SetIconLocation(targetPath.c_str(), 0);
shellLink->SetShowCmd(SW_SHOWMINNOACTIVE);
IPersistFile* persistFile = nullptr;
const HRESULT queryHr = shellLink->QueryInterface(
IID_IPersistFile,
reinterpret_cast<void**>(&persistFile)
);
bool success = false;
if (SUCCEEDED(queryHr) && persistFile) {
success = SUCCEEDED(
persistFile->Save(shortcutPath.c_str(), TRUE)
);
persistFile->Release();
}
shellLink->Release();
return success;
}
void RemoveIfExists(const std::wstring& path) {
std::error_code error;
std::filesystem::remove(path, error);
}
bool CopyFileReplace(
const std::wstring& sourcePath,
const std::wstring& destinationPath
) {
std::error_code error;
std::filesystem::copy_file(
sourcePath,
destinationPath,
std::filesystem::copy_options::overwrite_existing,
error
);
return !error;
}
std::vector<std::byte> LoadPayloadResource() {
const HRSRC resource = FindResourceW(
nullptr,
MAKEINTRESOURCEW(IDR_APP_PAYLOAD),
RT_RCDATA
);
if (!resource) {
return {};
}
const HGLOBAL loaded = LoadResource(nullptr, resource);
if (!loaded) {
return {};
}
const DWORD size = SizeofResource(nullptr, resource);
const void* data = LockResource(loaded);
if (!data || size == 0) {
return {};
}
const auto* bytes = static_cast<const std::byte*>(data);
return std::vector<std::byte>(bytes, bytes + size);
}
void StopRunningClickGuard() {
const HANDLE snapshot = CreateToolhelp32Snapshot(
TH32CS_SNAPPROCESS,
0
);
if (snapshot == INVALID_HANDLE_VALUE) {
return;
}
PROCESSENTRY32W entry{};
entry.dwSize = sizeof(entry);
if (!Process32FirstW(snapshot, &entry)) {
CloseHandle(snapshot);
return;
}
do {
if (wcscmp(entry.szExeFile, APP_EXE_NAME) != 0) {
continue;
}
HANDLE process = OpenProcess(
PROCESS_TERMINATE,
FALSE,
entry.th32ProcessID
);
if (!process) {
continue;
}
TerminateProcess(process, 0);
CloseHandle(process);
} while (Process32NextW(snapshot, &entry));
CloseHandle(snapshot);
Sleep(500);
}
bool WriteInstalledExe(const std::wstring& installDir) {
std::error_code fsError;
std::filesystem::create_directories(installDir, fsError);
if (fsError) {
return false;
}
const auto payload = LoadPayloadResource();
if (payload.empty()) {
return false;
}
const std::filesystem::path targetPath =
std::filesystem::path(installDir) / APP_EXE_NAME;
std::ofstream output(
targetPath,
std::ios::binary | std::ios::trunc
);
if (!output) {
return false;
}
output.write(
reinterpret_cast<const char*>(payload.data()),
static_cast<std::streamsize>(payload.size())
);
return output.good();
}
bool WriteUninstallRegistry(
const std::wstring& uninstallExePath,
const std::wstring& installDir
) {
HKEY uninstallKey = nullptr;
const LONG createStatus = RegCreateKeyExW(
HKEY_CURRENT_USER,
UNINSTALL_REG_PATH,
0,
nullptr,
0,
KEY_WRITE,
nullptr,
&uninstallKey,
nullptr
);
if (createStatus != ERROR_SUCCESS || !uninstallKey) {
return false;
}
const std::wstring displayName = APP_NAME;
const std::wstring uninstallString =
L"\"" + uninstallExePath + L"\" /uninstall";
const std::wstring publisher = L"drawliin";
const DWORD noModify = 1;
const DWORD noRepair = 1;
auto setString = [&](const wchar_t* name, const std::wstring& value) {
return RegSetValueExW(
uninstallKey,
name,
0,
REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()),
static_cast<DWORD>((value.size() + 1) * sizeof(wchar_t))
) == ERROR_SUCCESS;
};
const bool ok =
setString(L"DisplayName", displayName) &&
setString(L"DisplayIcon", uninstallExePath) &&
setString(L"InstallLocation", installDir) &&
setString(L"Publisher", publisher) &&
setString(L"UninstallString", uninstallString) &&
RegSetValueExW(
uninstallKey,
L"NoModify",
0,
REG_DWORD,
reinterpret_cast<const BYTE*>(&noModify),
sizeof(noModify)
) == ERROR_SUCCESS &&
RegSetValueExW(
uninstallKey,
L"NoRepair",
0,
REG_DWORD,
reinterpret_cast<const BYTE*>(&noRepair),
sizeof(noRepair)
) == ERROR_SUCCESS;
RegCloseKey(uninstallKey);
return ok;
}
void RemoveUninstallRegistry() {
RegDeleteTreeW(HKEY_CURRENT_USER, UNINSTALL_REG_PATH);
}
bool RunHiddenCommand(const std::wstring& commandLine) {
STARTUPINFOW startupInfo{};
startupInfo.cb = sizeof(startupInfo);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_HIDE;
PROCESS_INFORMATION processInfo{};
std::wstring mutableCommandLine = commandLine;
const BOOL ok = CreateProcessW(
nullptr,
mutableCommandLine.data(),
nullptr,
nullptr,
FALSE,
CREATE_NO_WINDOW,
nullptr,
nullptr,
&startupInfo,
&processInfo
);
if (!ok) {
return false;
}
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
return true;
}
bool UninstallClickGuard(
const std::wstring& installDir,
const std::wstring& desktop,
const std::wstring& programs,
const std::wstring& startup,
const std::wstring& currentModulePath
) {
StopRunningClickGuard();
RemoveIfExists(desktop + L"\\" + APP_NAME + L".lnk");
RemoveIfExists(programs + L"\\" + APP_NAME + L".lnk");
RemoveIfExists(programs + L"\\" + UNINSTALL_LINK_NAME);
RemoveIfExists(startup + L"\\" + APP_NAME + L".lnk");
RemoveUninstallRegistry();
RemoveIfExists(installDir + L"\\" + APP_EXE_NAME);
const std::wstring cleanupCommand =
L"cmd.exe /c ping 127.0.0.1 -n 2 >nul && del /f /q \"" +
currentModulePath +
L"\" && rmdir /s /q \"" +
installDir +
L"\"";
return RunHiddenCommand(cleanupCommand);
}
int WINAPI wWinMain(
HINSTANCE,
HINSTANCE,
PWSTR cmdLine,
int
) {
const HRESULT initHr = CoInitializeEx(
nullptr,
COINIT_APARTMENTTHREADED
);
if (FAILED(initHr)) {
MessageBoxW(
nullptr,
L"Failed to initialize setup.",
L"ClickGuard Setup",
MB_OK | MB_ICONERROR
);
return 1;
}
const std::wstring localAppData =
GetKnownFolder(FOLDERID_LocalAppData);
const std::wstring desktop =
GetKnownFolder(FOLDERID_Desktop);
const std::wstring programs =
GetKnownFolder(FOLDERID_Programs);
const std::wstring startup =
GetKnownFolder(FOLDERID_Startup);
if (
localAppData.empty() ||
desktop.empty() ||
programs.empty() ||
startup.empty()
) {
MessageBoxW(
nullptr,
L"Failed to resolve Windows folders for installation.",
L"ClickGuard Setup",
MB_OK | MB_ICONERROR
);
CoUninitialize();
return 1;
}
const std::wstring installDir =
localAppData + L"\\" + APP_NAME;
const std::wstring exePath =
installDir + L"\\" + APP_EXE_NAME;
const std::wstring uninstallExePath =
installDir + L"\\" + UNINSTALL_EXE_NAME;
const std::wstring selfPath = GetModulePath();
const std::wstring commandLine =
cmdLine ? std::wstring(cmdLine) : std::wstring();
if (
commandLine.find(L"/uninstall") != std::wstring::npos ||
commandLine.find(L"-uninstall") != std::wstring::npos
) {
const bool removed = UninstallClickGuard(
installDir,
desktop,
programs,
startup,
selfPath
);
MessageBoxW(
nullptr,
removed
? L"ClickGuard uninstall was started.\n\nAny remaining cleanup will finish after this window closes."
: L"ClickGuard could not be fully removed.",
L"ClickGuard Uninstall",
MB_OK | (removed ? MB_ICONINFORMATION : MB_ICONWARNING)
);
CoUninitialize();
return removed ? 0 : 1;
}
StopRunningClickGuard();
if (!WriteInstalledExe(installDir)) {
MessageBoxW(
nullptr,
L"Failed to install ClickGuard.exe.",
L"ClickGuard Setup",
MB_OK | MB_ICONERROR
);
CoUninitialize();
return 1;
}
if (!CopyFileReplace(selfPath, uninstallExePath)) {
MessageBoxW(
nullptr,
L"Failed to create the ClickGuard uninstaller.",
L"ClickGuard Setup",
MB_OK | MB_ICONERROR
);
CoUninitialize();
return 1;
}
const bool desktopOk = CreateShortcut(
desktop + L"\\" + APP_NAME + L".lnk",
exePath,
L"",
installDir,
L"Launch ClickGuard"
);
const bool programsOk = CreateShortcut(
programs + L"\\" + APP_NAME + L".lnk",
exePath,
L"",
installDir,
L"Launch ClickGuard"
);
const bool startupOk = CreateShortcut(
startup + L"\\" + APP_NAME + L".lnk",
exePath,
L"",
installDir,
L"Launch ClickGuard at sign-in"
);
const bool uninstallShortcutOk = CreateShortcut(
programs + L"\\" + UNINSTALL_LINK_NAME,
uninstallExePath,
L"/uninstall",
installDir,
L"Remove ClickGuard"
);
const bool uninstallRegistryOk = WriteUninstallRegistry(
uninstallExePath,
installDir
);
if (!desktopOk || !programsOk || !startupOk) {
MessageBoxW(
nullptr,
L"ClickGuard was installed, but one or more app shortcuts could not be created.",
L"ClickGuard Setup",
MB_OK | MB_ICONWARNING
);
}
if (!uninstallShortcutOk || !uninstallRegistryOk) {
MessageBoxW(
nullptr,
L"ClickGuard was installed, but the uninstall entry could not be fully created.",
L"ClickGuard Setup",
MB_OK | MB_ICONWARNING
);
}
ShellExecuteW(
nullptr,
L"open",
exePath.c_str(),
nullptr,
installDir.c_str(),
SW_SHOWMINNOACTIVE
);
MessageBoxW(
nullptr,
L"ClickGuard was installed.\n\nDesktop, Start Menu, Startup, and uninstall entries were created.",
L"ClickGuard Setup",
MB_OK | MB_ICONINFORMATION
);
CoUninitialize();
return 0;
}