procedure TForm1.FormCreate(Sender: TObject); CONST ewx_force=4; ewx_logoff=0; ewx_reboot=2; ewx_shutdown=1; var how:word; begin how:=ewx_shutdown; exitwindowsex(how,0); end;
//在window98下直接调用ExitWindows就可以了,但在Win2k下不同,下面的程序可以让你在两种系统都可以用 function IsWin9x: Boolean; var OsInfo: TOSVERSIONINFO; begin OsInfo.dwOSVersionInfoSize := sizeof(TOSVERSIONINFO); GetVersionEx(OsInfo); Result := (OsInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS); end;function SetShutdownPrivilege(Enable: Boolean): Boolean; var PrevPrivileges: TTokenPrivileges; Privileges: TTokenPrivileges; Token: THandle; dwRetLen: DWord; begin Result := False; OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token); Privileges.PrivilegeCount := 1; if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Privileges.Privileges[0].LUID) then begin if Enable then Privileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED else Privileges.Privileges[0].Attributes := 0; dwRetLen := 0; Result := AdjustTokenPrivileges(Token, False, Privileges, SizeOf(PrevPrivileges), PrevPrivileges, dwRetLen); end; CloseHandle(Token); end;procedure Reboot; begin Application.Terminate; if IsWin9x then ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0) else begin SetShutdownPrivilege(True); ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0); SetShutdownPrivilege(False); end; end;procedure ShutDown; begin Application.Terminate; if IsWin9x then ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0) else begin SetShutdownPrivilege(True); ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0); SetShutdownPrivilege(False); end; end;
CONST
ewx_force=4;
ewx_logoff=0;
ewx_reboot=2;
ewx_shutdown=1;
var
how:word;
begin
how:=ewx_shutdown;
exitwindowsex(how,0);
end;
function IsWin9x: Boolean;
var
OsInfo: TOSVERSIONINFO;
begin
OsInfo.dwOSVersionInfoSize := sizeof(TOSVERSIONINFO);
GetVersionEx(OsInfo);
Result := (OsInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS);
end;function SetShutdownPrivilege(Enable: Boolean): Boolean;
var
PrevPrivileges: TTokenPrivileges;
Privileges: TTokenPrivileges;
Token: THandle;
dwRetLen: DWord;
begin
Result := False;
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
Privileges.PrivilegeCount := 1;
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Privileges.Privileges[0].LUID) then
begin
if Enable then
Privileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
Privileges.Privileges[0].Attributes := 0;
dwRetLen := 0;
Result := AdjustTokenPrivileges(Token, False, Privileges, SizeOf(PrevPrivileges), PrevPrivileges, dwRetLen);
end;
CloseHandle(Token);
end;procedure Reboot;
begin
Application.Terminate;
if IsWin9x then
ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0)
else
begin
SetShutdownPrivilege(True);
ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0);
SetShutdownPrivilege(False);
end;
end;procedure ShutDown;
begin
Application.Terminate;
if IsWin9x then
ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0)
else
begin
SetShutdownPrivilege(True);
ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0);
SetShutdownPrivilege(False);
end;
end;