Delphi 操作Windows系统睡眠-防止系统/电脑 进入睡眠或关闭显示器

Delphi 操作Windows系统睡眠-防止系统/电脑 进入睡眠或关闭显示器

1、防止进入睡眠

1.1、引用单元

Delphi 单元:

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
unit SystemCriticalU;
 
interface
 
uses
  Windows;
 
type
  TSystemCritical = class
  private
    FIsCritical: Boolean;
    procedure SetIsCritical(const Value: Boolean) ;
  protected
    procedure UpdateCritical(Value: Boolean) ; virtual;
  public
    constructor Create;
    property IsCritical: Boolean read FIsCritical write SetIsCritical;
  end;
 
var
  SystemCritical: TSystemCritical;
 
implementation
 
{ TSystemCritical }
// REF: http://msdn.microsoft.com/en-us/library/aa373208.aspx
type
  EXECUTION_STATE = DWORD;
   
const
  ES_SYSTEM_REQUIRED = $00000001;
  ES_DISPLAY_REQUIRED = $00000002;
  ES_USER_PRESENT = $00000004;
  ES_AWAYMODE_REQUIRED = $00000040;
  ES_CONTINUOUS = $80000000;
   
  KernelDLL = 'kernel32.dll';
 
{
  SetThreadExecutionState Function
  Enables an application to inform the system that it is in use,
  thereby preventing the system from entering sleep or turning off the
  display while the application is running.
}
procedure SetThreadExecutionState(ESFlags: EXECUTION_STATE);
  stdcall; external kernel32 name 'SetThreadExecutionState';
 
constructor TSystemCritical.Create;
begin
  inherited;
  FIsCritical := False;
end;
 
procedure TSystemCritical.SetIsCritical(const Value: Boolean) ;
begin
  if FIsCritical = Value then
    Exit;
  FIsCritical := Value;
  UpdateCritical(FIsCritical);
end;
 
procedure TSystemCritical.UpdateCritical(Value: Boolean) ;
begin
  if Value then
    // 防止睡眠空闲超时和关机。
    SetThreadExecutionState(ES_SYSTEM_REQUIRED or ES_CONTINUOUS)
  else
    //清除执行状态标志以禁用离开模式并允许
    // 系统空闲以正常睡眠
    SetThreadExecutionState(ES_CONTINUOUS);
end;
 
initialization
 
SystemCritical := TSystemCritical.Create;
 
finalization
 
SystemCritical.IsCritical := False;
SystemCritical.Free;
 
end. 

引用示例:

1
2
3
4
5
6
7
SystemCritical.IsCritical = true;
try
  // 这里做关键操作
  // 不会进入睡眠和关闭显示器
finally
  SystemCritical.IsCritical = false;
end;

 

1.2 直接使用WinAPI函数,点击查看:SetThreadExecutionState 

 

 

2、使电脑进入睡眠

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//提升进程令牌函数
function AdjustProcessPrivilege(ProcessHandle: THandle; Token_Name: Pchar): boolean;
var
  Token: THandle;
  TokenPri: _TOKEN_PRIVILEGES;
  ProcessDest: int64;
  l: DWORD;
begin
  Result := False;
  if OpenProcessToken(ProcessHandle, TOKEN_Adjust_Privileges, Token) then
  begin
    if LookupPrivilegeValue(nil, Token_Name, ProcessDest) then
    begin
      TokenPri.PrivilegeCount := 1;
      TokenPri.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      TokenPri.Privileges[0].Luid := ProcessDest;
      l := 0;
      //更新进程令牌,成功返回TRUE
      if AdjustTokenPrivileges(Token, False, TokenPri, sizeof(TokenPri), nil, l) then
        Result := True;
    end;
  end;
end;  

引用示例:

1
2
3
4
5
6
if AdjustProcessPrivilege(GetCurrentProcess,'SeShutdownPrivilege') then//提升权限
begin
  SetSystemPowerState(false,TRUE); //进入睡眠
end else begin
  //
end;

  

  

 

 

创建时间:2020.11.03  更新时间:

 

posted on   滔Roy  阅读(715)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本

导航

点击右上角即可分享
微信分享提示