Skip to content
Permalink
Browse files

WinUpdater: Removed MAX_PATH limitation on path to updater, also fixe…

…d a wrong size parameter
  • Loading branch information...
CookiePLMonster committed Jul 4, 2019
1 parent 94a19ca commit baab660f1cde50d7ac3fdf936db6629a59f38477
Showing with 26 additions and 3 deletions.
  1. +26 −3 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;
}

0 comments on commit baab660

Please sign in to comment.
You can’t perform that action at this time.