-
Notifications
You must be signed in to change notification settings - Fork 643
/
CloseApps.cpp
570 lines (473 loc) · 18.7 KB
/
CloseApps.cpp
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// 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"
#define DEFAULT_PROCESS_EXIT_WAIT_TIME 5000
// WixCloseApplication Target Description Condition Attributes Sequence
// structs
LPCWSTR wzQUERY_CLOSEAPPS = L"SELECT `WixCloseApplication`, `Target`, `Description`, `Condition`, `Attributes`, `Property`, `TerminateExitCode`, `Timeout` FROM `WixCloseApplication` ORDER BY `Sequence`";
enum eQUERY_CLOSEAPPS { QCA_ID = 1, QCA_TARGET, QCA_DESCRIPTION, QCA_CONDITION, QCA_ATTRIBUTES, QCA_PROPERTY, QCA_TERMINATEEXITCODE, QCA_TIMEOUT };
// CloseApplication.Attributes
enum CLOSEAPP_ATTRIBUTES
{
CLOSEAPP_ATTRIBUTE_NONE = 0x0,
CLOSEAPP_ATTRIBUTE_CLOSEMESSAGE = 0x1,
CLOSEAPP_ATTRIBUTE_REBOOTPROMPT = 0x2,
CLOSEAPP_ATTRIBUTE_ELEVATEDCLOSEMESSAGE = 0x4,
CLOSEAPP_ATTRIBUTE_ENDSESSIONMESSAGE = 0x8,
CLOSEAPP_ATTRIBUTE_ELEVATEDENDSESSIONMESSAGE = 0x10,
CLOSEAPP_ATTRIBUTE_TERMINATEPROCESS = 0x20,
CLOSEAPP_ATTRIBUTE_PROMPTTOCONTINUE = 0x40,
};
struct PROCESS_AND_MESSAGE
{
DWORD dwProcessId;
DWORD dwMessageId;
DWORD dwTimeout;
};
/******************************************************************
EnumWindowsProc - callback function which sends message if the
current window matches the passed in process ID
******************************************************************/
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
PROCESS_AND_MESSAGE* pPM = reinterpret_cast<PROCESS_AND_MESSAGE*>(lParam);
DWORD dwProcessId = 0;
DWORD_PTR dwResult = 0;
BOOL fQueryEndSession = WM_QUERYENDSESSION == pPM->dwMessageId;
BOOL fContinueWindowsInProcess = TRUE; // assume we will send message to all top-level windows in a process.
::GetWindowThreadProcessId(hwnd, &dwProcessId);
// check if the process Id is the one we're looking for
if (dwProcessId != pPM->dwProcessId)
{
return TRUE;
}
WcaLog(LOGMSG_VERBOSE, "Sending message to process id 0x%x", dwProcessId);
if (::SendMessageTimeoutW(hwnd, pPM->dwMessageId, 0, fQueryEndSession ? ENDSESSION_CLOSEAPP : 0, SMTO_BLOCK, pPM->dwTimeout, &dwResult))
{
WcaLog(LOGMSG_VERBOSE, "Result 0x%x", dwResult);
if (fQueryEndSession)
{
// If application said it was okay to close, do that.
if (dwResult)
{
::SendMessageTimeoutW(hwnd, WM_ENDSESSION, TRUE, ENDSESSION_CLOSEAPP, SMTO_BLOCK, pPM->dwTimeout, &dwResult);
}
else // application said don't try to close it, so don't bother sending messages to any other top-level windows.
{
fContinueWindowsInProcess = FALSE;
}
}
}
else // log result message.
{
WcaLog(LOGMSG_VERBOSE, "Failed to send message id: %u, error: 0x%x", pPM->dwMessageId, ::GetLastError());
}
// so we know we succeeded
::SetLastError(ERROR_SUCCESS);
return fContinueWindowsInProcess;
}
/******************************************************************
PromptToContinue - displays the prompt if the application is still
running.
******************************************************************/
static HRESULT PromptToContinue(
__in_z LPCWSTR wzApplication,
__in_z LPCWSTR wzPrompt
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
PMSIHANDLE hRecMessage = NULL;
DWORD *prgProcessIds = NULL;
DWORD cProcessIds = 0;
hRecMessage = ::MsiCreateRecord(1);
ExitOnNull(hRecMessage, hr, E_OUTOFMEMORY, "Failed to create record for prompt.");
er = ::MsiRecordSetStringW(hRecMessage, 0, wzPrompt);
ExitOnWin32Error(er, hr, "Failed to set prompt record field string");
do
{
hr = ProcFindAllIdsFromExeName(wzApplication, &prgProcessIds, &cProcessIds);
if (SUCCEEDED(hr) && 0 < cProcessIds)
{
er = WcaProcessMessage(static_cast<INSTALLMESSAGE>(INSTALLMESSAGE_WARNING | MB_ABORTRETRYIGNORE | MB_DEFBUTTON3 | MB_ICONWARNING), hRecMessage);
if (IDABORT == er)
{
hr = HRESULT_FROM_WIN32(ERROR_INSTALL_USEREXIT);
}
else if (IDRETRY == er)
{
hr = S_FALSE;
}
else if (IDIGNORE == er)
{
hr = S_OK;
}
else
{
ExitOnWin32Error(er, hr, "Unexpected return value from prompt to continue.");
}
}
ReleaseNullMem(prgProcessIds);
cProcessIds = 0;
} while (S_FALSE == hr);
LExit:
ReleaseMem(prgProcessIds);
return hr;
}
/******************************************************************
SendProcessMessage - helper function to enumerate the top-level
windows and send to all matching a process ID.
******************************************************************/
void SendProcessMessage(
__in DWORD dwProcessId,
__in DWORD dwMessageId,
__in DWORD dwTimeout
)
{
WcaLog(LOGMSG_VERBOSE, "Attempting to send process id 0x%x message id: %u", dwProcessId, dwMessageId);
PROCESS_AND_MESSAGE pm = { };
pm.dwProcessId = dwProcessId;
pm.dwMessageId = dwMessageId;
pm.dwTimeout = dwTimeout;
if (!::EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&pm)))
{
DWORD dwLastError = ::GetLastError();
if (ERROR_SUCCESS != dwLastError)
{
WcaLog(LOGMSG_VERBOSE, "CloseApp enumeration error: 0x%x", dwLastError);
}
}
}
/******************************************************************
SendApplicationMessage - helper function to iterate through the
processes for the specified application and send all
applicable process Ids a message and give them time to process
the message.
******************************************************************/
void SendApplicationMessage(
__in LPCWSTR wzApplication,
__in DWORD dwMessageId,
__in DWORD dwTimeout
)
{
DWORD *prgProcessIds = NULL;
DWORD cProcessIds = 0, iProcessId;
HRESULT hr = S_OK;
WcaLog(LOGMSG_VERBOSE, "Checking App: %ls ", wzApplication);
hr = ProcFindAllIdsFromExeName(wzApplication, &prgProcessIds, &cProcessIds);
if (SUCCEEDED(hr) && 0 < cProcessIds)
{
WcaLog(LOGMSG_VERBOSE, "App: %ls found running, %d processes, attempting to send message.", wzApplication, cProcessIds);
for (iProcessId = 0; iProcessId < cProcessIds; ++iProcessId)
{
SendProcessMessage(prgProcessIds[iProcessId], dwMessageId, dwTimeout);
}
ProcWaitForIds(prgProcessIds, cProcessIds, dwTimeout);
}
ReleaseMem(prgProcessIds);
}
/******************************************************************
SetRunningProcessProperty - helper function that sets the specified
property if there are any instances of the specified executable
running. Useful to show custom UI to ask for shutdown.
******************************************************************/
void SetRunningProcessProperty(
__in LPCWSTR wzApplication,
__in LPCWSTR wzProperty
)
{
DWORD *prgProcessIds = NULL;
DWORD cProcessIds = 0;
HRESULT hr = S_OK;
WcaLog(LOGMSG_VERBOSE, "Checking App: %ls ", wzApplication);
hr = ProcFindAllIdsFromExeName(wzApplication, &prgProcessIds, &cProcessIds);
if (SUCCEEDED(hr) && 0 < cProcessIds)
{
WcaLog(LOGMSG_VERBOSE, "App: %ls found running, %d processes, setting '%ls' property.", wzApplication, cProcessIds, wzProperty);
WcaSetIntProperty(wzProperty, cProcessIds);
}
ReleaseMem(prgProcessIds);
}
/******************************************************************
TerminateProcesses - helper function that kills the provided set of
process ids such that they return a particular exit code.
******************************************************************/
void TerminateProcesses(
__in_ecount(cProcessIds) DWORD rgdwProcessIds[],
__in DWORD cProcessIds,
__in DWORD dwExitCode
)
{
for (DWORD i = 0; i < cProcessIds; ++i)
{
HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, rgdwProcessIds[i]);
if (hProcess)
{
::TerminateProcess(hProcess, dwExitCode);
::CloseHandle(hProcess);
}
}
}
/******************************************************************
WixCloseApplications - entry point for WixCloseApplications Custom Action
called as Type 1 CustomAction (binary DLL) from Windows Installer
in InstallExecuteSequence before InstallFiles
******************************************************************/
extern "C" UINT __stdcall WixCloseApplications(
__in MSIHANDLE hInstall
)
{
//AssertSz(FALSE, "debug WixCloseApplications");
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
LPWSTR pwzData = NULL;
LPWSTR pwzId = NULL;
LPWSTR pwzTarget = NULL;
LPWSTR pwzDescription = NULL;
LPWSTR pwzCondition = NULL;
LPWSTR pwzProperty = NULL;
DWORD dwAttributes = 0;
DWORD dwTimeout = 0;
DWORD dwTerminateExitCode = 0;
MSICONDITION condition = MSICONDITION_NONE;
DWORD cCloseApps = 0;
PMSIHANDLE hView = NULL;
PMSIHANDLE hRec = NULL;
MSIHANDLE hListboxTable = NULL;
MSIHANDLE hListboxColumns = NULL;
LPWSTR pwzCustomActionData = NULL;
//DWORD cchCustomActionData = 0;
//
// initialize
//
hr = WcaInitialize(hInstall, "WixCloseApplications");
ExitOnFailure(hr, "failed to initialize");
//
// loop through all the objects to be secured
//
hr = WcaOpenExecuteView(wzQUERY_CLOSEAPPS, &hView);
ExitOnFailure(hr, "failed to open view on WixCloseApplication table");
while (S_OK == (hr = WcaFetchRecord(hView, &hRec)))
{
hr = WcaGetRecordString(hRec, QCA_ID, &pwzId);
ExitOnFailure(hr, "failed to get id from WixCloseApplication table");
hr = WcaGetRecordString(hRec, QCA_CONDITION, &pwzCondition);
ExitOnFailure(hr, "failed to get condition from WixCloseApplication table");
if (pwzCondition && *pwzCondition)
{
condition = ::MsiEvaluateConditionW(hInstall, pwzCondition);
if (MSICONDITION_ERROR == condition)
{
hr = E_INVALIDARG;
ExitOnFailure1(hr, "failed to process condition for WixCloseApplication '%ls'", pwzId);
}
else if (MSICONDITION_FALSE == condition)
{
continue; // skip processing this target
}
}
hr = WcaGetRecordFormattedString(hRec, QCA_TARGET, &pwzTarget);
ExitOnFailure(hr, "failed to get target from WixCloseApplication table");
hr = WcaGetRecordFormattedString(hRec, QCA_DESCRIPTION, &pwzDescription);
ExitOnFailure(hr, "failed to get description from WixCloseApplication table");
hr = WcaGetRecordInteger(hRec, QCA_ATTRIBUTES, reinterpret_cast<int*>(&dwAttributes));
ExitOnFailure(hr, "failed to get attributes from WixCloseApplication table");
hr = WcaGetRecordFormattedString(hRec, QCA_PROPERTY, &pwzProperty);
ExitOnFailure(hr, "failed to get property from WixCloseApplication table");
hr = WcaGetRecordInteger(hRec, QCA_TERMINATEEXITCODE, reinterpret_cast<int*>(&dwTerminateExitCode));
if (S_FALSE == hr)
{
dwTerminateExitCode = 0;
hr = S_OK;
}
ExitOnFailure(hr, "failed to get timeout from WixCloseApplication table");
hr = WcaGetRecordInteger(hRec, QCA_TIMEOUT, reinterpret_cast<int*>(&dwTimeout));
if (S_FALSE == hr)
{
dwTimeout = DEFAULT_PROCESS_EXIT_WAIT_TIME;
hr = S_OK;
}
ExitOnFailure(hr, "failed to get timeout from WixCloseApplication table");
// Before trying any changes to the machine, prompt if requested.
if (dwAttributes & CLOSEAPP_ATTRIBUTE_PROMPTTOCONTINUE)
{
hr = PromptToContinue(pwzTarget, pwzDescription ? pwzDescription : L"");
if (HRESULT_FROM_WIN32(ERROR_INSTALL_USEREXIT) == hr)
{
// Skip error message if user canceled.
ExitFunction();
}
ExitOnFailure(hr, "Failure while prompting user to continue to close application.");
}
//
// send WM_CLOSE or WM_QUERYENDSESSION to currently running applications
//
if (dwAttributes & CLOSEAPP_ATTRIBUTE_CLOSEMESSAGE)
{
SendApplicationMessage(pwzTarget, WM_CLOSE, dwTimeout);
}
if (dwAttributes & CLOSEAPP_ATTRIBUTE_ENDSESSIONMESSAGE)
{
SendApplicationMessage(pwzTarget, WM_QUERYENDSESSION, dwTimeout);
}
//
// Pass the targets to the deferred action in case the app comes back
// even if we close it now.
//
if (dwAttributes & (CLOSEAPP_ATTRIBUTE_ELEVATEDCLOSEMESSAGE | CLOSEAPP_ATTRIBUTE_ELEVATEDENDSESSIONMESSAGE | CLOSEAPP_ATTRIBUTE_REBOOTPROMPT | CLOSEAPP_ATTRIBUTE_TERMINATEPROCESS))
{
hr = WcaWriteStringToCaData(pwzTarget, &pwzCustomActionData);
ExitOnFailure(hr, "failed to add target data to CustomActionData");
hr = WcaWriteIntegerToCaData(dwAttributes, &pwzCustomActionData);
ExitOnFailure(hr, "failed to add attribute data to CustomActionData");
hr = WcaWriteIntegerToCaData(dwTimeout, &pwzCustomActionData);
ExitOnFailure(hr, "failed to add timeout data to CustomActionData");
hr = WcaWriteIntegerToCaData(dwTerminateExitCode, &pwzCustomActionData);
ExitOnFailure(hr, "failed to add timeout data to CustomActionData");
}
if (pwzProperty && *pwzProperty)
{
SetRunningProcessProperty(pwzTarget, pwzProperty);
}
++cCloseApps;
}
// if we looped through all records all is well
if (E_NOMOREITEMS == hr)
{
hr = S_OK;
}
ExitOnFailure(hr, "failed while looping through all apps to close");
//
// Do the UI dance now.
//
/*
TODO: Do this eventually
if (cCloseApps)
{
while (TRUE)
{
for (DWORD i = 0; i < cCloseApps; ++i)
{
hr = WcaAddTempRecord(&hListboxTable, &hListboxColumns, L"ListBox", NULL, 0, 4, L"FileInUseProcess", i, target, description);
if (FAILED(hr))
{
}
}
}
}
*/
//
// schedule the custom action and add to progress bar
//
if (pwzCustomActionData && *pwzCustomActionData)
{
Assert(0 < cCloseApps);
hr = WcaDoDeferredAction(PLATFORM_DECORATION(L"WixCloseApplicationsDeferred"), pwzCustomActionData, cCloseApps * COST_CLOSEAPP);
ExitOnFailure(hr, "failed to schedule WixCloseApplicationsDeferred action");
}
LExit:
if (hListboxColumns)
{
::MsiCloseHandle(hListboxColumns);
}
if (hListboxTable)
{
::MsiCloseHandle(hListboxTable);
}
ReleaseStr(pwzCustomActionData);
ReleaseStr(pwzData);
ReleaseStr(pwzProperty);
ReleaseStr(pwzCondition);
ReleaseStr(pwzDescription);
ReleaseStr(pwzTarget);
ReleaseStr(pwzId);
if (FAILED(hr))
{
er = HRESULT_FROM_WIN32(ERROR_INSTALL_USEREXIT) == hr ? ERROR_INSTALL_USEREXIT : ERROR_INSTALL_FAILURE;
}
return WcaFinalize(er);
}
/******************************************************************
WixCloseApplicationsDeferred - entry point for
WixCloseApplicationsDeferred Custom Action
called as Type 1025 CustomAction
(deferred binary DLL)
NOTE: deferred CustomAction since it modifies the machine
NOTE: CustomActionData == wzTarget\tdwAttributes\tdwTimeout\tdwTerminateExitCode\t...
******************************************************************/
extern "C" UINT __stdcall WixCloseApplicationsDeferred(
__in MSIHANDLE hInstall
)
{
//AssertSz(FALSE, "debug WixCloseApplicationsDeferred");
HRESULT hr = S_OK;
DWORD er = ERROR_SUCCESS;
LPWSTR pwz = NULL;
LPWSTR pwzData = NULL;
LPWSTR pwzTarget = NULL;
DWORD dwAttributes = 0;
DWORD dwTimeout = 0;
DWORD dwTerminateExitCode = 0;
DWORD *prgProcessIds = NULL;
DWORD cProcessIds = 0;
//
// initialize
//
hr = WcaInitialize(hInstall, "WixCloseApplicationsDeferred");
ExitOnFailure(hr, "failed to initialize");
hr = WcaGetProperty(L"CustomActionData", &pwzData);
ExitOnFailure(hr, "failed to get CustomActionData");
WcaLog(LOGMSG_TRACEONLY, "CustomActionData: %ls", pwzData);
pwz = pwzData;
//
// loop through all the passed in data
//
while (pwz && *pwz)
{
hr = WcaReadStringFromCaData(&pwz, &pwzTarget);
ExitOnFailure(hr, "failed to process target from CustomActionData");
hr = WcaReadIntegerFromCaData(&pwz, reinterpret_cast<int*>(&dwAttributes));
ExitOnFailure(hr, "failed to process attributes from CustomActionData");
hr = WcaReadIntegerFromCaData(&pwz, reinterpret_cast<int*>(&dwTimeout));
ExitOnFailure(hr, "failed to process timeout from CustomActionData");
hr = WcaReadIntegerFromCaData(&pwz, reinterpret_cast<int*>(&dwTerminateExitCode));
ExitOnFailure(hr, "failed to process terminate exit code from CustomActionData");
WcaLog(LOGMSG_VERBOSE, "Checking for App: %ls Attributes: %d", pwzTarget, dwAttributes);
//
// send WM_CLOSE or WM_QUERYENDSESSION to currently running applications
//
if (dwAttributes & CLOSEAPP_ATTRIBUTE_ELEVATEDCLOSEMESSAGE)
{
SendApplicationMessage(pwzTarget, WM_CLOSE, dwTimeout);
}
if (dwAttributes & CLOSEAPP_ATTRIBUTE_ELEVATEDENDSESSIONMESSAGE)
{
SendApplicationMessage(pwzTarget, WM_QUERYENDSESSION, dwTimeout);
}
// If we find that an app that we need closed is still runing, require a
// restart or kill the process as directed.
ProcFindAllIdsFromExeName(pwzTarget, &prgProcessIds, &cProcessIds);
if (0 < cProcessIds)
{
if (dwAttributes & CLOSEAPP_ATTRIBUTE_REBOOTPROMPT)
{
WcaLog(LOGMSG_VERBOSE, "App: %ls found running, requiring a reboot.", pwzTarget);
WcaDeferredActionRequiresReboot();
}
else if (dwAttributes & CLOSEAPP_ATTRIBUTE_TERMINATEPROCESS)
{
TerminateProcesses(prgProcessIds, cProcessIds, dwTerminateExitCode);
}
}
hr = WcaProgressMessage(COST_CLOSEAPP, FALSE);
ExitOnFailure(hr, "failed to send progress message");
}
LExit:
ReleaseMem(prgProcessIds);
ReleaseStr(pwzTarget);
ReleaseStr(pwzData);
if (FAILED(hr))
{
er = ERROR_INSTALL_FAILURE;
}
return WcaFinalize(er);
}