WinUpdater: Removed MAX_PATH limitation on path to updater, also fixed a wrong size parameter

This commit is contained in:
Silent 2019-07-04 21:33:11 +02:00
parent 94a19ca670
commit baab660f1c
No known key found for this signature in database
GPG Key ID: AE53149BB0C45AF1
1 changed files with 26 additions and 3 deletions

View File

@ -6,6 +6,7 @@
#include <ShlObj.h> #include <ShlObj.h>
#include <shellapi.h> #include <shellapi.h>
#include <optional>
#include <string> #include <string>
#include <vector> #include <vector>
@ -32,6 +33,28 @@ std::vector<std::string> CommandLineToUtf8Argv(PCWSTR command_line)
LocalFree(tokenized); LocalFree(tokenized);
return argv; 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 }; // namespace
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) 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; return 1;
} }
wchar_t path[MAX_PATH]; auto path = GetModuleName(hInstance);
if (GetModuleFileName(hInstance, path, sizeof(path)) == 0) if (!path)
{ {
MessageBox(nullptr, L"Failed to get updater filename.", L"Error", MB_ICONERROR); MessageBox(nullptr, L"Failed to get updater filename.", L"Error", MB_ICONERROR);
return 1; return 1;
} }
// Relaunch the updater as administrator // 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; return 0;
} }