diff --git a/CMakeLists.txt b/CMakeLists.txt index f16d87b..bfbf3fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,7 @@ set(SOURCE_FILE http_client.cpp utils_static.cpp hash_map.cpp + reg_util.cpp ) set(SOURCE_FILE_HEADERS cfileop.h @@ -152,6 +153,7 @@ set(SOURCE_FILE_HEADERS binary_search_tree.h utils_static.h hash_map.h + reg_util.h ) if (NOT HAVE_STRPTIME) diff --git a/meson.build b/meson.build index 0aaa0bb..45afb89 100644 --- a/meson.build +++ b/meson.build @@ -115,6 +115,7 @@ source_file = files([ 'http_client.cpp', 'utils_static.cpp', 'hash_map.cpp', + 'reg_util.cpp', ]) source_file_headers = files([ @@ -143,6 +144,7 @@ source_file_headers = files([ 'binary_search_tree.h', 'utils_static.h', 'hash_map.h', + 'reg_util.h', ]) if conf.get('HAVE_STRPTIME') == 0 diff --git a/reg_util.cpp b/reg_util.cpp new file mode 100644 index 0000000..4134b48 --- /dev/null +++ b/reg_util.cpp @@ -0,0 +1,25 @@ +#include "reg_util.h" +#ifdef _WIN32 +bool reg_util::get_string(std::wstring& result, HKEY hKey, std::wstring subKey, std::wstring value) { + DWORD len; + if (RegGetValueW(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_SZ, nullptr, nullptr, &len) != ERROR_SUCCESS) { + return false; + } + wchar_t* buffer = new wchar_t[len / sizeof(wchar_t)]; + if (RegGetValueW(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_SZ, nullptr, buffer, &len) != ERROR_SUCCESS) { + delete[] buffer; + return false; + } + result = std::wstring(buffer); + delete[] buffer; + return true; +} + +bool reg_util::get_dword(DWORD& result, HKEY hKey, std::wstring subKey, std::wstring value) { + DWORD len = sizeof(DWORD); + if (RegGetValueW(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_DWORD, nullptr, &result, &len) != ERROR_SUCCESS) { + return false; + } + return true; +} +#endif diff --git a/reg_util.h b/reg_util.h new file mode 100644 index 0000000..5aeed38 --- /dev/null +++ b/reg_util.h @@ -0,0 +1,27 @@ +#ifndef _UTIL_REG_UTIL_H +#define _UTIL_REG_UTIL_H +#ifdef _WIN32 +#include +#include +namespace reg_util { + /** + * @brief Get string(REG_SZ) value from registry. + * @param result Result string + * @param hKey Registry key handle + * @param subKey Subkey path + * @param value Value name + * @return true if success, false otherwise. + */ + bool get_string(std::wstring& result, HKEY hKey, std::wstring subKey, std::wstring value); + /** + * @brief Get DWORD(REG_DWORD) value from registry. + * @param result Result DWORD + * @param hKey Registry key handle + * @param subKey Subkey path + * @param value Value name + * @return true if success, false otherwise. + */ + bool get_dword(DWORD& result, HKEY hKey, std::wstring subKey, std::wstring value); +} +#endif +#endif