From 41b7087e20a47f4fbcf6eba194cbd3e2ffdc521d Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Wed, 26 Aug 2026 18:39:25 -0500 Subject: [PATCH] Update Windows AutoStart implementation to allow non-ASCII paths --- AutoStart/AutoStart-Windows.cpp | 48 ++++++++++++--------------------- AutoStart/AutoStart-Windows.h | 1 - StringUtils.cpp | 7 +++++ StringUtils.h | 1 + 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/AutoStart/AutoStart-Windows.cpp b/AutoStart/AutoStart-Windows.cpp index c8e78b436..2c8285c5c 100644 --- a/AutoStart/AutoStart-Windows.cpp +++ b/AutoStart/AutoStart-Windows.cpp @@ -12,6 +12,7 @@ #include #include "AutoStart-Windows.h" #include "LogManager.h" +#include "StringUtils.h" #include "filesystem.h" #include "windows.h" @@ -71,11 +72,11 @@ bool AutoStart::EnableAutoStart(AutoStartInfo autostart_info) HRESULT result; IShellLinkW* shellLink = NULL; - std::wstring exepathw = utf8_decode(autostart_info.path); - std::wstring argumentsw = utf8_decode(autostart_info.args); - std::wstring startupfilepathw = utf8_decode(autostart_file); - std::wstring descriptionw = utf8_decode(autostart_info.desc); - std::wstring iconw = utf8_decode(autostart_info.path); + std::wstring exepathw = StringUtils::string_to_wstring(autostart_info.path); + std::wstring argumentsw = StringUtils::string_to_wstring(autostart_info.args); + std::wstring startupfilepathw = StringUtils::string_to_wstring(autostart_file); + std::wstring descriptionw = StringUtils::string_to_wstring(autostart_info.desc); + std::wstring iconw = StringUtils::string_to_wstring(autostart_info.path); result = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLinkW, (void**)&shellLink); @@ -150,11 +151,16 @@ std::string AutoStart::GetExePath() /*-----------------------------------------------------*\ | Create the OpenRGB executable path | \*-----------------------------------------------------*/ - char exepath[MAX_PATH] = ""; + wchar_t exepath[MAX_PATH] = L""; - DWORD count = GetModuleFileNameA(NULL, exepath, MAX_PATH); + DWORD count = GetModuleFileNameW(NULL, exepath, MAX_PATH); - return(std::string(exepath, (count > 0) ? count : 0)); + if(count == 0) + { + return(std::string()); + } + + return(StringUtils::wstring_to_string(std::wstring(exepath, count))); } /*---------------------------------------------------------*\ @@ -164,18 +170,18 @@ std::string AutoStart::GetExePath() void AutoStart::InitAutoStart(std::string name) { - char startMenuPath[MAX_PATH]; + wchar_t startMenuPath[MAX_PATH]; autostart_name = name; /*-----------------------------------------------------*\ | Get startup applications path | \*-----------------------------------------------------*/ - HRESULT result = SHGetFolderPathA(NULL, CSIDL_PROGRAMS, NULL, 0, startMenuPath); + HRESULT result = SHGetFolderPathW(NULL, CSIDL_PROGRAMS, NULL, 0, startMenuPath); if(SUCCEEDED(result)) { - std::string autostart_dir = std::string(startMenuPath); + std::string autostart_dir = StringUtils::wstring_to_string(startMenuPath); autostart_dir += "\\Startup\\"; /*-------------------------------------------------*\ @@ -200,23 +206,3 @@ void AutoStart::InitAutoStart(std::string name) autostart_file.clear(); } } - -/*---------------------------------------------------------*\ -| Convert an UTF8 string to a wide Unicode String | -| (from wmi.cpp) | -\*---------------------------------------------------------*/ -std::wstring AutoStart::utf8_decode(const std::string& str) -{ - if(str.empty()) - { - return std::wstring(); - } - - int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int) str.size(), nullptr, 0); - - std::wstring wstrTo(size_needed, 0); - - MultiByteToWideChar(CP_UTF8, 0, &str[0], (int) str.size(), &wstrTo[0], size_needed); - - return(wstrTo); -} diff --git a/AutoStart/AutoStart-Windows.h b/AutoStart/AutoStart-Windows.h index b0dfc0cca..751eb112c 100644 --- a/AutoStart/AutoStart-Windows.h +++ b/AutoStart/AutoStart-Windows.h @@ -24,5 +24,4 @@ public: private: void InitAutoStart(std::string name); - std::wstring utf8_decode(const std::string& str); }; diff --git a/StringUtils.cpp b/StringUtils.cpp index 9ece1c75b..0c6e2a2ea 100644 --- a/StringUtils.cpp +++ b/StringUtils.cpp @@ -90,6 +90,13 @@ std::string StringUtils::wstring_to_string(const std::wstring wstring) return(converter.to_bytes(wstring)); } +std::wstring StringUtils::string_to_wstring(const std::string input) +{ + std::wstring_convert, wchar_t> converter; + + return(converter.from_bytes(input)); +} + std::string StringUtils::u16string_to_string(const std::u16string wstring) { std::wstring_convert,char16_t> converter; diff --git a/StringUtils.h b/StringUtils.h index cde1ec5b1..21b8eba18 100644 --- a/StringUtils.h +++ b/StringUtils.h @@ -17,6 +17,7 @@ public: static const char* wchar_to_char(const wchar_t* pwchar); static std::string wchar_to_string(const wchar_t* pwchar); static std::string wstring_to_string(const std::wstring wstring); + static std::wstring string_to_wstring(const std::string input); static std::string u16string_to_string(const std::u16string wstring); static const std::string remove_null_terminating_chars(std::string input); static std::string u32int_to_hexString(unsigned int value);