1 /*
2 * LAME MP3 encoder for DirectShow
3 * Registry calls handling class
4 *
5 * Copyright (c) 2000-2005 Marie Orlova, Peter Gubanov, Vitaly Ivanov, Elecard Ltd.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 #include <Windows.h>
24
25 #include "reg.h"
26
27 namespace Lame
28 {
29
CRegKey(void)30 CRegKey::CRegKey(void)
31 {
32 m_hRootKey = NULL;
33 m_name[0] = 0;
34 m_hKey = NULL;
35 }
36
37
CRegKey(HKEY rt,PTSTR pName)38 CRegKey::CRegKey(HKEY rt, PTSTR pName)
39 {
40 m_hRootKey = rt;
41 m_hKey = NULL;
42 if(pName)
43 {
44 lstrcpy(m_name, pName);
45 Open(m_hRootKey, m_name);
46 }
47 else
48 m_name[0] = 0;
49 }
50
51
~CRegKey(void)52 CRegKey::~CRegKey(void)
53 {
54 Close();
55 }
56
57
58
Open(HKEY rootKey,PTSTR pName)59 BOOL CRegKey::Open(HKEY rootKey, PTSTR pName)
60 {
61 if(m_hKey)
62 Close();
63
64 m_hRootKey = rootKey;
65 if(pName)
66 {
67 lstrcpy(m_name, pName);
68 if(RegOpenKeyEx(m_hRootKey, m_name, 0, KEY_ALL_ACCESS, &m_hKey) != ERROR_SUCCESS)
69 {
70 m_hKey = NULL;
71 return FALSE;
72 }
73 }
74 else
75 {
76 m_name[0] = 0;
77 m_hKey = m_hRootKey;
78 }
79
80 return TRUE;
81 }
82
83
Create(HKEY rootKey,PTSTR pName)84 BOOL CRegKey::Create(HKEY rootKey, PTSTR pName)
85 {
86 if(m_hKey)
87 Close();
88
89 m_hRootKey = rootKey;
90 if(pName)
91 {
92 lstrcpy(m_name, pName);
93 if(RegCreateKeyEx(m_hRootKey, pName, NULL,
94 TEXT(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
95 &m_hKey, NULL) != ERROR_SUCCESS)
96 {
97
98 m_hKey = NULL;
99 return FALSE;
100 }
101 }
102 else
103 {
104 m_name[0] = 0;
105 }
106 m_hRootKey = m_hKey;
107
108 return TRUE;
109 }
110
111
Open(PTSTR an)112 BOOL CRegKey::Open(PTSTR an)
113 {
114 TCHAR achName[MAX_PATH];
115
116 if(m_hKey)
117 Close();
118
119 lstrcpy(achName, m_name);
120 if(an)
121 lstrcat(achName, an);
122
123 if(RegOpenKeyEx(m_hRootKey, achName, 0, KEY_ALL_ACCESS, &m_hKey) != ERROR_SUCCESS)
124 {
125 m_hKey = NULL;
126 return FALSE;
127 }
128
129 return TRUE;
130 }
131
132
Create(PTSTR an)133 BOOL CRegKey::Create(PTSTR an)
134 {
135 TCHAR achName[MAX_PATH];
136
137 if(m_hKey)
138 Close();
139
140 lstrcpy(achName, m_name);
141 if(an)
142 lstrcat(achName, an);
143
144 if(RegCreateKeyEx(m_hRootKey, achName, NULL,
145 TEXT(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
146 &m_hKey, NULL) != ERROR_SUCCESS)
147 {
148
149 m_hKey = NULL;
150 return FALSE;
151 }
152
153 return TRUE;
154 }
155
156
Close(void)157 BOOL CRegKey::Close(void)
158 {
159 if(m_hKey && m_hKey != m_hRootKey)
160 RegCloseKey(m_hKey);
161
162 m_hKey = 0;
163 return TRUE;
164 }
165
getFlag(PTSTR valuename,BOOL bDefault)166 BOOL CRegKey::getFlag(PTSTR valuename, BOOL bDefault)
167 {
168 if(!m_hKey)
169 return bDefault;
170
171 DWORD cbData;
172 DWORD dwData;
173 DWORD dwType;
174
175 cbData = sizeof(dwData);
176 if(RegQueryValueEx(m_hKey, valuename, NULL, &dwType, (PBYTE)&dwData, &cbData) == ERROR_SUCCESS)
177 {
178 if(dwType == REG_DWORD)
179 return (dwData) ? TRUE : FALSE;
180 }
181 return bDefault;
182 }
183
184
setFlag(PTSTR valuename,BOOL bValue,BOOL bDefault)185 void CRegKey::setFlag(PTSTR valuename, BOOL bValue, BOOL bDefault)
186 {
187 if(getFlag(valuename, bDefault) == bValue )
188 return;
189
190 RegSetValueEx(m_hKey, valuename, 0, REG_DWORD, (PBYTE)&bValue, sizeof(bValue));
191 }
192
193
setFlag(PTSTR valuename,BOOL bValue)194 void CRegKey::setFlag(PTSTR valuename, BOOL bValue)
195 {
196 RegSetValueEx(m_hKey, valuename, 0, REG_DWORD, (PBYTE)&bValue, sizeof(bValue));
197 }
198
199
getDWORD(PTSTR valuename,DWORD bDefault)200 DWORD CRegKey::getDWORD(PTSTR valuename, DWORD bDefault)
201 {
202 DWORD dwData;
203 DWORD cbData;
204 DWORD dwType;
205
206 if(!m_hKey)
207 return bDefault;
208
209 cbData = sizeof(dwData);
210 if(RegQueryValueEx(m_hKey, valuename, NULL, &dwType, (PBYTE)&dwData, &cbData) == ERROR_SUCCESS) {
211 if(dwType == REG_DWORD)
212 {
213 return (UINT)dwData;
214 }
215 }
216
217 return bDefault;
218 }
219
220
setDWORD(PTSTR valuename,DWORD dwValue,DWORD dwDefault)221 void CRegKey::setDWORD(PTSTR valuename, DWORD dwValue, DWORD dwDefault)
222 {
223 DWORD dwData = dwValue;
224
225 if(getDWORD(valuename, dwDefault) == dwValue)
226 return;
227
228 RegSetValueEx(m_hKey, valuename, 0, REG_DWORD, (PBYTE)&dwData, sizeof(dwData));
229 }
230
231
setDWORD(PTSTR valuename,DWORD dwValue)232 void CRegKey::setDWORD(PTSTR valuename, DWORD dwValue)
233 {
234 DWORD dwData = dwValue;
235 RegSetValueEx(m_hKey, valuename, 0, REG_DWORD, (PBYTE)&dwData, sizeof(dwData));
236 }
237
238
getString(PTSTR valuename,PTSTR pDefault,PTSTR pResult,int cbSize)239 DWORD CRegKey::getString(PTSTR valuename, PTSTR pDefault, PTSTR pResult, int cbSize)
240 {
241 DWORD dwType;
242
243 cbSize *= sizeof(TCHAR); // for unicode strings
244
245 if(m_hKey)
246 {
247 if(RegQueryValueEx(m_hKey, valuename, NULL, &dwType, (LPBYTE)pResult, (LPDWORD)&cbSize) == ERROR_SUCCESS)
248 {
249 if(dwType == REG_SZ)
250 {
251 return(cbSize - 1);
252 }
253 }
254 }
255 lstrcpy(pResult, pDefault);
256 return lstrlen(pDefault);
257 }
258
259
setString(PTSTR valuename,PTSTR pData)260 void CRegKey::setString(PTSTR valuename, PTSTR pData)
261 {
262 RegSetValueEx(m_hKey, valuename, 0, REG_SZ, (LPBYTE)pData, (lstrlen(pData) + 1)*sizeof(TCHAR));
263 }
264
265
getBinary(PTSTR valuename,PVOID pDefault,PVOID pResult,int cbSize)266 DWORD CRegKey::getBinary(PTSTR valuename, PVOID pDefault, PVOID pResult, int cbSize)
267 {
268 DWORD dwType;
269
270 if(RegQueryValueEx(m_hKey, valuename, NULL, &dwType, (LPBYTE)pResult, (LPDWORD)&cbSize) == ERROR_SUCCESS)
271 {
272 if(dwType == REG_BINARY)
273 {
274 return cbSize;
275 }
276 }
277
278 memmove(pResult, pDefault, cbSize);
279 return cbSize;
280 }
281
282
setBinary(PTSTR valuename,PVOID pData,int cbSize)283 DWORD CRegKey::setBinary(PTSTR valuename, PVOID pData, int cbSize)
284 {
285 RegSetValueEx(m_hKey, valuename, 0, REG_BINARY, (LPBYTE)pData, cbSize);
286 return cbSize;
287 }
288
289 } // namespace Lame
290