关机函数

作者在 2010-01-23 21:21:16 发布以下内容
unit Unit;

interface

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

type
  TnForm = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    procedure GetPrivilege;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}
procedure TForm1.GetPrivilege;
var
  NewState:       TTokenPrivileges;
  lpLuid:         Int64;
  ReturnLength:   DWord;
  ToKenHandle:    Cardinal;
begin
  OpenProcessToken(GetCurrentProcess,
                   TOKEN_ADJUST_PRIVILEGES
                   OR TOKEN_ALL_ACCESS
                   OR STANDARD_RIGHTS_REQUIRED
                   OR TOKEN_QUERY,ToKenHandle);
  LookupPrivilegeValue(nil,'SeShutdownPrivilege',lpLuid);
  NewState.PrivilegeCount:=1;
  NewState.Privileges[0].Luid:=lpLuid;
  NewState.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
  ReturnLength:=0;
  AdjustTokenPrivileges(ToKenHandle,False,NewState,0,nil,ReturnLength);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  GetPrivilege;
  ExitWindowsEx(EWX_REBOOT OR EWX_POWEROFF, 0);  //重启
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  GetPrivilege;
  ExitWindowsEx(EWX_SHUTDOWN OR EWX_POWEROFF, 0);   //关机
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ExitWindowsEx(EWX_LOGOFF, 0);  // 注销
end;

end.

编程点滴 | 阅读 1089 次
文章评论,共0条
游客请输入验证码
浏览2354423次