OSDN Git Service

Update TranslationsStatus.*
[winmerge-jp/winmerge-jp.git] / ShellExtension / RegKey.cpp
1 /** 
2  * @file  RegKey.cpp
3  *
4  * @brief Implementation of CRegKeyEx C++ wrapper class for reading Windows registry
5  */
6
7 #define NOMINMAX
8 #include "RegKey.h"
9 #include <windows.h>
10 #include <cassert>
11 #include <strsafe.h>
12 #include "UnicodeString.h"
13
14 /**
15  * @brief Default constructor.
16  */
17 CRegKeyEx::CRegKeyEx()
18 : m_hKey(NULL)
19 {
20 }
21
22 /**
23  * @brief Default destructor.
24  */
25 CRegKeyEx::~CRegKeyEx()
26 {
27         Close();
28 }
29
30 /**
31  * @brief Closes the key.
32  */
33 void CRegKeyEx::Close()
34 {
35         if (m_hKey) 
36         {
37                 RegCloseKey(m_hKey);
38                 m_hKey = NULL;
39         }
40 }
41
42 /**
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.
47  */
48 LONG CRegKeyEx::Open(HKEY hKeyRoot, LPCTSTR pszPath)
49 {
50         return OpenWithAccess(hKeyRoot, pszPath, KEY_ALL_ACCESS);
51 }
52
53 /**
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.
59  */
60 LONG CRegKeyEx::OpenWithAccess(HKEY hKeyRoot, LPCTSTR pszPath, REGSAM regsam)
61 {
62         DWORD dw;
63
64         Close();
65         m_sPath = pszPath;
66
67         return RegCreateKeyEx(hKeyRoot, pszPath, 0L, NULL,
68                 REG_OPTION_NON_VOLATILE, regsam, NULL, 
69                 &m_hKey, &dw);
70 }
71
72 /**
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.
78  */
79 LONG CRegKeyEx::OpenNoCreateWithAccess(HKEY hKeyRoot, LPCTSTR pszPath, REGSAM regsam)
80 {
81         Close();
82
83         m_sPath = pszPath;
84
85         return RegOpenKeyEx (hKeyRoot, pszPath, 0L, regsam, &m_hKey);
86 }
87
88 /**
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.
92  */
93 bool CRegKeyEx::QueryRegMachine(LPCTSTR key)
94 {
95         return OpenNoCreateWithAccess(HKEY_LOCAL_MACHINE, key, KEY_QUERY_VALUE) == ERROR_SUCCESS;
96 }
97
98 /**
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.
102  */
103 bool CRegKeyEx::QueryRegUser(LPCTSTR key)
104 {
105         return OpenNoCreateWithAccess(HKEY_CURRENT_USER, key, KEY_QUERY_VALUE) == ERROR_SUCCESS;
106 }
107
108 /**
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.
113  */
114 LONG CRegKeyEx::WriteDword(LPCTSTR pszKey, DWORD dwVal)
115 {
116         assert(m_hKey);
117         assert(pszKey);
118         return RegSetValueEx(m_hKey, pszKey, 0L, REG_DWORD,
119                 (const LPBYTE) &dwVal, sizeof(DWORD));
120 }
121
122 /**
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.
127  */
128 LONG CRegKeyEx::WriteBool(LPCTSTR pszKey, BOOL bVal)
129 {
130         assert(m_hKey);
131         assert(pszKey);
132         DWORD dwVal = (DWORD)bVal; 
133         return RegSetValueEx(m_hKey, pszKey, 0L, REG_DWORD,
134                 (const LPBYTE) &dwVal, sizeof(DWORD));
135 }
136
137 /**
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.
142  */
143 LONG CRegKeyEx::WriteFloat(LPCTSTR pszKey, float fVal)
144 {
145         assert(m_hKey);
146         assert(pszKey);
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) );
150 }
151
152 /**
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.
157  */
158 LONG CRegKeyEx::WriteString(LPCTSTR pszKey, LPCTSTR pszData)
159 {
160         assert(m_hKey);
161         assert(pszKey);
162         assert(pszData);
163
164         return RegSetValueEx(m_hKey, pszKey, 0L, REG_SZ,
165                 (const LPBYTE) pszData, static_cast<DWORD>(_tcslen(pszData)+ 1)*sizeof(TCHAR));
166 }
167
168 /**
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.
173  */
174 DWORD CRegKeyEx::ReadDword(LPCTSTR pszKey, DWORD defval)
175 {
176         assert(m_hKey);
177         assert(pszKey);
178
179         DWORD dwType;
180         DWORD dwSize = sizeof (DWORD);
181         DWORD dwDest;
182
183         LONG lRet = RegQueryValueEx (m_hKey, (LPTSTR) pszKey, NULL, 
184                 &dwType, (LPBYTE) &dwDest, &dwSize);
185
186         if (lRet == ERROR_SUCCESS)
187                 return dwDest;
188         else
189                 return defval;
190 }
191
192 /**
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.
197  */
198 LONG CRegKeyEx::ReadLong(LPCTSTR pszKey, LONG defval)
199 {
200         return (LONG)ReadDword(pszKey, (DWORD)defval);
201 }
202
203 /**
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.
208  */
209 UINT CRegKeyEx::ReadUint(LPCTSTR pszKey, UINT defval)
210 {
211         return (UINT)ReadDword(pszKey, (DWORD)defval);
212 }
213
214 /**
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.
219  */
220 UINT CRegKeyEx::ReadInt(LPCTSTR pszKey, int defval)
221 {
222         return (int)ReadDword(pszKey, (DWORD)defval);
223 }
224
225 /**
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.
230  */
231 short int CRegKeyEx::ReadShort(LPCTSTR pszKey, short int defval)
232 {
233         return (short int)ReadDword(pszKey, (DWORD)defval);
234 }
235
236 /**
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.
241  */
242 BYTE CRegKeyEx::ReadByte(LPCTSTR pszKey, BYTE defval)
243 {
244         return (BYTE)ReadDword(pszKey, (DWORD)defval);
245 }
246
247 /**
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.
252  */
253 float CRegKeyEx::ReadFloat(LPCTSTR pszKey, float defval)
254 {
255         assert(m_hKey);
256         assert(pszKey);
257
258         DWORD dwType;
259         DWORD dwSize = 100;
260         TCHAR  string[100];
261
262         LONG lReturn = RegQueryValueEx(m_hKey, (LPTSTR) pszKey, NULL,
263                 &dwType, (LPBYTE) string, &dwSize);
264
265         if (lReturn == ERROR_SUCCESS)
266                 return (float)_tcstod(string, NULL);
267         else
268                 return defval;
269 }
270
271 /**
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.
276  */
277 BOOL CRegKeyEx::ReadBool(LPCTSTR pszKey, BOOL defval)
278 {
279         assert(m_hKey);
280         assert(pszKey);
281
282         DWORD dwType;
283         DWORD dwSize = sizeof(DWORD);
284         DWORD dwDest;
285
286         LONG lRet = RegQueryValueEx(m_hKey, (LPTSTR) pszKey, NULL, 
287                 &dwType, (LPBYTE) &dwDest, &dwSize);
288
289         if (lRet == ERROR_SUCCESS)
290                 return (dwDest!=(DWORD)0);
291         else
292                 return defval;
293 }
294
295 /**
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.
300  */
301 String CRegKeyEx::ReadString (LPCTSTR pszKey, LPCTSTR defval)
302 {
303         assert(m_hKey);
304         assert(pszKey);
305
306         DWORD dwType;
307         DWORD dwSize = 0;
308         String retString;
309
310         // Get size of the string
311         LONG lReturn = RegQueryValueEx(m_hKey, (LPTSTR) pszKey, NULL,
312                 &dwType, NULL, &dwSize);
313
314         if (lReturn == ERROR_SUCCESS)
315         {
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);
320         }
321         if (lReturn == ERROR_SUCCESS)
322                 return retString;
323         else
324                 return defval;
325 }
326
327 /**
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.
333  */
334 void CRegKeyEx::ReadChars (LPCTSTR pszKey, LPTSTR pData, DWORD dwLen, LPCTSTR defval)
335 {
336         assert(m_hKey);
337         assert(pszKey);
338
339         DWORD dwType;
340         DWORD len = dwLen;
341
342         LONG ret = RegQueryValueEx (m_hKey, (LPTSTR) pszKey, NULL,
343                 &dwType, (LPBYTE)pData, &len);
344         if (ret != ERROR_SUCCESS)
345                 StringCchCopy(pData, dwLen, defval);
346 }