mirror of
https://github.com/RetroDECK/Duckstation.git
synced 2024-11-27 08:05:41 +00:00
FileSystem: Fix GetProgramPath() sometimes returning stale paths
See https://social.msdn.microsoft.com/Forums/windowshardware/en-US/4b7b3884-d0bb-4812-bc18-2078c61d4b90/queryfullprocessimagename-gives-wrong-path-for-renamed-directory?forum=windowsgeneraldevelopmentissues
This commit is contained in:
parent
2219b9aa66
commit
e8e461c0a7
|
@ -1114,16 +1114,18 @@ bool FileSystem::DeleteDirectory(const char* Path, bool Recursive)
|
||||||
|
|
||||||
std::string GetProgramPath()
|
std::string GetProgramPath()
|
||||||
{
|
{
|
||||||
const HANDLE hProcess = GetCurrentProcess();
|
|
||||||
|
|
||||||
std::wstring buffer;
|
std::wstring buffer;
|
||||||
buffer.resize(MAX_PATH);
|
buffer.resize(MAX_PATH);
|
||||||
|
|
||||||
|
// Fall back to the main module if this fails.
|
||||||
|
HMODULE module = nullptr;
|
||||||
|
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
||||||
|
reinterpret_cast<LPCWSTR>(&GetProgramPath), &module);
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
DWORD nChars = static_cast<DWORD>(buffer.size());
|
DWORD nChars = GetModuleFileNameW(module, buffer.data(), static_cast<DWORD>(buffer.size()));
|
||||||
if (!QueryFullProcessImageNameW(GetCurrentProcess(), 0, buffer.data(), &nChars) &&
|
if (nChars == static_cast<DWORD>(buffer.size()) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
||||||
GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
|
||||||
{
|
{
|
||||||
buffer.resize(buffer.size() * 2);
|
buffer.resize(buffer.size() * 2);
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Reference in a new issue