| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <windows.h> |
| 5 | #include <ShlObj.h> |
| 6 | #include <shellapi.h> |
| 7 | |
| 8 | #include <optional> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "Common/CommonFuncs.h" |
| 13 | #include "Common/StringUtil.h" |
| 14 | |
| 15 | #include "UpdaterCommon/UI.h" |
| 16 | #include "UpdaterCommon/UpdaterCommon.h" |
| 17 | |
| 18 | // Refer to docs/autoupdate_overview.md for a detailed overview of the autoupdate process |
| 19 | |
| 20 | int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) |
| 21 | { |
| 22 | if (lstrlenW(pCmdLine) == 0) |
| 23 | { |
| 24 | MessageBoxW(nullptr, |
| 25 | L"This updater is not meant to be launched directly. Configure Auto-Update in " |
| 26 | "Dolphin's settings instead.", |
| 27 | L"Error", MB_ICONERROR); |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | // Test for write permissions |
| 32 | bool need_admin = false; |
| 33 | |
| 34 | auto path = Common::GetModuleName(hInstance); |
| 35 | if (!path) |
| 36 | { |
| 37 | UI::Error("Failed to get updater filename."); |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | const auto test_fh = ::CreateFileW( |
| 42 | (std::filesystem::path(*path).parent_path() / "directory_writable_check.tmp").c_str(), |
| 43 | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, |
| 44 | FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr); |
| 45 | |
| 46 | if (test_fh == INVALID_HANDLE_VALUE) |
| 47 | need_admin = true; |
| 48 | else |
| 49 | CloseHandle(test_fh); |
| 50 | |
| 51 | if (need_admin) |
| 52 | { |
| 53 | if (IsUserAnAdmin()) |
| 54 | { |
| 55 | MessageBox(nullptr, L"Failed to write to directory despite administrator privileges.", |
| 56 | L"Error", MB_ICONERROR); |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | // Relaunch the updater as administrator |
| 61 | ShellExecuteW(nullptr, L"runas", path->c_str(), pCmdLine, nullptr, SW_SHOW); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | std::vector<std::string> args = Common::CommandLineToUtf8Argv(pCmdLine); |
| 66 | |
| 67 | return RunUpdater(args) ? 0 : 1; |
| 68 | } |