6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * 2011 Mário Kacmár /Mario Kacmar/ aka Kario (kario@szm.sk)
8 * 2014 Robert Naumann <gonzomdx@gmail.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <ndk/exfuncs.h>
27 #include <ndk/pofuncs.h>
28 #include <ndk/rtlfuncs.h>
30 // Uncomment when NtInitiatePowerAction() is implemented
31 // #define NT_INITIATE_POWERACTION_IMPLEMENTED
34 EnablePrivilege(LPCWSTR lpszPrivilegeName
, BOOL bEnablePrivilege
)
40 Success
= OpenProcessToken(GetCurrentProcess(),
41 TOKEN_ADJUST_PRIVILEGES
,
43 if (!Success
) return Success
;
45 Success
= LookupPrivilegeValueW(NULL
,
47 &tp
.Privileges
[0].Luid
);
48 if (!Success
) goto Quit
;
50 tp
.PrivilegeCount
= 1;
51 tp
.Privileges
[0].Attributes
= (bEnablePrivilege
? SE_PRIVILEGE_ENABLED
: 0);
53 Success
= AdjustTokenPrivileges(hToken
, FALSE
, &tp
, 0, NULL
, NULL
);
61 ShutDown_StandBy(VOID
)
65 if (!EnablePrivilege(SE_SHUTDOWN_NAME
, TRUE
))
67 ShowWin32Error(GetLastError());
71 #ifdef NT_INITIATE_POWERACTION_IMPLEMENTED
72 Status
= NtInitiatePowerAction(PowerActionSleep
,
76 Status
= NtSetSystemPowerState(PowerActionSleep
,
81 if (!NT_SUCCESS(Status
))
82 ShowWin32Error(RtlNtStatusToDosError(Status
));
84 EnablePrivilege(SE_SHUTDOWN_NAME
, FALSE
);
88 ShutDown_Hibernate(VOID
)
92 if (!EnablePrivilege(SE_SHUTDOWN_NAME
, TRUE
))
94 ShowWin32Error(GetLastError());
98 #ifdef NT_INITIATE_POWERACTION_IMPLEMENTED
99 Status
= NtInitiatePowerAction(PowerActionHibernate
,
100 PowerSystemHibernate
,
103 Status
= NtSetSystemPowerState(PowerActionHibernate
,
104 PowerSystemHibernate
,
108 if (!NT_SUCCESS(Status
))
109 ShowWin32Error(RtlNtStatusToDosError(Status
));
111 EnablePrivilege(SE_SHUTDOWN_NAME
, FALSE
);
115 ShutDown_PowerOff(VOID
)
117 /* Trick: on Windows, pressing the CTRL key forces shutdown via NT API */
118 BOOL ForceShutdown
= !!(GetKeyState(VK_CONTROL
) & 0x8000);
120 if (!EnablePrivilege(SE_SHUTDOWN_NAME
, TRUE
))
122 ShowWin32Error(GetLastError());
128 NTSTATUS Status
= NtShutdownSystem(ShutdownPowerOff
);
129 if (!NT_SUCCESS(Status
))
130 ShowWin32Error(RtlNtStatusToDosError(Status
));
134 // The choice of EWX_SHUTDOWN or EWX_POWEROFF may be done with NtPowerInformation
135 if (!ExitWindowsEx(EWX_POWEROFF
/* EWX_SHUTDOWN */, SHTDN_REASON_MAJOR_OTHER
| SHTDN_REASON_MINOR_OTHER
))
136 ShowWin32Error(GetLastError());
139 EnablePrivilege(SE_SHUTDOWN_NAME
, FALSE
);
143 ShutDown_Reboot(VOID
)
145 /* Trick: on Windows, pressing the CTRL key forces reboot via NT API */
146 BOOL ForceReboot
= !!(GetKeyState(VK_CONTROL
) & 0x8000);
148 if (!EnablePrivilege(SE_SHUTDOWN_NAME
, TRUE
))
150 ShowWin32Error(GetLastError());
156 NTSTATUS Status
= NtShutdownSystem(ShutdownReboot
);
157 if (!NT_SUCCESS(Status
))
158 ShowWin32Error(RtlNtStatusToDosError(Status
));
162 if (!ExitWindowsEx(EWX_REBOOT
, SHTDN_REASON_MAJOR_OTHER
| SHTDN_REASON_MINOR_OTHER
))
163 ShowWin32Error(GetLastError());
166 EnablePrivilege(SE_SHUTDOWN_NAME
, FALSE
);
170 ShutDown_LogOffUser(VOID
)
172 if (!ExitWindowsEx(EWX_LOGOFF
, SHTDN_REASON_MAJOR_OTHER
| SHTDN_REASON_MINOR_OTHER
))
173 ShowWin32Error(GetLastError());
177 ShutDown_SwitchUser(VOID
)
182 ShutDown_LockComputer(VOID
)
184 if (!LockWorkStation())
185 ShowWin32Error(GetLastError());
189 ShutDown_Disconnect(VOID
)
194 ShutDown_EjectComputer(VOID
)