-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerop.cpp
More file actions
114 lines (96 loc) · 2.4 KB
/
powerop.cpp
File metadata and controls
114 lines (96 loc) · 2.4 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "powerop.h"
#ifdef Q_OS_WIN
#include <windows.h>
#include "powrprof.h"
#include "winuser.h"
#include "processthreadsapi.h"
#pragma comment(lib,"PowrProf.lib")
#pragma comment(lib,"User32.lib")
#pragma comment(lib,"Advapi32.lib")
#elif Q_OS_MACOS
#elif Q_OS_LINUX
#endif
PowerOp::PowerOp(QObject *parent)
: QObject{parent}
{
}
void PowerOp::shutdown()
{
#ifdef Q_OS_WIN
//强行关机..
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
//获取进程标志..
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return;
//获取关机特权的LUID..
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
//获取这个进程的关机特权..
AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return;
// 强制关闭计算机..
if ( !ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
return;
#elif Q_OS_MACOS
#elif Q_OS_LINUX
#endif
return;
}
void PowerOp::reboot()
{
#ifdef Q_OS_WIN
//重启计算机..
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
//获取进程标志..
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return;
//获取关机特权的LUID..
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
//获取这个进程的关机特权..
AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return;
// 强制重启计算机..
if ( !ExitWindowsEx(EWX_REBOOT| EWX_FORCE, 0))
return;
#elif Q_OS_MACOS
#elif Q_OS_LINUX
#endif
return;
}
void PowerOp::hibernate()
{
#ifdef Q_OS_WIN
// 让系统进入休眠 ..
SetSuspendState(TRUE, TRUE, FALSE);
#elif Q_OS_MACOS
#elif Q_OS_LINUX
#endif
return;
}
void PowerOp::sleep()
{
#ifdef Q_OS_WIN
// 让系统进入睡眠..
SetSuspendState(FALSE, TRUE, FALSE);
#elif Q_OS_MACOS
#elif Q_OS_LINUX
#endif
return;
}
void PowerOp::closeMonitor()
{
#ifdef Q_OS_WIN
//关闭显示器..
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
#elif Q_OS_MACOS
#elif Q_OS_LINUX
#endif
return;
}