DelphiFAQ Home Search:

Adjust the Windows NT privileges to reboot

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

If you need to reboot the PC in Delphi 3 on a Windows NT machine, you need to get the right privileges for this.
The following code provides a procedure to adjust the privileges and then boots the PC.

Unit BootNT;

// uses ....

Interface

Function AdjustTokenPrivileges (TokenHandle: THandle;
  DisableAllPrivileges: BOOL; Const NewState: TTokenPrivileges;
  BufferLength: DWORD; PreviousState: PTokenPrivileges;
  ReturnLength: PWORD) : BOOL;
  Stdcall;
Implementation
Function AdjustTokenPrivileges;
  External 'advapi32.dll' Name 'AdjustTokenPrivileges';
{$R *.DFM}
Procedure RebootWindowsNT;
Const
  AdjustMsg = 'Could not adjust the Privilege.' + #13#10;
  TokenMsg = 'Could not open the Token.' + #13#10;
  FailMsg = 'Rebooting Windows is failed';
Var
  Success : Boolean;
  TokenPriv : TTokenPrivileges;
  TokenHandle : THandle;
  CurrentProc : THandle;
Begin
  Success := False;
  CurrentProc := GetCurrentProcess;
  If OpenProcessToken (CurrentProc,
                       TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
                       TokenHandle) Then
  Begin
    // get privilege
    If LookupPrivilegeValue (Nil, 'SeShutdownPrivilege',
	     TokenPriv.Privileges[0].LUID) Then
    Begin
      TokenPriv.PrivilegeCount := 1;
      TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      // Reboot system
      If AdjustTokenPrivileges (TokenHandle, False, TokenPriv,
	                            0, Nil, Nil) Then
        Success := ExitWindowsEx (EWX_REBOOT, 0);
      If not Success Then
        ShowMessage (AdjustMsg + FailMsg)
    End
  End
  Else
    ShowMessage (TokenMsg + FailMsg);
End;

Procedure TForm1.Button1Click (Sender: TObject);
Begin
  RebootWindowsNT;
End;
End.
You don't like the formatting? Check out SourceCoder then!

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: