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
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
LPCWSTR vcsShortcutsQuery =
L"SELECT `Component_`, `Directory_`, `Name`, `Target`, `Attributes`, `IconFile`, `IconIndex` "
L"FROM `Wix4InternetShortcut`";
enum eShortcutsQuery { esqComponent = 1, esqDirectory, esqFilename, esqTarget, esqAttributes, esqIconFile, esqIconIndex };
enum eShortcutsAttributes { esaLink = 0, esaURL = 1 };
/******************************************************************
WixSchedInternetShortcuts - entry point
********************************************************************/
extern "C" UINT __stdcall WixSchedInternetShortcuts(
__in MSIHANDLE hInstall
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
UINT uiCost = 0;
PMSIHANDLE hView = NULL;
PMSIHANDLE hRec = NULL;
MSIHANDLE hCreateFolderTable = NULL;
MSIHANDLE hCreateFolderColumns = NULL;
LPWSTR pwzCustomActionData = NULL;
LPWSTR pwzComponent = NULL;
LPWSTR pwzDirectory = NULL;
LPWSTR pwzFilename = NULL;
LPWSTR pwzTarget = NULL;
LPWSTR pwzShortcutPath = NULL;
int iAttr = 0;
LPWSTR pwzIconFile = NULL;
int iIconIndex = 0;
IUniformResourceLocatorW* piURL = NULL;
IShellLinkW* piShellLink = NULL;
BOOL fInitializedCom = FALSE;
hr = WcaInitialize(hInstall, "WixSchedInternetShortcuts");
ExitOnFailure(hr, "failed to initialize WixSchedInternetShortcuts.");
// anything to do?
if (S_OK != WcaTableExists(L"Wix4InternetShortcut"))
{
WcaLog(LOGMSG_STANDARD, "Wix4InternetShortcut table doesn't exist, so there are no Internet shortcuts to process");
goto LExit;
}
// check to see if we can create a shortcut - Server Core and others may not have a shell registered.
hr = ::CoInitialize(NULL);
ExitOnFailure(hr, "failed to initialize COM");
fInitializedCom = TRUE;
hr = ::CoCreateInstance(CLSID_InternetShortcut, NULL, CLSCTX_ALL, IID_IUniformResourceLocatorW, (void**)&piURL);
if (S_OK != hr)
{
WcaLog(LOGMSG_STANDARD, "failed to create an instance of IUniformResourceLocatorW, skipping shortcut creation");
ExitFunction1(hr = S_OK);
}
hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLinkW, (void**)&piShellLink);
if (S_OK != hr)
{
WcaLog(LOGMSG_STANDARD, "failed to create an instance of IShellLinkW, skipping shortcut creation");
ExitFunction1(hr = S_OK);
}
// query and loop through all the shortcuts
hr = WcaOpenExecuteView(vcsShortcutsQuery, &hView);
ExitOnFailure(hr, "failed to open view on Wix4InternetShortcut table");
while (S_OK == (hr = WcaFetchRecord(hView, &hRec)))
{
// read column values
hr = WcaGetRecordString(hRec, esqComponent, &pwzComponent);
ExitOnFailure(hr, "failed to get shortcut component");
hr = WcaGetRecordString(hRec, esqDirectory, &pwzDirectory);
ExitOnFailure(hr, "failed to get shortcut directory");
hr = WcaGetRecordString(hRec, esqFilename, &pwzFilename);
ExitOnFailure(hr, "failed to get shortcut filename");
hr = WcaGetRecordFormattedString(hRec, esqTarget, &pwzTarget);
ExitOnFailure(hr, "failed to get shortcut target");
hr = WcaGetRecordInteger(hRec, esqAttributes, &iAttr);
ExitOnFailure(hr, "failed to get shortcut attributes");
hr = WcaGetRecordFormattedString(hRec, esqIconFile, &pwzIconFile);
ExitOnFailure(hr, "failed to get shortcut icon file");
hr = WcaGetRecordInteger(hRec, esqIconIndex, &iIconIndex);
ExitOnFailure(hr, "failed to get shortcut icon index");
// skip processing this Wix4InternetShortcut row if the component isn't being configured
WCA_TODO todo = WcaGetComponentToDo(pwzComponent);
if (WCA_TODO_UNKNOWN == todo)
{
WcaLog(LOGMSG_VERBOSE, "Skipping shortcut for null-action component '%ls'", pwzComponent);
continue;
}
// we need to create the directory where the shortcut is supposed to live; rather
// than doing so in our deferred custom action, use the CreateFolder table to have MSI
// make (and remove) them on our behalf (including the correct cleanup of parent directories).
MSIDBERROR dbError = MSIDBERROR_NOERROR;
WcaLog(LOGMSG_STANDARD, "Adding folder '%ls', component '%ls' to the CreateFolder table", pwzDirectory, pwzComponent);
hr = WcaAddTempRecord(&hCreateFolderTable, &hCreateFolderColumns, L"CreateFolder", &dbError, 0, 2, pwzDirectory, pwzComponent);
if (MSIDBERROR_DUPLICATEKEY == dbError)
{
WcaLog(LOGMSG_STANDARD, "Folder '%ls' already exists in the CreateFolder table; the above error is harmless", pwzDirectory);
hr = S_OK;
}
ExitOnFailure(hr, "Couldn't add temporary CreateFolder row");
// only if we're installing/reinstalling do we need to schedule the deferred CA
// (uninstallation is handled via permanent RemoveFile rows and temporary CreateFolder rows)
if (WCA_TODO_INSTALL == todo || WCA_TODO_REINSTALL == todo)
{
// turn the Directory_ id into a path
hr = WcaGetTargetPath(pwzDirectory, &pwzShortcutPath);
ExitOnFailure(hr, "failed to allocate string for shortcut directory");
// append the shortcut filename
hr = StrAllocConcat(&pwzShortcutPath, pwzFilename, 0);
ExitOnFailure(hr, "failed to allocate string for shortcut filename");
// write the shortcut path and target to custom action data for deferred CAs
hr = WcaWriteStringToCaData(pwzShortcutPath, &pwzCustomActionData);
ExitOnFailure(hr, "failed to write shortcut path to custom action data");
hr = WcaWriteStringToCaData(pwzTarget, &pwzCustomActionData);
ExitOnFailure(hr, "failed to write shortcut target to custom action data");
hr = WcaWriteIntegerToCaData(iAttr, &pwzCustomActionData);
ExitOnFailure(hr, "failed to write shortcut attributes to custom action data");
hr = WcaWriteStringToCaData(pwzIconFile, &pwzCustomActionData);
ExitOnFailure(hr, "failed to write icon file to custom action data");
hr = WcaWriteIntegerToCaData(iIconIndex, &pwzCustomActionData);
ExitOnFailure(hr, "failed to write icon index to custom action data");
uiCost += COST_INTERNETSHORTCUT;
}
}
if (E_NOMOREITEMS == hr)
{
hr = S_OK;
}
ExitOnFailure(hr, "Failure occured while processing Wix4InternetShortcut table");
// if we have any shortcuts to install
if (pwzCustomActionData && *pwzCustomActionData)
{
// add cost to progress bar
hr = WcaProgressMessage(uiCost, TRUE);
ExitOnFailure(hr, "failed to extend progress bar for InternetShortcuts");
// provide custom action data to deferred and rollback CAs
hr = WcaSetProperty(CUSTOM_ACTION_DECORATION(L"RollbackInternetShortcuts"), pwzCustomActionData);
ExitOnFailure(hr, "failed to set WixRollbackInternetShortcuts rollback custom action data");
hr = WcaSetProperty(CUSTOM_ACTION_DECORATION(L"CreateInternetShortcuts"), pwzCustomActionData);
ExitOnFailure(hr, "failed to set WixCreateInternetShortcuts custom action data");
}
LExit:
if (hCreateFolderTable)
{
::MsiCloseHandle(hCreateFolderTable);
}
if (hCreateFolderColumns)
{
::MsiCloseHandle(hCreateFolderColumns);
}
ReleaseStr(pwzCustomActionData);
ReleaseStr(pwzComponent);
ReleaseStr(pwzDirectory);
ReleaseStr(pwzFilename);
ReleaseStr(pwzTarget);
ReleaseStr(pwzShortcutPath);
ReleaseObject(piShellLink);
ReleaseObject(piURL);
if (fInitializedCom)
{
::CoUninitialize();
}
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}
/******************************************************************
CreateUrl - Creates a shortcut via IUniformResourceLocatorW
*******************************************************************/
static HRESULT CreateUrl(
__in_z LPCWSTR wzTarget,
__in_z LPCWSTR wzShortcutPath,
__in_z_opt LPCWSTR wzIconPath,
__in int iconIndex
)
{
HRESULT hr = S_OK;
IUniformResourceLocatorW* piURL = NULL;
IPersistFile* piPersistFile = NULL;
IPropertySetStorage* piProperties = NULL;
IPropertyStorage* piStorage = NULL;
// create an internet shortcut object
WcaLog(LOGMSG_STANDARD, "Creating IUniformResourceLocatorW shortcut '%ls' target '%ls'", wzShortcutPath, wzTarget);
hr = ::CoCreateInstance(CLSID_InternetShortcut, NULL, CLSCTX_ALL, IID_IUniformResourceLocatorW, (void**)&piURL);
ExitOnFailure(hr, "failed to create an instance of IUniformResourceLocatorW");
// set shortcut target
hr = piURL->SetURL(wzTarget, 0);
ExitOnFailure(hr, "failed to set shortcut '%ls' target '%ls'", wzShortcutPath, wzTarget);
if (wzIconPath)
{
WcaLog(LOGMSG_STANDARD, "Adding icon '%ls' index '%d'", wzIconPath, iconIndex);
hr = piURL->QueryInterface(IID_IPropertySetStorage, (void **)&piProperties);
ExitOnFailure(hr, "failed to get IPropertySetStorage for shortcut '%ls'", wzShortcutPath);
hr = piProperties->Open(FMTID_Intshcut, STGM_WRITE, &piStorage);
ExitOnFailure(hr, "failed to open storage for shortcut '%ls'", wzShortcutPath);
PROPSPEC ppids[2] = { {PRSPEC_PROPID, PID_IS_ICONINDEX}, {PRSPEC_PROPID, PID_IS_ICONFILE} };
PROPVARIANT ppvar[2];
PropVariantInit(ppvar);
PropVariantInit(ppvar + 1);
ppvar[0].vt = VT_I4;
ppvar[0].lVal = iconIndex;
ppvar[1].vt = VT_LPWSTR;
ppvar[1].pwszVal = const_cast<LPWSTR>(wzIconPath);
hr = piStorage->WriteMultiple(2, ppids, ppvar, 0);
ExitOnFailure(hr, "failed to write icon storage for shortcut '%ls'", wzShortcutPath);
hr = piStorage->Commit(STGC_DEFAULT);
ExitOnFailure(hr, "failed to commit icon storage for shortcut '%ls'", wzShortcutPath);
}
// get an IPersistFile and save the shortcut
hr = piURL->QueryInterface(IID_IPersistFile, (void**)&piPersistFile);
ExitOnFailure(hr, "failed to get IPersistFile for shortcut '%ls'", wzShortcutPath);
hr = piPersistFile->Save(wzShortcutPath, TRUE);
ExitOnFailure(hr, "failed to save shortcut '%ls'", wzShortcutPath);
LExit:
ReleaseObject(piPersistFile);
ReleaseObject(piURL);
ReleaseObject(piStorage);
ReleaseObject(piProperties);
return hr;
}
/******************************************************************
CreateLink - Creates a shortcut via IShellLinkW
*******************************************************************/
static HRESULT CreateLink(
__in_z LPCWSTR wzTarget,
__in_z LPCWSTR wzShortcutPath,
__in_z_opt LPCWSTR wzIconPath,
__in int iconIndex
)
{
HRESULT hr = S_OK;
IShellLinkW* piShellLink = NULL;
IPersistFile* piPersistFile = NULL;
// create an internet shortcut object
WcaLog(LOGMSG_STANDARD, "Creating IShellLinkW shortcut '%ls' target '%ls'", wzShortcutPath, wzTarget);
hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLinkW, (void**)&piShellLink);
ExitOnFailure(hr, "failed to create an instance of IShellLinkW");
// set shortcut target
hr = piShellLink->SetPath(wzTarget);
ExitOnFailure(hr, "failed to set shortcut '%ls' target '%ls'", wzShortcutPath, wzTarget);
if (wzIconPath)
{
WcaLog(LOGMSG_STANDARD, "Adding icon '%ls' index '%d'", wzIconPath, iconIndex);
hr = piShellLink->SetIconLocation(wzIconPath, iconIndex);
ExitOnFailure(hr, "failed to set icon for shortcut '%ls'", wzShortcutPath);
}
// get an IPersistFile and save the shortcut
hr = piShellLink->QueryInterface(IID_IPersistFile, (void**)&piPersistFile);
ExitOnFailure(hr, "failed to get IPersistFile for shortcut '%ls'", wzShortcutPath);
hr = piPersistFile->Save(wzShortcutPath, TRUE);
ExitOnFailure(hr, "failed to save shortcut '%ls'", wzShortcutPath);
LExit:
ReleaseObject(piPersistFile);
ReleaseObject(piShellLink);
return hr;
}
/******************************************************************
WixCreateInternetShortcuts - entry point for Internet shortcuts
custom action
*******************************************************************/
extern "C" UINT __stdcall WixCreateInternetShortcuts(
__in MSIHANDLE hInstall
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
LPWSTR pwz = NULL;
LPWSTR pwzCustomActionData = NULL;
LPWSTR pwzTarget = NULL;
LPWSTR pwzShortcutPath = NULL;
LPWSTR pwzIconPath = NULL;
BOOL fInitializedCom = FALSE;
int iAttr = 0;
int iIconIndex = 0;
// initialize
hr = WcaInitialize(hInstall, "WixCreateInternetShortcuts");
ExitOnFailure(hr, "failed to initialize WixCreateInternetShortcuts");
hr = ::CoInitialize(NULL);
ExitOnFailure(hr, "failed to initialize COM");
fInitializedCom = TRUE;
// extract the custom action data
hr = WcaGetProperty(L"CustomActionData", &pwzCustomActionData);
ExitOnFailure(hr, "failed to get CustomActionData");
// loop through all the custom action data
pwz = pwzCustomActionData;
while (pwz && *pwz)
{
hr = WcaReadStringFromCaData(&pwz, &pwzShortcutPath);
ExitOnFailure(hr, "failed to read shortcut path from custom action data");
hr = WcaReadStringFromCaData(&pwz, &pwzTarget);
ExitOnFailure(hr, "failed to read shortcut target from custom action data");
hr = WcaReadIntegerFromCaData(&pwz, &iAttr);
ExitOnFailure(hr, "failed to read shortcut attributes from custom action data");
hr = WcaReadStringFromCaData(&pwz, &pwzIconPath);
ExitOnFailure(hr, "failed to read shortcut icon path from custom action data");
hr = WcaReadIntegerFromCaData(&pwz, &iIconIndex);
ExitOnFailure(hr, "failed to read shortcut icon index from custom action data");
if ((iAttr & esaURL) == esaURL)
{
hr = CreateUrl(pwzTarget, pwzShortcutPath, pwzIconPath, iIconIndex);
}
else
{
hr = CreateLink(pwzTarget, pwzShortcutPath, pwzIconPath, iIconIndex);
}
ExitOnFailure(hr, "failed to create Internet shortcut");
// tick the progress bar
hr = WcaProgressMessage(COST_INTERNETSHORTCUT, FALSE);
ExitOnFailure(hr, "failed to tick progress bar for shortcut: %ls", pwzShortcutPath);
}
LExit:
ReleaseStr(pwzCustomActionData);
ReleaseStr(pwzTarget);
ReleaseStr(pwzShortcutPath);
if (fInitializedCom)
{
::CoUninitialize();
}
er = FAILED(hr) ? ERROR_INSTALL_FAILURE : er;
return WcaFinalize(er);
}
/******************************************************************
WixRollbackInternetShortcuts - entry point for Internet shortcuts
custom action (rollback)
*******************************************************************/
extern "C" UINT __stdcall WixRollbackInternetShortcuts(
__in MSIHANDLE hInstall
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
LPWSTR pwz = NULL;
LPWSTR pwzCustomActionData = NULL;
LPWSTR pwzShortcutPath = NULL;
int iAttr = 0;
// initialize
hr = WcaInitialize(hInstall, "WixRemoveInternetShortcuts");
ExitOnFailure(hr, "failed to initialize WixRemoveInternetShortcuts");
hr = WcaGetProperty(L"CustomActionData", &pwzCustomActionData);
ExitOnFailure(hr, "failed to get CustomActionData");
// loop through all the custom action data
pwz = pwzCustomActionData;
while (pwz && *pwz)
{
// extract the custom action data we're interested in
hr = WcaReadStringFromCaData(&pwz, &pwzShortcutPath);
ExitOnFailure(hr, "failed to read shortcut path from custom action data for rollback");
// delete file
hr = FileEnsureDelete(pwzShortcutPath);
ExitOnFailure(hr, "failed to delete file '%ls'", pwzShortcutPath);
// skip over the shortcut target and attributes
hr = WcaReadStringFromCaData(&pwz, &pwzShortcutPath);
ExitOnFailure(hr, "failed to skip shortcut target from custom action data for rollback");
hr = WcaReadIntegerFromCaData(&pwz, &iAttr);
ExitOnFailure(hr, "failed to read shortcut attributes from custom action data");
}
LExit:
ReleaseStr(pwzCustomActionData);
ReleaseStr(pwzShortcutPath);
er = FAILED(hr) ? ERROR_INSTALL_FAILURE : er;
return WcaFinalize(er);
}
|