要权限,用这个单元 UNIT ShutDown_NT;interfaceuses Windows; function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean; function WinExit (iFlags: integer) : Boolean;implementationfunction SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean; var TPPrev, TP : TTokenPrivileges; Token : THandle; dwRetLen : DWORD; begin result := False; OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token); TP.PrivilegeCount := 1; if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then begin if bEnabled then TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED else TP.Privileges[0].Attributes := 0; dwRetLen := 0; result := AdjustTokenPrivileges (Token, False, TP, SizeOf (TPPrev), TPPrev, dwRetLen) 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 WinExit (iFlags: integer) : Boolean; begin result := true; if SetPrivilege ('SeShutdownPrivilege', true) then begin if (not ExitWindowsEx (iFlags, 0)) then // handle errors... result := False; SetPrivilege ('SeShutdownPrivilege', False); end else // handle errors... result := False; end;end.
function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean; var TPPrev, TP : TTokenPrivileges; Token : THandle; dwRetLen : DWORD; begin result := False; OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token); TP.PrivilegeCount := 1; if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then begin if bEnabled then TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED else TP.Privileges[0].Attributes := 0; dwRetLen := 0; result := AdjustTokenPrivileges (Token, False, TP, SizeOf (TPPrev), TPPrev, dwRetLen) 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 WinNTExit (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;// EWX_LOGOFF //注消 // EWX_REBOOT //重启 // EWX_SHUTDOWN //关机 //改变iFlags为其中任何一个,可以实现其中一个功能,不受系统限制,其中已经作了系统判断 procedure ReBoot(iFlags: integer); begin case Win32Platform of VER_PLATFORM_WIN32_WINDOWS: begin Win32Check(ExitWindowsEx(iFlags+EWX_FORCE, 0)); end; VER_PLATFORM_WIN32_NT: begin WinNTExit(iFlags+EWX_FORCE); end; else begin Win32Check(ExitWindowsEx(iFlags+EWX_FORCE, 0)); end; end; end;
function ShutDownSystem():BOOL; var hProcess,hAccessToken:THandle; LUID_AND_ATTRIBUTES:TLUIDAndAttributes; TOKEN_PRIVILEGES: TTokenPrivileges; BufferIsNull:DWORD; const SE_SHUTDOWN_NAME='SeShutdownPrivilege'; begin hProcess:=GetCurrentProcess(); OpenProcessToken(hprocess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hAccessToken); LookupPrivilegeValue(Nil,SE_SHUTDOWN_NAME,LUID_AND_ATTRIBUTES.Luid); LUID_AND_ATTRIBUTES.Attributes:=SE_PRIVILEGE_ENABLED; TOKEN_PRIVILEGES.PrivilegeCount:=1; TOKEN_PRIVILEGES.Privileges[0]:=LUID_AND_ATTRIBUTES; BufferIsNull:=0; AdjustTokenPrivileges(hAccessToken,False,TOKEN_PRIVILEGES,sizeof(TOKEN_PRIVILEGES),Nil,BufferIsNull); ExitWindowsEx(EWX_POWEROFF+EWX_FORCE, 0); ShutDownSystem:=True; end;
UNIT ShutDown_NT;interfaceuses
Windows; function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;
function WinExit (iFlags: integer) : Boolean;implementationfunction SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;
var
TPPrev,
TP : TTokenPrivileges;
Token : THandle;
dwRetLen : DWORD;
begin
result := False;
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
TP.PrivilegeCount := 1;
if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then
begin
if bEnabled then
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
TP.Privileges[0].Attributes := 0; dwRetLen := 0;
result := AdjustTokenPrivileges (Token, False, TP, SizeOf (TPPrev), TPPrev,
dwRetLen)
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 WinExit (iFlags: integer) : Boolean;
begin
result := true;
if SetPrivilege ('SeShutdownPrivilege', true) then
begin
if (not ExitWindowsEx (iFlags, 0)) then
// handle errors...
result := False;
SetPrivilege ('SeShutdownPrivilege', False);
end
else
// handle errors...
result := False;
end;end.
var
TPPrev,
TP : TTokenPrivileges;
Token : THandle;
dwRetLen : DWORD;
begin
result := False;
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
TP.PrivilegeCount := 1;
if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then
begin
if bEnabled then
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
TP.Privileges[0].Attributes := 0;
dwRetLen := 0;
result := AdjustTokenPrivileges (Token, False, TP, SizeOf (TPPrev), TPPrev,
dwRetLen)
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 WinNTExit (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;// EWX_LOGOFF //注消
// EWX_REBOOT //重启
// EWX_SHUTDOWN //关机
//改变iFlags为其中任何一个,可以实现其中一个功能,不受系统限制,其中已经作了系统判断
procedure ReBoot(iFlags: integer);
begin
case Win32Platform of
VER_PLATFORM_WIN32_WINDOWS:
begin
Win32Check(ExitWindowsEx(iFlags+EWX_FORCE, 0));
end;
VER_PLATFORM_WIN32_NT:
begin
WinNTExit(iFlags+EWX_FORCE);
end;
else
begin
Win32Check(ExitWindowsEx(iFlags+EWX_FORCE, 0));
end;
end;
end;
var
hProcess,hAccessToken:THandle;
LUID_AND_ATTRIBUTES:TLUIDAndAttributes;
TOKEN_PRIVILEGES: TTokenPrivileges;
BufferIsNull:DWORD;
const
SE_SHUTDOWN_NAME='SeShutdownPrivilege';
begin
hProcess:=GetCurrentProcess();
OpenProcessToken(hprocess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hAccessToken);
LookupPrivilegeValue(Nil,SE_SHUTDOWN_NAME,LUID_AND_ATTRIBUTES.Luid);
LUID_AND_ATTRIBUTES.Attributes:=SE_PRIVILEGE_ENABLED;
TOKEN_PRIVILEGES.PrivilegeCount:=1;
TOKEN_PRIVILEGES.Privileges[0]:=LUID_AND_ATTRIBUTES;
BufferIsNull:=0;
AdjustTokenPrivileges(hAccessToken,False,TOKEN_PRIVILEGES,sizeof(TOKEN_PRIVILEGES),Nil,BufferIsNull);
ExitWindowsEx(EWX_POWEROFF+EWX_FORCE, 0);
ShutDownSystem:=True;
end;