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 library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define WIN32_NO_STATUS
38 #include "advapi32_misc.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
45 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
46 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
48 static const WCHAR name_CLASSES_ROOT
[] =
49 {'M','a','c','h','i','n','e','\\',
50 'S','o','f','t','w','a','r','e','\\',
51 'C','l','a','s','s','e','s',0};
52 static const WCHAR name_LOCAL_MACHINE
[] =
53 {'M','a','c','h','i','n','e',0};
54 static const WCHAR name_USERS
[] =
56 static const WCHAR name_PERFORMANCE_DATA
[] =
57 {'P','e','r','f','D','a','t','a',0};
58 static const WCHAR name_CURRENT_CONFIG
[] =
59 {'M','a','c','h','i','n','e','\\',
60 'S','y','s','t','e','m','\\',
61 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
62 'H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s','\\',
63 'C','u','r','r','e','n','t',0};
64 static const WCHAR name_DYN_DATA
[] =
65 {'D','y','n','D','a','t','a',0};
67 static const WCHAR
* const root_key_names
[] =
70 NULL
, /* HKEY_CURRENT_USER is determined dynamically */
73 name_PERFORMANCE_DATA
,
78 #define NB_SPECIAL_ROOT_KEYS (sizeof(root_key_names)/sizeof(root_key_names[0]))
80 static HKEY special_root_keys
[NB_SPECIAL_ROOT_KEYS
];
81 static BOOL hkcu_cache_disabled
;
83 static const BOOL is_win64
= (sizeof(void *) > sizeof(int));
85 /* check if value type needs string conversion (Ansi<->Unicode) */
86 static inline BOOL
is_string( DWORD type
)
88 return (type
== REG_SZ
) || (type
== REG_EXPAND_SZ
) || (type
== REG_MULTI_SZ
);
91 /* check if current version is NT or Win95 */
92 static inline BOOL
is_version_nt(void)
94 return !(GetVersion() & 0x80000000);
97 static BOOL
is_wow6432node( const UNICODE_STRING
*name
)
99 static const WCHAR wow6432nodeW
[] = {'W','o','w','6','4','3','2','N','o','d','e'};
101 return (name
->Length
== sizeof(wow6432nodeW
) &&
102 !memicmpW( name
->Buffer
, wow6432nodeW
, sizeof(wow6432nodeW
)/sizeof(WCHAR
) ));
105 /* open the Wow6432Node subkey of the specified key */
106 static HANDLE
open_wow6432node( HANDLE key
, const UNICODE_STRING
*name
)
108 static const WCHAR wow6432nodeW
[] = {'W','o','w','6','4','3','2','N','o','d','e',0};
109 OBJECT_ATTRIBUTES attr
;
110 UNICODE_STRING nameW
;
113 attr
.Length
= sizeof(attr
);
114 attr
.RootDirectory
= key
;
115 attr
.ObjectName
= &nameW
;
117 attr
.SecurityDescriptor
= NULL
;
118 attr
.SecurityQualityOfService
= NULL
;
119 RtlInitUnicodeString( &nameW
, wow6432nodeW
);
120 if (NtOpenKey( &ret
, MAXIMUM_ALLOWED
, &attr
)) ret
= 0;
124 /* wrapper for NtCreateKey that creates the key recursively if necessary */
125 static NTSTATUS
create_key( HKEY
*retkey
, ACCESS_MASK access
, OBJECT_ATTRIBUTES
*attr
,
126 const UNICODE_STRING
*class, ULONG options
, PULONG dispos
)
128 BOOL force_wow32
= is_win64
&& (access
& KEY_WOW64_32KEY
);
129 NTSTATUS status
= STATUS_OBJECT_NAME_NOT_FOUND
;
131 if (!force_wow32
) status
= NtCreateKey( (HANDLE
*)retkey
, access
, attr
, 0, class, options
, dispos
);
133 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
135 HANDLE subkey
, root
= attr
->RootDirectory
;
136 WCHAR
*buffer
= attr
->ObjectName
->Buffer
;
137 DWORD attrs
, pos
= 0, i
= 0, len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
140 while (i
< len
&& buffer
[i
] != '\\') i
++;
141 if (i
== len
&& !force_wow32
) return status
;
143 attrs
= attr
->Attributes
;
144 attr
->ObjectName
= &str
;
148 str
.Buffer
= buffer
+ pos
;
149 str
.Length
= (i
- pos
) * sizeof(WCHAR
);
150 if (force_wow32
&& pos
)
152 if (is_wow6432node( &str
)) force_wow32
= FALSE
;
153 else if ((subkey
= open_wow6432node( attr
->RootDirectory
, &str
)))
155 if (attr
->RootDirectory
!= root
) NtClose( attr
->RootDirectory
);
156 attr
->RootDirectory
= subkey
;
162 attr
->Attributes
= attrs
;
163 status
= NtCreateKey( (PHANDLE
)retkey
, access
, attr
, 0, class, options
, dispos
);
167 attr
->Attributes
= attrs
& ~OBJ_OPENLINK
;
168 status
= NtCreateKey( &subkey
, access
, attr
, 0, class,
169 options
& ~REG_OPTION_CREATE_LINK
, dispos
);
171 if (attr
->RootDirectory
!= root
) NtClose( attr
->RootDirectory
);
172 if (status
) return status
;
174 attr
->RootDirectory
= subkey
;
175 while (i
< len
&& buffer
[i
] == '\\') i
++;
177 while (i
< len
&& buffer
[i
] != '\\') i
++;
183 /* wrapper for NtOpenKey to handle Wow6432 nodes */
184 static NTSTATUS
open_key( HKEY
*retkey
, ACCESS_MASK access
, OBJECT_ATTRIBUTES
*attr
)
187 BOOL force_wow32
= is_win64
&& (access
& KEY_WOW64_32KEY
);
188 HANDLE subkey
, root
= attr
->RootDirectory
;
189 WCHAR
*buffer
= attr
->ObjectName
->Buffer
;
190 DWORD attrs
, pos
= 0, i
= 0, len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
193 if (!force_wow32
) return NtOpenKey( (HANDLE
*)retkey
, access
, attr
);
195 if (len
&& buffer
[0] == '\\') return STATUS_OBJECT_PATH_INVALID
;
196 while (i
< len
&& buffer
[i
] != '\\') i
++;
197 attrs
= attr
->Attributes
;
198 attr
->ObjectName
= &str
;
202 str
.Buffer
= buffer
+ pos
;
203 str
.Length
= (i
- pos
) * sizeof(WCHAR
);
204 if (force_wow32
&& pos
)
206 if (is_wow6432node( &str
)) force_wow32
= FALSE
;
207 else if ((subkey
= open_wow6432node( attr
->RootDirectory
, &str
)))
209 if (attr
->RootDirectory
!= root
) NtClose( attr
->RootDirectory
);
210 attr
->RootDirectory
= subkey
;
216 attr
->Attributes
= attrs
;
217 status
= NtOpenKey( (PHANDLE
)retkey
, access
, attr
);
221 attr
->Attributes
= attrs
& ~OBJ_OPENLINK
;
222 status
= NtOpenKey( &subkey
, access
, attr
);
224 if (attr
->RootDirectory
!= root
) NtClose( attr
->RootDirectory
);
225 if (status
) return status
;
227 attr
->RootDirectory
= subkey
;
228 while (i
< len
&& buffer
[i
] == '\\') i
++;
230 while (i
< len
&& buffer
[i
] != '\\') i
++;
235 /* create one of the HKEY_* special root keys */
236 static HKEY
create_special_root_hkey( HKEY hkey
, DWORD access
)
239 int idx
= HandleToUlong(hkey
) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST
);
241 if (HandleToUlong(hkey
) == HandleToUlong(HKEY_CURRENT_USER
))
243 if (RtlOpenCurrentUser( access
, (HANDLE
*)&hkey
)) return 0;
244 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey
);
246 /* don't cache the key in the table if caching is disabled */
247 if (hkcu_cache_disabled
)
252 OBJECT_ATTRIBUTES attr
;
255 attr
.Length
= sizeof(attr
);
256 attr
.RootDirectory
= 0;
257 attr
.ObjectName
= &name
;
259 attr
.SecurityDescriptor
= NULL
;
260 attr
.SecurityQualityOfService
= NULL
;
261 RtlInitUnicodeString( &name
, root_key_names
[idx
] );
262 if (create_key( &hkey
, access
, &attr
, NULL
, 0, NULL
)) return 0;
263 TRACE( "%s -> %p\n", debugstr_w(attr
.ObjectName
->Buffer
), hkey
);
266 if (!(access
& (KEY_WOW64_64KEY
| KEY_WOW64_32KEY
)))
268 if (!(ret
= InterlockedCompareExchangePointer( (void **)&special_root_keys
[idx
], hkey
, 0 )))
271 NtClose( hkey
); /* somebody beat us to it */
278 /* map the hkey from special root to normal key if necessary */
279 static inline HKEY
get_special_root_hkey( HKEY hkey
, REGSAM access
)
283 if ((HandleToUlong(hkey
) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST
))
284 && (HandleToUlong(hkey
) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST
)))
288 if (HandleToUlong(hkey
) == HandleToUlong(HKEY_CLASSES_ROOT
))
289 mask
= KEY_WOW64_32KEY
| KEY_WOW64_64KEY
;
291 if ((access
& mask
) ||
292 !(ret
= special_root_keys
[HandleToUlong(hkey
) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST
)]))
293 ret
= create_special_root_hkey( hkey
, MAXIMUM_ALLOWED
| (access
& mask
) );
299 /******************************************************************************
300 * RegOverridePredefKey [ADVAPI32.@]
302 LSTATUS WINAPI
RegOverridePredefKey( HKEY hkey
, HKEY override
)
307 TRACE("(%p %p)\n", hkey
, override
);
309 if ((HandleToUlong(hkey
) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST
))
310 || (HandleToUlong(hkey
) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST
)))
311 return ERROR_INVALID_PARAMETER
;
312 idx
= HandleToUlong(hkey
) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST
);
316 NTSTATUS status
= NtDuplicateObject( GetCurrentProcess(), override
,
317 GetCurrentProcess(), (HANDLE
*)&override
,
318 0, 0, DUPLICATE_SAME_ACCESS
);
319 if (status
) return RtlNtStatusToDosError( status
);
322 old_key
= InterlockedExchangePointer( (void **)&special_root_keys
[idx
], override
);
323 if (old_key
) NtClose( old_key
);
324 return ERROR_SUCCESS
;
328 /******************************************************************************
329 * RegCreateKeyExW [ADVAPI32.@]
331 * See RegCreateKeyExA.
333 LSTATUS WINAPI
RegCreateKeyExW( HKEY hkey
, LPCWSTR name
, DWORD reserved
, LPWSTR
class,
334 DWORD options
, REGSAM access
, SECURITY_ATTRIBUTES
*sa
,
335 PHKEY retkey
, LPDWORD dispos
)
337 OBJECT_ATTRIBUTES attr
;
338 UNICODE_STRING nameW
, classW
;
340 if (reserved
) return ERROR_INVALID_PARAMETER
;
341 if (!(hkey
= get_special_root_hkey( hkey
, access
))) return ERROR_INVALID_HANDLE
;
343 attr
.Length
= sizeof(attr
);
344 attr
.RootDirectory
= hkey
;
345 attr
.ObjectName
= &nameW
;
347 attr
.SecurityDescriptor
= NULL
;
348 attr
.SecurityQualityOfService
= NULL
;
349 if (options
& REG_OPTION_OPEN_LINK
) attr
.Attributes
|= OBJ_OPENLINK
;
350 RtlInitUnicodeString( &nameW
, name
);
351 RtlInitUnicodeString( &classW
, class );
353 return RtlNtStatusToDosError( create_key( retkey
, access
, &attr
, &classW
, options
, dispos
) );
357 /******************************************************************************
358 * RegCreateKeyExA [ADVAPI32.@]
360 * Open a registry key, creating it if it doesn't exist.
363 * hkey [I] Handle of the parent registry key
364 * name [I] Name of the new key to open or create
365 * reserved [I] Reserved, pass 0
366 * class [I] The object type of the new key
367 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
368 * access [I] Access level desired
369 * sa [I] Security attributes for the key
370 * retkey [O] Destination for the resulting handle
371 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
374 * Success: ERROR_SUCCESS.
375 * Failure: A standard Win32 error code. retkey remains untouched.
378 * MAXIMUM_ALLOWED in access mask not supported by server
380 LSTATUS WINAPI
RegCreateKeyExA( HKEY hkey
, LPCSTR name
, DWORD reserved
, LPSTR
class,
381 DWORD options
, REGSAM access
, SECURITY_ATTRIBUTES
*sa
,
382 PHKEY retkey
, LPDWORD dispos
)
384 OBJECT_ATTRIBUTES attr
;
385 UNICODE_STRING classW
;
386 ANSI_STRING nameA
, classA
;
389 if (reserved
) return ERROR_INVALID_PARAMETER
;
390 if (!is_version_nt())
392 access
= MAXIMUM_ALLOWED
; /* Win95 ignores the access mask */
393 if (name
&& *name
== '\\') name
++; /* win9x,ME ignores one (and only one) beginning backslash */
395 if (!(hkey
= get_special_root_hkey( hkey
, access
))) return ERROR_INVALID_HANDLE
;
397 attr
.Length
= sizeof(attr
);
398 attr
.RootDirectory
= hkey
;
399 attr
.ObjectName
= &NtCurrentTeb()->StaticUnicodeString
;
401 attr
.SecurityDescriptor
= NULL
;
402 attr
.SecurityQualityOfService
= NULL
;
403 if (options
& REG_OPTION_OPEN_LINK
) attr
.Attributes
|= OBJ_OPENLINK
;
404 RtlInitAnsiString( &nameA
, name
);
405 RtlInitAnsiString( &classA
, class );
407 if (!(status
= RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString
,
410 if (!(status
= RtlAnsiStringToUnicodeString( &classW
, &classA
, TRUE
)))
412 status
= create_key( retkey
, access
, &attr
, &classW
, options
, dispos
);
413 RtlFreeUnicodeString( &classW
);
416 return RtlNtStatusToDosError( status
);
420 /******************************************************************************
421 * RegCreateKeyW [ADVAPI32.@]
423 * Creates the specified reg key.
426 * hKey [I] Handle to an open key.
427 * lpSubKey [I] Name of a key that will be opened or created.
428 * phkResult [O] Receives a handle to the opened or created key.
431 * Success: ERROR_SUCCESS
432 * Failure: nonzero error code defined in Winerror.h
434 LSTATUS WINAPI
RegCreateKeyW( HKEY hkey
, LPCWSTR lpSubKey
, PHKEY phkResult
)
436 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
437 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
438 return RegCreateKeyExW( hkey
, lpSubKey
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
439 MAXIMUM_ALLOWED
, NULL
, phkResult
, NULL
);
443 /******************************************************************************
444 * RegCreateKeyA [ADVAPI32.@]
448 LSTATUS WINAPI
RegCreateKeyA( HKEY hkey
, LPCSTR lpSubKey
, PHKEY phkResult
)
450 return RegCreateKeyExA( hkey
, lpSubKey
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
451 MAXIMUM_ALLOWED
, NULL
, phkResult
, NULL
);
456 /******************************************************************************
457 * RegOpenKeyExW [ADVAPI32.@]
461 LSTATUS WINAPI
RegOpenKeyExW( HKEY hkey
, LPCWSTR name
, DWORD options
, REGSAM access
, PHKEY retkey
)
463 OBJECT_ATTRIBUTES attr
;
464 UNICODE_STRING nameW
;
466 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
467 if (HandleToUlong(hkey
) == HandleToUlong(HKEY_CLASSES_ROOT
) && name
&& *name
== '\\') name
++;
469 if (!retkey
) return ERROR_INVALID_PARAMETER
;
470 if (!(hkey
= get_special_root_hkey( hkey
, access
))) return ERROR_INVALID_HANDLE
;
472 attr
.Length
= sizeof(attr
);
473 attr
.RootDirectory
= hkey
;
474 attr
.ObjectName
= &nameW
;
476 attr
.SecurityDescriptor
= NULL
;
477 attr
.SecurityQualityOfService
= NULL
;
478 if (options
& REG_OPTION_OPEN_LINK
) attr
.Attributes
|= OBJ_OPENLINK
;
479 RtlInitUnicodeString( &nameW
, name
);
480 return RtlNtStatusToDosError( open_key( retkey
, access
, &attr
) );
484 /******************************************************************************
485 * RegOpenKeyExA [ADVAPI32.@]
487 * Open a registry key.
490 * hkey [I] Handle of open key
491 * name [I] Name of subkey to open
492 * options [I] Open options (can be set to REG_OPTION_OPEN_LINK)
493 * access [I] Security access mask
494 * retkey [O] Handle to open key
497 * Success: ERROR_SUCCESS
498 * Failure: A standard Win32 error code. retkey is set to 0.
501 * Unlike RegCreateKeyExA(), this function will not create the key if it
504 LSTATUS WINAPI
RegOpenKeyExA( HKEY hkey
, LPCSTR name
, DWORD options
, REGSAM access
, PHKEY retkey
)
506 OBJECT_ATTRIBUTES attr
;
510 if (!is_version_nt()) access
= MAXIMUM_ALLOWED
; /* Win95 ignores the access mask */
513 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
514 if (HandleToUlong(hkey
) == HandleToUlong(HKEY_CLASSES_ROOT
) && name
&& *name
== '\\') name
++;
517 if (!(hkey
= get_special_root_hkey( hkey
, access
))) return ERROR_INVALID_HANDLE
;
519 attr
.Length
= sizeof(attr
);
520 attr
.RootDirectory
= hkey
;
521 attr
.ObjectName
= &NtCurrentTeb()->StaticUnicodeString
;
523 attr
.SecurityDescriptor
= NULL
;
524 attr
.SecurityQualityOfService
= NULL
;
525 if (options
& REG_OPTION_OPEN_LINK
) attr
.Attributes
|= OBJ_OPENLINK
;
527 RtlInitAnsiString( &nameA
, name
);
528 if (!(status
= RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString
,
531 status
= open_key( retkey
, access
, &attr
);
533 return RtlNtStatusToDosError( status
);
537 /******************************************************************************
538 * RegOpenKeyW [ADVAPI32.@]
542 LSTATUS WINAPI
RegOpenKeyW( HKEY hkey
, LPCWSTR name
, PHKEY retkey
)
545 return ERROR_INVALID_PARAMETER
;
550 return ERROR_SUCCESS
;
552 return RegOpenKeyExW( hkey
, name
, 0, MAXIMUM_ALLOWED
, retkey
);
556 /******************************************************************************
557 * RegOpenKeyA [ADVAPI32.@]
559 * Open a registry key.
562 * hkey [I] Handle of parent key to open the new key under
563 * name [I] Name of the key under hkey to open
564 * retkey [O] Destination for the resulting Handle
567 * Success: ERROR_SUCCESS
568 * Failure: A standard Win32 error code. When retkey is valid, *retkey is set to 0.
570 LSTATUS WINAPI
RegOpenKeyA( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
573 return ERROR_INVALID_PARAMETER
;
578 return ERROR_SUCCESS
;
580 return RegOpenKeyExA( hkey
, name
, 0, MAXIMUM_ALLOWED
, retkey
);
584 /******************************************************************************
585 * RegOpenCurrentUser [ADVAPI32.@]
587 * Get a handle to the HKEY_CURRENT_USER key for the user
588 * the current thread is impersonating.
591 * access [I] Desired access rights to the key
592 * retkey [O] Handle to the opened key
595 * Success: ERROR_SUCCESS
596 * Failure: nonzero error code from Winerror.h
599 * This function is supposed to retrieve a handle to the
600 * HKEY_CURRENT_USER for the user the current thread is impersonating.
601 * Since Wine does not currently allow threads to impersonate other users,
602 * this stub should work fine.
604 LSTATUS WINAPI
RegOpenCurrentUser( REGSAM access
, PHKEY retkey
)
606 return RegOpenKeyExA( HKEY_CURRENT_USER
, "", 0, access
, retkey
);
611 /******************************************************************************
612 * RegEnumKeyExW [ADVAPI32.@]
614 * Enumerate subkeys of the specified open registry key.
617 * hkey [I] Handle to key to enumerate
618 * index [I] Index of subkey to enumerate
619 * name [O] Buffer for subkey name
620 * name_len [O] Size of subkey buffer
621 * reserved [I] Reserved
622 * class [O] Buffer for class string
623 * class_len [O] Size of class buffer
624 * ft [O] Time key last written to
627 * Success: ERROR_SUCCESS
628 * Failure: System error code. If there are no more subkeys available, the
629 * function returns ERROR_NO_MORE_ITEMS.
631 LSTATUS WINAPI
RegEnumKeyExW( HKEY hkey
, DWORD index
, LPWSTR name
, LPDWORD name_len
,
632 LPDWORD reserved
, LPWSTR
class, LPDWORD class_len
, FILETIME
*ft
)
635 char buffer
[256], *buf_ptr
= buffer
;
636 KEY_NODE_INFORMATION
*info
= (KEY_NODE_INFORMATION
*)buffer
;
639 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey
, index
, name
, name_len
,
640 name_len
? *name_len
: 0, reserved
, class, class_len
, ft
);
642 if (reserved
) return ERROR_INVALID_PARAMETER
;
643 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
645 status
= NtEnumerateKey( hkey
, index
, KeyNodeInformation
,
646 buffer
, sizeof(buffer
), &total_size
);
648 while (status
== STATUS_BUFFER_OVERFLOW
)
650 /* retry with a dynamically allocated buffer */
651 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
652 if (!(buf_ptr
= heap_alloc( total_size
)))
653 return ERROR_NOT_ENOUGH_MEMORY
;
654 info
= (KEY_NODE_INFORMATION
*)buf_ptr
;
655 status
= NtEnumerateKey( hkey
, index
, KeyNodeInformation
,
656 buf_ptr
, total_size
, &total_size
);
661 DWORD len
= info
->NameLength
/ sizeof(WCHAR
);
662 DWORD cls_len
= info
->ClassLength
/ sizeof(WCHAR
);
664 if (ft
) *ft
= *(FILETIME
*)&info
->LastWriteTime
;
666 if (len
>= *name_len
|| (class && class_len
&& (cls_len
>= *class_len
)))
667 status
= STATUS_BUFFER_OVERFLOW
;
671 memcpy( name
, info
->Name
, info
->NameLength
);
675 *class_len
= cls_len
;
678 memcpy( class, buf_ptr
+ info
->ClassOffset
, info
->ClassLength
);
685 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
686 return RtlNtStatusToDosError( status
);
690 /******************************************************************************
691 * RegEnumKeyExA [ADVAPI32.@]
695 LSTATUS WINAPI
RegEnumKeyExA( HKEY hkey
, DWORD index
, LPSTR name
, LPDWORD name_len
,
696 LPDWORD reserved
, LPSTR
class, LPDWORD class_len
, FILETIME
*ft
)
699 char buffer
[256], *buf_ptr
= buffer
;
700 KEY_NODE_INFORMATION
*info
= (KEY_NODE_INFORMATION
*)buffer
;
703 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey
, index
, name
, name_len
,
704 name_len
? *name_len
: 0, reserved
, class, class_len
, ft
);
706 if (reserved
) return ERROR_INVALID_PARAMETER
;
707 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
709 status
= NtEnumerateKey( hkey
, index
, KeyNodeInformation
,
710 buffer
, sizeof(buffer
), &total_size
);
712 while (status
== STATUS_BUFFER_OVERFLOW
)
714 /* retry with a dynamically allocated buffer */
715 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
716 if (!(buf_ptr
= heap_alloc( total_size
)))
717 return ERROR_NOT_ENOUGH_MEMORY
;
718 info
= (KEY_NODE_INFORMATION
*)buf_ptr
;
719 status
= NtEnumerateKey( hkey
, index
, KeyNodeInformation
,
720 buf_ptr
, total_size
, &total_size
);
727 RtlUnicodeToMultiByteSize( &len
, info
->Name
, info
->NameLength
);
728 RtlUnicodeToMultiByteSize( &cls_len
, (WCHAR
*)(buf_ptr
+ info
->ClassOffset
),
730 if (ft
) *ft
= *(FILETIME
*)&info
->LastWriteTime
;
732 if (len
>= *name_len
|| (class && class_len
&& (cls_len
>= *class_len
)))
733 status
= STATUS_BUFFER_OVERFLOW
;
737 RtlUnicodeToMultiByteN( name
, len
, NULL
, info
->Name
, info
->NameLength
);
741 *class_len
= cls_len
;
744 RtlUnicodeToMultiByteN( class, cls_len
, NULL
,
745 (WCHAR
*)(buf_ptr
+ info
->ClassOffset
),
753 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
754 return RtlNtStatusToDosError( status
);
758 /******************************************************************************
759 * RegEnumKeyW [ADVAPI32.@]
761 * Enumerates subkeys of the specified open reg key.
764 * hKey [I] Handle to an open key.
765 * dwIndex [I] Index of the subkey of hKey to retrieve.
766 * lpName [O] Name of the subkey.
767 * cchName [I] Size of lpName in TCHARS.
770 * Success: ERROR_SUCCESS
771 * Failure: system error code. If there are no more subkeys available, the
772 * function returns ERROR_NO_MORE_ITEMS.
774 LSTATUS WINAPI
RegEnumKeyW( HKEY hkey
, DWORD index
, LPWSTR name
, DWORD name_len
)
776 return RegEnumKeyExW( hkey
, index
, name
, &name_len
, NULL
, NULL
, NULL
, NULL
);
780 /******************************************************************************
781 * RegEnumKeyA [ADVAPI32.@]
785 LSTATUS WINAPI
RegEnumKeyA( HKEY hkey
, DWORD index
, LPSTR name
, DWORD name_len
)
787 return RegEnumKeyExA( hkey
, index
, name
, &name_len
, NULL
, NULL
, NULL
, NULL
);
791 /******************************************************************************
792 * RegQueryInfoKeyW [ADVAPI32.@]
794 * Retrieves information about the specified registry key.
797 * hkey [I] Handle to key to query
798 * class [O] Buffer for class string
799 * class_len [O] Size of class string buffer
800 * reserved [I] Reserved
801 * subkeys [O] Buffer for number of subkeys
802 * max_subkey [O] Buffer for longest subkey name length
803 * max_class [O] Buffer for longest class string length
804 * values [O] Buffer for number of value entries
805 * max_value [O] Buffer for longest value name length
806 * max_data [O] Buffer for longest value data length
807 * security [O] Buffer for security descriptor length
808 * modif [O] Modification time
811 * Success: ERROR_SUCCESS
812 * Failure: system error code.
815 * - win95 allows class to be valid and class_len to be NULL
816 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
817 * - both allow class to be NULL and class_len to be NULL
818 * (it's hard to test validity, so test !NULL instead)
820 LSTATUS WINAPI
RegQueryInfoKeyW( HKEY hkey
, LPWSTR
class, LPDWORD class_len
, LPDWORD reserved
,
821 LPDWORD subkeys
, LPDWORD max_subkey
, LPDWORD max_class
,
822 LPDWORD values
, LPDWORD max_value
, LPDWORD max_data
,
823 LPDWORD security
, FILETIME
*modif
)
826 char buffer
[256], *buf_ptr
= buffer
;
827 KEY_FULL_INFORMATION
*info
= (KEY_FULL_INFORMATION
*)buffer
;
830 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey
, class, class_len
? *class_len
: 0,
831 reserved
, subkeys
, max_subkey
, values
, max_value
, max_data
, security
, modif
);
833 if (class && !class_len
&& is_version_nt()) return ERROR_INVALID_PARAMETER
;
834 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
836 status
= NtQueryKey( hkey
, KeyFullInformation
, buffer
, sizeof(buffer
), &total_size
);
837 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
841 /* retry with a dynamically allocated buffer */
842 while (status
== STATUS_BUFFER_OVERFLOW
)
844 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
845 if (!(buf_ptr
= heap_alloc( total_size
)))
846 return ERROR_NOT_ENOUGH_MEMORY
;
847 info
= (KEY_FULL_INFORMATION
*)buf_ptr
;
848 status
= NtQueryKey( hkey
, KeyFullInformation
, buf_ptr
, total_size
, &total_size
);
851 if (status
) goto done
;
853 if (class_len
&& (info
->ClassLength
/sizeof(WCHAR
) + 1 > *class_len
))
855 status
= STATUS_BUFFER_OVERFLOW
;
859 memcpy( class, buf_ptr
+ info
->ClassOffset
, info
->ClassLength
);
860 class[info
->ClassLength
/sizeof(WCHAR
)] = 0;
863 else status
= STATUS_SUCCESS
;
865 if (class_len
) *class_len
= info
->ClassLength
/ sizeof(WCHAR
);
866 if (subkeys
) *subkeys
= info
->SubKeys
;
867 if (max_subkey
) *max_subkey
= info
->MaxNameLen
;
868 if (max_class
) *max_class
= info
->MaxClassLen
;
869 if (values
) *values
= info
->Values
;
870 if (max_value
) *max_value
= info
->MaxValueNameLen
;
871 if (max_data
) *max_data
= info
->MaxValueDataLen
;
872 if (modif
) *modif
= *(FILETIME
*)&info
->LastWriteTime
;
875 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
876 return RtlNtStatusToDosError( status
);
880 /******************************************************************************
881 * RegQueryMultipleValuesA [ADVAPI32.@]
883 * Retrieves the type and data for a list of value names associated with a key.
886 * hKey [I] Handle to an open key.
887 * val_list [O] Array of VALENT structures that describes the entries.
888 * num_vals [I] Number of elements in val_list.
889 * lpValueBuf [O] Pointer to a buffer that receives the data for each value.
890 * ldwTotsize [I/O] Size of lpValueBuf.
893 * Success: ERROR_SUCCESS. ldwTotsize contains num bytes copied.
894 * Failure: nonzero error code from Winerror.h ldwTotsize contains num needed
897 LSTATUS WINAPI
RegQueryMultipleValuesA( HKEY hkey
, PVALENTA val_list
, DWORD num_vals
,
898 LPSTR lpValueBuf
, LPDWORD ldwTotsize
)
901 DWORD maxBytes
= *ldwTotsize
;
903 LPSTR bufptr
= lpValueBuf
;
906 TRACE("(%p,%p,%d,%p,%p=%d)\n", hkey
, val_list
, num_vals
, lpValueBuf
, ldwTotsize
, *ldwTotsize
);
908 for(i
=0; i
< num_vals
; ++i
)
911 val_list
[i
].ve_valuelen
=0;
912 status
= RegQueryValueExA(hkey
, val_list
[i
].ve_valuename
, NULL
, NULL
, NULL
, &val_list
[i
].ve_valuelen
);
913 if(status
!= ERROR_SUCCESS
)
918 if(lpValueBuf
!= NULL
&& *ldwTotsize
+ val_list
[i
].ve_valuelen
<= maxBytes
)
920 status
= RegQueryValueExA(hkey
, val_list
[i
].ve_valuename
, NULL
, &val_list
[i
].ve_type
,
921 (LPBYTE
)bufptr
, &val_list
[i
].ve_valuelen
);
922 if(status
!= ERROR_SUCCESS
)
927 val_list
[i
].ve_valueptr
= (DWORD_PTR
)bufptr
;
929 bufptr
+= val_list
[i
].ve_valuelen
;
932 *ldwTotsize
+= val_list
[i
].ve_valuelen
;
934 return lpValueBuf
!= NULL
&& *ldwTotsize
<= maxBytes
? ERROR_SUCCESS
: ERROR_MORE_DATA
;
938 /******************************************************************************
939 * RegQueryMultipleValuesW [ADVAPI32.@]
941 * See RegQueryMultipleValuesA.
943 LSTATUS WINAPI
RegQueryMultipleValuesW( HKEY hkey
, PVALENTW val_list
, DWORD num_vals
,
944 LPWSTR lpValueBuf
, LPDWORD ldwTotsize
)
947 DWORD maxBytes
= *ldwTotsize
;
949 LPSTR bufptr
= (LPSTR
)lpValueBuf
;
952 TRACE("(%p,%p,%d,%p,%p=%d)\n", hkey
, val_list
, num_vals
, lpValueBuf
, ldwTotsize
, *ldwTotsize
);
954 for(i
=0; i
< num_vals
; ++i
)
956 val_list
[i
].ve_valuelen
=0;
957 status
= RegQueryValueExW(hkey
, val_list
[i
].ve_valuename
, NULL
, NULL
, NULL
, &val_list
[i
].ve_valuelen
);
958 if(status
!= ERROR_SUCCESS
)
963 if(lpValueBuf
!= NULL
&& *ldwTotsize
+ val_list
[i
].ve_valuelen
<= maxBytes
)
965 status
= RegQueryValueExW(hkey
, val_list
[i
].ve_valuename
, NULL
, &val_list
[i
].ve_type
,
966 (LPBYTE
)bufptr
, &val_list
[i
].ve_valuelen
);
967 if(status
!= ERROR_SUCCESS
)
972 val_list
[i
].ve_valueptr
= (DWORD_PTR
)bufptr
;
974 bufptr
+= val_list
[i
].ve_valuelen
;
977 *ldwTotsize
+= val_list
[i
].ve_valuelen
;
979 return lpValueBuf
!= NULL
&& *ldwTotsize
<= maxBytes
? ERROR_SUCCESS
: ERROR_MORE_DATA
;
982 /******************************************************************************
983 * RegQueryInfoKeyA [ADVAPI32.@]
985 * Retrieves information about a registry key.
988 * hKey [I] Handle to an open key.
989 * lpClass [O] Class string of the key.
990 * lpcClass [I/O] size of lpClass.
991 * lpReserved [I] Reserved; must be NULL.
992 * lpcSubKeys [O] Number of subkeys contained by the key.
993 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
994 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
996 * lpcValues [O] Number of values associated with the key.
997 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
998 * lpcMaxValueLen [O] Longest data component among the key's values
999 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
1000 * lpftLastWriteTime [O] FILETIME structure that is the last write time.
1003 * Success: ERROR_SUCCESS
1004 * Failure: nonzero error code from Winerror.h
1006 LSTATUS WINAPI
RegQueryInfoKeyA( HKEY hkey
, LPSTR
class, LPDWORD class_len
, LPDWORD reserved
,
1007 LPDWORD subkeys
, LPDWORD max_subkey
, LPDWORD max_class
,
1008 LPDWORD values
, LPDWORD max_value
, LPDWORD max_data
,
1009 LPDWORD security
, FILETIME
*modif
)
1012 char buffer
[256], *buf_ptr
= buffer
;
1013 KEY_FULL_INFORMATION
*info
= (KEY_FULL_INFORMATION
*)buffer
;
1014 DWORD total_size
, len
;
1016 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey
, class, class_len
? *class_len
: 0,
1017 reserved
, subkeys
, max_subkey
, values
, max_value
, max_data
, security
, modif
);
1019 if (class && !class_len
&& is_version_nt()) return ERROR_INVALID_PARAMETER
;
1020 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
1022 status
= NtQueryKey( hkey
, KeyFullInformation
, buffer
, sizeof(buffer
), &total_size
);
1023 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
1025 if (class || class_len
)
1027 /* retry with a dynamically allocated buffer */
1028 while (status
== STATUS_BUFFER_OVERFLOW
)
1030 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
1031 if (!(buf_ptr
= heap_alloc( total_size
)))
1032 return ERROR_NOT_ENOUGH_MEMORY
;
1033 info
= (KEY_FULL_INFORMATION
*)buf_ptr
;
1034 status
= NtQueryKey( hkey
, KeyFullInformation
, buf_ptr
, total_size
, &total_size
);
1037 if (status
) goto done
;
1039 RtlUnicodeToMultiByteSize( &len
, (WCHAR
*)(buf_ptr
+ info
->ClassOffset
), info
->ClassLength
);
1042 if (len
+ 1 > *class_len
) status
= STATUS_BUFFER_OVERFLOW
;
1045 if (class && !status
)
1047 RtlUnicodeToMultiByteN( class, len
, NULL
, (WCHAR
*)(buf_ptr
+ info
->ClassOffset
),
1048 info
->ClassLength
);
1052 else status
= STATUS_SUCCESS
;
1054 if (subkeys
) *subkeys
= info
->SubKeys
;
1055 if (max_subkey
) *max_subkey
= info
->MaxNameLen
;
1056 if (max_class
) *max_class
= info
->MaxClassLen
;
1057 if (values
) *values
= info
->Values
;
1058 if (max_value
) *max_value
= info
->MaxValueNameLen
;
1059 if (max_data
) *max_data
= info
->MaxValueDataLen
;
1060 if (modif
) *modif
= *(FILETIME
*)&info
->LastWriteTime
;
1063 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
1064 return RtlNtStatusToDosError( status
);
1068 /******************************************************************************
1069 * RegCloseKey [ADVAPI32.@]
1071 * Close an open registry key.
1074 * hkey [I] Handle of key to close
1077 * Success: ERROR_SUCCESS
1078 * Failure: Error code
1080 LSTATUS WINAPI
RegCloseKey( HKEY hkey
)
1082 if (!hkey
) return ERROR_INVALID_HANDLE
;
1083 if (hkey
>= (HKEY
)0x80000000) return ERROR_SUCCESS
;
1084 return RtlNtStatusToDosError( NtClose( hkey
) );
1088 /******************************************************************************
1089 * RegDeleteKeyExW [ADVAPI32.@]
1091 LSTATUS WINAPI
RegDeleteKeyExW( HKEY hkey
, LPCWSTR name
, REGSAM access
, DWORD reserved
)
1096 if (!name
) return ERROR_INVALID_PARAMETER
;
1098 if (!(hkey
= get_special_root_hkey( hkey
, access
))) return ERROR_INVALID_HANDLE
;
1100 access
&= KEY_WOW64_64KEY
| KEY_WOW64_32KEY
;
1101 if (!(ret
= RegOpenKeyExW( hkey
, name
, 0, access
| DELETE
, &tmp
)))
1103 ret
= RtlNtStatusToDosError( NtDeleteKey( tmp
) );
1106 TRACE("%s ret=%08x\n", debugstr_w(name
), ret
);
1111 /******************************************************************************
1112 * RegDeleteKeyW [ADVAPI32.@]
1114 * See RegDeleteKeyA.
1116 LSTATUS WINAPI
RegDeleteKeyW( HKEY hkey
, LPCWSTR name
)
1118 return RegDeleteKeyExW( hkey
, name
, 0, 0 );
1122 /******************************************************************************
1123 * RegDeleteKeyExA [ADVAPI32.@]
1125 LSTATUS WINAPI
RegDeleteKeyExA( HKEY hkey
, LPCSTR name
, REGSAM access
, DWORD reserved
)
1130 if (!name
) return ERROR_INVALID_PARAMETER
;
1132 if (!(hkey
= get_special_root_hkey( hkey
, access
))) return ERROR_INVALID_HANDLE
;
1134 access
&= KEY_WOW64_64KEY
| KEY_WOW64_32KEY
;
1135 if (!(ret
= RegOpenKeyExA( hkey
, name
, 0, access
| DELETE
, &tmp
)))
1137 if (!is_version_nt()) /* win95 does recursive key deletes */
1141 while(!RegEnumKeyA(tmp
, 0, sub
, sizeof(sub
)))
1143 if(RegDeleteKeyExA(tmp
, sub
, access
, reserved
)) /* recurse */
1147 ret
= RtlNtStatusToDosError( NtDeleteKey( tmp
) );
1150 TRACE("%s ret=%08x\n", debugstr_a(name
), ret
);
1155 /******************************************************************************
1156 * RegDeleteKeyA [ADVAPI32.@]
1158 * Delete a registry key.
1161 * hkey [I] Handle to parent key containing the key to delete
1162 * name [I] Name of the key user hkey to delete
1166 * MSDN is wrong when it says that hkey must be opened with the DELETE access
1167 * right. In reality, it opens a new handle with DELETE access.
1170 * Success: ERROR_SUCCESS
1171 * Failure: Error code
1173 LSTATUS WINAPI
RegDeleteKeyA( HKEY hkey
, LPCSTR name
)
1175 return RegDeleteKeyExA( hkey
, name
, 0, 0 );
1180 /******************************************************************************
1181 * RegSetValueExW [ADVAPI32.@]
1183 * Set the data and contents of a registry value.
1186 * hkey [I] Handle of key to set value for
1187 * name [I] Name of value to set
1188 * reserved [I] Reserved, must be zero
1189 * type [I] Type of the value being set
1190 * data [I] The new contents of the value to set
1191 * count [I] Size of data
1194 * Success: ERROR_SUCCESS
1195 * Failure: Error code
1197 LSTATUS WINAPI
RegSetValueExW( HKEY hkey
, LPCWSTR name
, DWORD reserved
,
1198 DWORD type
, const BYTE
*data
, DWORD count
)
1200 UNICODE_STRING nameW
;
1202 /* no need for version check, not implemented on win9x anyway */
1204 if (data
&& ((ULONG_PTR
)data
>> 16) == 0) return ERROR_NOACCESS
;
1206 if (count
&& is_string(type
))
1208 LPCWSTR str
= (LPCWSTR
)data
;
1209 /* if user forgot to count terminating null, add it (yes NT does this) */
1210 if (str
[count
/ sizeof(WCHAR
) - 1] && !str
[count
/ sizeof(WCHAR
)])
1211 count
+= sizeof(WCHAR
);
1213 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
1215 RtlInitUnicodeString( &nameW
, name
);
1216 return RtlNtStatusToDosError( NtSetValueKey( hkey
, &nameW
, 0, type
, data
, count
) );
1220 /******************************************************************************
1221 * RegSetValueExA [ADVAPI32.@]
1223 * See RegSetValueExW.
1226 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1227 * NT does definitely care (aj)
1229 LSTATUS WINAPI
RegSetValueExA( HKEY hkey
, LPCSTR name
, DWORD reserved
, DWORD type
,
1230 const BYTE
*data
, DWORD count
)
1233 UNICODE_STRING nameW
;
1234 WCHAR
*dataW
= NULL
;
1237 if (!is_version_nt()) /* win95 */
1241 if (!data
) return ERROR_INVALID_PARAMETER
;
1242 count
= strlen((const char *)data
) + 1;
1245 else if (count
&& is_string(type
))
1247 /* if user forgot to count terminating null, add it (yes NT does this) */
1248 if (data
[count
-1] && !data
[count
]) count
++;
1251 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
1253 if (is_string( type
)) /* need to convert to Unicode */
1256 RtlMultiByteToUnicodeSize( &lenW
, (const char *)data
, count
);
1257 if (!(dataW
= heap_alloc( lenW
))) return ERROR_OUTOFMEMORY
;
1258 RtlMultiByteToUnicodeN( dataW
, lenW
, NULL
, (const char *)data
, count
);
1260 data
= (BYTE
*)dataW
;
1263 RtlInitAnsiString( &nameA
, name
);
1264 if (!(status
= RtlAnsiStringToUnicodeString( &nameW
, &nameA
, TRUE
)))
1266 status
= NtSetValueKey( hkey
, &nameW
, 0, type
, data
, count
);
1267 RtlFreeUnicodeString( &nameW
);
1270 return RtlNtStatusToDosError( status
);
1274 /******************************************************************************
1275 * RegSetValueW [ADVAPI32.@]
1277 * Sets the data for the default or unnamed value of a reg key.
1280 * hkey [I] Handle to an open key.
1281 * subkey [I] Name of a subkey of hKey.
1282 * type [I] Type of information to store.
1283 * data [I] String that contains the data to set for the default value.
1284 * count [I] Ignored.
1287 * Success: ERROR_SUCCESS
1288 * Failure: nonzero error code from Winerror.h
1290 LSTATUS WINAPI
RegSetValueW( HKEY hkey
, LPCWSTR subkey
, DWORD type
, LPCWSTR data
, DWORD count
)
1292 TRACE("(%p,%s,%d,%s,%d)\n", hkey
, debugstr_w(subkey
), type
, debugstr_w(data
), count
);
1294 if (type
!= REG_SZ
|| !data
) return ERROR_INVALID_PARAMETER
;
1296 return RegSetKeyValueW( hkey
, subkey
, NULL
, type
, data
, (strlenW(data
) + 1)*sizeof(WCHAR
) );
1299 /******************************************************************************
1300 * RegSetValueA [ADVAPI32.@]
1304 LSTATUS WINAPI
RegSetValueA( HKEY hkey
, LPCSTR subkey
, DWORD type
, LPCSTR data
, DWORD count
)
1306 TRACE("(%p,%s,%d,%s,%d)\n", hkey
, debugstr_a(subkey
), type
, debugstr_a(data
), count
);
1308 if (type
!= REG_SZ
|| !data
) return ERROR_INVALID_PARAMETER
;
1310 return RegSetKeyValueA( hkey
, subkey
, NULL
, type
, data
, strlen(data
) + 1 );
1313 /******************************************************************************
1314 * RegSetKeyValueW [ADVAPI32.@]
1316 LONG WINAPI
RegSetKeyValueW( HKEY hkey
, LPCWSTR subkey
, LPCWSTR name
, DWORD type
, const void *data
, DWORD len
)
1318 HKEY hsubkey
= NULL
;
1321 TRACE("(%p,%s,%s,%d,%p,%d)\n", hkey
, debugstr_w(subkey
), debugstr_w(name
), type
, data
, len
);
1323 if (subkey
&& subkey
[0]) /* need to create the subkey */
1325 if ((ret
= RegCreateKeyW( hkey
, subkey
, &hsubkey
)) != ERROR_SUCCESS
) return ret
;
1329 ret
= RegSetValueExW( hkey
, name
, 0, type
, (const BYTE
*)data
, len
);
1330 if (hsubkey
) RegCloseKey( hsubkey
);
1334 /******************************************************************************
1335 * RegSetKeyValueA [ADVAPI32.@]
1337 LONG WINAPI
RegSetKeyValueA( HKEY hkey
, LPCSTR subkey
, LPCSTR name
, DWORD type
, const void *data
, DWORD len
)
1339 HKEY hsubkey
= NULL
;
1342 TRACE("(%p,%s,%s,%d,%p,%d)\n", hkey
, debugstr_a(subkey
), debugstr_a(name
), type
, data
, len
);
1344 if (subkey
&& subkey
[0]) /* need to create the subkey */
1346 if ((ret
= RegCreateKeyA( hkey
, subkey
, &hsubkey
)) != ERROR_SUCCESS
) return ret
;
1350 ret
= RegSetValueExA( hkey
, name
, 0, type
, (const BYTE
*)data
, len
);
1351 if (hsubkey
) RegCloseKey( hsubkey
);
1355 /******************************************************************************
1356 * RegQueryValueExW [ADVAPI32.@]
1358 * See RegQueryValueExA.
1360 LSTATUS WINAPI
RegQueryValueExW( HKEY hkey
, LPCWSTR name
, LPDWORD reserved
, LPDWORD type
,
1361 LPBYTE data
, LPDWORD count
)
1364 UNICODE_STRING name_str
;
1366 char buffer
[256], *buf_ptr
= buffer
;
1367 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
1368 static const int info_size
= offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
1370 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1371 hkey
, debugstr_w(name
), reserved
, type
, data
, count
,
1372 (count
&& data
) ? *count
: 0 );
1374 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
1375 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
1377 RtlInitUnicodeString( &name_str
, name
);
1379 if (data
) total_size
= min( sizeof(buffer
), *count
+ info_size
);
1382 total_size
= info_size
;
1383 if (count
) *count
= 0;
1386 status
= NtQueryValueKey( hkey
, &name_str
, KeyValuePartialInformation
,
1387 buffer
, total_size
, &total_size
);
1388 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
1392 /* retry with a dynamically allocated buffer */
1393 while (status
== STATUS_BUFFER_OVERFLOW
&& total_size
- info_size
<= *count
)
1395 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
1396 if (!(buf_ptr
= heap_alloc( total_size
)))
1397 return ERROR_NOT_ENOUGH_MEMORY
;
1398 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf_ptr
;
1399 status
= NtQueryValueKey( hkey
, &name_str
, KeyValuePartialInformation
,
1400 buf_ptr
, total_size
, &total_size
);
1405 memcpy( data
, buf_ptr
+ info_size
, total_size
- info_size
);
1406 /* if the type is REG_SZ and data is not 0-terminated
1407 * and there is enough space in the buffer NT appends a \0 */
1408 if (total_size
- info_size
<= *count
-sizeof(WCHAR
) && is_string(info
->Type
))
1410 WCHAR
*ptr
= (WCHAR
*)(data
+ total_size
- info_size
);
1411 if (ptr
> (WCHAR
*)data
&& ptr
[-1]) *ptr
= 0;
1414 else if (status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
1416 else status
= STATUS_SUCCESS
;
1418 if (type
) *type
= info
->Type
;
1419 if (count
) *count
= total_size
- info_size
;
1422 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
1423 return RtlNtStatusToDosError(status
);
1427 /******************************************************************************
1428 * RegQueryValueExA [ADVAPI32.@]
1430 * Get the type and contents of a specified value under with a key.
1433 * hkey [I] Handle of the key to query
1434 * name [I] Name of value under hkey to query
1435 * reserved [I] Reserved - must be NULL
1436 * type [O] Destination for the value type, or NULL if not required
1437 * data [O] Destination for the values contents, or NULL if not required
1438 * count [I/O] Size of data, updated with the number of bytes returned
1441 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1442 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1443 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1444 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1447 * MSDN states that if data is too small it is partially filled. In reality
1448 * it remains untouched.
1450 LSTATUS WINAPI
RegQueryValueExA( HKEY hkey
, LPCSTR name
, LPDWORD reserved
, LPDWORD type
,
1451 LPBYTE data
, LPDWORD count
)
1455 UNICODE_STRING nameW
;
1456 DWORD total_size
, datalen
= 0;
1457 char buffer
[256], *buf_ptr
= buffer
;
1458 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
1459 static const int info_size
= offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
1461 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1462 hkey
, debugstr_a(name
), reserved
, type
, data
, count
, count
? *count
: 0 );
1464 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
1465 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
1467 if (count
) datalen
= *count
;
1468 if (!data
&& count
) *count
= 0;
1470 /* this matches Win9x behaviour - NT sets *type to a random value */
1471 if (type
) *type
= REG_NONE
;
1473 RtlInitAnsiString( &nameA
, name
);
1474 if ((status
= RtlAnsiStringToUnicodeString( &nameW
, &nameA
, TRUE
)))
1475 return RtlNtStatusToDosError(status
);
1477 status
= NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
1478 buffer
, sizeof(buffer
), &total_size
);
1479 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
1481 /* we need to fetch the contents for a string type even if not requested,
1482 * because we need to compute the length of the ASCII string. */
1483 if (data
|| is_string(info
->Type
))
1485 /* retry with a dynamically allocated buffer */
1486 while (status
== STATUS_BUFFER_OVERFLOW
)
1488 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
1489 if (!(buf_ptr
= heap_alloc( total_size
)))
1491 status
= STATUS_NO_MEMORY
;
1494 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf_ptr
;
1495 status
= NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
1496 buf_ptr
, total_size
, &total_size
);
1499 if (status
) goto done
;
1501 if (is_string(info
->Type
))
1505 RtlUnicodeToMultiByteSize( &len
, (WCHAR
*)(buf_ptr
+ info_size
),
1506 total_size
- info_size
);
1509 if (len
> datalen
) status
= STATUS_BUFFER_OVERFLOW
;
1512 RtlUnicodeToMultiByteN( (char*)data
, len
, NULL
, (WCHAR
*)(buf_ptr
+ info_size
),
1513 total_size
- info_size
);
1514 /* if the type is REG_SZ and data is not 0-terminated
1515 * and there is enough space in the buffer NT appends a \0 */
1516 if (len
< datalen
&& data
[len
-1]) data
[len
] = 0;
1519 total_size
= len
+ info_size
;
1523 if (total_size
- info_size
> datalen
) status
= STATUS_BUFFER_OVERFLOW
;
1524 else memcpy( data
, buf_ptr
+ info_size
, total_size
- info_size
);
1527 else status
= STATUS_SUCCESS
;
1529 if (type
) *type
= info
->Type
;
1530 if (count
) *count
= total_size
- info_size
;
1533 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
1534 RtlFreeUnicodeString( &nameW
);
1535 return RtlNtStatusToDosError(status
);
1539 /******************************************************************************
1540 * RegQueryValueW [ADVAPI32.@]
1542 * Retrieves the data associated with the default or unnamed value of a key.
1545 * hkey [I] Handle to an open key.
1546 * name [I] Name of the subkey of hKey.
1547 * data [O] Receives the string associated with the default value
1549 * count [I/O] Size of lpValue in bytes.
1552 * Success: ERROR_SUCCESS
1553 * Failure: nonzero error code from Winerror.h
1555 LSTATUS WINAPI
RegQueryValueW( HKEY hkey
, LPCWSTR name
, LPWSTR data
, LPLONG count
)
1560 TRACE("(%p,%s,%p,%d)\n", hkey
, debugstr_w(name
), data
, count
? *count
: 0 );
1562 if (name
&& name
[0])
1564 if ((ret
= RegOpenKeyW( hkey
, name
, &subkey
)) != ERROR_SUCCESS
) return ret
;
1566 ret
= RegQueryValueExW( subkey
, NULL
, NULL
, NULL
, (LPBYTE
)data
, (LPDWORD
)count
);
1567 if (subkey
!= hkey
) RegCloseKey( subkey
);
1568 if (ret
== ERROR_FILE_NOT_FOUND
)
1570 /* return empty string if default value not found */
1571 if (data
) *data
= 0;
1572 if (count
) *count
= sizeof(WCHAR
);
1573 ret
= ERROR_SUCCESS
;
1579 /******************************************************************************
1580 * RegQueryValueA [ADVAPI32.@]
1582 * See RegQueryValueW.
1584 LSTATUS WINAPI
RegQueryValueA( HKEY hkey
, LPCSTR name
, LPSTR data
, LPLONG count
)
1589 TRACE("(%p,%s,%p,%d)\n", hkey
, debugstr_a(name
), data
, count
? *count
: 0 );
1591 if (name
&& name
[0])
1593 if ((ret
= RegOpenKeyA( hkey
, name
, &subkey
)) != ERROR_SUCCESS
) return ret
;
1595 ret
= RegQueryValueExA( subkey
, NULL
, NULL
, NULL
, (LPBYTE
)data
, (LPDWORD
)count
);
1596 if (subkey
!= hkey
) RegCloseKey( subkey
);
1597 if (ret
== ERROR_FILE_NOT_FOUND
)
1599 /* return empty string if default value not found */
1600 if (data
) *data
= 0;
1601 if (count
) *count
= 1;
1602 ret
= ERROR_SUCCESS
;
1608 /******************************************************************************
1609 * ADVAPI_ApplyRestrictions [internal]
1611 * Helper function for RegGetValueA/W.
1613 static VOID
ADVAPI_ApplyRestrictions( DWORD dwFlags
, DWORD dwType
,
1614 DWORD cbData
, PLONG ret
)
1616 /* Check if the type is restricted by the passed flags */
1617 if (*ret
== ERROR_SUCCESS
|| *ret
== ERROR_MORE_DATA
)
1623 case REG_NONE
: dwMask
= RRF_RT_REG_NONE
; break;
1624 case REG_SZ
: dwMask
= RRF_RT_REG_SZ
; break;
1625 case REG_EXPAND_SZ
: dwMask
= RRF_RT_REG_EXPAND_SZ
; break;
1626 case REG_MULTI_SZ
: dwMask
= RRF_RT_REG_MULTI_SZ
; break;
1627 case REG_BINARY
: dwMask
= RRF_RT_REG_BINARY
; break;
1628 case REG_DWORD
: dwMask
= RRF_RT_REG_DWORD
; break;
1629 case REG_QWORD
: dwMask
= RRF_RT_REG_QWORD
; break;
1632 if (dwFlags
& dwMask
)
1634 /* Type is not restricted, check for size mismatch */
1635 if (dwType
== REG_BINARY
)
1639 if ((dwFlags
& RRF_RT_ANY
) == RRF_RT_DWORD
)
1641 else if ((dwFlags
& RRF_RT_ANY
) == RRF_RT_QWORD
)
1644 if (cbExpect
&& cbData
!= cbExpect
)
1645 *ret
= ERROR_DATATYPE_MISMATCH
;
1648 else *ret
= ERROR_UNSUPPORTED_TYPE
;
1653 /******************************************************************************
1654 * RegGetValueW [ADVAPI32.@]
1656 * Retrieves the type and data for a value name associated with a key,
1657 * optionally expanding its content and restricting its type.
1660 * hKey [I] Handle to an open key.
1661 * pszSubKey [I] Name of the subkey of hKey.
1662 * pszValue [I] Name of value under hKey/szSubKey to query.
1663 * dwFlags [I] Flags restricting the value type to retrieve.
1664 * pdwType [O] Destination for the values type, may be NULL.
1665 * pvData [O] Destination for the values content, may be NULL.
1666 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
1667 * retrieve the whole content, including the trailing '\0'
1671 * Success: ERROR_SUCCESS
1672 * Failure: nonzero error code from Winerror.h
1675 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
1676 * expanded and pdwType is set to REG_SZ instead.
1677 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1678 * without RRF_NOEXPAND is thus not allowed.
1679 * An exception is the case where RRF_RT_ANY is specified, because then
1680 * RRF_NOEXPAND is allowed.
1682 LSTATUS WINAPI
RegGetValueW( HKEY hKey
, LPCWSTR pszSubKey
, LPCWSTR pszValue
,
1683 DWORD dwFlags
, LPDWORD pdwType
, PVOID pvData
,
1686 DWORD dwType
, cbData
= pcbData
? *pcbData
: 0;
1690 TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n",
1691 hKey
, debugstr_w(pszSubKey
), debugstr_w(pszValue
), dwFlags
, pdwType
,
1692 pvData
, pcbData
, cbData
);
1694 if (pvData
&& !pcbData
)
1695 return ERROR_INVALID_PARAMETER
;
1696 if ((dwFlags
& RRF_RT_REG_EXPAND_SZ
) && !(dwFlags
& RRF_NOEXPAND
) &&
1697 ((dwFlags
& RRF_RT_ANY
) != RRF_RT_ANY
))
1698 return ERROR_INVALID_PARAMETER
;
1700 if (pszSubKey
&& pszSubKey
[0])
1702 ret
= RegOpenKeyExW(hKey
, pszSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
1703 if (ret
!= ERROR_SUCCESS
) return ret
;
1706 ret
= RegQueryValueExW(hKey
, pszValue
, NULL
, &dwType
, pvData
, &cbData
);
1708 /* If we are going to expand we need to read in the whole the value even
1709 * if the passed buffer was too small as the expanded string might be
1710 * smaller than the unexpanded one and could fit into cbData bytes. */
1711 if ((ret
== ERROR_SUCCESS
|| ret
== ERROR_MORE_DATA
) &&
1712 dwType
== REG_EXPAND_SZ
&& !(dwFlags
& RRF_NOEXPAND
))
1717 pvBuf
= heap_alloc(cbData
);
1720 ret
= ERROR_NOT_ENOUGH_MEMORY
;
1724 if (ret
== ERROR_MORE_DATA
|| !pvData
)
1725 ret
= RegQueryValueExW(hKey
, pszValue
, NULL
,
1726 &dwType
, pvBuf
, &cbData
);
1729 /* Even if cbData was large enough we have to copy the
1730 * string since ExpandEnvironmentStrings can't handle
1731 * overlapping buffers. */
1732 CopyMemory(pvBuf
, pvData
, cbData
);
1735 /* Both the type or the value itself could have been modified in
1736 * between so we have to keep retrying until the buffer is large
1737 * enough or we no longer have to expand the value. */
1738 } while (dwType
== REG_EXPAND_SZ
&& ret
== ERROR_MORE_DATA
);
1740 if (ret
== ERROR_SUCCESS
)
1742 /* Recheck dwType in case it changed since the first call */
1743 if (dwType
== REG_EXPAND_SZ
)
1745 cbData
= ExpandEnvironmentStringsW(pvBuf
, pvData
,
1746 pcbData
? *pcbData
: 0) * sizeof(WCHAR
);
1748 if(pvData
&& pcbData
&& cbData
> *pcbData
)
1749 ret
= ERROR_MORE_DATA
;
1752 CopyMemory(pvData
, pvBuf
, *pcbData
);
1758 if (pszSubKey
&& pszSubKey
[0])
1761 ADVAPI_ApplyRestrictions(dwFlags
, dwType
, cbData
, &ret
);
1763 if (pvData
&& ret
!= ERROR_SUCCESS
&& (dwFlags
& RRF_ZEROONFAILURE
))
1764 ZeroMemory(pvData
, *pcbData
);
1766 if (pdwType
) *pdwType
= dwType
;
1767 if (pcbData
) *pcbData
= cbData
;
1773 /******************************************************************************
1774 * RegGetValueA [ADVAPI32.@]
1778 LSTATUS WINAPI
RegGetValueA( HKEY hKey
, LPCSTR pszSubKey
, LPCSTR pszValue
,
1779 DWORD dwFlags
, LPDWORD pdwType
, PVOID pvData
,
1782 DWORD dwType
, cbData
= pcbData
? *pcbData
: 0;
1786 TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n",
1787 hKey
, debugstr_a(pszSubKey
), debugstr_a(pszValue
), dwFlags
,
1788 pdwType
, pvData
, pcbData
, cbData
);
1790 if (pvData
&& !pcbData
)
1791 return ERROR_INVALID_PARAMETER
;
1792 if ((dwFlags
& RRF_RT_REG_EXPAND_SZ
) && !(dwFlags
& RRF_NOEXPAND
) &&
1793 ((dwFlags
& RRF_RT_ANY
) != RRF_RT_ANY
))
1794 return ERROR_INVALID_PARAMETER
;
1796 if (pszSubKey
&& pszSubKey
[0])
1798 ret
= RegOpenKeyExA(hKey
, pszSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
1799 if (ret
!= ERROR_SUCCESS
) return ret
;
1802 ret
= RegQueryValueExA(hKey
, pszValue
, NULL
, &dwType
, pvData
, &cbData
);
1804 /* If we are going to expand we need to read in the whole the value even
1805 * if the passed buffer was too small as the expanded string might be
1806 * smaller than the unexpanded one and could fit into cbData bytes. */
1807 if ((ret
== ERROR_SUCCESS
|| ret
== ERROR_MORE_DATA
) &&
1808 dwType
== REG_EXPAND_SZ
&& !(dwFlags
& RRF_NOEXPAND
))
1813 pvBuf
= heap_alloc(cbData
);
1816 ret
= ERROR_NOT_ENOUGH_MEMORY
;
1820 if (ret
== ERROR_MORE_DATA
|| !pvData
)
1821 ret
= RegQueryValueExA(hKey
, pszValue
, NULL
,
1822 &dwType
, pvBuf
, &cbData
);
1825 /* Even if cbData was large enough we have to copy the
1826 * string since ExpandEnvironmentStrings can't handle
1827 * overlapping buffers. */
1828 CopyMemory(pvBuf
, pvData
, cbData
);
1831 /* Both the type or the value itself could have been modified in
1832 * between so we have to keep retrying until the buffer is large
1833 * enough or we no longer have to expand the value. */
1834 } while (dwType
== REG_EXPAND_SZ
&& ret
== ERROR_MORE_DATA
);
1836 if (ret
== ERROR_SUCCESS
)
1838 /* Recheck dwType in case it changed since the first call */
1839 if (dwType
== REG_EXPAND_SZ
)
1841 cbData
= ExpandEnvironmentStringsA(pvBuf
, pvData
,
1842 pcbData
? *pcbData
: 0);
1844 if(pvData
&& pcbData
&& cbData
> *pcbData
)
1845 ret
= ERROR_MORE_DATA
;
1848 CopyMemory(pvData
, pvBuf
, *pcbData
);
1854 if (pszSubKey
&& pszSubKey
[0])
1857 ADVAPI_ApplyRestrictions(dwFlags
, dwType
, cbData
, &ret
);
1859 if (pvData
&& ret
!= ERROR_SUCCESS
&& (dwFlags
& RRF_ZEROONFAILURE
))
1860 ZeroMemory(pvData
, *pcbData
);
1862 if (pdwType
) *pdwType
= dwType
;
1863 if (pcbData
) *pcbData
= cbData
;
1869 /******************************************************************************
1870 * RegEnumValueW [ADVAPI32.@]
1872 * Enumerates the values for the specified open registry key.
1875 * hkey [I] Handle to key to query
1876 * index [I] Index of value to query
1877 * value [O] Value string
1878 * val_count [I/O] Size of value buffer (in wchars)
1879 * reserved [I] Reserved
1880 * type [O] Type code
1881 * data [O] Value data
1882 * count [I/O] Size of data buffer (in bytes)
1885 * Success: ERROR_SUCCESS
1886 * Failure: nonzero error code from Winerror.h
1889 LSTATUS WINAPI
RegEnumValueW( HKEY hkey
, DWORD index
, LPWSTR value
, LPDWORD val_count
,
1890 LPDWORD reserved
, LPDWORD type
, LPBYTE data
, LPDWORD count
)
1894 char buffer
[256], *buf_ptr
= buffer
;
1895 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)buffer
;
1896 static const int info_size
= offsetof( KEY_VALUE_FULL_INFORMATION
, Name
);
1898 TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
1899 hkey
, index
, value
, val_count
, reserved
, type
, data
, count
);
1901 /* NT only checks count, not val_count */
1902 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
1903 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
1905 total_size
= info_size
+ (MAX_PATH
+ 1) * sizeof(WCHAR
);
1906 if (data
) total_size
+= *count
;
1907 total_size
= min( sizeof(buffer
), total_size
);
1909 status
= NtEnumerateValueKey( hkey
, index
, KeyValueFullInformation
,
1910 buffer
, total_size
, &total_size
);
1911 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
1915 /* retry with a dynamically allocated buffer */
1916 while (status
== STATUS_BUFFER_OVERFLOW
)
1918 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
1919 if (!(buf_ptr
= heap_alloc( total_size
)))
1920 return ERROR_NOT_ENOUGH_MEMORY
;
1921 info
= (KEY_VALUE_FULL_INFORMATION
*)buf_ptr
;
1922 status
= NtEnumerateValueKey( hkey
, index
, KeyValueFullInformation
,
1923 buf_ptr
, total_size
, &total_size
);
1926 if (status
) goto done
;
1930 if (info
->NameLength
/sizeof(WCHAR
) >= *val_count
)
1932 status
= STATUS_BUFFER_OVERFLOW
;
1935 memcpy( value
, info
->Name
, info
->NameLength
);
1936 *val_count
= info
->NameLength
/ sizeof(WCHAR
);
1937 value
[*val_count
] = 0;
1942 if (total_size
- info
->DataOffset
> *count
)
1944 status
= STATUS_BUFFER_OVERFLOW
;
1947 memcpy( data
, buf_ptr
+ info
->DataOffset
, total_size
- info
->DataOffset
);
1948 if (total_size
- info
->DataOffset
<= *count
-sizeof(WCHAR
) && is_string(info
->Type
))
1950 /* if the type is REG_SZ and data is not 0-terminated
1951 * and there is enough space in the buffer NT appends a \0 */
1952 WCHAR
*ptr
= (WCHAR
*)(data
+ total_size
- info
->DataOffset
);
1953 if (ptr
> (WCHAR
*)data
&& ptr
[-1]) *ptr
= 0;
1957 else status
= STATUS_SUCCESS
;
1960 if (type
) *type
= info
->Type
;
1961 if (count
) *count
= info
->DataLength
;
1964 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
1965 return RtlNtStatusToDosError(status
);
1969 /******************************************************************************
1970 * RegEnumValueA [ADVAPI32.@]
1972 * See RegEnumValueW.
1974 LSTATUS WINAPI
RegEnumValueA( HKEY hkey
, DWORD index
, LPSTR value
, LPDWORD val_count
,
1975 LPDWORD reserved
, LPDWORD type
, LPBYTE data
, LPDWORD count
)
1979 char buffer
[256], *buf_ptr
= buffer
;
1980 KEY_VALUE_FULL_INFORMATION
*info
= (KEY_VALUE_FULL_INFORMATION
*)buffer
;
1981 static const int info_size
= offsetof( KEY_VALUE_FULL_INFORMATION
, Name
);
1983 TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
1984 hkey
, index
, value
, val_count
, reserved
, type
, data
, count
);
1986 /* NT only checks count, not val_count */
1987 if ((data
&& !count
) || reserved
) return ERROR_INVALID_PARAMETER
;
1988 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
1990 total_size
= info_size
+ (MAX_PATH
+ 1) * sizeof(WCHAR
);
1991 if (data
) total_size
+= *count
;
1992 total_size
= min( sizeof(buffer
), total_size
);
1994 status
= NtEnumerateValueKey( hkey
, index
, KeyValueFullInformation
,
1995 buffer
, total_size
, &total_size
);
1996 if (status
&& status
!= STATUS_BUFFER_OVERFLOW
) goto done
;
1998 /* we need to fetch the contents for a string type even if not requested,
1999 * because we need to compute the length of the ASCII string. */
2000 if (value
|| data
|| is_string(info
->Type
))
2002 /* retry with a dynamically allocated buffer */
2003 while (status
== STATUS_BUFFER_OVERFLOW
)
2005 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
2006 if (!(buf_ptr
= heap_alloc( total_size
)))
2007 return ERROR_NOT_ENOUGH_MEMORY
;
2008 info
= (KEY_VALUE_FULL_INFORMATION
*)buf_ptr
;
2009 status
= NtEnumerateValueKey( hkey
, index
, KeyValueFullInformation
,
2010 buf_ptr
, total_size
, &total_size
);
2013 if (status
) goto done
;
2015 if (is_string(info
->Type
))
2018 RtlUnicodeToMultiByteSize( &len
, (WCHAR
*)(buf_ptr
+ info
->DataOffset
),
2019 total_size
- info
->DataOffset
);
2022 if (len
> *count
) status
= STATUS_BUFFER_OVERFLOW
;
2025 RtlUnicodeToMultiByteN( (char*)data
, len
, NULL
, (WCHAR
*)(buf_ptr
+ info
->DataOffset
),
2026 total_size
- info
->DataOffset
);
2027 /* if the type is REG_SZ and data is not 0-terminated
2028 * and there is enough space in the buffer NT appends a \0 */
2029 if (len
< *count
&& data
[len
-1]) data
[len
] = 0;
2032 info
->DataLength
= len
;
2036 if (total_size
- info
->DataOffset
> *count
) status
= STATUS_BUFFER_OVERFLOW
;
2037 else memcpy( data
, buf_ptr
+ info
->DataOffset
, total_size
- info
->DataOffset
);
2040 if (value
&& !status
)
2044 RtlUnicodeToMultiByteSize( &len
, info
->Name
, info
->NameLength
);
2045 if (len
>= *val_count
)
2047 status
= STATUS_BUFFER_OVERFLOW
;
2050 len
= *val_count
- 1;
2051 RtlUnicodeToMultiByteN( value
, len
, NULL
, info
->Name
, info
->NameLength
);
2057 RtlUnicodeToMultiByteN( value
, len
, NULL
, info
->Name
, info
->NameLength
);
2063 else status
= STATUS_SUCCESS
;
2065 if (type
) *type
= info
->Type
;
2066 if (count
) *count
= info
->DataLength
;
2069 if (buf_ptr
!= buffer
) heap_free( buf_ptr
);
2070 return RtlNtStatusToDosError(status
);
2073 /******************************************************************************
2074 * RegDeleteValueW [ADVAPI32.@]
2076 * See RegDeleteValueA.
2078 LSTATUS WINAPI
RegDeleteValueW( HKEY hkey
, LPCWSTR name
)
2080 return RegDeleteKeyValueW( hkey
, NULL
, name
);
2083 /******************************************************************************
2084 * RegDeleteValueA [ADVAPI32.@]
2086 * Delete a value from the registry.
2089 * hkey [I] Registry handle of the key holding the value
2090 * name [I] Name of the value under hkey to delete
2093 * Success: ERROR_SUCCESS
2094 * Failure: nonzero error code from Winerror.h
2096 LSTATUS WINAPI
RegDeleteValueA( HKEY hkey
, LPCSTR name
)
2098 return RegDeleteKeyValueA( hkey
, NULL
, name
);
2101 /******************************************************************************
2102 * RegDeleteKeyValueW [ADVAPI32.@]
2104 LONG WINAPI
RegDeleteKeyValueW( HKEY hkey
, LPCWSTR subkey
, LPCWSTR name
)
2106 UNICODE_STRING nameW
;
2110 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
2114 if ((ret
= RegOpenKeyExW( hkey
, subkey
, 0, KEY_SET_VALUE
, &hsubkey
)))
2119 RtlInitUnicodeString( &nameW
, name
);
2120 ret
= RtlNtStatusToDosError( NtDeleteValueKey( hkey
, &nameW
) );
2121 if (hsubkey
) RegCloseKey( hsubkey
);
2125 /******************************************************************************
2126 * RegDeleteKeyValueA [ADVAPI32.@]
2128 LONG WINAPI
RegDeleteKeyValueA( HKEY hkey
, LPCSTR subkey
, LPCSTR name
)
2130 UNICODE_STRING nameW
;
2135 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
2139 LONG ret
= RegOpenKeyExA( hkey
, subkey
, 0, KEY_SET_VALUE
, &hsubkey
);
2145 RtlInitAnsiString( &nameA
, name
);
2146 if (!(status
= RtlAnsiStringToUnicodeString( &nameW
, &nameA
, TRUE
)))
2148 status
= NtDeleteValueKey( hkey
, &nameW
);
2149 RtlFreeUnicodeString( &nameW
);
2152 if (hsubkey
) RegCloseKey( hsubkey
);
2153 return RtlNtStatusToDosError( status
);
2156 /******************************************************************************
2157 * RegLoadKeyW [ADVAPI32.@]
2159 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
2160 * registration information from a specified file into that subkey.
2163 * hkey [I] Handle of open key
2164 * subkey [I] Address of name of subkey
2165 * filename [I] Address of filename for registry information
2168 * Success: ERROR_SUCCESS
2169 * Failure: nonzero error code from Winerror.h
2171 LSTATUS WINAPI
RegLoadKeyW( HKEY hkey
, LPCWSTR subkey
, LPCWSTR filename
)
2173 OBJECT_ATTRIBUTES destkey
, file
;
2174 UNICODE_STRING subkeyW
, filenameW
;
2177 if (!(hkey
= get_special_root_hkey(hkey
, 0))) return ERROR_INVALID_HANDLE
;
2179 destkey
.Length
= sizeof(destkey
);
2180 destkey
.RootDirectory
= hkey
; /* root key: HKLM or HKU */
2181 destkey
.ObjectName
= &subkeyW
; /* name of the key */
2182 destkey
.Attributes
= 0;
2183 destkey
.SecurityDescriptor
= NULL
;
2184 destkey
.SecurityQualityOfService
= NULL
;
2185 RtlInitUnicodeString(&subkeyW
, subkey
);
2187 file
.Length
= sizeof(file
);
2188 file
.RootDirectory
= NULL
;
2189 file
.ObjectName
= &filenameW
; /* file containing the hive */
2190 file
.Attributes
= OBJ_CASE_INSENSITIVE
;
2191 file
.SecurityDescriptor
= NULL
;
2192 file
.SecurityQualityOfService
= NULL
;
2193 RtlDosPathNameToNtPathName_U(filename
, &filenameW
, NULL
, NULL
);
2195 status
= NtLoadKey(&destkey
, &file
);
2196 RtlFreeUnicodeString(&filenameW
);
2197 return RtlNtStatusToDosError( status
);
2201 /******************************************************************************
2202 * RegLoadKeyA [ADVAPI32.@]
2206 LSTATUS WINAPI
RegLoadKeyA( HKEY hkey
, LPCSTR subkey
, LPCSTR filename
)
2208 UNICODE_STRING subkeyW
, filenameW
;
2209 STRING subkeyA
, filenameA
;
2213 RtlInitAnsiString(&subkeyA
, subkey
);
2214 RtlInitAnsiString(&filenameA
, filename
);
2216 RtlInitUnicodeString(&subkeyW
, NULL
);
2217 RtlInitUnicodeString(&filenameW
, NULL
);
2218 if (!(status
= RtlAnsiStringToUnicodeString(&subkeyW
, &subkeyA
, TRUE
)) &&
2219 !(status
= RtlAnsiStringToUnicodeString(&filenameW
, &filenameA
, TRUE
)))
2221 ret
= RegLoadKeyW(hkey
, subkeyW
.Buffer
, filenameW
.Buffer
);
2223 else ret
= RtlNtStatusToDosError(status
);
2224 RtlFreeUnicodeString(&subkeyW
);
2225 RtlFreeUnicodeString(&filenameW
);
2230 /******************************************************************************
2231 * RegSaveKeyW [ADVAPI32.@]
2233 * Save a key and all of its subkeys and values to a new file in the standard format.
2236 * hkey [I] Handle of key where save begins
2237 * lpFile [I] Address of filename to save to
2238 * sa [I] Address of security structure
2241 * Success: ERROR_SUCCESS
2242 * Failure: nonzero error code from Winerror.h
2244 LSTATUS WINAPI
RegSaveKeyW( HKEY hkey
, LPCWSTR file
, LPSECURITY_ATTRIBUTES sa
)
2246 static const WCHAR format
[] =
2247 {'r','e','g','%','0','4','x','.','t','m','p',0};
2248 WCHAR buffer
[MAX_PATH
];
2254 TRACE( "(%p,%s,%p)\n", hkey
, debugstr_w(file
), sa
);
2256 if (!file
|| !*file
) return ERROR_INVALID_PARAMETER
;
2257 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
2259 err
= GetLastError();
2260 GetFullPathNameW( file
, sizeof(buffer
)/sizeof(WCHAR
), buffer
, &nameW
);
2264 snprintfW( nameW
, 16, format
, count
++ );
2265 handle
= CreateFileW( buffer
, GENERIC_WRITE
, 0, NULL
,
2266 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, 0 );
2267 if (handle
!= INVALID_HANDLE_VALUE
) break;
2268 if ((ret
= GetLastError()) != ERROR_FILE_EXISTS
) goto done
;
2270 /* Something gone haywire ? Please report if this happens abnormally */
2272 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
);
2275 ret
= RtlNtStatusToDosError(NtSaveKey(hkey
, handle
));
2277 CloseHandle( handle
);
2280 if (!MoveFileExW( buffer
, file
, MOVEFILE_REPLACE_EXISTING
))
2282 ERR( "Failed to move %s to %s\n", debugstr_w(buffer
),
2284 ret
= GetLastError();
2287 if (ret
) DeleteFileW( buffer
);
2290 SetLastError( err
); /* restore last error code */
2295 /******************************************************************************
2296 * RegSaveKeyA [ADVAPI32.@]
2300 LSTATUS WINAPI
RegSaveKeyA( HKEY hkey
, LPCSTR file
, LPSECURITY_ATTRIBUTES sa
)
2302 UNICODE_STRING
*fileW
= &NtCurrentTeb()->StaticUnicodeString
;
2306 RtlInitAnsiString(&fileA
, file
);
2307 if ((status
= RtlAnsiStringToUnicodeString(fileW
, &fileA
, FALSE
)))
2308 return RtlNtStatusToDosError( status
);
2309 return RegSaveKeyW(hkey
, fileW
->Buffer
, sa
);
2313 /******************************************************************************
2314 * RegRestoreKeyW [ADVAPI32.@]
2316 * Read the registry information from a file and copy it over a key.
2319 * hkey [I] Handle of key where restore begins
2320 * lpFile [I] Address of filename containing saved tree
2321 * dwFlags [I] Optional flags
2324 * Success: ERROR_SUCCESS
2325 * Failure: nonzero error code from Winerror.h
2327 LSTATUS WINAPI
RegRestoreKeyW( HKEY hkey
, LPCWSTR lpFile
, DWORD dwFlags
)
2329 TRACE("(%p,%s,%d)\n",hkey
,debugstr_w(lpFile
),dwFlags
);
2331 /* It seems to do this check before the hkey check */
2332 if (!lpFile
|| !*lpFile
)
2333 return ERROR_INVALID_PARAMETER
;
2335 FIXME("(%p,%s,%d): stub\n",hkey
,debugstr_w(lpFile
),dwFlags
);
2337 /* Check for file existence */
2339 return ERROR_SUCCESS
;
2343 /******************************************************************************
2344 * RegRestoreKeyA [ADVAPI32.@]
2346 * See RegRestoreKeyW.
2348 LSTATUS WINAPI
RegRestoreKeyA( HKEY hkey
, LPCSTR lpFile
, DWORD dwFlags
)
2350 UNICODE_STRING lpFileW
;
2353 RtlCreateUnicodeStringFromAsciiz( &lpFileW
, lpFile
);
2354 ret
= RegRestoreKeyW( hkey
, lpFileW
.Buffer
, dwFlags
);
2355 RtlFreeUnicodeString( &lpFileW
);
2360 /******************************************************************************
2361 * RegUnLoadKeyW [ADVAPI32.@]
2363 * Unload a registry key and its subkeys from the registry.
2366 * hkey [I] Handle of open key
2367 * lpSubKey [I] Address of name of subkey to unload
2370 * Success: ERROR_SUCCESS
2371 * Failure: nonzero error code from Winerror.h
2373 LSTATUS WINAPI
RegUnLoadKeyW( HKEY hkey
, LPCWSTR lpSubKey
)
2377 OBJECT_ATTRIBUTES attr
;
2378 UNICODE_STRING subkey
;
2380 TRACE("(%p,%s)\n",hkey
, debugstr_w(lpSubKey
));
2382 ret
= RegOpenKeyW(hkey
,lpSubKey
,&shkey
);
2384 return ERROR_INVALID_PARAMETER
;
2386 RtlInitUnicodeString(&subkey
, lpSubKey
);
2387 InitializeObjectAttributes(&attr
, &subkey
, OBJ_CASE_INSENSITIVE
, shkey
, NULL
);
2388 ret
= RtlNtStatusToDosError(NtUnloadKey(&attr
));
2396 /******************************************************************************
2397 * RegUnLoadKeyA [ADVAPI32.@]
2399 * See RegUnLoadKeyW.
2401 LSTATUS WINAPI
RegUnLoadKeyA( HKEY hkey
, LPCSTR lpSubKey
)
2403 UNICODE_STRING lpSubKeyW
;
2406 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW
, lpSubKey
);
2407 ret
= RegUnLoadKeyW( hkey
, lpSubKeyW
.Buffer
);
2408 RtlFreeUnicodeString( &lpSubKeyW
);
2413 /******************************************************************************
2414 * RegReplaceKeyW [ADVAPI32.@]
2416 * Replace the file backing a registry key and all its subkeys with another file.
2419 * hkey [I] Handle of open key
2420 * lpSubKey [I] Address of name of subkey
2421 * lpNewFile [I] Address of filename for file with new data
2422 * lpOldFile [I] Address of filename for backup file
2425 * Success: ERROR_SUCCESS
2426 * Failure: nonzero error code from Winerror.h
2428 LSTATUS WINAPI
RegReplaceKeyW( HKEY hkey
, LPCWSTR lpSubKey
, LPCWSTR lpNewFile
,
2431 FIXME("(%p,%s,%s,%s): stub\n", hkey
, debugstr_w(lpSubKey
),
2432 debugstr_w(lpNewFile
),debugstr_w(lpOldFile
));
2433 return ERROR_SUCCESS
;
2437 /******************************************************************************
2438 * RegReplaceKeyA [ADVAPI32.@]
2440 * See RegReplaceKeyW.
2442 LSTATUS WINAPI
RegReplaceKeyA( HKEY hkey
, LPCSTR lpSubKey
, LPCSTR lpNewFile
,
2445 UNICODE_STRING lpSubKeyW
;
2446 UNICODE_STRING lpNewFileW
;
2447 UNICODE_STRING lpOldFileW
;
2450 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW
, lpSubKey
);
2451 RtlCreateUnicodeStringFromAsciiz( &lpOldFileW
, lpOldFile
);
2452 RtlCreateUnicodeStringFromAsciiz( &lpNewFileW
, lpNewFile
);
2453 ret
= RegReplaceKeyW( hkey
, lpSubKeyW
.Buffer
, lpNewFileW
.Buffer
, lpOldFileW
.Buffer
);
2454 RtlFreeUnicodeString( &lpOldFileW
);
2455 RtlFreeUnicodeString( &lpNewFileW
);
2456 RtlFreeUnicodeString( &lpSubKeyW
);
2461 /******************************************************************************
2462 * RegSetKeySecurity [ADVAPI32.@]
2464 * Set the security of an open registry key.
2467 * hkey [I] Open handle of key to set
2468 * SecurityInfo [I] Descriptor contents
2469 * pSecurityDesc [I] Address of descriptor for key
2472 * Success: ERROR_SUCCESS
2473 * Failure: nonzero error code from Winerror.h
2475 LSTATUS WINAPI
RegSetKeySecurity( HKEY hkey
, SECURITY_INFORMATION SecurityInfo
,
2476 PSECURITY_DESCRIPTOR pSecurityDesc
)
2478 TRACE("(%p,%d,%p)\n",hkey
,SecurityInfo
,pSecurityDesc
);
2480 /* It seems to perform this check before the hkey check */
2481 if ((SecurityInfo
& OWNER_SECURITY_INFORMATION
) ||
2482 (SecurityInfo
& GROUP_SECURITY_INFORMATION
) ||
2483 (SecurityInfo
& DACL_SECURITY_INFORMATION
) ||
2484 (SecurityInfo
& SACL_SECURITY_INFORMATION
)) {
2487 return ERROR_INVALID_PARAMETER
;
2490 return ERROR_INVALID_PARAMETER
;
2492 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
2494 return RtlNtStatusToDosError( NtSetSecurityObject( hkey
, SecurityInfo
, pSecurityDesc
) );
2498 /******************************************************************************
2499 * RegGetKeySecurity [ADVAPI32.@]
2501 * Get a copy of the security descriptor for a given registry key.
2504 * hkey [I] Open handle of key to set
2505 * SecurityInformation [I] Descriptor contents
2506 * pSecurityDescriptor [O] Address of descriptor for key
2507 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2510 * Success: ERROR_SUCCESS
2511 * Failure: Error code
2513 LSTATUS WINAPI
RegGetKeySecurity( HKEY hkey
, SECURITY_INFORMATION SecurityInformation
,
2514 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
2515 LPDWORD lpcbSecurityDescriptor
)
2517 TRACE("(%p,%d,%p,%d)\n",hkey
,SecurityInformation
,pSecurityDescriptor
,
2518 *lpcbSecurityDescriptor
);
2520 if (!(hkey
= get_special_root_hkey( hkey
, 0 ))) return ERROR_INVALID_HANDLE
;
2522 return RtlNtStatusToDosError( NtQuerySecurityObject( hkey
,
2523 SecurityInformation
, pSecurityDescriptor
,
2524 *lpcbSecurityDescriptor
, lpcbSecurityDescriptor
) );
2528 /******************************************************************************
2529 * RegFlushKey [ADVAPI32.@]
2531 * Immediately write a registry key to registry.
2534 * hkey [I] Handle of key to write
2537 * Success: ERROR_SUCCESS
2538 * Failure: Error code
2540 LSTATUS WINAPI
RegFlushKey( HKEY hkey
)
2542 hkey
= get_special_root_hkey( hkey
, 0 );
2543 if (!hkey
) return ERROR_INVALID_HANDLE
;
2545 return RtlNtStatusToDosError( NtFlushKey( hkey
) );
2549 /******************************************************************************
2550 * RegConnectRegistryW [ADVAPI32.@]
2552 * Establish a connection to a predefined registry key on another computer.
2555 * lpMachineName [I] Address of name of remote computer
2556 * hHey [I] Predefined registry handle
2557 * phkResult [I] Address of buffer for remote registry handle
2560 * Success: ERROR_SUCCESS
2561 * Failure: nonzero error code from Winerror.h
2563 LSTATUS WINAPI
RegConnectRegistryW( LPCWSTR lpMachineName
, HKEY hKey
,
2568 TRACE("(%s,%p,%p)\n",debugstr_w(lpMachineName
), hKey
, phkResult
);
2570 if (!lpMachineName
|| !*lpMachineName
) {
2571 /* Use the local machine name */
2572 ret
= RegOpenKeyW( hKey
, NULL
, phkResult
);
2575 WCHAR compName
[MAX_COMPUTERNAME_LENGTH
+ 1];
2576 DWORD len
= sizeof(compName
) / sizeof(WCHAR
);
2578 /* MSDN says lpMachineName must start with \\ : not so */
2579 if( lpMachineName
[0] == '\\' && lpMachineName
[1] == '\\')
2581 if (GetComputerNameW(compName
, &len
))
2583 if (!strcmpiW(lpMachineName
, compName
))
2584 ret
= RegOpenKeyW(hKey
, NULL
, phkResult
);
2587 FIXME("Connect to %s is not supported.\n",debugstr_w(lpMachineName
));
2588 ret
= ERROR_BAD_NETPATH
;
2592 ret
= GetLastError();
2598 /******************************************************************************
2599 * RegConnectRegistryA [ADVAPI32.@]
2601 * See RegConnectRegistryW.
2603 LSTATUS WINAPI
RegConnectRegistryA( LPCSTR machine
, HKEY hkey
, PHKEY reskey
)
2605 UNICODE_STRING machineW
;
2608 RtlCreateUnicodeStringFromAsciiz( &machineW
, machine
);
2609 ret
= RegConnectRegistryW( machineW
.Buffer
, hkey
, reskey
);
2610 RtlFreeUnicodeString( &machineW
);
2615 /******************************************************************************
2616 * RegNotifyChangeKeyValue [ADVAPI32.@]
2618 * Notify the caller about changes to the attributes or contents of a registry key.
2621 * hkey [I] Handle of key to watch
2622 * fWatchSubTree [I] Flag for subkey notification
2623 * fdwNotifyFilter [I] Changes to be reported
2624 * hEvent [I] Handle of signaled event
2625 * fAsync [I] Flag for asynchronous reporting
2628 * Success: ERROR_SUCCESS
2629 * Failure: nonzero error code from Winerror.h
2631 LSTATUS WINAPI
RegNotifyChangeKeyValue( HKEY hkey
, BOOL fWatchSubTree
,
2632 DWORD fdwNotifyFilter
, HANDLE hEvent
,
2636 IO_STATUS_BLOCK iosb
;
2638 hkey
= get_special_root_hkey( hkey
, 0 );
2639 if (!hkey
) return ERROR_INVALID_HANDLE
;
2641 TRACE("(%p,%i,%d,%p,%i)\n", hkey
, fWatchSubTree
, fdwNotifyFilter
,
2644 status
= NtNotifyChangeKey( hkey
, hEvent
, NULL
, NULL
, &iosb
,
2645 fdwNotifyFilter
, fAsync
, NULL
, 0,
2648 if (status
&& status
!= STATUS_TIMEOUT
)
2649 return RtlNtStatusToDosError( status
);
2651 return ERROR_SUCCESS
;
2654 /******************************************************************************
2655 * RegOpenUserClassesRoot [ADVAPI32.@]
2657 * Open the HKEY_CLASSES_ROOT key for a user.
2660 * hToken [I] Handle of token representing the user
2661 * dwOptions [I] Reserved, must be 0
2662 * samDesired [I] Desired access rights
2663 * phkResult [O] Destination for the resulting key handle
2666 * Success: ERROR_SUCCESS
2667 * Failure: nonzero error code from Winerror.h
2670 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2671 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2672 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2674 LSTATUS WINAPI
RegOpenUserClassesRoot(
2681 FIXME("(%p, 0x%x, 0x%x, %p) semi-stub\n", hToken
, dwOptions
, samDesired
, phkResult
);
2683 *phkResult
= HKEY_CLASSES_ROOT
;
2684 return ERROR_SUCCESS
;
2687 /******************************************************************************
2688 * load_string [Internal]
2690 * This is basically a copy of user32/resource.c's LoadStringW. Necessary to
2691 * avoid importing user32, which is higher level than advapi32. Helper for
2694 static int load_string(HINSTANCE hModule
, UINT resId
, LPWSTR pwszBuffer
, INT cMaxChars
)
2701 /* Negative values have to be inverted. */
2702 if (HIWORD(resId
) == 0xffff)
2703 resId
= (UINT
)(-((INT
)resId
));
2705 /* Load the resource into memory and get a pointer to it. */
2706 hResource
= FindResourceW(hModule
, MAKEINTRESOURCEW(LOWORD(resId
>> 4) + 1), (LPWSTR
)RT_STRING
);
2707 if (!hResource
) return 0;
2708 hMemory
= LoadResource(hModule
, hResource
);
2709 if (!hMemory
) return 0;
2710 pString
= LockResource(hMemory
);
2712 /* Strings are length-prefixed. Lowest nibble of resId is an index. */
2713 idxString
= resId
& 0xf;
2714 while (idxString
--) pString
+= *pString
+ 1;
2716 /* If no buffer is given, return length of the string. */
2717 if (!pwszBuffer
) return *pString
;
2719 /* Else copy over the string, respecting the buffer size. */
2720 cMaxChars
= (*pString
< cMaxChars
) ? *pString
: (cMaxChars
- 1);
2721 if (cMaxChars
>= 0) {
2722 memcpy(pwszBuffer
, pString
+1, cMaxChars
* sizeof(WCHAR
));
2723 pwszBuffer
[cMaxChars
] = '\0';
2729 /******************************************************************************
2730 * RegLoadMUIStringW [ADVAPI32.@]
2732 * Load the localized version of a string resource from some PE, respective
2733 * id and path of which are given in the registry value in the format
2734 * @[path]\dllname,-resourceId
2737 * hKey [I] Key, of which to load the string value from.
2738 * pszValue [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2739 * pszBuffer [O] Buffer to store the localized string in.
2740 * cbBuffer [I] Size of the destination buffer in bytes.
2741 * pcbData [O] Number of bytes written to pszBuffer (optional, may be NULL).
2742 * dwFlags [I] None supported yet.
2743 * pszBaseDir [I] Not supported yet.
2746 * Success: ERROR_SUCCESS,
2747 * Failure: nonzero error code from winerror.h
2750 * This is an API of Windows Vista, which wasn't available at the time this code
2751 * was written. We have to check for the correct behaviour once it's available.
2753 LSTATUS WINAPI
RegLoadMUIStringW(HKEY hKey
, LPCWSTR pwszValue
, LPWSTR pwszBuffer
, DWORD cbBuffer
,
2754 LPDWORD pcbData
, DWORD dwFlags
, LPCWSTR pwszBaseDir
)
2756 DWORD dwValueType
, cbData
;
2757 LPWSTR pwszTempBuffer
= NULL
, pwszExpandedBuffer
= NULL
;
2760 TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %d, pcbData = %p, "
2761 "dwFlags = %d, pwszBaseDir = %s)\n", hKey
, debugstr_w(pwszValue
), pwszBuffer
,
2762 cbBuffer
, pcbData
, dwFlags
, debugstr_w(pwszBaseDir
));
2764 /* Parameter sanity checks. */
2765 if (!hKey
|| !pwszBuffer
)
2766 return ERROR_INVALID_PARAMETER
;
2768 if (pwszBaseDir
&& *pwszBaseDir
) {
2769 FIXME("BaseDir parameter not yet supported!\n");
2770 return ERROR_INVALID_PARAMETER
;
2773 /* Check for value existence and correctness of its type, allocate a buffer and load it. */
2774 result
= RegQueryValueExW(hKey
, pwszValue
, NULL
, &dwValueType
, NULL
, &cbData
);
2775 if (result
!= ERROR_SUCCESS
) goto cleanup
;
2776 if (!(dwValueType
== REG_SZ
|| dwValueType
== REG_EXPAND_SZ
) || !cbData
) {
2777 result
= ERROR_FILE_NOT_FOUND
;
2780 pwszTempBuffer
= heap_alloc(cbData
);
2781 if (!pwszTempBuffer
) {
2782 result
= ERROR_NOT_ENOUGH_MEMORY
;
2785 result
= RegQueryValueExW(hKey
, pwszValue
, NULL
, &dwValueType
, (LPBYTE
)pwszTempBuffer
, &cbData
);
2786 if (result
!= ERROR_SUCCESS
) goto cleanup
;
2788 /* Expand environment variables, if appropriate, or copy the original string over. */
2789 if (dwValueType
== REG_EXPAND_SZ
) {
2790 cbData
= ExpandEnvironmentStringsW(pwszTempBuffer
, NULL
, 0) * sizeof(WCHAR
);
2791 if (!cbData
) goto cleanup
;
2792 pwszExpandedBuffer
= heap_alloc(cbData
);
2793 if (!pwszExpandedBuffer
) {
2794 result
= ERROR_NOT_ENOUGH_MEMORY
;
2797 ExpandEnvironmentStringsW(pwszTempBuffer
, pwszExpandedBuffer
, cbData
);
2799 pwszExpandedBuffer
= heap_alloc(cbData
);
2800 memcpy(pwszExpandedBuffer
, pwszTempBuffer
, cbData
);
2803 /* If the value references a resource based string, parse the value and load the string.
2804 * Else just copy over the original value. */
2805 result
= ERROR_SUCCESS
;
2806 if (*pwszExpandedBuffer
!= '@') { /* '@' is the prefix for resource based string entries. */
2807 lstrcpynW(pwszBuffer
, pwszExpandedBuffer
, cbBuffer
/ sizeof(WCHAR
));
2809 WCHAR
*pComma
= strrchrW(pwszExpandedBuffer
, ',');
2813 /* Format of the expanded value is 'path_to_dll,-resId' */
2814 if (!pComma
|| pComma
[1] != '-') {
2815 result
= ERROR_BADKEY
;
2819 uiStringId
= atoiW(pComma
+2);
2822 hModule
= LoadLibraryW(pwszExpandedBuffer
+ 1);
2823 if (!hModule
|| !load_string(hModule
, uiStringId
, pwszBuffer
, cbBuffer
/sizeof(WCHAR
)))
2824 result
= ERROR_BADKEY
;
2825 FreeLibrary(hModule
);
2829 heap_free(pwszTempBuffer
);
2830 heap_free(pwszExpandedBuffer
);
2834 /******************************************************************************
2835 * RegLoadMUIStringA [ADVAPI32.@]
2837 * See RegLoadMUIStringW
2839 LSTATUS WINAPI
RegLoadMUIStringA(HKEY hKey
, LPCSTR pszValue
, LPSTR pszBuffer
, DWORD cbBuffer
,
2840 LPDWORD pcbData
, DWORD dwFlags
, LPCSTR pszBaseDir
)
2842 UNICODE_STRING valueW
, baseDirW
;
2844 DWORD cbData
= cbBuffer
* sizeof(WCHAR
);
2847 valueW
.Buffer
= baseDirW
.Buffer
= pwszBuffer
= NULL
;
2848 if (!RtlCreateUnicodeStringFromAsciiz(&valueW
, pszValue
) ||
2849 !RtlCreateUnicodeStringFromAsciiz(&baseDirW
, pszBaseDir
) ||
2850 !(pwszBuffer
= heap_alloc(cbData
)))
2852 result
= ERROR_NOT_ENOUGH_MEMORY
;
2856 result
= RegLoadMUIStringW(hKey
, valueW
.Buffer
, pwszBuffer
, cbData
, NULL
, dwFlags
,
2859 if (result
== ERROR_SUCCESS
) {
2860 cbData
= WideCharToMultiByte(CP_ACP
, 0, pwszBuffer
, -1, pszBuffer
, cbBuffer
, NULL
, NULL
);
2866 heap_free(pwszBuffer
);
2867 RtlFreeUnicodeString(&baseDirW
);
2868 RtlFreeUnicodeString(&valueW
);
2873 /******************************************************************************
2874 * RegDisablePredefinedCache [ADVAPI32.@]
2876 * Disables the caching of the HKEY_CLASSES_ROOT key for the process.
2882 * Success: ERROR_SUCCESS
2883 * Failure: nonzero error code from Winerror.h
2886 * This is useful for services that use impersonation.
2888 LSTATUS WINAPI
RegDisablePredefinedCache(void)
2890 HKEY hkey_current_user
;
2891 int idx
= HandleToUlong(HKEY_CURRENT_USER
) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST
);
2893 /* prevent caching of future requests */
2894 hkcu_cache_disabled
= TRUE
;
2896 hkey_current_user
= InterlockedExchangePointer( (void **)&special_root_keys
[idx
], NULL
);
2898 if (hkey_current_user
)
2899 NtClose( hkey_current_user
);
2901 return ERROR_SUCCESS
;
2904 /******************************************************************************
2905 * RegDeleteTreeW [ADVAPI32.@]
2908 LSTATUS WINAPI
RegDeleteTreeW(HKEY hKey
, LPCWSTR lpszSubKey
)
2911 DWORD dwMaxSubkeyLen
, dwMaxValueLen
;
2912 DWORD dwMaxLen
, dwSize
;
2913 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
2914 HKEY hSubKey
= hKey
;
2916 TRACE("(hkey=%p,%p %s)\n", hKey
, lpszSubKey
, debugstr_w(lpszSubKey
));
2920 ret
= RegOpenKeyExW(hKey
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
2921 if (ret
) return ret
;
2924 /* Get highest length for keys, values */
2925 ret
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, NULL
,
2926 &dwMaxSubkeyLen
, NULL
, NULL
, &dwMaxValueLen
, NULL
, NULL
, NULL
);
2927 if (ret
) goto cleanup
;
2931 dwMaxLen
= max(dwMaxSubkeyLen
, dwMaxValueLen
);
2932 if (dwMaxLen
> sizeof(szNameBuf
)/sizeof(WCHAR
))
2934 /* Name too big: alloc a buffer for it */
2935 if (!(lpszName
= heap_alloc( dwMaxLen
*sizeof(WCHAR
))))
2937 ret
= ERROR_NOT_ENOUGH_MEMORY
;
2943 /* Recursively delete all the subkeys */
2947 if (RegEnumKeyExW(hSubKey
, 0, lpszName
, &dwSize
, NULL
,
2948 NULL
, NULL
, NULL
)) break;
2950 ret
= RegDeleteTreeW(hSubKey
, lpszName
);
2951 if (ret
) goto cleanup
;
2955 ret
= RegDeleteKeyW(hKey
, lpszSubKey
);
2960 if (RegEnumValueW(hKey
, 0, lpszName
, &dwSize
,
2961 NULL
, NULL
, NULL
, NULL
)) break;
2963 ret
= RegDeleteValueW(hKey
, lpszName
);
2964 if (ret
) goto cleanup
;
2968 /* Free buffer if allocated */
2969 if (lpszName
!= szNameBuf
)
2970 heap_free( lpszName
);
2972 RegCloseKey(hSubKey
);
2976 /******************************************************************************
2977 * RegDeleteTreeA [ADVAPI32.@]
2980 LSTATUS WINAPI
RegDeleteTreeA(HKEY hKey
, LPCSTR lpszSubKey
)
2983 UNICODE_STRING lpszSubKeyW
;
2985 if (lpszSubKey
) RtlCreateUnicodeStringFromAsciiz( &lpszSubKeyW
, lpszSubKey
);
2986 else lpszSubKeyW
.Buffer
= NULL
;
2987 ret
= RegDeleteTreeW( hKey
, lpszSubKeyW
.Buffer
);
2988 RtlFreeUnicodeString( &lpszSubKeyW
);
2992 /******************************************************************************
2993 * RegDisableReflectionKey [ADVAPI32.@]
2996 LONG WINAPI
RegDisableReflectionKey(HKEY base
)
2998 FIXME("%p: stub\n", base
);
2999 return ERROR_SUCCESS
;