[fpc-pascal]Windows ShutDown Privilege
MPDJ
mpdj at btinternet.com
Sun Dec 8 11:11:37 CET 2002
Thanks for the Shutdown routine. I'm starting to get really annoying
with this now as I compiled the routine and ran it and exactly the
same problem appeared with the same error message stating I don't have
the correct privilege.
I added a CheckPrivilege call before and after the
AdjustTokenPrivileges and it appears the "SeShutdownPrivilege" has not
been altered.
Is there something strange about the Privilege settings with Windows
that I'm missing somehow? I've searched Google and all the routines
are very similar to each other yet despite tweaking the function the
error remains. I'll have to try compiling the function in Delphi just
to prove to myself I'm not going mad!
Friday, December 6, 2002, 11:12:32 AM, you wrote:
micu> The following works in Delphi. Compile it as a Win32 console app. It's a brute
micu> force shutdown, no apps get a chance to complain, and the data under NT/2k etc
micu> is not backed up. It exits just about as fast as possible, and closes down
micu> windows in about 30 seconds on my machine. Don't be surprised if shortcuts, run
micu> menu last command ect and settings are lost between boots, they are not backed
micu> up because of the reason I mentioned above.
micu> Matt
micu> program shutdown;
micu> uses
micu> SysUtils, Windows;
micu> function SetPrivilege(
micu> sPrivilegeName : string;
micu> bEnabled : boolean )
micu> : boolean;
micu> var
micu> TPPrev,
micu> TP : TTokenPrivileges;
micu> Token : THandle;
micu> dwRetLen : DWord;
micu> begin
micu> Result := False;
micu> OpenProcessToken(
micu> GetCurrentProcess,
micu> TOKEN_ADJUST_PRIVILEGES
micu> or TOKEN_QUERY,
micu> Token );
micu> TP.PrivilegeCount := 1;
micu> if( LookupPrivilegeValue(
micu> Nil,
micu> PChar( sPrivilegeName ),
micu> TP.Privileges[ 0 ].LUID ) )then
micu> begin
micu> if( bEnabled )then
micu> begin
micu> TP.Privileges[ 0 ].Attributes :=
micu> SE_PRIVILEGE_ENABLED;
micu> end else
micu> begin
micu> TP.Privileges[ 0 ].Attributes :=
micu> 0;
micu> end;
micu> dwRetLen := 0;
micu> Result := AdjustTokenPrivileges(
micu> Token,
micu> False,
micu> TP,
micu> SizeOf( TPPrev ),
micu> TPPrev,
micu> dwRetLen );
micu> end;
micu> CloseHandle( Token );
micu> end;
micu> //
micu> // iFlags:
micu> //
micu> // one of the following must be
micu> // specified
micu> //
micu> // EWX_LOGOFF
micu> // EWX_REBOOT
micu> // EWX_SHUTDOWN
micu> //
micu> // following attributes may be
micu> // combined with above flags
micu> //
micu> // EWX_POWEROFF
micu> // EWX_FORCE : terminate processes
micu> //
micu> function WinExit( iFlags : integer ) : boolean;
micu> begin
micu> Result := True;
micu> if( SetPrivilege( 'SeShutdownPrivilege', True ) )then
micu> begin
micu> if( not ExitWindowsEx( iFlags, 0 ) )then
micu> begin
micu> // handle errors...
micu> Result := False;
micu> end;
micu> SetPrivilege( 'SeShutdownPrivilege', False )
micu> end else
micu> begin
micu> // handle errors...
micu> Result := False;
micu> end;
micu> end;
micu> procedure doexit;
micu> begin
micu> if not WinExit(EWX_FORCE + EWX_POWEROFF + EWX_SHUTDOWN) then begin
micu> writeln('windows refused to cooperate.');
micu> readln;
micu> end;
micu> end;
micu> begin
micu> doexit;
micu> end.
micu> ---------------------------------------------
micu> This message was sent using Mistral WebMail.
micu> http://www.mistral.co.uk/
micu> _______________________________________________
micu> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
micu> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
More information about the fpc-pascal
mailing list