程序中主要使用了InitiateSystemShutdown()这个函数。
ffffff>
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; } |
关机:
void CPage1::OnShutdown() {
GetDlgItem(IDC_STATE)->SetWindowText(“正在试图关闭计算机,请稍候…”);
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不好意思,我要关机了!”, m_nTime, m_bAlert==0?true:false, m_nType==0?false:true )) { GetDlgItem(IDC_STATE)->SetWindowText(this->ShowError()); } else GetDlgItem(IDC_STATE)->SetWindowText(“关机成功”);
} |
取消关机:
void CPage1::OnAbort() {
GetDlgItem(IDC_STATE)->SetWindowText(“正在试图取消关机操作,请稍候…”);
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(“取消成功”); } |
休眠:
void CPage1::OnXiu() { if(MessageBox(“确实要休眠吗?”,”关机程序”,MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDYES) { 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); } ::SetSystemPowerState(false,true); } } |
《关机 、待机和休眠编程》有 1 条评论