Delphi for shutdown, compatible with all Windows systems

Source: Internet
Author: User

{============================================================================================================== =========
To turn off the Windows function ExitWindowsEx (UINT uflag,dword:dwreserved) Description:

Switches that control windows: such as shutting down Windows, restarting Windows, and so on, ExitWindowsEx (UINT Uflags,dword dwreserved), an API function that implements this functionality. If the complile prompt ewx_xxxx is undefined, then manually define these constants, by default, without our manual definition.
Const
ewx_force=4; Close all programs and log in as a different user? (!! should be "Enforce no" bar!! )
ewx_logoff=0; Restart the computer and switch to MS-DOS mode
ewx_reboot=2; Restart your computer
ewx_shutdown=1;//shutting down the computer
ewx_poweroff=8;//Power off
ewx_forceifhung=$10;//don't remember, anyone kindly check out MSDN
Call Method:
ExitWindowsEx (ewx_reboot,0); Restart your computer
ExitWindowsEx (ewx_force+ewx_shutdown,0); Force shutdown
However, bloggers often hear that this API is only valid under Windows 95/98/98se/me, but not in Windows NT/2000/XP.
In fact, this is not true, this API is valid under the above platform, but we must get the shutdown privilege before executing this function under the Windows NT/2000/XP platform, in fact, the Windows NT/2000/XP system itself shutdown also must go this process.
View Plainprint?
The Get Shutdown privilege function is as follows:
Procedure Get_shutdown_privilege; Get user shutdown privileges, 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
End

Another Shutdown Api,initiatesystemshutdown (PChar (computer_name), PChar (hint_msg), time,force,reboot); in Windows Nt/2000/ The Shutdown prompt window of the system itself is also automatically invoked under the XP platform.
InitiateSystemShutdown (PChar (computer_name), PChar (hint_msg), time,force,reboot);
Shutdown computer name, shutdown prompt information, length of stay, whether forcibly shutdown, whether to restart
When we set computer_name to nil, the default is native, such as InitiateSystemShutdown (nil,nil,0,true,false);//forced shutdown

Since we need to make a general shutdown program, it is relatively simple to judge the current operating system, and the function is as follows:
function getoperatingsystem:string;//get operating system Information
var osverinfo:tosversioninfo;
Begin
Result: = ';
Osverinfo.dwosversioninfosize: = SizeOf (Tosversioninfo);
If GetVersionEx (osverinfo) Then
Case Osverinfo.dwplatformid of
VER_PLATFORM_WIN32_NT:
Begin
Result: = ' Windows nt/2000/xp '
End
Ver_platform_win32_windows:
Begin
Result: = ' Windows 95/98/98se/me ';
End
End
End

Perform the main function of shutdown:
Procedure Shutdowncomputer;
Begin
If getoperatingsystem= ' Windows nt/2000/xp ' then
Begin
Get_shutdown_privilege;
Calling this function will cause the System Shutdown prompt window and allow the user to cancel the shutdown action
InitiateSystemShutdown (Nil, ' Shutdown tip: hate you so it's off you! ', 0,true,false);
ExitWindowsEx (ewx_shutdown+ewx_force+ewx_poweroff+ewx_forceifhung,0);
End Else
Begin
ExitWindowsEx (ewx_shutdown+ewx_force+ewx_poweroff+ewx_forceifhung,0);
End
End
=============================================================================================================== ==========}

Use:

Procedure Tshutdownform.btn_poweroffclick (Sender:object);
Begin
Shutdowncomputer;
End

Delphi for shutdown, compatible with all Windows systems

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.