blob: 79cfd0072e9eb8da732af9b846806298c186d676 [file] [log] [blame]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001/*
2 * Registry management
3 *
4 * Copyright (C) 1999 Alexandre Julliard
5 *
6 * Based on misc/registry.c code
7 * Copyright (C) 1996 Marcus Meissner
8 * Copyright (C) 1998 Matthew Becker
9 * Copyright (C) 1999 Sylvain St-Germain
10 *
11 * This file is concerned about handle management and interaction with the Wine server.
12 * Registry file I/O is in misc/registry.c.
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000013 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000027 */
28
29#include <stdlib.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000030#include <stdarg.h>
Alexandre Julliard383da682000-02-10 22:15:21 +000031#include <stdio.h>
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000032
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000033#include "windef.h"
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000034#include "winbase.h"
35#include "winreg.h"
36#include "winerror.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000037#include "ntstatus.h"
Alexandre Julliardc7e7df82000-08-14 14:41:19 +000038#include "wine/unicode.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000039#include "wine/server.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000040#include "wine/debug.h"
Matthew Davison6f2a0712003-01-20 23:23:12 +000041#include "winternl.h"
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000042
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000043WINE_DEFAULT_DEBUG_CHANNEL(reg);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000044
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +000045/* allowed bits for access mask */
46#define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
47
48#define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
49#define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
50#define NB_SPECIAL_ROOT_KEYS ((UINT)HKEY_SPECIAL_ROOT_LAST - (UINT)HKEY_SPECIAL_ROOT_FIRST + 1)
51
52static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
53
54static const WCHAR name_CLASSES_ROOT[] =
55 {'M','a','c','h','i','n','e','\\',
56 'S','o','f','t','w','a','r','e','\\',
57 'C','l','a','s','s','e','s',0};
58static const WCHAR name_LOCAL_MACHINE[] =
59 {'M','a','c','h','i','n','e',0};
60static const WCHAR name_USERS[] =
61 {'U','s','e','r',0};
62static const WCHAR name_PERFORMANCE_DATA[] =
63 {'P','e','r','f','D','a','t','a',0};
64static const WCHAR name_CURRENT_CONFIG[] =
65 {'M','a','c','h','i','n','e','\\',
66 'S','y','s','t','e','m','\\',
67 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
68 'H','a','r','d','w','a','r','e','P','r','o','f','i','l','e','s','\\',
69 'C','u','r','r','e','n','t',0};
70static const WCHAR name_DYN_DATA[] =
71 {'D','y','n','D','a','t','a',0};
72
73#define DECL_STR(key) { sizeof(name_##key)-sizeof(WCHAR), sizeof(name_##key), (LPWSTR)name_##key }
74static UNICODE_STRING root_key_names[NB_SPECIAL_ROOT_KEYS] =
75{
76 DECL_STR(CLASSES_ROOT),
77 { 0, 0, NULL }, /* HKEY_CURRENT_USER is determined dynamically */
78 DECL_STR(LOCAL_MACHINE),
79 DECL_STR(USERS),
80 DECL_STR(PERFORMANCE_DATA),
81 DECL_STR(CURRENT_CONFIG),
82 DECL_STR(DYN_DATA)
83};
84#undef DECL_STR
85
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000086
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000087/* check if value type needs string conversion (Ansi<->Unicode) */
Michael Cardenas7dbce652002-03-21 01:24:52 +000088inline static int is_string( DWORD type )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000089{
90 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
91}
92
Michael Cardenas7dbce652002-03-21 01:24:52 +000093/* check if current version is NT or Win95 */
94inline static int is_version_nt(void)
95{
96 return !(GetVersion() & 0x80000000);
97}
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +000098
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +000099/* create one of the HKEY_* special root keys */
100static HKEY create_special_root_hkey( HKEY hkey, DWORD access )
101{
102 HKEY ret = 0;
103 int idx = (UINT)hkey - (UINT)HKEY_SPECIAL_ROOT_FIRST;
104
105 if (hkey == HKEY_CURRENT_USER)
106 {
107 if (RtlOpenCurrentUser( access, &hkey )) return 0;
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000108 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000109 }
110 else
111 {
112 OBJECT_ATTRIBUTES attr;
113
114 attr.Length = sizeof(attr);
115 attr.RootDirectory = 0;
116 attr.ObjectName = &root_key_names[idx];
117 attr.Attributes = 0;
118 attr.SecurityDescriptor = NULL;
119 attr.SecurityQualityOfService = NULL;
120 if (NtCreateKey( &hkey, access, &attr, 0, NULL, 0, NULL )) return 0;
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000121 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000122 }
123
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000124 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000125 ret = hkey;
126 else
127 NtClose( hkey ); /* somebody beat us to it */
128 return ret;
129}
130
131/* map the hkey from special root to normal key if necessary */
132inline static HKEY get_special_root_hkey( HKEY hkey )
133{
134 HKEY ret = hkey;
135
136 if ((hkey >= HKEY_SPECIAL_ROOT_FIRST) && (hkey <= HKEY_SPECIAL_ROOT_LAST))
137 {
138 if (!(ret = special_root_keys[(UINT)hkey - (UINT)HKEY_SPECIAL_ROOT_FIRST]))
139 ret = create_special_root_hkey( hkey, KEY_ALL_ACCESS );
140 }
141 return ret;
142}
143
Juergen Schmieda44e0b02002-05-09 19:39:10 +0000144
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000145/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000146 * RegCreateKeyExW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000147 *
Jon Griffiths50ce0842004-04-23 21:32:34 +0000148 * See RegCreateKeyExA.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000149 */
Daniel Marmierc33e4c42003-10-21 23:44:37 +0000150DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPCWSTR class,
Vincent Béron9a624912002-05-31 23:06:46 +0000151 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
Alexandre Julliard5cf56a32002-10-03 19:46:27 +0000152 PHKEY retkey, LPDWORD dispos )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000153{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000154 OBJECT_ATTRIBUTES attr;
155 UNICODE_STRING nameW, classW;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000156
157 if (reserved) return ERROR_INVALID_PARAMETER;
Juergen Schmieda44e0b02002-05-09 19:39:10 +0000158 if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000159 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000160
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000161 attr.Length = sizeof(attr);
162 attr.RootDirectory = hkey;
163 attr.ObjectName = &nameW;
164 attr.Attributes = 0;
165 attr.SecurityDescriptor = NULL;
166 attr.SecurityQualityOfService = NULL;
167 RtlInitUnicodeString( &nameW, name );
168 RtlInitUnicodeString( &classW, class );
169
170 return RtlNtStatusToDosError( NtCreateKey( retkey, access, &attr, 0,
171 &classW, options, dispos ) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000172}
173
174
175/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000176 * RegCreateKeyExA [ADVAPI32.@]
Juergen Schmieda44e0b02002-05-09 19:39:10 +0000177 *
Jon Griffiths50ce0842004-04-23 21:32:34 +0000178 * Open a registry key, creating it if it doesn't exist.
179 *
180 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +0000181 * hkey [I] Handle of the parent registry key
182 * name [I] Name of the new key to open or create
183 * reserved [I] Reserved, pass 0
184 * class [I] The object type of the new key
185 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
186 * access [I] Access level desired
187 * sa [I] Security attributes for the key
188 * retkey [O] Destination for the resulting handle
189 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
Jon Griffiths50ce0842004-04-23 21:32:34 +0000190 *
191 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +0000192 * Success: ERROR_SUCCESS.
193 * Failure: A standard Win32 error code. retkey remains untouched.
Jon Griffiths50ce0842004-04-23 21:32:34 +0000194 *
195 * FIXME
James Hawkins6a358c42004-07-13 23:33:14 +0000196 * MAXIMUM_ALLOWED in access mask not supported by server
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000197 */
Daniel Marmierc33e4c42003-10-21 23:44:37 +0000198DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPCSTR class,
Vincent Béron9a624912002-05-31 23:06:46 +0000199 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
Alexandre Julliard5cf56a32002-10-03 19:46:27 +0000200 PHKEY retkey, LPDWORD dispos )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000201{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000202 OBJECT_ATTRIBUTES attr;
Alexandre Julliardab5e9752001-03-23 19:12:01 +0000203 UNICODE_STRING classW;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000204 ANSI_STRING nameA, classA;
205 NTSTATUS status;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000206
207 if (reserved) return ERROR_INVALID_PARAMETER;
Michael Cardenas7dbce652002-03-21 01:24:52 +0000208 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
Juergen Schmieda44e0b02002-05-09 19:39:10 +0000209 else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000210 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000211
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000212 attr.Length = sizeof(attr);
213 attr.RootDirectory = hkey;
Alexandre Julliardab5e9752001-03-23 19:12:01 +0000214 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000215 attr.Attributes = 0;
216 attr.SecurityDescriptor = NULL;
217 attr.SecurityQualityOfService = NULL;
218 RtlInitAnsiString( &nameA, name );
219 RtlInitAnsiString( &classA, class );
220
Alexandre Julliardab5e9752001-03-23 19:12:01 +0000221 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
222 &nameA, FALSE )))
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000223 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000224 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
225 {
226 status = NtCreateKey( retkey, access, &attr, 0, &classW, options, dispos );
227 RtlFreeUnicodeString( &classW );
228 }
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000229 }
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000230 return RtlNtStatusToDosError( status );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000231}
232
233
234/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000235 * RegCreateKeyW [ADVAPI32.@]
236 *
237 * Creates the specified reg key.
238 *
239 * PARAMS
240 * hKey [I] Handle to an open key.
241 * lpSubKey [I] Name of a key that will be opened or created.
242 * phkResult [O] Receives a handle to the opened or created key.
243 *
244 * RETURNS
245 * Success: ERROR_SUCCESS
246 * Failure: nonzero error code defined in Winerror.h
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000247 */
James Hawkins6a358c42004-07-13 23:33:14 +0000248DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR lpSubKey, PHKEY phkResult )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000249{
250 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
251 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
James Hawkins6a358c42004-07-13 23:33:14 +0000252 return RegCreateKeyExW( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
253 KEY_ALL_ACCESS, NULL, phkResult, NULL );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000254}
255
256
257/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000258 * RegCreateKeyA [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000259 *
James Hawkins6a358c42004-07-13 23:33:14 +0000260 * see RegCreateKeyW
261 */
262DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR lpSubKey, PHKEY phkResult )
263{
264 return RegCreateKeyExA( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
265 KEY_ALL_ACCESS, NULL, phkResult, NULL );
266}
267
268
269
270/******************************************************************************
271 * RegOpenKeyExW [ADVAPI32.@]
272 *
Jon Griffiths50ce0842004-04-23 21:32:34 +0000273 * See RegOpenKeyExA.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000274 */
Alexandre Julliard5cf56a32002-10-03 19:46:27 +0000275DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000276{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000277 OBJECT_ATTRIBUTES attr;
278 UNICODE_STRING nameW;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000279
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000280 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
281
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000282 attr.Length = sizeof(attr);
283 attr.RootDirectory = hkey;
284 attr.ObjectName = &nameW;
285 attr.Attributes = 0;
286 attr.SecurityDescriptor = NULL;
287 attr.SecurityQualityOfService = NULL;
288 RtlInitUnicodeString( &nameW, name );
289 return RtlNtStatusToDosError( NtOpenKey( retkey, access, &attr ) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000290}
291
292
293/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000294 * RegOpenKeyExA [ADVAPI32.@]
Jon Griffiths50ce0842004-04-23 21:32:34 +0000295 *
296 * Open a registry key.
297 *
298 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +0000299 * hkey [I] Handle of open key
300 * name [I] Name of subkey to open
301 * reserved [I] Reserved - must be zero
302 * access [I] Security access mask
303 * retkey [O] Handle to open key
Jon Griffiths50ce0842004-04-23 21:32:34 +0000304 *
305 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +0000306 * Success: ERROR_SUCCESS
307 * Failure: A standard Win32 error code. retkey is set to 0.
Jon Griffiths50ce0842004-04-23 21:32:34 +0000308 *
309 * NOTES
James Hawkins6a358c42004-07-13 23:33:14 +0000310 * Unlike RegCreateKeyExA(), this function will not create the key if it
311 * does not exist.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000312 */
Alexandre Julliard5cf56a32002-10-03 19:46:27 +0000313DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000314{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000315 OBJECT_ATTRIBUTES attr;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000316 STRING nameA;
317 NTSTATUS status;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000318
Michael Cardenas7dbce652002-03-21 01:24:52 +0000319 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
320
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000321 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
322
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000323 attr.Length = sizeof(attr);
324 attr.RootDirectory = hkey;
Alexandre Julliardab5e9752001-03-23 19:12:01 +0000325 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000326 attr.Attributes = 0;
327 attr.SecurityDescriptor = NULL;
328 attr.SecurityQualityOfService = NULL;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000329
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000330 RtlInitAnsiString( &nameA, name );
Alexandre Julliardab5e9752001-03-23 19:12:01 +0000331 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
332 &nameA, FALSE )))
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000333 {
334 status = NtOpenKey( retkey, access, &attr );
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000335 }
336 return RtlNtStatusToDosError( status );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000337}
338
339
340/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000341 * RegOpenKeyW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000342 *
Jon Griffiths50ce0842004-04-23 21:32:34 +0000343 * See RegOpenKeyA.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000344 */
Alexandre Julliard5cf56a32002-10-03 19:46:27 +0000345DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000346{
347 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
348}
349
350
351/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000352 * RegOpenKeyA [ADVAPI32.@]
Jon Griffiths50ce0842004-04-23 21:32:34 +0000353 *
354 * Open a registry key.
355 *
356 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +0000357 * hkey [I] Handle of parent key to open the new key under
358 * name [I] Name of the key under hkey to open
359 * retkey [O] Destination for the resulting Handle
Jon Griffiths50ce0842004-04-23 21:32:34 +0000360 *
361 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +0000362 * Success: ERROR_SUCCESS
363 * Failure: A standard Win32 error code. retkey is set to 0.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000364 */
Alexandre Julliard5cf56a32002-10-03 19:46:27 +0000365DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000366{
367 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
368}
369
370
David Elliott44f84b52000-10-29 01:24:54 +0000371/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000372 * RegOpenCurrentUser [ADVAPI32.@]
373 *
David Elliott44f84b52000-10-29 01:24:54 +0000374 * FIXME: This function is supposed to retrieve a handle to the
375 * HKEY_CURRENT_USER for the user the current thread is impersonating.
376 * Since Wine does not currently allow threads to impersonate other users,
377 * this stub should work fine.
378 */
379DWORD WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
380{
381 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
382}
383
384
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000385
386/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000387 * RegEnumKeyExW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000388 *
389 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +0000390 * hkey [I] Handle to key to enumerate
391 * index [I] Index of subkey to enumerate
392 * name [O] Buffer for subkey name
393 * name_len [O] Size of subkey buffer
394 * reserved [I] Reserved
395 * class [O] Buffer for class string
396 * class_len [O] Size of class buffer
397 * ft [O] Time key last written to
398 *
399 * RETURNS
400 * Success: ERROR_SUCCESS
401 * Failure: System error code. If there are no more subkeys available, the
402 * function returns ERROR_NO_MORE_ITEMS.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000403 */
404DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
405 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
406{
Alexandre Julliard454355e2000-10-02 03:46:58 +0000407 NTSTATUS status;
408 char buffer[256], *buf_ptr = buffer;
409 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
410 DWORD total_size;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000411
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000412 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000413 name_len ? *name_len : -1, reserved, class, class_len, ft );
414
415 if (reserved) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000416 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Rein Klazes0bb6fdd2002-07-05 01:21:13 +0000417
Alexandre Julliard454355e2000-10-02 03:46:58 +0000418 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
419 buffer, sizeof(buffer), &total_size );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000420
Alexandre Julliard454355e2000-10-02 03:46:58 +0000421 while (status == STATUS_BUFFER_OVERFLOW)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000422 {
Alexandre Julliard454355e2000-10-02 03:46:58 +0000423 /* retry with a dynamically allocated buffer */
424 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
425 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
426 return ERROR_NOT_ENOUGH_MEMORY;
427 info = (KEY_NODE_INFORMATION *)buf_ptr;
428 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
429 buf_ptr, total_size, &total_size );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000430 }
Alexandre Julliard454355e2000-10-02 03:46:58 +0000431
432 if (!status)
433 {
434 DWORD len = info->NameLength / sizeof(WCHAR);
435 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
436
Dmitry Timoshkov6b6596a2001-11-23 18:44:43 +0000437 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000438
Aric Stewart20a4cc32002-02-05 19:45:55 +0000439 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
Alexandre Julliard454355e2000-10-02 03:46:58 +0000440 status = STATUS_BUFFER_OVERFLOW;
441 else
442 {
443 *name_len = len;
444 memcpy( name, info->Name, info->NameLength );
445 name[len] = 0;
446 if (class_len)
447 {
448 *class_len = cls_len;
449 if (class)
450 {
451 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
452 class[cls_len] = 0;
453 }
454 }
455 }
456 }
457
458 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
459 return RtlNtStatusToDosError( status );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000460}
461
462
463/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000464 * RegEnumKeyExA [ADVAPI32.@]
465 *
466 * see RegEnumKeyExW
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000467 */
468DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
469 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
470{
Alexandre Julliard454355e2000-10-02 03:46:58 +0000471 NTSTATUS status;
472 char buffer[256], *buf_ptr = buffer;
473 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
474 DWORD total_size;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000475
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000476 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000477 name_len ? *name_len : -1, reserved, class, class_len, ft );
478
479 if (reserved) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000480 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Rein Klazes0bb6fdd2002-07-05 01:21:13 +0000481
Alexandre Julliard454355e2000-10-02 03:46:58 +0000482 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
483 buffer, sizeof(buffer), &total_size );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000484
Alexandre Julliard454355e2000-10-02 03:46:58 +0000485 while (status == STATUS_BUFFER_OVERFLOW)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000486 {
Alexandre Julliard454355e2000-10-02 03:46:58 +0000487 /* retry with a dynamically allocated buffer */
488 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
489 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
490 return ERROR_NOT_ENOUGH_MEMORY;
491 info = (KEY_NODE_INFORMATION *)buf_ptr;
492 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
493 buf_ptr, total_size, &total_size );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000494 }
Alexandre Julliard454355e2000-10-02 03:46:58 +0000495
496 if (!status)
497 {
Alexandre Julliard60fd03d2001-12-05 22:18:48 +0000498 DWORD len, cls_len;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000499
Alexandre Julliard60fd03d2001-12-05 22:18:48 +0000500 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
501 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
502 info->ClassLength );
Dmitry Timoshkov6b6596a2001-11-23 18:44:43 +0000503 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000504
Aric Stewart20a4cc32002-02-05 19:45:55 +0000505 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
Alexandre Julliard454355e2000-10-02 03:46:58 +0000506 status = STATUS_BUFFER_OVERFLOW;
507 else
508 {
509 *name_len = len;
Alexandre Julliard60fd03d2001-12-05 22:18:48 +0000510 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
Alexandre Julliard454355e2000-10-02 03:46:58 +0000511 name[len] = 0;
512 if (class_len)
513 {
514 *class_len = cls_len;
515 if (class)
516 {
Alexandre Julliard60fd03d2001-12-05 22:18:48 +0000517 RtlUnicodeToMultiByteN( class, cls_len, NULL,
518 (WCHAR *)(buf_ptr + info->ClassOffset),
519 info->ClassLength );
Alexandre Julliard454355e2000-10-02 03:46:58 +0000520 class[cls_len] = 0;
521 }
522 }
523 }
524 }
525
526 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
527 return RtlNtStatusToDosError( status );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000528}
529
530
531/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000532 * RegEnumKeyW [ADVAPI32.@]
533 *
534 * Enumerates subkyes of the specified open reg key.
535 *
536 * PARAMS
537 * hKey [I] Handle to an open key.
538 * dwIndex [I] Index of the subkey of hKey to retrieve.
539 * lpName [O] Name of the subkey.
540 * cchName [I] Size of lpName in TCHARS.
541 *
542 * RETURNS
543 * Success: ERROR_SUCCESS
544 * Failure: system error code. If there are no more subkeys available, the
545 * function returns ERROR_NO_MORE_ITEMS.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000546 */
547DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
548{
549 return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
550}
551
552
553/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000554 * RegEnumKeyA [ADVAPI32.@]
555 *
556 * see RegEnumKeyW
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000557 */
558DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
559{
560 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
561}
562
563
564/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000565 * RegQueryInfoKeyW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000566 *
567 * PARAMS
568 * hkey [I] Handle to key to query
569 * class [O] Buffer for class string
570 * class_len [O] Size of class string buffer
571 * reserved [I] Reserved
572 * subkeys [O] Buffer for number of subkeys
573 * max_subkey [O] Buffer for longest subkey name length
574 * max_class [O] Buffer for longest class string length
575 * values [O] Buffer for number of value entries
576 * max_value [O] Buffer for longest value name length
577 * max_data [O] Buffer for longest value data length
578 * security [O] Buffer for security descriptor length
579 * modif [O] Modification time
580 *
Vincent Béron9a624912002-05-31 23:06:46 +0000581 * - win95 allows class to be valid and class_len to be NULL
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000582 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
Vincent Béron9a624912002-05-31 23:06:46 +0000583 * - both allow class to be NULL and class_len to be NULL
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000584 * (it's hard to test validity, so test !NULL instead)
585 */
586DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
587 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
588 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
589 LPDWORD security, FILETIME *modif )
590{
Alexandre Julliard454355e2000-10-02 03:46:58 +0000591 NTSTATUS status;
592 char buffer[256], *buf_ptr = buffer;
593 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
594 DWORD total_size;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000595
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000596 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000597 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
598
Michael Cardenas7dbce652002-03-21 01:24:52 +0000599 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000600 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000601
Alexandre Julliard454355e2000-10-02 03:46:58 +0000602 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
Alexandre Julliardc6615832001-02-14 21:45:52 +0000603 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000604
605 if (class)
606 {
Alexandre Julliard454355e2000-10-02 03:46:58 +0000607 /* retry with a dynamically allocated buffer */
608 while (status == STATUS_BUFFER_OVERFLOW)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000609 {
Alexandre Julliard454355e2000-10-02 03:46:58 +0000610 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
611 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
612 return ERROR_NOT_ENOUGH_MEMORY;
613 info = (KEY_FULL_INFORMATION *)buf_ptr;
614 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000615 }
Alexandre Julliard454355e2000-10-02 03:46:58 +0000616
Alexandre Julliardc6615832001-02-14 21:45:52 +0000617 if (status) goto done;
618
619 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
Alexandre Julliard454355e2000-10-02 03:46:58 +0000620 {
Alexandre Julliardc6615832001-02-14 21:45:52 +0000621 status = STATUS_BUFFER_OVERFLOW;
622 }
623 else
624 {
625 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
626 class[info->ClassLength/sizeof(WCHAR)] = 0;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000627 }
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000628 }
Alexandre Julliardc6615832001-02-14 21:45:52 +0000629 else status = STATUS_SUCCESS;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000630
Alexandre Julliardc6615832001-02-14 21:45:52 +0000631 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
632 if (subkeys) *subkeys = info->SubKeys;
633 if (max_subkey) *max_subkey = info->MaxNameLen;
634 if (max_class) *max_class = info->MaxClassLen;
635 if (values) *values = info->Values;
636 if (max_value) *max_value = info->MaxValueNameLen;
637 if (max_data) *max_data = info->MaxValueDataLen;
Dmitry Timoshkov6b6596a2001-11-23 18:44:43 +0000638 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000639
Alexandre Julliardc6615832001-02-14 21:45:52 +0000640 done:
Alexandre Julliard454355e2000-10-02 03:46:58 +0000641 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
642 return RtlNtStatusToDosError( status );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000643}
644
645
646/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000647 * RegQueryMultipleValuesA [ADVAPI32.@]
648 *
649 * Retrieves the type and data for a list of value names associated with a key.
650 *
651 * PARAMS
652 * hKey [I] Handle to an open key.
653 * val_list [O] Array of VALENT structures that describes the entries.
654 * num_vals [I] Number of elements in val_list.
655 * lpValueBuf [O] Pointer to a buffer that receives the data for each value.
656 * ldwTotsize [I/O] Size of lpValueBuf.
657 *
658 * RETURNS
659 * Success: ERROR_SUCCESS. ldwTotsize contains num bytes copied.
660 * Failure: nonzero error code from Winerror.h ldwTotsize contains num needed
661 * bytes.
Dominik Strasser751c4542002-07-20 20:02:55 +0000662 */
663DWORD WINAPI RegQueryMultipleValuesA(HKEY hkey, PVALENTA val_list, DWORD num_vals,
664 LPSTR lpValueBuf, LPDWORD ldwTotsize)
665{
666 int i;
667 DWORD maxBytes = *ldwTotsize;
668 HRESULT status;
669 LPSTR bufptr = lpValueBuf;
670 *ldwTotsize = 0;
671
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000672 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
Dominik Strasser751c4542002-07-20 20:02:55 +0000673
674 for(i=0; i < num_vals; ++i)
675 {
676
677 val_list[i].ve_valuelen=0;
678 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
679 if(status != ERROR_SUCCESS)
680 {
681 return status;
682 }
683
684 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
685 {
686 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
687 bufptr, &val_list[i].ve_valuelen);
688 if(status != ERROR_SUCCESS)
689 {
690 return status;
691 }
692
693 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
694
695 bufptr += val_list[i].ve_valuelen;
696 }
697
698 *ldwTotsize += val_list[i].ve_valuelen;
699 }
700 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
701}
702
703
704/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000705 * RegQueryMultipleValuesW [ADVAPI32.@]
706 *
707 * see RegQueryMultipleValuesA
Dominik Strasser751c4542002-07-20 20:02:55 +0000708 */
709DWORD WINAPI RegQueryMultipleValuesW(HKEY hkey, PVALENTW val_list, DWORD num_vals,
710 LPWSTR lpValueBuf, LPDWORD ldwTotsize)
711{
712 int i;
713 DWORD maxBytes = *ldwTotsize;
714 HRESULT status;
715 LPSTR bufptr = (LPSTR)lpValueBuf;
716 *ldwTotsize = 0;
717
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000718 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
Dominik Strasser751c4542002-07-20 20:02:55 +0000719
720 for(i=0; i < num_vals; ++i)
721 {
722 val_list[i].ve_valuelen=0;
723 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
724 if(status != ERROR_SUCCESS)
725 {
726 return status;
727 }
728
729 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
730 {
731 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
732 bufptr, &val_list[i].ve_valuelen);
733 if(status != ERROR_SUCCESS)
734 {
735 return status;
736 }
737
738 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
739
740 bufptr += val_list[i].ve_valuelen;
741 }
742
743 *ldwTotsize += val_list[i].ve_valuelen;
744 }
745 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
746}
747
748/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000749 * RegQueryInfoKeyA [ADVAPI32.@]
750 *
751 * Retrieves information about a registry key.
752 *
753 * PARAMS
754 * hKey [I] Handle to an open key.
755 * lpClass [O] Class string of the key.
756 * lpcClass [I/O] size of lpClass.
757 * lpReserved [I] Reserved; must be NULL.
758 * lpcSubKeys [O] Number of subkeys contained by the key.
759 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
760 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
761 * class in TCHARS.
762 * lpcValues [O] Number of values associated with the key.
763 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
764 * lpcMaxValueLen [O] Longest data component among the key's values
765 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
766 * lpftLastWriteTime [O] FILETIME strucutre that is the last write time.
767 *
768 * RETURNS
769 * Success: ERROR_SUCCESS
770 * Failure: nonzero error code from Winerror.h
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000771 */
772DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
773 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
774 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
775 LPDWORD security, FILETIME *modif )
776{
Alexandre Julliard454355e2000-10-02 03:46:58 +0000777 NTSTATUS status;
778 char buffer[256], *buf_ptr = buffer;
779 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
Alexandre Julliardc6615832001-02-14 21:45:52 +0000780 DWORD total_size, len;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000781
Michael Stefaniuccbacde52002-10-25 19:17:33 +0000782 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000783 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
784
Michael Cardenas7dbce652002-03-21 01:24:52 +0000785 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000786 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000787
Alexandre Julliard454355e2000-10-02 03:46:58 +0000788 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
Alexandre Julliardc6615832001-02-14 21:45:52 +0000789 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000790
Alexandre Julliard454355e2000-10-02 03:46:58 +0000791 if (class || class_len)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000792 {
Alexandre Julliard454355e2000-10-02 03:46:58 +0000793 /* retry with a dynamically allocated buffer */
794 while (status == STATUS_BUFFER_OVERFLOW)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000795 {
Alexandre Julliard454355e2000-10-02 03:46:58 +0000796 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
797 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
798 return ERROR_NOT_ENOUGH_MEMORY;
799 info = (KEY_FULL_INFORMATION *)buf_ptr;
800 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000801 }
Alexandre Julliard454355e2000-10-02 03:46:58 +0000802
Alexandre Julliardc6615832001-02-14 21:45:52 +0000803 if (status) goto done;
804
Alexandre Julliard60fd03d2001-12-05 22:18:48 +0000805 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
Alexandre Julliardc6615832001-02-14 21:45:52 +0000806 if (class_len)
Alexandre Julliard454355e2000-10-02 03:46:58 +0000807 {
Alexandre Julliardc6615832001-02-14 21:45:52 +0000808 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
809 *class_len = len;
810 }
811 if (class && !status)
812 {
Alexandre Julliard60fd03d2001-12-05 22:18:48 +0000813 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
814 info->ClassLength );
Alexandre Julliardc6615832001-02-14 21:45:52 +0000815 class[len] = 0;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000816 }
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000817 }
Alexandre Julliardc6615832001-02-14 21:45:52 +0000818 else status = STATUS_SUCCESS;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000819
Alexandre Julliardc6615832001-02-14 21:45:52 +0000820 if (subkeys) *subkeys = info->SubKeys;
821 if (max_subkey) *max_subkey = info->MaxNameLen;
822 if (max_class) *max_class = info->MaxClassLen;
823 if (values) *values = info->Values;
824 if (max_value) *max_value = info->MaxValueNameLen;
825 if (max_data) *max_data = info->MaxValueDataLen;
Dmitry Timoshkov6b6596a2001-11-23 18:44:43 +0000826 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000827
Alexandre Julliardc6615832001-02-14 21:45:52 +0000828 done:
Alexandre Julliard454355e2000-10-02 03:46:58 +0000829 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
830 return RtlNtStatusToDosError( status );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000831}
832
833
834/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000835 * RegCloseKey [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000836 *
Jon Griffiths50ce0842004-04-23 21:32:34 +0000837 * Close an open registry key.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000838 *
839 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +0000840 * hkey [I] Handle of key to close
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000841 *
842 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +0000843 * Success: ERROR_SUCCESS
844 * Failure: Error code
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000845 */
846DWORD WINAPI RegCloseKey( HKEY hkey )
847{
Alexandre Julliard73755972002-07-31 19:26:03 +0000848 if (!hkey || hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000849 return RtlNtStatusToDosError( NtClose( hkey ) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000850}
851
852
853/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000854 * RegDeleteKeyW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000855 *
Jon Griffiths50ce0842004-04-23 21:32:34 +0000856 * See RegDeleteKeyA.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000857 */
858DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
859{
860 DWORD ret;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000861 HKEY tmp;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000862
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000863 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
864
Mike McCormackda007422002-06-14 23:35:37 +0000865 if (!name || !*name)
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000866 {
Mike McCormackda007422002-06-14 23:35:37 +0000867 ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
868 }
Alexandre Julliarda9298732002-09-12 20:47:14 +0000869 else if (!(ret = RegOpenKeyExW( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
Mike McCormackda007422002-06-14 23:35:37 +0000870 {
871 if (!is_version_nt()) /* win95 does recursive key deletes */
872 {
873 WCHAR name[MAX_PATH];
874
Francois Gouget95918362003-06-18 19:45:22 +0000875 while(!RegEnumKeyW(tmp, 0, name, sizeof(name)))
Mike McCormackda007422002-06-14 23:35:37 +0000876 {
877 if(RegDeleteKeyW(tmp, name)) /* recurse */
878 break;
879 }
880 }
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000881 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
882 RegCloseKey( tmp );
883 }
Mike McCormackda007422002-06-14 23:35:37 +0000884 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000885 return ret;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000886}
887
888
889/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000890 * RegDeleteKeyA [ADVAPI32.@]
Jon Griffiths50ce0842004-04-23 21:32:34 +0000891 *
892 * Delete a registry key.
893 *
894 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +0000895 * hkey [I] Handle to parent key containing the key to delete
896 * name [I] Name of the key user hkey to delete
Jon Griffiths50ce0842004-04-23 21:32:34 +0000897 *
898 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +0000899 * Success: ERROR_SUCCESS
900 * Failure: Error code
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000901 */
902DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
903{
904 DWORD ret;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000905 HKEY tmp;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000906
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000907 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
908
Mike McCormackda007422002-06-14 23:35:37 +0000909 if (!name || !*name)
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000910 {
Mike McCormackda007422002-06-14 23:35:37 +0000911 ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
912 }
Alexandre Julliarda9298732002-09-12 20:47:14 +0000913 else if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
Mike McCormackda007422002-06-14 23:35:37 +0000914 {
915 if (!is_version_nt()) /* win95 does recursive key deletes */
916 {
917 CHAR name[MAX_PATH];
918
Francois Gouget95918362003-06-18 19:45:22 +0000919 while(!RegEnumKeyA(tmp, 0, name, sizeof(name)))
Mike McCormackda007422002-06-14 23:35:37 +0000920 {
921 if(RegDeleteKeyA(tmp, name)) /* recurse */
922 break;
923 }
924 }
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000925 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
926 RegCloseKey( tmp );
927 }
Mike McCormackda007422002-06-14 23:35:37 +0000928 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000929 return ret;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000930}
931
932
933
934/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000935 * RegSetValueExW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000936 *
Jon Griffiths50ce0842004-04-23 21:32:34 +0000937 * Set the data and contents of a registry value.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000938 *
939 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +0000940 * hkey [I] Handle of key to set value for
941 * name [I] Name of value to set
942 * reserved [I] Reserved, must be zero
943 * type [I] Type of the value being set
944 * data [I] The new contents of the value to set
945 * count [I] Size of data
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000946 *
947 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +0000948 * Success: ERROR_SUCCESS
949 * Failure: Error code
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000950 *
951 * NOTES
James Hawkins6a358c42004-07-13 23:33:14 +0000952 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
953 * NT does definitely care (aj)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000954 */
955DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
956 DWORD type, CONST BYTE *data, DWORD count )
957{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000958 UNICODE_STRING nameW;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000959
Michael Cardenas7dbce652002-03-21 01:24:52 +0000960 if (!is_version_nt()) /* win95 */
Alexandre Julliard918da642001-04-10 21:30:24 +0000961 {
962 if (type == REG_SZ) count = (strlenW( (WCHAR *)data ) + 1) * sizeof(WCHAR);
963 }
964 else if (count && is_string(type))
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000965 {
966 LPCWSTR str = (LPCWSTR)data;
967 /* if user forgot to count terminating null, add it (yes NT does this) */
968 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
969 count += sizeof(WCHAR);
970 }
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +0000971 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliarda01004d2000-05-14 22:57:57 +0000972
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000973 RtlInitUnicodeString( &nameW, name );
974 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000975}
976
977
978/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +0000979 * RegSetValueExA [ADVAPI32.@]
980 *
981 * see RegSetValueExW
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000982 */
983DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000984 CONST BYTE *data, DWORD count )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000985{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000986 ANSI_STRING nameA;
987 WCHAR *dataW = NULL;
988 NTSTATUS status;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000989
Michael Cardenas7dbce652002-03-21 01:24:52 +0000990 if (!is_version_nt()) /* win95 */
Alexandre Julliard918da642001-04-10 21:30:24 +0000991 {
992 if (type == REG_SZ) count = strlen(data) + 1;
993 }
994 else if (count && is_string(type))
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +0000995 {
996 /* if user forgot to count terminating null, add it (yes NT does this) */
997 if (data[count-1] && !data[count]) count++;
998 }
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000999
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001000 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1001
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001002 if (is_string( type )) /* need to convert to Unicode */
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001003 {
Alexandre Julliard60fd03d2001-12-05 22:18:48 +00001004 DWORD lenW;
1005 RtlMultiByteToUnicodeSize( &lenW, data, count );
1006 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
1007 RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
1008 count = lenW;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001009 data = (BYTE *)dataW;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001010 }
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001011
1012 RtlInitAnsiString( &nameA, name );
Alexandre Julliardab5e9752001-03-23 19:12:01 +00001013 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1014 &nameA, FALSE )))
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001015 {
Alexandre Julliardab5e9752001-03-23 19:12:01 +00001016 status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001017 }
1018 if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
1019 return RtlNtStatusToDosError( status );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001020}
1021
1022
1023/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001024 * RegSetValueW [ADVAPI32.@]
1025 *
1026 * Sets the data for the default or unnamed value of a reg key.
1027 *
1028 * PARAMS
1029 * hKey [I] Handle to an open key.
1030 * lpSubKey [I] Name of a subkey of hKey.
1031 * dwType [I] Type of information to store.
1032 * lpData [I] String that contains the data to set for the default value.
1033 * cbData [I] Size of lpData.
1034 *
1035 * RETURNS
1036 * Success: ERROR_SUCCESS
1037 * Failure: nonzero error code from Winerror.h
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001038 */
1039DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
1040{
1041 HKEY subkey = hkey;
1042 DWORD ret;
1043
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001044 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001045
1046 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1047
1048 if (name && name[0]) /* need to create the subkey */
1049 {
1050 if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1051 }
1052
1053 ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
Alexandre Julliardc7e7df82000-08-14 14:41:19 +00001054 (strlenW( data ) + 1) * sizeof(WCHAR) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001055 if (subkey != hkey) RegCloseKey( subkey );
1056 return ret;
1057}
1058
1059
1060/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001061 * RegSetValueA [ADVAPI32.@]
1062 *
1063 * see RegSetValueW
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001064 */
1065DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
1066{
1067 HKEY subkey = hkey;
1068 DWORD ret;
1069
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001070 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001071
1072 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1073
1074 if (name && name[0]) /* need to create the subkey */
1075 {
1076 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1077 }
1078 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
1079 if (subkey != hkey) RegCloseKey( subkey );
1080 return ret;
1081}
1082
1083
1084
1085/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001086 * RegQueryValueExW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001087 *
Jon Griffiths50ce0842004-04-23 21:32:34 +00001088 * See RegQueryValueExA.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001089 */
1090DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001091 LPBYTE data, LPDWORD count )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001092{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001093 NTSTATUS status;
1094 UNICODE_STRING name_str;
1095 DWORD total_size;
Alexandre Julliard454355e2000-10-02 03:46:58 +00001096 char buffer[256], *buf_ptr = buffer;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001097 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
Alexandre Julliarda0109322002-07-05 21:30:38 +00001098 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001099
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001100 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
Francois Gouget8f63a712003-12-13 01:34:40 +00001101 hkey, debugstr_w(name), reserved, type, data, count,
1102 (count && data) ? *count : 0 );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001103
Eric Pouech2c655f51999-11-25 22:05:46 +00001104 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001105 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001106
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001107 RtlInitUnicodeString( &name_str, name );
1108
1109 if (data) total_size = min( sizeof(buffer), *count + info_size );
1110 else total_size = info_size;
1111
1112 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1113 buffer, total_size, &total_size );
1114 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001115
1116 if (data)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001117 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001118 /* retry with a dynamically allocated buffer */
1119 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001120 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001121 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1122 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
Alexandre Julliard454355e2000-10-02 03:46:58 +00001123 return ERROR_NOT_ENOUGH_MEMORY;
1124 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1125 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1126 buf_ptr, total_size, &total_size );
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001127 }
1128
1129 if (!status)
1130 {
1131 memcpy( data, buf_ptr + info_size, total_size - info_size );
1132 /* if the type is REG_SZ and data is not 0-terminated
1133 * and there is enough space in the buffer NT appends a \0 */
1134 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001135 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001136 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1137 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001138 }
1139 }
Alexandre Julliard454355e2000-10-02 03:46:58 +00001140 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001141 }
Alexandre Julliardc6615832001-02-14 21:45:52 +00001142 else status = STATUS_SUCCESS;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001143
1144 if (type) *type = info->Type;
1145 if (count) *count = total_size - info_size;
1146
1147 done:
Alexandre Julliard454355e2000-10-02 03:46:58 +00001148 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001149 return RtlNtStatusToDosError(status);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001150}
1151
1152
1153/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001154 * RegQueryValueExA [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001155 *
Jon Griffiths50ce0842004-04-23 21:32:34 +00001156 * Get the type and contents of a specified value under with a key.
1157 *
1158 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001159 * hkey [I] Handle of the key to query
1160 * name [I] Name of value under hkey to query
1161 * reserved [I] Reserved - must be NULL
1162 * type [O] Destination for the value type, or NULL if not required
1163 * data [O] Destination for the values contents, or NULL if not required
1164 * count [I/O] Size of data, updated with the number of bytes returned
Jon Griffiths50ce0842004-04-23 21:32:34 +00001165 *
1166 * RETURNS
1167 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1168 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1169 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1170 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1171 *
1172 * NOTES
1173 * MSDN states that if data is too small it is partially filled. In reality
1174 * it remains untouched.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001175 */
1176DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001177 LPBYTE data, LPDWORD count )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001178{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001179 NTSTATUS status;
1180 ANSI_STRING nameA;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001181 DWORD total_size;
Alexandre Julliard454355e2000-10-02 03:46:58 +00001182 char buffer[256], *buf_ptr = buffer;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001183 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
Alexandre Julliarda0109322002-07-05 21:30:38 +00001184 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001185
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001186 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001187 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1188
Eric Pouech2c655f51999-11-25 22:05:46 +00001189 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001190 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001191
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001192 RtlInitAnsiString( &nameA, name );
Alexandre Julliardab5e9752001-03-23 19:12:01 +00001193 if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1194 &nameA, FALSE )))
Alexandre Julliard454355e2000-10-02 03:46:58 +00001195 return RtlNtStatusToDosError(status);
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001196
Alexandre Julliardab5e9752001-03-23 19:12:01 +00001197 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1198 KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001199 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001200
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001201 /* we need to fetch the contents for a string type even if not requested,
1202 * because we need to compute the length of the ASCII string. */
1203 if (data || is_string(info->Type))
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001204 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001205 /* retry with a dynamically allocated buffer */
1206 while (status == STATUS_BUFFER_OVERFLOW)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001207 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001208 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1209 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
Alexandre Julliard454355e2000-10-02 03:46:58 +00001210 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001211 status = STATUS_NO_MEMORY;
Alexandre Julliard454355e2000-10-02 03:46:58 +00001212 goto done;
1213 }
1214 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
Alexandre Julliardab5e9752001-03-23 19:12:01 +00001215 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1216 KeyValuePartialInformation, buf_ptr, total_size, &total_size );
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001217 }
1218
Alexandre Julliardc6615832001-02-14 21:45:52 +00001219 if (status) goto done;
1220
1221 if (is_string(info->Type))
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001222 {
Alexandre Julliard60fd03d2001-12-05 22:18:48 +00001223 DWORD len;
1224
1225 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1226 total_size - info_size );
Alexandre Julliardc6615832001-02-14 21:45:52 +00001227 if (data && len)
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001228 {
Alexandre Julliardc6615832001-02-14 21:45:52 +00001229 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1230 else
1231 {
Alexandre Julliard60fd03d2001-12-05 22:18:48 +00001232 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1233 total_size - info_size );
Alexandre Julliardc6615832001-02-14 21:45:52 +00001234 /* if the type is REG_SZ and data is not 0-terminated
1235 * and there is enough space in the buffer NT appends a \0 */
1236 if (len < *count && data[len-1]) data[len] = 0;
1237 }
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001238 }
Alexandre Julliardc6615832001-02-14 21:45:52 +00001239 total_size = len + info_size;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001240 }
Alexandre Julliardc6615832001-02-14 21:45:52 +00001241 else if (data)
1242 {
1243 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1244 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1245 }
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001246 }
Alexandre Julliardc6615832001-02-14 21:45:52 +00001247 else status = STATUS_SUCCESS;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001248
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001249 if (type) *type = info->Type;
1250 if (count) *count = total_size - info_size;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001251
1252 done:
Alexandre Julliard454355e2000-10-02 03:46:58 +00001253 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001254 return RtlNtStatusToDosError(status);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001255}
1256
1257
1258/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001259 * RegQueryValueW [ADVAPI32.@]
1260 *
1261 * Retrieves the data associated with the default or unnamed value of a key.
1262 *
1263 * PARAMS
1264 * hKey [I] Handle to an open key.
1265 * lpSubKey [I] Name of the subkey of hKey.
1266 * lpValue [O] Receives the string associated with the default value
1267 * of the key.
1268 * lpcbValue [I/O] Size of lpValue.
1269 *
1270 * RETURNS
1271 * Success: ERROR_SUCCESS
1272 * Failure: nonzero error code from Winerror.h
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001273 */
1274DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1275{
1276 DWORD ret;
1277 HKEY subkey = hkey;
1278
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001279 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001280
1281 if (name && name[0])
1282 {
1283 if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1284 }
1285 ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1286 if (subkey != hkey) RegCloseKey( subkey );
1287 if (ret == ERROR_FILE_NOT_FOUND)
1288 {
1289 /* return empty string if default value not found */
1290 if (data) *data = 0;
1291 if (count) *count = 1;
1292 ret = ERROR_SUCCESS;
1293 }
1294 return ret;
1295}
1296
1297
1298/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001299 * RegQueryValueA [ADVAPI32.@]
1300 *
1301 * see RegQueryValueW
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001302 */
1303DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1304{
1305 DWORD ret;
1306 HKEY subkey = hkey;
1307
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001308 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001309
1310 if (name && name[0])
1311 {
1312 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1313 }
1314 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1315 if (subkey != hkey) RegCloseKey( subkey );
1316 if (ret == ERROR_FILE_NOT_FOUND)
1317 {
1318 /* return empty string if default value not found */
1319 if (data) *data = 0;
1320 if (count) *count = 1;
1321 ret = ERROR_SUCCESS;
1322 }
1323 return ret;
1324}
1325
1326
1327/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001328 * RegEnumValueW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001329 *
1330 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001331 * hkey [I] Handle to key to query
1332 * index [I] Index of value to query
1333 * value [O] Value string
1334 * val_count [I/O] Size of value buffer (in wchars)
1335 * reserved [I] Reserved
1336 * type [O] Type code
1337 * data [O] Value data
1338 * count [I/O] Size of data buffer (in bytes)
1339 *
1340 * RETURNS
1341 * Success: ERROR_SUCCESS
1342 * Failure: nonzero error code from Winerror.h
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001343 */
1344
1345DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1346 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1347{
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001348 NTSTATUS status;
1349 DWORD total_size;
1350 char buffer[256], *buf_ptr = buffer;
1351 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
Alexandre Julliarda0109322002-07-05 21:30:38 +00001352 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001353
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001354 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001355 hkey, index, value, val_count, reserved, type, data, count );
1356
1357 /* NT only checks count, not val_count */
Eric Pouech2c655f51999-11-25 22:05:46 +00001358 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001359 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001360
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001361 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1362 if (data) total_size += *count;
1363 total_size = min( sizeof(buffer), total_size );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001364
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001365 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1366 buffer, total_size, &total_size );
1367 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001368
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001369 if (value || data)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001370 {
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001371 /* retry with a dynamically allocated buffer */
1372 while (status == STATUS_BUFFER_OVERFLOW)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001373 {
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001374 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1375 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1376 return ERROR_NOT_ENOUGH_MEMORY;
1377 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1378 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1379 buf_ptr, total_size, &total_size );
1380 }
1381
1382 if (status) goto done;
1383
1384 if (value)
1385 {
1386 if (info->NameLength/sizeof(WCHAR) >= *val_count)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001387 {
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001388 status = STATUS_BUFFER_OVERFLOW;
Alexandre Julliardc1dddbe2002-07-10 03:27:35 +00001389 goto overflow;
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001390 }
1391 memcpy( value, info->Name, info->NameLength );
1392 *val_count = info->NameLength / sizeof(WCHAR);
1393 value[*val_count] = 0;
1394 }
1395
1396 if (data)
1397 {
1398 if (total_size - info->DataOffset > *count)
1399 {
1400 status = STATUS_BUFFER_OVERFLOW;
Alexandre Julliardc1dddbe2002-07-10 03:27:35 +00001401 goto overflow;
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001402 }
1403 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1404 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1405 {
1406 /* if the type is REG_SZ and data is not 0-terminated
1407 * and there is enough space in the buffer NT appends a \0 */
1408 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1409 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001410 }
1411 }
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001412 }
Alexandre Julliardc6615832001-02-14 21:45:52 +00001413 else status = STATUS_SUCCESS;
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001414
Alexandre Julliardc1dddbe2002-07-10 03:27:35 +00001415 overflow:
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001416 if (type) *type = info->Type;
1417 if (count) *count = info->DataLength;
1418
1419 done:
1420 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1421 return RtlNtStatusToDosError(status);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001422}
1423
1424
1425/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001426 * RegEnumValueA [ADVAPI32.@]
1427 *
1428 * see RegEnumValueW
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001429 */
1430DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1431 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1432{
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001433 NTSTATUS status;
1434 DWORD total_size;
1435 char buffer[256], *buf_ptr = buffer;
1436 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
Alexandre Julliarda0109322002-07-05 21:30:38 +00001437 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001438
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001439 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001440 hkey, index, value, val_count, reserved, type, data, count );
1441
1442 /* NT only checks count, not val_count */
Eric Pouech2c655f51999-11-25 22:05:46 +00001443 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001444 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001445
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001446 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1447 if (data) total_size += *count;
1448 total_size = min( sizeof(buffer), total_size );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001449
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001450 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1451 buffer, total_size, &total_size );
1452 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001453
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001454 /* we need to fetch the contents for a string type even if not requested,
1455 * because we need to compute the length of the ASCII string. */
1456 if (value || data || is_string(info->Type))
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001457 {
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001458 /* retry with a dynamically allocated buffer */
1459 while (status == STATUS_BUFFER_OVERFLOW)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001460 {
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001461 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1462 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1463 return ERROR_NOT_ENOUGH_MEMORY;
1464 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1465 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1466 buf_ptr, total_size, &total_size );
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001467 }
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001468
1469 if (status) goto done;
1470
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001471 if (is_string(info->Type))
1472 {
Alexandre Julliard60fd03d2001-12-05 22:18:48 +00001473 DWORD len;
1474 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1475 total_size - info->DataOffset );
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001476 if (data && len)
1477 {
Alexandre Julliardc1dddbe2002-07-10 03:27:35 +00001478 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1479 else
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001480 {
Alexandre Julliardc1dddbe2002-07-10 03:27:35 +00001481 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1482 total_size - info->DataOffset );
1483 /* if the type is REG_SZ and data is not 0-terminated
1484 * and there is enough space in the buffer NT appends a \0 */
1485 if (len < *count && data[len-1]) data[len] = 0;
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001486 }
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001487 }
1488 info->DataLength = len;
1489 }
1490 else if (data)
1491 {
1492 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1493 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1494 }
Alexandre Julliardc1dddbe2002-07-10 03:27:35 +00001495
1496 if (value && !status)
1497 {
1498 DWORD len;
1499
1500 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1501 if (len >= *val_count)
1502 {
1503 status = STATUS_BUFFER_OVERFLOW;
1504 if (*val_count)
1505 {
1506 len = *val_count - 1;
1507 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1508 value[len] = 0;
1509 }
1510 }
1511 else
1512 {
1513 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1514 value[len] = 0;
1515 *val_count = len;
1516 }
1517 }
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001518 }
Alexandre Julliardc6615832001-02-14 21:45:52 +00001519 else status = STATUS_SUCCESS;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001520
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001521 if (type) *type = info->Type;
1522 if (count) *count = info->DataLength;
1523
1524 done:
1525 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1526 return RtlNtStatusToDosError(status);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001527}
1528
1529
1530
1531/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001532 * RegDeleteValueW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001533 *
Jon Griffithscd4234a2003-03-18 18:35:48 +00001534 * See RegDeleteValueA.
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001535 */
1536DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1537{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001538 UNICODE_STRING nameW;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001539
1540 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1541
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001542 RtlInitUnicodeString( &nameW, name );
1543 return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001544}
1545
1546
1547/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001548 * RegDeleteValueA [ADVAPI32.@]
Jon Griffithscd4234a2003-03-18 18:35:48 +00001549 *
1550 * Delete a value from the registry.
1551 *
1552 * PARAMS
1553 * hkey [I] Registry handle of the key holding the value
1554 * name [I] Name of the value under hkey to delete
1555 *
1556 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +00001557 * Success: ERROR_SUCCESS
1558 * Failure: nonzero error code from Winerror.h
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001559 */
1560DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1561{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001562 STRING nameA;
1563 NTSTATUS status;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001564
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001565 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1566
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001567 RtlInitAnsiString( &nameA, name );
Alexandre Julliardab5e9752001-03-23 19:12:01 +00001568 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1569 &nameA, FALSE )))
Alexandre Julliardab5e9752001-03-23 19:12:01 +00001570 status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001571 return RtlNtStatusToDosError( status );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001572}
1573
1574
1575/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001576 * RegLoadKeyW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001577 *
1578 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001579 * hkey [I] Handle of open key
1580 * subkey [I] Address of name of subkey
1581 * filename [I] Address of filename for registry information
1582 *
1583 * RETURNS
1584 * Success: ERROR_SUCCES
1585 * Failure: nonzero error code from Winerror.h
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001586 */
1587LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1588{
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001589 HANDLE file;
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001590 DWORD ret, len, err = GetLastError();
Aric Stewartcdb9be72002-11-13 19:45:27 +00001591 HKEY shkey;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001592
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001593 TRACE( "(%p,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001594
1595 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1596 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001597 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001598
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001599 len = strlenW( subkey ) * sizeof(WCHAR);
1600 if (len > MAX_PATH*sizeof(WCHAR)) return ERROR_INVALID_PARAMETER;
1601
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001602 if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
François Gougetda2b6a92001-01-06 01:29:18 +00001603 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001604 {
1605 ret = GetLastError();
1606 goto done;
1607 }
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001608
Aric Stewartcdb9be72002-11-13 19:45:27 +00001609 RegCreateKeyW(hkey,subkey,&shkey);
1610
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001611 SERVER_START_REQ( load_registry )
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001612 {
Aric Stewartcdb9be72002-11-13 19:45:27 +00001613 req->hkey = shkey;
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001614 req->file = file;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001615 wine_server_add_data( req, subkey, len );
1616 ret = RtlNtStatusToDosError( wine_server_call(req) );
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001617 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001618 SERVER_END_REQ;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001619 CloseHandle( file );
Aric Stewartcdb9be72002-11-13 19:45:27 +00001620 RegCloseKey(shkey);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001621
1622 done:
1623 SetLastError( err ); /* restore the last error code */
1624 return ret;
1625}
1626
1627
1628/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001629 * RegLoadKeyA [ADVAPI32.@]
1630 *
1631 * see RegLoadKeyW
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001632 */
1633LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1634{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001635 WCHAR buffer[MAX_PATH];
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001636 HANDLE file;
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001637 DWORD ret, len, err = GetLastError();
Aric Stewartcdb9be72002-11-13 19:45:27 +00001638 HKEY shkey;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001639
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001640 TRACE( "(%p,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001641
1642 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1643 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001644 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001645
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001646 if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
1647 return ERROR_INVALID_PARAMETER;
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001648
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001649 if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
François Gougetda2b6a92001-01-06 01:29:18 +00001650 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001651 {
1652 ret = GetLastError();
1653 goto done;
1654 }
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001655
Aric Stewartcdb9be72002-11-13 19:45:27 +00001656 RegCreateKeyA(hkey,subkey,&shkey);
1657
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001658 SERVER_START_REQ( load_registry )
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001659 {
Aric Stewartcdb9be72002-11-13 19:45:27 +00001660 req->hkey = shkey;
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001661 req->file = file;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001662 wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
1663 ret = RtlNtStatusToDosError( wine_server_call(req) );
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001664 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001665 SERVER_END_REQ;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001666 CloseHandle( file );
Aric Stewartcdb9be72002-11-13 19:45:27 +00001667 RegCloseKey(shkey);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001668
1669 done:
1670 SetLastError( err ); /* restore the last error code */
1671 return ret;
1672}
1673
1674
1675/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001676 * RegSaveKeyW [ADVAPI32.@]
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001677 *
1678 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001679 * hkey [I] Handle of key where save begins
1680 * lpFile [I] Address of filename to save to
1681 * sa [I] Address of security structure
1682 *
1683 * RETURNS
1684 * Success: ERROR_SUCCESS
1685 * Failure: nonzero error code from Winerror.h
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001686 */
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001687LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001688{
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001689 static const WCHAR format[] =
1690 {'r','e','g','%','0','4','x','.','t','m','p',0};
1691 WCHAR buffer[MAX_PATH];
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001692 int count = 0;
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001693 LPWSTR nameW;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001694 DWORD ret, err;
Alexandre Julliard73755972002-07-31 19:26:03 +00001695 HANDLE handle;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001696
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001697 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001698
1699 if (!file || !*file) return ERROR_INVALID_PARAMETER;
Alexandre Julliardf2ef2c22002-09-13 21:42:28 +00001700 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001701
1702 err = GetLastError();
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001703 GetFullPathNameW( file, sizeof(buffer)/sizeof(WCHAR), buffer, &nameW );
1704
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001705 for (;;)
1706 {
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001707 snprintfW( nameW, 16, format, count++ );
1708 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
François Gougetda2b6a92001-01-06 01:29:18 +00001709 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001710 if (handle != INVALID_HANDLE_VALUE) break;
Alexandre Julliard53544172000-03-26 18:18:03 +00001711 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
Andreas Mohr62974512000-05-09 22:31:43 +00001712
1713 /* Something gone haywire ? Please report if this happens abnormally */
1714 if (count >= 100)
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001715 MESSAGE("Wow, we are already fiddling with a temp file %s with an ordinal as high as %d !\nYou might want to delete all corresponding temp files in that directory.\n", debugstr_w(buffer), count);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001716 }
1717
Alexandre Julliard67a74992001-02-27 02:09:16 +00001718 SERVER_START_REQ( save_registry )
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001719 {
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001720 req->hkey = hkey;
1721 req->file = handle;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001722 ret = RtlNtStatusToDosError( wine_server_call( req ) );
Alexandre Julliard57f05e12000-10-15 00:40:25 +00001723 }
1724 SERVER_END_REQ;
1725
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001726 CloseHandle( handle );
1727 if (!ret)
1728 {
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001729 if (!MoveFileExW( buffer, file, MOVEFILE_REPLACE_EXISTING ))
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001730 {
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001731 ERR( "Failed to move %s to %s\n", debugstr_w(buffer),
1732 debugstr_w(file) );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001733 ret = GetLastError();
1734 }
1735 }
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001736 if (ret) DeleteFileW( buffer );
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001737
1738done:
1739 SetLastError( err ); /* restore last error code */
1740 return ret;
1741}
1742
1743
1744/******************************************************************************
James Hawkins6a358c42004-07-13 23:33:14 +00001745 * RegSaveKeyA [ADVAPI32.@]
1746 *
1747 * see RegSaveKeyW
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001748 */
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001749LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001750{
Tony Lambregts4550b8b2003-03-14 04:50:34 +00001751 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
1752 NTSTATUS status;
1753 STRING fileA;
1754
1755 RtlInitAnsiString(&fileA, file);
1756 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
1757 return RtlNtStatusToDosError( status );
1758 return RegSaveKeyW(hkey, fileW->Buffer, sa);
Alexandre Julliard2fab2ef1999-11-23 19:41:34 +00001759}
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001760
1761
1762/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001763 * RegRestoreKeyW [ADVAPI32.@]
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001764 *
1765 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001766 * hkey [I] Handle of key where restore begins
1767 * lpFile [I] Address of filename containing saved tree
1768 * dwFlags [I] Optional flags
1769 *
1770 * RETURNS
1771 * Success: ERROR_SUCCESS
1772 * Failure: nonzero error code from Winerror.h
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001773 */
1774LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
1775{
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001776 TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001777
1778 /* It seems to do this check before the hkey check */
1779 if (!lpFile || !*lpFile)
1780 return ERROR_INVALID_PARAMETER;
1781
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001782 FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001783
1784 /* Check for file existence */
1785
1786 return ERROR_SUCCESS;
1787}
1788
1789
1790/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001791 * RegRestoreKeyA [ADVAPI32.@]
James Hawkins6a358c42004-07-13 23:33:14 +00001792 *
1793 * see RegRestoreKeyW
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001794 */
1795LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
1796{
Matthew Davison6f2a0712003-01-20 23:23:12 +00001797 UNICODE_STRING lpFileW;
1798 LONG ret;
1799
1800 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
1801 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
1802 RtlFreeUnicodeString( &lpFileW );
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001803 return ret;
1804}
1805
1806
1807/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001808 * RegUnLoadKeyW [ADVAPI32.@]
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001809 *
1810 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001811 * hkey [I] Handle of open key
1812 * lpSubKey [I] Address of name of subkey to unload
1813 *
1814 * RETURNS
1815 * Success: ERROR_SUCCESS
1816 * Failure: nonzero error code from Winerror.h
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001817 */
1818LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
1819{
Mike McCormack5ac945c2003-08-19 03:08:17 +00001820 DWORD ret;
1821 HKEY shkey;
1822
1823 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
1824
1825 ret = RegOpenKeyW(hkey,lpSubKey,&shkey);
1826 if( ret )
1827 return ERROR_INVALID_PARAMETER;
1828
1829 SERVER_START_REQ( unload_registry )
1830 {
1831 req->hkey = shkey;
1832 ret = RtlNtStatusToDosError( wine_server_call(req) );
1833 }
1834 SERVER_END_REQ;
1835 RegCloseKey(shkey);
1836
1837 return ret;
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001838}
1839
1840
1841/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001842 * RegUnLoadKeyA [ADVAPI32.@]
James Hawkins6a358c42004-07-13 23:33:14 +00001843 *
1844 * see RegUnLoadKeyW
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001845 */
1846LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
1847{
Matthew Davison6f2a0712003-01-20 23:23:12 +00001848 UNICODE_STRING lpSubKeyW;
1849 LONG ret;
1850
1851 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
1852 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
1853 RtlFreeUnicodeString( &lpSubKeyW );
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001854 return ret;
1855}
1856
1857
1858/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001859 * RegReplaceKeyW [ADVAPI32.@]
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001860 *
1861 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001862 * hkey [I] Handle of open key
1863 * lpSubKey [I] Address of name of subkey
1864 * lpNewFile [I] Address of filename for file with new data
1865 * lpOldFile [I] Address of filename for backup file
1866 *
1867 * RETURNS
1868 * Success: ERROR_SUCCESS
1869 * Failure: nonzero error code from Winerror.h
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001870 */
1871LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
1872 LPCWSTR lpOldFile )
1873{
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001874 FIXME("(%p,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001875 debugstr_w(lpNewFile),debugstr_w(lpOldFile));
1876 return ERROR_SUCCESS;
1877}
1878
1879
1880/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001881 * RegReplaceKeyA [ADVAPI32.@]
James Hawkins6a358c42004-07-13 23:33:14 +00001882 *
1883 * see RegReplaceKeyW
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001884 */
1885LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
1886 LPCSTR lpOldFile )
1887{
Matthew Davison6f2a0712003-01-20 23:23:12 +00001888 UNICODE_STRING lpSubKeyW;
1889 UNICODE_STRING lpNewFileW;
1890 UNICODE_STRING lpOldFileW;
1891 LONG ret;
1892
1893 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
1894 RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, lpOldFile );
1895 RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, lpNewFile );
1896 ret = RegReplaceKeyW( hkey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
1897 RtlFreeUnicodeString( &lpOldFileW );
1898 RtlFreeUnicodeString( &lpNewFileW );
1899 RtlFreeUnicodeString( &lpSubKeyW );
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001900 return ret;
1901}
1902
1903
1904/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001905 * RegSetKeySecurity [ADVAPI32.@]
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001906 *
1907 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001908 * hkey [I] Open handle of key to set
1909 * SecurityInfo [I] Descriptor contents
1910 * pSecurityDesc [I] Address of descriptor for key
1911 *
1912 * RETURNS
1913 * Success: ERROR_SUCCESS
1914 * Failure: nonzero error code from Winerror.h
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001915 */
1916LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
1917 PSECURITY_DESCRIPTOR pSecurityDesc )
1918{
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001919 TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001920
1921 /* It seems to perform this check before the hkey check */
1922 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
1923 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
1924 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
1925 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
1926 /* Param OK */
1927 } else
1928 return ERROR_INVALID_PARAMETER;
1929
1930 if (!pSecurityDesc)
1931 return ERROR_INVALID_PARAMETER;
1932
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001933 FIXME(":(%p,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001934
1935 return ERROR_SUCCESS;
1936}
1937
1938
1939/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001940 * RegGetKeySecurity [ADVAPI32.@]
Jon Griffiths50ce0842004-04-23 21:32:34 +00001941 *
1942 * Get a copy of the security descriptor for a given registry key.
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001943 *
1944 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001945 * hkey [I] Open handle of key to set
1946 * SecurityInformation [I] Descriptor contents
1947 * pSecurityDescriptor [O] Address of descriptor for key
1948 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001949 *
1950 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +00001951 * Success: ERROR_SUCCESS
1952 * Failure: Error code
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001953 */
1954LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
1955 PSECURITY_DESCRIPTOR pSecurityDescriptor,
1956 LPDWORD lpcbSecurityDescriptor )
1957{
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001958 TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001959 lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1960
1961 /* FIXME: Check for valid SecurityInformation values */
1962
1963 if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
1964 return ERROR_INSUFFICIENT_BUFFER;
1965
Michael Stefaniuccbacde52002-10-25 19:17:33 +00001966 FIXME("(%p,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001967 pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1968
Dmitry Timoshkov79313d32002-04-02 02:41:27 +00001969 /* Do not leave security descriptor filled with garbage */
1970 RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1971
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001972 return ERROR_SUCCESS;
1973}
1974
1975
1976/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001977 * RegFlushKey [ADVAPI32.@]
Jon Griffiths50ce0842004-04-23 21:32:34 +00001978 *
1979 * Immediately write a registry key to registry.
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001980 *
1981 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00001982 * hkey [I] Handle of key to write
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001983 *
1984 * RETURNS
James Hawkins6a358c42004-07-13 23:33:14 +00001985 * Success: ERROR_SUCCESS
1986 * Failure: Error code
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001987 */
1988DWORD WINAPI RegFlushKey( HKEY hkey )
1989{
Mike Hearn43cb03b2004-01-03 00:38:30 +00001990 hkey = get_special_root_hkey( hkey );
1991 if (!hkey) return ERROR_INVALID_HANDLE;
1992
1993 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001994}
1995
1996
1997/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00001998 * RegConnectRegistryW [ADVAPI32.@]
Alexandre Julliard5ce23292000-11-30 20:31:41 +00001999 *
2000 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00002001 * lpMachineName [I] Address of name of remote computer
2002 * hHey [I] Predefined registry handle
2003 * phkResult [I] Address of buffer for remote registry handle
2004 *
2005 * RETURNS
2006 * Success: ERROR_SUCCESS
2007 * Failure: nonzero error code from Winerror.h
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002008 */
Vincent Béron9a624912002-05-31 23:06:46 +00002009LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
Alexandre Julliard5cf56a32002-10-03 19:46:27 +00002010 PHKEY phkResult )
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002011{
Juan Langae176d12004-03-18 01:34:23 +00002012 LONG ret;
2013
Michael Stefaniuccbacde52002-10-25 19:17:33 +00002014 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002015
2016 if (!lpMachineName || !*lpMachineName) {
2017 /* Use the local machine name */
Juan Langae176d12004-03-18 01:34:23 +00002018 ret = RegOpenKeyW( hKey, NULL, phkResult );
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002019 }
Juan Langae176d12004-03-18 01:34:23 +00002020 else if (lpMachineName[0] != '\\' || lpMachineName[1] != '\\')
2021 ret = ERROR_BAD_NETPATH;
2022 else
2023 {
2024 WCHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
2025 DWORD len = sizeof(compName) / sizeof(WCHAR);
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002026
Juan Langae176d12004-03-18 01:34:23 +00002027 if (GetComputerNameW(compName, &len))
2028 {
2029 if (!strcmpiW(lpMachineName + 2, compName))
2030 ret = RegOpenKeyW(hKey, NULL, phkResult);
2031 else
2032 {
2033 FIXME("Cannot connect to %s\n",debugstr_w(lpMachineName));
2034 ret = ERROR_BAD_NETPATH;
2035 }
2036 }
2037 else
2038 ret = GetLastError();
2039 }
2040 return ret;
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002041}
2042
2043
2044/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00002045 * RegConnectRegistryA [ADVAPI32.@]
James Hawkins6a358c42004-07-13 23:33:14 +00002046 *
2047 * see RegConnectRegistryW
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002048 */
Alexandre Julliard5cf56a32002-10-03 19:46:27 +00002049LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, PHKEY reskey )
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002050{
Matthew Davison6f2a0712003-01-20 23:23:12 +00002051 UNICODE_STRING machineW;
2052 LONG ret;
2053
2054 RtlCreateUnicodeStringFromAsciiz( &machineW, machine );
2055 ret = RegConnectRegistryW( machineW.Buffer, hkey, reskey );
2056 RtlFreeUnicodeString( &machineW );
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002057 return ret;
2058}
2059
2060
2061/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +00002062 * RegNotifyChangeKeyValue [ADVAPI32.@]
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002063 *
2064 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00002065 * hkey [I] Handle of key to watch
2066 * fWatchSubTree [I] Flag for subkey notification
2067 * fdwNotifyFilter [I] Changes to be reported
2068 * hEvent [I] Handle of signaled event
2069 * fAsync [I] Flag for asynchronous reporting
2070 *
2071 * RETURNS
2072 * Success: ERROR_SUCCESS
2073 * Failure: nonzero error code from Winerror.h
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002074 */
Vincent Béron9a624912002-05-31 23:06:46 +00002075LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002076 DWORD fdwNotifyFilter, HANDLE hEvent,
2077 BOOL fAsync )
2078{
Mike McCormack11f4b442002-11-25 02:47:32 +00002079 LONG ret;
2080
2081 TRACE("(%p,%i,%ld,%p,%i)\n",hkey,fWatchSubTree,fdwNotifyFilter,
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002082 hEvent,fAsync);
Mike McCormack11f4b442002-11-25 02:47:32 +00002083
2084 if( !fAsync )
2085 hEvent = CreateEventA(NULL, 0, 0, NULL);
2086
2087 SERVER_START_REQ( set_registry_notification )
2088 {
2089 req->hkey = hkey;
2090 req->event = hEvent;
2091 req->subtree = fWatchSubTree;
2092 req->filter = fdwNotifyFilter;
2093 ret = RtlNtStatusToDosError( wine_server_call(req) );
2094 }
2095 SERVER_END_REQ;
2096
2097 if( !fAsync )
2098 {
2099 if( ret == ERROR_SUCCESS )
2100 WaitForSingleObject( hEvent, INFINITE );
2101 CloseHandle( hEvent );
2102 }
2103
2104 return ret;
Alexandre Julliard5ce23292000-11-30 20:31:41 +00002105}
Robert Shearman65e81822004-01-16 04:40:33 +00002106
2107/******************************************************************************
2108 * RegOpenUserClassesRoot [ADVAPI32.@]
Jon Griffiths50ce0842004-04-23 21:32:34 +00002109 *
2110 * Open the HKEY_CLASSES_ROOT key for a user.
Robert Shearman65e81822004-01-16 04:40:33 +00002111 *
2112 * PARAMS
James Hawkins6a358c42004-07-13 23:33:14 +00002113 * hToken [I] Handle of token representing the user
2114 * dwOptions [I] Reserved, nust be 0
2115 * samDesired [I] Desired access rights
2116 * phkResult [O] Destination for the resulting key handle
Robert Shearman65e81822004-01-16 04:40:33 +00002117 *
James Hawkins6a358c42004-07-13 23:33:14 +00002118 * RETURNS
2119 * Success: ERROR_SUCCESS
2120 * Failure: nonzero error code from Winerror.h
2121 *
Jon Griffiths50ce0842004-04-23 21:32:34 +00002122 * NOTES
James Hawkins6a358c42004-07-13 23:33:14 +00002123 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2124 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2125 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
Robert Shearman65e81822004-01-16 04:40:33 +00002126 */
2127LONG WINAPI RegOpenUserClassesRoot(
2128 HANDLE hToken,
2129 DWORD dwOptions,
2130 REGSAM samDesired,
2131 PHKEY phkResult
2132)
2133{
2134 FIXME("(%p, 0x%lx, 0x%lx, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2135
2136 *phkResult = HKEY_CLASSES_ROOT;
2137 return ERROR_SUCCESS;
2138}