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
40 #include "wine/unicode.h"
41 #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( HANDLE hkey, DWORD access )
103 int idx = (UINT_PTR)hkey - (UINT_PTR)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_PTR)hkey - (UINT_PTR)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, LPWSTR 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( (PHANDLE)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, LPSTR 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( (PHANDLE)retkey, access, &attr, 0, &classW, options, dispos );
227 RtlFreeUnicodeString( &classW );
230 return RtlNtStatusToDosError( status );
234 /******************************************************************************
235 * RegCreateKeyW [ADVAPI32.@]
237 * Creates the specified reg key.
240 * hKey [I] Handle to an open key.
241 * lpSubKey [I] Name of a key that will be opened or created.
242 * phkResult [O] Receives a handle to the opened or created key.
245 * Success: ERROR_SUCCESS
246 * Failure: nonzero error code defined in Winerror.h
248 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR lpSubKey, PHKEY phkResult )
250 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
251 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
252 return RegCreateKeyExW( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
253 KEY_ALL_ACCESS, NULL, phkResult, NULL );
257 /******************************************************************************
258 * RegCreateKeyA [ADVAPI32.@]
262 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR lpSubKey, PHKEY phkResult )
264 return RegCreateKeyExA( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
265 KEY_ALL_ACCESS, NULL, phkResult, NULL );
270 /******************************************************************************
271 * RegOpenKeyExW [ADVAPI32.@]
275 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
277 OBJECT_ATTRIBUTES attr;
278 UNICODE_STRING nameW;
280 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
282 attr.Length = sizeof(attr);
283 attr.RootDirectory = hkey;
284 attr.ObjectName = &nameW;
286 attr.SecurityDescriptor = NULL;
287 attr.SecurityQualityOfService = NULL;
288 RtlInitUnicodeString( &nameW, name );
289 return RtlNtStatusToDosError( NtOpenKey( (PHANDLE)retkey, access, &attr ) );
293 /******************************************************************************
294 * RegOpenKeyExA [ADVAPI32.@]
296 * Open a registry key.
299 * hkey [I] Handle of open key
300 * name [I] Name of subkey to open
301 * reserved [I] Reserved - must be zero
302 * access [I] Security access mask
303 * retkey [O] Handle to open key
306 * Success: ERROR_SUCCESS
307 * Failure: A standard Win32 error code. retkey is set to 0.
310 * Unlike RegCreateKeyExA(), this function will not create the key if it
313 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
315 OBJECT_ATTRIBUTES attr;
319 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
321 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
323 attr.Length = sizeof(attr);
324 attr.RootDirectory = hkey;
325 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
327 attr.SecurityDescriptor = NULL;
328 attr.SecurityQualityOfService = NULL;
330 RtlInitAnsiString( &nameA, name );
331 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
334 status = NtOpenKey( (PHANDLE)retkey, access, &attr );
336 return RtlNtStatusToDosError( status );
340 /******************************************************************************
341 * RegOpenKeyW [ADVAPI32.@]
345 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
350 return ERROR_SUCCESS;
352 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
356 /******************************************************************************
357 * RegOpenKeyA [ADVAPI32.@]
359 * Open a registry key.
362 * hkey [I] Handle of parent key to open the new key under
363 * name [I] Name of the key under hkey to open
364 * retkey [O] Destination for the resulting Handle
367 * Success: ERROR_SUCCESS
368 * Failure: A standard Win32 error code. retkey is set to 0.
370 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
375 return ERROR_SUCCESS;
377 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
381 /******************************************************************************
382 * RegOpenCurrentUser [ADVAPI32.@]
384 * Get a handle to the HKEY_CURRENT_USER key for the user
385 * the current thread is impersonating.
388 * access [I] Desired access rights to the key
389 * retkey [O] Handle to the opened key
392 * Success: ERROR_SUCCESS
393 * Failure: nonzero error code from Winerror.h
396 * This function is supposed to retrieve a handle to the
397 * HKEY_CURRENT_USER for the user the current thread is impersonating.
398 * Since Wine does not currently allow threads to impersonate other users,
399 * this stub should work fine.
401 DWORD WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
403 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
408 /******************************************************************************
409 * RegEnumKeyExW [ADVAPI32.@]
411 * Enumerate subkeys of the specified open registry key.
414 * hkey [I] Handle to key to enumerate
415 * index [I] Index of subkey to enumerate
416 * name [O] Buffer for subkey name
417 * name_len [O] Size of subkey buffer
418 * reserved [I] Reserved
419 * class [O] Buffer for class string
420 * class_len [O] Size of class buffer
421 * ft [O] Time key last written to
424 * Success: ERROR_SUCCESS
425 * Failure: System error code. If there are no more subkeys available, the
426 * function returns ERROR_NO_MORE_ITEMS.
428 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
429 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
432 char buffer[256], *buf_ptr = buffer;
433 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
436 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
437 name_len ? *name_len : -1, reserved, class, class_len, ft );
439 if (reserved) return ERROR_INVALID_PARAMETER;
440 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
442 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
443 buffer, sizeof(buffer), &total_size );
445 while (status == STATUS_BUFFER_OVERFLOW)
447 /* retry with a dynamically allocated buffer */
448 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
449 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
450 return ERROR_NOT_ENOUGH_MEMORY;
451 info = (KEY_NODE_INFORMATION *)buf_ptr;
452 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
453 buf_ptr, total_size, &total_size );
458 DWORD len = info->NameLength / sizeof(WCHAR);
459 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
461 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
463 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
464 status = STATUS_BUFFER_OVERFLOW;
468 memcpy( name, info->Name, info->NameLength );
472 *class_len = cls_len;
475 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
482 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
483 return RtlNtStatusToDosError( status );
487 /******************************************************************************
488 * RegEnumKeyExA [ADVAPI32.@]
492 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
493 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
496 char buffer[256], *buf_ptr = buffer;
497 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
500 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
501 name_len ? *name_len : -1, reserved, class, class_len, ft );
503 if (reserved) return ERROR_INVALID_PARAMETER;
504 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
506 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
507 buffer, sizeof(buffer), &total_size );
509 while (status == STATUS_BUFFER_OVERFLOW)
511 /* retry with a dynamically allocated buffer */
512 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
513 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
514 return ERROR_NOT_ENOUGH_MEMORY;
515 info = (KEY_NODE_INFORMATION *)buf_ptr;
516 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
517 buf_ptr, total_size, &total_size );
524 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
525 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
527 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
529 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
530 status = STATUS_BUFFER_OVERFLOW;
534 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
538 *class_len = cls_len;
541 RtlUnicodeToMultiByteN( class, cls_len, NULL,
542 (WCHAR *)(buf_ptr + info->ClassOffset),
550 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
551 return RtlNtStatusToDosError( status );
555 /******************************************************************************
556 * RegEnumKeyW [ADVAPI32.@]
558 * Enumerates subkyes of the specified open reg key.
561 * hKey [I] Handle to an open key.
562 * dwIndex [I] Index of the subkey of hKey to retrieve.
563 * lpName [O] Name of the subkey.
564 * cchName [I] Size of lpName in TCHARS.
567 * Success: ERROR_SUCCESS
568 * Failure: system error code. If there are no more subkeys available, the
569 * function returns ERROR_NO_MORE_ITEMS.
571 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
573 return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
577 /******************************************************************************
578 * RegEnumKeyA [ADVAPI32.@]
582 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
584 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
588 /******************************************************************************
589 * RegQueryInfoKeyW [ADVAPI32.@]
591 * Retrieves information about the specified registry key.
594 * hkey [I] Handle to key to query
595 * class [O] Buffer for class string
596 * class_len [O] Size of class string buffer
597 * reserved [I] Reserved
598 * subkeys [O] Buffer for number of subkeys
599 * max_subkey [O] Buffer for longest subkey name length
600 * max_class [O] Buffer for longest class string length
601 * values [O] Buffer for number of value entries
602 * max_value [O] Buffer for longest value name length
603 * max_data [O] Buffer for longest value data length
604 * security [O] Buffer for security descriptor length
605 * modif [O] Modification time
608 * Success: ERROR_SUCCESS
609 * Failure: system error code.
612 * - win95 allows class to be valid and class_len to be NULL
613 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
614 * - both allow class to be NULL and class_len to be NULL
615 * (it's hard to test validity, so test !NULL instead)
617 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
618 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
619 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
620 LPDWORD security, FILETIME *modif )
623 char buffer[256], *buf_ptr = buffer;
624 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
627 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
628 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
630 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
631 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
633 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
634 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
638 /* retry with a dynamically allocated buffer */
639 while (status == STATUS_BUFFER_OVERFLOW)
641 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
642 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
643 return ERROR_NOT_ENOUGH_MEMORY;
644 info = (KEY_FULL_INFORMATION *)buf_ptr;
645 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
648 if (status) goto done;
650 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
652 status = STATUS_BUFFER_OVERFLOW;
656 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
657 class[info->ClassLength/sizeof(WCHAR)] = 0;
660 else status = STATUS_SUCCESS;
662 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
663 if (subkeys) *subkeys = info->SubKeys;
664 if (max_subkey) *max_subkey = info->MaxNameLen;
665 if (max_class) *max_class = info->MaxClassLen;
666 if (values) *values = info->Values;
667 if (max_value) *max_value = info->MaxValueNameLen;
668 if (max_data) *max_data = info->MaxValueDataLen;
669 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
672 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
673 return RtlNtStatusToDosError( status );
677 /******************************************************************************
678 * RegQueryMultipleValuesA [ADVAPI32.@]
680 * Retrieves the type and data for a list of value names associated with a key.
683 * hKey [I] Handle to an open key.
684 * val_list [O] Array of VALENT structures that describes the entries.
685 * num_vals [I] Number of elements in val_list.
686 * lpValueBuf [O] Pointer to a buffer that receives the data for each value.
687 * ldwTotsize [I/O] Size of lpValueBuf.
690 * Success: ERROR_SUCCESS. ldwTotsize contains num bytes copied.
691 * Failure: nonzero error code from Winerror.h ldwTotsize contains num needed
694 DWORD WINAPI RegQueryMultipleValuesA(HKEY hkey, PVALENTA val_list, DWORD num_vals,
695 LPSTR lpValueBuf, LPDWORD ldwTotsize)
698 DWORD maxBytes = *ldwTotsize;
700 LPSTR bufptr = lpValueBuf;
703 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
705 for(i=0; i < num_vals; ++i)
708 val_list[i].ve_valuelen=0;
709 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
710 if(status != ERROR_SUCCESS)
715 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
717 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
718 (LPBYTE)bufptr, &val_list[i].ve_valuelen);
719 if(status != ERROR_SUCCESS)
724 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
726 bufptr += val_list[i].ve_valuelen;
729 *ldwTotsize += val_list[i].ve_valuelen;
731 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
735 /******************************************************************************
736 * RegQueryMultipleValuesW [ADVAPI32.@]
738 * See RegQueryMultipleValuesA.
740 DWORD WINAPI RegQueryMultipleValuesW(HKEY hkey, PVALENTW val_list, DWORD num_vals,
741 LPWSTR lpValueBuf, LPDWORD ldwTotsize)
744 DWORD maxBytes = *ldwTotsize;
746 LPSTR bufptr = (LPSTR)lpValueBuf;
749 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
751 for(i=0; i < num_vals; ++i)
753 val_list[i].ve_valuelen=0;
754 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
755 if(status != ERROR_SUCCESS)
760 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
762 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
763 (LPBYTE)bufptr, &val_list[i].ve_valuelen);
764 if(status != ERROR_SUCCESS)
769 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
771 bufptr += val_list[i].ve_valuelen;
774 *ldwTotsize += val_list[i].ve_valuelen;
776 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
779 /******************************************************************************
780 * RegQueryInfoKeyA [ADVAPI32.@]
782 * Retrieves information about a registry key.
785 * hKey [I] Handle to an open key.
786 * lpClass [O] Class string of the key.
787 * lpcClass [I/O] size of lpClass.
788 * lpReserved [I] Reserved; must be NULL.
789 * lpcSubKeys [O] Number of subkeys contained by the key.
790 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
791 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
793 * lpcValues [O] Number of values associated with the key.
794 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
795 * lpcMaxValueLen [O] Longest data component among the key's values
796 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
797 * lpftLastWriteTime [O] FILETIME strucutre that is the last write time.
800 * Success: ERROR_SUCCESS
801 * Failure: nonzero error code from Winerror.h
803 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
804 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
805 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
806 LPDWORD security, FILETIME *modif )
809 char buffer[256], *buf_ptr = buffer;
810 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
811 DWORD total_size, len;
813 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
814 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
816 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
817 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
819 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
820 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
822 if (class || class_len)
824 /* retry with a dynamically allocated buffer */
825 while (status == STATUS_BUFFER_OVERFLOW)
827 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
828 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
829 return ERROR_NOT_ENOUGH_MEMORY;
830 info = (KEY_FULL_INFORMATION *)buf_ptr;
831 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
834 if (status) goto done;
836 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
839 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
842 if (class && !status)
844 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
849 else status = STATUS_SUCCESS;
851 if (subkeys) *subkeys = info->SubKeys;
852 if (max_subkey) *max_subkey = info->MaxNameLen;
853 if (max_class) *max_class = info->MaxClassLen;
854 if (values) *values = info->Values;
855 if (max_value) *max_value = info->MaxValueNameLen;
856 if (max_data) *max_data = info->MaxValueDataLen;
857 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
860 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
861 return RtlNtStatusToDosError( status );
865 /******************************************************************************
866 * RegCloseKey [ADVAPI32.@]
868 * Close an open registry key.
871 * hkey [I] Handle of key to close
874 * Success: ERROR_SUCCESS
875 * Failure: Error code
877 DWORD WINAPI RegCloseKey( HKEY hkey )
879 if (!hkey) return ERROR_INVALID_HANDLE;
880 if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
881 return RtlNtStatusToDosError( NtClose( hkey ) );
885 /******************************************************************************
886 * RegDeleteKeyW [ADVAPI32.@]
890 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
895 if (!name) return ERROR_INVALID_PARAMETER;
897 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
899 if (!(ret = RegOpenKeyExW( hkey, name, 0, DELETE, &tmp )))
901 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
904 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
909 /******************************************************************************
910 * RegDeleteKeyA [ADVAPI32.@]
912 * Delete a registry key.
915 * hkey [I] Handle to parent key containing the key to delete
916 * name [I] Name of the key user hkey to delete
920 * MSDN is wrong when it says that hkey must be opened with the DELETE access
921 * right. In reality, it opens a new handle with DELETE access.
924 * Success: ERROR_SUCCESS
925 * Failure: Error code
927 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
932 if (!name) return ERROR_INVALID_PARAMETER;
934 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
936 if (!(ret = RegOpenKeyExA( hkey, name, 0, DELETE, &tmp )))
938 if (!is_version_nt()) /* win95 does recursive key deletes */
942 while(!RegEnumKeyA(tmp, 0, name, sizeof(name)))
944 if(RegDeleteKeyA(tmp, name)) /* recurse */
948 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
951 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
957 /******************************************************************************
958 * RegSetValueExW [ADVAPI32.@]
960 * Set the data and contents of a registry value.
963 * hkey [I] Handle of key to set value for
964 * name [I] Name of value to set
965 * reserved [I] Reserved, must be zero
966 * type [I] Type of the value being set
967 * data [I] The new contents of the value to set
968 * count [I] Size of data
971 * Success: ERROR_SUCCESS
972 * Failure: Error code
974 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
975 DWORD type, CONST BYTE *data, DWORD count )
977 UNICODE_STRING nameW;
979 /* no need for version check, not implemented on win9x anyway */
980 if (count && is_string(type))
982 LPCWSTR str = (LPCWSTR)data;
983 /* if user forgot to count terminating null, add it (yes NT does this) */
984 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
985 count += sizeof(WCHAR);
987 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
989 RtlInitUnicodeString( &nameW, name );
990 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
994 /******************************************************************************
995 * RegSetValueExA [ADVAPI32.@]
997 * See RegSetValueExW.
1000 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1001 * NT does definitely care (aj)
1003 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1004 CONST BYTE *data, DWORD count )
1007 WCHAR *dataW = NULL;
1010 if (!is_version_nt()) /* win95 */
1014 if (!data) return ERROR_INVALID_PARAMETER;
1015 count = strlen((const char *)data) + 1;
1018 else if (count && is_string(type))
1020 /* if user forgot to count terminating null, add it (yes NT does this) */
1021 if (data[count-1] && !data[count]) count++;
1024 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1026 if (is_string( type )) /* need to convert to Unicode */
1029 RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1030 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
1031 RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1033 data = (BYTE *)dataW;
1036 RtlInitAnsiString( &nameA, name );
1037 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1040 status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
1042 HeapFree( GetProcessHeap(), 0, dataW );
1043 return RtlNtStatusToDosError( status );
1047 /******************************************************************************
1048 * RegSetValueW [ADVAPI32.@]
1050 * Sets the data for the default or unnamed value of a reg key.
1053 * hKey [I] Handle to an open key.
1054 * lpSubKey [I] Name of a subkey of hKey.
1055 * dwType [I] Type of information to store.
1056 * lpData [I] String that contains the data to set for the default value.
1057 * cbData [I] Size of lpData.
1060 * Success: ERROR_SUCCESS
1061 * Failure: nonzero error code from Winerror.h
1063 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
1068 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
1070 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1072 if (name && name[0]) /* need to create the subkey */
1074 if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1077 ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (const BYTE*)data,
1078 (strlenW( data ) + 1) * sizeof(WCHAR) );
1079 if (subkey != hkey) RegCloseKey( subkey );
1084 /******************************************************************************
1085 * RegSetValueA [ADVAPI32.@]
1089 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
1094 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
1096 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1098 if (name && name[0]) /* need to create the subkey */
1100 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1102 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (const BYTE*)data, strlen(data)+1 );
1103 if (subkey != hkey) RegCloseKey( subkey );
1109 /******************************************************************************
1110 * RegQueryValueExW [ADVAPI32.@]
1112 * See RegQueryValueExA.
1114 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1115 LPBYTE data, LPDWORD count )
1118 UNICODE_STRING name_str;
1120 char buffer[256], *buf_ptr = buffer;
1121 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1122 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1124 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1125 hkey, debugstr_w(name), reserved, type, data, count,
1126 (count && data) ? *count : 0 );
1128 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1129 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1131 RtlInitUnicodeString( &name_str, name );
1133 if (data) total_size = min( sizeof(buffer), *count + info_size );
1134 else total_size = info_size;
1136 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1137 buffer, total_size, &total_size );
1138 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1142 /* retry with a dynamically allocated buffer */
1143 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1145 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1146 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1147 return ERROR_NOT_ENOUGH_MEMORY;
1148 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1149 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1150 buf_ptr, total_size, &total_size );
1155 memcpy( data, buf_ptr + info_size, total_size - info_size );
1156 /* if the type is REG_SZ and data is not 0-terminated
1157 * and there is enough space in the buffer NT appends a \0 */
1158 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1160 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1161 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1164 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1166 else status = STATUS_SUCCESS;
1168 if (type) *type = info->Type;
1169 if (count) *count = total_size - info_size;
1172 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1173 return RtlNtStatusToDosError(status);
1177 /******************************************************************************
1178 * RegQueryValueExA [ADVAPI32.@]
1180 * Get the type and contents of a specified value under with a key.
1183 * hkey [I] Handle of the key to query
1184 * name [I] Name of value under hkey to query
1185 * reserved [I] Reserved - must be NULL
1186 * type [O] Destination for the value type, or NULL if not required
1187 * data [O] Destination for the values contents, or NULL if not required
1188 * count [I/O] Size of data, updated with the number of bytes returned
1191 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1192 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1193 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1194 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1197 * MSDN states that if data is too small it is partially filled. In reality
1198 * it remains untouched.
1200 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
1201 LPBYTE data, LPDWORD count )
1206 char buffer[256], *buf_ptr = buffer;
1207 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1208 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1210 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1211 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1213 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1214 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1216 RtlInitAnsiString( &nameA, name );
1217 if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1219 return RtlNtStatusToDosError(status);
1221 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1222 KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
1223 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1225 /* we need to fetch the contents for a string type even if not requested,
1226 * because we need to compute the length of the ASCII string. */
1227 if (data || is_string(info->Type))
1229 /* retry with a dynamically allocated buffer */
1230 while (status == STATUS_BUFFER_OVERFLOW)
1232 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1233 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1235 status = STATUS_NO_MEMORY;
1238 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1239 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1240 KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1243 if (status) goto done;
1245 if (is_string(info->Type))
1249 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1250 total_size - info_size );
1253 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1256 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1257 total_size - info_size );
1258 /* if the type is REG_SZ and data is not 0-terminated
1259 * and there is enough space in the buffer NT appends a \0 */
1260 if (len < *count && data[len-1]) data[len] = 0;
1263 total_size = len + info_size;
1267 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1268 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1271 else status = STATUS_SUCCESS;
1273 if (type) *type = info->Type;
1274 if (count) *count = total_size - info_size;
1277 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1278 return RtlNtStatusToDosError(status);
1282 /******************************************************************************
1283 * RegQueryValueW [ADVAPI32.@]
1285 * Retrieves the data associated with the default or unnamed value of a key.
1288 * hkey [I] Handle to an open key.
1289 * name [I] Name of the subkey of hKey.
1290 * data [O] Receives the string associated with the default value
1292 * count [I/O] Size of lpValue in bytes.
1295 * Success: ERROR_SUCCESS
1296 * Failure: nonzero error code from Winerror.h
1298 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1303 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1305 if (name && name[0])
1307 if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1309 ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, (LPDWORD)count );
1310 if (subkey != hkey) RegCloseKey( subkey );
1311 if (ret == ERROR_FILE_NOT_FOUND)
1313 /* return empty string if default value not found */
1314 if (data) *data = 0;
1315 if (count) *count = sizeof(WCHAR);
1316 ret = ERROR_SUCCESS;
1322 /******************************************************************************
1323 * RegQueryValueA [ADVAPI32.@]
1325 * See RegQueryValueW.
1327 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1332 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1334 if (name && name[0])
1336 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1338 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, (LPDWORD)count );
1339 if (subkey != hkey) RegCloseKey( subkey );
1340 if (ret == ERROR_FILE_NOT_FOUND)
1342 /* return empty string if default value not found */
1343 if (data) *data = 0;
1344 if (count) *count = 1;
1345 ret = ERROR_SUCCESS;
1351 /******************************************************************************
1352 * ADVAPI_ApplyRestrictions [internal]
1354 * Helper function for RegGetValueA/W.
1356 VOID ADVAPI_ApplyRestrictions( DWORD dwFlags, DWORD dwType, DWORD cbData,
1359 /* Check if the type is restricted by the passed flags */
1360 if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1366 case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1367 case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1368 case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1369 case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1370 case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1371 case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1372 case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1375 if (dwFlags & dwMask)
1377 /* Type is not restricted, check for size mismatch */
1378 if (dwType == REG_BINARY)
1382 if ((dwFlags & RRF_RT_DWORD) == RRF_RT_DWORD)
1384 else if ((dwFlags & RRF_RT_DWORD) == RRF_RT_QWORD)
1387 if (cbExpect && cbData != cbExpect)
1388 *ret = ERROR_DATATYPE_MISMATCH;
1391 else *ret = ERROR_UNSUPPORTED_TYPE;
1396 /******************************************************************************
1397 * RegGetValueW [ADVAPI32.@]
1399 * Retrieves the type and data for a value name associated with a key
1400 * optionally expanding it's content and restricting it's type.
1403 * hKey [I] Handle to an open key.
1404 * pszSubKey [I] Name of the subkey of hKey.
1405 * pszValue [I] Name of value under hKey/szSubKey to query.
1406 * dwFlags [I] Flags restricting the value type to retrieve.
1407 * pdwType [O] Destination for the values type, may be NULL.
1408 * pvData [O] Destination for the values content, may be NULL.
1409 * pcbData [I/O] Size of pvData, updated with the size required to
1410 * retrieve the whole content.
1413 * Success: ERROR_SUCCESS
1414 * Failure: nonzero error code from Winerror.h
1417 * - Unless RRF_NOEXPAND is specified REG_EXPAND_SZ is automatically expanded
1418 * and REG_SZ is retrieved instead.
1419 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1420 * without RRF_NOEXPAND is thus not allowed.
1422 LONG WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1423 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1426 DWORD dwType, cbData = pcbData ? *pcbData : 0;
1430 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1431 hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1432 pvData, pcbData, cbData);
1434 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND))
1435 return ERROR_INVALID_PARAMETER;
1437 if (pszSubKey && pszSubKey[0])
1439 ret = RegOpenKeyExW(hKey, pszSubKey, 0, KEY_QUERY_VALUE, &hKey);
1440 if (ret != ERROR_SUCCESS) return ret;
1443 ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1445 /* If we are going to expand we need to read in the whole the value even
1446 * if the passed buffer was too small as the expanded string might be
1447 * smaller than the unexpanded one and could fit into cbData bytes. */
1448 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1449 (dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND)))
1452 if (pvBuf) HeapFree(GetProcessHeap(), 0, pvBuf);
1454 pvBuf = HeapAlloc(GetProcessHeap(), 0, cbData);
1457 ret = ERROR_NOT_ENOUGH_MEMORY;
1461 if (ret == ERROR_MORE_DATA)
1462 ret = RegQueryValueExW(hKey, pszValue, NULL,
1463 &dwType, pvBuf, &cbData);
1466 /* Even if cbData was large enough we have to copy the
1467 * string since ExpandEnvironmentStrings can't handle
1468 * overlapping buffers. */
1469 CopyMemory(pvBuf, pvData, cbData);
1472 /* Both the type or the value itself could have been modified in
1473 * between so we have to keep retrying until the buffer is large
1474 * enough or we no longer have to expand the value. */
1475 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1477 if (ret == ERROR_SUCCESS)
1479 if (dwType == REG_EXPAND_SZ)
1481 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1482 pcbData ? *pcbData : 0);
1484 if(pcbData && cbData > *pcbData)
1485 ret = ERROR_MORE_DATA;
1488 CopyMemory(pvData, pvBuf, *pcbData);
1491 if (pvBuf) HeapFree(GetProcessHeap(), 0, pvBuf);
1494 if (pszSubKey && pszSubKey[0])
1497 ADVAPI_ApplyRestrictions(dwFlags, dwType, cbData, &ret);
1499 if (pcbData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1500 ZeroMemory(pvData, *pcbData);
1502 if (pdwType) *pdwType = dwType;
1503 if (pcbData) *pcbData = cbData;
1509 /******************************************************************************
1510 * RegGetValueA [ADVAPI32.@]
1514 LONG WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
1515 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1518 DWORD dwType, cbData = pcbData ? *pcbData : 0;
1522 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1523 hKey, pszSubKey, pszValue, dwFlags, pdwType, pvData, pcbData,
1526 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND))
1527 return ERROR_INVALID_PARAMETER;
1529 if (pszSubKey && pszSubKey[0])
1531 ret = RegOpenKeyExA(hKey, pszSubKey, 0, KEY_QUERY_VALUE, &hKey);
1532 if (ret != ERROR_SUCCESS) return ret;
1535 ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1537 /* If we are going to expand we need to read in the whole the value even
1538 * if the passed buffer was too small as the expanded string might be
1539 * smaller than the unexpanded one and could fit into cbData bytes. */
1540 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1541 (dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND)))
1544 if (pvBuf) HeapFree(GetProcessHeap(), 0, pvBuf);
1546 pvBuf = HeapAlloc(GetProcessHeap(), 0, cbData);
1549 ret = ERROR_NOT_ENOUGH_MEMORY;
1553 if (ret == ERROR_MORE_DATA)
1554 ret = RegQueryValueExA(hKey, pszValue, NULL,
1555 &dwType, pvBuf, &cbData);
1558 /* Even if cbData was large enough we have to copy the
1559 * string since ExpandEnvironmentStrings can't handle
1560 * overlapping buffers. */
1561 CopyMemory(pvBuf, pvData, cbData);
1564 /* Both the type or the value itself could have been modified in
1565 * between so we have to keep retrying until the buffer is large
1566 * enough or we no longer have to expand the value. */
1567 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1569 if (ret == ERROR_SUCCESS)
1571 if (dwType == REG_EXPAND_SZ)
1573 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
1574 pcbData ? *pcbData : 0);
1576 if(pcbData && cbData > *pcbData)
1577 ret = ERROR_MORE_DATA;
1580 CopyMemory(pvData, pvBuf, *pcbData);
1583 if (pvBuf) HeapFree(GetProcessHeap(), 0, pvBuf);
1586 if (pszSubKey && pszSubKey[0])
1589 ADVAPI_ApplyRestrictions(dwFlags, dwType, cbData, &ret);
1591 if (pcbData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1592 ZeroMemory(pvData, *pcbData);
1594 if (pdwType) *pdwType = dwType;
1595 if (pcbData) *pcbData = cbData;
1601 /******************************************************************************
1602 * RegEnumValueW [ADVAPI32.@]
1604 * Enumerates the values for the specified open registry key.
1607 * hkey [I] Handle to key to query
1608 * index [I] Index of value to query
1609 * value [O] Value string
1610 * val_count [I/O] Size of value buffer (in wchars)
1611 * reserved [I] Reserved
1612 * type [O] Type code
1613 * data [O] Value data
1614 * count [I/O] Size of data buffer (in bytes)
1617 * Success: ERROR_SUCCESS
1618 * Failure: nonzero error code from Winerror.h
1621 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1622 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1626 char buffer[256], *buf_ptr = buffer;
1627 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1628 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1630 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1631 hkey, index, value, val_count, reserved, type, data, count );
1633 /* NT only checks count, not val_count */
1634 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1635 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1637 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1638 if (data) total_size += *count;
1639 total_size = min( sizeof(buffer), total_size );
1641 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1642 buffer, total_size, &total_size );
1643 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1647 /* retry with a dynamically allocated buffer */
1648 while (status == STATUS_BUFFER_OVERFLOW)
1650 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1651 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1652 return ERROR_NOT_ENOUGH_MEMORY;
1653 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1654 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1655 buf_ptr, total_size, &total_size );
1658 if (status) goto done;
1662 if (info->NameLength/sizeof(WCHAR) >= *val_count)
1664 status = STATUS_BUFFER_OVERFLOW;
1667 memcpy( value, info->Name, info->NameLength );
1668 *val_count = info->NameLength / sizeof(WCHAR);
1669 value[*val_count] = 0;
1674 if (total_size - info->DataOffset > *count)
1676 status = STATUS_BUFFER_OVERFLOW;
1679 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1680 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1682 /* if the type is REG_SZ and data is not 0-terminated
1683 * and there is enough space in the buffer NT appends a \0 */
1684 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1685 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1689 else status = STATUS_SUCCESS;
1692 if (type) *type = info->Type;
1693 if (count) *count = info->DataLength;
1696 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1697 return RtlNtStatusToDosError(status);
1701 /******************************************************************************
1702 * RegEnumValueA [ADVAPI32.@]
1704 * See RegEnumValueW.
1706 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1707 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1711 char buffer[256], *buf_ptr = buffer;
1712 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1713 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1715 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1716 hkey, index, value, val_count, reserved, type, data, count );
1718 /* NT only checks count, not val_count */
1719 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1720 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1722 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1723 if (data) total_size += *count;
1724 total_size = min( sizeof(buffer), total_size );
1726 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1727 buffer, total_size, &total_size );
1728 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1730 /* we need to fetch the contents for a string type even if not requested,
1731 * because we need to compute the length of the ASCII string. */
1732 if (value || data || is_string(info->Type))
1734 /* retry with a dynamically allocated buffer */
1735 while (status == STATUS_BUFFER_OVERFLOW)
1737 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1738 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1739 return ERROR_NOT_ENOUGH_MEMORY;
1740 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1741 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1742 buf_ptr, total_size, &total_size );
1745 if (status) goto done;
1747 if (is_string(info->Type))
1750 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1751 total_size - info->DataOffset );
1754 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1757 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1758 total_size - info->DataOffset );
1759 /* if the type is REG_SZ and data is not 0-terminated
1760 * and there is enough space in the buffer NT appends a \0 */
1761 if (len < *count && data[len-1]) data[len] = 0;
1764 info->DataLength = len;
1768 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1769 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1772 if (value && !status)
1776 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1777 if (len >= *val_count)
1779 status = STATUS_BUFFER_OVERFLOW;
1782 len = *val_count - 1;
1783 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1789 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1795 else status = STATUS_SUCCESS;
1797 if (type) *type = info->Type;
1798 if (count) *count = info->DataLength;
1801 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1802 return RtlNtStatusToDosError(status);
1807 /******************************************************************************
1808 * RegDeleteValueW [ADVAPI32.@]
1810 * See RegDeleteValueA.
1812 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1814 UNICODE_STRING nameW;
1816 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1818 RtlInitUnicodeString( &nameW, name );
1819 return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1823 /******************************************************************************
1824 * RegDeleteValueA [ADVAPI32.@]
1826 * Delete a value from the registry.
1829 * hkey [I] Registry handle of the key holding the value
1830 * name [I] Name of the value under hkey to delete
1833 * Success: ERROR_SUCCESS
1834 * Failure: nonzero error code from Winerror.h
1836 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1841 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1843 RtlInitAnsiString( &nameA, name );
1844 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1846 status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1847 return RtlNtStatusToDosError( status );
1851 /******************************************************************************
1852 * RegLoadKeyW [ADVAPI32.@]
1854 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
1855 * registration information from a specified file into that subkey.
1858 * hkey [I] Handle of open key
1859 * subkey [I] Address of name of subkey
1860 * filename [I] Address of filename for registry information
1863 * Success: ERROR_SUCCES
1864 * Failure: nonzero error code from Winerror.h
1866 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1868 OBJECT_ATTRIBUTES destkey, file;
1869 UNICODE_STRING subkeyW, filenameW;
1871 if (!(hkey = get_special_root_hkey(hkey))) return ERROR_INVALID_HANDLE;
1873 destkey.Length = sizeof(destkey);
1874 destkey.RootDirectory = hkey; /* root key: HKLM or HKU */
1875 destkey.ObjectName = &subkeyW; /* name of the key */
1876 destkey.Attributes = 0;
1877 destkey.SecurityDescriptor = NULL;
1878 destkey.SecurityQualityOfService = NULL;
1879 RtlInitUnicodeString(&subkeyW, subkey);
1881 file.Length = sizeof(file);
1882 file.RootDirectory = NULL;
1883 file.ObjectName = &filenameW; /* file containing the hive */
1884 file.Attributes = OBJ_CASE_INSENSITIVE;
1885 file.SecurityDescriptor = NULL;
1886 file.SecurityQualityOfService = NULL;
1887 RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
1889 return RtlNtStatusToDosError( NtLoadKey(&destkey, &file) );
1893 /******************************************************************************
1894 * RegLoadKeyA [ADVAPI32.@]
1898 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1900 UNICODE_STRING subkeyW, filenameW;
1901 STRING subkeyA, filenameA;
1904 RtlInitAnsiString(&subkeyA, subkey);
1905 RtlInitAnsiString(&filenameA, filename);
1907 if ((status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)))
1908 return RtlNtStatusToDosError(status);
1910 if ((status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
1911 return RtlNtStatusToDosError(status);
1913 return RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
1917 /******************************************************************************
1918 * RegSaveKeyW [ADVAPI32.@]
1920 * Save a key and all of its subkeys and values to a new file in the standard format.
1923 * hkey [I] Handle of key where save begins
1924 * lpFile [I] Address of filename to save to
1925 * sa [I] Address of security structure
1928 * Success: ERROR_SUCCESS
1929 * Failure: nonzero error code from Winerror.h
1931 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1933 static const WCHAR format[] =
1934 {'r','e','g','%','0','4','x','.','t','m','p',0};
1935 WCHAR buffer[MAX_PATH];
1941 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
1943 if (!file || !*file) return ERROR_INVALID_PARAMETER;
1944 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1946 err = GetLastError();
1947 GetFullPathNameW( file, sizeof(buffer)/sizeof(WCHAR), buffer, &nameW );
1951 snprintfW( nameW, 16, format, count++ );
1952 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
1953 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1954 if (handle != INVALID_HANDLE_VALUE) break;
1955 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1957 /* Something gone haywire ? Please report if this happens abnormally */
1959 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);
1962 ret = RtlNtStatusToDosError(NtSaveKey(hkey, handle));
1964 CloseHandle( handle );
1967 if (!MoveFileExW( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1969 ERR( "Failed to move %s to %s\n", debugstr_w(buffer),
1971 ret = GetLastError();
1974 if (ret) DeleteFileW( buffer );
1977 SetLastError( err ); /* restore last error code */
1982 /******************************************************************************
1983 * RegSaveKeyA [ADVAPI32.@]
1987 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1989 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
1993 RtlInitAnsiString(&fileA, file);
1994 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
1995 return RtlNtStatusToDosError( status );
1996 return RegSaveKeyW(hkey, fileW->Buffer, sa);
2000 /******************************************************************************
2001 * RegRestoreKeyW [ADVAPI32.@]
2003 * Read the registry information from a file and copy it over a key.
2006 * hkey [I] Handle of key where restore begins
2007 * lpFile [I] Address of filename containing saved tree
2008 * dwFlags [I] Optional flags
2011 * Success: ERROR_SUCCESS
2012 * Failure: nonzero error code from Winerror.h
2014 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2016 TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
2018 /* It seems to do this check before the hkey check */
2019 if (!lpFile || !*lpFile)
2020 return ERROR_INVALID_PARAMETER;
2022 FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2024 /* Check for file existence */
2026 return ERROR_SUCCESS;
2030 /******************************************************************************
2031 * RegRestoreKeyA [ADVAPI32.@]
2033 * See RegRestoreKeyW.
2035 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2037 UNICODE_STRING lpFileW;
2040 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2041 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2042 RtlFreeUnicodeString( &lpFileW );
2047 /******************************************************************************
2048 * RegUnLoadKeyW [ADVAPI32.@]
2050 * Unload a registry key and its subkeys from the registry.
2053 * hkey [I] Handle of open key
2054 * lpSubKey [I] Address of name of subkey to unload
2057 * Success: ERROR_SUCCESS
2058 * Failure: nonzero error code from Winerror.h
2060 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2065 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2067 ret = RegOpenKeyW(hkey,lpSubKey,&shkey);
2069 return ERROR_INVALID_PARAMETER;
2071 ret = RtlNtStatusToDosError(NtUnloadKey(shkey));
2079 /******************************************************************************
2080 * RegUnLoadKeyA [ADVAPI32.@]
2082 * See RegUnLoadKeyW.
2084 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2086 UNICODE_STRING lpSubKeyW;
2089 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2090 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2091 RtlFreeUnicodeString( &lpSubKeyW );
2096 /******************************************************************************
2097 * RegReplaceKeyW [ADVAPI32.@]
2099 * Replace the file backing a registry key and all its subkeys with another file.
2102 * hkey [I] Handle of open key
2103 * lpSubKey [I] Address of name of subkey
2104 * lpNewFile [I] Address of filename for file with new data
2105 * lpOldFile [I] Address of filename for backup file
2108 * Success: ERROR_SUCCESS
2109 * Failure: nonzero error code from Winerror.h
2111 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
2114 FIXME("(%p,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
2115 debugstr_w(lpNewFile),debugstr_w(lpOldFile));
2116 return ERROR_SUCCESS;
2120 /******************************************************************************
2121 * RegReplaceKeyA [ADVAPI32.@]
2123 * See RegReplaceKeyW.
2125 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
2128 UNICODE_STRING lpSubKeyW;
2129 UNICODE_STRING lpNewFileW;
2130 UNICODE_STRING lpOldFileW;
2133 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2134 RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, lpOldFile );
2135 RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, lpNewFile );
2136 ret = RegReplaceKeyW( hkey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
2137 RtlFreeUnicodeString( &lpOldFileW );
2138 RtlFreeUnicodeString( &lpNewFileW );
2139 RtlFreeUnicodeString( &lpSubKeyW );
2144 /******************************************************************************
2145 * RegSetKeySecurity [ADVAPI32.@]
2147 * Set the security of an open registry key.
2150 * hkey [I] Open handle of key to set
2151 * SecurityInfo [I] Descriptor contents
2152 * pSecurityDesc [I] Address of descriptor for key
2155 * Success: ERROR_SUCCESS
2156 * Failure: nonzero error code from Winerror.h
2158 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2159 PSECURITY_DESCRIPTOR pSecurityDesc )
2161 TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2163 /* It seems to perform this check before the hkey check */
2164 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2165 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2166 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2167 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2170 return ERROR_INVALID_PARAMETER;
2173 return ERROR_INVALID_PARAMETER;
2175 FIXME(":(%p,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
2177 return ERROR_SUCCESS;
2181 /******************************************************************************
2182 * RegGetKeySecurity [ADVAPI32.@]
2184 * Get a copy of the security descriptor for a given registry key.
2187 * hkey [I] Open handle of key to set
2188 * SecurityInformation [I] Descriptor contents
2189 * pSecurityDescriptor [O] Address of descriptor for key
2190 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2193 * Success: ERROR_SUCCESS
2194 * Failure: Error code
2196 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2197 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2198 LPDWORD lpcbSecurityDescriptor )
2200 TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
2201 lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
2203 /* FIXME: Check for valid SecurityInformation values */
2205 if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
2206 return ERROR_INSUFFICIENT_BUFFER;
2208 FIXME("(%p,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
2209 pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
2211 /* Do not leave security descriptor filled with garbage */
2212 RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2214 return ERROR_SUCCESS;
2218 /******************************************************************************
2219 * RegFlushKey [ADVAPI32.@]
2221 * Immediately write a registry key to registry.
2224 * hkey [I] Handle of key to write
2227 * Success: ERROR_SUCCESS
2228 * Failure: Error code
2230 DWORD WINAPI RegFlushKey( HKEY hkey )
2232 hkey = get_special_root_hkey( hkey );
2233 if (!hkey) return ERROR_INVALID_HANDLE;
2235 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2239 /******************************************************************************
2240 * RegConnectRegistryW [ADVAPI32.@]
2242 * Establishe a connection to a predefined registry key on another computer.
2245 * lpMachineName [I] Address of name of remote computer
2246 * hHey [I] Predefined registry handle
2247 * phkResult [I] Address of buffer for remote registry handle
2250 * Success: ERROR_SUCCESS
2251 * Failure: nonzero error code from Winerror.h
2253 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
2258 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
2260 if (!lpMachineName || !*lpMachineName) {
2261 /* Use the local machine name */
2262 ret = RegOpenKeyW( hKey, NULL, phkResult );
2265 WCHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
2266 DWORD len = sizeof(compName) / sizeof(WCHAR);
2268 /* MSDN says lpMachineName must start with \\ : not so */
2269 if( lpMachineName[0] == '\\' && lpMachineName[1] == '\\')
2271 if (GetComputerNameW(compName, &len))
2273 if (!strcmpiW(lpMachineName, compName))
2274 ret = RegOpenKeyW(hKey, NULL, phkResult);
2277 FIXME("Connect to %s is not supported.\n",debugstr_w(lpMachineName));
2278 ret = ERROR_BAD_NETPATH;
2282 ret = GetLastError();
2288 /******************************************************************************
2289 * RegConnectRegistryA [ADVAPI32.@]
2291 * See RegConnectRegistryW.
2293 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, PHKEY reskey )
2295 UNICODE_STRING machineW;
2298 RtlCreateUnicodeStringFromAsciiz( &machineW, machine );
2299 ret = RegConnectRegistryW( machineW.Buffer, hkey, reskey );
2300 RtlFreeUnicodeString( &machineW );
2305 /******************************************************************************
2306 * RegNotifyChangeKeyValue [ADVAPI32.@]
2308 * Notify the caller about changes to the attributes or contents of a registry key.
2311 * hkey [I] Handle of key to watch
2312 * fWatchSubTree [I] Flag for subkey notification
2313 * fdwNotifyFilter [I] Changes to be reported
2314 * hEvent [I] Handle of signaled event
2315 * fAsync [I] Flag for asynchronous reporting
2318 * Success: ERROR_SUCCESS
2319 * Failure: nonzero error code from Winerror.h
2321 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2322 DWORD fdwNotifyFilter, HANDLE hEvent,
2326 IO_STATUS_BLOCK iosb;
2328 hkey = get_special_root_hkey( hkey );
2329 if (!hkey) return ERROR_INVALID_HANDLE;
2331 TRACE("(%p,%i,%ld,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2334 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2335 fdwNotifyFilter, fWatchSubTree, NULL, 0,
2338 if (status && status != STATUS_TIMEOUT)
2339 return RtlNtStatusToDosError( status );
2341 return ERROR_SUCCESS;
2344 /******************************************************************************
2345 * RegOpenUserClassesRoot [ADVAPI32.@]
2347 * Open the HKEY_CLASSES_ROOT key for a user.
2350 * hToken [I] Handle of token representing the user
2351 * dwOptions [I] Reserved, nust be 0
2352 * samDesired [I] Desired access rights
2353 * phkResult [O] Destination for the resulting key handle
2356 * Success: ERROR_SUCCESS
2357 * Failure: nonzero error code from Winerror.h
2360 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2361 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2362 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2364 LONG WINAPI RegOpenUserClassesRoot(
2371 FIXME("(%p, 0x%lx, 0x%lx, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2373 *phkResult = HKEY_CLASSES_ROOT;
2374 return ERROR_SUCCESS;