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