From 46464d27224b57d623b840061ea417e535db8bda Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 4 Jan 2020 09:02:10 -0800 Subject: [PATCH] Windows: Resolve module/full paths for long paths. --- Common/FileUtil.cpp | 23 +++++++++++++++++------ Core/PSPLoaders.cpp | 14 +++++++++++--- Windows/W32Util/Misc.cpp | 31 ++++++++++++++++++++++++++----- Windows/W32Util/Misc.h | 1 + Windows/WindowsHost.cpp | 13 ++++++++++--- Windows/main.cpp | 12 ++++++------ 6 files changed, 71 insertions(+), 23 deletions(-) diff --git a/Common/FileUtil.cpp b/Common/FileUtil.cpp index 04ae730c1c..d1ad8a5b72 100644 --- a/Common/FileUtil.cpp +++ b/Common/FileUtil.cpp @@ -792,12 +792,23 @@ const std::string &GetExeDirectory() if (ExePath.empty()) { #ifdef _WIN32 - TCHAR program_path[4096] = {0}; - GetModuleFileName(NULL, program_path, ARRAY_SIZE(program_path) - 1); - program_path[ARRAY_SIZE(program_path) - 1] = '\0'; - TCHAR *last_slash = _tcsrchr(program_path, '\\'); - if (last_slash != NULL) - *(last_slash + 1) = '\0'; +#ifdef UNICODE + std::wstring program_path; +#else + std::string program_path; +#endif + size_t sz; + do { + program_path.resize(program_path.size() + MAX_PATH); + // On failure, this will return the same value as passed in, but success will always be one lower. + sz = GetModuleFileName(nullptr, &program_path[0], (DWORD)program_path.size()); + } while (sz >= program_path.size()); + + TCHAR *last_slash = _tcsrchr(&program_path[0], '\\'); + if (last_slash != nullptr) + program_path.resize(last_slash - &program_path[0] + 1); + else + program_path.resize(sz); #ifdef UNICODE ExePath = ConvertWStringToUTF8(program_path); #else diff --git a/Core/PSPLoaders.cpp b/Core/PSPLoaders.cpp index 721d26d16e..2e032ae2a3 100644 --- a/Core/PSPLoaders.cpp +++ b/Core/PSPLoaders.cpp @@ -284,10 +284,18 @@ bool Load_PSP_ISO(FileLoader *fileLoader, std::string *error_string) { static std::string NormalizePath(const std::string &path) { #ifdef _WIN32 - wchar_t buf[512] = {0}; std::wstring wpath = ConvertUTF8ToWString(path); - if (GetFullPathName(wpath.c_str(), (int)ARRAY_SIZE(buf) - 1, buf, NULL) == 0) - return ""; + std::wstring buf; + buf.resize(512); + size_t sz = GetFullPathName(wpath.c_str(), (DWORD)buf.size(), &buf[0], nullptr); + if (sz != 0 && sz < buf.size()) { + buf.resize(sz); + } else if (sz > buf.size()) { + buf.resize(sz); + sz = GetFullPathName(wpath.c_str(), (DWORD)buf.size(), &buf[0], nullptr); + // This should truncate off the null terminator. + buf.resize(sz); + } return ConvertWStringToUTF8(buf); #else char buf[PATH_MAX + 1]; diff --git a/Windows/W32Util/Misc.cpp b/Windows/W32Util/Misc.cpp index 7c2953b2d6..ca26ad7a4b 100644 --- a/Windows/W32Util/Misc.cpp +++ b/Windows/W32Util/Misc.cpp @@ -131,15 +131,36 @@ namespace W32Util return cmdline; } + void GetSelfExecuteParams(std::wstring &workingDirectory, std::wstring &moduleFilename) { + workingDirectory.resize(MAX_PATH); + size_t sz = GetCurrentDirectoryW((DWORD)workingDirectory.size(), &workingDirectory[0]); + if (sz != 0 && sz < workingDirectory.size()) { + // This means success, so now we can remove the null terminator. + workingDirectory.resize(sz); + } else if (sz > workingDirectory.size()) { + // If insufficient, sz will include the null terminator, so we remove after. + workingDirectory.resize(sz); + sz = GetCurrentDirectoryW((DWORD)sz, &workingDirectory[0]); + workingDirectory.resize(sz); + } + + moduleFilename.clear(); + do { + moduleFilename.resize(moduleFilename.size() + MAX_PATH); + // On failure, this will return the same value as passed in, but success will always be one lower. + sz = GetModuleFileName(GetModuleHandle(nullptr), &moduleFilename[0], (DWORD)moduleFilename.size()); + } while (sz >= moduleFilename.size()); + moduleFilename.resize(sz); + } + void ExitAndRestart() { // This preserves arguments (for example, config file) and working directory. + std::wstring workingDirectory; + std::wstring moduleFilename; + GetSelfExecuteParams(workingDirectory, moduleFilename); - wchar_t moduleFilename[MAX_PATH]; - wchar_t workingDirectory[MAX_PATH]; - GetCurrentDirectoryW(MAX_PATH, workingDirectory); const wchar_t *cmdline = RemoveExecutableFromCommandLine(GetCommandLineW()); - GetModuleFileName(GetModuleHandle(NULL), moduleFilename, MAX_PATH); - ShellExecute(NULL, NULL, moduleFilename, cmdline, workingDirectory, SW_SHOW); + ShellExecute(nullptr, nullptr, moduleFilename.c_str(), cmdline, workingDirectory.c_str(), SW_SHOW); ExitProcess(0); } diff --git a/Windows/W32Util/Misc.h b/Windows/W32Util/Misc.h index 457874450e..680a48c62e 100644 --- a/Windows/W32Util/Misc.h +++ b/Windows/W32Util/Misc.h @@ -12,6 +12,7 @@ namespace W32Util BOOL CopyTextToClipboard(HWND hwnd, const std::wstring &wtext); void MakeTopMost(HWND hwnd, bool topMost); void ExitAndRestart(); + void GetSelfExecuteParams(std::wstring &workingDirectory, std::wstring &moduleFilename); } struct GenericListViewColumn diff --git a/Windows/WindowsHost.cpp b/Windows/WindowsHost.cpp index 4508a1d3d3..84d09875bd 100644 --- a/Windows/WindowsHost.cpp +++ b/Windows/WindowsHost.cpp @@ -353,6 +353,7 @@ bool WindowsHost::CreateDesktopShortcut(std::string argumentPath, std::string ga // Get the desktop folder + // TODO: Not long path safe. wchar_t *pathbuf = new wchar_t[MAX_PATH + gameTitle.size() + 100]; SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, pathbuf); @@ -370,10 +371,16 @@ bool WindowsHost::CreateDesktopShortcut(std::string argumentPath, std::string ga wcscat(pathbuf, L"\\"); wcscat(pathbuf, ConvertUTF8ToWString(gameTitle).c_str()); - wchar_t module[MAX_PATH]; - GetModuleFileName(NULL, module, MAX_PATH); + std::wstring moduleFilename; + size_t sz; + do { + moduleFilename.resize(moduleFilename.size() + MAX_PATH); + // On failure, this will return the same value as passed in, but success will always be one lower. + sz = GetModuleFileName(nullptr, &moduleFilename[0], (DWORD)moduleFilename.size()); + } while (sz >= moduleFilename.size()); + moduleFilename.resize(sz); - CreateLink(module, ConvertUTF8ToWString(argumentPath).c_str(), pathbuf, ConvertUTF8ToWString(gameTitle).c_str()); + CreateLink(moduleFilename.c_str(), ConvertUTF8ToWString(argumentPath).c_str(), pathbuf, ConvertUTF8ToWString(gameTitle).c_str()); delete [] pathbuf; return false; diff --git a/Windows/main.cpp b/Windows/main.cpp index 866f4c0746..24718559ed 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -374,17 +374,17 @@ static std::string GetDefaultLangRegion() { static const int EXIT_CODE_VULKAN_WORKS = 42; static bool DetectVulkanInExternalProcess() { - wchar_t moduleFilename[MAX_PATH]; - wchar_t workingDirectory[MAX_PATH]; - GetCurrentDirectoryW(MAX_PATH, workingDirectory); + std::wstring workingDirectory; + std::wstring moduleFilename; + W32Util::GetSelfExecuteParams(workingDirectory, moduleFilename); + const wchar_t *cmdline = L"--vulkan-available-check"; - GetModuleFileName(GetModuleHandle(NULL), moduleFilename, MAX_PATH); SHELLEXECUTEINFO info{ sizeof(SHELLEXECUTEINFO) }; info.fMask = SEE_MASK_NOCLOSEPROCESS; - info.lpFile = moduleFilename; + info.lpFile = moduleFilename.c_str(); info.lpParameters = cmdline; - info.lpDirectory = workingDirectory; + info.lpDirectory = workingDirectory.c_str(); info.nShow = SW_HIDE; if (ShellExecuteEx(&info) != TRUE) { return false;