mirror of
https://github.com/lifegpc/c-utils.git
synced 2026-07-08 01:30:39 +08:00
Add winreg functions
This commit is contained in:
@@ -125,6 +125,7 @@ set(SOURCE_FILE
|
|||||||
http_client.cpp
|
http_client.cpp
|
||||||
utils_static.cpp
|
utils_static.cpp
|
||||||
hash_map.cpp
|
hash_map.cpp
|
||||||
|
reg_util.cpp
|
||||||
)
|
)
|
||||||
set(SOURCE_FILE_HEADERS
|
set(SOURCE_FILE_HEADERS
|
||||||
cfileop.h
|
cfileop.h
|
||||||
@@ -152,6 +153,7 @@ set(SOURCE_FILE_HEADERS
|
|||||||
binary_search_tree.h
|
binary_search_tree.h
|
||||||
utils_static.h
|
utils_static.h
|
||||||
hash_map.h
|
hash_map.h
|
||||||
|
reg_util.h
|
||||||
)
|
)
|
||||||
|
|
||||||
if (NOT HAVE_STRPTIME)
|
if (NOT HAVE_STRPTIME)
|
||||||
|
|||||||
@@ -115,6 +115,7 @@ source_file = files([
|
|||||||
'http_client.cpp',
|
'http_client.cpp',
|
||||||
'utils_static.cpp',
|
'utils_static.cpp',
|
||||||
'hash_map.cpp',
|
'hash_map.cpp',
|
||||||
|
'reg_util.cpp',
|
||||||
])
|
])
|
||||||
|
|
||||||
source_file_headers = files([
|
source_file_headers = files([
|
||||||
@@ -143,6 +144,7 @@ source_file_headers = files([
|
|||||||
'binary_search_tree.h',
|
'binary_search_tree.h',
|
||||||
'utils_static.h',
|
'utils_static.h',
|
||||||
'hash_map.h',
|
'hash_map.h',
|
||||||
|
'reg_util.h',
|
||||||
])
|
])
|
||||||
|
|
||||||
if conf.get('HAVE_STRPTIME') == 0
|
if conf.get('HAVE_STRPTIME') == 0
|
||||||
|
|||||||
25
reg_util.cpp
Normal file
25
reg_util.cpp
Normal 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
27
reg_util.h
Normal 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
|
||||||
Reference in New Issue
Block a user