1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir //---------------------------------------
29*cdf0e10cSrcweir //
30*cdf0e10cSrcweir //---------------------------------------
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "registryw9x.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <windows.h>
35*cdf0e10cSrcweir #include <malloc.h>
36*cdf0e10cSrcweir #include "registryvalueimpl.hxx"
37*cdf0e10cSrcweir #include "registryexception.hxx"
38*cdf0e10cSrcweir #include "stringconverter.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <assert.h>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #ifdef _MSC_VER
43*cdf0e10cSrcweir #pragma warning(disable : 4786 4350)
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir //---------------------------------------
47*cdf0e10cSrcweir //
48*cdf0e10cSrcweir //---------------------------------------
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir const size_t MAX_TMP_BUFF_SIZE = 1024 * sizeof(wchar_t);
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir //############################################
54*cdf0e10cSrcweir // Creation
55*cdf0e10cSrcweir // only possible through WindowsRegistry class
56*cdf0e10cSrcweir //############################################
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir //-----------------------------------------------------
60*cdf0e10cSrcweir /** Create instance and open the specified Registry key
61*cdf0e10cSrcweir */
62*cdf0e10cSrcweir RegistryKeyImplWin9x::RegistryKeyImplWin9x(HKEY RootKey, const std::wstring& KeyName) :
63*cdf0e10cSrcweir 	RegistryKeyImpl(RootKey, KeyName)
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir //-----------------------------------------------------
68*cdf0e10cSrcweir /** Create instance and open the specified Registry key
69*cdf0e10cSrcweir */
70*cdf0e10cSrcweir RegistryKeyImplWin9x::RegistryKeyImplWin9x(HKEY RootKey) :
71*cdf0e10cSrcweir 	RegistryKeyImpl(RootKey)
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir //-----------------------------------------------------
76*cdf0e10cSrcweir /** Create an instances of the specified Registry key,
77*cdf0e10cSrcweir 	the key is assumed to be already opened.
78*cdf0e10cSrcweir */
79*cdf0e10cSrcweir RegistryKeyImplWin9x::RegistryKeyImplWin9x(HKEY RootKey, HKEY SubKey, const std::wstring& KeyName, bool Writeable) :
80*cdf0e10cSrcweir 	RegistryKeyImpl(RootKey, SubKey, KeyName, Writeable)
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir //############################################
86*cdf0e10cSrcweir // Queries
87*cdf0e10cSrcweir //############################################
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir //-----------------------------------------------------
91*cdf0e10cSrcweir /** The number of sub values of the key at hand
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	@precond IsOpen = true
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 	@throws
96*cdf0e10cSrcweir */
97*cdf0e10cSrcweir size_t RegistryKeyImplWin9x::GetSubValueCount() const
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir 	assert(IsOpen());
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 	DWORD nSubValues = 0;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	LONG rc = RegQueryInfoKeyA(
104*cdf0e10cSrcweir 		m_hSubKey,
105*cdf0e10cSrcweir 		0, 0, 0, 0, 0, 0, &nSubValues, 0, 0, 0, 0);
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
108*cdf0e10cSrcweir 		throw RegistryIOException(rc);
109*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
110*cdf0e10cSrcweir 		throw RegistryException(rc);
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	return nSubValues;
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir //-----------------------------------------------------
116*cdf0e10cSrcweir /** The number of sub-keys of the key at hand
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	@precond IsOpen = true
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	@throws
121*cdf0e10cSrcweir */
122*cdf0e10cSrcweir size_t RegistryKeyImplWin9x::GetSubKeyCount() const
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir 	assert(IsOpen());
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	DWORD nSubKeys = 0;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	LONG rc = RegQueryInfoKeyA(
129*cdf0e10cSrcweir 		m_hSubKey,
130*cdf0e10cSrcweir 		0, 0, 0, &nSubKeys, 0, 0, 0, 0, 0, 0, 0);
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
133*cdf0e10cSrcweir 		throw RegistryIOException(rc);
134*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
135*cdf0e10cSrcweir 		throw RegistryException(rc);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 	return nSubKeys;
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir //-----------------------------------------------------
141*cdf0e10cSrcweir /**
142*cdf0e10cSrcweir */
143*cdf0e10cSrcweir StringListPtr RegistryKeyImplWin9x::GetSubKeyNames() const
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	assert(IsOpen());
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	char        buff[1024];
148*cdf0e10cSrcweir 	DWORD   buff_size = sizeof(buff);
149*cdf0e10cSrcweir 	FILETIME ftime;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	StringList* key_names = new StringList();
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 	LONG rc = ERROR_SUCCESS;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	for (DWORD i = 0; /* left empty */; i++)
156*cdf0e10cSrcweir 	{
157*cdf0e10cSrcweir 		rc = RegEnumKeyExA(
158*cdf0e10cSrcweir 			m_hSubKey, i, buff, &buff_size,
159*cdf0e10cSrcweir 			0, 0, 0, &ftime);
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 		if (ERROR_SUCCESS != rc &&
162*cdf0e10cSrcweir 			ERROR_MORE_DATA != rc)
163*cdf0e10cSrcweir 			break;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 		buff_size = sizeof(buff);
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 		key_names->push_back(AnsiToUnicodeString(buff));
168*cdf0e10cSrcweir 	}
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
171*cdf0e10cSrcweir 		throw RegistryIOException(rc);
172*cdf0e10cSrcweir 	else if (ERROR_NO_MORE_ITEMS != rc && ERROR_SUCCESS != rc)
173*cdf0e10cSrcweir 		throw RegistryException(rc);
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 	return (StringListPtr) key_names;
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir //-----------------------------------------------------
179*cdf0e10cSrcweir /**
180*cdf0e10cSrcweir */
181*cdf0e10cSrcweir StringListPtr RegistryKeyImplWin9x::GetSubValueNames() const
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir 	assert(IsOpen());
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	char        buff[1024];
186*cdf0e10cSrcweir 	DWORD   buff_size = sizeof(buff);
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 	StringList* value_names = new StringList();
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	LONG rc = ERROR_SUCCESS;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	for (DWORD i = 0; /* left empty */; i++)
193*cdf0e10cSrcweir 	{
194*cdf0e10cSrcweir         rc = RegEnumValueA(
195*cdf0e10cSrcweir 			m_hSubKey, i, buff, &buff_size,
196*cdf0e10cSrcweir 			0, 0, 0, 0);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 		if (ERROR_SUCCESS != rc &&
199*cdf0e10cSrcweir 			ERROR_MORE_DATA != rc)
200*cdf0e10cSrcweir 			break;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 		buff_size = sizeof(buff);
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 		value_names->push_back(AnsiToUnicodeString(buff));
205*cdf0e10cSrcweir 	}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
208*cdf0e10cSrcweir 		throw RegistryIOException(rc);
209*cdf0e10cSrcweir 	else if (ERROR_NO_MORE_ITEMS != rc && ERROR_SUCCESS != rc)
210*cdf0e10cSrcweir 		throw RegistryException(rc);
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	return (StringListPtr) value_names;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir //-----------------------------------------------------
216*cdf0e10cSrcweir /** Get the specified registry value
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	@precond IsOpen = true
219*cdf0e10cSrcweir */
220*cdf0e10cSrcweir RegistryValue RegistryKeyImplWin9x::GetValue(const std::wstring& Name) const
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir 	assert(IsOpen());
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 	DWORD Type;
225*cdf0e10cSrcweir 	char  buff[MAX_TMP_BUFF_SIZE];
226*cdf0e10cSrcweir 	DWORD size = sizeof(buff);
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 	LONG rc = RegQueryValueExA(
229*cdf0e10cSrcweir 		m_hSubKey,
230*cdf0e10cSrcweir 		UnicodeToAnsiString(Name).c_str(),
231*cdf0e10cSrcweir 		0,
232*cdf0e10cSrcweir 		&Type,
233*cdf0e10cSrcweir 		reinterpret_cast<LPBYTE>(buff),
234*cdf0e10cSrcweir 		&size);
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 	if (ERROR_FILE_NOT_FOUND == rc)
237*cdf0e10cSrcweir 		throw RegistryValueNotFoundException(rc);
238*cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
239*cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
240*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
241*cdf0e10cSrcweir 		throw RegistryException(rc);
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 	RegistryValue regval;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 	if (REG_DWORD == Type)
246*cdf0e10cSrcweir     {
247*cdf0e10cSrcweir 		regval = RegistryValue(new RegistryValueImpl(Name, *(reinterpret_cast<int*>(buff))));
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir 	else if (REG_SZ == Type || REG_EXPAND_SZ == Type || REG_MULTI_SZ == Type)
250*cdf0e10cSrcweir     {
251*cdf0e10cSrcweir         if (size > 0)
252*cdf0e10cSrcweir             regval = RegistryValue(new RegistryValueImpl(Name, std::string(reinterpret_cast<char*>(buff))));
253*cdf0e10cSrcweir         else
254*cdf0e10cSrcweir             regval = RegistryValue(new RegistryValueImpl(Name, std::string()));
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir 	else
257*cdf0e10cSrcweir     {
258*cdf0e10cSrcweir 		assert(false);
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 	return regval;
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir //-----------------------------------------------------
265*cdf0e10cSrcweir /** Get the specified registry value, return the given
266*cdf0e10cSrcweir 	default value if value not found
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 	@precond IsOpen = true
269*cdf0e10cSrcweir */
270*cdf0e10cSrcweir RegistryValue RegistryKeyImplWin9x::GetValue(const std::wstring& Name, const RegistryValue& Default) const
271*cdf0e10cSrcweir {
272*cdf0e10cSrcweir 	assert(IsOpen());
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 	DWORD Type;
275*cdf0e10cSrcweir 	char  buff[MAX_TMP_BUFF_SIZE];
276*cdf0e10cSrcweir 	DWORD size = sizeof(buff);
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	LONG rc = RegQueryValueExA(
279*cdf0e10cSrcweir 		m_hSubKey,
280*cdf0e10cSrcweir 		UnicodeToAnsiString(Name).c_str(),
281*cdf0e10cSrcweir 		0,
282*cdf0e10cSrcweir 		&Type,
283*cdf0e10cSrcweir 		reinterpret_cast<LPBYTE>(buff),
284*cdf0e10cSrcweir 		&size);
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 	if (ERROR_FILE_NOT_FOUND == rc)
287*cdf0e10cSrcweir 	{
288*cdf0e10cSrcweir         #if !defined(__MINGW32__) && (_MSC_VER < 1300)
289*cdf0e10cSrcweir 		return Default;
290*cdf0e10cSrcweir 		#else
291*cdf0e10cSrcweir 		RegistryValue regval_ptr;
292*cdf0e10cSrcweir 		regval_ptr = RegistryValue(new RegistryValueImpl(*Default));
293*cdf0e10cSrcweir 		return regval_ptr;
294*cdf0e10cSrcweir 		#endif
295*cdf0e10cSrcweir 		}
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir 	if (ERROR_ACCESS_DENIED == rc)
298*cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
299*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
300*cdf0e10cSrcweir 		throw RegistryException(rc);
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 	RegistryValue regval;
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir 	if (REG_DWORD == Type)
305*cdf0e10cSrcweir 		regval = RegistryValue(new RegistryValueImpl(Name, *reinterpret_cast<int*>(buff)));
306*cdf0e10cSrcweir 	else if (REG_SZ == Type || REG_EXPAND_SZ == Type || REG_MULTI_SZ == Type)
307*cdf0e10cSrcweir 		regval = RegistryValue(new RegistryValueImpl(Name, std::string(reinterpret_cast<char*>(buff))));
308*cdf0e10cSrcweir 	else
309*cdf0e10cSrcweir 		assert(false);
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir 	return regval;
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir //############################################
316*cdf0e10cSrcweir // Commands
317*cdf0e10cSrcweir //############################################
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir //-----------------------------------------------------
321*cdf0e10cSrcweir /** Open the registry key, has no effect if
322*cdf0e10cSrcweir 	the key is already open
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir 	@precond IsOpen = false
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 	@throws RegistryKeyNotFoundException
327*cdf0e10cSrcweir 			RegistryWriteAccessDenyException
328*cdf0e10cSrcweir 			RegistryAccessDenyException
329*cdf0e10cSrcweir */
330*cdf0e10cSrcweir void RegistryKeyImplWin9x::Open(bool Writeable)
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir 	assert(!IsOpen());
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir 	REGSAM regsam = KEY_READ;
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 	if (Writeable)
337*cdf0e10cSrcweir 		regsam |= KEY_WRITE;
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 	LONG rc = RegOpenKeyExA(
340*cdf0e10cSrcweir 		m_hRootKey,
341*cdf0e10cSrcweir 		UnicodeToAnsiString(m_KeyName).c_str(),
342*cdf0e10cSrcweir 		0,
343*cdf0e10cSrcweir 		regsam,
344*cdf0e10cSrcweir 		&m_hSubKey);
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir 	if (ERROR_FILE_NOT_FOUND == rc)
347*cdf0e10cSrcweir 		throw RegistryKeyNotFoundException(rc);
348*cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
349*cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
350*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
351*cdf0e10cSrcweir 		throw RegistryException(rc);
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir 	m_IsWriteable = Writeable;
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir 	assert(IsOpen());
356*cdf0e10cSrcweir }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir //-----------------------------------------------------
359*cdf0e10cSrcweir /** Open the specified sub-key of the registry key
360*cdf0e10cSrcweir 	at hand
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 	@precond IsOpen = true
363*cdf0e10cSrcweir 			 HasSubKey(Name) = true
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir 	@throws RegistryIOException
366*cdf0e10cSrcweir 			RegistryKeyNotFoundException
367*cdf0e10cSrcweir 			RegistryAccessDeniedException
368*cdf0e10cSrcweir */
369*cdf0e10cSrcweir RegistryKey RegistryKeyImplWin9x::OpenSubKey(const std::wstring& Name, bool Writeable)
370*cdf0e10cSrcweir {
371*cdf0e10cSrcweir 	RegistryKey regkey(new RegistryKeyImplWin9x(m_hSubKey, Name));
372*cdf0e10cSrcweir 	regkey->Open(Writeable);
373*cdf0e10cSrcweir 	return regkey;
374*cdf0e10cSrcweir }
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir //-----------------------------------------------------
377*cdf0e10cSrcweir /** Creates a new sub-key below the key at hand
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir 	@precond IsOpen = true
380*cdf0e10cSrcweir 			 IsWriteable = true
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir 	@throws  RegistryIOException
383*cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
384*cdf0e10cSrcweir */
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir RegistryKey RegistryKeyImplWin9x::CreateSubKey(const std::wstring& Name)
387*cdf0e10cSrcweir {
388*cdf0e10cSrcweir 	assert(IsOpen());
389*cdf0e10cSrcweir 	assert(IsWriteable());
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir 	HKEY hRoot = IsRootKey() ? m_hRootKey : m_hSubKey;
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir 	HKEY hKey;
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir 	LONG rc = RegCreateKeyExA(
396*cdf0e10cSrcweir 		hRoot,
397*cdf0e10cSrcweir 		UnicodeToAnsiString(Name).c_str(),
398*cdf0e10cSrcweir 		0,
399*cdf0e10cSrcweir 		0,
400*cdf0e10cSrcweir 		REG_OPTION_NON_VOLATILE,
401*cdf0e10cSrcweir 		KEY_READ | KEY_WRITE,
402*cdf0e10cSrcweir 		0,
403*cdf0e10cSrcweir 		&hKey,
404*cdf0e10cSrcweir 		0);
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
407*cdf0e10cSrcweir 		throw RegistryIOException(rc);
408*cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
409*cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
410*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
411*cdf0e10cSrcweir 		throw RegistryException(rc);
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir 	return RegistryKey(new RegistryKeyImplWin9x(hRoot, hKey, Name));
414*cdf0e10cSrcweir }
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir //-----------------------------------------------------
417*cdf0e10cSrcweir /** Deletes a sub-key below the key at hand, the
418*cdf0e10cSrcweir 	key must not have sub-keys
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir 	@precond IsOpen = true
421*cdf0e10cSrcweir 			 IsWriteable = true
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir 	@throws  RegistryIOException
424*cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
425*cdf0e10cSrcweir */
426*cdf0e10cSrcweir void RegistryKeyImplWin9x::DeleteSubKey(const std::wstring& Name)
427*cdf0e10cSrcweir {
428*cdf0e10cSrcweir 	assert(IsOpen());
429*cdf0e10cSrcweir 	assert(IsWriteable());
430*cdf0e10cSrcweir 	assert(HasSubKey(Name));
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir 	RegistryKey SubKey = OpenSubKey(Name);
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir 	size_t nSubKeyCount = SubKey->GetSubKeyCount();
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir 	assert(0 == nSubKeyCount);
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir 	if (nSubKeyCount)
439*cdf0e10cSrcweir 		throw RegistryInvalidOperationException(ERROR_NOT_SUPPORTED);
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir 	LONG rc = RegDeleteKeyA(m_hSubKey, UnicodeToAnsiString(Name).c_str());
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
444*cdf0e10cSrcweir 		throw RegistryIOException(rc);
445*cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
446*cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
447*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
448*cdf0e10cSrcweir 		throw RegistryException(rc);
449*cdf0e10cSrcweir }
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir //-----------------------------------------------------
452*cdf0e10cSrcweir /** Deletes a sub-key below the key at hand with all
453*cdf0e10cSrcweir 	its sub-keys
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir 	@precond IsOpen = true
456*cdf0e10cSrcweir 			 IsWriteable = true;
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir 	@throws  RegistryIOException
459*cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
460*cdf0e10cSrcweir */
461*cdf0e10cSrcweir void RegistryKeyImplWin9x::DeleteSubKeyTree(const std::wstring& Name)
462*cdf0e10cSrcweir {
463*cdf0e10cSrcweir 	LONG rc = RegDeleteKeyA(m_hSubKey, UnicodeToAnsiString(Name).c_str());
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
466*cdf0e10cSrcweir 		throw RegistryIOException(rc);
467*cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
468*cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
469*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
470*cdf0e10cSrcweir 		throw RegistryException(rc);
471*cdf0e10cSrcweir }
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir //-----------------------------------------------------
474*cdf0e10cSrcweir /** Delete the specified value
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir 		@precond IsOpen = true
477*cdf0e10cSrcweir 				 IsWriteable = true
478*cdf0e10cSrcweir 				 HasValue(Name) = true
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir 		@throws	RegistryIOException
481*cdf0e10cSrcweir 				RegistryWriteAccessDeniedException
482*cdf0e10cSrcweir 				RegistryValueNotFoundException
483*cdf0e10cSrcweir */
484*cdf0e10cSrcweir void RegistryKeyImplWin9x::DeleteValue(const std::wstring& Name)
485*cdf0e10cSrcweir {
486*cdf0e10cSrcweir 	assert(IsOpen());
487*cdf0e10cSrcweir 	assert(HasValue(Name));
488*cdf0e10cSrcweir 	assert(IsWriteable());
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir 	LONG rc = RegDeleteValueA(
491*cdf0e10cSrcweir 		m_hSubKey,
492*cdf0e10cSrcweir 		UnicodeToAnsiString(Name).c_str());
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
495*cdf0e10cSrcweir 		throw RegistryIOException(rc);
496*cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
497*cdf0e10cSrcweir 		throw RegistryNoWriteAccessException(rc);
498*cdf0e10cSrcweir 	else if (ERROR_FILE_NOT_FOUND == rc)
499*cdf0e10cSrcweir 		throw RegistryValueNotFoundException(rc);
500*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
501*cdf0e10cSrcweir 		throw RegistryException(rc);
502*cdf0e10cSrcweir }
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir //-----------------------------------------------------
505*cdf0e10cSrcweir /** Set the specified registry value
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir 	@precond IsOpen = true
508*cdf0e10cSrcweir 			 IsWriteable = true
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir 	@throws  RegistryIOException
511*cdf0e10cSrcweir 			 RegistryWriteAccessDenyException
512*cdf0e10cSrcweir */
513*cdf0e10cSrcweir void RegistryKeyImplWin9x::SetValue(const RegistryValue& Value)
514*cdf0e10cSrcweir {
515*cdf0e10cSrcweir 	assert(IsOpen());
516*cdf0e10cSrcweir 	assert(IsWriteable());
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir 	LONG rc = ERROR_SUCCESS;
519*cdf0e10cSrcweir 
520*cdf0e10cSrcweir 	if (REG_SZ == Value->GetType())
521*cdf0e10cSrcweir 	{
522*cdf0e10cSrcweir 		std::string AnsiStr = Value->GetDataAsAnsiString();
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir 		rc = RegSetValueExA(
525*cdf0e10cSrcweir 			m_hSubKey,
526*cdf0e10cSrcweir 			UnicodeToAnsiString(Value->GetName()).c_str(),
527*cdf0e10cSrcweir 			0,
528*cdf0e10cSrcweir 			Value->GetType(),
529*cdf0e10cSrcweir 			reinterpret_cast<const unsigned char*>(AnsiStr.c_str()),
530*cdf0e10cSrcweir 			static_cast<DWORD>((AnsiStr.length() + 1)));
531*cdf0e10cSrcweir 	}
532*cdf0e10cSrcweir 	else
533*cdf0e10cSrcweir 	{
534*cdf0e10cSrcweir 		rc = RegSetValueExA(
535*cdf0e10cSrcweir 			m_hSubKey,
536*cdf0e10cSrcweir 			UnicodeToAnsiString(Value->GetName()).c_str(),
537*cdf0e10cSrcweir 			0,
538*cdf0e10cSrcweir 			Value->GetType(),
539*cdf0e10cSrcweir 			reinterpret_cast<const unsigned char*>(Value->GetDataBuffer()),
540*cdf0e10cSrcweir 			static_cast<DWORD>(Value->GetDataSize()));
541*cdf0e10cSrcweir 	}
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir 	if (ERROR_INVALID_HANDLE == rc)
544*cdf0e10cSrcweir 		throw RegistryIOException(rc);
545*cdf0e10cSrcweir 	else if (ERROR_ACCESS_DENIED == rc)
546*cdf0e10cSrcweir 		throw RegistryAccessDeniedException(rc);
547*cdf0e10cSrcweir 	else if (ERROR_SUCCESS != rc)
548*cdf0e10cSrcweir 		throw RegistryException(rc);
549*cdf0e10cSrcweir }
550*cdf0e10cSrcweir 
551