2020-06-11 16:09:06 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "RestartManagement.h"
|
|
|
|
|
|
|
|
#include <RestartManager.h>
|
|
|
|
#include <Psapi.h>
|
|
|
|
|
2020-12-15 20:16:09 +08:00
|
|
|
#include <common/utils/processApi.h>
|
2020-06-11 16:09:06 +08:00
|
|
|
|
|
|
|
void RestartProcess(const std::wstring& processName)
|
|
|
|
{
|
|
|
|
DWORD sessionHandle{};
|
|
|
|
WCHAR sessionKey[CCH_RM_SESSION_KEY + 1];
|
|
|
|
if (RmStartSession(&sessionHandle, 0, sessionKey) != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-07-28 00:53:29 +08:00
|
|
|
auto processHandles = getProcessHandlesByName(processName, PROCESS_QUERY_INFORMATION);
|
|
|
|
std::vector<RM_UNIQUE_PROCESS> pInfo;
|
|
|
|
for (const auto& hProcess : processHandles)
|
|
|
|
{
|
|
|
|
FILETIME creationTime{};
|
|
|
|
FILETIME _{};
|
|
|
|
if (GetProcessTimes(hProcess.get(), &creationTime, &_, &_, &_))
|
|
|
|
{
|
|
|
|
pInfo.emplace_back(RM_UNIQUE_PROCESS{ GetProcessId(hProcess.get()), creationTime });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 16:09:06 +08:00
|
|
|
if (pInfo.empty() ||
|
|
|
|
RmRegisterResources(sessionHandle, 0, nullptr, sizeof(pInfo), pInfo.data(), 0, nullptr) != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RmShutdown(sessionHandle, RmForceShutdown, nullptr);
|
|
|
|
RmRestart(sessionHandle, 0, nullptr);
|
|
|
|
RmEndSession(sessionHandle);
|
|
|
|
}
|