The initiatesystemshutdown () function is mainly used in the program.
Cstring cpage1: showerror () { Lpvoid lpmsgbuf; : Formatmessage ( Format_message_allocate_buffer | Format_message_from_system | Format_message_ignore_inserts, Null, : Getlasterror (), Makelangid (lang_neutral, sublang_default), // default language (Lptstr) & lpmsgbuf, 0, Null ); Cstring strmsg (lpctstr) lpmsgbuf ); : Localfree (lpmsgbuf ); Return strmsg; } |
Shutdown:
Void cpage1: onshutdown () { Getdlgitem (idc_state)-> setwindowtext ("an attempt is being made to shut down the computer. Please wait ..."); Static handle htoken; Static token_privileges TP; Static luid; If (: openprocesstoken (getcurrentprocess (), Token_adjust_privileges | token_query, & Htoken )) { : Lookupprivilegevalue (null, se_shutdown_name, & luid ); TP. privilegecount = 1; TP. Privileges [0]. luid = luid; TP. Privileges [0]. Attributes = se_privilege_enabled; : Adjusttokenprivileges (htoken, false, & TP, sizeof (token_privileges), null, null ); } Updatedata (true ); Cstring STR; Str. Format ("// % s", m_strname ); If (! : Initiatesystemshutdown ( (Lptstr) (lpctstr) STR, "/N sorry, I want to shut down! ", M_ntime, M_balert = 0? True: false, M_ntype = 0? False: True )) { Getdlgitem (idc_state)-> setwindowtext (this-> showerror ()); } Else Getdlgitem (idc_state)-> setwindowtext ("shutdown successful "); } |
Cancel shutdown:
Void cpage1: onabort () { Getdlgitem (idc_state)-> setwindowtext ("an attempt is being made to cancel shutdown. Please wait ...");
Updatedata (true ); Cstring STR; Str. Format ("// % s", m_strname ); If (! : Abortsystemshutdown (lptstr) (lpctstr) Str )) { Getdlgitem (idc_state)-> setwindowtext (this-> showerror ()); } Else Getdlgitem (idc_state)-> setwindowtext ("canceled successfully "); } |
Sleep:
Void cpage1: onxiu () { If (MessageBox ("do you want to sleep? "," Shutdown program ", mb_yesno | mb_defbutton2 | mb_iconquestion) = idyes) { Static handle htoken; Static token_privileges TP; Static luid; If (: openprocesstoken (getcurrentprocess (), Token_adjust_privileges | token_query, & Htoken )) { : Lookupprivilegevalue (null, se_shutdown_name, & luid ); TP. privilegecount = 1; TP. Privileges [0]. luid = luid; TP. Privileges [0]. Attributes = se_privilege_enabled; : Adjusttokenprivileges (htoken, false, & TP, sizeof (token_privileges), null, null ); } : Setsystempowerstate (false, true ); } } |
For details, see the source code.