Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 3726|回复: 5

注销、关机、重启、Win8混合关机、待机、休眠、锁屏、关闭显示器单元

[复制链接]

该用户从未签到

发表于 2014-8-1 23:21:49 | 显示全部楼层 |阅读模式
[ 本帖最后由 talince 于 2014-8-1 23:35 编辑 ]\n\n注销、关机、重启、Win8混合关机、待机、休眠、锁屏、关闭显示器单元

unit ExitWindows;

interface

const
  ANYSIZE_ARRAY = 1;
  TOKEN_ADJUST_PRIVILEGES = 32;
  TOKEN_QUERY = 8;
  SE_PRIVILEGE_ENABLED = $2;

  EWX_FORCE = 4;//强行关闭,不管应用程序有没有挂起
  EWX_LOGOFF = 0;//注销
  EWX_POWEROFF = 8;//关机,关闭系统后切断电源,需主板支持
  EWX_REBOOT = 2;//重启
  EWX_SHUTDOWN = 1;//关闭系统后不切断电源,即使主板支持ATX电源管理
  EWX_FORCEIFHUNG = 16;//应用程序挂起一段时间后强行关闭
  EWX_QUICKRESOLVE= 32; // win32_winnt>=0x500
  EWX_RESTARTAPPS = 64; // win32_winnt>=0x600
  EWX_HYBRID_SHUTDOWN = $400000;//Win8混合关机快速启动

  MONITOR_ON = -1;//开启显示
  MONITOR_LOWPOWER = 1;//关闭显示器电源 :)---深度睡眠
  MONITOR_OFF = 2;//关闭显示器
  SC_MONITORPOWER = $F170;
  WM_SYSCOMMAND = $112;

type
  TLargeInteger = Int64;
  PLargeInteger = ^TLargeInteger;

  LUID = TlargeInteger;
  TLUID = LUID;
  PLUID = ^LUID;

  LUID_AND_ATTRIBUTES = packed record
  Luid : LUID;
  Attributes : DWORD;
  end;
  _LUID_AND_ATTRIBUTES = LUID_AND_ATTRIBUTES;
  TLUIDANDATTRIBUTES = LUID_AND_ATTRIBUTES;
  PLUIDANDATTRIBUTES = ^LUID_AND_ATTRIBUTES;

  TOKEN_PRIVILEGES = packed record
  PrivilegeCount : DWORD;
  Privileges : array[0..(ANYSIZE_ARRAY)-1] of LUID_AND_ATTRIBUTES;
  end;

  PTOKEN_PRIVILEGES = ^TOKEN_PRIVILEGES;
  LPTOKEN_PRIVILEGES = ^TOKEN_PRIVILEGES;
  _TOKEN_PRIVILEGES = TOKEN_PRIVILEGES;
  TTOKENPRIVILEGES = TOKEN_PRIVILEGES;
  PTOKENPRIVILEGES = ^TOKEN_PRIVILEGES;

  function Get_Shutdown_Privilege: Boolean;
  function ExitWindows_Logoff(Force:Boolean=True): Boolean;  //注销
  function ExitWindows_Poweroff(Force:Boolean=True): Boolean;//关机
  function ExitWindows_Reboot(Force:Boolean=True): Boolean;  //重启
  function ExitWindows_Shutdown(Force:Boolean=True): Boolean;//关机不切断电源
  function ExitWindows_HybridShutdown(Force:Boolean=True): Boolean;//Win8混合关机快速启动
  function ExitWindows_Standby(Force:Boolean=True): Boolean;//待机
  function ExitWindows_Hibernate(Force:Boolean=True): Boolean;//休眠
  function ExitWindows_Lock(): Boolean;//锁屏
  function ExitWindows_TurnoffScreen(MonitorFlag:Longint =MONITOR_OFF): Longint;//关闭显示器
  function ExitWindows_TurnoffScreen(hWnd: LongWord;MonitorFlag:Longint=MONITOR_OFF): Longint;//关闭显示器
  function ExtractFileNameF(const FileName: string;WithExeName : Boolean=False): string; // 解出文件名

  function SetSystemPowerState(fSuspend, fForce: Boolean): Boolean; stdcall;external 'kernel32.dll';//待机休眠
  function LockWorkStation: Boolean; stdcall;external 'user32.dll';//锁屏
  function ExitWindowsEx(uFlags:LongWord; dwReserved:DWORD):Boolean;stdcall;external 'user32.dll';

  function OpenProcessToken(ProcessHandle: LongWord; DesiredAccess: DWORD; var TokenHandle: LongWord): Boolean;stdcall;external 'advapi32.dll';
  function GetCurrentProcess:LongWord; external 'kernel32.dll';
  function LookupPrivilegeValueA(lpSystemName, lpName: PChar; var lpLuid: Int64): Boolean;stdcall;external 'advapi32.dll';
  function AdjustTokenPrivileges(TokenHandle: LongWord; DisableAllPrivileges: Boolean; const NewState: TTokenPrivileges; BufferLength: DWORD;
                               var PreviousState: TTokenPrivileges; var ReturnLength: DWORD): Boolean;stdcall;external 'advapi32.dll';
  function SendMessageA(hWnd: LongWord; Msg: LongWord; wParam: Longint; lParam: Longint): Longint; stdcall; external 'user32.dll';
  function FindWindowA(lpClassName:PChar; lpWindowName:PChar):LongWord; stdcall; external 'user32.dll';


implementation

function ExtractFileNameF(const FileName: string;WithExeName : Boolean=False): string;
var
  FPosition: longint;
begin
  FPosition := Length(FileName);
  while (FPosition > 0) and (FileName[FPosition] <> '\') and (FileName[FPosition] <> ':') do Dec(FPosition);
  if WithExeName then
    Result := Copy(FileName, FPosition + 1, Length(FileName) - FPosition)
  else
    Result := Copy(FileName, FPosition + 1, Length(FileName) - FPosition - 4);
end;

function Get_Shutdown_Privilege: Boolean; //获得用户关机特权,仅对Windows NT/2000/XP
var
  rl: Cardinal;
  hToken: Cardinal;
  tkp: TOKEN_PRIVILEGES;
  tkpNew: TOKEN_PRIVILEGES;
begin
  Result := False;
  OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
//if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then
  if LookupPrivilegeValueA(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then
  begin
    tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    tkp.PrivilegeCount := 1;
    AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTokenPrivileges), tkpNew, rl);
    Result := True;
  end;
end;

function ExitWindows_Do(uFlags:LongWord; Force:Boolean=True): Boolean;//操作系统
begin
  Result := False;
  if Get_Shutdown_Privilege then
    if Force then
      Result:=ExitWindowsEx(uFlags or EWX_FORCE,0)
    else
      Result:=ExitWindowsEx(uFlags,0);
end;

function ExitWindows_Logoff(Force:Boolean=True): Boolean;  //注销
begin
  Result:=ExitWindows_Do(EWX_LOGOFF,Force);
end;

function ExitWindows_Poweroff(Force:Boolean=True): Boolean;//关机
begin
  Result:=ExitWindows_Do(EWX_POWEROFF,Force);
end;

function ExitWindows_Reboot(Force:Boolean=True): Boolean;  //重启
begin
  Result:=ExitWindows_Do(EWX_REBOOT,Force);
end;

function ExitWindows_Shutdown(Force:Boolean=True): Boolean;//关机不切断电源
begin
  Result:=ExitWindows_Do(EWX_SHUTDOWN,Force);
end;

function ExitWindows_HybridShutdown(Force:Boolean=True): Boolean;//Win8混合关机
begin
  Result:=ExitWindows_Do(EWX_HYBRID_SHUTDOWN,Force);
end;

function ExitWindows_Standby(Force:Boolean=True): Boolean;//待机
begin
  Result := False;
  if Get_Shutdown_Privilege then
    Result := SetSystemPowerState(True, Force);
end;

function ExitWindows_Hibernate(Force:Boolean=True): Boolean;//休眠
begin
  Result := False;
  if Get_Shutdown_Privilege then
    Result := SetSystemPowerState(False, Force);
end;

function ExitWindows_Lock(): Boolean;//锁屏
begin
  Result:=LockWorkStation();
end;

function ExitWindows_TurnoffScreen(MonitorFlag:Longint=MONITOR_OFF): Longint;//关闭显示器
var
  hWnd : LongWord;
begin
  hWnd :=FindWindowA(nil, PChar(ExtractFileNameF(ParamStr(0))));
  Result:=SendMessageA(hWnd,WM_SYSCOMMAND,SC_MONITORPOWER,MonitorFlag);
end;

function ExitWindows_TurnoffScreen(hWnd: LongWord;MonitorFlag:Longint=MONITOR_OFF): Longint;//关闭显示器
begin
  Result:=SendMessageA(hWnd,WM_SYSCOMMAND,SC_MONITORPOWER,MonitorFlag);
end;

end.   
回复

使用道具 举报

该用户从未签到

发表于 2014-8-4 10:42:16 | 显示全部楼层
帮顶,加支持
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2014-8-4 12:24:00 | 显示全部楼层
mark
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2014-8-5 10:47:40 | 显示全部楼层
顶一下
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2014-8-12 19:43:00 | 显示全部楼层
不错。
谢谢分享。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2014-9-7 17:06:38 | 显示全部楼层
很好的源码。
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-12-3 23:48 , Processed in 0.059959 second(s), 9 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表