Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cRegKeyX.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cRegKeyX_H
8 #define _INC_cRegKeyX_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "../GrayLibBase.h"
14 
15 #ifdef _WIN32
19 
20 namespace GrayLib
21 {
22  struct cRegHive
23  {
27 
28  HKEY m_hKeyBase;
29  cString m_sName;
30  };
31 
32  class GRAYLIB_LINK cRegKeyX
33  : public cRegKey
34  {
40  typedef cRegKey SUPER_t;
41 
42  public:
43  static const cRegKeyName k_aNames[]; // map default HKEY values to names.
44 
45  cRegKeyX(HKEY hKey = HKEY_LOCAL_MACHINE) noexcept
46  : cRegKey(hKey)
47  {
50  }
51  ~cRegKeyX() noexcept
52  {
53  }
54 
55  static const FILECHAR_t* GRAYCALL GetNameBase(HKEY hKey) noexcept;
56 
57  const FILECHAR_t* get_NameBase() const noexcept
58  {
59  return GetNameBase(get_HKey());
60  }
61 
62  HRESULT OpenCreate(HKEY hKeyBase, const FILECHAR_t* pszSubKey, DWORD dwOptions = REG_OPTION_NON_VOLATILE,
63  REGSAM samDesired = KEY_ALL_ACCESS, SECURITY_ATTRIBUTES* pSa = nullptr);
64 
65  // Keys
66 
67  HRESULT SetSubKey(const FILECHAR_t* pszSubKey, const FILECHAR_t* pszValue)
68  {
76  DWORD dwLenData;
77  if (pszValue == nullptr)
78  {
79  dwLenData = 0;
80  }
81  else
82  {
83  dwLenData = StrT::Len(pszValue) + 1;
84  }
85 #ifdef UNDER_CE
86  LSTATUS lRet = _FNF(::RegSetValueEx)(get_HKey(), pszSubKey, 0, REG_SZ, (const BYTE*)pszValue, dwLenData);
87 #else
88  LSTATUS lRet = _FNF(::RegSetValue)(get_HKey(), pszSubKey, REG_SZ, pszValue, dwLenData);
89 #endif
90  return HResult::FromWin32(lRet);
91  }
92 
93 #if (_MSC_VER>=1300) || defined(__GNUC__) // && !defined(_WINDLL)
94  HRESULT DeleteKeyTree(const FILECHAR_t* pszSubKey);
95 #endif
96 
97  // Values
98 
99  HRESULT SetValueInit(const cRegKeyInit& val);
100 
101  HRESULT SetValueStr(const FILECHAR_t* lpszValueName, const FILECHAR_t* pszValue)
102  {
108  ASSERT(pszValue != nullptr);
109  return SetValue(lpszValueName, REG_SZ, pszValue, StrT::Len(pszValue) + 1);
110  }
111 
112  HRESULT QueryValueDWORD(const FILECHAR_t* lpszValueName, OUT DWORD& dwValue);
113  HRESULT QueryValueStr(const FILECHAR_t* lpszValueName, OUT FILECHAR_t* pszValue, StrLen_t iSizeMax, bool bExp = false);
114 
115  // Used for profile type access
116  HRESULT WriteValueStringExp(const FILECHAR_t* pszKey, const FILECHAR_t* pszValue)
117  {
120  ASSERT(pszValue != nullptr);
121  return SetValue(pszKey, REG_EXPAND_SZ, pszValue, StrT::Len(pszValue) + 1);
122  }
123  HRESULT WriteValueInt(const FILECHAR_t* pszKey, int iValue)
124  {
127  return SetValueDWORD(pszKey, iValue);
128  }
129 
130  cStringT<FILECHAR_t> GetValueString(const FILECHAR_t* pszKey, const FILECHAR_t* pszValDefault = _FN(""), bool bExp = false)
131  {
133  HRESULT hRes = QueryValueStr(pszKey, szTmp, STRMAX(szTmp), bExp);
134  if (SUCCEEDED(hRes))
135  {
136  return szTmp;
137  }
138  if (pszValDefault == nullptr)
139  {
140  return cString("");
141  }
142  return cStringT<FILECHAR_t>(pszValDefault);
143  }
144  int GetValueInt(const FILECHAR_t* pszKey, int iValDefault)
145  {
147  DWORD dwValue;
148  HRESULT hRes = QueryValueDWORD(pszKey, dwValue);
149  if (SUCCEEDED(hRes))
150  {
151  return dwValue;
152  }
153  return iValDefault;
154  }
155 
156  // Helper Combos
157 
158  static bool GRAYCALL MakeValueDWORD(OUT DWORD& rdwValue, DWORD dwType, const void* pData, DWORD dwDataSize);
159  static StrLen_t GRAYCALL MakeValueStr(OUT FILECHAR_t* pszValue, StrLen_t iSizeMax, DWORD dwType, const void* pData, DWORD dwDataSize, bool bExp = false);
160 
161  // HRESULT SetNotifyChangeKeyValue(bool bWatchSubtree, DWORD dwNotifyFilter, HANDLE hEvent, bool fAsynchronous = true);
162 
163  UNITTEST_FRIEND(cRegKey);
164  };
165 }
166 
167 #endif // _WIN32
168 #endif // _INC_cRegKey_H
#define _FN(x)
like _T(x) macro for static text file names.
Definition: FileName.h:23
#define _FNF(c)
_WIN32 name has a A or W for UTF8 or UNICODE
Definition: FileName.h:24
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define SUCCEEDED(x)
Definition: HResult.h:29
#define STRMAX(x)
Get Max size of static string space. minus the '\0' terminator character.
Definition: StrConst.h:33
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
static HRESULT FromWin32(DWORD dwWin32Code) noexcept
Definition: HResult.h:198
Definition: cMesh.h:22
LONG LSTATUS
AKA error_status_t. FACILITY_WIN32 codes returned from RegCreateKeyEx() etc. Maybe NOT GetLastError()...
Definition: HResult.h:74
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
cStringT< GChar_t > cString
Definition: cString.h:636
static const StrLen_t k_LEN_MAX
arbitrary max size for Format() etc. NOTE: _MSC_VER says stack frame should be at least 16384
Definition: StrT.h:75
static StrLen_t Len(const TYPE *pszStr) noexcept