4 * @brief Implementation of CRegKeyEx C++ wrapper class for reading Windows registry
12 #include "UnicodeString.h"
15 * @brief Default constructor.
17 CRegKeyEx::CRegKeyEx()
23 * @brief Default destructor.
25 CRegKeyEx::~CRegKeyEx()
31 * @brief Closes the key.
33 void CRegKeyEx::Close()
43 * @brief Opens or creates a key in given path.
44 * @param [in] hKeyRoot Root key to open, HKLM, HKCU..
45 * @param [in] pszPath Path to actual registry key to access.
46 * @return ERROR_SUCCESS or error value.
48 LONG CRegKeyEx::Open(HKEY hKeyRoot, LPCTSTR pszPath)
50 return OpenWithAccess(hKeyRoot, pszPath, KEY_ALL_ACCESS);
54 * @brief Opens or creates a key in given path with access control.
55 * @param [in] hKeyRoot Root key to open, HKLM, HKCU..
56 * @param [in] pszPath Path to actual registry key to access.
57 * @param [in] regsam Registry access parameter.
58 * @return ERROR_SUCCESS or error value.
60 LONG CRegKeyEx::OpenWithAccess(HKEY hKeyRoot, LPCTSTR pszPath, REGSAM regsam)
67 return RegCreateKeyEx(hKeyRoot, pszPath, 0L, NULL,
68 REG_OPTION_NON_VOLATILE, regsam, NULL,
73 * @brief Opens key in given path.
74 * @param [in] hKeyRoot Root key to open, HKLM, HKCU..
75 * @param [in] pszPath Path to actual registry key to access.
76 * @param [in] regsam Registry access parameter.
77 * @return ERROR_SUCCESS or error value.
79 LONG CRegKeyEx::OpenNoCreateWithAccess(HKEY hKeyRoot, LPCTSTR pszPath, REGSAM regsam)
85 return RegOpenKeyEx (hKeyRoot, pszPath, 0L, regsam, &m_hKey);
89 * @brief Opens registry key from HKEY_LOCAL_MACHINE for reading.
90 * @param [in] key Path to actual registry key to access.
91 * @return true on success, false otherwise.
93 bool CRegKeyEx::QueryRegMachine(LPCTSTR key)
95 return OpenNoCreateWithAccess(HKEY_LOCAL_MACHINE, key, KEY_QUERY_VALUE) == ERROR_SUCCESS;
99 * @brief Opens registry key from HKEY_CURRENT_USER for reading.
100 * @param [in] key Path to actual registry key to access.
101 * @return true on success, false otherwise.
103 bool CRegKeyEx::QueryRegUser(LPCTSTR key)
105 return OpenNoCreateWithAccess(HKEY_CURRENT_USER, key, KEY_QUERY_VALUE) == ERROR_SUCCESS;
109 * @brief Write DWORD value to registry.
110 * @param [in] pszKey Path to actual registry key to access.
111 * @param [in] dwVal Value to write.
112 * @return ERROR_SUCCESS on success, or error value.
114 LONG CRegKeyEx::WriteDword(LPCTSTR pszKey, DWORD dwVal)
118 return RegSetValueEx(m_hKey, pszKey, 0L, REG_DWORD,
119 (const LPBYTE) &dwVal, sizeof(DWORD));
123 * @brief Write BOOL value to registry.
124 * @param [in] pszKey Path to actual registry key to access.
125 * @param [in] bVal Value to write.
126 * @return ERROR_SUCCESS on success, or error value.
128 LONG CRegKeyEx::WriteBool(LPCTSTR pszKey, BOOL bVal)
132 DWORD dwVal = (DWORD)bVal;
133 return RegSetValueEx(m_hKey, pszKey, 0L, REG_DWORD,
134 (const LPBYTE) &dwVal, sizeof(DWORD));
138 * @brief Write float value to registry.
139 * @param [in] pszKey Path to actual registry key to access.
140 * @param [in] fVal Value to write.
141 * @return ERROR_SUCCESS on success, or error value.
143 LONG CRegKeyEx::WriteFloat(LPCTSTR pszKey, float fVal)
147 String s = strutils::to_str(fVal);
148 return RegSetValueEx(m_hKey, pszKey, 0L, REG_SZ,
149 (const LPBYTE) s.c_str(), static_cast<DWORD>((s.length() + 1))*sizeof(TCHAR) );
153 * @brief Write string value to registry.
154 * @param [in] pszKey Path to actual registry key to access.
155 * @param [in] pszData Value to write.
156 * @return ERROR_SUCCESS on success, or error value.
158 LONG CRegKeyEx::WriteString(LPCTSTR pszKey, LPCTSTR pszData)
164 return RegSetValueEx(m_hKey, pszKey, 0L, REG_SZ,
165 (const LPBYTE) pszData, static_cast<DWORD>(_tcslen(pszData)+ 1)*sizeof(TCHAR));
169 * @brief Read DWORD value from registry.
170 * @param [in] pszKey Path to actual registry key to access.
171 * @param [in] defval Default value to return if reading fails.
172 * @return Read DWORD value.
174 DWORD CRegKeyEx::ReadDword(LPCTSTR pszKey, DWORD defval)
180 DWORD dwSize = sizeof (DWORD);
183 LONG lRet = RegQueryValueEx (m_hKey, (LPTSTR) pszKey, NULL,
184 &dwType, (LPBYTE) &dwDest, &dwSize);
186 if (lRet == ERROR_SUCCESS)
193 * @brief Read LONG value from registry.
194 * @param [in] pszKey Path to actual registry key to access.
195 * @param [in] defval Default value to return if reading fails.
196 * @return Read LONG value.
198 LONG CRegKeyEx::ReadLong(LPCTSTR pszKey, LONG defval)
200 return (LONG)ReadDword(pszKey, (DWORD)defval);
204 * @brief Read UINT value from registry.
205 * @param [in] pszKey Path to actual registry key to access.
206 * @param [in] defval Default value to return if reading fails.
207 * @return Read UINT value.
209 UINT CRegKeyEx::ReadUint(LPCTSTR pszKey, UINT defval)
211 return (UINT)ReadDword(pszKey, (DWORD)defval);
215 * @brief Read UINT value from registry.
216 * @param [in] pszKey Path to actual registry key to access.
217 * @param [in] defval Default value to return if reading fails.
218 * @return Read UINT value.
220 UINT CRegKeyEx::ReadInt(LPCTSTR pszKey, int defval)
222 return (int)ReadDword(pszKey, (DWORD)defval);
226 * @brief Read short int value from registry.
227 * @param [in] pszKey Path to actual registry key to access.
228 * @param [in] defval Default value to return if reading fails.
229 * @return Read short int value.
231 short int CRegKeyEx::ReadShort(LPCTSTR pszKey, short int defval)
233 return (short int)ReadDword(pszKey, (DWORD)defval);
237 * @brief Read BYTE value from registry.
238 * @param [in] pszKey Path to actual registry key to access.
239 * @param [in] defval Default value to return if reading fails.
240 * @return Read BYTE value.
242 BYTE CRegKeyEx::ReadByte(LPCTSTR pszKey, BYTE defval)
244 return (BYTE)ReadDword(pszKey, (DWORD)defval);
248 * @brief Read float value from registry.
249 * @param [in] pszKey Path to actual registry key to access.
250 * @param [in] defval Default value to return if reading fails.
251 * @return Read float value.
253 float CRegKeyEx::ReadFloat(LPCTSTR pszKey, float defval)
262 LONG lReturn = RegQueryValueEx(m_hKey, (LPTSTR) pszKey, NULL,
263 &dwType, (LPBYTE) string, &dwSize);
265 if (lReturn == ERROR_SUCCESS)
266 return (float)_tcstod(string, NULL);
272 * @brief Read BOOL value from registry.
273 * @param [in] pszKey Path to actual registry key to access.
274 * @param [in] defval Default value to return if reading fails.
275 * @return Read BOOL value.
277 BOOL CRegKeyEx::ReadBool(LPCTSTR pszKey, BOOL defval)
283 DWORD dwSize = sizeof(DWORD);
286 LONG lRet = RegQueryValueEx(m_hKey, (LPTSTR) pszKey, NULL,
287 &dwType, (LPBYTE) &dwDest, &dwSize);
289 if (lRet == ERROR_SUCCESS)
290 return (dwDest!=(DWORD)0);
296 * @brief Read String value from registry.
297 * @param [in] pszKey Path to actual registry key to access.
298 * @param [in] defval Default value to return if reading fails.
299 * @return Read String value.
301 String CRegKeyEx::ReadString (LPCTSTR pszKey, LPCTSTR defval)
310 // Get size of the string
311 LONG lReturn = RegQueryValueEx(m_hKey, (LPTSTR) pszKey, NULL,
312 &dwType, NULL, &dwSize);
314 if (lReturn == ERROR_SUCCESS)
316 retString.resize(dwSize/sizeof(TCHAR));
317 lReturn = RegQueryValueEx(m_hKey, (LPTSTR) pszKey, NULL,
318 &dwType, (LPBYTE) retString.data(), &dwSize);
319 retString.resize(dwSize/sizeof(TCHAR)-1);
321 if (lReturn == ERROR_SUCCESS)
328 * @brief Read char table from registry.
329 * @param [in] pszKey Path to actual registry key to access.
330 * @param [in] pData Pointer to char table where value is written to.
331 * @param [in] dwLen Size of pData table in bytes.
332 * @param [in] defval Default value to return if reading fails.
334 void CRegKeyEx::ReadChars (LPCTSTR pszKey, LPTSTR pData, DWORD dwLen, LPCTSTR defval)
342 LONG ret = RegQueryValueEx (m_hKey, (LPTSTR) pszKey, NULL,
343 &dwType, (LPBYTE)pData, &len);
344 if (ret != ERROR_SUCCESS)
345 StringCchCopy(pData, dwLen, defval);