Add winreg functions

This commit is contained in:
2025-05-01 13:37:39 +08:00
parent 96812b7565
commit d706a5de03
4 changed files with 56 additions and 0 deletions

View File

@@ -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)

View File

@@ -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

25
reg_util.cpp Normal file
View File

@@ -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

27
reg_util.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef _UTIL_REG_UTIL_H
#define _UTIL_REG_UTIL_H
#ifdef _WIN32
#include <windows.h>
#include <string>
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