Transferred from: http://blog.sina.com.cn/s/blog_611ab6c50100mznx.html
{============================================================================================================== =========
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;
- //Call this function to have 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