//重启系统 HANDLE app; DWORD num; TOKEN_PRIVILEGES tok; OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&app); tok.PrivilegeCount=1; LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&(tok.Privileges[0].Luid)); tok.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(app,false,&tok,sizeof(tok),&tok,&num); ExitWindowsEx(EWX_REBOOT,0);
以下是通过命令行的方式来实现的(Shutdown -r -s 等)
void CreateTestFuncProcess(CString strCmd) { TCHAR* lpCmdLine = (LPTSTR)((LPCTSTR)strCmd); STARTUPINFO si = {sizeof(si)}; si.dwFlags |= STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; PROCESS_INFORMATION pi; if(!CreateProcess(NULL,lpCmdLine,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)) { CString strDbg = _T(""); strDbg.Format(_T("CreateProcess Failed code : %d"),GetLastError()); MessageBox(strDbg,L"Error",MB_OK); }//else MessageBox(NULL,_T("Create process OK!"),MessageBoxName,MB_OK); }
//程序重启 TCHAR strPath[MAX_PATH]; ::GetModuleFileName(AfxGetInstanceHandle(), strPath, sizeof(strPath) / sizeof(TCHAR)); TCHAR strCurDirectory[MAX_PATH]; ::GetCurrentDirectory(sizeof(strCurDirectory) / sizeof(TCHAR), strCurDirectory); DWORD dwProcessId = ::GetCurrentProcessId(); HINSTANCE hInst = ::ShellExecute(NULL, NULL, strPath, L"/Reboot", strCurDirectory, SW_SHOWNORMAL);
//随机启动 void setautorun(CString strValue) { CString strPath = L"SOFTWARE//Microsoft//Windows//CurrentVersion//Run"; CRegKey regkey; LONG lResult; lResult = regkey.Open(HKEY_LOCAL_MACHINE,LPCTSTR(strPath),KEY_ALL_ACCESS); if(lResult != ERROR_SUCCESS) { AfxMessageBox(L"error: "); return; } regkey.SetValue(strValue,L"my"); regkey.Close(); }