1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #pragma once #include <Windows.h> #include <string>
class Registry { public: enum class RegistryValueKind { None = REG_NONE, String = REG_SZ, ExpandString = REG_EXPAND_SZ, Binary = REG_BINARY, DWord = REG_DWORD, QWord = REG_QWORD, MultiString = REG_MULTI_SZ };
static bool SetValue(HKEY hKey, const std::string& subKey, const std::string& valueName, const void* data, DWORD dataSize, RegistryValueKind kind, bool use64View = false) { if (subKey.empty() || valueName.empty()) { return false; }
const DWORD flags = use64View ? KEY_WOW64_64KEY : 0;
HKEY hKey_; LONG result = RegCreateKeyEx(hKey, subKey.c_str(), 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE | flags, nullptr, &hKey_, nullptr); if (result != ERROR_SUCCESS) { return false; }
result = RegSetValueEx(hKey_, valueName.c_str(), 0, static_cast<DWORD>(kind), static_cast<const BYTE*>(data), dataSize); if (result != ERROR_SUCCESS) { RegCloseKey(hKey_); return false; }
RegCloseKey(hKey_); return true; }
static bool GetValue(HKEY hKey, const std::string& subKey, const std::string& valueName, void* data, DWORD& dataSize, RegistryValueKind& kind, bool use64View = false) { if (subKey.empty() || valueName.empty()) { return false; }
const DWORD flags = use64View ? KEY_WOW64_64KEY : 0;
HKEY hKey_; LONG result = RegOpenKeyEx(hKey, subKey.c_str(), 0, KEY_READ | flags, &hKey_); if (result != ERROR_SUCCESS) { return false; }
result = RegQueryValueEx(hKey_, valueName.c_str(), nullptr, reinterpret_cast<LPDWORD>(&kind), static_cast<LPBYTE>(data), &dataSize); if (result != ERROR_SUCCESS) { RegCloseKey(hKey_); return false; }
RegCloseKey(hKey_); return true; }
static bool DeleteKey(HKEY hKey, const std::string& subKey) { if (subKey.empty()) { return false; }
const LONG result = RegDeleteKey(hKey, subKey.c_str()); if (result != ERROR_SUCCESS) { return false; } return true; }
static bool DeleteValue(HKEY hKey, const std::string& subKey, const std::string& valueName, bool use64View = false) { if (subKey.empty() || valueName.empty()) { return false; }
const DWORD flags = use64View ? KEY_WOW64_64KEY : 0;
HKEY hKey_; LONG result = RegOpenKeyEx(hKey, subKey.c_str(), 0, KEY_WRITE | flags, &hKey_); if (result != ERROR_SUCCESS) { return false; }
result = RegDeleteValue(hKey_, valueName.c_str()); if (result != ERROR_SUCCESS) { return false; }
RegCloseKey(hKey_); return true; } };
|