1*32b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir //---------------------------------------
25cdf0e10cSrcweir //
26cdf0e10cSrcweir //---------------------------------------
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #ifdef _MSC_VER
29cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir #include <windows.h>
32cdf0e10cSrcweir #ifdef _MSC_VER
33cdf0e10cSrcweir #pragma warning(pop)
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <malloc.h>
37cdf0e10cSrcweir #include "registrywnt.hxx"
38cdf0e10cSrcweir #include "registryvalueimpl.hxx"
39cdf0e10cSrcweir #include "registryexception.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <assert.h>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #ifdef _MSC_VER
44cdf0e10cSrcweir #pragma warning(disable : 4786 4350)
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //---------------------------------------
48cdf0e10cSrcweir //
49cdf0e10cSrcweir //---------------------------------------
50cdf0e10cSrcweir 
51cdf0e10cSrcweir const size_t MAX_TMP_BUFF_SIZE = 1024 * sizeof(wchar_t);
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //############################################
55cdf0e10cSrcweir // Creation
56cdf0e10cSrcweir // only possible through WindowsRegistry class
57cdf0e10cSrcweir //############################################
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //-----------------------------------------------------
61cdf0e10cSrcweir /** Create instance and open the specified Registry key
62cdf0e10cSrcweir */
RegistryKeyImplWinNT(HKEY RootKey,const std::wstring & KeyName)63cdf0e10cSrcweir RegistryKeyImplWinNT::RegistryKeyImplWinNT(HKEY RootKey, const std::wstring& KeyName) :
64cdf0e10cSrcweir 	RegistryKeyImpl(RootKey, KeyName)
65cdf0e10cSrcweir {
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir //-----------------------------------------------------
69cdf0e10cSrcweir /** Create instance and open the specified Registry key
70cdf0e10cSrcweir */
RegistryKeyImplWinNT(HKEY RootKey)71cdf0e10cSrcweir RegistryKeyImplWinNT::RegistryKeyImplWinNT(HKEY RootKey) :
72cdf0e10cSrcweir 	RegistryKeyImpl(RootKey)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir //-----------------------------------------------------
77cdf0e10cSrcweir /** Create an instances of the specified Registry key,
78cdf0e10cSrcweir 	the key is assumed to be already opened.
79cdf0e10cSrcweir */
RegistryKeyImplWinNT(HKEY RootKey,HKEY SubKey,const std::wstring & KeyName,bool Writeable)80cdf0e10cSrcweir RegistryKeyImplWinNT::RegistryKeyImplWinNT(HKEY RootKey, HKEY SubKey, const std::wstring& KeyName, bool Writeable) :
81cdf0e10cSrcweir 	RegistryKeyImpl(RootKey, SubKey, KeyName, Writeable)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir //############################################
87cdf0e10cSrcweir // Queries
88cdf0e10cSrcweir //############################################
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 
91cdf0e10cSrcweir //-----------------------------------------------------
92cdf0e10cSrcweir /** The number of sub values of the key at hand
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	@precond IsOpen = true
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	@throws
97cdf0e10cSrcweir */
GetSubValueCount() const98cdf0e10cSrcweir size_t RegistryKeyImplWinNT::GetSubValueCount() const
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	assert(IsOpen());
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	DWORD nSubValues = 0;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	LONG rc = RegQueryInfoKeyW(
105cdf0e10cSrcweir 		m_hSubKey,
106cdf0e10cSrcweir 		0, 0, 0, 0, 0, 0, &nSubValues, 0, 0, 0, 0);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
109cdf0e10cSrcweir 		throw RegistryIOException(rc);
110cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
111cdf0e10cSrcweir 		throw RegistryException(rc);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	return nSubValues;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir //-----------------------------------------------------
117cdf0e10cSrcweir /** The number of sub-keys of the key at hand
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	@precond IsOpen = true
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	@throws
122cdf0e10cSrcweir */
GetSubKeyCount() const123cdf0e10cSrcweir size_t RegistryKeyImplWinNT::GetSubKeyCount() const
124cdf0e10cSrcweir {
125cdf0e10cSrcweir 	assert(IsOpen());
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	DWORD nSubKeys = 0;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	LONG rc = RegQueryInfoKeyA(
130cdf0e10cSrcweir 		m_hSubKey,
131cdf0e10cSrcweir 		0, 0, 0, &nSubKeys, 0, 0, 0, 0, 0, 0, 0);
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
134cdf0e10cSrcweir 		throw RegistryIOException(rc);
135cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
136cdf0e10cSrcweir 		throw RegistryException(rc);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	return nSubKeys;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir //-----------------------------------------------------
142cdf0e10cSrcweir /**
143cdf0e10cSrcweir */
GetSubKeyNames() const144cdf0e10cSrcweir StringListPtr RegistryKeyImplWinNT::GetSubKeyNames() const
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	assert(IsOpen());
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	wchar_t buff[1024];
149cdf0e10cSrcweir 	DWORD  buff_size = sizeof(buff);
150cdf0e10cSrcweir 	FILETIME ftime;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	StringList* key_names = new StringList();
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	LONG rc = ERROR_SUCCESS;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	for (DWORD i = 0; /* left empty */; i++)
157cdf0e10cSrcweir 	{
158cdf0e10cSrcweir 		rc = RegEnumKeyExW(
159cdf0e10cSrcweir 			m_hSubKey, i, buff, &buff_size,
160cdf0e10cSrcweir 			0, 0, 0, &ftime);
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 		if (ERROR_SUCCESS != rc &&
163cdf0e10cSrcweir 			ERROR_MORE_DATA != rc)
164cdf0e10cSrcweir 			break;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 		buff_size = sizeof(buff);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 		key_names->push_back(buff);
169cdf0e10cSrcweir 	}
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
172cdf0e10cSrcweir 		throw RegistryIOException(rc);
173cdf0e10cSrcweir 	else if (ERROR_NO_MORE_ITEMS != rc && ERROR_SUCCESS != rc)
174cdf0e10cSrcweir 		throw RegistryException(rc);
175cdf0e10cSrcweir 
176cdf0e10cSrcweir #if (_MSC_VER < 1300) && !defined(__MINGW32__)
177cdf0e10cSrcweir 	return key_names;
178cdf0e10cSrcweir #else
179cdf0e10cSrcweir 	return (StringListPtr) key_names;
180cdf0e10cSrcweir #endif
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir //-----------------------------------------------------
184cdf0e10cSrcweir /**
185cdf0e10cSrcweir */
GetSubValueNames() const186cdf0e10cSrcweir StringListPtr RegistryKeyImplWinNT::GetSubValueNames() const
187cdf0e10cSrcweir {
188cdf0e10cSrcweir 	assert(IsOpen());
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	wchar_t buff[1024];
191cdf0e10cSrcweir 	DWORD  buff_size = sizeof(buff);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	StringList* value_names = new StringList();
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	LONG rc = ERROR_SUCCESS;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	for (DWORD i = 0; /* left empty */; i++)
198cdf0e10cSrcweir 	{
199cdf0e10cSrcweir 		rc = RegEnumValueW(
200cdf0e10cSrcweir 			m_hSubKey, i, buff, &buff_size,
201cdf0e10cSrcweir 			0, 0, 0, 0);
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 		if (ERROR_SUCCESS != rc &&
204cdf0e10cSrcweir 			ERROR_MORE_DATA != rc)
205cdf0e10cSrcweir 			break;
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 		buff_size = sizeof(buff);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 		value_names->push_back(buff);
210cdf0e10cSrcweir 	}
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
213cdf0e10cSrcweir 		throw RegistryIOException(rc);
214cdf0e10cSrcweir 	else if (ERROR_NO_MORE_ITEMS != rc && ERROR_SUCCESS != rc)
215cdf0e10cSrcweir 		throw RegistryException(rc);
216cdf0e10cSrcweir 
217cdf0e10cSrcweir #if (_MSC_VER < 1300) && !defined(__MINGW32__)
218cdf0e10cSrcweir 	return value_names;
219cdf0e10cSrcweir #else
220cdf0e10cSrcweir 	return (StringListPtr) value_names;
221cdf0e10cSrcweir #endif
222cdf0e10cSrcweir }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir //-----------------------------------------------------
225cdf0e10cSrcweir /** Get the specified registry value
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	@precond IsOpen = true
228cdf0e10cSrcweir */
GetValue(const std::wstring & Name) const229cdf0e10cSrcweir RegistryValue RegistryKeyImplWinNT::GetValue(const std::wstring& Name) const
230cdf0e10cSrcweir {
231cdf0e10cSrcweir 	assert(IsOpen());
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	DWORD Type;
234cdf0e10cSrcweir 	wchar_t buff[MAX_TMP_BUFF_SIZE];
235cdf0e10cSrcweir 	DWORD   size = sizeof(buff);
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	LONG rc = RegQueryValueExW(
238cdf0e10cSrcweir 		m_hSubKey,
239cdf0e10cSrcweir 		Name.c_str(),
240cdf0e10cSrcweir 		0,
241cdf0e10cSrcweir 		&Type,
242cdf0e10cSrcweir 		reinterpret_cast<LPBYTE>(buff),
243cdf0e10cSrcweir 		&size);
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 	if (ERROR_FILE_NOT_FOUND == rc)
246cdf0e10cSrcweir 		throw RegistryValueNotFoundException(rc);
247cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
248cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
249cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
250cdf0e10cSrcweir 		throw RegistryException(rc);
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	RegistryValue regval;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 	if (REG_DWORD == Type)
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir 		regval = RegistryValue(new RegistryValueImpl(Name, *(reinterpret_cast<int*>(buff))));
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir 	else if (REG_SZ == Type || REG_EXPAND_SZ == Type || REG_MULTI_SZ == Type)
259cdf0e10cSrcweir     {
260cdf0e10cSrcweir         if (size > 0)
261cdf0e10cSrcweir             regval = RegistryValue(new RegistryValueImpl(Name, std::wstring(reinterpret_cast<wchar_t*>(buff))));
262cdf0e10cSrcweir         else
263cdf0e10cSrcweir             regval = RegistryValue(new RegistryValueImpl(Name, std::wstring()));
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir 	else
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir 		assert(false);
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 	return regval;
271cdf0e10cSrcweir }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir //-----------------------------------------------------
274cdf0e10cSrcweir /** Get the specified registry value, return the given
275cdf0e10cSrcweir 	default value if value not found
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	@precond IsOpen = true
278cdf0e10cSrcweir */
GetValue(const std::wstring & Name,const RegistryValue & Default) const279cdf0e10cSrcweir RegistryValue RegistryKeyImplWinNT::GetValue(const std::wstring& Name, const RegistryValue& Default) const
280cdf0e10cSrcweir {
281cdf0e10cSrcweir 	assert(IsOpen());
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 	DWORD Type;
284cdf0e10cSrcweir 	wchar_t buff[MAX_TMP_BUFF_SIZE];
285cdf0e10cSrcweir 	DWORD   size = sizeof(buff);
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 	LONG rc = RegQueryValueExW(
288cdf0e10cSrcweir 		m_hSubKey,
289cdf0e10cSrcweir 		Name.c_str(),
290cdf0e10cSrcweir 		0,
291cdf0e10cSrcweir 		&Type,
292cdf0e10cSrcweir 		reinterpret_cast<LPBYTE>(buff),
293cdf0e10cSrcweir 		&size);
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	if (ERROR_FILE_NOT_FOUND == rc)
296cdf0e10cSrcweir 	{
297cdf0e10cSrcweir 		#if (_MSC_VER < 1300) && !defined(__MINGW32__)
298cdf0e10cSrcweir 		return Default;
299cdf0e10cSrcweir 		#else
300cdf0e10cSrcweir 		RegistryValue regval_ptr;
301cdf0e10cSrcweir 		regval_ptr = RegistryValue(new RegistryValueImpl(*Default));
302cdf0e10cSrcweir 		return regval_ptr;
303cdf0e10cSrcweir 		#endif
304cdf0e10cSrcweir 	}
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	if (ERROR_ACCESS_DENIED == rc)
307cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
308cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
309cdf0e10cSrcweir 		throw RegistryException(rc);
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 	RegistryValue regval;
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 	if (REG_DWORD == Type)
314cdf0e10cSrcweir 		regval = RegistryValue(new RegistryValueImpl(Name, *reinterpret_cast<int*>(buff)));
315cdf0e10cSrcweir 	else if (REG_SZ == Type || REG_EXPAND_SZ == Type || REG_MULTI_SZ == Type)
316cdf0e10cSrcweir 		regval = RegistryValue(new RegistryValueImpl(Name, std::wstring(reinterpret_cast<wchar_t*>(buff))));
317cdf0e10cSrcweir 	else
318cdf0e10cSrcweir 		assert(false);
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 	return regval;
321cdf0e10cSrcweir }
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 
324cdf0e10cSrcweir //############################################
325cdf0e10cSrcweir // Commands
326cdf0e10cSrcweir //############################################
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 
329cdf0e10cSrcweir //-----------------------------------------------------
330cdf0e10cSrcweir /** Open the registry key, has no effect if
331cdf0e10cSrcweir 	the key is already open
332cdf0e10cSrcweir 
333cdf0e10cSrcweir 	@precond IsOpen = false
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 	@throws RegistryKeyNotFoundException
336cdf0e10cSrcweir 			RegistryWriteAccessDenyException
337cdf0e10cSrcweir 			RegistryAccessDenyException
338cdf0e10cSrcweir */
Open(bool Writeable)339cdf0e10cSrcweir void RegistryKeyImplWinNT::Open(bool Writeable)
340cdf0e10cSrcweir {
341cdf0e10cSrcweir 	assert(!IsOpen());
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 	REGSAM regsam = KEY_READ;
344cdf0e10cSrcweir 
345cdf0e10cSrcweir 	if (Writeable)
346cdf0e10cSrcweir 		regsam |= KEY_WRITE;
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 	LONG rc = RegOpenKeyExW(
349cdf0e10cSrcweir 		m_hRootKey,
350cdf0e10cSrcweir 		m_KeyName.c_str(),
351cdf0e10cSrcweir 		0,
352cdf0e10cSrcweir 		regsam,
353cdf0e10cSrcweir 		&m_hSubKey);
354cdf0e10cSrcweir 
355cdf0e10cSrcweir 	if (ERROR_FILE_NOT_FOUND == rc)
356cdf0e10cSrcweir 		throw RegistryKeyNotFoundException(rc);
357cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
358cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
359cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
360cdf0e10cSrcweir 		throw RegistryException(rc);
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 	m_IsWriteable = Writeable;
363cdf0e10cSrcweir 
364cdf0e10cSrcweir 	assert(IsOpen());
365cdf0e10cSrcweir }
366cdf0e10cSrcweir 
367cdf0e10cSrcweir //-----------------------------------------------------
368cdf0e10cSrcweir /** Open the specified sub-key of the registry key
369cdf0e10cSrcweir 	at hand
370cdf0e10cSrcweir 
371cdf0e10cSrcweir 	@precond IsOpen = true
372cdf0e10cSrcweir 			 HasSubKey(Name) = true
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 	@throws RegistryIOException
375cdf0e10cSrcweir 			RegistryKeyNotFoundException
376cdf0e10cSrcweir 			RegistryAccessDeniedException
377cdf0e10cSrcweir */
OpenSubKey(const std::wstring & Name,bool Writeable)378cdf0e10cSrcweir RegistryKey RegistryKeyImplWinNT::OpenSubKey(const std::wstring& Name, bool Writeable)
379cdf0e10cSrcweir {
380cdf0e10cSrcweir 	RegistryKey regkey(new RegistryKeyImplWinNT(m_hSubKey, Name));
381cdf0e10cSrcweir 	regkey->Open(Writeable);
382cdf0e10cSrcweir 	return regkey;
383cdf0e10cSrcweir }
384cdf0e10cSrcweir 
385cdf0e10cSrcweir //-----------------------------------------------------
386cdf0e10cSrcweir /** Creates a new sub-key below the key at hand
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 	@precond IsOpen = true
389cdf0e10cSrcweir 			 IsWriteable = true
390cdf0e10cSrcweir 
391cdf0e10cSrcweir 	@throws  RegistryIOException
392cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
393cdf0e10cSrcweir */
394cdf0e10cSrcweir 
CreateSubKey(const std::wstring & Name)395cdf0e10cSrcweir RegistryKey RegistryKeyImplWinNT::CreateSubKey(const std::wstring& Name)
396cdf0e10cSrcweir {
397cdf0e10cSrcweir 	assert(IsOpen());
398cdf0e10cSrcweir 	assert(IsWriteable());
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 	HKEY hRoot = IsRootKey() ? m_hRootKey : m_hSubKey;
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 	HKEY hKey;
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 	LONG rc = RegCreateKeyExW(
405cdf0e10cSrcweir 		hRoot,
406cdf0e10cSrcweir 		Name.c_str(),
407cdf0e10cSrcweir 		0,
408cdf0e10cSrcweir 		0,
409cdf0e10cSrcweir 		REG_OPTION_NON_VOLATILE,
410cdf0e10cSrcweir 		KEY_READ | KEY_WRITE,
411cdf0e10cSrcweir 		0,
412cdf0e10cSrcweir 		&hKey,
413cdf0e10cSrcweir 		0);
414cdf0e10cSrcweir 
415cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
416cdf0e10cSrcweir 		throw RegistryIOException(rc);
417cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
418cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
419cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
420cdf0e10cSrcweir 		throw RegistryException(rc);
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 	return RegistryKey(new RegistryKeyImplWinNT(hRoot, hKey, Name));
423cdf0e10cSrcweir }
424cdf0e10cSrcweir 
425cdf0e10cSrcweir //-----------------------------------------------------
426cdf0e10cSrcweir /** Deletes a sub-key below the key at hand, the
427cdf0e10cSrcweir 	key must not have sub-keys
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 	@precond IsOpen = true
430cdf0e10cSrcweir 			 IsWriteable = true
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 	@throws  RegistryIOException
433cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
434cdf0e10cSrcweir */
DeleteSubKey(const std::wstring & Name)435cdf0e10cSrcweir void RegistryKeyImplWinNT::DeleteSubKey(const std::wstring& Name)
436cdf0e10cSrcweir {
437cdf0e10cSrcweir 	assert(IsOpen());
438cdf0e10cSrcweir 	assert(IsWriteable());
439cdf0e10cSrcweir 	assert(HasSubKey(Name));
440cdf0e10cSrcweir 
441cdf0e10cSrcweir 	RegistryKey SubKey = OpenSubKey(Name);
442cdf0e10cSrcweir 
443cdf0e10cSrcweir 	size_t nSubKeyCount = SubKey->GetSubKeyCount();
444cdf0e10cSrcweir 
445cdf0e10cSrcweir 	assert(0 == nSubKeyCount);
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 	if (nSubKeyCount)
448cdf0e10cSrcweir 		throw RegistryInvalidOperationException(ERROR_NOT_SUPPORTED);
449cdf0e10cSrcweir 
450cdf0e10cSrcweir 	LONG rc = RegDeleteKeyW(m_hSubKey, Name.c_str());
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
453cdf0e10cSrcweir 		throw RegistryIOException(rc);
454cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
455cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
456cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
457cdf0e10cSrcweir 		throw RegistryException(rc);
458cdf0e10cSrcweir }
459cdf0e10cSrcweir 
460cdf0e10cSrcweir //-----------------------------------------------------
461cdf0e10cSrcweir /** Deletes a sub-key below the key at hand with all
462cdf0e10cSrcweir 	its sub-keys
463cdf0e10cSrcweir 
464cdf0e10cSrcweir 	@precond IsOpen = true
465cdf0e10cSrcweir 			 IsWriteable = true;
466cdf0e10cSrcweir 
467cdf0e10cSrcweir 	@throws  RegistryIOException
468cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
469cdf0e10cSrcweir */
DeleteSubKeyTree(const std::wstring & Name)470cdf0e10cSrcweir void RegistryKeyImplWinNT::DeleteSubKeyTree(const std::wstring& Name)
471cdf0e10cSrcweir {
472cdf0e10cSrcweir 	ImplDeleteSubKeyTree(m_hSubKey, Name);
473cdf0e10cSrcweir }
474cdf0e10cSrcweir 
475cdf0e10cSrcweir //-----------------------------------------------------
476cdf0e10cSrcweir /** Deletes a sub-key below the key at hand with all
477cdf0e10cSrcweir 	its sub-keys
478cdf0e10cSrcweir 
479cdf0e10cSrcweir 	@precond IsOpen = true
480cdf0e10cSrcweir 			 IsWriteable = true;
481cdf0e10cSrcweir 
482cdf0e10cSrcweir 	@throws  RegistryIOException
483cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
484cdf0e10cSrcweir */
ImplDeleteSubKeyTree(HKEY RootKey,const std::wstring & Name)485cdf0e10cSrcweir LONG RegistryKeyImplWinNT::ImplDeleteSubKeyTree(HKEY RootKey, const std::wstring& Name)
486cdf0e10cSrcweir {
487cdf0e10cSrcweir 	assert(IsOpen());
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 	HKEY hKey;
490cdf0e10cSrcweir 
491cdf0e10cSrcweir 	LONG rc = RegOpenKeyExW(
492cdf0e10cSrcweir 		RootKey,
493cdf0e10cSrcweir 		Name.c_str(),
494cdf0e10cSrcweir 		0,
495cdf0e10cSrcweir 		KEY_READ | DELETE,
496cdf0e10cSrcweir 		&hKey);
497cdf0e10cSrcweir 
498cdf0e10cSrcweir 	if (ERROR_SUCCESS == rc)
499cdf0e10cSrcweir 	{
500cdf0e10cSrcweir 		wchar_t* lpSubKey;
501cdf0e10cSrcweir 		DWORD    nMaxSubKeyLen;
502cdf0e10cSrcweir 
503cdf0e10cSrcweir 		rc = RegQueryInfoKeyW(
504cdf0e10cSrcweir 			hKey, 0, 0, 0, 0,
505cdf0e10cSrcweir 			&nMaxSubKeyLen,
506cdf0e10cSrcweir 			0, 0, 0, 0, 0, 0);
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 		nMaxSubKeyLen++; // space for trailing '\0'
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 		lpSubKey = reinterpret_cast<wchar_t*>(
511cdf0e10cSrcweir 			_alloca(nMaxSubKeyLen*sizeof(wchar_t)));
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 		while (ERROR_SUCCESS == rc)
514cdf0e10cSrcweir         {
515cdf0e10cSrcweir 			DWORD nLen = nMaxSubKeyLen;
516cdf0e10cSrcweir 
517cdf0e10cSrcweir 			rc = RegEnumKeyExW(
518cdf0e10cSrcweir 				hKey,
519cdf0e10cSrcweir                 0,       // always index zero
520cdf0e10cSrcweir                 lpSubKey,
521cdf0e10cSrcweir                 &nLen,
522cdf0e10cSrcweir                 0, 0, 0, 0);
523cdf0e10cSrcweir 
524cdf0e10cSrcweir             if (ERROR_NO_MORE_ITEMS == rc)
525cdf0e10cSrcweir             {
526cdf0e10cSrcweir 				rc = RegDeleteKeyW(RootKey, Name.c_str());
527cdf0e10cSrcweir                 break;
528cdf0e10cSrcweir             }
529cdf0e10cSrcweir             else if (rc == ERROR_SUCCESS)
530cdf0e10cSrcweir 			{
531cdf0e10cSrcweir 				rc = ImplDeleteSubKeyTree(hKey, lpSubKey);
532cdf0e10cSrcweir 			}
533cdf0e10cSrcweir 
534cdf0e10cSrcweir 		} // while
535cdf0e10cSrcweir 
536cdf0e10cSrcweir         RegCloseKey(hKey);
537cdf0e10cSrcweir 
538cdf0e10cSrcweir 	} // if
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
541cdf0e10cSrcweir 		throw RegistryIOException(rc);
542cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
543cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
544cdf0e10cSrcweir 	else if (ERROR_FILE_NOT_FOUND == rc)
545cdf0e10cSrcweir 		throw RegistryKeyNotFoundException(rc);
546cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
547cdf0e10cSrcweir 		throw RegistryException(rc);
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 	return rc;
550cdf0e10cSrcweir }
551cdf0e10cSrcweir 
552cdf0e10cSrcweir //-----------------------------------------------------
553cdf0e10cSrcweir /** Delete the specified value
554cdf0e10cSrcweir 
555cdf0e10cSrcweir 		@precond IsOpen = true
556cdf0e10cSrcweir 				 IsWriteable = true
557cdf0e10cSrcweir 				 HasValue(Name) = true
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 		@throws	RegistryIOException
560cdf0e10cSrcweir 				RegistryWriteAccessDeniedException
561cdf0e10cSrcweir 				RegistryValueNotFoundException
562cdf0e10cSrcweir */
DeleteValue(const std::wstring & Name)563cdf0e10cSrcweir void RegistryKeyImplWinNT::DeleteValue(const std::wstring& Name)
564cdf0e10cSrcweir {
565cdf0e10cSrcweir 	assert(IsOpen());
566cdf0e10cSrcweir 	assert(HasValue(Name));
567cdf0e10cSrcweir 	assert(IsWriteable());
568cdf0e10cSrcweir 
569cdf0e10cSrcweir 	LONG rc = RegDeleteValueW(
570cdf0e10cSrcweir 		m_hSubKey,
571cdf0e10cSrcweir 		Name.c_str());
572cdf0e10cSrcweir 
573cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
574cdf0e10cSrcweir 		throw RegistryIOException(rc);
575cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
576cdf0e10cSrcweir 		throw RegistryNoWriteAccessException(rc);
577cdf0e10cSrcweir 	else if (ERROR_FILE_NOT_FOUND == rc)
578cdf0e10cSrcweir 		throw RegistryValueNotFoundException(rc);
579cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
580cdf0e10cSrcweir 		throw RegistryException(rc);
581cdf0e10cSrcweir }
582cdf0e10cSrcweir 
583cdf0e10cSrcweir //-----------------------------------------------------
584cdf0e10cSrcweir /** Set the specified registry value
585cdf0e10cSrcweir 
586cdf0e10cSrcweir 	@precond IsOpen = true
587cdf0e10cSrcweir 			 IsWriteable = true
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 	@throws  RegistryIOException
590cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
591cdf0e10cSrcweir */
SetValue(const RegistryValue & Value)592cdf0e10cSrcweir void RegistryKeyImplWinNT::SetValue(const RegistryValue& Value)
593cdf0e10cSrcweir {
594cdf0e10cSrcweir 	assert(IsOpen());
595cdf0e10cSrcweir 	assert(IsWriteable());
596cdf0e10cSrcweir 
597cdf0e10cSrcweir 	LONG rc = RegSetValueExW(
598cdf0e10cSrcweir 		m_hSubKey,
599cdf0e10cSrcweir 		Value->GetName().c_str(),
600cdf0e10cSrcweir 		0,
601cdf0e10cSrcweir 		Value->GetType(),
602cdf0e10cSrcweir 		reinterpret_cast<const unsigned char*>(Value->GetDataBuffer()),
603cdf0e10cSrcweir 		static_cast<DWORD>(Value->GetDataSize()));
604cdf0e10cSrcweir 
605cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
606cdf0e10cSrcweir 		throw RegistryIOException(rc);
607cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
608cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
609cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
610cdf0e10cSrcweir 		throw RegistryException(rc);
611cdf0e10cSrcweir }
612cdf0e10cSrcweir 
613cdf0e10cSrcweir 
614cdf0e10cSrcweir 
615cdf0e10cSrcweir 
616