From baab660f1cde50d7ac3fdf936db6629a59f38477 Mon Sep 17 00:00:00 2001 From: Silent Date: Thu, 4 Jul 2019 21:33:11 +0200 Subject: [PATCH] WinUpdater: Removed MAX_PATH limitation on path to updater, also fixed a wrong size parameter --- Source/Core/WinUpdater/Main.cpp | 29 ++++++++++++++++++++++++++--- 1 file 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 #include +#include #include #include @@ -32,6 +33,28 @@ std::vector CommandLineToUtf8Argv(PCWSTR command_line) LocalFree(tokenized); return argv; } + +std::optional 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; }