[Installer] Restart explorer.exe programatically after successful update (#4215)

* Restart explorer.exe programatically after successful update

* Move RestartManager related code into common

* Add newline at the end of files

* Note that explorer.exe should not be localized string
This commit is contained in:
vldmr11080
2020-06-11 10:09:06 +02:00
committed by GitHub
parent d76234c112
commit b0a25f59d9
7 changed files with 101 additions and 9 deletions

View File

@@ -0,0 +1,64 @@
#include "pch.h"
#include "RestartManagement.h"
#include <RestartManager.h>
#include <Psapi.h>
std::vector<RM_UNIQUE_PROCESS> GetProcessInfoByName(const std::wstring& processName)
{
DWORD bytesReturned{};
std::vector<DWORD> processIds{};
processIds.resize(1024);
DWORD processIdSize{ (DWORD)processIds.size() * sizeof(DWORD) };
EnumProcesses(processIds.data(), processIdSize, &bytesReturned);
while (bytesReturned == processIdSize)
{
processIdSize *= 2;
processIds.resize(processIdSize / sizeof(DWORD));
EnumProcesses(processIds.data(), processIdSize, &bytesReturned);
}
std::vector<RM_UNIQUE_PROCESS> pInfos{};
for (const DWORD& processId : processIds)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (hProcess)
{
wchar_t name[MAX_PATH];
if (GetProcessImageFileName(hProcess, name, MAX_PATH) > 0)
{
if (processName == PathFindFileName(name))
{
FILETIME creationTime{};
FILETIME exitTime{};
FILETIME kernelTime{};
FILETIME userTime{};
if (GetProcessTimes(hProcess, &creationTime, &exitTime, &kernelTime, &userTime))
{
pInfos.push_back({ processId, creationTime });
}
}
}
CloseHandle(hProcess);
}
}
return pInfos;
}
void RestartProcess(const std::wstring& processName)
{
DWORD sessionHandle{};
WCHAR sessionKey[CCH_RM_SESSION_KEY + 1];
if (RmStartSession(&sessionHandle, 0, sessionKey) != ERROR_SUCCESS)
{
return;
}
std::vector<RM_UNIQUE_PROCESS> pInfo = GetProcessInfoByName(processName);
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);
}

View File

@@ -0,0 +1,5 @@
#pragma once
#include <string>
void RestartProcess(const std::wstring& processName);

View File

@@ -125,6 +125,7 @@
<ClInclude Include="keyboard_layout.h" />
<ClInclude Include="keyboard_layout_impl.h" />
<ClInclude Include="notifications.h" />
<ClInclude Include="RestartManagement.h" />
<ClInclude Include="shared_constants.h" />
<ClInclude Include="timeutil.h" />
<ClInclude Include="two_way_pipe_message_ipc.h" />
@@ -161,6 +162,7 @@
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="RestartManagement.cpp" />
<ClCompile Include="settings_helpers.cpp" />
<ClCompile Include="settings_objects.cpp" />
<ClCompile Include="icon_helpers.cpp" />

View File

@@ -111,6 +111,9 @@
<ClInclude Include="two_way_pipe_message_ipc.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RestartManagement.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="d2d_svg.cpp">
@@ -180,6 +183,9 @@
<ClCompile Include="two_way_pipe_message_ipc.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RestartManagement.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />