Windows Shutdown function exitwindowsex usage Daquan-Delphi (applicable to all windows operating platforms)

Source: Internet
Author: User

{===================================================== ========================================================== ==========================================================
Disable Windows function exitwindowsex (uint uflag, DWORD: dwreserved) Description:

 

Windows Control Switch: for example, disabling windows, restarting windows, exitwindowsex (uint uflags, DWORD dwreserved) is an API function that implements this function. If the prompt ewx_xxxx is undefined during complile, manually define these constants. By default, we do not need to manually define them.
Const
Ewx_force = 4; // close all programs and Log On As another user? (!! It should be "Force Execution no !!)
Ewx_logoff = 0; // restart the computer and switch to MS-DOS Mode
Ewx_reboot = 2; // restart the computer
Ewx_shutdown = 1; // disable the computer
Ewx_poweroff = 8; // power off
Ewx_forceifhung = $10; // don't remember. Who 'd like to check msdn?
Call method:
Exitwindowsex (ewx_reboot, 0); // restart the computer
Exitwindowsex (ewx_force + ewx_shutdown, 0); // Force Shutdown

However, bloggers often hear that this API is only valid for Windows 95/98/98SE/me, but not for Windows NT/2000/XP.
In fact, this is incorrect. This API is valid on the above platform, but before we execute this function on the Windows NT/2000/XP platform, you must obtain the shutdown privilege. In fact, even if the Windows NT/2000/XP system shuts down, you must follow this process.

View plainprint?
  1. The shutdown privilege function is as follows:
  2. Procedure get_shutdown_privilege; // get the user's shutdown privilege, only for Windows NT/2000/XP
  3. VaR
  4. RL: Cardinal;
  5. Htoken: Cardinal;
  6. Tkp: token_privileges;
  7. Begin
  8. Openprocesstoken (getcurrentprocess, token_adjust_privileges or token_query, htoken );
  9. If lookupprivilegevalue (nil, 'seshutdownprivilege ', tkp. Privileges [0]. luid) then
  10. Begin
  11. Tkp. Privileges [0]. attributes: = se_privilege_enabled;
  12. Tkp. privilegecount: = 1;
  13. Adjusttokenprivileges (htoken, false, tkp, 0, nil, rl );
  14. End;
  15. End;

The function for obtaining the shutdown privilege is procedure get_shutdown_privilege; // the user's shutdown privilege is obtained only for Windows NT/2000/XP var RL: Cardinal; htoken: Cardinal; tkp: token_privileges; begin openprocesstoken (getcurrentprocess, token_adjust_privileges or token_query, htoken); If lookupprivilegevalue (nil, 'seshutdownprivilege', tkp. privileges [0]. luid) then begin tkp. privileges [0]. attributes: = se_privilege_enabled; tkp. privilegecount: = 1; adjusttokenprivileges (htoken, false, tkp, 0, nil, rl); end;
Another shutdown API, initiatesystemshutdown (pchar (computer_name), pchar (hint_msg), time, force, reboot ); on the Windows NT/2000/XP platform, the system's shutdown Prompt window is automatically called.
Initiatesystemshutdown (pchar (computer_name), pchar (hint_msg), time, force, reboot );
// Shutdown the computer name, shutdown prompt information, stay duration, force shutdown, or restart
When we set computer_name to nil, the default value is local, such as initiatesystemshutdown (nil, nil, 0, true, false); // Force Shutdown

 

As we need to create a general Shutdown program, we need to judge the current operating system. This is relatively simple and the function is as follows:

  1. Function getoperatingsystem: string; // obtains the operating system information.
  2. VaR osverinfo: tosversioninfo;
  3. Begin
  4. Result: = '';
  5. Osverinfo. dwosversioninfosize: = sizeof (tosversioninfo );
  6. If getversionex (osverinfo) then
  7. Case osverinfo. dwplatformid
  8. Ver_platform_win32_nt:
  9. Begin
  10. Result: = 'windows NT/2000/XXX'
  11. End;
  12. Ver_platform_win32_windows:
  13. Begin
  14. Result: = 'windows 95/98/98SE/Me ';
  15. End;
  16. End;
  17. End;
  18. Execute the shutdown main function:
  19. Procedure shutdowncomputer;
  20. Begin
  21. If getoperatingsystem = 'windows NT/2000/XXX' then
  22. Begin
  23. Get_shutdown_privilege;
  24. // When this function is called, a system shutdown Prompt window appears, allowing you to cancel Shutdown
  25. // Initiatesystemshutdown (nil, 'shutdown prompt: I hate you, so I shut you down! ', 0, true, false );
  26. Exitwindowsex (ewx_shutdown + ewx_force + ewx_poweroff + ewx_forceifhung, 0 );
  27. End else
  28. Begin
  29. Exitwindowsex (ewx_shutdown + ewx_force + ewx_poweroff + ewx_forceifhung, 0 );
  30. End;
  31. End;
  32. ========================================================== ========================================================== ========================================================== =}
  33. Usage:
  34. Procedure tshutdownform. btn_poweroffclick (Sender: Object );
  35. Begin
  36. Shutdowncomputer;
  37. End;
Related Article

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.