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
211 lines (174 loc) · 6.63 KB
/
Copy pathmain.cpp
File metadata and controls
211 lines (174 loc) · 6.63 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
// 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 defaultVersion = L"1.0.0.0";
path GenerateUninstaller(std::wostream& out, const path& installDirectory, const std::wstring& productID)
{
path uninstallerPath = installDirectory;
uninstallerPath /= "UninstallTestExe.bat";
out << "Uninstaller located at path: " << uninstallerPath << std::endl;
path uninstallerOutputTextFilePath = installDirectory;
uninstallerOutputTextFilePath /= "TestExeUninstalled.txt";
std::wstring registryKey{ 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& displayVersion)
{
HKEY hkey;
LONG lReg;
// String inputs to registry must be of wide char type
const wchar_t* displayName = L"AppInstallerTestExeInstaller";
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(
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, (DWORD)(wcslen(displayName) + 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;
}
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 version;
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];
}
}
// Supports custom version
else if (_wcsicmp(argv[i], L"/Version") == 0)
{
if (++i < argc)
{
version = 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;
}
}
}
if (version.empty())
{
version = defaultVersion;
}
path outFilePath = installDirectory;
outFilePath /= "TestExeInstalled.txt";
std::wofstream file(outFilePath, std::ofstream::out);
file << outContent.str();
file.close();
path uninstallerPath = GenerateUninstaller(*out, installDirectory, productCode);
WriteToUninstallRegistry(*out, productCode, uninstallerPath, version);
return exitCode;
}