-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumireUninstaller.cpp
More file actions
275 lines (237 loc) · 7.59 KB
/
Copy pathSumireUninstaller.cpp
File metadata and controls
275 lines (237 loc) · 7.59 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
#include <windows.h>
#include <shellapi.h>
#include <filesystem>
#include <fstream>
#include <initializer_list>
#include <string>
#include "SumireInstallUtil.h"
#include "SumireSettingsStore.h"
namespace
{
struct UninstallOptions
{
bool silent = false;
bool removeSettings = false;
};
bool IsSwitch(const std::wstring& argument, std::initializer_list<const wchar_t*> names)
{
for (const wchar_t* name : names)
{
if (CompareStringOrdinal(argument.c_str(), -1, name, -1, TRUE) == CSTR_EQUAL)
{
return true;
}
}
return false;
}
UninstallOptions ParseUninstallOptions()
{
UninstallOptions options;
int argumentCount = 0;
LPWSTR* arguments = CommandLineToArgvW(GetCommandLineW(), &argumentCount);
if (arguments == nullptr)
{
return options;
}
bool removeSettingsSpecified = false;
for (int index = 1; index < argumentCount; ++index)
{
const std::wstring argument = arguments[index] != nullptr ? arguments[index] : L"";
if (IsSwitch(argument, {L"/quiet", L"-quiet", L"--quiet",
L"/silent", L"-silent", L"--silent",
L"/verysilent", L"-verysilent", L"--verysilent",
L"/S", L"-S"}))
{
options.silent = true;
}
else if (IsSwitch(argument, {L"/remove-settings", L"-remove-settings", L"--remove-settings"}))
{
options.removeSettings = true;
removeSettingsSpecified = true;
}
else if (IsSwitch(argument, {L"/keep-settings", L"-keep-settings", L"--keep-settings"}))
{
options.removeSettings = false;
removeSettingsSpecified = true;
}
}
LocalFree(arguments);
if (options.silent && !removeSettingsSpecified)
{
options.removeSettings = true;
}
return options;
}
std::filesystem::path FindInstalledDll(const std::filesystem::path& installDirectory)
{
const std::filesystem::path primary = installDirectory / L"Sumite-Desktop.dll";
if (std::filesystem::exists(primary))
{
return primary;
}
const std::filesystem::path fallback = installDirectory / L"TextService.dll";
if (std::filesystem::exists(fallback))
{
return fallback;
}
return std::filesystem::path();
}
std::wstring QuoteCommandLineArgument(const std::wstring& value)
{
std::wstring quoted;
quoted.reserve(value.size() + 2);
quoted.push_back(L'"');
for (wchar_t ch : value)
{
if (ch == L'"')
{
quoted.push_back(L'\\');
}
quoted.push_back(ch);
}
quoted.push_back(L'"');
return quoted;
}
std::filesystem::path GetTempDirectory()
{
std::wstring path(MAX_PATH, L'\0');
const DWORD length = GetTempPathW(static_cast<DWORD>(path.size()), &path[0]);
if (length == 0)
{
return std::filesystem::path();
}
if (length >= path.size())
{
path.assign(length + 1, L'\0');
if (GetTempPathW(static_cast<DWORD>(path.size()), &path[0]) == 0)
{
return std::filesystem::path();
}
}
path.resize(wcslen(path.c_str()));
return std::filesystem::path(path);
}
bool LaunchPostUninstallCleanup(const std::filesystem::path& installDirectory)
{
if (installDirectory.empty())
{
return false;
}
const std::filesystem::path tempDirectory = GetTempDirectory();
if (tempDirectory.empty())
{
return false;
}
const DWORD processId = GetCurrentProcessId();
const std::filesystem::path scriptPath =
tempDirectory / (L"sumire-cleanup-" + std::to_wstring(processId) + L".cmd");
std::wofstream script(scriptPath);
if (!script)
{
return false;
}
const std::wstring installPath = installDirectory.wstring();
script
<< L"@echo off\n"
<< L"set \"target=" << installPath << L"\"\n"
<< L"for /L %%i in (1,1,30) do (\n"
<< L" del /f /q \"%target%\\SumireUninstaller.exe\" >nul 2>nul\n"
<< L" rmdir \"%target%\" >nul 2>nul\n"
<< L" if not exist \"%target%\" goto done\n"
<< L" timeout /t 1 /nobreak >nul 2>nul\n"
<< L")\n"
<< L":done\n"
<< L"del /f /q \"%~f0\" >nul 2>nul\n";
script.close();
wchar_t comspec[MAX_PATH] = {};
DWORD comspecLength = GetEnvironmentVariableW(L"ComSpec", comspec, ARRAYSIZE(comspec));
std::wstring commandProcessor = (comspecLength > 0 && comspecLength < ARRAYSIZE(comspec))
? std::wstring(comspec)
: std::wstring(L"C:\\Windows\\System32\\cmd.exe");
std::wstring commandLine =
QuoteCommandLineArgument(commandProcessor) + L" /c " + QuoteCommandLineArgument(scriptPath.wstring());
STARTUPINFOW startupInfo = {};
startupInfo.cb = sizeof(startupInfo);
PROCESS_INFORMATION processInfo = {};
if (!CreateProcessW(
nullptr,
&commandLine[0],
nullptr,
nullptr,
FALSE,
CREATE_NO_WINDOW,
nullptr,
nullptr,
&startupInfo,
&processInfo))
{
DeleteFileW(scriptPath.c_str());
return false;
}
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
return true;
}
}
int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
UninstallOptions options = ParseUninstallOptions();
if (!options.silent)
{
const int settingsChoice = MessageBoxW(
nullptr,
L"Remove user settings as well?\nYes: remove settings\nNo: keep settings\nCancel: abort",
L"Sumire IME",
MB_ICONQUESTION | MB_YESNOCANCEL);
if (settingsChoice == IDCANCEL)
{
return 0;
}
options.removeSettings = settingsChoice == IDYES;
}
std::filesystem::path installDirectory = SumireInstallUtil::GetInstallDirectoryFromRegistry();
if (installDirectory.empty())
{
installDirectory = SumireInstallUtil::GetExecutableDirectory();
}
const std::filesystem::path installedDll = FindInstalledDll(installDirectory);
SumireInstallUtil::DeactivateTextServiceProfile();
if (!installedDll.empty())
{
SumireInstallUtil::UnregisterTextServiceDll(installedDll);
}
SumireInstallUtil::RemoveShortcut(SumireInstallUtil::GetStartMenuShortcutPath(L"Sumire Settings.lnk"));
SumireInstallUtil::RemoveShortcut(SumireInstallUtil::GetStartMenuShortcutPath(L"Uninstall Sumire IME.lnk"));
SumireInstallUtil::RemoveShortcut(SumireInstallUtil::GetDesktopShortcutPath(L"Sumire Settings.lnk"));
SumireInstallUtil::RemoveInstallMetadata();
if (options.removeSettings)
{
RegDeleteTreeW(HKEY_CURRENT_USER, SumireSettingsStore::GetRegistryPath().c_str());
}
bool rebootRequired = false;
SumireInstallUtil::DeleteDirectoryBestEffort(installDirectory, &rebootRequired);
bool cleanupLaunched = false;
std::error_code existsError;
if (std::filesystem::exists(installDirectory, existsError) && !existsError)
{
cleanupLaunched = LaunchPostUninstallCleanup(installDirectory);
if (!cleanupLaunched)
{
rebootRequired = true;
}
}
if (rebootRequired)
{
if (!options.silent)
{
SumireInstallUtil::ShowInfoMessage(
L"Uninstall completed, but some files are still in use and will be removed after sign-out or reboot.");
}
return ERROR_SUCCESS_REBOOT_REQUIRED;
}
if (!options.silent)
{
SumireInstallUtil::ShowInfoMessage(L"Sumire IME was uninstalled.");
}
return 0;
}