From 771f3c49e5f0e992979c12635f1e2d8ec19abdb9 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Mon, 21 Feb 2022 00:45:32 +0100 Subject: [PATCH] windows: fix windows shortcut paths --- rpcs3/rpcs3qt/shortcut_utils.cpp | 61 ++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/rpcs3/rpcs3qt/shortcut_utils.cpp b/rpcs3/rpcs3qt/shortcut_utils.cpp index 608cffb62a..3037507491 100644 --- a/rpcs3/rpcs3qt/shortcut_utils.cpp +++ b/rpcs3/rpcs3qt/shortcut_utils.cpp @@ -82,33 +82,6 @@ namespace gui::utils } #ifdef _WIN32 - std::string link_file; - - if (const char* home = getenv("USERPROFILE")) - { - if (is_desktop_shortcut) - { - link_file = fmt::format("%s/Desktop/%s.lnk", home, simple_name); - } - else - { - const std::string programs_dir = fmt::format("%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/RPCS3", home); - if (!fs::create_path(programs_dir)) - { - sys_log.error("Failed to create shortcut: Could not create start menu directory: %s", programs_dir); - return false; - } - link_file = fmt::format("%s/%s.lnk", programs_dir, simple_name); - } - } - else - { - sys_log.error("Failed to create shortcut: home path empty"); - return false; - } - - sys_log.notice("Creating shortcut '%s' with arguments '%s' and .ico dir '%s'", link_file, target_cli_args, target_icon_dir); - const auto str_error = [](HRESULT hr) -> std::string { _com_error err(hr); @@ -116,6 +89,40 @@ namespace gui::utils return fmt::format("%s [%d]", wchar_to_utf8(errMsg), hr); }; + std::string link_file; + + if (is_desktop_shortcut) + { + TCHAR path[_MAX_PATH]; + HRESULT res = SHGetFolderPath(NULL, CSIDL_DESKTOP, 0, NULL, path); + if (FAILED(res)) + { + sys_log.error("Failed to create shortcut: SHGetFolderPath(CSIDL_DESKTOP) failed (%s)", str_error(res)); + return false; + } + link_file = fmt::format("%s/%s.lnk", wchar_to_utf8(std::wstring(path)), simple_name); + } + else + { + TCHAR path[_MAX_PATH]; + HRESULT res = SHGetFolderPath(NULL, CSIDL_PROGRAMS, 0, NULL, path); + if (FAILED(res)) + { + sys_log.error("Failed to create shortcut: SHGetFolderPath(CSIDL_PROGRAMS) failed (%s)", str_error(res)); + return false; + } + + const std::string programs_dir = fmt::format("%s/RPCS3", wchar_to_utf8(std::wstring(path))); + if (!fs::create_path(programs_dir)) + { + sys_log.error("Failed to create shortcut: Could not create start menu directory: %s", programs_dir); + return false; + } + link_file = fmt::format("%s/%s.lnk", programs_dir, simple_name); + } + + sys_log.notice("Creating shortcut '%s' with arguments '%s' and .ico dir '%s'", link_file, target_cli_args, target_icon_dir); + // https://stackoverflow.com/questions/3906974/how-to-programmatically-create-a-shortcut-using-win32 HRESULT res = CoInitialize(NULL); if (FAILED(res))