Sync with trunk r63270.
[reactos.git] / dll / win32 / advapi32 / misc / shutdown.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/advapi32/misc/shutdown.c
5 * PURPOSE: System shutdown functions
6 * PROGRAMMER: Lee Schroeder <spaceseel at gmail dot com>
7 * Emanuele Aliberti
8 */
9
10 #include <advapi32.h>
11
12 #include <ndk/exfuncs.h>
13
14 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
15
16 /**********************************************************************
17 * AbortSystemShutdownW
18 *
19 * @unimplemented
20 */
21 BOOL WINAPI
22 AbortSystemShutdownW(LPCWSTR lpMachineName)
23 {
24 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
25 return FALSE;
26 }
27
28
29 /**********************************************************************
30 * AbortSystemShutdownA
31 *
32 * see AbortSystemShutdownW
33 */
34 BOOL WINAPI
35 AbortSystemShutdownA(LPCSTR lpMachineName)
36 {
37 ANSI_STRING MachineNameA;
38 UNICODE_STRING MachineNameW;
39 NTSTATUS Status;
40 BOOL rv;
41
42 RtlInitAnsiString(&MachineNameA, (LPSTR)lpMachineName);
43 Status = RtlAnsiStringToUnicodeString(&MachineNameW, &MachineNameA, TRUE);
44 if (STATUS_SUCCESS != Status)
45 {
46 SetLastError(RtlNtStatusToDosError(Status));
47 return FALSE;
48 }
49
50 rv = AbortSystemShutdownW(MachineNameW.Buffer);
51 RtlFreeUnicodeString(&MachineNameW);
52 SetLastError(ERROR_SUCCESS);
53 return rv;
54 }
55
56 /**********************************************************************
57 * InitiateSystemShutdownW
58 *
59 * @implemented
60 */
61 BOOL WINAPI
62 InitiateSystemShutdownW(LPWSTR lpMachineName,
63 LPWSTR lpMessage,
64 DWORD dwTimeout,
65 BOOL bForceAppsClosed,
66 BOOL bRebootAfterShutdown)
67 {
68 return InitiateSystemShutdownExW(lpMachineName,
69 lpMessage,
70 dwTimeout,
71 bForceAppsClosed,
72 bRebootAfterShutdown,
73 SHTDN_REASON_MAJOR_OTHER |
74 SHTDN_REASON_MINOR_OTHER |
75 SHTDN_REASON_FLAG_PLANNED
76 /* SHTDN_REASON_MAJOR_LEGACY_API */);
77 }
78
79 /**********************************************************************
80 * InitiateSystemShutdownA
81 *
82 * @implemented
83 */
84 BOOL
85 WINAPI
86 InitiateSystemShutdownA(LPSTR lpMachineName,
87 LPSTR lpMessage,
88 DWORD dwTimeout,
89 BOOL bForceAppsClosed,
90 BOOL bRebootAfterShutdown)
91 {
92 return InitiateSystemShutdownExA(lpMachineName,
93 lpMessage,
94 dwTimeout,
95 bForceAppsClosed,
96 bRebootAfterShutdown,
97 SHTDN_REASON_MAJOR_OTHER |
98 SHTDN_REASON_MINOR_OTHER |
99 SHTDN_REASON_FLAG_PLANNED
100 /* SHTDN_REASON_MAJOR_LEGACY_API */);
101 }
102
103 /******************************************************************************
104 * InitiateSystemShutdownExW [ADVAPI32.@]
105 *
106 * @unimplemented
107 */
108 BOOL WINAPI
109 InitiateSystemShutdownExW(LPWSTR lpMachineName,
110 LPWSTR lpMessage,
111 DWORD dwTimeout,
112 BOOL bForceAppsClosed,
113 BOOL bRebootAfterShutdown,
114 DWORD dwReason)
115 {
116 SHUTDOWN_ACTION action;
117 NTSTATUS Status;
118 ULONG Timeout_ms;
119
120 /* Convert to milliseconds so we can use the value later on */
121 Timeout_ms = dwTimeout * 1000;
122
123 if (lpMachineName != NULL)
124 {
125 /* FIXME: Remote system shutdown not supported yet */
126 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
127 return FALSE;
128 }
129 else /* The local system is being used */
130 {
131 /* FIXME: Right now, only basic shutting down and rebooting
132 is supported */
133 if(bRebootAfterShutdown == TRUE)
134 {
135 action = ShutdownReboot;
136 }
137 else
138 {
139 action = ShutdownNoReboot;
140 }
141
142 Status = NtShutdownSystem(action);
143 }
144
145 SetLastError(RtlNtStatusToDosError(Status));
146 return (Status == STATUS_SUCCESS);
147 }
148
149 /******************************************************************************
150 * InitiateSystemShutdownExA [ADVAPI32.@]
151 *
152 * see InitiateSystemShutdownExW
153 */
154 BOOL WINAPI
155 InitiateSystemShutdownExA(LPSTR lpMachineName,
156 LPSTR lpMessage,
157 DWORD dwTimeout,
158 BOOL bForceAppsClosed,
159 BOOL bRebootAfterShutdown,
160 DWORD dwReason)
161 {
162 ANSI_STRING MachineNameA, MessageA;
163 UNICODE_STRING MachineNameW, MessageW;
164 NTSTATUS Status;
165 INT LastError;
166 BOOL rv;
167
168 MachineNameW.Buffer = NULL;
169 MessageW.Buffer = NULL;
170
171 if (lpMachineName)
172 {
173 RtlInitAnsiString(&MachineNameA, lpMachineName);
174 Status = RtlAnsiStringToUnicodeString(&MachineNameW, &MachineNameA, TRUE);
175 if (STATUS_SUCCESS != Status)
176 {
177 if(MachineNameW.Buffer)
178 RtlFreeUnicodeString(&MachineNameW);
179
180 SetLastError(RtlNtStatusToDosError(Status));
181 return FALSE;
182 }
183 }
184
185 if (lpMessage)
186 {
187 RtlInitAnsiString(&MessageA, lpMessage);
188 Status = RtlAnsiStringToUnicodeString(&MessageW, &MessageA, TRUE);
189 if (STATUS_SUCCESS != Status)
190 {
191 if (MessageW.Buffer)
192 RtlFreeUnicodeString(&MessageW);
193
194 SetLastError(RtlNtStatusToDosError(Status));
195 return FALSE;
196 }
197 }
198
199 rv = InitiateSystemShutdownExW(MachineNameW.Buffer,
200 MessageW.Buffer,
201 dwTimeout,
202 bForceAppsClosed,
203 bRebootAfterShutdown,
204 dwReason);
205 LastError = GetLastError();
206
207 /* Clear the values of both strings */
208 if (lpMachineName)
209 RtlFreeUnicodeString(&MachineNameW);
210
211 if (lpMessage)
212 RtlFreeUnicodeString(&MessageW);
213
214 SetLastError(LastError);
215 return rv;
216 }
217
218 /******************************************************************************
219 * InitiateShutdownW [ADVAPI32.@]
220 *
221 * @unimplamented
222 */
223 DWORD WINAPI
224 InitiateShutdownW(LPWSTR lpMachineName,
225 LPWSTR lpMessage,
226 DWORD dwGracePeriod,
227 DWORD dwShutdownFlags,
228 DWORD dwReason)
229 {
230 UNIMPLEMENTED;
231 return ERROR_SUCCESS;
232 }
233
234 /******************************************************************************
235 * InitiateShutdownA [ADVAPI32.@]
236 *
237 * see InitiateShutdownW
238 */
239 DWORD WINAPI
240 InitiateShutdownA(LPSTR lpMachineName,
241 LPSTR lpMessage,
242 DWORD dwGracePeriod,
243 DWORD dwShutdownFlags,
244 DWORD dwReason)
245 {
246 ANSI_STRING MachineNameA, MessageA;
247 UNICODE_STRING MachineNameW, MessageW;
248 NTSTATUS Status;
249 INT LastError;
250 DWORD rv;
251
252 MachineNameW.Buffer = NULL;
253 MessageW.Buffer = NULL;
254
255 if (lpMachineName)
256 {
257 RtlInitAnsiString(&MachineNameA, lpMachineName);
258 Status = RtlAnsiStringToUnicodeString(&MachineNameW, &MachineNameA, TRUE);
259 if (STATUS_SUCCESS != Status)
260 {
261 if(MachineNameW.Buffer)
262 RtlFreeUnicodeString(&MachineNameW);
263
264 SetLastError(RtlNtStatusToDosError(Status));
265 return FALSE;
266 }
267 }
268
269 if (lpMessage)
270 {
271 RtlInitAnsiString(&MessageA, lpMessage);
272 Status = RtlAnsiStringToUnicodeString(&MessageW, &MessageA, TRUE);
273 if (STATUS_SUCCESS != Status)
274 {
275 if (MessageW.Buffer)
276 RtlFreeUnicodeString(&MessageW);
277
278 SetLastError(RtlNtStatusToDosError(Status));
279 return FALSE;
280 }
281 }
282
283 rv = InitiateShutdownW(MachineNameW.Buffer,
284 MessageW.Buffer,
285 dwGracePeriod,
286 dwShutdownFlags,
287 dwReason);
288 LastError = GetLastError();
289
290 /* Clear the values of both strings */
291 if (lpMachineName)
292 RtlFreeUnicodeString(&MachineNameW);
293
294 if (lpMessage)
295 RtlFreeUnicodeString(&MessageW);
296
297 SetLastError(LastError);
298 return rv;
299 }
300
301 /* EOF */