From 689378b435c73b624cfdea8fe971db6b1d72c48e Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 6 Oct 2019 22:17:00 +0200 Subject: Move GetModuleName to Common This unifies GetModuleFileName calls between Dolphin and WinUpdater and allows to gracefully remove MAX_PATH limit from GetExePath --- Source/Core/Common/CommonFuncs.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'Source/Core/Common/CommonFuncs.cpp') diff --git a/Source/Core/Common/CommonFuncs.cpp b/Source/Core/Common/CommonFuncs.cpp index 7b109e81c5..a8c1abfea2 100644 --- a/Source/Core/Common/CommonFuncs.cpp +++ b/Source/Core/Common/CommonFuncs.cpp @@ -49,4 +49,27 @@ std::string GetLastErrorString() MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_message, BUFFER_SIZE, nullptr); return std::string(error_message); } + +// Obtains a full path to the specified module. +std::optional GetModuleName(void* hInstance) +{ + DWORD max_size = 50; // Start with space for 50 characters and grow if needed + std::wstring name(max_size, L'\0'); + + DWORD size; + while ((size = GetModuleFileNameW(static_cast(hInstance), name.data(), max_size)) == + max_size && + GetLastError() == ERROR_INSUFFICIENT_BUFFER) + { + max_size *= 2; + name.resize(max_size); + } + + if (size == 0) + { + return std::nullopt; + } + name.resize(size); + return name; +} #endif -- cgit v1.2.3