RestartManagement.cpp
1 #include "pch.h" 2 #include "RestartManagement.h" 3 4 #include <RestartManager.h> 5 #include <Psapi.h> 6 7 #include <common/utils/processApi.h> 8 9 void RestartProcess(const std::wstring& processName) 10 { 11 DWORD sessionHandle{}; 12 WCHAR sessionKey[CCH_RM_SESSION_KEY + 1]; 13 if (RmStartSession(&sessionHandle, 0, sessionKey) != ERROR_SUCCESS) 14 { 15 return; 16 } 17 auto processHandles = getProcessHandlesByName(processName, PROCESS_QUERY_INFORMATION); 18 std::vector<RM_UNIQUE_PROCESS> pInfo; 19 for (const auto& hProcess : processHandles) 20 { 21 FILETIME creationTime{}; 22 FILETIME _{}; 23 if (GetProcessTimes(hProcess.get(), &creationTime, &_, &_, &_)) 24 { 25 pInfo.emplace_back(RM_UNIQUE_PROCESS{ GetProcessId(hProcess.get()), creationTime }); 26 } 27 } 28 29 if (pInfo.empty() || 30 RmRegisterResources(sessionHandle, 0, nullptr, sizeof(pInfo), pInfo.data(), 0, nullptr) != ERROR_SUCCESS) 31 { 32 return; 33 } 34 RmShutdown(sessionHandle, RmForceShutdown, nullptr); 35 RmRestart(sessionHandle, 0, nullptr); 36 RmEndSession(sessionHandle); 37 }