-
Notifications
You must be signed in to change notification settings - Fork 1
/
quick_reboot.cpp
59 lines (39 loc) · 1.55 KB
/
quick_reboot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <Windows.h>
bool PrivilegeMgr(const char* Name, bool Enable) {
HANDLE hToken; TOKEN_PRIVILEGES Priv; Priv.PrivilegeCount = 1;
auto CurrentProcess = GetCurrentProcess();
OpenProcessToken(CurrentProcess, TOKEN_ALL_ACCESS, &hToken);
Priv.Privileges[0].Attributes = Enable ? SE_PRIVILEGE_ENABLED : 0;
auto Ret = LookupPrivilegeValueA( nullptr, Name, &Priv.Privileges[0].Luid);
AdjustTokenPrivileges( hToken, false, &Priv, sizeof(Priv), nullptr, nullptr);
CloseHandle(hToken); return Ret;
}
typedef enum _SHUTDOWN_ACTION {
ShutdownNoReboot,
ShutdownReboot,
ShutdownPowerOff
} SHUTDOWN_ACTION, * PSHUTDOWN_ACTION;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
int ret = MessageBoxA(0, "Reboot (YES) or Shutdown (NO)", "Quick Reboot", MB_YESNOCANCEL);
typedef NTSTATUS(
NTAPI*
NtShutdownSystemFn)(
IN SHUTDOWN_ACTION Action);
HINSTANCE ntdll = GetModuleHandleA("ntdll.dll");
auto NtShutdownSystem = (NtShutdownSystemFn)GetProcAddress(ntdll, "NtShutdownSystem");
if (ret == IDYES) {
PrivilegeMgr("SeShutdownPrivilege", 1);
NtShutdownSystem(ShutdownReboot);
PrivilegeMgr("SeShutdownPrivilege", 0);
}
if (ret == IDNO) {
PrivilegeMgr("SeShutdownPrivilege", 1);
NtShutdownSystem(ShutdownPowerOff);
PrivilegeMgr("SeShutdownPrivilege", 0);
}
return 0;
}