forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
303 lines (255 loc) · 9.44 KB
/
Copy pathmain.cpp
File metadata and controls
303 lines (255 loc) · 9.44 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <windows.h>
#include <winreg.h>
#include <winerror.h>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <sstream>
using namespace std::filesystem;
std::wstring_view RegistrySubkey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
std::wstring_view DefaultProductID = L"{A499DD5E-8DC5-4AD2-911A-BCD0263295E9}";
std::wstring_view DefaultDisplayName = L"AppInstallerTestExeInstaller";
std::wstring_view DefaultDisplayVersion = L"1.0.0.0";
path GenerateUninstaller(std::wostream& out, const path& installDirectory, const std::wstring& productID, bool useHKLM)
{
path uninstallerPath = installDirectory;
uninstallerPath /= "UninstallTestExe.bat";
out << "Uninstaller located at path: " << uninstallerPath << std::endl;
path uninstallerOutputTextFilePath = installDirectory;
uninstallerOutputTextFilePath /= "TestExeUninstalled.txt";
std::wstring registryKey{ useHKLM ? L"HKEY_LOCAL_MACHINE\\" : L"HKEY_CURRENT_USER\\" };
registryKey += RegistrySubkey;
if (!productID.empty())
{
registryKey += productID;
}
else
{
registryKey += DefaultProductID;
}
std::wofstream uninstallerScript(uninstallerPath);
uninstallerScript << "@echo off\n";
uninstallerScript << "ECHO. >" << uninstallerOutputTextFilePath << "\n";
uninstallerScript << "ECHO AppInstallerTestExeInstaller.exe uninstalled successfully.\n";
uninstallerScript << "REG DELETE " << registryKey << " /f\n";
uninstallerScript.close();
return uninstallerPath;
}
void WriteToUninstallRegistry(
std::wostream& out,
const std::wstring& productID,
const path& uninstallerPath,
const std::wstring& displayName,
const std::wstring& displayVersion,
const std::wstring& installLocation,
bool useHKLM)
{
HKEY hkey;
LONG lReg;
// String inputs to registry must be of wide char type
const wchar_t* publisher = L"Microsoft Corporation";
std::wstring uninstallString = uninstallerPath.wstring();
DWORD version = 1;
std::wstring registryKey{ RegistrySubkey };
if (!productID.empty())
{
registryKey += productID;
out << "Product Code overridden to: " << registryKey << std::endl;
}
else
{
registryKey += DefaultProductID;
out << "Default Product Code used: " << registryKey << std::endl;
}
lReg = RegCreateKeyEx(
useHKLM ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
registryKey.c_str(),
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hkey,
NULL);
if (lReg == ERROR_SUCCESS)
{
out << "Successfully opened registry key" << std::endl;
// Set Display Name Property Value
if (LONG res = RegSetValueEx(hkey, L"DisplayName", NULL, REG_SZ, (LPBYTE)displayName.c_str(), (DWORD)(displayName.length() + 1) * sizeof(wchar_t)) != ERROR_SUCCESS)
{
out << "Failed to write DisplayName value. Error Code: " << res << std::endl;
}
// Set Display Version Property Value
if (LONG res = RegSetValueEx(hkey, L"DisplayVersion", NULL, REG_SZ, (LPBYTE)displayVersion.c_str(), (DWORD)(displayVersion.length() + 1) * sizeof(wchar_t)) != ERROR_SUCCESS)
{
out << "Failed to write DisplayVersion value. Error Code: " << res << std::endl;
}
// Set Publisher Property Value
if (LONG res = RegSetValueEx(hkey, L"Publisher", NULL, REG_SZ, (LPBYTE)publisher, (DWORD)(wcslen(publisher) + 1) * sizeof(wchar_t)) != ERROR_SUCCESS)
{
out << "Failed to write Publisher value. Error Code: " << res << std::endl;
}
// Set UninstallString Property Value
if (LONG res = RegSetValueEx(hkey, L"UninstallString", NULL, REG_EXPAND_SZ, (LPBYTE)uninstallString.c_str(), (DWORD)(uninstallString.length() + 1) * sizeof(wchar_t)) != ERROR_SUCCESS)
{
out << "Failed to write UninstallString value. Error Code: " << res << std::endl;
}
// Set Version Property Value
if (LONG res = RegSetValueEx(hkey, L"Version", NULL, REG_DWORD, (LPBYTE)&version, sizeof(version)) != ERROR_SUCCESS)
{
out << "Failed to write Version value. Error Code: " << res << std::endl;
}
// Set InstallLocation Property Value
if (LONG res = RegSetValueEx(hkey, L"InstallLocation", NULL, REG_SZ, (LPBYTE)installLocation.c_str(), (DWORD)(installLocation.length() + 1) * sizeof(wchar_t)) != ERROR_SUCCESS)
{
out << "Failed to write InstallLocation value. Error Code: " << res << std::endl;
}
out << "Write to registry key completed" << std::endl;
}
else {
out << "Key Creation Failed" << std::endl;
}
RegCloseKey(hkey);
}
// The installer prints all args to an output file and writes to the Uninstall registry key
int wmain(int argc, const wchar_t** argv)
{
path installDirectory = temp_directory_path();
std::wstringstream outContent;
std::wstring productCode;
std::wstring displayName;
std::wstring displayVersion;
std::wstring aliasToExecute;
std::wstring aliasArguments;
bool useHKLM = false;
bool noOperation = false;
int exitCode = 0;
// Output to cout by default, but swap to a file if requested
std::wostream* out = &std::wcout;
std::wofstream logFile;
for (int i = 1; i < argc; i++)
{
outContent << argv[i] << ' ';
// Supports custom install path.
if (_wcsicmp(argv[i], L"/InstallDir") == 0)
{
if (++i < argc)
{
installDirectory = argv[i];
outContent << argv[i] << ' ';
}
}
// Supports custom exit code
else if (_wcsicmp(argv[i], L"/ExitCode") == 0)
{
if (++i < argc)
{
exitCode = static_cast<int>(std::stoll(argv[i], 0, 0));
outContent << argv[i] << ' ';
}
}
// Supports custom product code ID
else if (_wcsicmp(argv[i], L"/ProductID") == 0)
{
if (++i < argc)
{
productCode = argv[i];
outContent << argv[i] << ' ';
}
}
// Supports custom DisplayName
else if (_wcsicmp(argv[i], L"/DisplayName") == 0)
{
if (++i < argc)
{
displayName = argv[i];
outContent << argv[i] << ' ';
}
}
// Supports custom version
else if (_wcsicmp(argv[i], L"/Version") == 0)
{
if (++i < argc)
{
displayVersion = argv[i];
outContent << argv[i] << ' ';
}
}
// Supports log file
else if (_wcsicmp(argv[i], L"/LogFile") == 0)
{
if (++i < argc)
{
logFile = std::wofstream(argv[i], std::wofstream::out | std::wofstream::trunc);
out = &logFile;
outContent << argv[i] << ' ';
}
}
// Writes to HKLM
else if (_wcsicmp(argv[i], L"/UseHKLM") == 0)
{
useHKLM = true;
}
// Executes a command alias during installation
else if (_wcsicmp(argv[i], L"/AliasToExecute") == 0)
{
if (++i < argc)
{
aliasToExecute = argv[i];
outContent << argv[i] << ' ';
}
}
// Additional arguments to include when executing the command alias during installation
else if (_wcsicmp(argv[i], L"/AliasArguments") == 0)
{
if (++i < argc)
{
aliasArguments = argv[i];
outContent << argv[i] << ' ';
}
}
// Returns the success exit code to emulate being invoked by another caller.
else if (_wcsicmp(argv[i], L"/NoOperation") == 0)
{
noOperation = true;
}
}
if (noOperation)
{
return exitCode;
}
if (!aliasToExecute.empty())
{
SHELLEXECUTEINFOW execInfo = { 0 };
execInfo.cbSize = sizeof(execInfo);
execInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
execInfo.lpFile = aliasToExecute.c_str();
if (!aliasArguments.empty())
{
execInfo.lpParameters = aliasArguments.c_str();
}
execInfo.nShow = SW_SHOW;
if (!ShellExecuteExW(&execInfo) || !execInfo.hProcess)
{
return -1;
}
}
if (displayName.empty())
{
displayName = DefaultDisplayName;
}
if (displayVersion.empty())
{
displayVersion = DefaultDisplayVersion;
}
path outFilePath = installDirectory;
outFilePath /= "TestExeInstalled.txt";
std::wofstream file(outFilePath, std::ofstream::out);
file << outContent.str();
file.close();
path uninstallerPath = GenerateUninstaller(*out, installDirectory, productCode, useHKLM);
WriteToUninstallRegistry(*out, productCode, uninstallerPath, displayName, displayVersion, installDirectory.wstring(), useHKLM);
return exitCode;
}