Procedure exitwindowsnt (uflags: integer );
VaR
Htoken: thandle;
Tkp, tkdumb: ttokenprivileges;
Dumbint: DWORD;
Begin
Fillchar (tkp, sizeof (tkp), 0 );
// Get a token for this process
If not (openprocesstoken (getcurrentprocess, token_adjust_privileges
Or token_query, htoken) then
Raise exception. Create ('openprocesyunen failed with Code'
+ Inttostr (getlasterror ));
// Get the luid for the shutdown privilege
Lookupprivilegevalue (nil, pchar ('seshutdownprivilege '),
Tkp. Privileges [0]. luid );
Tkp. privilegecount: = 1; // One privilege to set
Tkp. Privileges [0]. attributes: = se_privilege_enabled;
// Get the shutdown provolege for this process
Adjusttokenprivileges (htoken, false, tkp, sizeof (tkdumb), tkdumb, dumbint );
// Cannot test the return value of adjusttokenprivileges
If getlasterror <> error_success then
Raise exception. Create ('adjusttokenprivileges failed with Code'
+ Inttostr (getlasterror ));
// Shut down the system and for all applications to close
If not exitwindowsex (uflags, 0) then
Raise exception. Create ('exitwindowsex failed with Code'
+ Inttostr (getlasterror ));
End;
Procedure tform1.button1click (Sender: tobject );
Begin
Exitwindowsnt (ewx_poweroff );
End;
Procedure tform1.button2click (Sender: tobject );
Begin
Exitwindowsnt (ewx_reboot );
End;
Shutdown 2
Procedure ttvapithing. Shutdown;
Const
Se_shutdown_name = 'seshutdownprivilege '; // Borland forgot this Declaration
VaR
Htoken: thandle;
Tkp: ttokenprivileges;
Tkpo: ttokenprivileges;
Zero: DWORD;
Begin
If pos ('windows nt ', osversion) = 1 then // we 've got to do a whole buch of things
Begin
Zero: = 0;
If not openprocesstoken (getcurrentprocess (), token_adjust_privileges or token_query, htoken) then
Begin
MessageBox (0, 'exit error', 'openprocesstoken () failed', mb_ OK );
Exit;
End; // if not openprocesstoken (getcurrentprocess (), token_adjust_privileges or token_query, htoken)
If not openprocesstoken (getcurrentprocess (), token_adjust_privileges or token_query, htoken) then
Begin
MessageBox (0, 'exit error', 'openprocesstoken () failed', mb_ OK );
Exit;
End; // if not openprocesstoken (getcurrentprocess (), token_adjust_privileges or token_query, htoken)
// Se_shutdown_name
If not lookupprivilegevalue (nil, 'seshutdownprivilege', tkp. Privileges [0]. luid) then
Begin
MessageBox (0, 'exit error', 'lookupprivilegevalue () failed', mb_ OK );
Exit;
End; // if not lookupprivilegevalue (nil, 'seshutdownprivilege', tkp. Privileges [0]. luid)
Tkp. privilegecount: = 1;
Tkp. Privileges [0]. attributes: = se_privilege_enabled;
Adjusttokenprivileges (htoken, false, tkp, sizeof (ttokenprivileges), tkpo, zero );
If Boolean (getlasterror () then
Begin
MessageBox (0, 'exit error', 'adjusttokenprivileges () failed', mb_ OK );
Exit;
End // If Boolean (getlasterror ())
Else
Exitwindowsex (ewx_force or ewx_shutdown or ewx_poweroff, 0 );
End // If osversion = 'windows nt'
Else
Begin // just shut the machine down
Exitwindowsex (ewx_force or ewx_shutdown, 0 );
End; // else