2024年1月24日发(作者:)

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

Label1: TLabel;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

function GetOperatingSystem(): string;

procedure ShutDownComputer();

procedure Get_Shutdown_Privilege();

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure 1Click(Sender: TObject);

begin

ShutDownComputer();

end;

procedure ShutDownComputer();

begin

if GetOperatingSystem() = 'Windows NT/2000/XP' then

begin

Get_Shutdown_Privilege();

//调用此函数会出现系统关机提示窗口,并允许用户取消关机动作

InitiateSystemShutDown(nil, '关机提示:讨厌你所以关了你!', 3, True, False);

// InitiateSystemShutDown去掉的话就不显示提示窗口

ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF+EWX_FORCEIFHUNG,0);

end

else

begin

ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF+EWX_FORCEIFHUNG,0);

end;

end;

function GetOperatingSystem(): string; //获取操作系统信息

var

osVerInfo: TOSVersionInfo;

begin

Result:= '';

rsionInfoSize:= SizeOf(TOSVersionInfo);

if GetVersionEx(osVerInfo) then

case formId of

VER_PLATFORM_WIN32_NT:

begin

Result:= 'Windows NT/2000/XP'

end;

VER_PLATFORM_WIN32_WINDOWS:

begin

Result := 'Windows 95/98/98SE/Me';

end;

end;

end;

procedure Get_Shutdown_Privilege(); //获得用户关机特权,仅对Windows NT/2000/XP

var

rl: Cardinal;

hToken: Cardinal;

tkp: TOKEN_PRIVILEGES;

begin

OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,

hToken);

if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', eges[0].Luid) then

begin

eges[0].Attributes:= SE_PRIVILEGE_ENABLED;

egeCount:= 1;

AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);

end;

end;

end.