Skip to content

Commit

Permalink
Merge pull request #6694 from spycrab/updater_uac
Browse files Browse the repository at this point in the history
Updater: Implement UAC support
  • Loading branch information
delroth committed Apr 25, 2018
2 parents c51c044 + 6e4c5d7 commit e0f677c
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions Source/Core/Updater/Main.cpp
Expand Up @@ -3,6 +3,7 @@
// Refer to the license.txt file included.

#include <windows.h>
#include <ShlObj.h>

#include <OptionParser.h>
#include <algorithm>
Expand Down Expand Up @@ -674,11 +675,18 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
return 1;
Options opts = std::move(*maybe_opts);

bool need_admin = false;

if (opts.log_file)
{
log_fp = _wfopen(UTF8ToUTF16(*opts.log_file).c_str(), L"w");
if (!log_fp)
{
log_fp = stderr;
// Failing to create the logfile for writing is a good indicator that we need administrator
// priviliges
need_admin = true;
}
else
atexit(FlushLog);
}
Expand All @@ -702,8 +710,25 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
fprintf(log_fp, "Completed! Proceeding with update.\n");
}

std::thread thread(UI::MessageLoop);
thread.detach();
if (need_admin)
{
if (IsUserAnAdmin())
{
FatalError("Failed to write to directory despite administrator priviliges.");
return 1;
}

wchar_t path[MAX_PATH];
if (GetModuleFileName(hInstance, path, sizeof(path)) == 0)
{
FatalError("Failed to get updater filename.");
return 1;
}

// Relaunch the updater as administrator
ShellExecuteW(nullptr, L"runas", path, pCmdLine, NULL, SW_SHOW);
return 0;
}

UI::SetDescription("Fetching and parsing manifests...");

Expand Down Expand Up @@ -754,8 +779,10 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine

if (opts.binary_to_restart)
{
ShellExecuteW(nullptr, L"open", UTF8ToUTF16(*opts.binary_to_restart).c_str(), L"", nullptr,
SW_SHOW);
// Hack: Launching the updater over the explorer ensures that admin priviliges are dropped. Why?
// Ask Microsoft.
ShellExecuteW(nullptr, nullptr, L"explorer.exe", UTF8ToUTF16(*opts.binary_to_restart).c_str(),
nullptr, SW_SHOW);
}

UI::Stop();
Expand Down

0 comments on commit e0f677c

Please sign in to comment.