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
34 #define WIN32_NO_STATUS
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(reg);
46 /* allowed bits for access mask */
47 #define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
49 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
50 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
51 #define NB_SPECIAL_ROOT_KEYS ((UINT)HKEY_SPECIAL_ROOT_LAST - (UINT)HKEY_SPECIAL_ROOT_FIRST + 1)
53 static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
55 static const WCHAR name_CLASSES_ROOT[] =
56 {'M','a','c','h','i','n','e','\\',
57 'S','o','f','t','w','a','r','e','\\',
58 'C','l','a','s','s','e','s',0};
59 static const WCHAR name_LOCAL_MACHINE[] =
60 {'M','a','c','h','i','n','e',0};
61 static const WCHAR name_USERS[] =
63 static const WCHAR name_PERFORMANCE_DATA[] =
64 {'P','e','r','f','D','a','t','a',0};
65 static const WCHAR name_CURRENT_CONFIG[] =
66 {'M','a','c','h','i','n','e','\\',
67 'S','y','s','t','e','m','\\',
68 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
69 'H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s','\\',
70 'C','u','r','r','e','n','t',0};
71 static const WCHAR name_DYN_DATA[] =
72 {'D','y','n','D','a','t','a',0};
74 #define DECL_STR(key) { sizeof(name_##key)-sizeof(WCHAR), sizeof(name_##key), (LPWSTR)name_##key }
75 static UNICODE_STRING root_key_names[NB_SPECIAL_ROOT_KEYS] =
77 DECL_STR(CLASSES_ROOT),
78 { 0, 0, NULL }, /* HKEY_CURRENT_USER is determined dynamically */
79 DECL_STR(LOCAL_MACHINE),
81 DECL_STR(PERFORMANCE_DATA),
82 DECL_STR(CURRENT_CONFIG),
88 /* check if value type needs string conversion (Ansi<->Unicode) */
89 inline static int is_string( DWORD type )
91 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
94 /* check if current version is NT or Win95 */
95 inline static int is_version_nt(void)
97 return !(GetVersion() & 0x80000000);
100 /* create one of the HKEY_* special root keys */
101 static HKEY create_special_root_hkey( HANDLE hkey, DWORD access )
104 int idx = (UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST;
106 if (hkey == HKEY_CURRENT_USER)
108 if (RtlOpenCurrentUser( access, &hkey )) return 0;
109 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
113 OBJECT_ATTRIBUTES attr;
115 attr.Length = sizeof(attr);
116 attr.RootDirectory = 0;
117 attr.ObjectName = &root_key_names[idx];
119 attr.SecurityDescriptor = NULL;
120 attr.SecurityQualityOfService = NULL;
121 if (NtCreateKey( &hkey, access, &attr, 0, NULL, 0, NULL )) return 0;
122 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
125 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
128 NtClose( hkey ); /* somebody beat us to it */
132 /* map the hkey from special root to normal key if necessary */
133 inline static HKEY get_special_root_hkey( HKEY hkey )
137 if ((hkey >= HKEY_SPECIAL_ROOT_FIRST) && (hkey <= HKEY_SPECIAL_ROOT_LAST))
139 if (!(ret = special_root_keys[(UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST]))
140 ret = create_special_root_hkey( hkey, KEY_ALL_ACCESS );
146 /******************************************************************************
147 * RegCreateKeyExW [ADVAPI32.@]
149 * See RegCreateKeyExA.
151 DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
152 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
153 PHKEY retkey, LPDWORD dispos )
155 OBJECT_ATTRIBUTES attr;
156 UNICODE_STRING nameW, classW;
158 if (reserved) return ERROR_INVALID_PARAMETER;
159 if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
160 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
162 attr.Length = sizeof(attr);
163 attr.RootDirectory = hkey;
164 attr.ObjectName = &nameW;
166 attr.SecurityDescriptor = NULL;
167 attr.SecurityQualityOfService = NULL;
168 RtlInitUnicodeString( &nameW, name );
169 RtlInitUnicodeString( &classW, class );
171 return RtlNtStatusToDosError( NtCreateKey( (PHANDLE)retkey, access, &attr, 0,
172 &classW, options, dispos ) );
176 /******************************************************************************
177 * RegCreateKeyExA [ADVAPI32.@]
179 * Open a registry key, creating it if it doesn't exist.
182 * hkey [I] Handle of the parent registry key
183 * name [I] Name of the new key to open or create
184 * reserved [I] Reserved, pass 0
185 * class [I] The object type of the new key
186 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
187 * access [I] Access level desired
188 * sa [I] Security attributes for the key
189 * retkey [O] Destination for the resulting handle
190 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
193 * Success: ERROR_SUCCESS.
194 * Failure: A standard Win32 error code. retkey remains untouched.
197 * MAXIMUM_ALLOWED in access mask not supported by server
199 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
200 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
201 PHKEY retkey, LPDWORD dispos )
203 OBJECT_ATTRIBUTES attr;
204 UNICODE_STRING classW;
205 ANSI_STRING nameA, classA;
208 if (reserved) return ERROR_INVALID_PARAMETER;
209 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
210 else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
211 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
213 attr.Length = sizeof(attr);
214 attr.RootDirectory = hkey;
215 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
217 attr.SecurityDescriptor = NULL;
218 attr.SecurityQualityOfService = NULL;
219 RtlInitAnsiString( &nameA, name );
220 RtlInitAnsiString( &classA, class );
222 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
225 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
227 status = NtCreateKey( (PHANDLE)retkey, access, &attr, 0, &classW, options, dispos );
228 RtlFreeUnicodeString( &classW );
231 return RtlNtStatusToDosError( status );
235 /******************************************************************************
236 * RegCreateKeyW [ADVAPI32.@]
238 * Creates the specified reg key.
241 * hKey [I] Handle to an open key.
242 * lpSubKey [I] Name of a key that will be opened or created.
243 * phkResult [O] Receives a handle to the opened or created key.
246 * Success: ERROR_SUCCESS
247 * Failure: nonzero error code defined in Winerror.h
249 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR lpSubKey, PHKEY phkResult )
251 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
252 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
253 return RegCreateKeyExW( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
254 KEY_ALL_ACCESS, NULL, phkResult, NULL );
258 /******************************************************************************
259 * RegCreateKeyA [ADVAPI32.@]
263 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR lpSubKey, PHKEY phkResult )
265 return RegCreateKeyExA( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
266 KEY_ALL_ACCESS, NULL, phkResult, NULL );
271 /******************************************************************************
272 * RegOpenKeyExW [ADVAPI32.@]
276 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
278 OBJECT_ATTRIBUTES attr;
279 UNICODE_STRING nameW;
281 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
283 attr.Length = sizeof(attr);
284 attr.RootDirectory = hkey;
285 attr.ObjectName = &nameW;
287 attr.SecurityDescriptor = NULL;
288 attr.SecurityQualityOfService = NULL;
289 RtlInitUnicodeString( &nameW, name );
290 return RtlNtStatusToDosError( NtOpenKey( (PHANDLE)retkey, access, &attr ) );
294 /******************************************************************************
295 * RegOpenKeyExA [ADVAPI32.@]
297 * Open a registry key.
300 * hkey [I] Handle of open key
301 * name [I] Name of subkey to open
302 * reserved [I] Reserved - must be zero
303 * access [I] Security access mask
304 * retkey [O] Handle to open key
307 * Success: ERROR_SUCCESS
308 * Failure: A standard Win32 error code. retkey is set to 0.
311 * Unlike RegCreateKeyExA(), this function will not create the key if it
314 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
316 OBJECT_ATTRIBUTES attr;
320 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
322 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
324 attr.Length = sizeof(attr);
325 attr.RootDirectory = hkey;
326 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
328 attr.SecurityDescriptor = NULL;
329 attr.SecurityQualityOfService = NULL;
331 RtlInitAnsiString( &nameA, name );
332 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
335 status = NtOpenKey( (PHANDLE)retkey, access, &attr );
337 return RtlNtStatusToDosError( status );
341 /******************************************************************************
342 * RegOpenKeyW [ADVAPI32.@]
346 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
351 return ERROR_SUCCESS;
353 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
357 /******************************************************************************
358 * RegOpenKeyA [ADVAPI32.@]
360 * Open a registry key.
363 * hkey [I] Handle of parent key to open the new key under
364 * name [I] Name of the key under hkey to open
365 * retkey [O] Destination for the resulting Handle
368 * Success: ERROR_SUCCESS
369 * Failure: A standard Win32 error code. retkey is set to 0.
371 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
376 return ERROR_SUCCESS;
378 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
382 /******************************************************************************
383 * RegOpenCurrentUser [ADVAPI32.@]
385 * Get a handle to the HKEY_CURRENT_USER key for the user
386 * the current thread is impersonating.
389 * access [I] Desired access rights to the key
390 * retkey [O] Handle to the opened key
393 * Success: ERROR_SUCCESS
394 * Failure: nonzero error code from Winerror.h
397 * This function is supposed to retrieve a handle to the
398 * HKEY_CURRENT_USER for the user the current thread is impersonating.
399 * Since Wine does not currently allow threads to impersonate other users,
400 * this stub should work fine.
402 DWORD WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
404 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
409 /******************************************************************************
410 * RegEnumKeyExW [ADVAPI32.@]
412 * Enumerate subkeys of the specified open registry key.
415 * hkey [I] Handle to key to enumerate
416 * index [I] Index of subkey to enumerate
417 * name [O] Buffer for subkey name
418 * name_len [O] Size of subkey buffer
419 * reserved [I] Reserved
420 * class [O] Buffer for class string
421 * class_len [O] Size of class buffer
422 * ft [O] Time key last written to
425 * Success: ERROR_SUCCESS
426 * Failure: System error code. If there are no more subkeys available, the
427 * function returns ERROR_NO_MORE_ITEMS.
429 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
430 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
433 char buffer[256], *buf_ptr = buffer;
434 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
437 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
438 name_len ? *name_len : -1, reserved, class, class_len, ft );
440 if (reserved) return ERROR_INVALID_PARAMETER;
441 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
443 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
444 buffer, sizeof(buffer), &total_size );
446 while (status == STATUS_BUFFER_OVERFLOW)
448 /* retry with a dynamically allocated buffer */
449 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
450 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
451 return ERROR_NOT_ENOUGH_MEMORY;
452 info = (KEY_NODE_INFORMATION *)buf_ptr;
453 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
454 buf_ptr, total_size, &total_size );
459 DWORD len = info->NameLength / sizeof(WCHAR);
460 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
462 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
464 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
465 status = STATUS_BUFFER_OVERFLOW;
469 memcpy( name, info->Name, info->NameLength );
473 *class_len = cls_len;
476 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
483 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
484 return RtlNtStatusToDosError( status );
488 /******************************************************************************
489 * RegEnumKeyExA [ADVAPI32.@]
493 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
494 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
497 char buffer[256], *buf_ptr = buffer;
498 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
501 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
502 name_len ? *name_len : -1, reserved, class, class_len, ft );
504 if (reserved) return ERROR_INVALID_PARAMETER;
505 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
507 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
508 buffer, sizeof(buffer), &total_size );
510 while (status == STATUS_BUFFER_OVERFLOW)
512 /* retry with a dynamically allocated buffer */
513 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
514 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
515 return ERROR_NOT_ENOUGH_MEMORY;
516 info = (KEY_NODE_INFORMATION *)buf_ptr;
517 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
518 buf_ptr, total_size, &total_size );
525 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
526 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
528 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
530 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
531 status = STATUS_BUFFER_OVERFLOW;
535 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
539 *class_len = cls_len;
542 RtlUnicodeToMultiByteN( class, cls_len, NULL,
543 (WCHAR *)(buf_ptr + info->ClassOffset),
551 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
552 return RtlNtStatusToDosError( status );
556 /******************************************************************************
557 * RegEnumKeyW [ADVAPI32.@]
559 * Enumerates subkyes of the specified open reg key.
562 * hKey [I] Handle to an open key.
563 * dwIndex [I] Index of the subkey of hKey to retrieve.
564 * lpName [O] Name of the subkey.
565 * cchName [I] Size of lpName in TCHARS.
568 * Success: ERROR_SUCCESS
569 * Failure: system error code. If there are no more subkeys available, the
570 * function returns ERROR_NO_MORE_ITEMS.
572 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
574 return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
578 /******************************************************************************
579 * RegEnumKeyA [ADVAPI32.@]
583 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
585 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
589 /******************************************************************************
590 * RegQueryInfoKeyW [ADVAPI32.@]
592 * Retrieves information about the specified registry key.
595 * hkey [I] Handle to key to query
596 * class [O] Buffer for class string
597 * class_len [O] Size of class string buffer
598 * reserved [I] Reserved
599 * subkeys [O] Buffer for number of subkeys
600 * max_subkey [O] Buffer for longest subkey name length
601 * max_class [O] Buffer for longest class string length
602 * values [O] Buffer for number of value entries
603 * max_value [O] Buffer for longest value name length
604 * max_data [O] Buffer for longest value data length
605 * security [O] Buffer for security descriptor length
606 * modif [O] Modification time
609 * Success: ERROR_SUCCESS
610 * Failure: system error code.
613 * - win95 allows class to be valid and class_len to be NULL
614 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
615 * - both allow class to be NULL and class_len to be NULL
616 * (it's hard to test validity, so test !NULL instead)
618 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
619 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
620 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
621 LPDWORD security, FILETIME *modif )
624 char buffer[256], *buf_ptr = buffer;
625 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
628 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
629 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
631 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
632 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
634 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
635 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
639 /* retry with a dynamically allocated buffer */
640 while (status == STATUS_BUFFER_OVERFLOW)
642 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
643 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
644 return ERROR_NOT_ENOUGH_MEMORY;
645 info = (KEY_FULL_INFORMATION *)buf_ptr;
646 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
649 if (status) goto done;
651 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
653 status = STATUS_BUFFER_OVERFLOW;
657 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
658 class[info->ClassLength/sizeof(WCHAR)] = 0;
661 else status = STATUS_SUCCESS;
663 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
664 if (subkeys) *subkeys = info->SubKeys;
665 if (max_subkey) *max_subkey = info->MaxNameLen;
666 if (max_class) *max_class = info->MaxClassLen;
667 if (values) *values = info->Values;
668 if (max_value) *max_value = info->MaxValueNameLen;
669 if (max_data) *max_data = info->MaxValueDataLen;
670 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
673 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
674 return RtlNtStatusToDosError( status );
678 /******************************************************************************
679 * RegQueryMultipleValuesA [ADVAPI32.@]
681 * Retrieves the type and data for a list of value names associated with a key.
684 * hKey [I] Handle to an open key.
685 * val_list [O] Array of VALENT structures that describes the entries.
686 * num_vals [I] Number of elements in val_list.
687 * lpValueBuf [O] Pointer to a buffer that receives the data for each value.
688 * ldwTotsize [I/O] Size of lpValueBuf.
691 * Success: ERROR_SUCCESS. ldwTotsize contains num bytes copied.
692 * Failure: nonzero error code from Winerror.h ldwTotsize contains num needed
695 DWORD WINAPI RegQueryMultipleValuesA(HKEY hkey, PVALENTA val_list, DWORD num_vals,
696 LPSTR lpValueBuf, LPDWORD ldwTotsize)
699 DWORD maxBytes = *ldwTotsize;
701 LPSTR bufptr = lpValueBuf;
704 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
706 for(i=0; i < num_vals; ++i)
709 val_list[i].ve_valuelen=0;
710 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
711 if(status != ERROR_SUCCESS)
716 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
718 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
719 (LPBYTE)bufptr, &val_list[i].ve_valuelen);
720 if(status != ERROR_SUCCESS)
725 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
727 bufptr += val_list[i].ve_valuelen;
730 *ldwTotsize += val_list[i].ve_valuelen;
732 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
736 /******************************************************************************
737 * RegQueryMultipleValuesW [ADVAPI32.@]
739 * See RegQueryMultipleValuesA.
741 DWORD WINAPI RegQueryMultipleValuesW(HKEY hkey, PVALENTW val_list, DWORD num_vals,
742 LPWSTR lpValueBuf, LPDWORD ldwTotsize)
745 DWORD maxBytes = *ldwTotsize;
747 LPSTR bufptr = (LPSTR)lpValueBuf;
750 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
752 for(i=0; i < num_vals; ++i)
754 val_list[i].ve_valuelen=0;
755 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
756 if(status != ERROR_SUCCESS)
761 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
763 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
764 (LPBYTE)bufptr, &val_list[i].ve_valuelen);
765 if(status != ERROR_SUCCESS)
770 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
772 bufptr += val_list[i].ve_valuelen;
775 *ldwTotsize += val_list[i].ve_valuelen;
777 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
780 /******************************************************************************
781 * RegQueryInfoKeyA [ADVAPI32.@]
783 * Retrieves information about a registry key.
786 * hKey [I] Handle to an open key.
787 * lpClass [O] Class string of the key.
788 * lpcClass [I/O] size of lpClass.
789 * lpReserved [I] Reserved; must be NULL.
790 * lpcSubKeys [O] Number of subkeys contained by the key.
791 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
792 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
794 * lpcValues [O] Number of values associated with the key.
795 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
796 * lpcMaxValueLen [O] Longest data component among the key's values
797 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
798 * lpftLastWriteTime [O] FILETIME strucutre that is the last write time.
801 * Success: ERROR_SUCCESS
802 * Failure: nonzero error code from Winerror.h
804 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
805 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
806 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
807 LPDWORD security, FILETIME *modif )
810 char buffer[256], *buf_ptr = buffer;
811 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
812 DWORD total_size, len;
814 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
815 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
817 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
818 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
820 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
821 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
823 if (class || class_len)
825 /* retry with a dynamically allocated buffer */
826 while (status == STATUS_BUFFER_OVERFLOW)
828 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
829 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
830 return ERROR_NOT_ENOUGH_MEMORY;
831 info = (KEY_FULL_INFORMATION *)buf_ptr;
832 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
835 if (status) goto done;
837 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
840 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
843 if (class && !status)
845 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
850 else status = STATUS_SUCCESS;
852 if (subkeys) *subkeys = info->SubKeys;
853 if (max_subkey) *max_subkey = info->MaxNameLen;
854 if (max_class) *max_class = info->MaxClassLen;
855 if (values) *values = info->Values;
856 if (max_value) *max_value = info->MaxValueNameLen;
857 if (max_data) *max_data = info->MaxValueDataLen;
858 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
861 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
862 return RtlNtStatusToDosError( status );
866 /******************************************************************************
867 * RegCloseKey [ADVAPI32.@]
869 * Close an open registry key.
872 * hkey [I] Handle of key to close
875 * Success: ERROR_SUCCESS
876 * Failure: Error code
878 DWORD WINAPI RegCloseKey( HKEY hkey )
880 if (!hkey) return ERROR_INVALID_HANDLE;
881 if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
882 return RtlNtStatusToDosError( NtClose( hkey ) );
886 /******************************************************************************
887 * RegDeleteKeyW [ADVAPI32.@]
891 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
896 if (!name) return ERROR_INVALID_PARAMETER;
898 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
900 if (!(ret = RegOpenKeyExW( hkey, name, 0, DELETE, &tmp )))
902 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
905 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
910 /******************************************************************************
911 * RegDeleteKeyA [ADVAPI32.@]
913 * Delete a registry key.
916 * hkey [I] Handle to parent key containing the key to delete
917 * name [I] Name of the key user hkey to delete
921 * MSDN is wrong when it says that hkey must be opened with the DELETE access
922 * right. In reality, it opens a new handle with DELETE access.
925 * Success: ERROR_SUCCESS
926 * Failure: Error code
928 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
933 if (!name) return ERROR_INVALID_PARAMETER;
935 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
937 if (!(ret = RegOpenKeyExA( hkey, name, 0, DELETE, &tmp )))
939 if (!is_version_nt()) /* win95 does recursive key deletes */
943 while(!RegEnumKeyA(tmp, 0, name, sizeof(name)))
945 if(RegDeleteKeyA(tmp, name)) /* recurse */
949 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
952 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
958 /******************************************************************************
959 * RegSetValueExW [ADVAPI32.@]
961 * Set the data and contents of a registry value.
964 * hkey [I] Handle of key to set value for
965 * name [I] Name of value to set
966 * reserved [I] Reserved, must be zero
967 * type [I] Type of the value being set
968 * data [I] The new contents of the value to set
969 * count [I] Size of data
972 * Success: ERROR_SUCCESS
973 * Failure: Error code
975 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
976 DWORD type, CONST BYTE *data, DWORD count )
978 UNICODE_STRING nameW;
980 /* no need for version check, not implemented on win9x anyway */
981 if (count && is_string(type))
983 LPCWSTR str = (LPCWSTR)data;
984 /* if user forgot to count terminating null, add it (yes NT does this) */
985 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
986 count += sizeof(WCHAR);
988 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
990 RtlInitUnicodeString( &nameW, name );
991 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
995 /******************************************************************************
996 * RegSetValueExA [ADVAPI32.@]
998 * See RegSetValueExW.
1001 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1002 * NT does definitely care (aj)
1004 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1005 CONST BYTE *data, DWORD count )
1008 WCHAR *dataW = NULL;
1011 if (!is_version_nt()) /* win95 */
1015 if (!data) return ERROR_INVALID_PARAMETER;
1016 count = strlen((const char *)data) + 1;
1019 else if (count && is_string(type))
1021 /* if user forgot to count terminating null, add it (yes NT does this) */
1022 if (data[count-1] && !data[count]) count++;
1025 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1027 if (is_string( type )) /* need to convert to Unicode */
1030 RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1031 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
1032 RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1034 data = (BYTE *)dataW;
1037 RtlInitAnsiString( &nameA, name );
1038 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1041 status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
1043 HeapFree( GetProcessHeap(), 0, dataW );
1044 return RtlNtStatusToDosError( status );
1048 /******************************************************************************
1049 * RegSetValueW [ADVAPI32.@]
1051 * Sets the data for the default or unnamed value of a reg key.
1054 * hKey [I] Handle to an open key.
1055 * lpSubKey [I] Name of a subkey of hKey.
1056 * dwType [I] Type of information to store.
1057 * lpData [I] String that contains the data to set for the default value.
1058 * cbData [I] Size of lpData.
1061 * Success: ERROR_SUCCESS
1062 * Failure: nonzero error code from Winerror.h
1064 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
1069 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
1071 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1073 if (name && name[0]) /* need to create the subkey */
1075 if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1078 ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (const BYTE*)data,
1079 (strlenW( data ) + 1) * sizeof(WCHAR) );
1080 if (subkey != hkey) RegCloseKey( subkey );
1085 /******************************************************************************
1086 * RegSetValueA [ADVAPI32.@]
1090 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
1095 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
1097 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1099 if (name && name[0]) /* need to create the subkey */
1101 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1103 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (const BYTE*)data, strlen(data)+1 );
1104 if (subkey != hkey) RegCloseKey( subkey );
1110 /******************************************************************************
1111 * RegQueryValueExW [ADVAPI32.@]
1113 * See RegQueryValueExA.
1115 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1116 LPBYTE data, LPDWORD count )
1119 UNICODE_STRING name_str;
1121 char buffer[256], *buf_ptr = buffer;
1122 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1123 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1125 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1126 hkey, debugstr_w(name), reserved, type, data, count,
1127 (count && data) ? *count : 0 );
1129 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1130 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1132 RtlInitUnicodeString( &name_str, name );
1134 if (data) total_size = min( sizeof(buffer), *count + info_size );
1135 else total_size = info_size;
1137 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1138 buffer, total_size, &total_size );
1139 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1143 /* retry with a dynamically allocated buffer */
1144 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1146 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1147 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1148 return ERROR_NOT_ENOUGH_MEMORY;
1149 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1150 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1151 buf_ptr, total_size, &total_size );
1156 memcpy( data, buf_ptr + info_size, total_size - info_size );
1157 /* if the type is REG_SZ and data is not 0-terminated
1158 * and there is enough space in the buffer NT appends a \0 */
1159 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1161 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1162 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1165 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1167 else status = STATUS_SUCCESS;
1169 if (type) *type = info->Type;
1170 if (count) *count = total_size - info_size;
1173 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1174 return RtlNtStatusToDosError(status);
1178 /******************************************************************************
1179 * RegQueryValueExA [ADVAPI32.@]
1181 * Get the type and contents of a specified value under with a key.
1184 * hkey [I] Handle of the key to query
1185 * name [I] Name of value under hkey to query
1186 * reserved [I] Reserved - must be NULL
1187 * type [O] Destination for the value type, or NULL if not required
1188 * data [O] Destination for the values contents, or NULL if not required
1189 * count [I/O] Size of data, updated with the number of bytes returned
1192 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1193 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1194 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1195 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1198 * MSDN states that if data is too small it is partially filled. In reality
1199 * it remains untouched.
1201 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
1202 LPBYTE data, LPDWORD count )
1207 char buffer[256], *buf_ptr = buffer;
1208 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1209 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1211 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1212 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1214 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1215 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1217 RtlInitAnsiString( &nameA, name );
1218 if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1220 return RtlNtStatusToDosError(status);
1222 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1223 KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
1224 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1226 /* we need to fetch the contents for a string type even if not requested,
1227 * because we need to compute the length of the ASCII string. */
1228 if (data || is_string(info->Type))
1230 /* retry with a dynamically allocated buffer */
1231 while (status == STATUS_BUFFER_OVERFLOW)
1233 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1234 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1236 status = STATUS_NO_MEMORY;
1239 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1240 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1241 KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1244 if (status) goto done;
1246 if (is_string(info->Type))
1250 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1251 total_size - info_size );
1254 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1257 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1258 total_size - info_size );
1259 /* if the type is REG_SZ and data is not 0-terminated
1260 * and there is enough space in the buffer NT appends a \0 */
1261 if (len < *count && data[len-1]) data[len] = 0;
1264 total_size = len + info_size;
1268 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1269 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1272 else status = STATUS_SUCCESS;
1274 if (type) *type = info->Type;
1275 if (count) *count = total_size - info_size;
1278 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1279 return RtlNtStatusToDosError(status);
1283 /******************************************************************************
1284 * RegQueryValueW [ADVAPI32.@]
1286 * Retrieves the data associated with the default or unnamed value of a key.
1289 * hkey [I] Handle to an open key.
1290 * name [I] Name of the subkey of hKey.
1291 * data [O] Receives the string associated with the default value
1293 * count [I/O] Size of lpValue in bytes.
1296 * Success: ERROR_SUCCESS
1297 * Failure: nonzero error code from Winerror.h
1299 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1304 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1306 if (name && name[0])
1308 if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1310 ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, (LPDWORD)count );
1311 if (subkey != hkey) RegCloseKey( subkey );
1312 if (ret == ERROR_FILE_NOT_FOUND)
1314 /* return empty string if default value not found */
1315 if (data) *data = 0;
1316 if (count) *count = sizeof(WCHAR);
1317 ret = ERROR_SUCCESS;
1323 /******************************************************************************
1324 * RegQueryValueA [ADVAPI32.@]
1326 * See RegQueryValueW.
1328 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1333 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1335 if (name && name[0])
1337 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1339 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, (LPDWORD)count );
1340 if (subkey != hkey) RegCloseKey( subkey );
1341 if (ret == ERROR_FILE_NOT_FOUND)
1343 /* return empty string if default value not found */
1344 if (data) *data = 0;
1345 if (count) *count = 1;
1346 ret = ERROR_SUCCESS;
1352 /******************************************************************************
1353 * ADVAPI_ApplyRestrictions [internal]
1355 * Helper function for RegGetValueA/W.
1357 VOID ADVAPI_ApplyRestrictions( DWORD dwFlags, DWORD dwType, DWORD cbData,
1360 /* Check if the type is restricted by the passed flags */
1361 if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1367 case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1368 case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1369 case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1370 case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1371 case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1372 case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1373 case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1376 if (dwFlags & dwMask)
1378 /* Type is not restricted, check for size mismatch */
1379 if (dwType == REG_BINARY)
1383 if ((dwFlags & RRF_RT_DWORD) == RRF_RT_DWORD)
1385 else if ((dwFlags & RRF_RT_DWORD) == RRF_RT_QWORD)
1388 if (cbExpect && cbData != cbExpect)
1389 *ret = ERROR_DATATYPE_MISMATCH;
1392 else *ret = ERROR_UNSUPPORTED_TYPE;
1397 /******************************************************************************
1398 * RegGetValueW [ADVAPI32.@]
1400 * Retrieves the type and data for a value name associated with a key
1401 * optionally expanding it's content and restricting it's type.
1404 * hKey [I] Handle to an open key.
1405 * pszSubKey [I] Name of the subkey of hKey.
1406 * pszValue [I] Name of value under hKey/szSubKey to query.
1407 * dwFlags [I] Flags restricting the value type to retrieve.
1408 * pdwType [O] Destination for the values type, may be NULL.
1409 * pvData [O] Destination for the values content, may be NULL.
1410 * pcbData [I/O] Size of pvData, updated with the size required to
1411 * retrieve the whole content.
1414 * Success: ERROR_SUCCESS
1415 * Failure: nonzero error code from Winerror.h
1418 * - Unless RRF_NOEXPAND is specified REG_EXPAND_SZ is automatically expanded
1419 * and REG_SZ is retrieved instead.
1420 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1421 * without RRF_NOEXPAND is thus not allowed.
1423 LONG WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1424 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1427 DWORD dwType, cbData = pcbData ? *pcbData : 0;
1431 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1432 hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1433 pvData, pcbData, cbData);
1435 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND))
1436 return ERROR_INVALID_PARAMETER;
1438 if (pszSubKey && pszSubKey[0])
1440 ret = RegOpenKeyExW(hKey, pszSubKey, 0, KEY_QUERY_VALUE, &hKey);
1441 if (ret != ERROR_SUCCESS) return ret;
1444 ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1446 /* If we are going to expand we need to read in the whole the value even
1447 * if the passed buffer was too small as the expanded string might be
1448 * smaller than the unexpanded one and could fit into cbData bytes. */
1449 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1450 (dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND)))
1453 if (pvBuf) HeapFree(GetProcessHeap(), 0, pvBuf);
1455 pvBuf = HeapAlloc(GetProcessHeap(), 0, cbData);
1458 ret = ERROR_NOT_ENOUGH_MEMORY;
1462 if (ret == ERROR_MORE_DATA)
1463 ret = RegQueryValueExW(hKey, pszValue, NULL,
1464 &dwType, pvBuf, &cbData);
1467 /* Even if cbData was large enough we have to copy the
1468 * string since ExpandEnvironmentStrings can't handle
1469 * overlapping buffers. */
1470 CopyMemory(pvBuf, pvData, cbData);
1473 /* Both the type or the value itself could have been modified in
1474 * between so we have to keep retrying until the buffer is large
1475 * enough or we no longer have to expand the value. */
1476 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1478 if (ret == ERROR_SUCCESS)
1480 if (dwType == REG_EXPAND_SZ)
1482 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1483 pcbData ? *pcbData : 0);
1485 if(pcbData && cbData > *pcbData)
1486 ret = ERROR_MORE_DATA;
1489 CopyMemory(pvData, pvBuf, *pcbData);
1492 if (pvBuf) HeapFree(GetProcessHeap(), 0, pvBuf);
1495 if (pszSubKey && pszSubKey[0])
1498 ADVAPI_ApplyRestrictions(dwFlags, dwType, cbData, &ret);
1500 if (pcbData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1501 ZeroMemory(pvData, *pcbData);
1503 if (pdwType) *pdwType = dwType;
1504 if (pcbData) *pcbData = cbData;
1510 /******************************************************************************
1511 * RegGetValueA [ADVAPI32.@]
1515 LONG WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
1516 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1519 DWORD dwType, cbData = pcbData ? *pcbData : 0;
1523 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1524 hKey, pszSubKey, pszValue, dwFlags, pdwType, pvData, pcbData,
1527 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND))
1528 return ERROR_INVALID_PARAMETER;
1530 if (pszSubKey && pszSubKey[0])
1532 ret = RegOpenKeyExA(hKey, pszSubKey, 0, KEY_QUERY_VALUE, &hKey);
1533 if (ret != ERROR_SUCCESS) return ret;
1536 ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1538 /* If we are going to expand we need to read in the whole the value even
1539 * if the passed buffer was too small as the expanded string might be
1540 * smaller than the unexpanded one and could fit into cbData bytes. */
1541 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1542 (dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND)))
1545 if (pvBuf) HeapFree(GetProcessHeap(), 0, pvBuf);
1547 pvBuf = HeapAlloc(GetProcessHeap(), 0, cbData);
1550 ret = ERROR_NOT_ENOUGH_MEMORY;
1554 if (ret == ERROR_MORE_DATA)
1555 ret = RegQueryValueExA(hKey, pszValue, NULL,
1556 &dwType, pvBuf, &cbData);
1559 /* Even if cbData was large enough we have to copy the
1560 * string since ExpandEnvironmentStrings can't handle
1561 * overlapping buffers. */
1562 CopyMemory(pvBuf, pvData, cbData);
1565 /* Both the type or the value itself could have been modified in
1566 * between so we have to keep retrying until the buffer is large
1567 * enough or we no longer have to expand the value. */
1568 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1570 if (ret == ERROR_SUCCESS)
1572 if (dwType == REG_EXPAND_SZ)
1574 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
1575 pcbData ? *pcbData : 0);
1577 if(pcbData && cbData > *pcbData)
1578 ret = ERROR_MORE_DATA;
1581 CopyMemory(pvData, pvBuf, *pcbData);
1584 if (pvBuf) HeapFree(GetProcessHeap(), 0, pvBuf);
1587 if (pszSubKey && pszSubKey[0])
1590 ADVAPI_ApplyRestrictions(dwFlags, dwType, cbData, &ret);
1592 if (pcbData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1593 ZeroMemory(pvData, *pcbData);
1595 if (pdwType) *pdwType = dwType;
1596 if (pcbData) *pcbData = cbData;
1602 /******************************************************************************
1603 * RegEnumValueW [ADVAPI32.@]
1605 * Enumerates the values for the specified open registry key.
1608 * hkey [I] Handle to key to query
1609 * index [I] Index of value to query
1610 * value [O] Value string
1611 * val_count [I/O] Size of value buffer (in wchars)
1612 * reserved [I] Reserved
1613 * type [O] Type code
1614 * data [O] Value data
1615 * count [I/O] Size of data buffer (in bytes)
1618 * Success: ERROR_SUCCESS
1619 * Failure: nonzero error code from Winerror.h
1622 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1623 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1627 char buffer[256], *buf_ptr = buffer;
1628 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1629 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1631 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1632 hkey, index, value, val_count, reserved, type, data, count );
1634 /* NT only checks count, not val_count */
1635 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1636 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1638 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1639 if (data) total_size += *count;
1640 total_size = min( sizeof(buffer), total_size );
1642 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1643 buffer, total_size, &total_size );
1644 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1648 /* retry with a dynamically allocated buffer */
1649 while (status == STATUS_BUFFER_OVERFLOW)
1651 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1652 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1653 return ERROR_NOT_ENOUGH_MEMORY;
1654 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1655 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1656 buf_ptr, total_size, &total_size );
1659 if (status) goto done;
1663 if (info->NameLength/sizeof(WCHAR) >= *val_count)
1665 status = STATUS_BUFFER_OVERFLOW;
1668 memcpy( value, info->Name, info->NameLength );
1669 *val_count = info->NameLength / sizeof(WCHAR);
1670 value[*val_count] = 0;
1675 if (total_size - info->DataOffset > *count)
1677 status = STATUS_BUFFER_OVERFLOW;
1680 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1681 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1683 /* if the type is REG_SZ and data is not 0-terminated
1684 * and there is enough space in the buffer NT appends a \0 */
1685 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1686 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1690 else status = STATUS_SUCCESS;
1693 if (type) *type = info->Type;
1694 if (count) *count = info->DataLength;
1697 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1698 return RtlNtStatusToDosError(status);
1702 /******************************************************************************
1703 * RegEnumValueA [ADVAPI32.@]
1705 * See RegEnumValueW.
1707 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1708 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1712 char buffer[256], *buf_ptr = buffer;
1713 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1714 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1716 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1717 hkey, index, value, val_count, reserved, type, data, count );
1719 /* NT only checks count, not val_count */
1720 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1721 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1723 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1724 if (data) total_size += *count;
1725 total_size = min( sizeof(buffer), total_size );
1727 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1728 buffer, total_size, &total_size );
1729 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1731 /* we need to fetch the contents for a string type even if not requested,
1732 * because we need to compute the length of the ASCII string. */
1733 if (value || data || is_string(info->Type))
1735 /* retry with a dynamically allocated buffer */
1736 while (status == STATUS_BUFFER_OVERFLOW)
1738 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1739 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1740 return ERROR_NOT_ENOUGH_MEMORY;
1741 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1742 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1743 buf_ptr, total_size, &total_size );
1746 if (status) goto done;
1748 if (is_string(info->Type))
1751 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1752 total_size - info->DataOffset );
1755 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1758 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1759 total_size - info->DataOffset );
1760 /* if the type is REG_SZ and data is not 0-terminated
1761 * and there is enough space in the buffer NT appends a \0 */
1762 if (len < *count && data[len-1]) data[len] = 0;
1765 info->DataLength = len;
1769 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1770 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1773 if (value && !status)
1777 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1778 if (len >= *val_count)
1780 status = STATUS_BUFFER_OVERFLOW;
1783 len = *val_count - 1;
1784 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1790 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1796 else status = STATUS_SUCCESS;
1798 if (type) *type = info->Type;
1799 if (count) *count = info->DataLength;
1802 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1803 return RtlNtStatusToDosError(status);
1808 /******************************************************************************
1809 * RegDeleteValueW [ADVAPI32.@]
1811 * See RegDeleteValueA.
1813 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1815 UNICODE_STRING nameW;
1817 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1819 RtlInitUnicodeString( &nameW, name );
1820 return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1824 /******************************************************************************
1825 * RegDeleteValueA [ADVAPI32.@]
1827 * Delete a value from the registry.
1830 * hkey [I] Registry handle of the key holding the value
1831 * name [I] Name of the value under hkey to delete
1834 * Success: ERROR_SUCCESS
1835 * Failure: nonzero error code from Winerror.h
1837 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1842 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1844 RtlInitAnsiString( &nameA, name );
1845 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1847 status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1848 return RtlNtStatusToDosError( status );
1852 /******************************************************************************
1853 * RegLoadKeyW [ADVAPI32.@]
1855 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
1856 * registration information from a specified file into that subkey.
1859 * hkey [I] Handle of open key
1860 * subkey [I] Address of name of subkey
1861 * filename [I] Address of filename for registry information
1864 * Success: ERROR_SUCCES
1865 * Failure: nonzero error code from Winerror.h
1867 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1869 OBJECT_ATTRIBUTES destkey, file;
1870 UNICODE_STRING subkeyW, filenameW;
1872 if (!(hkey = get_special_root_hkey(hkey))) return ERROR_INVALID_HANDLE;
1874 destkey.Length = sizeof(destkey);
1875 destkey.RootDirectory = hkey; /* root key: HKLM or HKU */
1876 destkey.ObjectName = &subkeyW; /* name of the key */
1877 destkey.Attributes = 0;
1878 destkey.SecurityDescriptor = NULL;
1879 destkey.SecurityQualityOfService = NULL;
1880 RtlInitUnicodeString(&subkeyW, subkey);
1882 file.Length = sizeof(file);
1883 file.RootDirectory = NULL;
1884 file.ObjectName = &filenameW; /* file containing the hive */
1885 file.Attributes = OBJ_CASE_INSENSITIVE;
1886 file.SecurityDescriptor = NULL;
1887 file.SecurityQualityOfService = NULL;
1888 RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
1890 return RtlNtStatusToDosError( NtLoadKey(&destkey, &file) );
1894 /******************************************************************************
1895 * RegLoadKeyA [ADVAPI32.@]
1899 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1901 UNICODE_STRING subkeyW, filenameW;
1902 STRING subkeyA, filenameA;
1905 RtlInitAnsiString(&subkeyA, subkey);
1906 RtlInitAnsiString(&filenameA, filename);
1908 if ((status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)))
1909 return RtlNtStatusToDosError(status);
1911 if ((status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
1912 return RtlNtStatusToDosError(status);
1914 return RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
1918 /******************************************************************************
1919 * RegSaveKeyW [ADVAPI32.@]
1921 * Save a key and all of its subkeys and values to a new file in the standard format.
1924 * hkey [I] Handle of key where save begins
1925 * lpFile [I] Address of filename to save to
1926 * sa [I] Address of security structure
1929 * Success: ERROR_SUCCESS
1930 * Failure: nonzero error code from Winerror.h
1932 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1934 static const WCHAR format[] =
1935 {'r','e','g','%','0','4','x','.','t','m','p',0};
1936 WCHAR buffer[MAX_PATH];
1942 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
1944 if (!file || !*file) return ERROR_INVALID_PARAMETER;
1945 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1947 err = GetLastError();
1948 GetFullPathNameW( file, sizeof(buffer)/sizeof(WCHAR), buffer, &nameW );
1952 snprintfW( nameW, 16, format, count++ );
1953 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
1954 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1955 if (handle != INVALID_HANDLE_VALUE) break;
1956 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1958 /* Something gone haywire ? Please report if this happens abnormally */
1960 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);
1963 ret = RtlNtStatusToDosError(NtSaveKey(hkey, handle));
1965 CloseHandle( handle );
1968 if (!MoveFileExW( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1970 ERR( "Failed to move %s to %s\n", debugstr_w(buffer),
1972 ret = GetLastError();
1975 if (ret) DeleteFileW( buffer );
1978 SetLastError( err ); /* restore last error code */
1983 /******************************************************************************
1984 * RegSaveKeyA [ADVAPI32.@]
1988 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1990 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
1994 RtlInitAnsiString(&fileA, file);
1995 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
1996 return RtlNtStatusToDosError( status );
1997 return RegSaveKeyW(hkey, fileW->Buffer, sa);
2001 /******************************************************************************
2002 * RegRestoreKeyW [ADVAPI32.@]
2004 * Read the registry information from a file and copy it over a key.
2007 * hkey [I] Handle of key where restore begins
2008 * lpFile [I] Address of filename containing saved tree
2009 * dwFlags [I] Optional flags
2012 * Success: ERROR_SUCCESS
2013 * Failure: nonzero error code from Winerror.h
2015 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2017 TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
2019 /* It seems to do this check before the hkey check */
2020 if (!lpFile || !*lpFile)
2021 return ERROR_INVALID_PARAMETER;
2023 FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2025 /* Check for file existence */
2027 return ERROR_SUCCESS;
2031 /******************************************************************************
2032 * RegRestoreKeyA [ADVAPI32.@]
2034 * See RegRestoreKeyW.
2036 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2038 UNICODE_STRING lpFileW;
2041 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2042 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2043 RtlFreeUnicodeString( &lpFileW );
2048 /******************************************************************************
2049 * RegUnLoadKeyW [ADVAPI32.@]
2051 * Unload a registry key and its subkeys from the registry.
2054 * hkey [I] Handle of open key
2055 * lpSubKey [I] Address of name of subkey to unload
2058 * Success: ERROR_SUCCESS
2059 * Failure: nonzero error code from Winerror.h
2061 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2066 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2068 ret = RegOpenKeyW(hkey,lpSubKey,&shkey);
2070 return ERROR_INVALID_PARAMETER;
2072 ret = RtlNtStatusToDosError(NtUnloadKey(shkey));
2080 /******************************************************************************
2081 * RegUnLoadKeyA [ADVAPI32.@]
2083 * See RegUnLoadKeyW.
2085 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2087 UNICODE_STRING lpSubKeyW;
2090 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2091 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2092 RtlFreeUnicodeString( &lpSubKeyW );
2097 /******************************************************************************
2098 * RegReplaceKeyW [ADVAPI32.@]
2100 * Replace the file backing a registry key and all its subkeys with another file.
2103 * hkey [I] Handle of open key
2104 * lpSubKey [I] Address of name of subkey
2105 * lpNewFile [I] Address of filename for file with new data
2106 * lpOldFile [I] Address of filename for backup file
2109 * Success: ERROR_SUCCESS
2110 * Failure: nonzero error code from Winerror.h
2112 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
2115 FIXME("(%p,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
2116 debugstr_w(lpNewFile),debugstr_w(lpOldFile));
2117 return ERROR_SUCCESS;
2121 /******************************************************************************
2122 * RegReplaceKeyA [ADVAPI32.@]
2124 * See RegReplaceKeyW.
2126 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
2129 UNICODE_STRING lpSubKeyW;
2130 UNICODE_STRING lpNewFileW;
2131 UNICODE_STRING lpOldFileW;
2134 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2135 RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, lpOldFile );
2136 RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, lpNewFile );
2137 ret = RegReplaceKeyW( hkey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
2138 RtlFreeUnicodeString( &lpOldFileW );
2139 RtlFreeUnicodeString( &lpNewFileW );
2140 RtlFreeUnicodeString( &lpSubKeyW );
2145 /******************************************************************************
2146 * RegSetKeySecurity [ADVAPI32.@]
2148 * Set the security of an open registry key.
2151 * hkey [I] Open handle of key to set
2152 * SecurityInfo [I] Descriptor contents
2153 * pSecurityDesc [I] Address of descriptor for key
2156 * Success: ERROR_SUCCESS
2157 * Failure: nonzero error code from Winerror.h
2159 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2160 PSECURITY_DESCRIPTOR pSecurityDesc )
2162 TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2164 /* It seems to perform this check before the hkey check */
2165 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2166 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2167 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2168 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2171 return ERROR_INVALID_PARAMETER;
2174 return ERROR_INVALID_PARAMETER;
2176 FIXME(":(%p,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
2178 return ERROR_SUCCESS;
2182 /******************************************************************************
2183 * RegGetKeySecurity [ADVAPI32.@]
2185 * Get a copy of the security descriptor for a given registry key.
2188 * hkey [I] Open handle of key to set
2189 * SecurityInformation [I] Descriptor contents
2190 * pSecurityDescriptor [O] Address of descriptor for key
2191 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2194 * Success: ERROR_SUCCESS
2195 * Failure: Error code
2197 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2198 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2199 LPDWORD lpcbSecurityDescriptor )
2201 TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
2202 lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
2204 /* FIXME: Check for valid SecurityInformation values */
2206 if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
2207 return ERROR_INSUFFICIENT_BUFFER;
2209 FIXME("(%p,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
2210 pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
2212 /* Do not leave security descriptor filled with garbage */
2213 RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2215 return ERROR_SUCCESS;
2219 /******************************************************************************
2220 * RegFlushKey [ADVAPI32.@]
2222 * Immediately write a registry key to registry.
2225 * hkey [I] Handle of key to write
2228 * Success: ERROR_SUCCESS
2229 * Failure: Error code
2231 DWORD WINAPI RegFlushKey( HKEY hkey )
2233 hkey = get_special_root_hkey( hkey );
2234 if (!hkey) return ERROR_INVALID_HANDLE;
2236 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2240 /******************************************************************************
2241 * RegConnectRegistryW [ADVAPI32.@]
2243 * Establishe a connection to a predefined registry key on another computer.
2246 * lpMachineName [I] Address of name of remote computer
2247 * hHey [I] Predefined registry handle
2248 * phkResult [I] Address of buffer for remote registry handle
2251 * Success: ERROR_SUCCESS
2252 * Failure: nonzero error code from Winerror.h
2254 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
2259 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
2261 if (!lpMachineName || !*lpMachineName) {
2262 /* Use the local machine name */
2263 ret = RegOpenKeyW( hKey, NULL, phkResult );
2266 WCHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
2267 DWORD len = sizeof(compName) / sizeof(WCHAR);
2269 /* MSDN says lpMachineName must start with \\ : not so */
2270 if( lpMachineName[0] == '\\' && lpMachineName[1] == '\\')
2272 if (GetComputerNameW(compName, &len))
2274 if (!strcmpiW(lpMachineName, compName))
2275 ret = RegOpenKeyW(hKey, NULL, phkResult);
2278 FIXME("Connect to %s is not supported.\n",debugstr_w(lpMachineName));
2279 ret = ERROR_BAD_NETPATH;
2283 ret = GetLastError();
2289 /******************************************************************************
2290 * RegConnectRegistryA [ADVAPI32.@]
2292 * See RegConnectRegistryW.
2294 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, PHKEY reskey )
2296 UNICODE_STRING machineW;
2299 RtlCreateUnicodeStringFromAsciiz( &machineW, machine );
2300 ret = RegConnectRegistryW( machineW.Buffer, hkey, reskey );
2301 RtlFreeUnicodeString( &machineW );
2306 /******************************************************************************
2307 * RegNotifyChangeKeyValue [ADVAPI32.@]
2309 * Notify the caller about changes to the attributes or contents of a registry key.
2312 * hkey [I] Handle of key to watch
2313 * fWatchSubTree [I] Flag for subkey notification
2314 * fdwNotifyFilter [I] Changes to be reported
2315 * hEvent [I] Handle of signaled event
2316 * fAsync [I] Flag for asynchronous reporting
2319 * Success: ERROR_SUCCESS
2320 * Failure: nonzero error code from Winerror.h
2322 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2323 DWORD fdwNotifyFilter, HANDLE hEvent,
2327 IO_STATUS_BLOCK iosb;
2329 hkey = get_special_root_hkey( hkey );
2330 if (!hkey) return ERROR_INVALID_HANDLE;
2332 TRACE("(%p,%i,%ld,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2335 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2336 fdwNotifyFilter, fWatchSubTree, NULL, 0,
2339 if (status && status != STATUS_TIMEOUT)
2340 return RtlNtStatusToDosError( status );
2342 return ERROR_SUCCESS;
2345 /******************************************************************************
2346 * RegOpenUserClassesRoot [ADVAPI32.@]
2348 * Open the HKEY_CLASSES_ROOT key for a user.
2351 * hToken [I] Handle of token representing the user
2352 * dwOptions [I] Reserved, nust be 0
2353 * samDesired [I] Desired access rights
2354 * phkResult [O] Destination for the resulting key handle
2357 * Success: ERROR_SUCCESS
2358 * Failure: nonzero error code from Winerror.h
2361 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2362 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2363 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2365 LONG WINAPI RegOpenUserClassesRoot(
2372 FIXME("(%p, 0x%lx, 0x%lx, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2374 *phkResult = HKEY_CLASSES_ROOT;
2375 return ERROR_SUCCESS;