[ADVAPI32]
[reactos.git] / reactos / 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 DBG_UNREFERENCED_LOCAL_VARIABLE(Timeout_ms);
121
122 /* Convert to milliseconds so we can use the value later on */
123 Timeout_ms = dwTimeout * 1000;
124
125 if (lpMachineName != NULL)
126 {
127 /* FIXME: Remote system shutdown not supported yet */
128 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
129 return FALSE;
130 }
131 else /* The local system is being used */
132 {
133 /* FIXME: Right now, only basic shutting down and rebooting
134 is supported */
135 if(bRebootAfterShutdown == TRUE)
136 {
137 action = ShutdownReboot;
138 }
139 else
140 {
141 action = ShutdownNoReboot;
142 }
143
144 Status = NtShutdownSystem(action);
145 }
146
147 SetLastError(RtlNtStatusToDosError(Status));
148 return (Status == STATUS_SUCCESS);
149 }
150
151 /******************************************************************************
152 * InitiateSystemShutdownExA [ADVAPI32.@]
153 *
154 * see InitiateSystemShutdownExW
155 */
156 BOOL WINAPI
157 InitiateSystemShutdownExA(LPSTR lpMachineName,
158 LPSTR lpMessage,
159 DWORD dwTimeout,
160 BOOL bForceAppsClosed,
161 BOOL bRebootAfterShutdown,
162 DWORD dwReason)
163 {
164 ANSI_STRING MachineNameA, MessageA;
165 UNICODE_STRING MachineNameW, MessageW;
166 NTSTATUS Status;
167 INT LastError;
168 BOOL rv;
169
170 MachineNameW.Buffer = NULL;
171 MessageW.Buffer = NULL;
172
173 if (lpMachineName)
174 {
175 RtlInitAnsiString(&MachineNameA, lpMachineName);
176 Status = RtlAnsiStringToUnicodeString(&MachineNameW, &MachineNameA, TRUE);
177 if (STATUS_SUCCESS != Status)
178 {
179 if(MachineNameW.Buffer)
180 RtlFreeUnicodeString(&MachineNameW);
181
182 SetLastError(RtlNtStatusToDosError(Status));
183 return FALSE;
184 }
185 }
186
187 if (lpMessage)
188 {
189 RtlInitAnsiString(&MessageA, lpMessage);
190 Status = RtlAnsiStringToUnicodeString(&MessageW, &MessageA, TRUE);
191 if (STATUS_SUCCESS != Status)
192 {
193 if (MessageW.Buffer)
194 RtlFreeUnicodeString(&MessageW);
195
196 SetLastError(RtlNtStatusToDosError(Status));
197 return FALSE;
198 }
199 }
200
201 rv = InitiateSystemShutdownExW(MachineNameW.Buffer,
202 MessageW.Buffer,
203 dwTimeout,
204 bForceAppsClosed,
205 bRebootAfterShutdown,
206 dwReason);
207 LastError = GetLastError();
208
209 /* Clear the values of both strings */
210 if (lpMachineName)
211 RtlFreeUnicodeString(&MachineNameW);
212
213 if (lpMessage)
214 RtlFreeUnicodeString(&MessageW);
215
216 SetLastError(LastError);
217 return rv;
218 }
219
220 /******************************************************************************
221 * InitiateShutdownW [ADVAPI32.@]
222 *
223 * @unimplamented
224 */
225 DWORD WINAPI
226 InitiateShutdownW(LPWSTR lpMachineName,
227 LPWSTR lpMessage,
228 DWORD dwGracePeriod,
229 DWORD dwShutdownFlags,
230 DWORD dwReason)
231 {
232 UNIMPLEMENTED;
233 return ERROR_SUCCESS;
234 }
235
236 /******************************************************************************
237 * InitiateShutdownA [ADVAPI32.@]
238 *
239 * see InitiateShutdownW
240 */
241 DWORD WINAPI
242 InitiateShutdownA(LPSTR lpMachineName,
243 LPSTR lpMessage,
244 DWORD dwGracePeriod,
245 DWORD dwShutdownFlags,
246 DWORD dwReason)
247 {
248 ANSI_STRING MachineNameA, MessageA;
249 UNICODE_STRING MachineNameW, MessageW;
250 NTSTATUS Status;
251 INT LastError;
252 DWORD rv;
253
254 MachineNameW.Buffer = NULL;
255 MessageW.Buffer = NULL;
256
257 if (lpMachineName)
258 {
259 RtlInitAnsiString(&MachineNameA, lpMachineName);
260 Status = RtlAnsiStringToUnicodeString(&MachineNameW, &MachineNameA, TRUE);
261 if (STATUS_SUCCESS != Status)
262 {
263 if(MachineNameW.Buffer)
264 RtlFreeUnicodeString(&MachineNameW);
265
266 SetLastError(RtlNtStatusToDosError(Status));
267 return FALSE;
268 }
269 }
270
271 if (lpMessage)
272 {
273 RtlInitAnsiString(&MessageA, lpMessage);
274 Status = RtlAnsiStringToUnicodeString(&MessageW, &MessageA, TRUE);
275 if (STATUS_SUCCESS != Status)
276 {
277 if (MessageW.Buffer)
278 RtlFreeUnicodeString(&MessageW);
279
280 SetLastError(RtlNtStatusToDosError(Status));
281 return FALSE;
282 }
283 }
284
285 rv = InitiateShutdownW(MachineNameW.Buffer,
286 MessageW.Buffer,
287 dwGracePeriod,
288 dwShutdownFlags,
289 dwReason);
290 LastError = GetLastError();
291
292 /* Clear the values of both strings */
293 if (lpMachineName)
294 RtlFreeUnicodeString(&MachineNameW);
295
296 if (lpMessage)
297 RtlFreeUnicodeString(&MessageW);
298
299 SetLastError(LastError);
300 return rv;
301 }
302
303 /* EOF */