advapi32: Fix registry API prototypes to match the PSDK.
[wine/wine64.git] / dlls / advapi32 / registry.c
blob81ed4845d34e87f8ceb3143f7594296a9828157a
1 /*
2 * Registry management
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winreg.h"
38 #include "winerror.h"
39 #include "winternl.h"
40 #include "winuser.h"
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(reg);
47 /* allowed bits for access mask */
48 #define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
50 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
51 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
52 #define NB_SPECIAL_ROOT_KEYS ((UINT)HKEY_SPECIAL_ROOT_LAST - (UINT)HKEY_SPECIAL_ROOT_FIRST + 1)
54 static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
55 static BOOL hkcu_cache_disabled;
57 static const WCHAR name_CLASSES_ROOT[] =
58 {'M','a','c','h','i','n','e','\\',
59 'S','o','f','t','w','a','r','e','\\',
60 'C','l','a','s','s','e','s',0};
61 static const WCHAR name_LOCAL_MACHINE[] =
62 {'M','a','c','h','i','n','e',0};
63 static const WCHAR name_USERS[] =
64 {'U','s','e','r',0};
65 static const WCHAR name_PERFORMANCE_DATA[] =
66 {'P','e','r','f','D','a','t','a',0};
67 static const WCHAR name_CURRENT_CONFIG[] =
68 {'M','a','c','h','i','n','e','\\',
69 'S','y','s','t','e','m','\\',
70 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
71 'H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s','\\',
72 'C','u','r','r','e','n','t',0};
73 static const WCHAR name_DYN_DATA[] =
74 {'D','y','n','D','a','t','a',0};
76 #define DECL_STR(key) { sizeof(name_##key)-sizeof(WCHAR), sizeof(name_##key), (LPWSTR)name_##key }
77 static UNICODE_STRING root_key_names[NB_SPECIAL_ROOT_KEYS] =
79 DECL_STR(CLASSES_ROOT),
80 { 0, 0, NULL }, /* HKEY_CURRENT_USER is determined dynamically */
81 DECL_STR(LOCAL_MACHINE),
82 DECL_STR(USERS),
83 DECL_STR(PERFORMANCE_DATA),
84 DECL_STR(CURRENT_CONFIG),
85 DECL_STR(DYN_DATA)
87 #undef DECL_STR
90 /* check if value type needs string conversion (Ansi<->Unicode) */
91 inline static int is_string( DWORD type )
93 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
96 /* check if current version is NT or Win95 */
97 inline static int is_version_nt(void)
99 return !(GetVersion() & 0x80000000);
102 /* create one of the HKEY_* special root keys */
103 static HKEY create_special_root_hkey( HANDLE hkey, DWORD access )
105 HKEY ret = 0;
106 int idx = (UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST;
108 if (hkey == HKEY_CURRENT_USER)
110 if (RtlOpenCurrentUser( access, &hkey )) return 0;
111 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
113 /* don't cache the key in the table if caching is disabled */
114 if (hkcu_cache_disabled)
115 return hkey;
117 else
119 OBJECT_ATTRIBUTES attr;
121 attr.Length = sizeof(attr);
122 attr.RootDirectory = 0;
123 attr.ObjectName = &root_key_names[idx];
124 attr.Attributes = 0;
125 attr.SecurityDescriptor = NULL;
126 attr.SecurityQualityOfService = NULL;
127 if (NtCreateKey( &hkey, access, &attr, 0, NULL, 0, NULL )) return 0;
128 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
131 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
132 ret = hkey;
133 else
134 NtClose( hkey ); /* somebody beat us to it */
135 return ret;
138 /* map the hkey from special root to normal key if necessary */
139 inline static HKEY get_special_root_hkey( HKEY hkey )
141 HKEY ret = hkey;
143 if ((hkey >= HKEY_SPECIAL_ROOT_FIRST) && (hkey <= HKEY_SPECIAL_ROOT_LAST))
145 if (!(ret = special_root_keys[(UINT_PTR)hkey - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST]))
146 ret = create_special_root_hkey( hkey, KEY_ALL_ACCESS );
148 return ret;
152 /******************************************************************************
153 * RegCreateKeyExW [ADVAPI32.@]
155 * See RegCreateKeyExA.
157 LONG WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
158 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
159 PHKEY retkey, LPDWORD dispos )
161 OBJECT_ATTRIBUTES attr;
162 UNICODE_STRING nameW, classW;
164 if (reserved) return ERROR_INVALID_PARAMETER;
165 if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
166 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
168 attr.Length = sizeof(attr);
169 attr.RootDirectory = hkey;
170 attr.ObjectName = &nameW;
171 attr.Attributes = 0;
172 attr.SecurityDescriptor = NULL;
173 attr.SecurityQualityOfService = NULL;
174 RtlInitUnicodeString( &nameW, name );
175 RtlInitUnicodeString( &classW, class );
177 return RtlNtStatusToDosError( NtCreateKey( (PHANDLE)retkey, access, &attr, 0,
178 &classW, options, dispos ) );
182 /******************************************************************************
183 * RegCreateKeyExA [ADVAPI32.@]
185 * Open a registry key, creating it if it doesn't exist.
187 * PARAMS
188 * hkey [I] Handle of the parent registry key
189 * name [I] Name of the new key to open or create
190 * reserved [I] Reserved, pass 0
191 * class [I] The object type of the new key
192 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
193 * access [I] Access level desired
194 * sa [I] Security attributes for the key
195 * retkey [O] Destination for the resulting handle
196 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
198 * RETURNS
199 * Success: ERROR_SUCCESS.
200 * Failure: A standard Win32 error code. retkey remains untouched.
202 * FIXME
203 * MAXIMUM_ALLOWED in access mask not supported by server
205 LONG WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
206 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
207 PHKEY retkey, LPDWORD dispos )
209 OBJECT_ATTRIBUTES attr;
210 UNICODE_STRING classW;
211 ANSI_STRING nameA, classA;
212 NTSTATUS status;
214 if (reserved) return ERROR_INVALID_PARAMETER;
215 if (!is_version_nt())
217 access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
218 if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
220 else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
221 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
223 attr.Length = sizeof(attr);
224 attr.RootDirectory = hkey;
225 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
226 attr.Attributes = 0;
227 attr.SecurityDescriptor = NULL;
228 attr.SecurityQualityOfService = NULL;
229 RtlInitAnsiString( &nameA, name );
230 RtlInitAnsiString( &classA, class );
232 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
233 &nameA, FALSE )))
235 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
237 status = NtCreateKey( (PHANDLE)retkey, access, &attr, 0, &classW, options, dispos );
238 RtlFreeUnicodeString( &classW );
241 return RtlNtStatusToDosError( status );
245 /******************************************************************************
246 * RegCreateKeyW [ADVAPI32.@]
248 * Creates the specified reg key.
250 * PARAMS
251 * hKey [I] Handle to an open key.
252 * lpSubKey [I] Name of a key that will be opened or created.
253 * phkResult [O] Receives a handle to the opened or created key.
255 * RETURNS
256 * Success: ERROR_SUCCESS
257 * Failure: nonzero error code defined in Winerror.h
259 LONG WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR lpSubKey, PHKEY phkResult )
261 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
262 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
263 return RegCreateKeyExW( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
264 KEY_ALL_ACCESS, NULL, phkResult, NULL );
268 /******************************************************************************
269 * RegCreateKeyA [ADVAPI32.@]
271 * See RegCreateKeyW.
273 LONG WINAPI RegCreateKeyA( HKEY hkey, LPCSTR lpSubKey, PHKEY phkResult )
275 return RegCreateKeyExA( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
276 KEY_ALL_ACCESS, NULL, phkResult, NULL );
281 /******************************************************************************
282 * RegOpenKeyExW [ADVAPI32.@]
284 * See RegOpenKeyExA.
286 LONG WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
288 OBJECT_ATTRIBUTES attr;
289 UNICODE_STRING nameW;
291 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
293 attr.Length = sizeof(attr);
294 attr.RootDirectory = hkey;
295 attr.ObjectName = &nameW;
296 attr.Attributes = 0;
297 attr.SecurityDescriptor = NULL;
298 attr.SecurityQualityOfService = NULL;
299 RtlInitUnicodeString( &nameW, name );
300 return RtlNtStatusToDosError( NtOpenKey( (PHANDLE)retkey, access, &attr ) );
304 /******************************************************************************
305 * RegOpenKeyExA [ADVAPI32.@]
307 * Open a registry key.
309 * PARAMS
310 * hkey [I] Handle of open key
311 * name [I] Name of subkey to open
312 * reserved [I] Reserved - must be zero
313 * access [I] Security access mask
314 * retkey [O] Handle to open key
316 * RETURNS
317 * Success: ERROR_SUCCESS
318 * Failure: A standard Win32 error code. retkey is set to 0.
320 * NOTES
321 * Unlike RegCreateKeyExA(), this function will not create the key if it
322 * does not exist.
324 LONG WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, PHKEY retkey )
326 OBJECT_ATTRIBUTES attr;
327 STRING nameA;
328 NTSTATUS status;
330 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
332 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
334 attr.Length = sizeof(attr);
335 attr.RootDirectory = hkey;
336 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
337 attr.Attributes = 0;
338 attr.SecurityDescriptor = NULL;
339 attr.SecurityQualityOfService = NULL;
341 RtlInitAnsiString( &nameA, name );
342 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
343 &nameA, FALSE )))
345 status = NtOpenKey( (PHANDLE)retkey, access, &attr );
347 return RtlNtStatusToDosError( status );
351 /******************************************************************************
352 * RegOpenKeyW [ADVAPI32.@]
354 * See RegOpenKeyA.
356 LONG WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
358 if (!name || !*name)
360 *retkey = hkey;
361 return ERROR_SUCCESS;
363 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
367 /******************************************************************************
368 * RegOpenKeyA [ADVAPI32.@]
370 * Open a registry key.
372 * PARAMS
373 * hkey [I] Handle of parent key to open the new key under
374 * name [I] Name of the key under hkey to open
375 * retkey [O] Destination for the resulting Handle
377 * RETURNS
378 * Success: ERROR_SUCCESS
379 * Failure: A standard Win32 error code. retkey is set to 0.
381 LONG WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
383 if (!name || !*name)
385 *retkey = hkey;
386 return ERROR_SUCCESS;
388 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
392 /******************************************************************************
393 * RegOpenCurrentUser [ADVAPI32.@]
395 * Get a handle to the HKEY_CURRENT_USER key for the user
396 * the current thread is impersonating.
398 * PARAMS
399 * access [I] Desired access rights to the key
400 * retkey [O] Handle to the opened key
402 * RETURNS
403 * Success: ERROR_SUCCESS
404 * Failure: nonzero error code from Winerror.h
406 * FIXME
407 * This function is supposed to retrieve a handle to the
408 * HKEY_CURRENT_USER for the user the current thread is impersonating.
409 * Since Wine does not currently allow threads to impersonate other users,
410 * this stub should work fine.
412 LONG WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
414 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
419 /******************************************************************************
420 * RegEnumKeyExW [ADVAPI32.@]
422 * Enumerate subkeys of the specified open registry key.
424 * PARAMS
425 * hkey [I] Handle to key to enumerate
426 * index [I] Index of subkey to enumerate
427 * name [O] Buffer for subkey name
428 * name_len [O] Size of subkey buffer
429 * reserved [I] Reserved
430 * class [O] Buffer for class string
431 * class_len [O] Size of class buffer
432 * ft [O] Time key last written to
434 * RETURNS
435 * Success: ERROR_SUCCESS
436 * Failure: System error code. If there are no more subkeys available, the
437 * function returns ERROR_NO_MORE_ITEMS.
439 LONG WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
440 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
442 NTSTATUS status;
443 char buffer[256], *buf_ptr = buffer;
444 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
445 DWORD total_size;
447 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
448 name_len ? *name_len : -1, reserved, class, class_len, ft );
450 if (reserved) return ERROR_INVALID_PARAMETER;
451 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
453 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
454 buffer, sizeof(buffer), &total_size );
456 while (status == STATUS_BUFFER_OVERFLOW)
458 /* retry with a dynamically allocated buffer */
459 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
460 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
461 return ERROR_NOT_ENOUGH_MEMORY;
462 info = (KEY_NODE_INFORMATION *)buf_ptr;
463 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
464 buf_ptr, total_size, &total_size );
467 if (!status)
469 DWORD len = info->NameLength / sizeof(WCHAR);
470 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
472 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
474 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
475 status = STATUS_BUFFER_OVERFLOW;
476 else
478 *name_len = len;
479 memcpy( name, info->Name, info->NameLength );
480 name[len] = 0;
481 if (class_len)
483 *class_len = cls_len;
484 if (class)
486 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
487 class[cls_len] = 0;
493 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
494 return RtlNtStatusToDosError( status );
498 /******************************************************************************
499 * RegEnumKeyExA [ADVAPI32.@]
501 * See RegEnumKeyExW.
503 LONG WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
504 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
506 NTSTATUS status;
507 char buffer[256], *buf_ptr = buffer;
508 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
509 DWORD total_size;
511 TRACE( "(%p,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
512 name_len ? *name_len : -1, reserved, class, class_len, ft );
514 if (reserved) return ERROR_INVALID_PARAMETER;
515 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
517 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
518 buffer, sizeof(buffer), &total_size );
520 while (status == STATUS_BUFFER_OVERFLOW)
522 /* retry with a dynamically allocated buffer */
523 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
524 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
525 return ERROR_NOT_ENOUGH_MEMORY;
526 info = (KEY_NODE_INFORMATION *)buf_ptr;
527 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
528 buf_ptr, total_size, &total_size );
531 if (!status)
533 DWORD len, cls_len;
535 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
536 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
537 info->ClassLength );
538 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
540 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
541 status = STATUS_BUFFER_OVERFLOW;
542 else
544 *name_len = len;
545 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
546 name[len] = 0;
547 if (class_len)
549 *class_len = cls_len;
550 if (class)
552 RtlUnicodeToMultiByteN( class, cls_len, NULL,
553 (WCHAR *)(buf_ptr + info->ClassOffset),
554 info->ClassLength );
555 class[cls_len] = 0;
561 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
562 return RtlNtStatusToDosError( status );
566 /******************************************************************************
567 * RegEnumKeyW [ADVAPI32.@]
569 * Enumerates subkyes of the specified open reg key.
571 * PARAMS
572 * hKey [I] Handle to an open key.
573 * dwIndex [I] Index of the subkey of hKey to retrieve.
574 * lpName [O] Name of the subkey.
575 * cchName [I] Size of lpName in TCHARS.
577 * RETURNS
578 * Success: ERROR_SUCCESS
579 * Failure: system error code. If there are no more subkeys available, the
580 * function returns ERROR_NO_MORE_ITEMS.
582 LONG WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
584 return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
588 /******************************************************************************
589 * RegEnumKeyA [ADVAPI32.@]
591 * See RegEnumKeyW.
593 LONG WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
595 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
599 /******************************************************************************
600 * RegQueryInfoKeyW [ADVAPI32.@]
602 * Retrieves information about the specified registry key.
604 * PARAMS
605 * hkey [I] Handle to key to query
606 * class [O] Buffer for class string
607 * class_len [O] Size of class string buffer
608 * reserved [I] Reserved
609 * subkeys [O] Buffer for number of subkeys
610 * max_subkey [O] Buffer for longest subkey name length
611 * max_class [O] Buffer for longest class string length
612 * values [O] Buffer for number of value entries
613 * max_value [O] Buffer for longest value name length
614 * max_data [O] Buffer for longest value data length
615 * security [O] Buffer for security descriptor length
616 * modif [O] Modification time
618 * RETURNS
619 * Success: ERROR_SUCCESS
620 * Failure: system error code.
622 * NOTES
623 * - win95 allows class to be valid and class_len to be NULL
624 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
625 * - both allow class to be NULL and class_len to be NULL
626 * (it's hard to test validity, so test !NULL instead)
628 LONG WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
629 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
630 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
631 LPDWORD security, FILETIME *modif )
633 NTSTATUS status;
634 char buffer[256], *buf_ptr = buffer;
635 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
636 DWORD total_size;
638 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
639 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
641 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
642 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
644 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
645 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
647 if (class)
649 /* retry with a dynamically allocated buffer */
650 while (status == STATUS_BUFFER_OVERFLOW)
652 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
653 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
654 return ERROR_NOT_ENOUGH_MEMORY;
655 info = (KEY_FULL_INFORMATION *)buf_ptr;
656 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
659 if (status) goto done;
661 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
663 status = STATUS_BUFFER_OVERFLOW;
665 else
667 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
668 class[info->ClassLength/sizeof(WCHAR)] = 0;
671 else status = STATUS_SUCCESS;
673 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
674 if (subkeys) *subkeys = info->SubKeys;
675 if (max_subkey) *max_subkey = info->MaxNameLen;
676 if (max_class) *max_class = info->MaxClassLen;
677 if (values) *values = info->Values;
678 if (max_value) *max_value = info->MaxValueNameLen;
679 if (max_data) *max_data = info->MaxValueDataLen;
680 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
682 done:
683 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
684 return RtlNtStatusToDosError( status );
688 /******************************************************************************
689 * RegQueryMultipleValuesA [ADVAPI32.@]
691 * Retrieves the type and data for a list of value names associated with a key.
693 * PARAMS
694 * hKey [I] Handle to an open key.
695 * val_list [O] Array of VALENT structures that describes the entries.
696 * num_vals [I] Number of elements in val_list.
697 * lpValueBuf [O] Pointer to a buffer that receives the data for each value.
698 * ldwTotsize [I/O] Size of lpValueBuf.
700 * RETURNS
701 * Success: ERROR_SUCCESS. ldwTotsize contains num bytes copied.
702 * Failure: nonzero error code from Winerror.h ldwTotsize contains num needed
703 * bytes.
705 LONG WINAPI RegQueryMultipleValuesA( HKEY hkey, PVALENTA val_list, DWORD num_vals,
706 LPSTR lpValueBuf, LPDWORD ldwTotsize )
708 unsigned int i;
709 DWORD maxBytes = *ldwTotsize;
710 HRESULT status;
711 LPSTR bufptr = lpValueBuf;
712 *ldwTotsize = 0;
714 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
716 for(i=0; i < num_vals; ++i)
719 val_list[i].ve_valuelen=0;
720 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
721 if(status != ERROR_SUCCESS)
723 return status;
726 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
728 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
729 (LPBYTE)bufptr, &val_list[i].ve_valuelen);
730 if(status != ERROR_SUCCESS)
732 return status;
735 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
737 bufptr += val_list[i].ve_valuelen;
740 *ldwTotsize += val_list[i].ve_valuelen;
742 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
746 /******************************************************************************
747 * RegQueryMultipleValuesW [ADVAPI32.@]
749 * See RegQueryMultipleValuesA.
751 LONG WINAPI RegQueryMultipleValuesW( HKEY hkey, PVALENTW val_list, DWORD num_vals,
752 LPWSTR lpValueBuf, LPDWORD ldwTotsize )
754 unsigned int i;
755 DWORD maxBytes = *ldwTotsize;
756 HRESULT status;
757 LPSTR bufptr = (LPSTR)lpValueBuf;
758 *ldwTotsize = 0;
760 TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
762 for(i=0; i < num_vals; ++i)
764 val_list[i].ve_valuelen=0;
765 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
766 if(status != ERROR_SUCCESS)
768 return status;
771 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
773 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
774 (LPBYTE)bufptr, &val_list[i].ve_valuelen);
775 if(status != ERROR_SUCCESS)
777 return status;
780 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
782 bufptr += val_list[i].ve_valuelen;
785 *ldwTotsize += val_list[i].ve_valuelen;
787 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
790 /******************************************************************************
791 * RegQueryInfoKeyA [ADVAPI32.@]
793 * Retrieves information about a registry key.
795 * PARAMS
796 * hKey [I] Handle to an open key.
797 * lpClass [O] Class string of the key.
798 * lpcClass [I/O] size of lpClass.
799 * lpReserved [I] Reserved; must be NULL.
800 * lpcSubKeys [O] Number of subkeys contained by the key.
801 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
802 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
803 * class in TCHARS.
804 * lpcValues [O] Number of values associated with the key.
805 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
806 * lpcMaxValueLen [O] Longest data component among the key's values
807 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
808 * lpftLastWriteTime [O] FILETIME strucutre that is the last write time.
810 * RETURNS
811 * Success: ERROR_SUCCESS
812 * Failure: nonzero error code from Winerror.h
814 LONG WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
815 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
816 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
817 LPDWORD security, FILETIME *modif )
819 NTSTATUS status;
820 char buffer[256], *buf_ptr = buffer;
821 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
822 DWORD total_size, len;
824 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
825 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
827 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
828 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
830 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
831 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
833 if (class || class_len)
835 /* retry with a dynamically allocated buffer */
836 while (status == STATUS_BUFFER_OVERFLOW)
838 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
839 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
840 return ERROR_NOT_ENOUGH_MEMORY;
841 info = (KEY_FULL_INFORMATION *)buf_ptr;
842 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
845 if (status) goto done;
847 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
848 if (class_len)
850 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
851 *class_len = len;
853 if (class && !status)
855 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
856 info->ClassLength );
857 class[len] = 0;
860 else status = STATUS_SUCCESS;
862 if (subkeys) *subkeys = info->SubKeys;
863 if (max_subkey) *max_subkey = info->MaxNameLen;
864 if (max_class) *max_class = info->MaxClassLen;
865 if (values) *values = info->Values;
866 if (max_value) *max_value = info->MaxValueNameLen;
867 if (max_data) *max_data = info->MaxValueDataLen;
868 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
870 done:
871 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
872 return RtlNtStatusToDosError( status );
876 /******************************************************************************
877 * RegCloseKey [ADVAPI32.@]
879 * Close an open registry key.
881 * PARAMS
882 * hkey [I] Handle of key to close
884 * RETURNS
885 * Success: ERROR_SUCCESS
886 * Failure: Error code
888 LONG WINAPI RegCloseKey( HKEY hkey )
890 if (!hkey) return ERROR_INVALID_HANDLE;
891 if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
892 return RtlNtStatusToDosError( NtClose( hkey ) );
896 /******************************************************************************
897 * RegDeleteKeyW [ADVAPI32.@]
899 * See RegDeleteKeyA.
901 LONG WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
903 DWORD ret;
904 HKEY tmp;
906 if (!name) return ERROR_INVALID_PARAMETER;
908 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
910 if (!(ret = RegOpenKeyExW( hkey, name, 0, DELETE, &tmp )))
912 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
913 RegCloseKey( tmp );
915 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
916 return ret;
920 /******************************************************************************
921 * RegDeleteKeyA [ADVAPI32.@]
923 * Delete a registry key.
925 * PARAMS
926 * hkey [I] Handle to parent key containing the key to delete
927 * name [I] Name of the key user hkey to delete
929 * NOTES
931 * MSDN is wrong when it says that hkey must be opened with the DELETE access
932 * right. In reality, it opens a new handle with DELETE access.
934 * RETURNS
935 * Success: ERROR_SUCCESS
936 * Failure: Error code
938 LONG WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
940 DWORD ret;
941 HKEY tmp;
943 if (!name) return ERROR_INVALID_PARAMETER;
945 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
947 if (!(ret = RegOpenKeyExA( hkey, name, 0, DELETE, &tmp )))
949 if (!is_version_nt()) /* win95 does recursive key deletes */
951 CHAR name[MAX_PATH];
953 while(!RegEnumKeyA(tmp, 0, name, sizeof(name)))
955 if(RegDeleteKeyA(tmp, name)) /* recurse */
956 break;
959 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
960 RegCloseKey( tmp );
962 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
963 return ret;
968 /******************************************************************************
969 * RegSetValueExW [ADVAPI32.@]
971 * Set the data and contents of a registry value.
973 * PARAMS
974 * hkey [I] Handle of key to set value for
975 * name [I] Name of value to set
976 * reserved [I] Reserved, must be zero
977 * type [I] Type of the value being set
978 * data [I] The new contents of the value to set
979 * count [I] Size of data
981 * RETURNS
982 * Success: ERROR_SUCCESS
983 * Failure: Error code
985 LONG WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
986 DWORD type, CONST BYTE *data, DWORD count )
988 UNICODE_STRING nameW;
990 /* no need for version check, not implemented on win9x anyway */
991 if (count && is_string(type))
993 LPCWSTR str = (LPCWSTR)data;
994 /* if user forgot to count terminating null, add it (yes NT does this) */
995 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
996 count += sizeof(WCHAR);
998 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1000 RtlInitUnicodeString( &nameW, name );
1001 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
1005 /******************************************************************************
1006 * RegSetValueExA [ADVAPI32.@]
1008 * See RegSetValueExW.
1010 * NOTES
1011 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1012 * NT does definitely care (aj)
1014 LONG WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1015 CONST BYTE *data, DWORD count )
1017 ANSI_STRING nameA;
1018 WCHAR *dataW = NULL;
1019 NTSTATUS status;
1021 if (!is_version_nt()) /* win95 */
1023 if (type == REG_SZ)
1025 if (!data) return ERROR_INVALID_PARAMETER;
1026 count = strlen((const char *)data) + 1;
1029 else if (count && is_string(type))
1031 /* if user forgot to count terminating null, add it (yes NT does this) */
1032 if (data[count-1] && !data[count]) count++;
1035 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1037 if (is_string( type )) /* need to convert to Unicode */
1039 DWORD lenW;
1040 RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1041 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
1042 RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1043 count = lenW;
1044 data = (BYTE *)dataW;
1047 RtlInitAnsiString( &nameA, name );
1048 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1049 &nameA, FALSE )))
1051 status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
1053 HeapFree( GetProcessHeap(), 0, dataW );
1054 return RtlNtStatusToDosError( status );
1058 /******************************************************************************
1059 * RegSetValueW [ADVAPI32.@]
1061 * Sets the data for the default or unnamed value of a reg key.
1063 * PARAMS
1064 * hKey [I] Handle to an open key.
1065 * lpSubKey [I] Name of a subkey of hKey.
1066 * dwType [I] Type of information to store.
1067 * lpData [I] String that contains the data to set for the default value.
1068 * cbData [I] Size of lpData.
1070 * RETURNS
1071 * Success: ERROR_SUCCESS
1072 * Failure: nonzero error code from Winerror.h
1074 LONG WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
1076 HKEY subkey = hkey;
1077 DWORD ret;
1079 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
1081 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1083 if (name && name[0]) /* need to create the subkey */
1085 if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1088 ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (const BYTE*)data,
1089 (strlenW( data ) + 1) * sizeof(WCHAR) );
1090 if (subkey != hkey) RegCloseKey( subkey );
1091 return ret;
1095 /******************************************************************************
1096 * RegSetValueA [ADVAPI32.@]
1098 * See RegSetValueW.
1100 LONG WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
1102 HKEY subkey = hkey;
1103 DWORD ret;
1105 TRACE("(%p,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
1107 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
1109 if (name && name[0]) /* need to create the subkey */
1111 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1113 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (const BYTE*)data, strlen(data)+1 );
1114 if (subkey != hkey) RegCloseKey( subkey );
1115 return ret;
1120 /******************************************************************************
1121 * RegQueryValueExW [ADVAPI32.@]
1123 * See RegQueryValueExA.
1125 LONG WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1126 LPBYTE data, LPDWORD count )
1128 NTSTATUS status;
1129 UNICODE_STRING name_str;
1130 DWORD total_size;
1131 char buffer[256], *buf_ptr = buffer;
1132 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1133 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1135 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1136 hkey, debugstr_w(name), reserved, type, data, count,
1137 (count && data) ? *count : 0 );
1139 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1140 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1142 RtlInitUnicodeString( &name_str, name );
1144 if (data) total_size = min( sizeof(buffer), *count + info_size );
1145 else total_size = info_size;
1147 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1148 buffer, total_size, &total_size );
1149 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1151 if (data)
1153 /* retry with a dynamically allocated buffer */
1154 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1156 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1157 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1158 return ERROR_NOT_ENOUGH_MEMORY;
1159 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1160 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1161 buf_ptr, total_size, &total_size );
1164 if (!status)
1166 memcpy( data, buf_ptr + info_size, total_size - info_size );
1167 /* if the type is REG_SZ and data is not 0-terminated
1168 * and there is enough space in the buffer NT appends a \0 */
1169 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1171 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1172 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1175 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1177 else status = STATUS_SUCCESS;
1179 if (type) *type = info->Type;
1180 if (count) *count = total_size - info_size;
1182 done:
1183 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1184 return RtlNtStatusToDosError(status);
1188 /******************************************************************************
1189 * RegQueryValueExA [ADVAPI32.@]
1191 * Get the type and contents of a specified value under with a key.
1193 * PARAMS
1194 * hkey [I] Handle of the key to query
1195 * name [I] Name of value under hkey to query
1196 * reserved [I] Reserved - must be NULL
1197 * type [O] Destination for the value type, or NULL if not required
1198 * data [O] Destination for the values contents, or NULL if not required
1199 * count [I/O] Size of data, updated with the number of bytes returned
1201 * RETURNS
1202 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1203 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1204 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1205 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1207 * NOTES
1208 * MSDN states that if data is too small it is partially filled. In reality
1209 * it remains untouched.
1211 LONG WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
1212 LPBYTE data, LPDWORD count )
1214 NTSTATUS status;
1215 ANSI_STRING nameA;
1216 DWORD total_size;
1217 char buffer[256], *buf_ptr = buffer;
1218 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1219 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1221 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1222 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1224 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1225 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1227 RtlInitAnsiString( &nameA, name );
1228 if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1229 &nameA, FALSE )))
1230 return RtlNtStatusToDosError(status);
1232 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1233 KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
1234 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1236 /* we need to fetch the contents for a string type even if not requested,
1237 * because we need to compute the length of the ASCII string. */
1238 if (data || is_string(info->Type))
1240 /* retry with a dynamically allocated buffer */
1241 while (status == STATUS_BUFFER_OVERFLOW)
1243 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1244 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1246 status = STATUS_NO_MEMORY;
1247 goto done;
1249 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1250 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1251 KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1254 if (status) goto done;
1256 if (is_string(info->Type))
1258 DWORD len;
1260 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1261 total_size - info_size );
1262 if (data && len)
1264 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1265 else
1267 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1268 total_size - info_size );
1269 /* if the type is REG_SZ and data is not 0-terminated
1270 * and there is enough space in the buffer NT appends a \0 */
1271 if (len < *count && data[len-1]) data[len] = 0;
1274 total_size = len + info_size;
1276 else if (data)
1278 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1279 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1282 else status = STATUS_SUCCESS;
1284 if (type) *type = info->Type;
1285 if (count) *count = total_size - info_size;
1287 done:
1288 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1289 return RtlNtStatusToDosError(status);
1293 /******************************************************************************
1294 * RegQueryValueW [ADVAPI32.@]
1296 * Retrieves the data associated with the default or unnamed value of a key.
1298 * PARAMS
1299 * hkey [I] Handle to an open key.
1300 * name [I] Name of the subkey of hKey.
1301 * data [O] Receives the string associated with the default value
1302 * of the key.
1303 * count [I/O] Size of lpValue in bytes.
1305 * RETURNS
1306 * Success: ERROR_SUCCESS
1307 * Failure: nonzero error code from Winerror.h
1309 LONG WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1311 DWORD ret;
1312 HKEY subkey = hkey;
1314 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1316 if (name && name[0])
1318 if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1320 ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, (LPDWORD)count );
1321 if (subkey != hkey) RegCloseKey( subkey );
1322 if (ret == ERROR_FILE_NOT_FOUND)
1324 /* return empty string if default value not found */
1325 if (data) *data = 0;
1326 if (count) *count = sizeof(WCHAR);
1327 ret = ERROR_SUCCESS;
1329 return ret;
1333 /******************************************************************************
1334 * RegQueryValueA [ADVAPI32.@]
1336 * See RegQueryValueW.
1338 LONG WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1340 DWORD ret;
1341 HKEY subkey = hkey;
1343 TRACE("(%p,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1345 if (name && name[0])
1347 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1349 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, (LPDWORD)count );
1350 if (subkey != hkey) RegCloseKey( subkey );
1351 if (ret == ERROR_FILE_NOT_FOUND)
1353 /* return empty string if default value not found */
1354 if (data) *data = 0;
1355 if (count) *count = 1;
1356 ret = ERROR_SUCCESS;
1358 return ret;
1362 /******************************************************************************
1363 * ADVAPI_ApplyRestrictions [internal]
1365 * Helper function for RegGetValueA/W.
1367 VOID ADVAPI_ApplyRestrictions( DWORD dwFlags, DWORD dwType, DWORD cbData,
1368 PLONG ret )
1370 /* Check if the type is restricted by the passed flags */
1371 if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1373 DWORD dwMask = 0;
1375 switch (dwType)
1377 case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1378 case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1379 case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1380 case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1381 case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1382 case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1383 case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1386 if (dwFlags & dwMask)
1388 /* Type is not restricted, check for size mismatch */
1389 if (dwType == REG_BINARY)
1391 DWORD cbExpect = 0;
1393 if ((dwFlags & RRF_RT_DWORD) == RRF_RT_DWORD)
1394 cbExpect = 4;
1395 else if ((dwFlags & RRF_RT_DWORD) == RRF_RT_QWORD)
1396 cbExpect = 8;
1398 if (cbExpect && cbData != cbExpect)
1399 *ret = ERROR_DATATYPE_MISMATCH;
1402 else *ret = ERROR_UNSUPPORTED_TYPE;
1407 /******************************************************************************
1408 * RegGetValueW [ADVAPI32.@]
1410 * Retrieves the type and data for a value name associated with a key
1411 * optionally expanding it's content and restricting it's type.
1413 * PARAMS
1414 * hKey [I] Handle to an open key.
1415 * pszSubKey [I] Name of the subkey of hKey.
1416 * pszValue [I] Name of value under hKey/szSubKey to query.
1417 * dwFlags [I] Flags restricting the value type to retrieve.
1418 * pdwType [O] Destination for the values type, may be NULL.
1419 * pvData [O] Destination for the values content, may be NULL.
1420 * pcbData [I/O] Size of pvData, updated with the size required to
1421 * retrieve the whole content.
1423 * RETURNS
1424 * Success: ERROR_SUCCESS
1425 * Failure: nonzero error code from Winerror.h
1427 * NOTES
1428 * - Unless RRF_NOEXPAND is specified REG_EXPAND_SZ is automatically expanded
1429 * and REG_SZ is retrieved instead.
1430 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1431 * without RRF_NOEXPAND is thus not allowed.
1433 LONG WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1434 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1435 LPDWORD pcbData )
1437 DWORD dwType, cbData = pcbData ? *pcbData : 0;
1438 PVOID pvBuf = NULL;
1439 LONG ret;
1441 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1442 hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1443 pvData, pcbData, cbData);
1445 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND))
1446 return ERROR_INVALID_PARAMETER;
1448 if (pszSubKey && pszSubKey[0])
1450 ret = RegOpenKeyExW(hKey, pszSubKey, 0, KEY_QUERY_VALUE, &hKey);
1451 if (ret != ERROR_SUCCESS) return ret;
1454 ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1456 /* If we are going to expand we need to read in the whole the value even
1457 * if the passed buffer was too small as the expanded string might be
1458 * smaller than the unexpanded one and could fit into cbData bytes. */
1459 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1460 (dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND)))
1462 do {
1463 HeapFree(GetProcessHeap(), 0, pvBuf);
1465 pvBuf = HeapAlloc(GetProcessHeap(), 0, cbData);
1466 if (!pvBuf)
1468 ret = ERROR_NOT_ENOUGH_MEMORY;
1469 break;
1472 if (ret == ERROR_MORE_DATA)
1473 ret = RegQueryValueExW(hKey, pszValue, NULL,
1474 &dwType, pvBuf, &cbData);
1475 else
1477 /* Even if cbData was large enough we have to copy the
1478 * string since ExpandEnvironmentStrings can't handle
1479 * overlapping buffers. */
1480 CopyMemory(pvBuf, pvData, cbData);
1483 /* Both the type or the value itself could have been modified in
1484 * between so we have to keep retrying until the buffer is large
1485 * enough or we no longer have to expand the value. */
1486 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1488 if (ret == ERROR_SUCCESS)
1490 if (dwType == REG_EXPAND_SZ)
1492 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1493 pcbData ? *pcbData : 0);
1494 dwType = REG_SZ;
1495 if(pcbData && cbData > *pcbData)
1496 ret = ERROR_MORE_DATA;
1498 else if (pcbData)
1499 CopyMemory(pvData, pvBuf, *pcbData);
1502 HeapFree(GetProcessHeap(), 0, pvBuf);
1505 if (pszSubKey && pszSubKey[0])
1506 RegCloseKey(hKey);
1508 ADVAPI_ApplyRestrictions(dwFlags, dwType, cbData, &ret);
1510 if (pcbData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1511 ZeroMemory(pvData, *pcbData);
1513 if (pdwType) *pdwType = dwType;
1514 if (pcbData) *pcbData = cbData;
1516 return ret;
1520 /******************************************************************************
1521 * RegGetValueA [ADVAPI32.@]
1523 * See RegGetValueW.
1525 LONG WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
1526 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1527 LPDWORD pcbData )
1529 DWORD dwType, cbData = pcbData ? *pcbData : 0;
1530 PVOID pvBuf = NULL;
1531 LONG ret;
1533 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1534 hKey, pszSubKey, pszValue, dwFlags, pdwType, pvData, pcbData,
1535 cbData);
1537 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND))
1538 return ERROR_INVALID_PARAMETER;
1540 if (pszSubKey && pszSubKey[0])
1542 ret = RegOpenKeyExA(hKey, pszSubKey, 0, KEY_QUERY_VALUE, &hKey);
1543 if (ret != ERROR_SUCCESS) return ret;
1546 ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1548 /* If we are going to expand we need to read in the whole the value even
1549 * if the passed buffer was too small as the expanded string might be
1550 * smaller than the unexpanded one and could fit into cbData bytes. */
1551 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1552 (dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND)))
1554 do {
1555 HeapFree(GetProcessHeap(), 0, pvBuf);
1557 pvBuf = HeapAlloc(GetProcessHeap(), 0, cbData);
1558 if (!pvBuf)
1560 ret = ERROR_NOT_ENOUGH_MEMORY;
1561 break;
1564 if (ret == ERROR_MORE_DATA)
1565 ret = RegQueryValueExA(hKey, pszValue, NULL,
1566 &dwType, pvBuf, &cbData);
1567 else
1569 /* Even if cbData was large enough we have to copy the
1570 * string since ExpandEnvironmentStrings can't handle
1571 * overlapping buffers. */
1572 CopyMemory(pvBuf, pvData, cbData);
1575 /* Both the type or the value itself could have been modified in
1576 * between so we have to keep retrying until the buffer is large
1577 * enough or we no longer have to expand the value. */
1578 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1580 if (ret == ERROR_SUCCESS)
1582 if (dwType == REG_EXPAND_SZ)
1584 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
1585 pcbData ? *pcbData : 0);
1586 dwType = REG_SZ;
1587 if(pcbData && cbData > *pcbData)
1588 ret = ERROR_MORE_DATA;
1590 else if (pcbData)
1591 CopyMemory(pvData, pvBuf, *pcbData);
1594 HeapFree(GetProcessHeap(), 0, pvBuf);
1597 if (pszSubKey && pszSubKey[0])
1598 RegCloseKey(hKey);
1600 ADVAPI_ApplyRestrictions(dwFlags, dwType, cbData, &ret);
1602 if (pcbData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1603 ZeroMemory(pvData, *pcbData);
1605 if (pdwType) *pdwType = dwType;
1606 if (pcbData) *pcbData = cbData;
1608 return ret;
1612 /******************************************************************************
1613 * RegEnumValueW [ADVAPI32.@]
1615 * Enumerates the values for the specified open registry key.
1617 * PARAMS
1618 * hkey [I] Handle to key to query
1619 * index [I] Index of value to query
1620 * value [O] Value string
1621 * val_count [I/O] Size of value buffer (in wchars)
1622 * reserved [I] Reserved
1623 * type [O] Type code
1624 * data [O] Value data
1625 * count [I/O] Size of data buffer (in bytes)
1627 * RETURNS
1628 * Success: ERROR_SUCCESS
1629 * Failure: nonzero error code from Winerror.h
1632 LONG WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1633 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1635 NTSTATUS status;
1636 DWORD total_size;
1637 char buffer[256], *buf_ptr = buffer;
1638 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1639 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1641 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1642 hkey, index, value, val_count, reserved, type, data, count );
1644 /* NT only checks count, not val_count */
1645 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1646 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1648 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1649 if (data) total_size += *count;
1650 total_size = min( sizeof(buffer), total_size );
1652 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1653 buffer, total_size, &total_size );
1654 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1656 if (value || data)
1658 /* retry with a dynamically allocated buffer */
1659 while (status == STATUS_BUFFER_OVERFLOW)
1661 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1662 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1663 return ERROR_NOT_ENOUGH_MEMORY;
1664 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1665 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1666 buf_ptr, total_size, &total_size );
1669 if (status) goto done;
1671 if (value)
1673 if (info->NameLength/sizeof(WCHAR) >= *val_count)
1675 status = STATUS_BUFFER_OVERFLOW;
1676 goto overflow;
1678 memcpy( value, info->Name, info->NameLength );
1679 *val_count = info->NameLength / sizeof(WCHAR);
1680 value[*val_count] = 0;
1683 if (data)
1685 if (total_size - info->DataOffset > *count)
1687 status = STATUS_BUFFER_OVERFLOW;
1688 goto overflow;
1690 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1691 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1693 /* if the type is REG_SZ and data is not 0-terminated
1694 * and there is enough space in the buffer NT appends a \0 */
1695 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1696 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1700 else status = STATUS_SUCCESS;
1702 overflow:
1703 if (type) *type = info->Type;
1704 if (count) *count = info->DataLength;
1706 done:
1707 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1708 return RtlNtStatusToDosError(status);
1712 /******************************************************************************
1713 * RegEnumValueA [ADVAPI32.@]
1715 * See RegEnumValueW.
1717 LONG WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1718 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1720 NTSTATUS status;
1721 DWORD total_size;
1722 char buffer[256], *buf_ptr = buffer;
1723 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1724 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1726 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
1727 hkey, index, value, val_count, reserved, type, data, count );
1729 /* NT only checks count, not val_count */
1730 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1731 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1733 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1734 if (data) total_size += *count;
1735 total_size = min( sizeof(buffer), total_size );
1737 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1738 buffer, total_size, &total_size );
1739 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1741 /* we need to fetch the contents for a string type even if not requested,
1742 * because we need to compute the length of the ASCII string. */
1743 if (value || data || is_string(info->Type))
1745 /* retry with a dynamically allocated buffer */
1746 while (status == STATUS_BUFFER_OVERFLOW)
1748 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1749 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1750 return ERROR_NOT_ENOUGH_MEMORY;
1751 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1752 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1753 buf_ptr, total_size, &total_size );
1756 if (status) goto done;
1758 if (is_string(info->Type))
1760 DWORD len;
1761 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1762 total_size - info->DataOffset );
1763 if (data && len)
1765 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1766 else
1768 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1769 total_size - info->DataOffset );
1770 /* if the type is REG_SZ and data is not 0-terminated
1771 * and there is enough space in the buffer NT appends a \0 */
1772 if (len < *count && data[len-1]) data[len] = 0;
1775 info->DataLength = len;
1777 else if (data)
1779 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1780 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1783 if (value && !status)
1785 DWORD len;
1787 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1788 if (len >= *val_count)
1790 status = STATUS_BUFFER_OVERFLOW;
1791 if (*val_count)
1793 len = *val_count - 1;
1794 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1795 value[len] = 0;
1798 else
1800 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1801 value[len] = 0;
1802 *val_count = len;
1806 else status = STATUS_SUCCESS;
1808 if (type) *type = info->Type;
1809 if (count) *count = info->DataLength;
1811 done:
1812 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1813 return RtlNtStatusToDosError(status);
1818 /******************************************************************************
1819 * RegDeleteValueW [ADVAPI32.@]
1821 * See RegDeleteValueA.
1823 LONG WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1825 UNICODE_STRING nameW;
1827 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1829 RtlInitUnicodeString( &nameW, name );
1830 return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1834 /******************************************************************************
1835 * RegDeleteValueA [ADVAPI32.@]
1837 * Delete a value from the registry.
1839 * PARAMS
1840 * hkey [I] Registry handle of the key holding the value
1841 * name [I] Name of the value under hkey to delete
1843 * RETURNS
1844 * Success: ERROR_SUCCESS
1845 * Failure: nonzero error code from Winerror.h
1847 LONG WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1849 STRING nameA;
1850 NTSTATUS status;
1852 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1854 RtlInitAnsiString( &nameA, name );
1855 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1856 &nameA, FALSE )))
1857 status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1858 return RtlNtStatusToDosError( status );
1862 /******************************************************************************
1863 * RegLoadKeyW [ADVAPI32.@]
1865 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
1866 * registration information from a specified file into that subkey.
1868 * PARAMS
1869 * hkey [I] Handle of open key
1870 * subkey [I] Address of name of subkey
1871 * filename [I] Address of filename for registry information
1873 * RETURNS
1874 * Success: ERROR_SUCCES
1875 * Failure: nonzero error code from Winerror.h
1877 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1879 OBJECT_ATTRIBUTES destkey, file;
1880 UNICODE_STRING subkeyW, filenameW;
1882 if (!(hkey = get_special_root_hkey(hkey))) return ERROR_INVALID_HANDLE;
1884 destkey.Length = sizeof(destkey);
1885 destkey.RootDirectory = hkey; /* root key: HKLM or HKU */
1886 destkey.ObjectName = &subkeyW; /* name of the key */
1887 destkey.Attributes = 0;
1888 destkey.SecurityDescriptor = NULL;
1889 destkey.SecurityQualityOfService = NULL;
1890 RtlInitUnicodeString(&subkeyW, subkey);
1892 file.Length = sizeof(file);
1893 file.RootDirectory = NULL;
1894 file.ObjectName = &filenameW; /* file containing the hive */
1895 file.Attributes = OBJ_CASE_INSENSITIVE;
1896 file.SecurityDescriptor = NULL;
1897 file.SecurityQualityOfService = NULL;
1898 RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
1900 return RtlNtStatusToDosError( NtLoadKey(&destkey, &file) );
1904 /******************************************************************************
1905 * RegLoadKeyA [ADVAPI32.@]
1907 * See RegLoadKeyW.
1909 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1911 UNICODE_STRING subkeyW, filenameW;
1912 STRING subkeyA, filenameA;
1913 NTSTATUS status;
1915 RtlInitAnsiString(&subkeyA, subkey);
1916 RtlInitAnsiString(&filenameA, filename);
1918 if ((status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)))
1919 return RtlNtStatusToDosError(status);
1921 if ((status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
1922 return RtlNtStatusToDosError(status);
1924 return RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
1928 /******************************************************************************
1929 * RegSaveKeyW [ADVAPI32.@]
1931 * Save a key and all of its subkeys and values to a new file in the standard format.
1933 * PARAMS
1934 * hkey [I] Handle of key where save begins
1935 * lpFile [I] Address of filename to save to
1936 * sa [I] Address of security structure
1938 * RETURNS
1939 * Success: ERROR_SUCCESS
1940 * Failure: nonzero error code from Winerror.h
1942 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1944 static const WCHAR format[] =
1945 {'r','e','g','%','0','4','x','.','t','m','p',0};
1946 WCHAR buffer[MAX_PATH];
1947 int count = 0;
1948 LPWSTR nameW;
1949 DWORD ret, err;
1950 HANDLE handle;
1952 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
1954 if (!file || !*file) return ERROR_INVALID_PARAMETER;
1955 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1957 err = GetLastError();
1958 GetFullPathNameW( file, sizeof(buffer)/sizeof(WCHAR), buffer, &nameW );
1960 for (;;)
1962 snprintfW( nameW, 16, format, count++ );
1963 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
1964 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1965 if (handle != INVALID_HANDLE_VALUE) break;
1966 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1968 /* Something gone haywire ? Please report if this happens abnormally */
1969 if (count >= 100)
1970 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);
1973 ret = RtlNtStatusToDosError(NtSaveKey(hkey, handle));
1975 CloseHandle( handle );
1976 if (!ret)
1978 if (!MoveFileExW( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1980 ERR( "Failed to move %s to %s\n", debugstr_w(buffer),
1981 debugstr_w(file) );
1982 ret = GetLastError();
1985 if (ret) DeleteFileW( buffer );
1987 done:
1988 SetLastError( err ); /* restore last error code */
1989 return ret;
1993 /******************************************************************************
1994 * RegSaveKeyA [ADVAPI32.@]
1996 * See RegSaveKeyW.
1998 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
2000 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
2001 NTSTATUS status;
2002 STRING fileA;
2004 RtlInitAnsiString(&fileA, file);
2005 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
2006 return RtlNtStatusToDosError( status );
2007 return RegSaveKeyW(hkey, fileW->Buffer, sa);
2011 /******************************************************************************
2012 * RegRestoreKeyW [ADVAPI32.@]
2014 * Read the registry information from a file and copy it over a key.
2016 * PARAMS
2017 * hkey [I] Handle of key where restore begins
2018 * lpFile [I] Address of filename containing saved tree
2019 * dwFlags [I] Optional flags
2021 * RETURNS
2022 * Success: ERROR_SUCCESS
2023 * Failure: nonzero error code from Winerror.h
2025 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2027 TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
2029 /* It seems to do this check before the hkey check */
2030 if (!lpFile || !*lpFile)
2031 return ERROR_INVALID_PARAMETER;
2033 FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2035 /* Check for file existence */
2037 return ERROR_SUCCESS;
2041 /******************************************************************************
2042 * RegRestoreKeyA [ADVAPI32.@]
2044 * See RegRestoreKeyW.
2046 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2048 UNICODE_STRING lpFileW;
2049 LONG ret;
2051 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2052 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2053 RtlFreeUnicodeString( &lpFileW );
2054 return ret;
2058 /******************************************************************************
2059 * RegUnLoadKeyW [ADVAPI32.@]
2061 * Unload a registry key and its subkeys from the registry.
2063 * PARAMS
2064 * hkey [I] Handle of open key
2065 * lpSubKey [I] Address of name of subkey to unload
2067 * RETURNS
2068 * Success: ERROR_SUCCESS
2069 * Failure: nonzero error code from Winerror.h
2071 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2073 DWORD ret;
2074 HKEY shkey;
2076 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2078 ret = RegOpenKeyW(hkey,lpSubKey,&shkey);
2079 if( ret )
2080 return ERROR_INVALID_PARAMETER;
2082 ret = RtlNtStatusToDosError(NtUnloadKey(shkey));
2084 RegCloseKey(shkey);
2086 return ret;
2090 /******************************************************************************
2091 * RegUnLoadKeyA [ADVAPI32.@]
2093 * See RegUnLoadKeyW.
2095 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2097 UNICODE_STRING lpSubKeyW;
2098 LONG ret;
2100 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2101 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2102 RtlFreeUnicodeString( &lpSubKeyW );
2103 return ret;
2107 /******************************************************************************
2108 * RegReplaceKeyW [ADVAPI32.@]
2110 * Replace the file backing a registry key and all its subkeys with another file.
2112 * PARAMS
2113 * hkey [I] Handle of open key
2114 * lpSubKey [I] Address of name of subkey
2115 * lpNewFile [I] Address of filename for file with new data
2116 * lpOldFile [I] Address of filename for backup file
2118 * RETURNS
2119 * Success: ERROR_SUCCESS
2120 * Failure: nonzero error code from Winerror.h
2122 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
2123 LPCWSTR lpOldFile )
2125 FIXME("(%p,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
2126 debugstr_w(lpNewFile),debugstr_w(lpOldFile));
2127 return ERROR_SUCCESS;
2131 /******************************************************************************
2132 * RegReplaceKeyA [ADVAPI32.@]
2134 * See RegReplaceKeyW.
2136 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
2137 LPCSTR lpOldFile )
2139 UNICODE_STRING lpSubKeyW;
2140 UNICODE_STRING lpNewFileW;
2141 UNICODE_STRING lpOldFileW;
2142 LONG ret;
2144 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2145 RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, lpOldFile );
2146 RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, lpNewFile );
2147 ret = RegReplaceKeyW( hkey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
2148 RtlFreeUnicodeString( &lpOldFileW );
2149 RtlFreeUnicodeString( &lpNewFileW );
2150 RtlFreeUnicodeString( &lpSubKeyW );
2151 return ret;
2155 /******************************************************************************
2156 * RegSetKeySecurity [ADVAPI32.@]
2158 * Set the security of an open registry key.
2160 * PARAMS
2161 * hkey [I] Open handle of key to set
2162 * SecurityInfo [I] Descriptor contents
2163 * pSecurityDesc [I] Address of descriptor for key
2165 * RETURNS
2166 * Success: ERROR_SUCCESS
2167 * Failure: nonzero error code from Winerror.h
2169 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2170 PSECURITY_DESCRIPTOR pSecurityDesc )
2172 TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2174 /* It seems to perform this check before the hkey check */
2175 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2176 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2177 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2178 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2179 /* Param OK */
2180 } else
2181 return ERROR_INVALID_PARAMETER;
2183 if (!pSecurityDesc)
2184 return ERROR_INVALID_PARAMETER;
2186 FIXME(":(%p,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
2188 return ERROR_SUCCESS;
2192 /******************************************************************************
2193 * RegGetKeySecurity [ADVAPI32.@]
2195 * Get a copy of the security descriptor for a given registry key.
2197 * PARAMS
2198 * hkey [I] Open handle of key to set
2199 * SecurityInformation [I] Descriptor contents
2200 * pSecurityDescriptor [O] Address of descriptor for key
2201 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2203 * RETURNS
2204 * Success: ERROR_SUCCESS
2205 * Failure: Error code
2207 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2208 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2209 LPDWORD lpcbSecurityDescriptor )
2211 TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
2212 lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
2214 /* FIXME: Check for valid SecurityInformation values */
2216 if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
2217 return ERROR_INSUFFICIENT_BUFFER;
2219 FIXME("(%p,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
2220 pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
2222 /* Do not leave security descriptor filled with garbage */
2223 RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2225 return ERROR_SUCCESS;
2229 /******************************************************************************
2230 * RegFlushKey [ADVAPI32.@]
2232 * Immediately write a registry key to registry.
2234 * PARAMS
2235 * hkey [I] Handle of key to write
2237 * RETURNS
2238 * Success: ERROR_SUCCESS
2239 * Failure: Error code
2241 LONG WINAPI RegFlushKey( HKEY hkey )
2243 hkey = get_special_root_hkey( hkey );
2244 if (!hkey) return ERROR_INVALID_HANDLE;
2246 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2250 /******************************************************************************
2251 * RegConnectRegistryW [ADVAPI32.@]
2253 * Establishe a connection to a predefined registry key on another computer.
2255 * PARAMS
2256 * lpMachineName [I] Address of name of remote computer
2257 * hHey [I] Predefined registry handle
2258 * phkResult [I] Address of buffer for remote registry handle
2260 * RETURNS
2261 * Success: ERROR_SUCCESS
2262 * Failure: nonzero error code from Winerror.h
2264 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
2265 PHKEY phkResult )
2267 LONG ret;
2269 TRACE("(%s,%p,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
2271 if (!lpMachineName || !*lpMachineName) {
2272 /* Use the local machine name */
2273 ret = RegOpenKeyW( hKey, NULL, phkResult );
2275 else {
2276 WCHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
2277 DWORD len = sizeof(compName) / sizeof(WCHAR);
2279 /* MSDN says lpMachineName must start with \\ : not so */
2280 if( lpMachineName[0] == '\\' && lpMachineName[1] == '\\')
2281 lpMachineName += 2;
2282 if (GetComputerNameW(compName, &len))
2284 if (!strcmpiW(lpMachineName, compName))
2285 ret = RegOpenKeyW(hKey, NULL, phkResult);
2286 else
2288 FIXME("Connect to %s is not supported.\n",debugstr_w(lpMachineName));
2289 ret = ERROR_BAD_NETPATH;
2292 else
2293 ret = GetLastError();
2295 return ret;
2299 /******************************************************************************
2300 * RegConnectRegistryA [ADVAPI32.@]
2302 * See RegConnectRegistryW.
2304 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, PHKEY reskey )
2306 UNICODE_STRING machineW;
2307 LONG ret;
2309 RtlCreateUnicodeStringFromAsciiz( &machineW, machine );
2310 ret = RegConnectRegistryW( machineW.Buffer, hkey, reskey );
2311 RtlFreeUnicodeString( &machineW );
2312 return ret;
2316 /******************************************************************************
2317 * RegNotifyChangeKeyValue [ADVAPI32.@]
2319 * Notify the caller about changes to the attributes or contents of a registry key.
2321 * PARAMS
2322 * hkey [I] Handle of key to watch
2323 * fWatchSubTree [I] Flag for subkey notification
2324 * fdwNotifyFilter [I] Changes to be reported
2325 * hEvent [I] Handle of signaled event
2326 * fAsync [I] Flag for asynchronous reporting
2328 * RETURNS
2329 * Success: ERROR_SUCCESS
2330 * Failure: nonzero error code from Winerror.h
2332 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2333 DWORD fdwNotifyFilter, HANDLE hEvent,
2334 BOOL fAsync )
2336 NTSTATUS status;
2337 IO_STATUS_BLOCK iosb;
2339 hkey = get_special_root_hkey( hkey );
2340 if (!hkey) return ERROR_INVALID_HANDLE;
2342 TRACE("(%p,%i,%ld,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2343 hEvent, fAsync);
2345 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2346 fdwNotifyFilter, fAsync, NULL, 0,
2347 fWatchSubTree);
2349 if (status && status != STATUS_TIMEOUT)
2350 return RtlNtStatusToDosError( status );
2352 return ERROR_SUCCESS;
2355 /******************************************************************************
2356 * RegOpenUserClassesRoot [ADVAPI32.@]
2358 * Open the HKEY_CLASSES_ROOT key for a user.
2360 * PARAMS
2361 * hToken [I] Handle of token representing the user
2362 * dwOptions [I] Reserved, nust be 0
2363 * samDesired [I] Desired access rights
2364 * phkResult [O] Destination for the resulting key handle
2366 * RETURNS
2367 * Success: ERROR_SUCCESS
2368 * Failure: nonzero error code from Winerror.h
2370 * NOTES
2371 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2372 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2373 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2375 LONG WINAPI RegOpenUserClassesRoot(
2376 HANDLE hToken,
2377 DWORD dwOptions,
2378 REGSAM samDesired,
2379 PHKEY phkResult
2382 FIXME("(%p, 0x%lx, 0x%lx, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2384 *phkResult = HKEY_CLASSES_ROOT;
2385 return ERROR_SUCCESS;
2388 /******************************************************************************
2389 * load_string [Internal]
2391 * This is basically a copy of user32/resource.c's LoadStringW. Necessary to
2392 * avoid importing user32, which is higher level than advapi32. Helper for
2393 * RegLoadMUIString.
2395 static int load_string(HINSTANCE hModule, UINT resId, LPWSTR pwszBuffer, INT cMaxChars)
2397 HGLOBAL hMemory;
2398 HRSRC hResource;
2399 WCHAR *pString;
2400 int idxString;
2402 /* Negative values have to be inverted. */
2403 if (HIWORD(resId) == 0xffff)
2404 resId = (UINT)(-((INT)resId));
2406 /* Load the resource into memory and get a pointer to it. */
2407 hResource = FindResourceW(hModule, MAKEINTRESOURCEW(LOWORD(resId >> 4) + 1), (LPWSTR)RT_STRING);
2408 if (!hResource) return 0;
2409 hMemory = LoadResource(hModule, hResource);
2410 if (!hMemory) return 0;
2411 pString = LockResource(hMemory);
2413 /* Strings are length-prefixed. Lowest nibble of resId is an index. */
2414 idxString = resId & 0xf;
2415 while (idxString--) pString += *pString + 1;
2417 /* If no buffer is given, return length of the string. */
2418 if (!pwszBuffer) return *pString;
2420 /* Else copy over the string, respecting the buffer size. */
2421 cMaxChars = (*pString < cMaxChars) ? *pString : (cMaxChars - 1);
2422 if (cMaxChars >= 0) {
2423 memcpy(pwszBuffer, pString+1, cMaxChars * sizeof(WCHAR));
2424 pwszBuffer[cMaxChars] = '\0';
2427 return cMaxChars;
2430 /******************************************************************************
2431 * RegLoadMUIStringW [ADVAPI32.@]
2433 * Load the localized version of a string resource from some PE, respective
2434 * id and path of which are given in the registry value in the format
2435 * @[path]\dllname,-resourceId
2437 * PARAMS
2438 * hKey [I] Key, of which to load the string value from.
2439 * pszValue [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2440 * pszBuffer [O] Buffer to store the localized string in.
2441 * cbBuffer [I] Size of the destination buffer in bytes.
2442 * pcbData [O] Number of bytes written to pszBuffer (optional, may be NULL).
2443 * dwFlags [I] None supported yet.
2444 * pszBaseDir [I] Not supported yet.
2446 * RETURNS
2447 * Success: ERROR_SUCCESS,
2448 * Failure: nonzero error code from winerror.h
2450 * NOTES
2451 * This is an API of Windows Vista, which wasn't available at the time this code
2452 * was written. We have to check for the correct behaviour once it's available.
2454 LONG WINAPI RegLoadMUIStringW(HKEY hKey, LPCWSTR pwszValue, LPWSTR pwszBuffer, DWORD cbBuffer,
2455 LPDWORD pcbData, DWORD dwFlags, LPCWSTR pwszBaseDir)
2457 DWORD dwValueType, cbData;
2458 LPWSTR pwszTempBuffer = NULL, pwszExpandedBuffer = NULL;
2459 LONG result;
2461 TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %ld, pcbData = %p, "
2462 "dwFlags = %ld, pwszBaseDir = %s) stub\n", hKey, debugstr_w(pwszValue), pwszBuffer,
2463 cbBuffer, pcbData, dwFlags, debugstr_w(pwszBaseDir));
2465 /* Parameter sanity checks. */
2466 if (!hKey || !pwszBuffer)
2467 return ERROR_INVALID_PARAMETER;
2469 if (pwszBaseDir && *pwszBaseDir) {
2470 FIXME("BaseDir parameter not yet supported!\n");
2471 return ERROR_INVALID_PARAMETER;
2474 /* Check for value existence and correctness of it's type, allocate a buffer and load it. */
2475 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, NULL, &cbData);
2476 if (result != ERROR_SUCCESS) goto cleanup;
2477 if (!(dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ) || !cbData) {
2478 result = ERROR_FILE_NOT_FOUND;
2479 goto cleanup;
2481 pwszTempBuffer = HeapAlloc(GetProcessHeap(), 0, cbData);
2482 if (!pwszTempBuffer) {
2483 result = ERROR_NOT_ENOUGH_MEMORY;
2484 goto cleanup;
2486 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, (LPBYTE)pwszTempBuffer, &cbData);
2487 if (result != ERROR_SUCCESS) goto cleanup;
2489 /* Expand environment variables, if appropriate, or copy the original string over. */
2490 if (dwValueType == REG_EXPAND_SZ) {
2491 cbData = ExpandEnvironmentStringsW(pwszTempBuffer, NULL, 0) * sizeof(WCHAR);
2492 if (!cbData) goto cleanup;
2493 pwszExpandedBuffer = HeapAlloc(GetProcessHeap(), 0, cbData);
2494 if (!pwszExpandedBuffer) {
2495 result = ERROR_NOT_ENOUGH_MEMORY;
2496 goto cleanup;
2498 ExpandEnvironmentStringsW(pwszTempBuffer, pwszExpandedBuffer, cbData);
2499 } else {
2500 pwszExpandedBuffer = HeapAlloc(GetProcessHeap(), 0, cbData);
2501 memcpy(pwszExpandedBuffer, pwszTempBuffer, cbData);
2504 /* If the value references a resource based string, parse the value and load the string.
2505 * Else just copy over the original value. */
2506 result = ERROR_SUCCESS;
2507 if (*pwszExpandedBuffer != '@') { /* '@' is the prefix for resource based string entries. */
2508 lstrcpynW(pwszBuffer, pwszExpandedBuffer, cbBuffer / sizeof(WCHAR));
2509 } else {
2510 WCHAR *pComma = strrchrW(pwszExpandedBuffer, ',');
2511 UINT uiStringId;
2512 HMODULE hModule;
2514 /* Format of the expanded value is 'path_to_dll,-resId' */
2515 if (!pComma || pComma[1] != '-') {
2516 result = ERROR_BADKEY;
2517 goto cleanup;
2520 uiStringId = atoiW(pComma+2);
2521 *pComma = '\0';
2523 hModule = LoadLibraryW(pwszExpandedBuffer + 1);
2524 if (!hModule || !load_string(hModule, uiStringId, pwszBuffer, cbBuffer/sizeof(WCHAR)))
2525 result = ERROR_BADKEY;
2526 FreeLibrary(hModule);
2529 cleanup:
2530 HeapFree(GetProcessHeap(), 0, pwszTempBuffer);
2531 HeapFree(GetProcessHeap(), 0, pwszExpandedBuffer);
2532 return result;
2535 /******************************************************************************
2536 * RegLoadMUIStringA [ADVAPI32.@]
2538 * See RegLoadMUIStringW
2540 LONG WINAPI RegLoadMUIStringA(HKEY hKey, LPCSTR pszValue, LPSTR pszBuffer, DWORD cbBuffer,
2541 LPDWORD pcbData, DWORD dwFlags, LPCSTR pszBaseDir)
2543 UNICODE_STRING valueW, baseDirW;
2544 WCHAR *pwszBuffer;
2545 DWORD cbData = cbBuffer * sizeof(WCHAR);
2546 LONG result;
2548 valueW.Buffer = baseDirW.Buffer = pwszBuffer = NULL;
2549 if (!RtlCreateUnicodeStringFromAsciiz(&valueW, pszValue) ||
2550 !RtlCreateUnicodeStringFromAsciiz(&baseDirW, pszBaseDir) ||
2551 !(pwszBuffer = HeapAlloc(GetProcessHeap(), 0, cbData)))
2553 result = ERROR_NOT_ENOUGH_MEMORY;
2554 goto cleanup;
2557 result = RegLoadMUIStringW(hKey, valueW.Buffer, pwszBuffer, cbData, NULL, dwFlags,
2558 baseDirW.Buffer);
2560 if (result == ERROR_SUCCESS) {
2561 cbData = WideCharToMultiByte(CP_ACP, 0, pwszBuffer, -1, pszBuffer, cbBuffer, NULL, NULL);
2562 if (pcbData)
2563 *pcbData = cbData;
2566 cleanup:
2567 HeapFree(GetProcessHeap(), 0, pwszBuffer);
2568 RtlFreeUnicodeString(&baseDirW);
2569 RtlFreeUnicodeString(&valueW);
2571 return result;
2574 /******************************************************************************
2575 * RegDisablePredefinedCache [ADVAPI32.@]
2577 * Disables the caching of the HKEY_CLASSES_ROOT key for the process.
2579 * PARAMS
2580 * None.
2582 * RETURNS
2583 * Success: ERROR_SUCCESS
2584 * Failure: nonzero error code from Winerror.h
2586 * NOTES
2587 * This is useful for services that use impersonation.
2589 LONG WINAPI RegDisablePredefinedCache(void)
2591 HKEY hkey_current_user;
2592 int idx = (UINT_PTR)HKEY_CURRENT_USER - (UINT_PTR)HKEY_SPECIAL_ROOT_FIRST;
2594 /* prevent caching of future requests */
2595 hkcu_cache_disabled = TRUE;
2597 hkey_current_user = InterlockedExchangePointer( (void **)&special_root_keys[idx], NULL );
2599 if (hkey_current_user)
2600 NtClose( hkey_current_user );
2602 return ERROR_SUCCESS;