Main.cpp 1.8 KB

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