diff --git a/Source/Core/UICommon/UICommon.cpp b/Source/Core/UICommon/UICommon.cpp index 60f8ff3575f8..608d6237770a 100644 --- a/Source/Core/UICommon/UICommon.cpp +++ b/Source/Core/UICommon/UICommon.cpp @@ -12,6 +12,8 @@ #include #ifdef _WIN32 #include // for SHGetFolderPath + +#include #endif #include "Common/Common.h" @@ -305,51 +307,46 @@ void SetUserDirectory(std::string custom_path) // -> Use GetExeDirectory()\User // Get AppData path in case we need it. - // TODO: Maybe use WIL when it's available? - PWSTR appdata = nullptr; - bool appdata_found = - SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, &appdata)); + wil::unique_cotaskmem_string appdata; + bool appdata_found = SUCCEEDED( + SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, appdata.put())); #ifndef STEAM // Check our registry keys - // TODO: Maybe use WIL when it's available? - HKEY hkey; + wil::unique_hkey hkey; DWORD local = 0; std::unique_ptr configPath; if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE, - &hkey) == ERROR_SUCCESS) + hkey.put()) == ERROR_SUCCESS) { - DWORD size = 4; - if (RegQueryValueEx(hkey, TEXT("LocalUserConfig"), nullptr, nullptr, + DWORD size = sizeof(local); + if (RegQueryValueEx(hkey.get(), TEXT("LocalUserConfig"), nullptr, nullptr, reinterpret_cast(&local), &size) != ERROR_SUCCESS) { local = 0; } size = 0; - RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr, nullptr, &size); + RegQueryValueEx(hkey.get(), TEXT("UserConfigPath"), nullptr, nullptr, nullptr, &size); configPath = std::make_unique(size / sizeof(TCHAR)); - if (RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr, + if (RegQueryValueEx(hkey.get(), TEXT("UserConfigPath"), nullptr, nullptr, reinterpret_cast(configPath.get()), &size) != ERROR_SUCCESS) { configPath.reset(); } - - RegCloseKey(hkey); } local = local != 0 || File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt"); - // Attempt to check if the old User directory exists in My Documents. - // TODO: Maybe use WIL when it's available? - PWSTR documents = nullptr; - bool documents_found = - SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &documents)); + // Attempt to check if the old User directory exists in Documents. + wil::unique_cotaskmem_string documents; + bool documents_found = SUCCEEDED( + SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, documents.put())); std::optional old_user_folder; if (documents_found) { - old_user_folder = TStrToUTF8(documents) + DIR_SEP NORMAL_USER_DIR DIR_SEP; + old_user_folder = TStrToUTF8(documents.get()) + DIR_SEP NORMAL_USER_DIR DIR_SEP; } if (local) // Case 1-2 @@ -366,7 +363,7 @@ void SetUserDirectory(std::string custom_path) } else if (appdata_found) // Case 5 { - user_path = TStrToUTF8(appdata) + DIR_SEP NORMAL_USER_DIR DIR_SEP; + user_path = TStrToUTF8(appdata.get()) + DIR_SEP NORMAL_USER_DIR DIR_SEP; // Set the UserConfigPath value in the registry for backwards compatibility with older Dolphin // builds, which will look for the default User directory in Documents. If we set this key, @@ -381,8 +378,6 @@ void SetUserDirectory(std::string custom_path) { user_path = File::GetExeDirectory() + DIR_SEP PORTABLE_USER_DIR DIR_SEP; } - - CoTaskMemFree(documents); #else // ifndef STEAM if (File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt")) // Case 1 { @@ -390,7 +385,7 @@ void SetUserDirectory(std::string custom_path) } else if (appdata_found) // Case 2 { - user_path = TStrToUTF8(appdata) + DIR_SEP NORMAL_USER_DIR DIR_SEP; + user_path = TStrToUTF8(appdata.get()) + DIR_SEP NORMAL_USER_DIR DIR_SEP; } else // Case 3 { @@ -398,7 +393,6 @@ void SetUserDirectory(std::string custom_path) } #endif - CoTaskMemFree(appdata); #else if (File::IsDirectory(ROOT_DIR DIR_SEP EMBEDDED_USER_DIR)) {