From 263858763fdfe368384db0b18f8d9139d95bc6bd Mon Sep 17 00:00:00 2001 From: Anonymous275 <36374260+Anonymous-275@users.noreply.github.com> Date: Thu, 20 Jan 2022 03:54:13 +0200 Subject: [PATCH] use of SHGetKnownFolderPath to get localAppData --- CMakeLists.txt | 4 ++-- src/Launcher.cpp | 26 ++++++++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 324c55b..4d49877 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,11 +44,11 @@ add_executable(${PROJECT_NAME} if (WIN32) target_link_libraries(${PROJECT_NAME} PRIVATE ${VcpkgRoot}/lib/discord-rpc.lib - ZLIB::ZLIB discord-rpc OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base Dbghelp) + ZLIB::ZLIB discord-rpc OpenSSL::SSL OpenSSL::Crypto ws2_32 wx::net wx::core wx::base Dbghelp comsuppw) else(WIN32) #MINGW add_definitions("-D_WIN32_WINNT=0x0600") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static") - target_link_libraries(${PROJECT_NAME} discord-rpc ssl crypto ws2_32 ssp crypt32 z Dbghelp) + target_link_libraries(${PROJECT_NAME} discord-rpc ssl crypto ws2_32 ssp crypt32 z Dbghelp comsuppw) endif(WIN32) add_definitions(-DELPP_NO_DEFAULT_LOG_FILE) target_include_directories(${PROJECT_NAME} PRIVATE "include") diff --git a/src/Launcher.cpp b/src/Launcher.cpp index c769167..f709d4d 100644 --- a/src/Launcher.cpp +++ b/src/Launcher.cpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) { LOG(ERROR) << "CAUGHT EXCEPTION! Code " << p->ExceptionRecord->ExceptionCode; @@ -129,16 +131,20 @@ std::string QueryValue(HKEY& hKey, const char* Name) { return {}; } std::string Launcher::GetLocalAppdata() { - HKEY Folders; - LONG RegRes = RegOpenKeyExA(HKEY_CURRENT_USER, R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders)", 0, KEY_READ, &Folders); - if(RegRes == ERROR_SUCCESS) { - std::string Path = QueryValue(Folders, "Local AppData"); - if(!Path.empty()) { - Path += "\\BeamNG.drive\\"; - VersionParser GameVer(BeamVersion); - Path += GameVer.split[0] + '.' + GameVer.split[1] + '\\'; - return Path; - } + PWSTR folderPath = nullptr; + + HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &folderPath); + if (!SUCCEEDED(hr))return {}; + + _bstr_t bstrPath(folderPath); + std::string Path((char*)bstrPath); + CoTaskMemFree(folderPath); + + if(!Path.empty()) { + Path += "\\BeamNG.drive\\"; + VersionParser GameVer(BeamVersion); + Path += GameVer.split[0] + '.' + GameVer.split[1] + '\\'; + return Path; } return {}; }