4 * Copyright (C) 1999 Alexandre Julliard
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
11 * This file is concerned about handle management and interaction with the Wine server.
12 * Registry file I/O is in misc/registry.c.
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.
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.
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
38 #include "wine/unicode.h"
39 #include "wine/server.h"
40 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(reg);
45 /* allowed bits for access mask */
46 #define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
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)
52 static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
54 static 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};
58 static const WCHAR name_LOCAL_MACHINE[] =
59 {'M','a','c','h','i','n','e',0};
60 static const WCHAR name_USERS[] =
62 static const WCHAR name_PERFORMANCE_DATA[] =
63 {'P','e','r','f','D','a','t','a',0};
64 static 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};
70 static const WCHAR name_DYN_DATA[] =
71 {'D','y','n','D','a','t','a',0};
73 #define DECL_STR(key) { sizeof(name_##key)-sizeof(WCHAR), sizeof(name_##key), (LPWSTR)name_##key }
74 static UNICODE_STRING root_key_names[NB_SPECIAL_ROOT_KEYS] =
76 DECL_STR(CLASSES_ROOT),
77 { 0, 0, NULL }, /* HKEY_CURRENT_USER is determined dynamically */
78 DECL_STR(LOCAL_MACHINE),
80 DECL_STR(PERFORMANCE_DATA),
81 DECL_STR(CURRENT_CONFIG),
87 /* check if value type needs string conversion (Ansi<->Unicode) */
88 inline static int is_string( DWORD type )
90 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
93 /* check if current version is NT or Win95 */
94 inline static int is_version_nt(void)
96 return !(GetVersion() & 0x80000000);
99 /* create one of the HKEY_* special root keys */
100 static HKEY create_special_root_hkey( HKEY hkey, DWORD access )
103 int idx = (UINT)hkey - (UINT)HKEY_SPECIAL_ROOT_FIRST;
105 if (hkey == HKEY_CURRENT_USER)
107 if (RtlOpenCurrentUser( access, &hkey )) return 0;
108 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
112 OBJECT_ATTRIBUTES attr;
114 attr.Length = sizeof(attr);
115 attr.RootDirectory = 0;
116 attr.ObjectName = &root_key_names[idx];
118 attr.SecurityDescriptor = NULL;
119 attr.SecurityQualityOfService = NULL;
120 if (NtCreateKey( &hkey, access, &attr, 0, NULL, 0, NULL )) return 0;
121 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
124 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
127 NtClose( hkey ); /* somebody beat us to it */
131 /* map the hkey from special root to normal key if necessary */
132 inline static HKEY get_special_root_hkey( HKEY hkey )
136 if ((hkey >= HKEY_SPECIAL_ROOT_FIRST) && (hkey <= HKEY_SPECIAL_ROOT_LAST))
138 if (!(ret = special_root_keys[(UINT)hkey - (UINT)HKEY_SPECIAL_ROOT_FIRST]))
139 ret = create_special_root_hkey( hkey, KEY_ALL_ACCESS );
145 /******************************************************************************
146 * RegCreateKeyExW [ADVAPI32.@]
148 * See RegCreateKeyExA.
150 DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPCWSTR class,
151 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
152 PHKEY retkey, LPDWORD dispos )
154 OBJECT_ATTRIBUTES attr;
155 UNICODE_STRING nameW, classW;
157 if (reserved) return ERROR_INVALID_PARAMETER;
158 if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
159 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
161 attr.Length = sizeof(attr);
162 attr.RootDirectory = hkey;
163 attr.ObjectName = &nameW;
165 attr.SecurityDescriptor = NULL;
166 attr.SecurityQualityOfService = NULL;
167 RtlInitUnicodeString( &nameW, name );
168 RtlInitUnicodeString( &classW, class );
170 return RtlNtStatusToDosError( NtCreateKey( retkey, access, &attr, 0,
171 &classW, options, dispos ) );
175 /******************************************************************************
176 * RegCreateKeyExA [ADVAPI32.@]
178 * Open a registry key, creating it if it doesn't exist.
181 * 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
192 * Success: ERROR_SUCCESS.
193 * Failure: A standard Win32 error code. retkey remains untouched.
196 * MAXIMUM_ALLOWED in access mask not supported by server
198 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPCSTR class,
199 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
200 PHKEY retkey, LPDWORD dispos )
202 OBJECT_ATTRIBUTES attr;
203 UNICODE_STRING classW;
204 ANSI_STRING nameA, classA;
207 if (reserved) return ERROR_INVALID_PARAMETER;
208 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
209 else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
210 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
212 attr.Length = sizeof(attr);
213 attr.RootDirectory = hkey;
214 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
216 attr.SecurityDescriptor = NULL;
217 attr.SecurityQualityOfService = NULL;
218 RtlInitAnsiString( &nameA, name );
219 RtlInitAnsiString( &classA, class );
221 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
224 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
226 status = NtCreateKey( retkey, access, &attr, 0, &classW, options, dispos );
227 RtlFreeUnicodeString( &classW );
230 return RtlNtStatusToDosError( status );
234 /******************************************************************************
235 * RegCreateKeyW [ADVAPI32.@]
237 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
239 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
240 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
241 return RegCreateKeyExW( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
242 KEY_ALL_ACCESS, NULL, retkey, NULL );
246 /******************************************************************************
247 * RegCreateKeyA [ADVAPI32.@]
249 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
251 return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
252 KEY_ALL_ACCESS, NULL, retkey, NULL );
257 /******************************************************************************
258 * RegOpenKeyExW [ADVAPI32.@]
262 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
264 OBJECT_ATTRIBUTES attr;
265 UNICODE_STRING nameW;
267 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
269 attr.Length = sizeof(attr);
270 attr.RootDirectory = hkey;
271 attr.ObjectName = &nameW;
273 attr.SecurityDescriptor = NULL;
274 attr.SecurityQualityOfService = NULL;
275 RtlInitUnicodeString( &nameW, name );
276 return RtlNtStatusToDosError( NtOpenKey( retkey, access, &attr ) );
280 /******************************************************************************
281 * RegOpenKeyExA [ADVAPI32.@]
283 * Open a registry key.
286 * hkey [I] Handle of open key
287 * name [I] Name of subkey to open
288 * reserved [I] Reserved - must be zero
289 * access [I] Security access mask
290 * retkey [O] Handle to open key
293 * Success: ERROR_SUCCESS
294 * Failure: A standard Win32 error code. retkey is set to 0.
297 * - Unlike RegCreateKeyExA(), this function will not create the key if it
300 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
302 OBJECT_ATTRIBUTES attr;
306 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
308 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
310 attr.Length = sizeof(attr);
311 attr.RootDirectory = hkey;
312 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
314 attr.SecurityDescriptor = NULL;
315 attr.SecurityQualityOfService = NULL;
317 RtlInitAnsiString( &nameA, name );
318 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
321 status = NtOpenKey( retkey, access, &attr );
323 return RtlNtStatusToDosError( status );
327 /******************************************************************************
328 * RegOpenKeyW [ADVAPI32.@]
332 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
334 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
338 /******************************************************************************
339 * RegOpenKeyA [ADVAPI32.@]
341 * Open a registry key.
344 * hkey [I] Handle of parent key to open the new key under
345 * name [I] Name of the key under hkey to open
346 * retkey [O] Destination for the resulting Handle
349 * Success: ERROR_SUCCESS
350 * Failure: A standard Win32 error code. retkey is set to 0.
352 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
354 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
358 /******************************************************************************
359 * RegOpenCurrentUser [ADVAPI32.@]
360 * FIXME: This function is supposed to retrieve a handle to the
361 * HKEY_CURRENT_USER for the user the current thread is impersonating.
362 * Since Wine does not currently allow threads to impersonate other users,
363 * this stub should work fine.
365 DWORD WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
367 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
372 /******************************************************************************
373 * RegEnumKeyExW [ADVAPI32.@]
376 * hkey [I] Handle to key to enumerate
377 * index [I] Index of subkey to enumerate
378 * name [O] Buffer for subkey name
379 * name_len [O] Size of subkey buffer
380 * reserved [I] Reserved
381 * class [O] Buffer for class string
382 * class_len [O] Size of class buffer
383 * ft [O] Time key last written to
385 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
386 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
389 char buffer[256], *buf_ptr = buffer;
390 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
393 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
394 name_len ? *name_len : -1, reserved, class, class_len, ft );
396 if (reserved) return ERROR_INVALID_PARAMETER;
397 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
399 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
400 buffer, sizeof(buffer), &total_size );
402 while (status == STATUS_BUFFER_OVERFLOW)
404 /* retry with a dynamically allocated buffer */
405 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
406 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
407 return ERROR_NOT_ENOUGH_MEMORY;
408 info = (KEY_NODE_INFORMATION *)buf_ptr;
409 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
410 buf_ptr, total_size, &total_size );
415 DWORD len = info->NameLength / sizeof(WCHAR);
416 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
418 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
420 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
421 status = STATUS_BUFFER_OVERFLOW;
425 memcpy( name, info->Name, info->NameLength );
429 *class_len = cls_len;
432 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
439 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
440 return RtlNtStatusToDosError( status );
444 /******************************************************************************
445 * RegEnumKeyExA [ADVAPI32.@]
447 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
448 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
451 char buffer[256], *buf_ptr = buffer;
452 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
455 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
456 name_len ? *name_len : -1, reserved, class, class_len, ft );
458 if (reserved) return ERROR_INVALID_PARAMETER;
459 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
461 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
462 buffer, sizeof(buffer), &total_size );
464 while (status == STATUS_BUFFER_OVERFLOW)
466 /* retry with a dynamically allocated buffer */
467 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
468 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
469 return ERROR_NOT_ENOUGH_MEMORY;
470 info = (KEY_NODE_INFORMATION *)buf_ptr;
471 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
472 buf_ptr, total_size, &total_size );
479 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
480 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
482 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
484 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
485 status = STATUS_BUFFER_OVERFLOW;
489 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
493 *class_len = cls_len;
496 RtlUnicodeToMultiByteN( class, cls_len, NULL,
497 (WCHAR *)(buf_ptr + info->ClassOffset),
505 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
506 return RtlNtStatusToDosError( status );
510 /******************************************************************************
511 * RegEnumKeyW [ADVAPI32.@]
513 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
515 return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
519 /******************************************************************************
520 * RegEnumKeyA [ADVAPI32.@]
522 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
524 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
528 /******************************************************************************
529 * RegQueryInfoKeyW [ADVAPI32.@]
532 * hkey [I] Handle to key to query
533 * class [O] Buffer for class string
534 * class_len [O] Size of class string buffer
535 * reserved [I] Reserved
536 * subkeys [O] Buffer for number of subkeys
537 * max_subkey [O] Buffer for longest subkey name length
538 * max_class [O] Buffer for longest class string length
539 * values [O] Buffer for number of value entries
540 * max_value [O] Buffer for longest value name length
541 * max_data [O] Buffer for longest value data length
542 * security [O] Buffer for security descriptor length
543 * modif [O] Modification time
545 * - win95 allows class to be valid and class_len to be NULL
546 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
547 * - both allow class to be NULL and class_len to be NULL
548 * (it's hard to test validity, so test !NULL instead)
550 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
551 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
552 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
553 LPDWORD security, FILETIME *modif )
556 char buffer[256], *buf_ptr = buffer;
557 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
560 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
561 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
563 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
564 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
566 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
567 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
571 /* retry with a dynamically allocated buffer */
572 while (status == STATUS_BUFFER_OVERFLOW)
574 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
575 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
576 return ERROR_NOT_ENOUGH_MEMORY;
577 info = (KEY_FULL_INFORMATION *)buf_ptr;
578 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
581 if (status) goto done;
583 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
585 status = STATUS_BUFFER_OVERFLOW;
589 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
590 class[info->ClassLength/sizeof(WCHAR)] = 0;
593 else status = STATUS_SUCCESS;
595 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
596 if (subkeys) *subkeys = info->SubKeys;
597 if (max_subkey) *max_subkey = info->MaxNameLen;
598 if (max_class) *max_class = info->MaxClassLen;
599 if (values) *values = info->Values;
600 if (max_value) *max_value = info->MaxValueNameLen;
601 if (max_data) *max_data = info->MaxValueDataLen;
602 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
605 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
606 return RtlNtStatusToDosError( status );
610 /******************************************************************************
611 * RegQueryMultipleValuesA [ADVAPI32.@]
613 DWORD WINAPI RegQueryMultipleValuesA(HKEY hkey, PVALENTA val_list, DWORD num_vals,
614 LPSTR lpValueBuf, LPDWORD ldwTotsize)
617 DWORD maxBytes = *ldwTotsize;
619 LPSTR bufptr = lpValueBuf;
622 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
624 for(i=0; i < num_vals; ++i)
627 val_list[i].ve_valuelen=0;
628 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
629 if(status != ERROR_SUCCESS)
634 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
636 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
637 bufptr, &val_list[i].ve_valuelen);
638 if(status != ERROR_SUCCESS)
643 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
645 bufptr += val_list[i].ve_valuelen;
648 *ldwTotsize += val_list[i].ve_valuelen;
650 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
654 /******************************************************************************
655 * RegQueryMultipleValuesW [ADVAPI32.@]
657 DWORD WINAPI RegQueryMultipleValuesW(HKEY hkey, PVALENTW val_list, DWORD num_vals,
658 LPWSTR lpValueBuf, LPDWORD ldwTotsize)
661 DWORD maxBytes = *ldwTotsize;
663 LPSTR bufptr = (LPSTR)lpValueBuf;
666 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
668 for(i=0; i < num_vals; ++i)
670 val_list[i].ve_valuelen=0;
671 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
672 if(status != ERROR_SUCCESS)
677 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
679 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
680 bufptr, &val_list[i].ve_valuelen);
681 if(status != ERROR_SUCCESS)
686 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
688 bufptr += val_list[i].ve_valuelen;
691 *ldwTotsize += val_list[i].ve_valuelen;
693 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
696 /******************************************************************************
697 * RegQueryInfoKeyA [ADVAPI32.@]
699 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
700 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
701 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
702 LPDWORD security, FILETIME *modif )
705 char buffer[256], *buf_ptr = buffer;
706 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
707 DWORD total_size, len;
709 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
710 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
712 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
713 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
715 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
716 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
718 if (class || class_len)
720 /* retry with a dynamically allocated buffer */
721 while (status == STATUS_BUFFER_OVERFLOW)
723 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
724 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
725 return ERROR_NOT_ENOUGH_MEMORY;
726 info = (KEY_FULL_INFORMATION *)buf_ptr;
727 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
730 if (status) goto done;
732 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
735 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
738 if (class && !status)
740 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
745 else status = STATUS_SUCCESS;
747 if (subkeys) *subkeys = info->SubKeys;
748 if (max_subkey) *max_subkey = info->MaxNameLen;
749 if (max_class) *max_class = info->MaxClassLen;
750 if (values) *values = info->Values;
751 if (max_value) *max_value = info->MaxValueNameLen;
752 if (max_data) *max_data = info->MaxValueDataLen;
753 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
756 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
757 return RtlNtStatusToDosError( status );
761 /******************************************************************************
762 * RegCloseKey [ADVAPI32.@]
764 * Close an open registry key.
767 * hkey [I] Handle of key to close
770 * Success: ERROR_SUCCESS
771 * Failure: Error code
773 DWORD WINAPI RegCloseKey( HKEY hkey )
775 if (!hkey || hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
776 return RtlNtStatusToDosError( NtClose( hkey ) );
780 /******************************************************************************
781 * RegDeleteKeyW [ADVAPI32.@]
785 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
790 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
794 ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
796 else if (!(ret = RegOpenKeyExW( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
798 if (!is_version_nt()) /* win95 does recursive key deletes */
800 WCHAR name[MAX_PATH];
802 while(!RegEnumKeyW(tmp, 0, name, sizeof(name)))
804 if(RegDeleteKeyW(tmp, name)) /* recurse */
808 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
811 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
816 /******************************************************************************
817 * RegDeleteKeyA [ADVAPI32.@]
819 * Delete a registry key.
822 * hkey [I] Handle to parent key containing the key to delete
823 * name [I] Name of the key user hkey to delete
826 * Success: ERROR_SUCCESS
827 * Failure: Error code
829 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
834 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
838 ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
840 else if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
842 if (!is_version_nt()) /* win95 does recursive key deletes */
846 while(!RegEnumKeyA(tmp, 0, name, sizeof(name)))
848 if(RegDeleteKeyA(tmp, name)) /* recurse */
852 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
855 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
861 /******************************************************************************
862 * RegSetValueExW [ADVAPI32.@]
864 * Set the data and contents of a registry value.
867 * hkey [I] Handle of key to set value for
868 * name [I] Name of value to set
869 * reserved [I] Reserved, must be zero
870 * type [I] Type of the value being set
871 * data [I] The new contents of the value to set
872 * count [I] Size of data
875 * Success: ERROR_SUCCESS
876 * Failure: Error code
879 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
880 * NT does definitely care (aj)
882 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
883 DWORD type, CONST BYTE *data, DWORD count )
885 UNICODE_STRING nameW;
887 if (!is_version_nt()) /* win95 */
889 if (type == REG_SZ) count = (strlenW( (WCHAR *)data ) + 1) * sizeof(WCHAR);
891 else if (count && is_string(type))
893 LPCWSTR str = (LPCWSTR)data;
894 /* if user forgot to count terminating null, add it (yes NT does this) */
895 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
896 count += sizeof(WCHAR);
898 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
900 RtlInitUnicodeString( &nameW, name );
901 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
905 /******************************************************************************
906 * RegSetValueExA [ADVAPI32.@]
908 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
909 CONST BYTE *data, DWORD count )
915 if (!is_version_nt()) /* win95 */
917 if (type == REG_SZ) count = strlen(data) + 1;
919 else if (count && is_string(type))
921 /* if user forgot to count terminating null, add it (yes NT does this) */
922 if (data[count-1] && !data[count]) count++;
925 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
927 if (is_string( type )) /* need to convert to Unicode */
930 RtlMultiByteToUnicodeSize( &lenW, data, count );
931 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
932 RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
934 data = (BYTE *)dataW;
937 RtlInitAnsiString( &nameA, name );
938 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
941 status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
943 if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
944 return RtlNtStatusToDosError( status );
948 /******************************************************************************
949 * RegSetValueW [ADVAPI32.@]
951 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
956 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
958 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
960 if (name && name[0]) /* need to create the subkey */
962 if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
965 ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
966 (strlenW( data ) + 1) * sizeof(WCHAR) );
967 if (subkey != hkey) RegCloseKey( subkey );
972 /******************************************************************************
973 * RegSetValueA [ADVAPI32.@]
975 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
980 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
982 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
984 if (name && name[0]) /* need to create the subkey */
986 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
988 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
989 if (subkey != hkey) RegCloseKey( subkey );
995 /******************************************************************************
996 * RegQueryValueExW [ADVAPI32.@]
998 * See RegQueryValueExA.
1000 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1001 LPBYTE data, LPDWORD count )
1004 UNICODE_STRING name_str;
1006 char buffer[256], *buf_ptr = buffer;
1007 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1008 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1010 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1011 hkey, debugstr_w(name), reserved, type, data, count,
1012 (count && data) ? *count : 0 );
1014 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1015 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1017 RtlInitUnicodeString( &name_str, name );
1019 if (data) total_size = min( sizeof(buffer), *count + info_size );
1020 else total_size = info_size;
1022 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1023 buffer, total_size, &total_size );
1024 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1028 /* retry with a dynamically allocated buffer */
1029 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1031 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1032 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1033 return ERROR_NOT_ENOUGH_MEMORY;
1034 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1035 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1036 buf_ptr, total_size, &total_size );
1041 memcpy( data, buf_ptr + info_size, total_size - info_size );
1042 /* if the type is REG_SZ and data is not 0-terminated
1043 * and there is enough space in the buffer NT appends a \0 */
1044 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1046 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1047 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1050 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1052 else status = STATUS_SUCCESS;
1054 if (type) *type = info->Type;
1055 if (count) *count = total_size - info_size;
1058 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1059 return RtlNtStatusToDosError(status);
1063 /******************************************************************************
1064 * RegQueryValueExA [ADVAPI32.@]
1066 * Get the type and contents of a specified value under with a key.
1069 * hkey [I] Handle of the key to query
1070 * name [I] Name of value under hkey to query
1071 * reserved [I] Reserved - must be NULL
1072 * type [O] Destination for the value type, or NULL if not required
1073 * data [O] Destination for the values contents, or NULL if not required
1074 * count [I/O] Size of data, updated with the number of bytes returned
1077 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1078 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1079 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1080 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1083 * MSDN states that if data is too small it is partially filled. In reality
1084 * it remains untouched.
1086 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
1087 LPBYTE data, LPDWORD count )
1092 char buffer[256], *buf_ptr = buffer;
1093 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1094 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1096 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1097 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1099 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1100 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1102 RtlInitAnsiString( &nameA, name );
1103 if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1105 return RtlNtStatusToDosError(status);
1107 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1108 KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
1109 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1111 /* we need to fetch the contents for a string type even if not requested,
1112 * because we need to compute the length of the ASCII string. */
1113 if (data || is_string(info->Type))
1115 /* retry with a dynamically allocated buffer */
1116 while (status == STATUS_BUFFER_OVERFLOW)
1118 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1119 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1121 status = STATUS_NO_MEMORY;
1124 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1125 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1126 KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1129 if (status) goto done;
1131 if (is_string(info->Type))
1135 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1136 total_size - info_size );
1139 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1142 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1143 total_size - info_size );
1144 /* if the type is REG_SZ and data is not 0-terminated
1145 * and there is enough space in the buffer NT appends a \0 */
1146 if (len < *count && data[len-1]) data[len] = 0;
1149 total_size = len + info_size;
1153 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1154 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1157 else status = STATUS_SUCCESS;
1159 if (type) *type = info->Type;
1160 if (count) *count = total_size - info_size;
1163 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1164 return RtlNtStatusToDosError(status);
1168 /******************************************************************************
1169 * RegQueryValueW [ADVAPI32.@]
1171 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1176 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1178 if (name && name[0])
1180 if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1182 ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1183 if (subkey != hkey) RegCloseKey( subkey );
1184 if (ret == ERROR_FILE_NOT_FOUND)
1186 /* return empty string if default value not found */
1187 if (data) *data = 0;
1188 if (count) *count = 1;
1189 ret = ERROR_SUCCESS;
1195 /******************************************************************************
1196 * RegQueryValueA [ADVAPI32.@]
1198 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1203 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1205 if (name && name[0])
1207 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1209 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1210 if (subkey != hkey) RegCloseKey( subkey );
1211 if (ret == ERROR_FILE_NOT_FOUND)
1213 /* return empty string if default value not found */
1214 if (data) *data = 0;
1215 if (count) *count = 1;
1216 ret = ERROR_SUCCESS;
1222 /******************************************************************************
1223 * RegEnumValueW [ADVAPI32.@]
1226 * hkey [I] Handle to key to query
1227 * index [I] Index of value to query
1228 * value [O] Value string
1229 * val_count [I/O] Size of value buffer (in wchars)
1230 * reserved [I] Reserved
1231 * type [O] Type code
1232 * data [O] Value data
1233 * count [I/O] Size of data buffer (in bytes)
1236 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1237 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1241 char buffer[256], *buf_ptr = buffer;
1242 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1243 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1245 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1246 hkey, index, value, val_count, reserved, type, data, count );
1248 /* NT only checks count, not val_count */
1249 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1250 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1252 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1253 if (data) total_size += *count;
1254 total_size = min( sizeof(buffer), total_size );
1256 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1257 buffer, total_size, &total_size );
1258 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1262 /* retry with a dynamically allocated buffer */
1263 while (status == STATUS_BUFFER_OVERFLOW)
1265 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1266 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1267 return ERROR_NOT_ENOUGH_MEMORY;
1268 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1269 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1270 buf_ptr, total_size, &total_size );
1273 if (status) goto done;
1277 if (info->NameLength/sizeof(WCHAR) >= *val_count)
1279 status = STATUS_BUFFER_OVERFLOW;
1282 memcpy( value, info->Name, info->NameLength );
1283 *val_count = info->NameLength / sizeof(WCHAR);
1284 value[*val_count] = 0;
1289 if (total_size - info->DataOffset > *count)
1291 status = STATUS_BUFFER_OVERFLOW;
1294 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1295 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1297 /* if the type is REG_SZ and data is not 0-terminated
1298 * and there is enough space in the buffer NT appends a \0 */
1299 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1300 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1304 else status = STATUS_SUCCESS;
1307 if (type) *type = info->Type;
1308 if (count) *count = info->DataLength;
1311 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1312 return RtlNtStatusToDosError(status);
1316 /******************************************************************************
1317 * RegEnumValueA [ADVAPI32.@]
1319 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1320 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1324 char buffer[256], *buf_ptr = buffer;
1325 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1326 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1328 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1329 hkey, index, value, val_count, reserved, type, data, count );
1331 /* NT only checks count, not val_count */
1332 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1333 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1335 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1336 if (data) total_size += *count;
1337 total_size = min( sizeof(buffer), total_size );
1339 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1340 buffer, total_size, &total_size );
1341 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1343 /* we need to fetch the contents for a string type even if not requested,
1344 * because we need to compute the length of the ASCII string. */
1345 if (value || data || is_string(info->Type))
1347 /* retry with a dynamically allocated buffer */
1348 while (status == STATUS_BUFFER_OVERFLOW)
1350 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1351 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1352 return ERROR_NOT_ENOUGH_MEMORY;
1353 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1354 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1355 buf_ptr, total_size, &total_size );
1358 if (status) goto done;
1360 if (is_string(info->Type))
1363 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1364 total_size - info->DataOffset );
1367 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1370 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1371 total_size - info->DataOffset );
1372 /* if the type is REG_SZ and data is not 0-terminated
1373 * and there is enough space in the buffer NT appends a \0 */
1374 if (len < *count && data[len-1]) data[len] = 0;
1377 info->DataLength = len;
1381 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1382 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1385 if (value && !status)
1389 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1390 if (len >= *val_count)
1392 status = STATUS_BUFFER_OVERFLOW;
1395 len = *val_count - 1;
1396 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1402 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1408 else status = STATUS_SUCCESS;
1410 if (type) *type = info->Type;
1411 if (count) *count = info->DataLength;
1414 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1415 return RtlNtStatusToDosError(status);
1420 /******************************************************************************
1421 * RegDeleteValueW [ADVAPI32.@]
1423 * See RegDeleteValueA.
1425 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1427 UNICODE_STRING nameW;
1429 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1431 RtlInitUnicodeString( &nameW, name );
1432 return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1436 /******************************************************************************
1437 * RegDeleteValueA [ADVAPI32.@]
1439 * Delete a value from the registry.
1442 * hkey [I] Registry handle of the key holding the value
1443 * name [I] Name of the value under hkey to delete
1447 * Failure: A standard Windows error code.
1449 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1454 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1456 RtlInitAnsiString( &nameA, name );
1457 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1459 status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1460 return RtlNtStatusToDosError( status );
1464 /******************************************************************************
1465 * RegLoadKeyW [ADVAPI32.@]
1468 * hkey [I] Handle of open key
1469 * subkey [I] Address of name of subkey
1470 * filename [I] Address of filename for registry information
1472 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1475 DWORD ret, len, err = GetLastError();
1478 TRACE( "(%p,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
1480 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1481 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1482 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1484 len = strlenW( subkey ) * sizeof(WCHAR);
1485 if (len > MAX_PATH*sizeof(WCHAR)) return ERROR_INVALID_PARAMETER;
1487 if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1488 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1490 ret = GetLastError();
1494 RegCreateKeyW(hkey,subkey,&shkey);
1496 SERVER_START_REQ( load_registry )
1500 wine_server_add_data( req, subkey, len );
1501 ret = RtlNtStatusToDosError( wine_server_call(req) );
1504 CloseHandle( file );
1508 SetLastError( err ); /* restore the last error code */
1513 /******************************************************************************
1514 * RegLoadKeyA [ADVAPI32.@]
1516 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1518 WCHAR buffer[MAX_PATH];
1520 DWORD ret, len, err = GetLastError();
1523 TRACE( "(%p,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
1525 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1526 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1527 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1529 if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
1530 return ERROR_INVALID_PARAMETER;
1532 if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1533 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1535 ret = GetLastError();
1539 RegCreateKeyA(hkey,subkey,&shkey);
1541 SERVER_START_REQ( load_registry )
1545 wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
1546 ret = RtlNtStatusToDosError( wine_server_call(req) );
1549 CloseHandle( file );
1553 SetLastError( err ); /* restore the last error code */
1558 /******************************************************************************
1559 * RegSaveKeyW [ADVAPI32.@]
1562 * hkey [I] Handle of key where save begins
1563 * lpFile [I] Address of filename to save to
1564 * sa [I] Address of security structure
1566 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1568 static const WCHAR format[] =
1569 {'r','e','g','%','0','4','x','.','t','m','p',0};
1570 WCHAR buffer[MAX_PATH];
1576 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
1578 if (!file || !*file) return ERROR_INVALID_PARAMETER;
1579 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1581 err = GetLastError();
1582 GetFullPathNameW( file, sizeof(buffer)/sizeof(WCHAR), buffer, &nameW );
1586 snprintfW( nameW, 16, format, count++ );
1587 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
1588 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1589 if (handle != INVALID_HANDLE_VALUE) break;
1590 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1592 /* Something gone haywire ? Please report if this happens abnormally */
1594 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);
1597 SERVER_START_REQ( save_registry )
1601 ret = RtlNtStatusToDosError( wine_server_call( req ) );
1605 CloseHandle( handle );
1608 if (!MoveFileExW( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1610 ERR( "Failed to move %s to %s\n", debugstr_w(buffer),
1612 ret = GetLastError();
1615 if (ret) DeleteFileW( buffer );
1618 SetLastError( err ); /* restore last error code */
1623 /******************************************************************************
1624 * RegSaveKeyA [ADVAPI32.@]
1626 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1628 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
1632 RtlInitAnsiString(&fileA, file);
1633 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
1634 return RtlNtStatusToDosError( status );
1635 return RegSaveKeyW(hkey, fileW->Buffer, sa);
1639 /******************************************************************************
1640 * RegRestoreKeyW [ADVAPI32.@]
1643 * hkey [I] Handle of key where restore begins
1644 * lpFile [I] Address of filename containing saved tree
1645 * dwFlags [I] Optional flags
1647 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
1649 TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
1651 /* It seems to do this check before the hkey check */
1652 if (!lpFile || !*lpFile)
1653 return ERROR_INVALID_PARAMETER;
1655 FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
1657 /* Check for file existence */
1659 return ERROR_SUCCESS;
1663 /******************************************************************************
1664 * RegRestoreKeyA [ADVAPI32.@]
1666 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
1668 UNICODE_STRING lpFileW;
1671 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
1672 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
1673 RtlFreeUnicodeString( &lpFileW );
1678 /******************************************************************************
1679 * RegUnLoadKeyW [ADVAPI32.@]
1682 * hkey [I] Handle of open key
1683 * lpSubKey [I] Address of name of subkey to unload
1685 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
1690 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
1692 ret = RegOpenKeyW(hkey,lpSubKey,&shkey);
1694 return ERROR_INVALID_PARAMETER;
1696 SERVER_START_REQ( unload_registry )
1699 ret = RtlNtStatusToDosError( wine_server_call(req) );
1708 /******************************************************************************
1709 * RegUnLoadKeyA [ADVAPI32.@]
1711 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
1713 UNICODE_STRING lpSubKeyW;
1716 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
1717 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
1718 RtlFreeUnicodeString( &lpSubKeyW );
1723 /******************************************************************************
1724 * RegReplaceKeyW [ADVAPI32.@]
1727 * hkey [I] Handle of open key
1728 * lpSubKey [I] Address of name of subkey
1729 * lpNewFile [I] Address of filename for file with new data
1730 * lpOldFile [I] Address of filename for backup file
1732 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
1735 FIXME("(%p,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
1736 debugstr_w(lpNewFile),debugstr_w(lpOldFile));
1737 return ERROR_SUCCESS;
1741 /******************************************************************************
1742 * RegReplaceKeyA [ADVAPI32.@]
1744 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
1747 UNICODE_STRING lpSubKeyW;
1748 UNICODE_STRING lpNewFileW;
1749 UNICODE_STRING lpOldFileW;
1752 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
1753 RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, lpOldFile );
1754 RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, lpNewFile );
1755 ret = RegReplaceKeyW( hkey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
1756 RtlFreeUnicodeString( &lpOldFileW );
1757 RtlFreeUnicodeString( &lpNewFileW );
1758 RtlFreeUnicodeString( &lpSubKeyW );
1763 /******************************************************************************
1764 * RegSetKeySecurity [ADVAPI32.@]
1767 * hkey [I] Open handle of key to set
1768 * SecurityInfo [I] Descriptor contents
1769 * pSecurityDesc [I] Address of descriptor for key
1771 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
1772 PSECURITY_DESCRIPTOR pSecurityDesc )
1774 TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
1776 /* It seems to perform this check before the hkey check */
1777 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
1778 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
1779 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
1780 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
1783 return ERROR_INVALID_PARAMETER;
1786 return ERROR_INVALID_PARAMETER;
1788 FIXME(":(%p,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
1790 return ERROR_SUCCESS;
1794 /******************************************************************************
1795 * RegGetKeySecurity [ADVAPI32.@]
1797 * Get a copy of the security descriptor for a given registry key.
1800 * hkey [I] Open handle of key to set
1801 * SecurityInformation [I] Descriptor contents
1802 * pSecurityDescriptor [O] Address of descriptor for key
1803 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
1806 * Success: ERROR_SUCCESS
1807 * Failure: Error code
1809 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
1810 PSECURITY_DESCRIPTOR pSecurityDescriptor,
1811 LPDWORD lpcbSecurityDescriptor )
1813 TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
1814 lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1816 /* FIXME: Check for valid SecurityInformation values */
1818 if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
1819 return ERROR_INSUFFICIENT_BUFFER;
1821 FIXME("(%p,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
1822 pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1824 /* Do not leave security descriptor filled with garbage */
1825 RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1827 return ERROR_SUCCESS;
1831 /******************************************************************************
1832 * RegFlushKey [ADVAPI32.@]
1834 * Immediately write a registry key to registry.
1837 * hkey [I] Handle of key to write
1840 * Success: ERROR_SUCCESS
1841 * Failure: Error code
1843 DWORD WINAPI RegFlushKey( HKEY hkey )
1845 hkey = get_special_root_hkey( hkey );
1846 if (!hkey) return ERROR_INVALID_HANDLE;
1848 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
1852 /******************************************************************************
1853 * RegConnectRegistryW [ADVAPI32.@]
1856 * lpMachineName [I] Address of name of remote computer
1857 * hHey [I] Predefined registry handle
1858 * phkResult [I] Address of buffer for remote registry handle
1860 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
1865 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
1867 if (!lpMachineName || !*lpMachineName) {
1868 /* Use the local machine name */
1869 ret = RegOpenKeyW( hKey, NULL, phkResult );
1871 else if (lpMachineName[0] != '\\' || lpMachineName[1] != '\\')
1872 ret = ERROR_BAD_NETPATH;
1875 WCHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1876 DWORD len = sizeof(compName) / sizeof(WCHAR);
1878 if (GetComputerNameW(compName, &len))
1880 if (!strcmpiW(lpMachineName + 2, compName))
1881 ret = RegOpenKeyW(hKey, NULL, phkResult);
1884 FIXME("Cannot connect to %s\n",debugstr_w(lpMachineName));
1885 ret = ERROR_BAD_NETPATH;
1889 ret = GetLastError();
1895 /******************************************************************************
1896 * RegConnectRegistryA [ADVAPI32.@]
1898 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, PHKEY reskey )
1900 UNICODE_STRING machineW;
1903 RtlCreateUnicodeStringFromAsciiz( &machineW, machine );
1904 ret = RegConnectRegistryW( machineW.Buffer, hkey, reskey );
1905 RtlFreeUnicodeString( &machineW );
1910 /******************************************************************************
1911 * RegNotifyChangeKeyValue [ADVAPI32.@]
1914 * hkey [I] Handle of key to watch
1915 * fWatchSubTree [I] Flag for subkey notification
1916 * fdwNotifyFilter [I] Changes to be reported
1917 * hEvent [I] Handle of signaled event
1918 * fAsync [I] Flag for asynchronous reporting
1920 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
1921 DWORD fdwNotifyFilter, HANDLE hEvent,
1926 TRACE("(%p,%i,%ld,%p,%i)\n",hkey,fWatchSubTree,fdwNotifyFilter,
1930 hEvent = CreateEventA(NULL, 0, 0, NULL);
1932 SERVER_START_REQ( set_registry_notification )
1935 req->event = hEvent;
1936 req->subtree = fWatchSubTree;
1937 req->filter = fdwNotifyFilter;
1938 ret = RtlNtStatusToDosError( wine_server_call(req) );
1944 if( ret == ERROR_SUCCESS )
1945 WaitForSingleObject( hEvent, INFINITE );
1946 CloseHandle( hEvent );
1952 /******************************************************************************
1953 * RegOpenUserClassesRoot [ADVAPI32.@]
1955 * Open the HKEY_CLASSES_ROOT key for a user.
1958 * hToken [I] Handle of token representing the user
1959 * dwOptions [I] Reserved, nust be 0
1960 * samDesired [I] Desired access rights
1961 * phkResult [O] Destination for the resulting key handle
1964 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
1965 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
1966 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
1968 LONG WINAPI RegOpenUserClassesRoot(
1975 FIXME("(%p, 0x%lx, 0x%lx, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
1977 *phkResult = HKEY_CLASSES_ROOT;
1978 return ERROR_SUCCESS;