forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestartManagement.cpp
37 lines (33 loc) · 1.08 KB
/
RestartManagement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "pch.h"
#include "RestartManagement.h"
#include <RestartManager.h>
#include <Psapi.h>
#include <common/utils/processApi.h>
void RestartProcess(const std::wstring& processName)
{
DWORD sessionHandle{};
WCHAR sessionKey[CCH_RM_SESSION_KEY + 1];
if (RmStartSession(&sessionHandle, 0, sessionKey) != ERROR_SUCCESS)
{
return;
}
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 });
}
}
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);
}