4 * Copyright (C) 1999 Alexandre Julliard
6 * Based on misc/registry.c code
7 * Copyright (C) 1996 Marcus Meissner
8 * Copyright (C) 1998 Matthew Becker
9 * Copyright (C) 1999 Sylvain St-Germain
11 * This file is concerned about handle management and interaction with the Wine server.
12 * Registry file I/O is in misc/registry.c.
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include "wine/unicode.h"
39 #include "wine/server.h"
40 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(reg);
45 /* allowed bits for access mask */
46 #define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
48 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
49 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
50 #define NB_SPECIAL_ROOT_KEYS ((UINT)HKEY_SPECIAL_ROOT_LAST - (UINT)HKEY_SPECIAL_ROOT_FIRST + 1)
52 static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
54 static const WCHAR name_CLASSES_ROOT[] =
55 {'M','a','c','h','i','n','e','\\',
56 'S','o','f','t','w','a','r','e','\\',
57 'C','l','a','s','s','e','s',0};
58 static const WCHAR name_LOCAL_MACHINE[] =
59 {'M','a','c','h','i','n','e',0};
60 static const WCHAR name_USERS[] =
62 static const WCHAR name_PERFORMANCE_DATA[] =
63 {'P','e','r','f','D','a','t','a',0};
64 static const WCHAR name_CURRENT_CONFIG[] =
65 {'M','a','c','h','i','n','e','\\',
66 'S','y','s','t','e','m','\\',
67 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
68 'H','a','r','d','w','a','r','e','P','r','o','f','i','l','e','s','\\',
69 'C','u','r','r','e','n','t',0};
70 static const WCHAR name_DYN_DATA[] =
71 {'D','y','n','D','a','t','a',0};
73 #define DECL_STR(key) { sizeof(name_##key)-sizeof(WCHAR), sizeof(name_##key), (LPWSTR)name_##key }
74 static UNICODE_STRING root_key_names[NB_SPECIAL_ROOT_KEYS] =
76 DECL_STR(CLASSES_ROOT),
77 { 0, 0, NULL }, /* HKEY_CURRENT_USER is determined dynamically */
78 DECL_STR(LOCAL_MACHINE),
80 DECL_STR(PERFORMANCE_DATA),
81 DECL_STR(CURRENT_CONFIG),
87 /* check if value type needs string conversion (Ansi<->Unicode) */
88 inline static int is_string( DWORD type )
90 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
93 /* check if current version is NT or Win95 */
94 inline static int is_version_nt(void)
96 return !(GetVersion() & 0x80000000);
99 /* create one of the HKEY_* special root keys */
100 static HKEY create_special_root_hkey( HKEY hkey, DWORD access )
103 int idx = (UINT)hkey - (UINT)HKEY_SPECIAL_ROOT_FIRST;
105 if (hkey == HKEY_CURRENT_USER)
107 if (RtlOpenCurrentUser( access, &hkey )) return 0;
108 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
112 OBJECT_ATTRIBUTES attr;
114 attr.Length = sizeof(attr);
115 attr.RootDirectory = 0;
116 attr.ObjectName = &root_key_names[idx];
118 attr.SecurityDescriptor = NULL;
119 attr.SecurityQualityOfService = NULL;
120 if (NtCreateKey( &hkey, access, &attr, 0, NULL, 0, NULL )) return 0;
121 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
124 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
127 NtClose( hkey ); /* somebody beat us to it */
131 /* map the hkey from special root to normal key if necessary */
132 inline static HKEY get_special_root_hkey( HKEY hkey )
136 if ((hkey >= HKEY_SPECIAL_ROOT_FIRST) && (hkey <= HKEY_SPECIAL_ROOT_LAST))
138 if (!(ret = special_root_keys[(UINT)hkey - (UINT)HKEY_SPECIAL_ROOT_FIRST]))
139 ret = create_special_root_hkey( hkey, KEY_ALL_ACCESS );
145 /******************************************************************************
146 * RegCreateKeyExW [ADVAPI32.@]
149 * hkey [I] Handle of an open key
150 * name [I] Address of subkey name
151 * reserved [I] Reserved - must be 0
152 * class [I] Address of class string
153 * options [I] Special options flag
154 * access [I] Desired security access
155 * sa [I] Address of key security structure
156 * retkey [O] Address of buffer for opened handle
157 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
160 * in case of failing retkey remains untouched
162 * FIXME MAXIMUM_ALLOWED in access mask not supported by server
164 DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPCWSTR class,
165 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
166 PHKEY retkey, LPDWORD dispos )
168 OBJECT_ATTRIBUTES attr;
169 UNICODE_STRING nameW, classW;
171 if (reserved) return ERROR_INVALID_PARAMETER;
172 if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
173 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
175 attr.Length = sizeof(attr);
176 attr.RootDirectory = hkey;
177 attr.ObjectName = &nameW;
179 attr.SecurityDescriptor = NULL;
180 attr.SecurityQualityOfService = NULL;
181 RtlInitUnicodeString( &nameW, name );
182 RtlInitUnicodeString( &classW, class );
184 return RtlNtStatusToDosError( NtCreateKey( retkey, access, &attr, 0,
185 &classW, options, dispos ) );
189 /******************************************************************************
190 * RegCreateKeyExA [ADVAPI32.@]
192 * FIXME MAXIMUM_ALLOWED in access mask not supported by server
194 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPCSTR class,
195 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
196 PHKEY retkey, LPDWORD dispos )
198 OBJECT_ATTRIBUTES attr;
199 UNICODE_STRING classW;
200 ANSI_STRING nameA, classA;
203 if (reserved) return ERROR_INVALID_PARAMETER;
204 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
205 else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
206 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
208 attr.Length = sizeof(attr);
209 attr.RootDirectory = hkey;
210 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
212 attr.SecurityDescriptor = NULL;
213 attr.SecurityQualityOfService = NULL;
214 RtlInitAnsiString( &nameA, name );
215 RtlInitAnsiString( &classA, class );
217 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
220 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
222 status = NtCreateKey( retkey, access, &attr, 0, &classW, options, dispos );
223 RtlFreeUnicodeString( &classW );
226 return RtlNtStatusToDosError( status );
230 /******************************************************************************
231 * RegCreateKeyW [ADVAPI32.@]
233 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
235 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
236 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
237 return RegCreateKeyExW( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
238 KEY_ALL_ACCESS, NULL, retkey, NULL );
242 /******************************************************************************
243 * RegCreateKeyA [ADVAPI32.@]
245 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
247 return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
248 KEY_ALL_ACCESS, NULL, retkey, NULL );
253 /******************************************************************************
254 * RegOpenKeyExW [ADVAPI32.@]
256 * Opens the specified key
258 * Unlike RegCreateKeyEx, this does not create the key if it does not exist.
261 * hkey [I] Handle of open key
262 * name [I] Name of subkey to open
263 * reserved [I] Reserved - must be zero
264 * access [I] Security access mask
265 * retkey [O] Handle to open key
268 * Success: ERROR_SUCCESS
269 * Failure: Error code
272 * in case of failing is retkey = 0
274 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
276 OBJECT_ATTRIBUTES attr;
277 UNICODE_STRING nameW;
279 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
281 attr.Length = sizeof(attr);
282 attr.RootDirectory = hkey;
283 attr.ObjectName = &nameW;
285 attr.SecurityDescriptor = NULL;
286 attr.SecurityQualityOfService = NULL;
287 RtlInitUnicodeString( &nameW, name );
288 return RtlNtStatusToDosError( NtOpenKey( retkey, access, &attr ) );
292 /******************************************************************************
293 * RegOpenKeyExA [ADVAPI32.@]
295 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
297 OBJECT_ATTRIBUTES attr;
301 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
303 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
305 attr.Length = sizeof(attr);
306 attr.RootDirectory = hkey;
307 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
309 attr.SecurityDescriptor = NULL;
310 attr.SecurityQualityOfService = NULL;
312 RtlInitAnsiString( &nameA, name );
313 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
316 status = NtOpenKey( retkey, access, &attr );
318 return RtlNtStatusToDosError( status );
322 /******************************************************************************
323 * RegOpenKeyW [ADVAPI32.@]
326 * hkey [I] Handle of open key
327 * name [I] Address of name of subkey to open
328 * retkey [O] Handle to open key
331 * Success: ERROR_SUCCESS
332 * Failure: Error code
335 * in case of failing is retkey = 0
337 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
339 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
343 /******************************************************************************
344 * RegOpenKeyA [ADVAPI32.@]
346 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
348 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
352 /******************************************************************************
353 * RegOpenCurrentUser [ADVAPI32.@]
354 * FIXME: This function is supposed to retrieve a handle to the
355 * HKEY_CURRENT_USER for the user the current thread is impersonating.
356 * Since Wine does not currently allow threads to impersonate other users,
357 * this stub should work fine.
359 DWORD WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
361 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
366 /******************************************************************************
367 * RegEnumKeyExW [ADVAPI32.@]
370 * hkey [I] Handle to key to enumerate
371 * index [I] Index of subkey to enumerate
372 * name [O] Buffer for subkey name
373 * name_len [O] Size of subkey buffer
374 * reserved [I] Reserved
375 * class [O] Buffer for class string
376 * class_len [O] Size of class buffer
377 * ft [O] Time key last written to
379 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
380 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
383 char buffer[256], *buf_ptr = buffer;
384 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
387 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
388 name_len ? *name_len : -1, reserved, class, class_len, ft );
390 if (reserved) return ERROR_INVALID_PARAMETER;
391 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
393 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
394 buffer, sizeof(buffer), &total_size );
396 while (status == STATUS_BUFFER_OVERFLOW)
398 /* retry with a dynamically allocated buffer */
399 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
400 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
401 return ERROR_NOT_ENOUGH_MEMORY;
402 info = (KEY_NODE_INFORMATION *)buf_ptr;
403 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
404 buf_ptr, total_size, &total_size );
409 DWORD len = info->NameLength / sizeof(WCHAR);
410 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
412 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
414 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
415 status = STATUS_BUFFER_OVERFLOW;
419 memcpy( name, info->Name, info->NameLength );
423 *class_len = cls_len;
426 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
433 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
434 return RtlNtStatusToDosError( status );
438 /******************************************************************************
439 * RegEnumKeyExA [ADVAPI32.@]
441 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
442 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
445 char buffer[256], *buf_ptr = buffer;
446 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
449 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
450 name_len ? *name_len : -1, reserved, class, class_len, ft );
452 if (reserved) return ERROR_INVALID_PARAMETER;
453 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
455 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
456 buffer, sizeof(buffer), &total_size );
458 while (status == STATUS_BUFFER_OVERFLOW)
460 /* retry with a dynamically allocated buffer */
461 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
462 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
463 return ERROR_NOT_ENOUGH_MEMORY;
464 info = (KEY_NODE_INFORMATION *)buf_ptr;
465 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
466 buf_ptr, total_size, &total_size );
473 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
474 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
476 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
478 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
479 status = STATUS_BUFFER_OVERFLOW;
483 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
487 *class_len = cls_len;
490 RtlUnicodeToMultiByteN( class, cls_len, NULL,
491 (WCHAR *)(buf_ptr + info->ClassOffset),
499 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
500 return RtlNtStatusToDosError( status );
504 /******************************************************************************
505 * RegEnumKeyW [ADVAPI32.@]
507 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
509 return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
513 /******************************************************************************
514 * RegEnumKeyA [ADVAPI32.@]
516 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
518 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
522 /******************************************************************************
523 * RegQueryInfoKeyW [ADVAPI32.@]
526 * hkey [I] Handle to key to query
527 * class [O] Buffer for class string
528 * class_len [O] Size of class string buffer
529 * reserved [I] Reserved
530 * subkeys [O] Buffer for number of subkeys
531 * max_subkey [O] Buffer for longest subkey name length
532 * max_class [O] Buffer for longest class string length
533 * values [O] Buffer for number of value entries
534 * max_value [O] Buffer for longest value name length
535 * max_data [O] Buffer for longest value data length
536 * security [O] Buffer for security descriptor length
537 * modif [O] Modification time
539 * - win95 allows class to be valid and class_len to be NULL
540 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
541 * - both allow class to be NULL and class_len to be NULL
542 * (it's hard to test validity, so test !NULL instead)
544 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
545 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
546 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
547 LPDWORD security, FILETIME *modif )
550 char buffer[256], *buf_ptr = buffer;
551 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
554 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
555 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
557 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
558 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
560 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
561 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
565 /* retry with a dynamically allocated buffer */
566 while (status == STATUS_BUFFER_OVERFLOW)
568 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
569 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
570 return ERROR_NOT_ENOUGH_MEMORY;
571 info = (KEY_FULL_INFORMATION *)buf_ptr;
572 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
575 if (status) goto done;
577 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
579 status = STATUS_BUFFER_OVERFLOW;
583 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
584 class[info->ClassLength/sizeof(WCHAR)] = 0;
587 else status = STATUS_SUCCESS;
589 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
590 if (subkeys) *subkeys = info->SubKeys;
591 if (max_subkey) *max_subkey = info->MaxNameLen;
592 if (max_class) *max_class = info->MaxClassLen;
593 if (values) *values = info->Values;
594 if (max_value) *max_value = info->MaxValueNameLen;
595 if (max_data) *max_data = info->MaxValueDataLen;
596 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
599 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
600 return RtlNtStatusToDosError( status );
604 /******************************************************************************
605 * RegQueryMultipleValuesA [ADVAPI32.@]
607 DWORD WINAPI RegQueryMultipleValuesA(HKEY hkey, PVALENTA val_list, DWORD num_vals,
608 LPSTR lpValueBuf, LPDWORD ldwTotsize)
611 DWORD maxBytes = *ldwTotsize;
613 LPSTR bufptr = lpValueBuf;
616 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
618 for(i=0; i < num_vals; ++i)
621 val_list[i].ve_valuelen=0;
622 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
623 if(status != ERROR_SUCCESS)
628 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
630 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
631 bufptr, &val_list[i].ve_valuelen);
632 if(status != ERROR_SUCCESS)
637 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
639 bufptr += val_list[i].ve_valuelen;
642 *ldwTotsize += val_list[i].ve_valuelen;
644 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
648 /******************************************************************************
649 * RegQueryMultipleValuesW [ADVAPI32.@]
651 DWORD WINAPI RegQueryMultipleValuesW(HKEY hkey, PVALENTW val_list, DWORD num_vals,
652 LPWSTR lpValueBuf, LPDWORD ldwTotsize)
655 DWORD maxBytes = *ldwTotsize;
657 LPSTR bufptr = (LPSTR)lpValueBuf;
660 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
662 for(i=0; i < num_vals; ++i)
664 val_list[i].ve_valuelen=0;
665 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
666 if(status != ERROR_SUCCESS)
671 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
673 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
674 bufptr, &val_list[i].ve_valuelen);
675 if(status != ERROR_SUCCESS)
680 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
682 bufptr += val_list[i].ve_valuelen;
685 *ldwTotsize += val_list[i].ve_valuelen;
687 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
690 /******************************************************************************
691 * RegQueryInfoKeyA [ADVAPI32.@]
693 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
694 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
695 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
696 LPDWORD security, FILETIME *modif )
699 char buffer[256], *buf_ptr = buffer;
700 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
701 DWORD total_size, len;
703 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
704 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
706 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
707 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
709 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
710 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
712 if (class || class_len)
714 /* retry with a dynamically allocated buffer */
715 while (status == STATUS_BUFFER_OVERFLOW)
717 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
718 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
719 return ERROR_NOT_ENOUGH_MEMORY;
720 info = (KEY_FULL_INFORMATION *)buf_ptr;
721 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
724 if (status) goto done;
726 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
729 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
732 if (class && !status)
734 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
739 else status = STATUS_SUCCESS;
741 if (subkeys) *subkeys = info->SubKeys;
742 if (max_subkey) *max_subkey = info->MaxNameLen;
743 if (max_class) *max_class = info->MaxClassLen;
744 if (values) *values = info->Values;
745 if (max_value) *max_value = info->MaxValueNameLen;
746 if (max_data) *max_data = info->MaxValueDataLen;
747 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
750 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
751 return RtlNtStatusToDosError( status );
755 /******************************************************************************
756 * RegCloseKey [ADVAPI32.@]
758 * Releases the handle of the specified key
761 * hkey [I] Handle of key to close
764 * Success: ERROR_SUCCESS
765 * Failure: Error code
767 DWORD WINAPI RegCloseKey( HKEY hkey )
769 if (!hkey || hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
770 return RtlNtStatusToDosError( NtClose( hkey ) );
774 /******************************************************************************
775 * RegDeleteKeyW [ADVAPI32.@]
778 * hkey [I] Handle to open key
779 * name [I] Name of subkey to delete
782 * Success: ERROR_SUCCESS
783 * Failure: Error code
785 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
790 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
794 ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
796 else if (!(ret = RegOpenKeyExW( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
798 if (!is_version_nt()) /* win95 does recursive key deletes */
800 WCHAR name[MAX_PATH];
802 while(!RegEnumKeyW(tmp, 0, name, sizeof(name)))
804 if(RegDeleteKeyW(tmp, name)) /* recurse */
808 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
811 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
816 /******************************************************************************
817 * RegDeleteKeyA [ADVAPI32.@]
819 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
824 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
828 ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
830 else if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
832 if (!is_version_nt()) /* win95 does recursive key deletes */
836 while(!RegEnumKeyA(tmp, 0, name, sizeof(name)))
838 if(RegDeleteKeyA(tmp, name)) /* recurse */
842 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
845 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
851 /******************************************************************************
852 * RegSetValueExW [ADVAPI32.@]
854 * Sets the data and type of a value under a register key
857 * hkey [I] Handle of key to set value for
858 * name [I] Name of value to set
859 * reserved [I] Reserved - must be zero
860 * type [I] Flag for value type
861 * data [I] Address of value data
862 * count [I] Size of value data
865 * Success: ERROR_SUCCESS
866 * Failure: Error code
869 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
870 * NT does definitely care (aj)
872 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
873 DWORD type, CONST BYTE *data, DWORD count )
875 UNICODE_STRING nameW;
877 if (!is_version_nt()) /* win95 */
879 if (type == REG_SZ) count = (strlenW( (WCHAR *)data ) + 1) * sizeof(WCHAR);
881 else if (count && is_string(type))
883 LPCWSTR str = (LPCWSTR)data;
884 /* if user forgot to count terminating null, add it (yes NT does this) */
885 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
886 count += sizeof(WCHAR);
888 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
890 RtlInitUnicodeString( &nameW, name );
891 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
895 /******************************************************************************
896 * RegSetValueExA [ADVAPI32.@]
898 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
899 CONST BYTE *data, DWORD count )
905 if (!is_version_nt()) /* win95 */
907 if (type == REG_SZ) count = strlen(data) + 1;
909 else if (count && is_string(type))
911 /* if user forgot to count terminating null, add it (yes NT does this) */
912 if (data[count-1] && !data[count]) count++;
915 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
917 if (is_string( type )) /* need to convert to Unicode */
920 RtlMultiByteToUnicodeSize( &lenW, data, count );
921 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
922 RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
924 data = (BYTE *)dataW;
927 RtlInitAnsiString( &nameA, name );
928 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
931 status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
933 if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
934 return RtlNtStatusToDosError( status );
938 /******************************************************************************
939 * RegSetValueW [ADVAPI32.@]
941 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
946 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
948 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
950 if (name && name[0]) /* need to create the subkey */
952 if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
955 ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
956 (strlenW( data ) + 1) * sizeof(WCHAR) );
957 if (subkey != hkey) RegCloseKey( subkey );
962 /******************************************************************************
963 * RegSetValueA [ADVAPI32.@]
965 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
970 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
972 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
974 if (name && name[0]) /* need to create the subkey */
976 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
978 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
979 if (subkey != hkey) RegCloseKey( subkey );
985 /******************************************************************************
986 * RegQueryValueExW [ADVAPI32.@]
988 * Retrieves type and data for a specified name associated with an open key
991 * hkey [I] Handle of key to query
992 * name [I] Name of value to query
993 * reserved [I] Reserved - must be NULL
994 * type [O] Address of buffer for value type. If NULL, the type
996 * data [O] Address of data buffer. If NULL, the actual data is
998 * count [I/O] Address of data buffer size
1001 * ERROR_SUCCESS: Success
1002 * ERROR_MORE_DATA: !!! if the specified buffer is not big enough to hold the data
1003 * buffer is left untouched. The MS-documentation is wrong (js) !!!
1005 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1006 LPBYTE data, LPDWORD count )
1009 UNICODE_STRING name_str;
1011 char buffer[256], *buf_ptr = buffer;
1012 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1013 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1015 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1016 hkey, debugstr_w(name), reserved, type, data, count,
1017 (count && data) ? *count : 0 );
1019 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1020 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1022 RtlInitUnicodeString( &name_str, name );
1024 if (data) total_size = min( sizeof(buffer), *count + info_size );
1025 else total_size = info_size;
1027 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1028 buffer, total_size, &total_size );
1029 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1033 /* retry with a dynamically allocated buffer */
1034 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1036 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1037 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1038 return ERROR_NOT_ENOUGH_MEMORY;
1039 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1040 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1041 buf_ptr, total_size, &total_size );
1046 memcpy( data, buf_ptr + info_size, total_size - info_size );
1047 /* if the type is REG_SZ and data is not 0-terminated
1048 * and there is enough space in the buffer NT appends a \0 */
1049 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1051 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1052 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1055 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1057 else status = STATUS_SUCCESS;
1059 if (type) *type = info->Type;
1060 if (count) *count = total_size - info_size;
1063 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1064 return RtlNtStatusToDosError(status);
1068 /******************************************************************************
1069 * RegQueryValueExA [ADVAPI32.@]
1072 * the documentation is wrong: if the buffer is too small it remains untouched
1074 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
1075 LPBYTE data, LPDWORD count )
1080 char buffer[256], *buf_ptr = buffer;
1081 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1082 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1084 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1085 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1087 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1088 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1090 RtlInitAnsiString( &nameA, name );
1091 if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1093 return RtlNtStatusToDosError(status);
1095 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1096 KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
1097 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1099 /* we need to fetch the contents for a string type even if not requested,
1100 * because we need to compute the length of the ASCII string. */
1101 if (data || is_string(info->Type))
1103 /* retry with a dynamically allocated buffer */
1104 while (status == STATUS_BUFFER_OVERFLOW)
1106 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1107 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1109 status = STATUS_NO_MEMORY;
1112 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1113 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1114 KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1117 if (status) goto done;
1119 if (is_string(info->Type))
1123 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1124 total_size - info_size );
1127 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1130 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1131 total_size - info_size );
1132 /* if the type is REG_SZ and data is not 0-terminated
1133 * and there is enough space in the buffer NT appends a \0 */
1134 if (len < *count && data[len-1]) data[len] = 0;
1137 total_size = len + info_size;
1141 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1142 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1145 else status = STATUS_SUCCESS;
1147 if (type) *type = info->Type;
1148 if (count) *count = total_size - info_size;
1151 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1152 return RtlNtStatusToDosError(status);
1156 /******************************************************************************
1157 * RegQueryValueW [ADVAPI32.@]
1159 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1164 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1166 if (name && name[0])
1168 if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1170 ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1171 if (subkey != hkey) RegCloseKey( subkey );
1172 if (ret == ERROR_FILE_NOT_FOUND)
1174 /* return empty string if default value not found */
1175 if (data) *data = 0;
1176 if (count) *count = 1;
1177 ret = ERROR_SUCCESS;
1183 /******************************************************************************
1184 * RegQueryValueA [ADVAPI32.@]
1186 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1191 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1193 if (name && name[0])
1195 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1197 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1198 if (subkey != hkey) RegCloseKey( subkey );
1199 if (ret == ERROR_FILE_NOT_FOUND)
1201 /* return empty string if default value not found */
1202 if (data) *data = 0;
1203 if (count) *count = 1;
1204 ret = ERROR_SUCCESS;
1210 /******************************************************************************
1211 * RegEnumValueW [ADVAPI32.@]
1214 * hkey [I] Handle to key to query
1215 * index [I] Index of value to query
1216 * value [O] Value string
1217 * val_count [I/O] Size of value buffer (in wchars)
1218 * reserved [I] Reserved
1219 * type [O] Type code
1220 * data [O] Value data
1221 * count [I/O] Size of data buffer (in bytes)
1224 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1225 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1229 char buffer[256], *buf_ptr = buffer;
1230 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1231 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1233 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1234 hkey, index, value, val_count, reserved, type, data, count );
1236 /* NT only checks count, not val_count */
1237 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1238 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1240 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1241 if (data) total_size += *count;
1242 total_size = min( sizeof(buffer), total_size );
1244 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1245 buffer, total_size, &total_size );
1246 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1250 /* retry with a dynamically allocated buffer */
1251 while (status == STATUS_BUFFER_OVERFLOW)
1253 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1254 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1255 return ERROR_NOT_ENOUGH_MEMORY;
1256 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1257 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1258 buf_ptr, total_size, &total_size );
1261 if (status) goto done;
1265 if (info->NameLength/sizeof(WCHAR) >= *val_count)
1267 status = STATUS_BUFFER_OVERFLOW;
1270 memcpy( value, info->Name, info->NameLength );
1271 *val_count = info->NameLength / sizeof(WCHAR);
1272 value[*val_count] = 0;
1277 if (total_size - info->DataOffset > *count)
1279 status = STATUS_BUFFER_OVERFLOW;
1282 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1283 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1285 /* if the type is REG_SZ and data is not 0-terminated
1286 * and there is enough space in the buffer NT appends a \0 */
1287 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1288 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1292 else status = STATUS_SUCCESS;
1295 if (type) *type = info->Type;
1296 if (count) *count = info->DataLength;
1299 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1300 return RtlNtStatusToDosError(status);
1304 /******************************************************************************
1305 * RegEnumValueA [ADVAPI32.@]
1307 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1308 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1312 char buffer[256], *buf_ptr = buffer;
1313 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1314 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1316 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1317 hkey, index, value, val_count, reserved, type, data, count );
1319 /* NT only checks count, not val_count */
1320 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1321 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1323 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1324 if (data) total_size += *count;
1325 total_size = min( sizeof(buffer), total_size );
1327 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1328 buffer, total_size, &total_size );
1329 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1331 /* we need to fetch the contents for a string type even if not requested,
1332 * because we need to compute the length of the ASCII string. */
1333 if (value || data || is_string(info->Type))
1335 /* retry with a dynamically allocated buffer */
1336 while (status == STATUS_BUFFER_OVERFLOW)
1338 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1339 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1340 return ERROR_NOT_ENOUGH_MEMORY;
1341 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1342 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1343 buf_ptr, total_size, &total_size );
1346 if (status) goto done;
1348 if (is_string(info->Type))
1351 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1352 total_size - info->DataOffset );
1355 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1358 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1359 total_size - info->DataOffset );
1360 /* if the type is REG_SZ and data is not 0-terminated
1361 * and there is enough space in the buffer NT appends a \0 */
1362 if (len < *count && data[len-1]) data[len] = 0;
1365 info->DataLength = len;
1369 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1370 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1373 if (value && !status)
1377 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1378 if (len >= *val_count)
1380 status = STATUS_BUFFER_OVERFLOW;
1383 len = *val_count - 1;
1384 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1390 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1396 else status = STATUS_SUCCESS;
1398 if (type) *type = info->Type;
1399 if (count) *count = info->DataLength;
1402 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1403 return RtlNtStatusToDosError(status);
1408 /******************************************************************************
1409 * RegDeleteValueW [ADVAPI32.@]
1411 * See RegDeleteValueA.
1413 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1415 UNICODE_STRING nameW;
1417 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1419 RtlInitUnicodeString( &nameW, name );
1420 return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1424 /******************************************************************************
1425 * RegDeleteValueA [ADVAPI32.@]
1427 * Delete a value from the registry.
1430 * hkey [I] Registry handle of the key holding the value
1431 * name [I] Name of the value under hkey to delete
1435 * Failure: A standard Windows error code.
1437 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1442 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1444 RtlInitAnsiString( &nameA, name );
1445 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1447 status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1448 return RtlNtStatusToDosError( status );
1452 /******************************************************************************
1453 * RegLoadKeyW [ADVAPI32.@]
1456 * hkey [I] Handle of open key
1457 * subkey [I] Address of name of subkey
1458 * filename [I] Address of filename for registry information
1460 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1463 DWORD ret, len, err = GetLastError();
1466 TRACE( "(%p,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
1468 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1469 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1470 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1472 len = strlenW( subkey ) * sizeof(WCHAR);
1473 if (len > MAX_PATH*sizeof(WCHAR)) return ERROR_INVALID_PARAMETER;
1475 if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1476 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1478 ret = GetLastError();
1482 RegCreateKeyW(hkey,subkey,&shkey);
1484 SERVER_START_REQ( load_registry )
1488 wine_server_add_data( req, subkey, len );
1489 ret = RtlNtStatusToDosError( wine_server_call(req) );
1492 CloseHandle( file );
1496 SetLastError( err ); /* restore the last error code */
1501 /******************************************************************************
1502 * RegLoadKeyA [ADVAPI32.@]
1504 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1506 WCHAR buffer[MAX_PATH];
1508 DWORD ret, len, err = GetLastError();
1511 TRACE( "(%p,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
1513 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1514 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1515 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1517 if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
1518 return ERROR_INVALID_PARAMETER;
1520 if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1521 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1523 ret = GetLastError();
1527 RegCreateKeyA(hkey,subkey,&shkey);
1529 SERVER_START_REQ( load_registry )
1533 wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
1534 ret = RtlNtStatusToDosError( wine_server_call(req) );
1537 CloseHandle( file );
1541 SetLastError( err ); /* restore the last error code */
1546 /******************************************************************************
1547 * RegSaveKeyW [ADVAPI32.@]
1550 * hkey [I] Handle of key where save begins
1551 * lpFile [I] Address of filename to save to
1552 * sa [I] Address of security structure
1554 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1556 static const WCHAR format[] =
1557 {'r','e','g','%','0','4','x','.','t','m','p',0};
1558 WCHAR buffer[MAX_PATH];
1564 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
1566 if (!file || !*file) return ERROR_INVALID_PARAMETER;
1567 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1569 err = GetLastError();
1570 GetFullPathNameW( file, sizeof(buffer)/sizeof(WCHAR), buffer, &nameW );
1574 snprintfW( nameW, 16, format, count++ );
1575 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
1576 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1577 if (handle != INVALID_HANDLE_VALUE) break;
1578 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1580 /* Something gone haywire ? Please report if this happens abnormally */
1582 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);
1585 SERVER_START_REQ( save_registry )
1589 ret = RtlNtStatusToDosError( wine_server_call( req ) );
1593 CloseHandle( handle );
1596 if (!MoveFileExW( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1598 ERR( "Failed to move %s to %s\n", debugstr_w(buffer),
1600 ret = GetLastError();
1603 if (ret) DeleteFileW( buffer );
1606 SetLastError( err ); /* restore last error code */
1611 /******************************************************************************
1612 * RegSaveKeyA [ADVAPI32.@]
1614 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1616 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
1620 RtlInitAnsiString(&fileA, file);
1621 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
1622 return RtlNtStatusToDosError( status );
1623 return RegSaveKeyW(hkey, fileW->Buffer, sa);
1627 /******************************************************************************
1628 * RegRestoreKeyW [ADVAPI32.@]
1631 * hkey [I] Handle of key where restore begins
1632 * lpFile [I] Address of filename containing saved tree
1633 * dwFlags [I] Optional flags
1635 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
1637 TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
1639 /* It seems to do this check before the hkey check */
1640 if (!lpFile || !*lpFile)
1641 return ERROR_INVALID_PARAMETER;
1643 FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
1645 /* Check for file existence */
1647 return ERROR_SUCCESS;
1651 /******************************************************************************
1652 * RegRestoreKeyA [ADVAPI32.@]
1654 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
1656 UNICODE_STRING lpFileW;
1659 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
1660 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
1661 RtlFreeUnicodeString( &lpFileW );
1666 /******************************************************************************
1667 * RegUnLoadKeyW [ADVAPI32.@]
1670 * hkey [I] Handle of open key
1671 * lpSubKey [I] Address of name of subkey to unload
1673 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
1678 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
1680 ret = RegOpenKeyW(hkey,lpSubKey,&shkey);
1682 return ERROR_INVALID_PARAMETER;
1684 SERVER_START_REQ( unload_registry )
1687 ret = RtlNtStatusToDosError( wine_server_call(req) );
1696 /******************************************************************************
1697 * RegUnLoadKeyA [ADVAPI32.@]
1699 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
1701 UNICODE_STRING lpSubKeyW;
1704 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
1705 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
1706 RtlFreeUnicodeString( &lpSubKeyW );
1711 /******************************************************************************
1712 * RegReplaceKeyW [ADVAPI32.@]
1715 * hkey [I] Handle of open key
1716 * lpSubKey [I] Address of name of subkey
1717 * lpNewFile [I] Address of filename for file with new data
1718 * lpOldFile [I] Address of filename for backup file
1720 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
1723 FIXME("(%p,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
1724 debugstr_w(lpNewFile),debugstr_w(lpOldFile));
1725 return ERROR_SUCCESS;
1729 /******************************************************************************
1730 * RegReplaceKeyA [ADVAPI32.@]
1732 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
1735 UNICODE_STRING lpSubKeyW;
1736 UNICODE_STRING lpNewFileW;
1737 UNICODE_STRING lpOldFileW;
1740 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
1741 RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, lpOldFile );
1742 RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, lpNewFile );
1743 ret = RegReplaceKeyW( hkey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
1744 RtlFreeUnicodeString( &lpOldFileW );
1745 RtlFreeUnicodeString( &lpNewFileW );
1746 RtlFreeUnicodeString( &lpSubKeyW );
1751 /******************************************************************************
1752 * RegSetKeySecurity [ADVAPI32.@]
1755 * hkey [I] Open handle of key to set
1756 * SecurityInfo [I] Descriptor contents
1757 * pSecurityDesc [I] Address of descriptor for key
1759 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
1760 PSECURITY_DESCRIPTOR pSecurityDesc )
1762 TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
1764 /* It seems to perform this check before the hkey check */
1765 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
1766 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
1767 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
1768 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
1771 return ERROR_INVALID_PARAMETER;
1774 return ERROR_INVALID_PARAMETER;
1776 FIXME(":(%p,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
1778 return ERROR_SUCCESS;
1782 /******************************************************************************
1783 * RegGetKeySecurity [ADVAPI32.@]
1784 * Retrieves a copy of security descriptor protecting the registry key
1787 * hkey [I] Open handle of key to set
1788 * SecurityInformation [I] Descriptor contents
1789 * pSecurityDescriptor [O] Address of descriptor for key
1790 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
1793 * Success: ERROR_SUCCESS
1794 * Failure: Error code
1796 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
1797 PSECURITY_DESCRIPTOR pSecurityDescriptor,
1798 LPDWORD lpcbSecurityDescriptor )
1800 TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
1801 lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1803 /* FIXME: Check for valid SecurityInformation values */
1805 if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
1806 return ERROR_INSUFFICIENT_BUFFER;
1808 FIXME("(%p,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
1809 pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1811 /* Do not leave security descriptor filled with garbage */
1812 RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1814 return ERROR_SUCCESS;
1818 /******************************************************************************
1819 * RegFlushKey [ADVAPI32.@]
1820 * Immediately writes key to registry.
1821 * Only returns after data has been written to disk.
1823 * FIXME: does it really wait until data is written ?
1826 * hkey [I] Handle of key to write
1829 * Success: ERROR_SUCCESS
1830 * Failure: Error code
1832 DWORD WINAPI RegFlushKey( HKEY hkey )
1834 hkey = get_special_root_hkey( hkey );
1835 if (!hkey) return ERROR_INVALID_HANDLE;
1837 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
1841 /******************************************************************************
1842 * RegConnectRegistryW [ADVAPI32.@]
1845 * lpMachineName [I] Address of name of remote computer
1846 * hHey [I] Predefined registry handle
1847 * phkResult [I] Address of buffer for remote registry handle
1849 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
1854 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
1856 if (!lpMachineName || !*lpMachineName) {
1857 /* Use the local machine name */
1858 ret = RegOpenKeyW( hKey, NULL, phkResult );
1860 else if (lpMachineName[0] != '\\' || lpMachineName[1] != '\\')
1861 ret = ERROR_BAD_NETPATH;
1864 WCHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1865 DWORD len = sizeof(compName) / sizeof(WCHAR);
1867 if (GetComputerNameW(compName, &len))
1869 if (!strcmpiW(lpMachineName + 2, compName))
1870 ret = RegOpenKeyW(hKey, NULL, phkResult);
1873 FIXME("Cannot connect to %s\n",debugstr_w(lpMachineName));
1874 ret = ERROR_BAD_NETPATH;
1878 ret = GetLastError();
1884 /******************************************************************************
1885 * RegConnectRegistryA [ADVAPI32.@]
1887 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, PHKEY reskey )
1889 UNICODE_STRING machineW;
1892 RtlCreateUnicodeStringFromAsciiz( &machineW, machine );
1893 ret = RegConnectRegistryW( machineW.Buffer, hkey, reskey );
1894 RtlFreeUnicodeString( &machineW );
1899 /******************************************************************************
1900 * RegNotifyChangeKeyValue [ADVAPI32.@]
1903 * hkey [I] Handle of key to watch
1904 * fWatchSubTree [I] Flag for subkey notification
1905 * fdwNotifyFilter [I] Changes to be reported
1906 * hEvent [I] Handle of signaled event
1907 * fAsync [I] Flag for asynchronous reporting
1909 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
1910 DWORD fdwNotifyFilter, HANDLE hEvent,
1915 TRACE("(%p,%i,%ld,%p,%i)\n",hkey,fWatchSubTree,fdwNotifyFilter,
1919 hEvent = CreateEventA(NULL, 0, 0, NULL);
1921 SERVER_START_REQ( set_registry_notification )
1924 req->event = hEvent;
1925 req->subtree = fWatchSubTree;
1926 req->filter = fdwNotifyFilter;
1927 ret = RtlNtStatusToDosError( wine_server_call(req) );
1933 if( ret == ERROR_SUCCESS )
1934 WaitForSingleObject( hEvent, INFINITE );
1935 CloseHandle( hEvent );
1941 /******************************************************************************
1942 * RegOpenUserClassesRoot [ADVAPI32.@]
1943 * Opens the HKEY_CLASSES_ROOT key for the user represented by the token
1946 * hToken [I] Handle of token representing user
1947 * dwOptions [I] Reserved
1948 * samDesired [I] Desired access rights
1949 * phkResult [O] Address of buffer for opened handle
1952 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
1953 * HKEY_LOCAL_MACHINE\Software\Classes and the
1954 * HKEY_CURRENT_USER\Software\Classes keys merged together.
1956 LONG WINAPI RegOpenUserClassesRoot(
1963 FIXME("(%p, 0x%lx, 0x%lx, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
1965 *phkResult = HKEY_CLASSES_ROOT;
1966 return ERROR_SUCCESS;