Shutdown source code (For WinNT)

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.