-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSControl.pas
105 lines (83 loc) · 2.99 KB
/
OSControl.pas
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
unit OSControl;
interface
procedure SystemShutDown();
procedure SystemReboot();
procedure SystemLogOut();
procedure SystemSuspend(OnErrorMsg,OnErrorTitle:string);
procedure SystemHibernate(OnErrorMsg,OnErrorTitle:string);
procedure SystemPowerOff(OnErrorMsg,OnErrorTitle:string);
procedure SystemLock(OnErrorMsg,OnErrorTitle:string);
procedure ScreenTurnOff();
procedure ScreenTurnOn();
implementation
uses
Windows, Messages,Dialogs,Forms,NTPrivilege;
//************************* PRIVATE DECLARATIONS *************************//
function SetSuspendState(hibernate, forcecritical, disablewakeevent: boolean): boolean; stdcall; external 'powrprof.dll' name 'SetSuspendState';
function IsHibernateAllowed: boolean; stdcall; external 'powrprof.dll' name 'IsPwrHibernateAllowed';
function IsPwrSuspendAllowed: Boolean; stdcall; external 'powrprof.dll' name 'IsPwrSuspendAllowed';
function IsPwrShutdownAllowed: Boolean; stdcall; external 'powrprof.dll' name 'IsPwrShutdownAllowed';
function LockWorkStation: boolean; stdcall; external 'user32.dll' name 'LockWorkStation';
function SetMonitorBrightness:boolean; stdcall; external 'dxva2.lib' name 'SetMonitorBrightness';
//************************************************************************//
procedure SystemShutDown();
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
NTSetPrivilege(SE_SHUTDOWN_NAME, True);
ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0);
end;
procedure SystemReboot();
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
NTSetPrivilege(SE_SHUTDOWN_NAME, True);
ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);
end;
procedure SystemLogOut();
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
NTSetPrivilege(SE_SHUTDOWN_NAME, True);
ExitWindowsEx(EWX_LOGOFF or EWX_FORCE, 0);
end;
procedure SystemSuspend(OnErrorMsg,OnErrorTitle:string);
begin
if IsPwrSuspendAllowed then
SetSuspendState(false, false, false)
else
MessageBox(GetActiveWindow, PChar(OnErrorMsg),PChar(OnErrorTitle), MB_OK+MB_ICONWARNING);
end;
procedure SystemHibernate(OnErrorMsg,OnErrorTitle:string);
begin
if IsHibernateAllowed then
SetSuspendState(true, false, false)
else
MessageBox(GetActiveWindow, PChar(OnErrorMsg),PChar(OnErrorTitle), MB_OK+MB_ICONWARNING);
end;
procedure SystemPowerOff(OnErrorMsg,OnErrorTitle:string);
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
if IsPwrShutdownAllowed then
begin
NTSetPrivilege(SE_SHUTDOWN_NAME, True);
ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0);
end
else
MessageBox(GetActiveWindow, PChar(OnErrorMsg),PChar(OnErrorTitle), MB_OK+MB_ICONWARNING);
end;
procedure SystemLock(OnErrorMsg,OnErrorTitle:string);
begin
if not LockWorkStation then
MessageBox(GetActiveWindow, PChar(OnErrorMsg),PChar(OnErrorTitle), MB_OK+MB_ICONWARNING);
end;
procedure ScreenTurnOff();
begin
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
end;
procedure ScreenTurnOn();
begin
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
end;
end.