这一段程序我调试通过了的你看一下是否能帮你,分都无所谓共同进步嘛
unit mainunit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
GroupBox1: TGroupBox;
Button1: TButton;
Button2: TButton;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function GetWinVer:Byte;
function SetPrivilege(PrivilegeName:string;Enable:Boolean):Boolean;
procedure ShutDownSystem(EWX_TYPE:Integer);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
EWX_FORCE=4;
EWX_LOGOFF=0;
EWX_SHUTDOWN=1;
EWX_REBOOT=2;
EWX_POWEROFF=8;
{ TForm1 }
function TForm1.GetWinVer: Byte;
var
OS:TOSVersionInfo;
begin
OS.dwOSVersionInfoSize:=sizeof(tosversioninfo);
GetVersionEx(OS);
case OS.dwPlatformId of
VER_PLATFORM_WIN32s : Result:=0;
VER_PLATFORM_WIN32_WINDOWS : Result:=1;
VER_PLATFORM_WIN32_NT :Result:=2;
end;
end;
function TForm1.SetPrivilege(PrivilegeName: string;
Enable: Boolean): Boolean;
var
NewState,PreviousState:TTokenPrivileges;
Token:THandle;
dwRetLen
WORD;
begin
Result:=false;
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY,token);
NewState.PrivilegeCount:=1;
if (LookupPrivilegeValue(nil,pchar(privilegeName),NewState.privileges[0].LUID)) then
begin
if enable then
NewState.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED
else
NewState.Privileges[0].Attributes:=0;
dwRetLen:=0;
Result:=AdjustTokenPrivileges(token,false,NewState,
sizeof(PreviousState),PreviousState,dwRetLen);
end;
CloseHandle(token);
end;
procedure TForm1.ShutDownSystem(EWX_TYPE: Integer);
begin
if getwinver=2 then
begin
SetPrivilege('SeShutdownPrivilege',true);
if (not ExitWindowsEx(EWX_TYPE,0)) then
SetPrivilege('SeShutdownPrivilege',false);
end
else
ExitWindowsEx(EWX_TYPE,0);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
RadioButton1.Checked:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if getwinver=2 then
begin
Label1.Caption:=Label1.Caption+'Windows NT';
end
else
begin
Label1.Caption:=Label1.Caption+'Windows 95/98';
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Application.MessageBox('你是否决定执行本操作?','系统询问',
MB_OKCANCEL+MB_OK)=IDOK then
begin
if (RadioButton1.Checked) then
ShutDownSystem(EWX_SHUTDOWN)
else if (RadioButton2.Checked) then
ShutDownSystem(EWX_REBOOT)
else if (RadioButton3.Checked) then
ShutDownSystem(EWX_LOGOFF)
else if (RadioButton4.Checked) then
ShutDownSystem(EWX_POWEROFF);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;
end.