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 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <iomanip>
#include <memory>
#include <string>
#include <sstream>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Shlobj.h>
#include <msiquery.h>
namespace
{
template <typename IntType> std::string Num2Hex(IntType n)
{
std::stringstream sMsg;
sMsg << "0x" << std::uppercase << std::setfill('0') << std::setw(sizeof(n) * 2) << std::hex
<< n;
return sMsg.str();
}
template <typename IntType> std::string Num2Dec(IntType n)
{
std::stringstream sMsg;
sMsg << n;
return sMsg.str();
}
std::string Win32ErrorMessage(const char* sFunc, DWORD nWin32Error)
{
std::stringstream sMsg;
sMsg << sFunc << " failed with Win32 error code " << Num2Hex(nWin32Error) << "!";
return sMsg.str();
}
void ThrowHResult(const char* sFunc, HRESULT hr)
{
std::stringstream sMsg;
sMsg << sFunc << " failed (HRESULT = " << Num2Hex(hr) << ")!";
throw std::exception(sMsg.str().c_str());
}
void CheckHResult(const char* sFunc, HRESULT hr)
{
if (FAILED(hr))
ThrowHResult(sFunc, hr);
}
void ThrowWin32Error(const char* sFunc, DWORD nWin32Error)
{
throw std::exception(Win32ErrorMessage(sFunc, nWin32Error).c_str());
}
void ThrowLastError(const char* sFunc) { ThrowWin32Error(sFunc, GetLastError()); }
void CheckWin32Error(const char* sFunc, DWORD nWin32Error)
{
if (nWin32Error != ERROR_SUCCESS)
ThrowWin32Error(sFunc, nWin32Error);
}
std::wstring GetKnownFolder(const KNOWNFOLDERID& rfid)
{
PWSTR sPath = nullptr;
HRESULT hr = SHGetKnownFolderPath(rfid, KF_FLAG_DEFAULT, nullptr, &sPath);
CheckHResult("SHGetKnownFolderPath", hr);
std::wstring sResult(sPath);
CoTaskMemFree(sPath);
return sResult;
}
void WriteLogElem(MSIHANDLE hInst, MSIHANDLE hRecord, std::ostringstream& sTmpl, UINT)
{
MsiRecordSetStringA(hRecord, 0, sTmpl.str().c_str());
MsiProcessMessage(hInst, INSTALLMESSAGE_INFO, hRecord);
}
void RecSetString(MSIHANDLE hRec, UINT nField, LPCSTR sVal)
{
MsiRecordSetStringA(hRec, nField, sVal);
}
void RecSetString(MSIHANDLE hRec, UINT nField, LPCWSTR sVal)
{
MsiRecordSetStringW(hRec, nField, sVal);
}
template <class Ch, class... SOther>
void WriteLogElem(MSIHANDLE hInst, MSIHANDLE hRec, std::ostringstream& sTmpl, UINT nField,
const Ch* elem, const SOther&... others)
{
sTmpl << " [" << nField << "]";
RecSetString(hRec, nField, elem);
WriteLogElem(hInst, hRec, sTmpl, nField + 1, others...);
}
template <class S1, class... SOther>
void WriteLogElem(MSIHANDLE hInst, MSIHANDLE hRec, std::ostringstream& sTmpl, UINT nField,
const S1& elem, const SOther&... others)
{
WriteLogElem(hInst, hRec, sTmpl, nField, elem.c_str(), others...);
}
static std::string sLogPrefix;
template <class... StrType> void WriteLog(MSIHANDLE hInst, const StrType&... elements)
{
PMSIHANDLE hRec = MsiCreateRecord(sizeof...(elements));
if (!hRec)
return;
std::ostringstream sTemplate;
sTemplate << sLogPrefix;
WriteLogElem(hInst, hRec, sTemplate, 1, elements...);
}
std::wstring MsiGetPropertyW(MSIHANDLE hInst, LPCWSTR szName)
{
std::wstring sResult;
DWORD nSz = 0;
UINT nRet = ::MsiGetPropertyW(hInst, szName, const_cast<wchar_t*>(L""), &nSz);
if (nRet == ERROR_MORE_DATA)
{
++nSz;
auto buf = std::make_unique<wchar_t[]>(nSz);
CheckWin32Error("MsiGetPropertyW", ::MsiGetPropertyW(hInst, szName, buf.get(), &nSz));
sResult = buf.get();
WriteLog(hInst, "Property", szName, "=", sResult);
}
else
CheckWin32Error("MsiGetPropertyW", nRet);
return sResult;
}
typedef std::unique_ptr<void, decltype(&CloseHandle)> CloseHandleGuard;
CloseHandleGuard Guard(HANDLE h) { return CloseHandleGuard(h, CloseHandle); }
void RegDLL(MSIHANDLE hInst, const std::wstring& sArgs, bool bUnreg)
{
static std::wstring sRegSvr32 = GetKnownFolder(FOLDERID_System) + L"\\regsvr32.exe";
try
{
std::wstring sCmd = L"\"" + sRegSvr32 + L"\" /s ";
if (bUnreg)
sCmd += L"/u ";
sCmd += sArgs;
WriteLog(hInst, "Prepared regsvr32 command:", sCmd);
STARTUPINFOW si{};
si.cb = sizeof(si);
PROCESS_INFORMATION pi{};
if (!CreateProcessW(sRegSvr32.c_str(), const_cast<LPWSTR>(sCmd.c_str()), nullptr, nullptr,
FALSE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi))
ThrowLastError("CreateProcessW");
auto aCloseProcHandleGuard(Guard(pi.hProcess));
WriteLog(hInst, "CreateProcessW succeeded");
DWORD nWaitResult = WaitForSingleObject(pi.hProcess, INFINITE);
if (nWaitResult != WAIT_OBJECT_0)
ThrowWin32Error("WaitForSingleObject", nWaitResult);
DWORD nExitCode = 0;
if (!GetExitCodeProcess(pi.hProcess, &nExitCode))
ThrowLastError("GetExitCodeProcess");
WriteLog(hInst, "regsvr32 returned:", Num2Dec(nExitCode));
}
catch (std::exception& e)
{
WriteLog(hInst, e.what());
}
}
void ProcessCustomActionData(MSIHANDLE hInst, bool bUnreg)
{
WriteLog(hInst, "Checking value of CustomActionData");
std::wstring sCustomActionData = MsiGetPropertyW(hInst, L"CustomActionData");
WriteLog(hInst, "Got CustomActionData value:", sCustomActionData);
std::wstringstream ss(sCustomActionData);
std::wstring sToken;
while (std::getline(ss, sToken, L'|'))
{
if (!sToken.empty())
{
RegDLL(hInst, sToken, bUnreg);
}
}
}
} // namespace
// Deferred action "reg_dlls" that must be run from system account. Receives a list of regsvr32
// arguments: DLLs which need registering, and possibly /i argument with its parameter.
extern "C" __declspec(dllexport) UINT __stdcall RegDLLs(MSIHANDLE hInstall)<--- The function 'RegDLLs' is never used.
{
sLogPrefix = "RegDLLs:";
WriteLog(hInstall, "started");
ProcessCustomActionData(hInstall, false);
return ERROR_SUCCESS;
}
// Deferred action "unreg_dlls" that must be run from system account. Receives a list of regsvr32
// arguments: DLLs which need registering, and possibly /i argument with its parameter.
extern "C" __declspec(dllexport) UINT __stdcall UnregDLLs(MSIHANDLE hInstall)<--- The function 'UnregDLLs' is never used.
{
sLogPrefix = "UnregDLLs:";
WriteLog(hInstall, "started");
ProcessCustomActionData(hInstall, true);
return ERROR_SUCCESS;
}
// Immediate action "prep_reg_unreg_dlls". Checks states of the features to prepare custom action data
// for reg_dlls and unreg_dlls deferred actions.
extern "C" __declspec(dllexport) UINT __stdcall PrepRegUnregDLLs(MSIHANDLE hInstall)<--- The function 'PrepRegUnregDLLs' is never used.
{
sLogPrefix = "PrepRegUnregDLLs:";
WriteLog(hInstall, "started");
try
{
INSTALLSTATE current_state_SubstMSO;
INSTALLSTATE future_state_SubstMSO;
CheckWin32Error("MsiGetFeatureStateW",
MsiGetFeatureStateW(hInstall, L"gm_SharePointSupport_SubstMSO",
¤t_state_SubstMSO, &future_state_SubstMSO));
WriteLog(hInstall, "gm_SharePointSupport_SubstMSO state:", //
"current", std::to_string(current_state_SubstMSO), //
"future", std::to_string(future_state_SubstMSO)); //
INSTALLSTATE current_state_Main;
INSTALLSTATE future_state_Main;
CheckWin32Error("MsiGetFeatureStateW",
MsiGetFeatureStateW(hInstall, L"gm_o_SharePointSupport",
¤t_state_Main, &future_state_Main));
WriteLog(hInstall, "gm_o_SharePointSupport state:", //
"current", std::to_string(current_state_Main), //
"future", std::to_string(future_state_Main)); //
const bool bUnregSubstMSO = current_state_SubstMSO == INSTALLSTATE_LOCAL
&& future_state_SubstMSO == INSTALLSTATE_ABSENT;
const bool bUnregMain
= current_state_Main == INSTALLSTATE_LOCAL && future_state_Main == INSTALLSTATE_ABSENT;
const bool bRegSubstMSO = current_state_SubstMSO == INSTALLSTATE_ABSENT
&& future_state_SubstMSO == INSTALLSTATE_LOCAL;
// basic registration is needed when either:
// 1. gm_o_SharePointSupport is installed;
// 2. gm_SharePointSupport_SubstMSO is uninstalled (and unregisters everything), but
// gm_o_SharePointSupport is not, so needs to be re-registered
const bool bRegMain
= (current_state_Main == INSTALLSTATE_ABSENT && future_state_Main == INSTALLSTATE_LOCAL)
|| (bUnregSubstMSO && !bUnregMain);
std::wstring sUnregStr;
if (bUnregSubstMSO)
{
sUnregStr = L"/i:Substitute_OWSSUPP \"[#spsupp_x86.dll]\"|"
L"/i:Substitute_OWSSUPP \"[#spsupp_x64.dll]\"";
}
else if (bUnregMain)
{
sUnregStr = L"\"[#spsupp_x86.dll]\"|\"[#spsupp_x64.dll]\"";
}
std::wstring sRegStr;
if (bRegSubstMSO)
{
sRegStr = L"/i:Substitute_OWSSUPP \"[#spsupp_x86.dll]\"|"
L"/i:Substitute_OWSSUPP \"[#spsupp_x64.dll]\"";
}
else if (bRegMain)
{
sRegStr = L"\"[#spsupp_x86.dll]\"|\"[#spsupp_x64.dll]\"";
}
auto SetFormattedPropW = [&](LPCWSTR sProp, const std::wstring& sVal) {
PMSIHANDLE hRec = MsiCreateRecord(0);
if (!hRec)
throw std::exception("MsiCreateRecord failed!");
MsiRecordSetStringW(hRec, 0, sVal.c_str());
DWORD nSz = 0;
if (MsiFormatRecordW(hInstall, hRec, const_cast<wchar_t*>(L""), &nSz)
== ERROR_MORE_DATA)
{
++nSz;
auto buf = std::make_unique<wchar_t[]>(nSz);
CheckWin32Error("MsiFormatRecordW",
MsiFormatRecordW(hInstall, hRec, buf.get(), &nSz));
CheckWin32Error("MsiSetPropertyW", MsiSetPropertyW(hInstall, sProp, buf.get()));
}
};
if (!sRegStr.empty())
SetFormattedPropW(L"reg_dlls", sRegStr);
if (!sUnregStr.empty())
SetFormattedPropW(L"unreg_dlls", sUnregStr);
}
catch (std::exception& e)
{
WriteLog(hInstall, e.what());
}
return ERROR_SUCCESS;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|