use of SHGetKnownFolderPath to get localAppData

This commit is contained in:
Anonymous275
2022-01-20 03:54:13 +02:00
parent f68e650202
commit 263858763f
2 changed files with 18 additions and 12 deletions
+2 -2
View File
@@ -44,11 +44,11 @@ add_executable(${PROJECT_NAME}
if (WIN32) if (WIN32)
target_link_libraries(${PROJECT_NAME} PRIVATE ${VcpkgRoot}/lib/discord-rpc.lib 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 else(WIN32) #MINGW
add_definitions("-D_WIN32_WINNT=0x0600") add_definitions("-D_WIN32_WINNT=0x0600")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static") 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) endif(WIN32)
add_definitions(-DELPP_NO_DEFAULT_LOG_FILE) add_definitions(-DELPP_NO_DEFAULT_LOG_FILE)
target_include_directories(${PROJECT_NAME} PRIVATE "include") target_include_directories(${PROJECT_NAME} PRIVATE "include")
+16 -10
View File
@@ -11,6 +11,8 @@
#include <csignal> #include <csignal>
#include <windows.h> #include <windows.h>
#include <shellapi.h> #include <shellapi.h>
#include <ShlObj.h>
#include <comutil.h>
LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) { LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) {
LOG(ERROR) << "CAUGHT EXCEPTION! Code " << p->ExceptionRecord->ExceptionCode; LOG(ERROR) << "CAUGHT EXCEPTION! Code " << p->ExceptionRecord->ExceptionCode;
@@ -129,16 +131,20 @@ std::string QueryValue(HKEY& hKey, const char* Name) {
return {}; return {};
} }
std::string Launcher::GetLocalAppdata() { std::string Launcher::GetLocalAppdata() {
HKEY Folders; PWSTR folderPath = nullptr;
LONG RegRes = RegOpenKeyExA(HKEY_CURRENT_USER, R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders)", 0, KEY_READ, &Folders);
if(RegRes == ERROR_SUCCESS) { HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &folderPath);
std::string Path = QueryValue(Folders, "Local AppData"); if (!SUCCEEDED(hr))return {};
if(!Path.empty()) {
Path += "\\BeamNG.drive\\"; _bstr_t bstrPath(folderPath);
VersionParser GameVer(BeamVersion); std::string Path((char*)bstrPath);
Path += GameVer.split[0] + '.' + GameVer.split[1] + '\\'; CoTaskMemFree(folderPath);
return Path;
} if(!Path.empty()) {
Path += "\\BeamNG.drive\\";
VersionParser GameVer(BeamVersion);
Path += GameVer.split[0] + '.' + GameVer.split[1] + '\\';
return Path;
} }
return {}; return {};
} }