Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cRegKey.h
Go to the documentation of this file.
1 //
4 //
5 #ifndef _INC_cRegKey_H
6 #define _INC_cRegKey_H
7 #ifndef NO_PRAGMA_ONCE
8 #pragma once
9 #endif
10 
11 #include "cHandlePtr.h"
12 #include "FileName.h"
13 #include "HResult.h"
14 
15 #ifdef _WIN32
16 
17 #ifndef HKEY_LOCAL_MACHINE
18 #define HKEY_LOCAL_MACHINE ((HKEY)(ULONG_PTR)((LONG)0x80000002))
19 #endif // HKEY_LOCAL_MACHINE
20 #ifndef HKEY_CURRENT_USER
21 #define HKEY_CURRENT_USER ((HKEY)(ULONG_PTR)((LONG)0x80000001))
22 #endif // HKEY_LOCAL_MACHINE
23 
24 namespace Gray
25 {
26  struct cRegKeyName
27  {
29  HKEY m_hKey;
30  const FILECHAR_t* m_pszRegPath;
31  };
32  class cRegKeyInit
33  {
36  public:
37  cRegKeyName m_Key;
38  const FILECHAR_t* m_pszKeyName;
39  void* m_pValue;
40  DWORD m_dwType;
41 
42  public:
43  bool isEndMarker() const noexcept
44  {
45  return m_Key.m_hKey == HANDLEPTR_NULL; // end of an array of values.
46  }
47  bool isRegValue() const noexcept
48  {
49  // ASSUME m_dwType is set as well.
50  return m_pszKeyName != nullptr;
51  }
52  };
53 
54  class cRegKey // not GRAYCORE_LINK since its inline
55  : public cHandlePtr < HKEY >
56  {
62  typedef cHandlePtr<HKEY> SUPER_t;
63 
64  public:
65  cRegKey(HKEY hKey = HKEY_LOCAL_MACHINE) noexcept
66  : cHandlePtr<HKEY>(hKey)
67  {
70  }
71  ~cRegKey() noexcept
72  {
73  }
74 
75  HKEY get_HKey() const noexcept
76  {
77  return get_Handle();
78  }
79 
80  static inline bool IsKeyBase(HKEY hKey) noexcept
81  {
84  return (((size_t)hKey) & ((size_t)HKEY_CLASSES_ROOT)) == ((size_t)HKEY_CLASSES_ROOT);
85  }
86  bool isKeyBase() const noexcept
87  {
90  return IsKeyBase(get_HKey());
91  }
92 
93  bool isKeyOpen() const noexcept
94  {
96  if (isKeyBase())
97  return false;
98  return SUPER_t::isValidHandle();
99  }
100 
101  void Attach(HKEY hKey)
102  {
103  SUPER_t::AttachHandle(hKey);
104  }
105  HKEY Detach() noexcept
106  {
108  HKEY h = get_HKey();
109  ref_Handle() = HKEY_LOCAL_MACHINE;
110  return h;
111  }
112 
113  HRESULT Open(HKEY hKeyBase, const FILECHAR_t* pszSubKey, REGSAM samDesired = KEY_READ)
114  {
121  CloseHandle();
122  const LSTATUS lRet = _FNF(::RegOpenKeyEx)(hKeyBase, pszSubKey, 0, samDesired, &ref_Handle());
123  return HResult::FromWin32(lRet);
124  }
125 
126  HRESULT OpenBase(const FILECHAR_t* pszSubKey, REGSAM samDesired = KEY_READ)
127  {
129  ASSERT(isKeyBase());
130  return Open(get_HKey(), pszSubKey, samDesired);
131  }
132 
133  HRESULT FlushX() noexcept
134  {
135  const LSTATUS lRet = ::RegFlushKey(get_Handle());
136  return HResult::FromWin32(lRet);
137  }
138 
139  // Keys
140 
141  HRESULT DeleteKey(const FILECHAR_t* pszSubKey) noexcept
142  {
146  const LSTATUS lRet = _FNF(::RegDeleteKey)(get_HKey(), pszSubKey);
147  return HResult::FromWin32(lRet);
148  }
149 
150  HRESULT EnumKey(DWORD dwIndex, OUT FILECHAR_t* pszNameRet, DWORD& dwSizeName) noexcept
151  {
157  const LSTATUS lRet = _FNF(::RegEnumKeyEx)(get_HKey(), dwIndex, pszNameRet, &dwSizeName,
158  nullptr, nullptr, nullptr, nullptr);
159  return HResult::FromWin32(lRet);
160  }
161 
162  // Values
163 
164  HRESULT EnumValue(DWORD dwIndex, FILECHAR_t* pszNameRet, DWORD& dwSizeName, DWORD* pdwTypeRet = nullptr, void* pDataRet = nullptr, DWORD* pdwSizeData = nullptr) noexcept
165  {
170  const LSTATUS lRet = _FNF(::RegEnumValue)(get_HKey(), dwIndex, pszNameRet, &dwSizeName, nullptr,
171  pdwTypeRet, (LPBYTE)pDataRet, pdwSizeData);
172  return HResult::FromWin32(lRet);
173  }
174  HRESULT DeleteValue(const FILECHAR_t* pszSubKey) noexcept
175  {
177  const LSTATUS lRet = _FNF(::RegDeleteValue)(get_HKey(), pszSubKey);
178  return HResult::FromWin32(lRet);
179  }
180  HRESULT SetValue(const FILECHAR_t* pszValueName, DWORD dwType, const void* pData, DWORD dwDataSize) noexcept
181  {
186 
187  const LSTATUS lRet = _FNF(::RegSetValueEx)(get_HKey(), pszValueName,
188  0, dwType, (LPBYTE)pData, dwDataSize);
189  return HResult::FromWin32(lRet);
190  }
191 
192  HRESULT QueryValue(const FILECHAR_t* pszValueName, OUT DWORD& rdwType, OUT void* pData, OUT DWORD& rdwDataSize) noexcept
193  {
201  const LSTATUS lRet = _FNF(::RegQueryValueEx)(get_HKey(), pszValueName, nullptr, &rdwType, (LPBYTE)pData, &rdwDataSize);
202  return HResult::FromWin32(lRet);
203  }
204 
205  HRESULT SetValueDWORD(const FILECHAR_t* lpszValueName, DWORD dwValue)
206  {
210  ASSERT(lpszValueName != nullptr);
211  return SetValue(lpszValueName, REG_DWORD, &dwValue, sizeof(DWORD));
212  }
213 
214  // Helper Combos
215  HRESULT OpenQuerySubKey(HKEY hKeyBase, const FILECHAR_t* pszSubKey, FILECHAR_t* pData, DWORD dwDataSize = _MAX_PATH - 1)
216  {
218  const HRESULT hRes = Open(hKeyBase, pszSubKey, KEY_QUERY_VALUE);
219  if (FAILED(hRes))
220  return hRes;
221  DWORD dwType = REG_SZ; // always REG_SZ or REG_EXPAND_SZ
222  return QueryValue(nullptr, dwType, pData, OUT dwDataSize);
223  }
224  };
225 
226  template <> inline void cHandlePtr<HKEY>::CloseHandle(HKEY h) // static
227  {
229  if (cRegKey::IsKeyBase(h)) // never close base keys
230  return;
231  ::RegCloseKey(h); // ignored BOOL return.
232  }
233 }
234 
235 #endif
236 #endif
#define _FNF(c)
_WIN32 name has a A or W for UTF8 or UNICODE
Definition: FileName.h:24
#define FAILED(x)
Definition: HResult.h:30
#define _MAX_PATH
Definition: SysTypes.h:423
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define HANDLEPTR_NULL
NOT the same as an OS HANDLE, Not an int in Linux. Always void* based.
Definition: cHandlePtr.h:17
static HRESULT FromWin32(DWORD dwWin32Code) noexcept
Definition: HResult.h:198
void CloseHandle()
Definition: cHandlePtr.h:79
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
LONG LSTATUS
AKA error_status_t. FACILITY_WIN32 codes returned from RegCreateKeyEx() etc. Maybe NOT GetLastError()...
Definition: HResult.h:74
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22