⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 可以强行关机、注销即重新启动电脑等操作。
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TfrmMain = class(TForm)
    rbSafe: TRadioButton;
    rbNoSafe: TRadioButton;
    btnPowerOff: TButton;
    procedure btnPowerOffClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.DFM}

function SetPrivilege(sPrivilegeName:string;bEnabled:Boolean):Boolean;
var
  TPPrev,
  TP : TTokenPrivileges;
  Token : THandle;
  dwRetLen : DWord;
begin
  Result := False;

  //opens the access token associated with a process.
  OpenProcessToken(
     GetCurrentProcess, //handle to process
     TOKEN_ADJUST_PRIVILEGES //Required to change the privileges specified in an access token.
     or TOKEN_QUERY, //Required to query the contents of an access token.
     Token);

  TP.PrivilegeCount := 1;
  //retrieves the locally unique identifier (LUID) used on a specified system to
  //locally represent the specified privilege name.
  if( LookupPrivilegeValue(
      Nil, //attempts to find the privilege name on the local system.
      PChar( sPrivilegeName ), // address of string specifying the privilege
      TP.Privileges[ 0 ].LUID ) // address of locally unique identifier
      )then begin
    if( bEnabled )then begin//Give this privileges
      TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
    end else begin //NOT Give this privileges
      TP.Privileges[ 0 ].Attributes := 0;
    end;

    dwRetLen := 0;
    //enables or disables privileges in the specified access token.
    Result := AdjustTokenPrivileges(
        Token, // handle to token that contains privileges
        False, //modifies privileges
        TP, // pointer to new privilege information
        SizeOf( TPPrev ), // size, in bytes, of the TPPrev buffer
        TPPrev, // receives original state of changed privileges
        dwRetLen // receives required size of the TPPrev buffer
        );
  end;
  CloseHandle(Token);
end;

//iFlags:
// one of the following must be specified
// EWX_LOGOFF
// EWX_REBOOT
// EWX_SHUTDOWN
// following attributes may be combined with above flags
// EWX_POWEROFF
// EWX_FORCE : terminate processes
function WinExitInNT(iFlags:Integer):Boolean;
begin
  Result := True;
  if (SetPrivilege('SeShutdownPrivilege', True)) then begin
    if(not ExitWindowsEx(iFlags, 0))then begin
       Result := False;
    end;
    SetPrivilege('SeShutdownPrivilege', False)
  end else begin
    Result := False;
  end;
end;

procedure TfrmMain.btnPowerOffClick(Sender: TObject);
begin
  if rbSafe.Checked then WinExitInNT(EWX_POWEROFF) //安全关机
  else                   WinExitInNT(EWX_POWEROFF+EWX_FORCE);//强行关机
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -