ReactOS  0.4.13-dev-52-g0efcfec
shutdown.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS Winlogon
4  * FILE: base/system/winlogon/shutdown.c
5  * PURPOSE: System shutdown dialog
6  * PROGRAMMERS: alpha5056 <alpha5056@users.noreply.github.com>
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include "winlogon.h"
12 
13 #include <rpc.h>
14 #include <winreg_s.h>
15 
16 /* DEFINES *******************************************************************/
17 
18 #define SHUTDOWN_TIMER_ID 2000
19 #define SECONDS_PER_DAY 86400
20 #define SECONDS_PER_DECADE 315360000
21 
22 
23 /* STRUCTS *******************************************************************/
24 
25 typedef struct _SYS_SHUTDOWN_PARAMS
26 {
32 
35 
36 
37 /* GLOBALS *******************************************************************/
38 
40 
41 
42 /* FUNCTIONS *****************************************************************/
43 
44 static
45 BOOL
47  IN PSYS_SHUTDOWN_PARAMS pShutdownParams)
48 {
49  BOOL Success;
50 
51  if (pShutdownParams->pszMessage)
52  {
53  HeapFree(GetProcessHeap(), 0, pShutdownParams->pszMessage);
54  pShutdownParams->pszMessage = NULL;
55  }
56 
57  /* If shutdown has been cancelled, bail out now */
58  if (!pShutdownParams->bShuttingDown)
59  return TRUE;
60 
61  Success = ExitWindowsEx((pShutdownParams->bRebootAfterShutdown ? EWX_REBOOT : EWX_SHUTDOWN) |
62  (pShutdownParams->bForceAppsClosed ? EWX_FORCE : 0),
63  pShutdownParams->dwReason);
64  if (!Success)
65  {
66  /* Something went wrong, cancel shutdown */
67  pShutdownParams->bShuttingDown = FALSE;
68  }
69 
70  return Success;
71 }
72 
73 
74 static
75 VOID
77  HWND hwndDlg,
78  PSYS_SHUTDOWN_PARAMS pShutdownParams)
79 {
80  WCHAR szFormatBuffer[32];
81  WCHAR szBuffer[32];
82  INT iSeconds, iMinutes, iHours, iDays;
83 
84  if (!pShutdownParams->bShuttingDown)
85  {
86  /* Shutdown has been cancelled, close the dialog and bail out */
87  EndDialog(hwndDlg, 0);
88  return;
89  }
90 
91  if (pShutdownParams->dwTimeout < SECONDS_PER_DAY)
92  {
93  iSeconds = (INT)pShutdownParams->dwTimeout;
94  iHours = iSeconds / 3600;
95  iSeconds -= iHours * 3600;
96  iMinutes = iSeconds / 60;
97  iSeconds -= iMinutes * 60;
98 
99  LoadStringW(hAppInstance, IDS_TIMEOUTSHORTFORMAT, szFormatBuffer, ARRAYSIZE(szFormatBuffer));
100  swprintf(szBuffer, szFormatBuffer, iHours, iMinutes, iSeconds);
101  }
102  else
103  {
104  iDays = (INT)(pShutdownParams->dwTimeout / SECONDS_PER_DAY);
105 
106  LoadStringW(hAppInstance, IDS_TIMEOUTLONGFORMAT, szFormatBuffer, ARRAYSIZE(szFormatBuffer));
107  swprintf(szBuffer, szFormatBuffer, iDays);
108  }
109 
110  SetDlgItemTextW(hwndDlg, IDC_SYSSHUTDOWNTIMELEFT, szBuffer);
111 
112  if (pShutdownParams->dwTimeout == 0)
113  {
114  /* Close the dialog and perform the system shutdown */
115  EndDialog(hwndDlg, 0);
116  DoSystemShutdown(pShutdownParams);
117  return;
118  }
119 
120  pShutdownParams->dwTimeout--;
121 }
122 
123 
124 static
125 INT_PTR
126 CALLBACK
128  HWND hwndDlg,
129  UINT uMsg,
130  WPARAM wParam,
131  LPARAM lParam)
132 {
133  PSYS_SHUTDOWN_PARAMS pShutdownParams;
134 
135  pShutdownParams = (PSYS_SHUTDOWN_PARAMS)GetWindowLongPtr(hwndDlg, DWLP_USER);
136 
137  switch (uMsg)
138  {
139  case WM_INITDIALOG:
140  {
141  pShutdownParams = (PSYS_SHUTDOWN_PARAMS)lParam;
142  SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pShutdownParams);
143 
144  if (pShutdownParams->pszMessage)
145  {
146  SetDlgItemTextW(hwndDlg,
148  pShutdownParams->pszMessage);
149  }
150 
152  SetWindowPos(hwndDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
153 
154  PostMessage(hwndDlg, WM_TIMER, 0, 0);
155  SetTimer(hwndDlg, SHUTDOWN_TIMER_ID, 1000, NULL);
156  break;
157  }
158 
159  /* NOTE: Do not handle WM_CLOSE */
160  case WM_DESTROY:
161  KillTimer(hwndDlg, SHUTDOWN_TIMER_ID);
162  break;
163 
164  case WM_TIMER:
165  OnTimer(hwndDlg, pShutdownParams);
166  break;
167 
168  default:
169  return FALSE;
170  }
171 
172  return TRUE;
173 }
174 
175 
176 static
177 DWORD
178 WINAPI
181 {
182  PSYS_SHUTDOWN_PARAMS pShutdownParams;
183  INT_PTR status;
184 
185  pShutdownParams = (PSYS_SHUTDOWN_PARAMS)lpParameter;
186 
189  NULL,
191  (LPARAM)pShutdownParams);
192 
193  if (pShutdownParams->pszMessage)
194  {
195  HeapFree(GetProcessHeap(), 0, pShutdownParams->pszMessage);
196  pShutdownParams->pszMessage = NULL;
197  }
198 
199  if (status >= 0)
200  return ERROR_SUCCESS;
201 
202  pShutdownParams->bShuttingDown = FALSE;
203  return GetLastError();
204 }
205 
206 
207 DWORD
209 {
212 
213  return ERROR_SUCCESS;
214 }
215 
216 
217 DWORD
219  PUNICODE_STRING lpMessage,
221  BOOLEAN bForceAppsClosed,
222  BOOLEAN bRebootAfterShutdown,
223  ULONG dwReason)
224 {
225  HANDLE hThread;
226 
227  /* Fail if the timeout is 10 years or more */
230 
233 
234  if (lpMessage && lpMessage->Length && lpMessage->Buffer)
235  {
238  lpMessage->Length + sizeof(UNICODE_NULL));
240  {
242  return GetLastError();
243  }
244 
246  lpMessage->Buffer,
247  lpMessage->Length / sizeof(WCHAR));
248  }
249  else
250  {
252  }
253 
255  g_ShutdownParams.bForceAppsClosed = bForceAppsClosed;
256  g_ShutdownParams.bRebootAfterShutdown = bRebootAfterShutdown;
258 
259  /* If dwTimeout is zero perform an immediate system shutdown, otherwise display the countdown shutdown dialog */
260  if (g_ShutdownParams.dwTimeout == 0)
261  {
263  return ERROR_SUCCESS;
264  }
265  else
266  {
268  if (hThread)
269  {
271  return ERROR_SUCCESS;
272  }
273  }
274 
276  {
279  }
280 
282  return GetLastError();
283 }
284 
285 /* EOF */
#define IDC_SYSSHUTDOWNTIMELEFT
Definition: resource.h:18
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define EWX_FORCE
Definition: winuser.h:630
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
INT_PTR WINAPI DialogBoxParamW(_In_opt_ HINSTANCE, _In_ LPCWSTR, _In_opt_ HWND, _In_opt_ DLGPROC, _In_ LPARAM)
#define MF_BYCOMMAND
Definition: winuser.h:202
#define ERROR_SUCCESS
Definition: deptool.c:10
#define IDC_SYSSHUTDOWNMESSAGE
Definition: resource.h:19
uint16_t * PWSTR
Definition: typedefs.h:54
#define IDD_SYSSHUTDOWN
Definition: resource.h:16
#define ERROR_SHUTDOWN_IN_PROGRESS
Definition: winerror.h:651
#define INT
Definition: polytest.cpp:20
#define CALLBACK
Definition: compat.h:27
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define HWND_TOPMOST
Definition: winuser.h:1194
UINT_PTR WPARAM
Definition: windef.h:207
int32_t INT_PTR
Definition: typedefs.h:62
#define EWX_REBOOT
Definition: winuser.h:633
SYS_SHUTDOWN_PARAMS g_ShutdownParams
Definition: shutdown.c:39
#define IDS_TIMEOUTLONGFORMAT
Definition: resource.h:37
static VOID OnTimer(HWND hwndDlg, PSYS_SHUTDOWN_PARAMS pShutdownParams)
Definition: shutdown.c:76
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
int32_t INT
Definition: typedefs.h:56
#define SetWindowLongPtr
Definition: treelist.c:70
WPARAM wParam
Definition: combotst.c:138
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
char _InterlockedCompareExchange8(_Interlocked_operand_ char volatile *_Destination, char _Exchange, char _Comparand)
BOOL WINAPI SetDlgItemTextW(_In_ HWND, _In_ int, _In_ LPCWSTR)
DWORD dwReason
Definition: misc.cpp:154
static INT_PTR CALLBACK ShutdownDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: shutdown.c:127
#define UNICODE_NULL
#define EWX_SHUTDOWN
Definition: winuser.h:634
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned char BOOLEAN
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:112
HMENU WINAPI GetSystemMenu(_In_ HWND, _In_ BOOL)
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
static BOOL DoSystemShutdown(IN PSYS_SHUTDOWN_PARAMS pShutdownParams)
Definition: shutdown.c:46
BOOLEAN bForceAppsClosed
Definition: shutdown.c:30
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
#define WM_DESTROY
Definition: winuser.h:1591
#define PostMessage
Definition: winuser.h:5698
DWORD TerminateSystemShutdown(VOID)
Definition: shutdown.c:208
DWORD StartSystemShutdown(PUNICODE_STRING lpMessage, ULONG dwTimeout, BOOLEAN bForceAppsClosed, BOOLEAN bRebootAfterShutdown, ULONG dwReason)
Definition: shutdown.c:218
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define ERROR_NO_SHUTDOWN_IN_PROGRESS
Definition: winerror.h:652
__wchar_t WCHAR
Definition: xmlstorage.h:180
BOOL WINAPI DeleteMenu(_In_ HMENU, _In_ UINT, _In_ UINT)
#define WM_TIMER
Definition: winuser.h:1718
#define swprintf(buf, format,...)
Definition: sprintf.c:56
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
#define IDS_TIMEOUTSHORTFORMAT
Definition: resource.h:36
BOOL WINAPI KillTimer(_In_opt_ HWND, _In_ UINT_PTR)
#define DWLP_USER
Definition: winuser.h:866
LPVOID lpParameter
Definition: kernel32.h:234
struct _SYS_SHUTDOWN_PARAMS SYS_SHUTDOWN_PARAMS
struct _SYS_SHUTDOWN_PARAMS * PSYS_SHUTDOWN_PARAMS
BOOL WINAPI ExitWindowsEx(_In_ UINT, _In_ DWORD)
#define SHUTDOWN_TIMER_ID
Definition: shutdown.c:18
#define SWP_NOSIZE
Definition: winuser.h:1230
_In_ void _In_ PCCERT_CONTEXT _In_opt_ LPFILETIME _In_ DWORD _In_ DWORD dwTimeout
Definition: wincrypt.h:6081
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
#define SECONDS_PER_DAY
Definition: shutdown.c:19
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
BOOLEAN bRebootAfterShutdown
Definition: shutdown.c:29
unsigned int UINT
Definition: ndis.h:50
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
HANDLE hThread
Definition: wizard.c:27
#define SC_CLOSE
Definition: winuser.h:2546
unsigned int ULONG
Definition: retypes.h:1
#define SWP_NOMOVE
Definition: winuser.h:1229
BOOL WINAPI SetWindowPos(_In_ HWND, _In_opt_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ UINT)
static DWORD WINAPI InitiateSystemShutdownThread(LPVOID lpParameter)
Definition: shutdown.c:179
#define SECONDS_PER_DECADE
Definition: shutdown.c:20
BOOLEAN bShuttingDown
Definition: shutdown.c:33
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
#define GetWindowLongPtr
Definition: treelist.c:73
static SERVICE_STATUS status
Definition: service.c:31
#define WM_INITDIALOG
Definition: winuser.h:1715
LPARAM lParam
Definition: combotst.c:139
#define HeapFree(x, y, z)
Definition: compat.h:394
HINSTANCE hAppInstance
Definition: mmc.c:23
Definition: ps.c:97