InitiateSystemShutdown () functions are 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 ( "is trying to shut down your computer, please wait ..."); static HANDLE hToken; static TOKEN_PRIVILEGES tp; static LUID 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 is sorry, I have 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 success");}
Cancellation:
Void cpage1 :: OnAbort () {getdlgitem (idc_state) -> setWindowText ("I am trying to cancel the shutdown operation, please wait ..."); Updatedata (true); cstract str ;format ("% s", m_strname ); If (! :: Abortsystemshutdown ((lpctstr) str) {getdlgitem (iDC_State) -> setWindowText (this-> showerror ());} else getdlgitem (IDC_State) -> setWindowText ("cancel success") Hibernation:
Void CPAGE1 :: OnXiu () {ix ("Do you want to sleep?", "Shutdown Program", MB_YESNO | MB_DEFB /TON2 | MB_ICONQUESTION == iDyes) {static handle htokeen; static token_privileges tp; static Luid 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;} :: setsystempower (false, true);}
Please see the source code for details.