[fpc-pascal]Windows ShutDown Privilege
memsom at interalpha.co.uk
memsom at interalpha.co.uk
Fri Dec 6 12:12:32 CET 2002
The following works in Delphi. Compile it as a Win32 console app. It's a brute
force shutdown, no apps get a chance to complain, and the data under NT/2k etc
is not backed up. It exits just about as fast as possible, and closes down
windows in about 30 seconds on my machine. Don't be surprised if shortcuts, run
menu last command ect and settings are lost between boots, they are not backed
up because of the reason I mentioned above.
Matt
program shutdown;
uses
SysUtils, Windows;
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
begin
TP.Privileges[ 0 ].Attributes :=
SE_PRIVILEGE_ENABLED;
end else
begin
TP.Privileges[ 0 ].Attributes :=
0;
end;
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
begin
// handle errors...
Result := False;
end;
SetPrivilege( 'SeShutdownPrivilege', False )
end else
begin
// handle errors...
Result := False;
end;
end;
procedure doexit;
begin
if not WinExit(EWX_FORCE + EWX_POWEROFF + EWX_SHUTDOWN) then begin
writeln('windows refused to cooperate.');
readln;
end;
end;
begin
doexit;
end.
---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/
More information about the fpc-pascal
mailing list