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
|
// 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 BURN_UITHREAD_CLASS_WINDOW L"WixBurnMessageWindow"
// structs
struct UITHREAD_CONTEXT
{
HANDLE hInitializedEvent;
HINSTANCE hInstance;
BURN_ENGINE_STATE* pEngineState;
};
struct UITHREAD_INFO
{
BOOL fElevatedEngine;
BURN_ENGINE_STATE* pEngineState;
};
// internal function declarations
static DWORD WINAPI ThreadProc(
__in LPVOID pvContext
);
static LRESULT CALLBACK WndProc(
__in HWND hWnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam
);
// function definitions
HRESULT UiCreateMessageWindow(
__in HINSTANCE hInstance,
__in BURN_ENGINE_STATE* pEngineState
)
{
HRESULT hr = S_OK;
HANDLE rgWaitHandles[2] = { };
UITHREAD_CONTEXT context = { };
// Create event to signal after the UI thread / window is initialized.
rgWaitHandles[0] = ::CreateEventW(NULL, TRUE, FALSE, NULL);
ExitOnNullWithLastError(rgWaitHandles[0], hr, "Failed to create initialization event.");
// Pass necessary information to create the window.
context.hInitializedEvent = rgWaitHandles[0];
context.hInstance = hInstance;
context.pEngineState = pEngineState;
// Create our separate UI thread.
rgWaitHandles[1] = ::CreateThread(NULL, 0, ThreadProc, &context, 0, NULL);
ExitOnNullWithLastError(rgWaitHandles[1], hr, "Failed to create the UI thread.");
// Wait for either the thread to be initialized or the window to exit / fail prematurely.
::WaitForMultipleObjects(countof(rgWaitHandles), rgWaitHandles, FALSE, INFINITE);
pEngineState->hMessageWindowThread = rgWaitHandles[1];
rgWaitHandles[1] = NULL;
LExit:
ReleaseHandle(rgWaitHandles[1]);
ReleaseHandle(rgWaitHandles[0]);
return hr;
}
void UiCloseMessageWindow(
__in BURN_ENGINE_STATE* pEngineState
)
{
if (::IsWindow(pEngineState->hMessageWindow))
{
::PostMessageW(pEngineState->hMessageWindow, WM_CLOSE, 0, 0);
// Give the window 15 seconds to close because if it stays open it can prevent
// the engine from starting a reboot (should a reboot actually be necessary).
::WaitForSingleObject(pEngineState->hMessageWindowThread, 15 * 1000);
}
}
// internal function definitions
static DWORD WINAPI ThreadProc(
__in LPVOID pvContext
)
{
HRESULT hr = S_OK;
UITHREAD_CONTEXT* pContext = static_cast<UITHREAD_CONTEXT*>(pvContext);
UITHREAD_INFO info = { };
WNDCLASSW wc = { };
BOOL fRegistered = FALSE;
HWND hWnd = NULL;
BOOL fRet = FALSE;
MSG msg = { };
BURN_ENGINE_STATE* pEngineState = pContext->pEngineState;
BOOL fElevatedEngine = BURN_MODE_ELEVATED == pContext->pEngineState->internalCommand.mode;
wc.lpfnWndProc = WndProc;
wc.hInstance = pContext->hInstance;
wc.lpszClassName = BURN_UITHREAD_CLASS_WINDOW;
if (!::RegisterClassW(&wc))
{
ExitWithLastError(hr, "Failed to register window.");
}
fRegistered = TRUE;
info.fElevatedEngine = fElevatedEngine;
info.pEngineState = pEngineState;
// Create the window to handle reboots without activating it.
hWnd = ::CreateWindowExW(WS_EX_NOACTIVATE, wc.lpszClassName, NULL, WS_POPUP, 0, 0, 0, 0, HWND_DESKTOP, NULL, pContext->hInstance, &info);
ExitOnNullWithLastError(hWnd, hr, "Failed to create window.");
::ShowWindow(hWnd, SW_SHOWNA);
// Persist the window handle and let the caller know we've initialized.
pEngineState->hMessageWindow = hWnd;
::SetEvent(pContext->hInitializedEvent);
// Pump messages until the window is closed.
while (0 != (fRet = ::GetMessageW(&msg, NULL, 0, 0)))
{
if (-1 == fRet)
{
hr = E_UNEXPECTED;
ExitOnFailure(hr, "Unexpected return value from message pump.");
}
else if (!::IsDialogMessageW(msg.hwnd, &msg))
{
::TranslateMessage(&msg);
::DispatchMessageW(&msg);
}
}
LExit:
if (fRegistered)
{
::UnregisterClassW(BURN_UITHREAD_CLASS_WINDOW, pContext->hInstance);
}
return hr;
}
static LRESULT CALLBACK WndProc(
__in HWND hWnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam
)
{
switch (uMsg)
{
case WM_NCCREATE:
{
LPCREATESTRUCTW lpcs = reinterpret_cast<LPCREATESTRUCTW>(lParam);
::SetWindowLongPtrW(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(lpcs->lpCreateParams));
break;
}
case WM_NCDESTROY:
{
LRESULT lRes = ::DefWindowProcW(hWnd, uMsg, wParam, lParam);
::SetWindowLongPtrW(hWnd, GWLP_USERDATA, 0);
return lRes;
}
case WM_QUERYENDSESSION:
{
DWORD dwEndSession = static_cast<DWORD>(lParam);
BOOL fCritical = ENDSESSION_CRITICAL & dwEndSession;
BOOL fCancel = FALSE;
BOOL fRet = FALSE;
// Always block shutdown during apply.
UITHREAD_INFO* pInfo = reinterpret_cast<UITHREAD_INFO*>(::GetWindowLongPtrW(hWnd, GWLP_USERDATA));
if (pInfo->pEngineState->plan.fApplying)
{
fCancel = TRUE;
}
pInfo->pEngineState->fCriticalShutdownInitiated |= fCritical;
fRet = !fCancel;
LogId(REPORT_STANDARD, MSG_SYSTEM_SHUTDOWN, LoggingBoolToString(fCritical), LoggingBoolToString(pInfo->fElevatedEngine), LoggingBoolToString(fRet));
return fRet;
}
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return ::DefWindowProcW(hWnd, uMsg, wParam, lParam);
}
|