From 166852254f2ae303f27e3996c24d45995abda253 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Tue, 25 Mar 2025 01:32:44 +0000 Subject: [PATCH] Use non-deprecated known folder API SHGetFolderPathW was deprecated in Windows Vista nearly two decades ago. ShGetKnownFolderPath is the replacement. Also log if there was an error. Someone seemed to be getting an error on Discord, despite other apps being able to get the path just fine with these functions. Also don't pass the flags to create the folders if they don't exist. We probably don't have the right permissions and if they don't exist, then there are bigger problems. Maybe this will fix the issue the user was having. Also add a comment about global config on Windows being fundamentally wrong. --- components/files/windowspath.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/components/files/windowspath.cpp b/components/files/windowspath.cpp index bbe0325b58..6fb9845976 100644 --- a/components/files/windowspath.cpp +++ b/components/files/windowspath.cpp @@ -36,12 +36,14 @@ namespace Files { std::filesystem::path userPath = std::filesystem::current_path(); - WCHAR path[MAX_PATH + 1] = {}; + PWSTR cString; + HRESULT result = SHGetKnownFolderPath(FOLDERID_Documents, 0, nullptr, &cString); + if (SUCCEEDED(result)) + userPath = std::filesystem::path(cString); + else + Log(Debug::Error) << "Error " << result << " when getting Documents path"; - if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, nullptr, 0, path))) - { - userPath = std::filesystem::path(path); - } + CoTaskMemFree(cString); return userPath / "My Games" / mName; } @@ -54,14 +56,19 @@ namespace Files std::filesystem::path WindowsPath::getGlobalConfigPath() const { + // The concept of a global config path is absurd on Windows. + // Always use local config instead. + // The virtual base class requires that we provide this, though. std::filesystem::path globalPath = std::filesystem::current_path(); - WCHAR path[MAX_PATH + 1] = {}; + PWSTR cString; + HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramFiles, 0, nullptr, &cString); + if (SUCCEEDED(result)) + globalPath = std::filesystem::path(cString); + else + Log(Debug::Error) << "Error " << result << " when getting Program Files path"; - if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_PROGRAM_FILES | CSIDL_FLAG_CREATE, nullptr, 0, path))) - { - globalPath = std::filesystem::path(path); - } + CoTaskMemFree(cString); return globalPath / mName; }