summaryrefslogtreecommitdiff
path: root/Source/Core/WinUpdater/Main.cpp
diff options
context:
space:
mode:
authorSilent <zdanio95@gmail.com>2019-07-04 21:33:11 +0200
committerSilent <zdanio95@gmail.com>2019-07-22 19:46:25 +0200
commitbaab660f1cde50d7ac3fdf936db6629a59f38477 (patch)
tree315e412e50c097138e8d6e79e748ef428b667c36 /Source/Core/WinUpdater/Main.cpp
parent94a19ca6702bceb6b70df0f6bac0445d7e68892e (diff)
WinUpdater: Removed MAX_PATH limitation on path to updater, also fixed a wrong size parameter
Diffstat (limited to 'Source/Core/WinUpdater/Main.cpp')
-rw-r--r--Source/Core/WinUpdater/Main.cpp29
1 files changed, 26 insertions, 3 deletions
diff --git a/Source/Core/WinUpdater/Main.cpp b/Source/Core/WinUpdater/Main.cpp
index 0cb09cb966..9196805819 100644
--- a/Source/Core/WinUpdater/Main.cpp
+++ b/Source/Core/WinUpdater/Main.cpp
@@ -6,6 +6,7 @@
#include <ShlObj.h>
#include <shellapi.h>
+#include <optional>
#include <string>
#include <vector>
@@ -32,6 +33,28 @@ std::vector<std::string> CommandLineToUtf8Argv(PCWSTR command_line)
LocalFree(tokenized);
return argv;
}
+
+std::optional<std::wstring> GetModuleName(HINSTANCE hInstance)
+{
+ std::wstring name;
+ DWORD max_size = 50; // Start with space for 50 characters and grow if needed
+ name.resize(max_size);
+
+ DWORD size;
+ while ((size = GetModuleFileNameW(hInstance, name.data(), max_size)) == max_size &&
+ GetLastError() == ERROR_INSUFFICIENT_BUFFER)
+ {
+ max_size *= 2;
+ name.resize(max_size);
+ }
+
+ if (size == 0)
+ {
+ return {};
+ }
+ name.resize(size);
+ return name;
+}
}; // namespace
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
@@ -64,15 +87,15 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
return 1;
}
- wchar_t path[MAX_PATH];
- if (GetModuleFileName(hInstance, path, sizeof(path)) == 0)
+ auto path = GetModuleName(hInstance);
+ if (!path)
{
MessageBox(nullptr, L"Failed to get updater filename.", L"Error", MB_ICONERROR);
return 1;
}
// Relaunch the updater as administrator
- ShellExecuteW(nullptr, L"runas", path, pCmdLine, NULL, SW_SHOW);
+ ShellExecuteW(nullptr, L"runas", path->c_str(), pCmdLine, NULL, SW_SHOW);
return 0;
}