mf/tests: Test input type for WMA decoder DMO.
[wine.git] / dlls / kernelbase / registry.c
bloba767d993254985dfe0e0a2b3c19825b232679479
1 /*
2 * Registry management
4 * Copyright 1996 Marcus Meissner
5 * Copyright 1998 Matthew Becker
6 * Copyright 1999 Sylvain St-Germain
7 * Copyright 1999 Alexandre Julliard
8 * Copyright 2017 Dmitry Timoshkov
9 * Copyright 2019 Nikolay Sivov for CodeWeavers
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
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "winerror.h"
36 #include "winternl.h"
37 #include "winperf.h"
38 #include "winuser.h"
39 #include "shlwapi.h"
40 #include "sddl.h"
42 #include "kernelbase.h"
43 #include "wine/debug.h"
44 #include "wine/exception.h"
45 #include "wine/heap.h"
46 #include "wine/list.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(reg);
50 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
51 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
53 static const WCHAR * const root_key_names[] =
55 L"\\Registry\\Machine\\Software\\Classes",
56 NULL, /* HKEY_CURRENT_USER is determined dynamically */
57 L"\\Registry\\Machine",
58 L"\\Registry\\User",
59 NULL, /* HKEY_PERFORMANCE_DATA is not a real key */
60 L"\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles\\Current",
61 L"\\Registry\\DynData"
64 static HKEY special_root_keys[ARRAY_SIZE(root_key_names)];
65 static BOOL cache_disabled[ARRAY_SIZE(root_key_names)];
67 static CRITICAL_SECTION reg_mui_cs;
68 static CRITICAL_SECTION_DEBUG reg_mui_cs_debug =
70 0, 0, &reg_mui_cs,
71 { &reg_mui_cs_debug.ProcessLocksList,
72 &reg_mui_cs_debug.ProcessLocksList },
73 0, 0, { (DWORD_PTR)(__FILE__ ": reg_mui_cs") }
75 static CRITICAL_SECTION reg_mui_cs = { &reg_mui_cs_debug, -1, 0, 0, 0, 0 };
76 struct mui_cache_entry {
77 struct list entry;
78 WCHAR *file_name; /* full path name */
79 DWORD index;
80 LCID locale;
81 WCHAR *text;
83 static struct list reg_mui_cache = LIST_INIT(reg_mui_cache); /* MRU */
84 static unsigned int reg_mui_cache_count;
85 #define REG_MUI_CACHE_SIZE 8
87 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
89 /* check if value type needs string conversion (Ansi<->Unicode) */
90 static inline BOOL is_string( DWORD type )
92 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
95 /* check if current version is NT or Win95 */
96 static inline BOOL is_version_nt(void)
98 return !(GetVersion() & 0x80000000);
101 static BOOL is_wow6432node( const UNICODE_STRING *name )
103 DWORD len = name->Length / sizeof(WCHAR);
104 return (len >= 11 && !wcsnicmp( name->Buffer, L"Wow6432Node\\", min( len, 12 ) ));
107 static BOOL is_classes_root( const UNICODE_STRING *name )
109 static const WCHAR classes_root[] = L"\\Registry\\Machine\\Software\\Classes\\";
110 DWORD classes_root_len = ARRAY_SIZE( classes_root ) - 1;
111 DWORD len = name->Length / sizeof(WCHAR);
112 return (len >= classes_root_len - 1 && !wcsnicmp( name->Buffer, classes_root, min( len, classes_root_len ) ));
115 static BOOL is_classes_wow6432node( HKEY key )
117 char buffer[256], *buf_ptr = buffer;
118 KEY_NAME_INFORMATION *info = (KEY_NAME_INFORMATION *)buffer;
119 DWORD len = sizeof(buffer);
120 UNICODE_STRING name;
121 NTSTATUS status;
122 BOOL ret = FALSE;
124 /* Obtain the name of the root key */
125 status = NtQueryKey( key, KeyNameInformation, info, len, &len );
126 if (status && status != STATUS_BUFFER_OVERFLOW) return FALSE;
128 /* Retry with a dynamically allocated buffer */
129 while (status == STATUS_BUFFER_OVERFLOW)
131 if (buf_ptr != buffer) heap_free( buf_ptr );
132 if (!(buf_ptr = heap_alloc( len ))) return FALSE;
133 info = (KEY_NAME_INFORMATION *)buf_ptr;
134 status = NtQueryKey( key, KeyNameInformation, info, len, &len );
137 /* Check if the key ends in Wow6432Node and if the root is the Classes key*/
138 if (!status && info->NameLength / sizeof(WCHAR) >= 11)
140 name.Buffer = info->Name + info->NameLength / sizeof(WCHAR) - 11;
141 name.Length = 11 * sizeof(WCHAR);
142 if (is_wow6432node( &name ))
144 name.Buffer = info->Name;
145 name.Length = info->NameLength;
146 ret = is_classes_root( &name );
150 if (buf_ptr != buffer) heap_free( buf_ptr );
152 return ret;
155 /* Open the Wow6432Node subkey of the specified key */
156 static HANDLE open_wow6432node( HANDLE key )
158 UNICODE_STRING nameW = RTL_CONSTANT_STRING( L"Wow6432Node" );
159 OBJECT_ATTRIBUTES attr;
160 HANDLE ret;
162 attr.Length = sizeof(attr);
163 attr.RootDirectory = key;
164 attr.ObjectName = &nameW;
165 attr.Attributes = 0;
166 attr.SecurityDescriptor = NULL;
167 attr.SecurityQualityOfService = NULL;
168 if (NtOpenKeyEx( &ret, MAXIMUM_ALLOWED, &attr, 0 )) return key;
169 return ret;
172 /* Open HKCR, which should already exist because it's used when we're in its Wow6432Node child */
173 static HANDLE open_classes_root( void )
175 OBJECT_ATTRIBUTES attr;
176 UNICODE_STRING nameW;
177 HANDLE ret = 0;
179 attr.Length = sizeof(attr);
180 attr.RootDirectory = 0;
181 attr.ObjectName = &nameW;
182 attr.Attributes = 0;
183 attr.SecurityDescriptor = NULL;
184 attr.SecurityQualityOfService = NULL;
185 RtlInitUnicodeString( &nameW, root_key_names[0] );
186 NtOpenKeyEx( &ret, MAXIMUM_ALLOWED, &attr, 0 );
187 return ret;
190 static HKEY get_perflib_key( HANDLE key )
192 static const WCHAR performance_text[] =
193 L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009";
194 char buffer[200];
195 OBJECT_NAME_INFORMATION *info = (OBJECT_NAME_INFORMATION *)buffer;
197 if (!NtQueryObject( key, ObjectNameInformation, buffer, sizeof(buffer), NULL ))
199 if (!wcsicmp( info->Name.Buffer, performance_text ))
201 NtClose( key );
202 return HKEY_PERFORMANCE_TEXT;
206 return key;
209 static NTSTATUS open_key( HKEY *retkey, HKEY root, UNICODE_STRING *name, DWORD options, ACCESS_MASK access, BOOL create );
211 static NTSTATUS open_subkey( HKEY *subkey, HKEY root, UNICODE_STRING *name, DWORD options, ACCESS_MASK access )
213 BOOL is_wow64_key = (is_win64 && (access & KEY_WOW64_32KEY)) || (is_wow64 && !(access & KEY_WOW64_64KEY));
214 ACCESS_MASK access_64 = access & ~KEY_WOW64_32KEY;
215 DWORD i = 0, len = name->Length / sizeof(WCHAR);
216 WCHAR *buffer = name->Buffer;
217 UNICODE_STRING str;
218 NTSTATUS status;
220 if (!root && len > 10 && !wcsnicmp( buffer, L"\\Registry\\", 10 )) i += 10;
221 if (i < len && buffer[i] == '\\') return STATUS_OBJECT_PATH_INVALID;
222 while (i < len && buffer[i] != '\\') i++;
224 str.Buffer = name->Buffer;
225 str.Length = i * sizeof(WCHAR);
227 if (i < len)
228 options &= ~REG_OPTION_OPEN_LINK;
230 status = open_key( subkey, root, &str, options, access_64, FALSE );
231 if (status == STATUS_OBJECT_NAME_NOT_FOUND && root && is_wow64_key)
233 /* Try to open the shared parent if we can't find the key in the Wow6432Node */
234 if (!is_classes_wow6432node( root ))
235 return STATUS_OBJECT_NAME_NOT_FOUND;
237 root = open_classes_root();
238 status = open_key( subkey, root, &str, options, access_64, FALSE );
240 if (!status)
241 NtClose( root );
242 else
243 *subkey = root;
246 if (!status)
248 while (i < len && buffer[i] == '\\') i++;
250 name->Buffer += i;
251 name->Length -= i * sizeof(WCHAR);
253 if (is_wow64_key && !is_wow6432node( name ))
255 HKEY wow6432node = open_wow6432node( *subkey );
256 if (wow6432node != *subkey)
258 NtClose( *subkey );
259 *subkey = wow6432node;
264 return status;
267 static NTSTATUS open_wow6432node_parent( HKEY *retkey, HKEY root, DWORD options, ACCESS_MASK access )
269 char buffer[256], *buf_ptr = buffer;
270 KEY_NAME_INFORMATION *info = (KEY_NAME_INFORMATION *)buffer;
271 DWORD len = sizeof(buffer);
272 UNICODE_STRING name;
273 NTSTATUS status;
275 /* Obtain the name of the root key */
276 status = NtQueryKey( root, KeyNameInformation, info, len, &len );
277 if (status && status != STATUS_BUFFER_OVERFLOW) return status;
279 /* Retry with a dynamically allocated buffer */
280 while (status == STATUS_BUFFER_OVERFLOW)
282 if (buf_ptr != buffer) heap_free( buf_ptr );
283 if (!(buf_ptr = heap_alloc( len )))
284 return STATUS_NO_MEMORY;
285 info = (KEY_NAME_INFORMATION *)buf_ptr;
286 status = NtQueryKey( root, KeyNameInformation, info, len, &len );
289 if (status)
291 if (buf_ptr != buffer) heap_free( buf_ptr );
292 return status;
295 name.Buffer = info->Name;
296 name.Length = info->NameLength;
297 root = 0;
299 /* Obtain the parent Wow6432Node if it exists */
300 while (!status && name.Length)
302 status = open_subkey( retkey, root, &name, options & ~REG_OPTION_OPEN_LINK, access );
303 if (root) NtClose( root );
304 root = *retkey;
307 if (buf_ptr != buffer) heap_free( buf_ptr );
309 return status;
312 /* wrapper for NtOpenKeyEx to handle Wow6432 nodes */
313 static NTSTATUS open_key( HKEY *retkey, HKEY root, UNICODE_STRING *name, DWORD options, ACCESS_MASK access, BOOL create )
315 BOOL is_wow64_key = (is_win64 && (access & KEY_WOW64_32KEY)) || (is_wow64 && !(access & KEY_WOW64_64KEY));
316 HKEY subkey = 0, subkey_root = root;
317 NTSTATUS status = STATUS_SUCCESS;
318 BOOL was_wow6432node = TRUE;
320 *retkey = NULL;
322 if (!(is_win64 && (access & KEY_WOW64_32KEY)) && !create)
324 OBJECT_ATTRIBUTES attr;
326 attr.Length = sizeof(attr);
327 attr.RootDirectory = root;
328 attr.ObjectName = name;
329 attr.Attributes = 0;
330 attr.SecurityDescriptor = NULL;
331 attr.SecurityQualityOfService = NULL;
333 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
334 status = NtOpenKeyEx( (HANDLE *)retkey, access, &attr, options );
335 if (status == STATUS_PREDEFINED_HANDLE)
337 *retkey = get_perflib_key( *retkey );
338 status = STATUS_SUCCESS;
340 return status;
343 if (root && (access & KEY_WOW64_32KEY) && !is_wow6432node( name ))
344 status = open_wow6432node_parent( &subkey_root, root, options, access );
345 else if (root && is_wow64 && !(access & KEY_WOW64_64KEY) && !is_wow6432node( name ))
347 subkey_root = open_wow6432node( root );
348 if (!is_classes_wow6432node( subkey_root ) && subkey_root != root)
350 NtClose( subkey_root );
351 subkey_root = root;
355 while (!status && (name->Length || !subkey))
357 was_wow6432node = is_wow6432node( name );
358 status = open_subkey( &subkey, subkey_root, name, options, access );
359 if (subkey && subkey_root && subkey_root != root) NtClose( subkey_root );
360 if (subkey) subkey_root = subkey;
363 /* Return the shared parent if we didn't explicitly look for the Wow6432Node */
364 if (!status && !was_wow6432node && is_wow64_key && is_classes_wow6432node( subkey_root ))
366 if (subkey_root && subkey_root != root) NtClose( subkey_root );
367 subkey_root = open_classes_root();
370 if (!status || (status == STATUS_OBJECT_NAME_NOT_FOUND && create))
371 *retkey = subkey_root;
372 else if (subkey_root && subkey_root != root)
373 NtClose( subkey_root );
375 return status;
378 static NTSTATUS create_key( HKEY *retkey, HKEY root, UNICODE_STRING name, ULONG options, ACCESS_MASK access,
379 const UNICODE_STRING *class, PULONG dispos );
381 static NTSTATUS create_subkey( HKEY *subkey, HKEY root, UNICODE_STRING *name, DWORD options, ACCESS_MASK access,
382 const UNICODE_STRING *class, PULONG dispos )
384 ACCESS_MASK access_64 = access & ~KEY_WOW64_32KEY;
385 DWORD i = 0, len = name->Length / sizeof(WCHAR);
386 WCHAR *buffer = name->Buffer;
387 UNICODE_STRING str;
388 NTSTATUS status;
390 if (i < len && buffer[i] == '\\') return STATUS_OBJECT_PATH_INVALID;
391 while (i < len && buffer[i] != '\\') i++;
393 str.Buffer = name->Buffer;
394 str.Length = i * sizeof(WCHAR);
396 if (i < len)
397 options &= ~REG_OPTION_CREATE_LINK;
399 status = create_key( subkey, root, str, options, access_64, class, dispos );
400 if (!status)
402 while (i < len && buffer[i] == '\\') i++;
404 name->Buffer += i;
405 name->Length -= i * sizeof(WCHAR);
408 return status;
411 /* wrapper for NtCreateKey that creates the key recursively if necessary */
412 static NTSTATUS create_key( HKEY *retkey, HKEY root, UNICODE_STRING name, ULONG options, ACCESS_MASK access,
413 const UNICODE_STRING *class, PULONG dispos )
415 NTSTATUS status = STATUS_OBJECT_NAME_NOT_FOUND;
416 HKEY subkey, subkey_root = root;
418 *retkey = NULL;
420 if (!(is_win64 && (access & KEY_WOW64_32KEY)))
422 OBJECT_ATTRIBUTES attr;
424 attr.Length = sizeof(attr);
425 attr.RootDirectory = root;
426 attr.ObjectName = &name;
427 attr.Attributes = 0;
428 attr.SecurityDescriptor = NULL;
429 attr.SecurityQualityOfService = NULL;
430 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
432 status = NtCreateKey( (HANDLE *)retkey, access, &attr, 0, class, options, dispos );
433 if (status == STATUS_PREDEFINED_HANDLE)
435 *retkey = get_perflib_key( *retkey );
436 status = STATUS_SUCCESS;
439 if (!status || status != STATUS_OBJECT_NAME_NOT_FOUND)
440 return status;
443 status = open_key( &subkey_root, root, &name, options & REG_OPTION_OPEN_LINK, access, TRUE );
444 if (!status && (options & REG_OPTION_CREATE_LINK))
446 NtClose( subkey_root );
447 status = STATUS_OBJECT_NAME_COLLISION;
450 if (!status)
451 if (dispos) *dispos = REG_OPENED_EXISTING_KEY;
453 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
455 status = STATUS_SUCCESS;
456 while (!status && name.Length)
458 status = create_subkey( &subkey, subkey_root, &name, options, access, class, dispos );
459 if (subkey_root && subkey_root != root) NtClose( subkey_root );
460 subkey_root = subkey;
464 if (!status)
465 *retkey = subkey_root;
467 return status;
470 /* create one of the HKEY_* special root keys */
471 static HKEY create_special_root_hkey( HKEY hkey, DWORD access )
473 HKEY ret = 0;
474 int idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
476 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CURRENT_USER))
478 if (RtlOpenCurrentUser( access, (HANDLE *)&hkey )) return 0;
479 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
481 else
483 UNICODE_STRING name;
485 RtlInitUnicodeString( &name, root_key_names[idx] );
486 if (create_key( &hkey, 0, name, 0, access, NULL, NULL )) return 0;
487 TRACE( "%s -> %p\n", debugstr_w(name.Buffer), hkey );
490 if (!cache_disabled[idx])
492 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
493 ret = hkey;
494 else
495 NtClose( hkey ); /* somebody beat us to it */
497 else
498 ret = hkey;
499 return ret;
502 /* map the hkey from special root to normal key if necessary */
503 static inline HKEY get_special_root_hkey( HKEY hkey )
505 unsigned int index = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
507 switch (HandleToUlong(hkey))
509 case (LONG)(LONG_PTR)HKEY_CLASSES_ROOT:
510 case (LONG)(LONG_PTR)HKEY_CURRENT_USER:
511 case (LONG)(LONG_PTR)HKEY_LOCAL_MACHINE:
512 case (LONG)(LONG_PTR)HKEY_USERS:
513 case (LONG)(LONG_PTR)HKEY_CURRENT_CONFIG:
514 case (LONG)(LONG_PTR)HKEY_DYN_DATA:
515 if (special_root_keys[index])
516 return special_root_keys[index];
517 return create_special_root_hkey( hkey, MAXIMUM_ALLOWED );
519 default:
520 return hkey;
524 static BOOL is_perf_key( HKEY key )
526 return HandleToUlong(key) == HandleToUlong(HKEY_PERFORMANCE_DATA)
527 || HandleToUlong(key) == HandleToUlong(HKEY_PERFORMANCE_TEXT)
528 || HandleToUlong(key) == HandleToUlong(HKEY_PERFORMANCE_NLSTEXT);
532 /******************************************************************************
533 * RemapPredefinedHandleInternal (kernelbase.@)
535 NTSTATUS WINAPI RemapPredefinedHandleInternal( HKEY hkey, HKEY override )
537 HKEY old_key;
538 int idx;
540 TRACE("(%p %p)\n", hkey, override);
542 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
543 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
544 return STATUS_INVALID_HANDLE;
545 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
547 if (override)
549 NTSTATUS status = NtDuplicateObject( GetCurrentProcess(), override,
550 GetCurrentProcess(), (HANDLE *)&override,
551 0, 0, DUPLICATE_SAME_ACCESS );
552 if (status) return status;
555 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], override );
556 if (old_key) NtClose( old_key );
557 return STATUS_SUCCESS;
561 /******************************************************************************
562 * DisablePredefinedHandleTableInternal (kernelbase.@)
564 NTSTATUS WINAPI DisablePredefinedHandleTableInternal( HKEY hkey )
566 HKEY old_key;
567 int idx;
569 TRACE("(%p)\n", hkey);
571 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
572 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
573 return STATUS_INVALID_HANDLE;
574 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
576 cache_disabled[idx] = TRUE;
578 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], NULL );
579 if (old_key) NtClose( old_key );
580 return STATUS_SUCCESS;
584 /******************************************************************************
585 * RegCreateKeyExW (kernelbase.@)
587 * See RegCreateKeyExA.
589 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
590 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
591 PHKEY retkey, LPDWORD dispos )
593 UNICODE_STRING nameW, classW;
595 if (reserved) return ERROR_INVALID_PARAMETER;
596 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
598 RtlInitUnicodeString( &nameW, name );
599 RtlInitUnicodeString( &classW, class );
601 return RtlNtStatusToDosError( create_key( retkey, hkey, nameW, options, access, &classW, dispos ) );
605 /******************************************************************************
606 * RegCreateKeyExA (kernelbase.@)
608 * Open a registry key, creating it if it doesn't exist.
610 * PARAMS
611 * hkey [I] Handle of the parent registry key
612 * name [I] Name of the new key to open or create
613 * reserved [I] Reserved, pass 0
614 * class [I] The object type of the new key
615 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
616 * access [I] Access level desired
617 * sa [I] Security attributes for the key
618 * retkey [O] Destination for the resulting handle
619 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
621 * RETURNS
622 * Success: ERROR_SUCCESS.
623 * Failure: A standard Win32 error code. retkey remains untouched.
625 * FIXME
626 * MAXIMUM_ALLOWED in access mask not supported by server
628 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
629 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
630 PHKEY retkey, LPDWORD dispos )
632 UNICODE_STRING classW;
633 ANSI_STRING nameA, classA;
634 NTSTATUS status;
636 if (reserved) return ERROR_INVALID_PARAMETER;
637 if (!is_version_nt())
639 access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
640 if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
642 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
644 RtlInitAnsiString( &nameA, name );
645 RtlInitAnsiString( &classA, class );
647 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
648 &nameA, FALSE )))
650 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
652 status = create_key( retkey, hkey, NtCurrentTeb()->StaticUnicodeString, options, access, &classW, dispos );
653 RtlFreeUnicodeString( &classW );
656 return RtlNtStatusToDosError( status );
660 /******************************************************************************
661 * RegOpenKeyExW (kernelbase.@)
663 * See RegOpenKeyExA.
665 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD options, REGSAM access, PHKEY retkey )
667 UNICODE_STRING nameW;
669 if (retkey && (!name || !name[0]) &&
670 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
671 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
673 *retkey = hkey;
674 return ERROR_SUCCESS;
677 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
678 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
680 if (!retkey) return ERROR_INVALID_PARAMETER;
681 *retkey = NULL;
682 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
684 RtlInitUnicodeString( &nameW, name );
685 return RtlNtStatusToDosError( open_key( retkey, hkey, &nameW, options, access, FALSE ) );
689 /******************************************************************************
690 * RegOpenKeyExA (kernelbase.@)
692 * Open a registry key.
694 * PARAMS
695 * hkey [I] Handle of open key
696 * name [I] Name of subkey to open
697 * options [I] Open options (can be set to REG_OPTION_OPEN_LINK)
698 * access [I] Security access mask
699 * retkey [O] Handle to open key
701 * RETURNS
702 * Success: ERROR_SUCCESS
703 * Failure: A standard Win32 error code. retkey is set to 0.
705 * NOTES
706 * Unlike RegCreateKeyExA(), this function will not create the key if it
707 * does not exist.
709 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD options, REGSAM access, PHKEY retkey )
711 STRING nameA;
712 NTSTATUS status;
714 if (retkey && (!name || !name[0]) &&
715 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
716 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
718 *retkey = hkey;
719 return ERROR_SUCCESS;
722 if (!is_version_nt()) access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
723 else
725 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
726 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
729 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
731 RtlInitAnsiString( &nameA, name );
732 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
733 &nameA, FALSE )))
735 UNICODE_STRING nameW = NtCurrentTeb()->StaticUnicodeString;
736 status = open_key( retkey, hkey, &nameW, options, access, FALSE );
738 return RtlNtStatusToDosError( status );
742 /******************************************************************************
743 * RegOpenCurrentUser (kernelbase.@)
745 * Get a handle to the HKEY_CURRENT_USER key for the user
746 * the current thread is impersonating.
748 * PARAMS
749 * access [I] Desired access rights to the key
750 * retkey [O] Handle to the opened key
752 * RETURNS
753 * Success: ERROR_SUCCESS
754 * Failure: nonzero error code from Winerror.h
756 * FIXME
757 * This function is supposed to retrieve a handle to the
758 * HKEY_CURRENT_USER for the user the current thread is impersonating.
759 * Since Wine does not currently allow threads to impersonate other users,
760 * this stub should work fine.
762 LSTATUS WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
764 void *data[20];
765 TOKEN_USER *info = (TOKEN_USER *)data;
766 HANDLE token;
767 DWORD len = 0;
769 /* get current user SID */
770 if (OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, FALSE, &token ))
772 len = sizeof(data);
773 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
774 CloseHandle( token );
776 if (!len)
778 ImpersonateSelf(SecurityIdentification);
779 if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &token))
781 len = sizeof(data);
782 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
783 CloseHandle( token );
785 RevertToSelf();
788 if (len)
790 WCHAR buffer[200];
791 UNICODE_STRING string = { 0, sizeof(buffer), buffer };
793 RtlConvertSidToUnicodeString( &string, info->User.Sid, FALSE );
794 return RegOpenKeyExW( HKEY_USERS, string.Buffer, 0, access, retkey );
797 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
802 /******************************************************************************
803 * RegEnumKeyExW (kernelbase.@)
805 * Enumerate subkeys of the specified open registry key.
807 * PARAMS
808 * hkey [I] Handle to key to enumerate
809 * index [I] Index of subkey to enumerate
810 * name [O] Buffer for subkey name
811 * name_len [O] Size of subkey buffer
812 * reserved [I] Reserved
813 * class [O] Buffer for class string
814 * class_len [O] Size of class buffer
815 * ft [O] Time key last written to
817 * RETURNS
818 * Success: ERROR_SUCCESS
819 * Failure: System error code. If there are no more subkeys available, the
820 * function returns ERROR_NO_MORE_ITEMS.
822 LSTATUS WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
823 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
825 NTSTATUS status;
826 char buffer[256], *buf_ptr = buffer;
827 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
828 DWORD total_size;
830 TRACE( "(%p,%ld,%p,%p(%lu),%p,%p,%p,%p)\n", hkey, index, name, name_len,
831 name_len ? *name_len : 0, reserved, class, class_len, ft );
833 if (reserved) return ERROR_INVALID_PARAMETER;
834 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
836 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
837 buffer, sizeof(buffer), &total_size );
839 while (status == STATUS_BUFFER_OVERFLOW)
841 /* retry with a dynamically allocated buffer */
842 if (buf_ptr != buffer) heap_free( buf_ptr );
843 if (!(buf_ptr = heap_alloc( total_size )))
844 return ERROR_NOT_ENOUGH_MEMORY;
845 info = (KEY_NODE_INFORMATION *)buf_ptr;
846 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
847 buf_ptr, total_size, &total_size );
850 if (!status)
852 DWORD len = info->NameLength / sizeof(WCHAR);
853 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
855 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
857 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
858 status = STATUS_BUFFER_OVERFLOW;
859 else
861 *name_len = len;
862 memcpy( name, info->Name, info->NameLength );
863 name[len] = 0;
864 if (class_len)
866 *class_len = cls_len;
867 if (class)
869 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
870 class[cls_len] = 0;
876 if (buf_ptr != buffer) heap_free( buf_ptr );
877 return RtlNtStatusToDosError( status );
881 /******************************************************************************
882 * RegEnumKeyExA (kernelbase.@)
884 * See RegEnumKeyExW.
886 LSTATUS WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
887 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
889 NTSTATUS status;
890 char buffer[256], *buf_ptr = buffer;
891 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
892 DWORD total_size;
894 TRACE( "(%p,%ld,%p,%p(%lu),%p,%p,%p,%p)\n", hkey, index, name, name_len,
895 name_len ? *name_len : 0, reserved, class, class_len, ft );
897 if (reserved) return ERROR_INVALID_PARAMETER;
898 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
900 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
901 buffer, sizeof(buffer), &total_size );
903 while (status == STATUS_BUFFER_OVERFLOW)
905 /* retry with a dynamically allocated buffer */
906 if (buf_ptr != buffer) heap_free( buf_ptr );
907 if (!(buf_ptr = heap_alloc( total_size )))
908 return ERROR_NOT_ENOUGH_MEMORY;
909 info = (KEY_NODE_INFORMATION *)buf_ptr;
910 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
911 buf_ptr, total_size, &total_size );
914 if (!status)
916 DWORD len, cls_len;
918 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
919 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
920 info->ClassLength );
921 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
923 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
924 status = STATUS_BUFFER_OVERFLOW;
925 else
927 *name_len = len;
928 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
929 name[len] = 0;
930 if (class_len)
932 *class_len = cls_len;
933 if (class)
935 RtlUnicodeToMultiByteN( class, cls_len, NULL,
936 (WCHAR *)(buf_ptr + info->ClassOffset),
937 info->ClassLength );
938 class[cls_len] = 0;
944 if (buf_ptr != buffer) heap_free( buf_ptr );
945 return RtlNtStatusToDosError( status );
949 /******************************************************************************
950 * RegQueryInfoKeyW (kernelbase.@)
952 * Retrieves information about the specified registry key.
954 * PARAMS
955 * hkey [I] Handle to key to query
956 * class [O] Buffer for class string
957 * class_len [O] Size of class string buffer
958 * reserved [I] Reserved
959 * subkeys [O] Buffer for number of subkeys
960 * max_subkey [O] Buffer for longest subkey name length
961 * max_class [O] Buffer for longest class string length
962 * values [O] Buffer for number of value entries
963 * max_value [O] Buffer for longest value name length
964 * max_data [O] Buffer for longest value data length
965 * security [O] Buffer for security descriptor length
966 * modif [O] Modification time
968 * RETURNS
969 * Success: ERROR_SUCCESS
970 * Failure: system error code.
972 * NOTES
973 * - win95 allows class to be valid and class_len to be NULL
974 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
975 * - both allow class to be NULL and class_len to be NULL
976 * (it's hard to test validity, so test !NULL instead)
978 LSTATUS WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
979 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
980 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
981 LPDWORD security, FILETIME *modif )
983 NTSTATUS status;
984 char buffer[256], *buf_ptr = buffer;
985 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
986 DWORD total_size;
988 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
989 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
991 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
992 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
994 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
995 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
997 if (class && class_len && *class_len)
999 /* retry with a dynamically allocated buffer */
1000 while (status == STATUS_BUFFER_OVERFLOW)
1002 if (buf_ptr != buffer) heap_free( buf_ptr );
1003 if (!(buf_ptr = heap_alloc( total_size )))
1004 return ERROR_NOT_ENOUGH_MEMORY;
1005 info = (KEY_FULL_INFORMATION *)buf_ptr;
1006 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
1009 if (status) goto done;
1011 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
1013 status = STATUS_BUFFER_TOO_SMALL;
1015 else
1017 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
1018 class[info->ClassLength/sizeof(WCHAR)] = 0;
1021 else status = STATUS_SUCCESS;
1023 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
1024 if (subkeys) *subkeys = info->SubKeys;
1025 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
1026 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
1027 if (values) *values = info->Values;
1028 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
1029 if (max_data) *max_data = info->MaxValueDataLen;
1030 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
1032 if (security)
1034 FIXME( "security argument not supported.\n");
1035 *security = 0;
1038 done:
1039 if (buf_ptr != buffer) heap_free( buf_ptr );
1040 return RtlNtStatusToDosError( status );
1044 /******************************************************************************
1045 * RegQueryInfoKeyA (kernelbase.@)
1047 * Retrieves information about a registry key.
1049 * PARAMS
1050 * hKey [I] Handle to an open key.
1051 * lpClass [O] Class string of the key.
1052 * lpcClass [I/O] size of lpClass.
1053 * lpReserved [I] Reserved; must be NULL.
1054 * lpcSubKeys [O] Number of subkeys contained by the key.
1055 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
1056 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
1057 * class in TCHARS.
1058 * lpcValues [O] Number of values associated with the key.
1059 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
1060 * lpcMaxValueLen [O] Longest data component among the key's values
1061 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
1062 * lpftLastWriteTime [O] FILETIME structure that is the last write time.
1064 * RETURNS
1065 * Success: ERROR_SUCCESS
1066 * Failure: nonzero error code from Winerror.h
1068 LSTATUS WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
1069 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
1070 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
1071 LPDWORD security, FILETIME *modif )
1073 NTSTATUS status;
1074 char buffer[256], *buf_ptr = buffer;
1075 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
1076 DWORD total_size;
1078 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
1079 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
1081 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
1082 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1084 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
1085 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1087 if (class || class_len)
1089 /* retry with a dynamically allocated buffer */
1090 while (status == STATUS_BUFFER_OVERFLOW)
1092 if (buf_ptr != buffer) heap_free( buf_ptr );
1093 if (!(buf_ptr = heap_alloc( total_size )))
1094 return ERROR_NOT_ENOUGH_MEMORY;
1095 info = (KEY_FULL_INFORMATION *)buf_ptr;
1096 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
1099 if (status) goto done;
1101 if (class && class_len && *class_len)
1103 DWORD len = *class_len;
1104 RtlUnicodeToMultiByteN( class, len, class_len,
1105 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
1106 if (*class_len == len)
1108 status = STATUS_BUFFER_OVERFLOW;
1109 *class_len -= 1;
1111 class[*class_len] = 0;
1113 else if (class_len)
1114 RtlUnicodeToMultiByteSize( class_len,
1115 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
1117 else status = STATUS_SUCCESS;
1119 if (subkeys) *subkeys = info->SubKeys;
1120 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
1121 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
1122 if (values) *values = info->Values;
1123 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
1124 if (max_data) *max_data = info->MaxValueDataLen;
1125 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
1127 if (security)
1129 FIXME( "security argument not supported.\n");
1130 *security = 0;
1133 done:
1134 if (buf_ptr != buffer) heap_free( buf_ptr );
1135 return RtlNtStatusToDosError( status );
1138 /******************************************************************************
1139 * RegCloseKey (kernelbase.@)
1141 * Close an open registry key.
1143 * PARAMS
1144 * hkey [I] Handle of key to close
1146 * RETURNS
1147 * Success: ERROR_SUCCESS
1148 * Failure: Error code
1150 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCloseKey( HKEY hkey )
1152 if (!hkey) return ERROR_INVALID_HANDLE;
1153 if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
1154 return RtlNtStatusToDosError( NtClose( hkey ) );
1158 /******************************************************************************
1159 * RegDeleteKeyExW (kernelbase.@)
1161 LSTATUS WINAPI RegDeleteKeyExW( HKEY hkey, LPCWSTR name, REGSAM access, DWORD reserved )
1163 DWORD ret;
1164 HKEY tmp;
1166 if (!name) return ERROR_INVALID_PARAMETER;
1168 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1170 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1171 if (!(ret = RegOpenKeyExW( hkey, name, 0, access | DELETE, &tmp )))
1173 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1174 RegCloseKey( tmp );
1176 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
1177 return ret;
1181 /******************************************************************************
1182 * RegDeleteKeyExA (kernelbase.@)
1184 LSTATUS WINAPI RegDeleteKeyExA( HKEY hkey, LPCSTR name, REGSAM access, DWORD reserved )
1186 DWORD ret;
1187 HKEY tmp;
1189 if (!name) return ERROR_INVALID_PARAMETER;
1191 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1193 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1194 if (!(ret = RegOpenKeyExA( hkey, name, 0, access | DELETE, &tmp )))
1196 if (!is_version_nt()) /* win95 does recursive key deletes */
1198 CHAR sub[MAX_PATH];
1199 DWORD len = sizeof(sub);
1200 while(!RegEnumKeyExA(tmp, 0, sub, &len, NULL, NULL, NULL, NULL))
1202 if(RegDeleteKeyExA(tmp, sub, access, reserved)) /* recurse */
1203 break;
1206 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1207 RegCloseKey( tmp );
1209 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
1210 return ret;
1213 /******************************************************************************
1214 * RegSetValueExW (kernelbase.@)
1216 * Set the data and contents of a registry value.
1218 * PARAMS
1219 * hkey [I] Handle of key to set value for
1220 * name [I] Name of value to set
1221 * reserved [I] Reserved, must be zero
1222 * type [I] Type of the value being set
1223 * data [I] The new contents of the value to set
1224 * count [I] Size of data
1226 * RETURNS
1227 * Success: ERROR_SUCCESS
1228 * Failure: Error code
1230 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
1231 DWORD type, const BYTE *data, DWORD count )
1233 UNICODE_STRING nameW;
1235 /* no need for version check, not implemented on win9x anyway */
1237 if ((data && ((ULONG_PTR)data >> 16) == 0) || (!data && count)) return ERROR_NOACCESS;
1239 if (count && is_string(type))
1241 LPCWSTR str = (LPCWSTR)data;
1242 /* if user forgot to count terminating null, add it (yes NT does this) */
1243 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
1244 count += sizeof(WCHAR);
1246 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1248 RtlInitUnicodeString( &nameW, name );
1249 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
1253 /******************************************************************************
1254 * RegSetValueExA (kernelbase.@)
1256 * See RegSetValueExW.
1258 * NOTES
1259 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1260 * NT does definitely care (aj)
1262 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1263 const BYTE *data, DWORD count )
1265 ANSI_STRING nameA;
1266 UNICODE_STRING nameW;
1267 WCHAR *dataW = NULL;
1268 NTSTATUS status;
1270 if (!is_version_nt()) /* win95 */
1272 if (type == REG_SZ)
1274 if (!data) return ERROR_INVALID_PARAMETER;
1275 count = strlen((const char *)data) + 1;
1278 else if (count && is_string(type))
1280 /* if user forgot to count terminating null, add it (yes NT does this) */
1281 if (data[count-1] && !data[count]) count++;
1284 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1286 if (is_string( type )) /* need to convert to Unicode */
1288 DWORD lenW;
1289 RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1290 if (!(dataW = heap_alloc( lenW ))) return ERROR_OUTOFMEMORY;
1291 RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1292 count = lenW;
1293 data = (BYTE *)dataW;
1296 RtlInitAnsiString( &nameA, name );
1297 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1299 status = NtSetValueKey( hkey, &nameW, 0, type, data, count );
1300 RtlFreeUnicodeString( &nameW );
1302 heap_free( dataW );
1303 return RtlNtStatusToDosError( status );
1307 /******************************************************************************
1308 * RegSetKeyValueW (kernelbase.@)
1310 LONG WINAPI RegSetKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name, DWORD type, const void *data, DWORD len )
1312 HKEY hsubkey = NULL;
1313 DWORD ret;
1315 TRACE("(%p,%s,%s,%ld,%p,%ld)\n", hkey, debugstr_w(subkey), debugstr_w(name), type, data, len );
1317 if (subkey && subkey[0]) /* need to create the subkey */
1319 if ((ret = RegCreateKeyExW( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1320 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1321 hkey = hsubkey;
1324 ret = RegSetValueExW( hkey, name, 0, type, (const BYTE*)data, len );
1325 if (hsubkey) RegCloseKey( hsubkey );
1326 return ret;
1329 /******************************************************************************
1330 * RegSetKeyValueA (kernelbase.@)
1332 LONG WINAPI RegSetKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name, DWORD type, const void *data, DWORD len )
1334 HKEY hsubkey = NULL;
1335 DWORD ret;
1337 TRACE("(%p,%s,%s,%ld,%p,%ld)\n", hkey, debugstr_a(subkey), debugstr_a(name), type, data, len );
1339 if (subkey && subkey[0]) /* need to create the subkey */
1341 if ((ret = RegCreateKeyExA( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1342 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1343 hkey = hsubkey;
1346 ret = RegSetValueExA( hkey, name, 0, type, (const BYTE*)data, len );
1347 if (hsubkey) RegCloseKey( hsubkey );
1348 return ret;
1351 /* FIXME: we should read data from system32/perf009c.dat (or perf###c depending
1352 * on locale) instead */
1353 static DWORD query_perf_names( DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1355 static const WCHAR names[] = L"1\0" "1847\0" "1846\0End Marker\0";
1356 DWORD size = *ret_size;
1358 if (type) *type = REG_MULTI_SZ;
1359 *ret_size = sizeof(names);
1360 if (!unicode) *ret_size /= sizeof(WCHAR);
1362 if (!data) return ERROR_SUCCESS;
1363 if (size < *ret_size) return ERROR_MORE_DATA;
1365 if (unicode)
1366 memcpy( data, names, sizeof(names) );
1367 else
1368 RtlUnicodeToMultiByteN( data, size, NULL, names, sizeof(names) );
1369 return ERROR_SUCCESS;
1372 /* FIXME: we should read data from system32/perf009h.dat (or perf###h depending
1373 * on locale) instead */
1374 static DWORD query_perf_help( DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1376 static const WCHAR names[] = L"1847\0End Marker\0";
1377 DWORD size = *ret_size;
1379 if (type) *type = REG_MULTI_SZ;
1380 *ret_size = sizeof(names);
1381 if (!unicode) *ret_size /= sizeof(WCHAR);
1383 if (!data) return ERROR_SUCCESS;
1384 if (size < *ret_size) return ERROR_MORE_DATA;
1386 if (unicode)
1387 memcpy( data, names, sizeof(names) );
1388 else
1389 RtlUnicodeToMultiByteN( data, size, NULL, names, sizeof(names) );
1390 return ERROR_SUCCESS;
1393 struct perf_provider
1395 HMODULE perflib;
1396 WCHAR linkage[MAX_PATH];
1397 WCHAR objects[MAX_PATH];
1398 PM_OPEN_PROC *pOpen;
1399 PM_CLOSE_PROC *pClose;
1400 PM_COLLECT_PROC *pCollect;
1403 static void *get_provider_entry(HKEY perf, HMODULE perflib, const char *name)
1405 char buf[MAX_PATH];
1406 DWORD err, type, len;
1408 len = sizeof(buf) - 1;
1409 err = RegQueryValueExA(perf, name, NULL, &type, (BYTE *)buf, &len);
1410 if (err != ERROR_SUCCESS || type != REG_SZ)
1411 return NULL;
1413 buf[len] = 0;
1414 TRACE("Loading function pointer for %s: %s\n", name, debugstr_a(buf));
1416 return GetProcAddress(perflib, buf);
1419 static BOOL load_provider(HKEY root, const WCHAR *name, struct perf_provider *provider)
1421 WCHAR buf[MAX_PATH], buf2[MAX_PATH];
1422 DWORD err, type, len;
1423 HKEY service, perf;
1425 err = RegOpenKeyExW(root, name, 0, KEY_READ, &service);
1426 if (err != ERROR_SUCCESS)
1427 return FALSE;
1429 provider->linkage[0] = 0;
1430 err = RegOpenKeyExW(service, L"Linkage", 0, KEY_READ, &perf);
1431 if (err == ERROR_SUCCESS)
1433 len = sizeof(buf) - sizeof(WCHAR);
1434 err = RegQueryValueExW(perf, L"Export", NULL, &type, (BYTE *)buf, &len);
1435 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1437 memcpy(provider->linkage, buf, len);
1438 provider->linkage[len / sizeof(WCHAR)] = 0;
1439 TRACE("Export: %s\n", debugstr_w(provider->linkage));
1441 RegCloseKey(perf);
1444 err = RegOpenKeyExW(service, L"Performance", 0, KEY_READ, &perf);
1445 RegCloseKey(service);
1446 if (err != ERROR_SUCCESS)
1447 return FALSE;
1449 provider->objects[0] = 0;
1450 len = sizeof(buf) - sizeof(WCHAR);
1451 err = RegQueryValueExW(perf, L"Object List", NULL, &type, (BYTE *)buf, &len);
1452 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1454 memcpy(provider->objects, buf, len);
1455 provider->objects[len / sizeof(WCHAR)] = 0;
1456 TRACE("Object List: %s\n", debugstr_w(provider->objects));
1459 len = sizeof(buf) - sizeof(WCHAR);
1460 err = RegQueryValueExW(perf, L"Library", NULL, &type, (BYTE *)buf, &len);
1461 if (err != ERROR_SUCCESS || !(type == REG_SZ || type == REG_EXPAND_SZ))
1462 goto error;
1464 buf[len / sizeof(WCHAR)] = 0;
1465 if (type == REG_EXPAND_SZ)
1467 len = ExpandEnvironmentStringsW(buf, buf2, MAX_PATH);
1468 if (!len || len > MAX_PATH) goto error;
1469 lstrcpyW(buf, buf2);
1472 if (!(provider->perflib = LoadLibraryW(buf)))
1474 WARN("Failed to load %s\n", debugstr_w(buf));
1475 goto error;
1478 GetModuleFileNameW(provider->perflib, buf, MAX_PATH);
1479 TRACE("Loaded provider %s\n", wine_dbgstr_w(buf));
1481 provider->pOpen = get_provider_entry(perf, provider->perflib, "Open");
1482 provider->pClose = get_provider_entry(perf, provider->perflib, "Close");
1483 provider->pCollect = get_provider_entry(perf, provider->perflib, "Collect");
1484 if (provider->pOpen && provider->pClose && provider->pCollect)
1486 RegCloseKey(perf);
1487 return TRUE;
1490 TRACE("Provider is missing required exports\n");
1491 FreeLibrary(provider->perflib);
1493 error:
1494 RegCloseKey(perf);
1495 return FALSE;
1498 static DWORD collect_data(struct perf_provider *provider, const WCHAR *query, void **data, DWORD *size, DWORD *obj_count)
1500 WCHAR *linkage = provider->linkage[0] ? provider->linkage : NULL;
1501 DWORD err;
1503 if (!query || !query[0])
1504 query = L"Global";
1506 err = provider->pOpen(linkage);
1507 if (err != ERROR_SUCCESS)
1509 TRACE("Open(%s) error %lu (%#lx)\n", debugstr_w(linkage), err, err);
1510 return err;
1513 *obj_count = 0;
1514 err = provider->pCollect((WCHAR *)query, data, size, obj_count);
1515 if (err != ERROR_SUCCESS)
1517 TRACE("Collect error %lu (%#lx)\n", err, err);
1518 *obj_count = 0;
1521 provider->pClose();
1522 return err;
1525 #define MAX_SERVICE_NAME 260
1527 static DWORD query_perf_data( const WCHAR *query, DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1529 DWORD err, i, data_size;
1530 HKEY root;
1531 PERF_DATA_BLOCK *pdb;
1533 if (!ret_size)
1534 return ERROR_INVALID_PARAMETER;
1536 if (!wcsnicmp( query, L"counter", 7 ))
1537 return query_perf_names( type, data, ret_size, unicode );
1538 if (!wcsnicmp( query, L"help", 4 ))
1539 return query_perf_help( type, data, ret_size, unicode );
1541 data_size = *ret_size;
1542 *ret_size = 0;
1544 if (type)
1545 *type = REG_BINARY;
1547 if (!data || data_size < sizeof(*pdb))
1548 return ERROR_MORE_DATA;
1550 pdb = data;
1552 pdb->Signature[0] = 'P';
1553 pdb->Signature[1] = 'E';
1554 pdb->Signature[2] = 'R';
1555 pdb->Signature[3] = 'F';
1556 #ifdef WORDS_BIGENDIAN
1557 pdb->LittleEndian = FALSE;
1558 #else
1559 pdb->LittleEndian = TRUE;
1560 #endif
1561 pdb->Version = PERF_DATA_VERSION;
1562 pdb->Revision = PERF_DATA_REVISION;
1563 pdb->TotalByteLength = 0;
1564 pdb->HeaderLength = sizeof(*pdb);
1565 pdb->NumObjectTypes = 0;
1566 pdb->DefaultObject = 0;
1567 NtQueryPerformanceCounter( &pdb->PerfTime, &pdb->PerfFreq );
1569 data = pdb + 1;
1570 pdb->SystemNameOffset = sizeof(*pdb);
1571 pdb->SystemNameLength = (data_size - sizeof(*pdb)) / sizeof(WCHAR);
1572 if (!GetComputerNameExW(ComputerNameNetBIOS, data, &pdb->SystemNameLength))
1573 return ERROR_MORE_DATA;
1575 pdb->SystemNameLength++;
1576 pdb->SystemNameLength *= sizeof(WCHAR);
1578 pdb->HeaderLength += pdb->SystemNameLength;
1580 /* align to 8 bytes */
1581 if (pdb->SystemNameLength & 7)
1582 pdb->HeaderLength += 8 - (pdb->SystemNameLength & 7);
1584 if (data_size < pdb->HeaderLength)
1585 return ERROR_MORE_DATA;
1587 pdb->TotalByteLength = pdb->HeaderLength;
1589 data_size -= pdb->HeaderLength;
1590 data = (char *)data + pdb->HeaderLength;
1592 err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services", 0, KEY_READ, &root);
1593 if (err != ERROR_SUCCESS)
1594 return err;
1596 i = 0;
1597 for (;;)
1599 DWORD collected_size = data_size, obj_count = 0;
1600 struct perf_provider provider;
1601 WCHAR name[MAX_SERVICE_NAME];
1602 DWORD len = ARRAY_SIZE( name );
1603 void *collected_data = data;
1605 err = RegEnumKeyExW(root, i++, name, &len, NULL, NULL, NULL, NULL);
1606 if (err == ERROR_NO_MORE_ITEMS)
1608 err = ERROR_SUCCESS;
1609 break;
1612 if (err != ERROR_SUCCESS)
1613 continue;
1615 if (!load_provider(root, name, &provider))
1616 continue;
1618 err = collect_data(&provider, query, &collected_data, &collected_size, &obj_count);
1619 FreeLibrary(provider.perflib);
1621 if (err == ERROR_MORE_DATA)
1622 break;
1624 if (err == ERROR_SUCCESS)
1626 PERF_OBJECT_TYPE *obj = (PERF_OBJECT_TYPE *)data;
1628 TRACE("Collect: obj->TotalByteLength %lu, collected_size %lu\n",
1629 obj->TotalByteLength, collected_size);
1631 data_size -= collected_size;
1632 data = collected_data;
1634 pdb->TotalByteLength += collected_size;
1635 pdb->NumObjectTypes += obj_count;
1639 RegCloseKey(root);
1641 if (err == ERROR_SUCCESS)
1643 *ret_size = pdb->TotalByteLength;
1645 GetSystemTime(&pdb->SystemTime);
1646 GetSystemTimeAsFileTime((FILETIME *)&pdb->PerfTime100nSec);
1649 return err;
1652 /******************************************************************************
1653 * RegQueryValueExW (kernelbase.@)
1655 * See RegQueryValueExA.
1657 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1658 LPBYTE data, LPDWORD count )
1660 NTSTATUS status;
1661 UNICODE_STRING name_str;
1662 DWORD total_size;
1663 char buffer[256], *buf_ptr = buffer;
1664 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1665 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1667 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1668 hkey, debugstr_w(name), reserved, type, data, count,
1669 (count && data) ? *count : 0 );
1671 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1673 if (is_perf_key( hkey ))
1674 return query_perf_data( name, type, data, count, TRUE );
1676 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
1678 RtlInitUnicodeString( &name_str, name );
1680 if (data) total_size = min( sizeof(buffer), *count + info_size );
1681 else
1683 total_size = info_size;
1684 if (count) *count = 0;
1687 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1688 buffer, total_size, &total_size );
1689 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1691 if (data)
1693 /* retry with a dynamically allocated buffer */
1694 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1696 if (buf_ptr != buffer) heap_free( buf_ptr );
1697 if (!(buf_ptr = heap_alloc( total_size )))
1698 return ERROR_NOT_ENOUGH_MEMORY;
1699 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1700 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1701 buf_ptr, total_size, &total_size );
1704 if (!status)
1706 memcpy( data, buf_ptr + info_size, total_size - info_size );
1707 /* if the type is REG_SZ and data is not 0-terminated
1708 * and there is enough space in the buffer NT appends a \0 */
1709 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1711 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1712 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1715 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1717 else status = STATUS_SUCCESS;
1719 if (type) *type = info->Type;
1720 if (count) *count = total_size - info_size;
1722 done:
1723 if (buf_ptr != buffer) heap_free( buf_ptr );
1724 return RtlNtStatusToDosError(status);
1728 /******************************************************************************
1729 * RegQueryValueExA (kernelbase.@)
1731 * Get the type and contents of a specified value under with a key.
1733 * PARAMS
1734 * hkey [I] Handle of the key to query
1735 * name [I] Name of value under hkey to query
1736 * reserved [I] Reserved - must be NULL
1737 * type [O] Destination for the value type, or NULL if not required
1738 * data [O] Destination for the values contents, or NULL if not required
1739 * count [I/O] Size of data, updated with the number of bytes returned
1741 * RETURNS
1742 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1743 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1744 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1745 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1747 * NOTES
1748 * MSDN states that if data is too small it is partially filled. In reality
1749 * it remains untouched.
1751 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved,
1752 LPDWORD type, LPBYTE data, LPDWORD count )
1754 NTSTATUS status;
1755 ANSI_STRING nameA;
1756 UNICODE_STRING nameW;
1757 DWORD total_size, datalen = 0;
1758 char buffer[256], *buf_ptr = buffer;
1759 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1760 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1762 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1763 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1765 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1766 if (!(hkey = get_special_root_hkey( hkey )))
1767 return ERROR_INVALID_HANDLE;
1769 if (count) datalen = *count;
1770 if (!data && count) *count = 0;
1772 /* this matches Win9x behaviour - NT sets *type to a random value */
1773 if (type) *type = REG_NONE;
1775 RtlInitAnsiString( &nameA, name );
1776 if ((status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1777 return RtlNtStatusToDosError(status);
1779 if (is_perf_key( hkey ))
1781 DWORD ret = query_perf_data( nameW.Buffer, type, data, count, FALSE );
1782 RtlFreeUnicodeString( &nameW );
1783 return ret;
1786 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1787 buffer, sizeof(buffer), &total_size );
1788 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1790 /* we need to fetch the contents for a string type even if not requested,
1791 * because we need to compute the length of the ANSI string. */
1792 if (data || is_string(info->Type))
1794 /* retry with a dynamically allocated buffer */
1795 while (status == STATUS_BUFFER_OVERFLOW)
1797 if (buf_ptr != buffer) heap_free( buf_ptr );
1798 if (!(buf_ptr = heap_alloc( total_size )))
1800 status = STATUS_NO_MEMORY;
1801 goto done;
1803 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1804 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1805 buf_ptr, total_size, &total_size );
1808 if (status) goto done;
1810 if (is_string(info->Type))
1812 DWORD len;
1814 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1815 total_size - info_size );
1816 if (data && len)
1818 if (len > datalen) status = STATUS_BUFFER_OVERFLOW;
1819 else
1821 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1822 total_size - info_size );
1823 /* if the type is REG_SZ and data is not 0-terminated
1824 * and there is enough space in the buffer NT appends a \0 */
1825 if (len < datalen && data[len-1]) data[len] = 0;
1828 total_size = len + info_size;
1830 else if (data)
1832 if (total_size - info_size > datalen) status = STATUS_BUFFER_OVERFLOW;
1833 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1836 else status = STATUS_SUCCESS;
1838 if (type) *type = info->Type;
1839 if (count) *count = total_size - info_size;
1841 done:
1842 if (buf_ptr != buffer) heap_free( buf_ptr );
1843 RtlFreeUnicodeString( &nameW );
1844 return RtlNtStatusToDosError(status);
1848 /******************************************************************************
1849 * apply_restrictions [internal]
1851 * Helper function for RegGetValueA/W.
1853 static void apply_restrictions( DWORD dwFlags, DWORD dwType, DWORD cbData, PLONG ret )
1855 /* Check if the type is restricted by the passed flags */
1856 if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1858 DWORD dwMask = 0;
1860 switch (dwType)
1862 case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1863 case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1864 case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1865 case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1866 case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1867 case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1868 case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1871 if (dwFlags & dwMask)
1873 /* Type is not restricted, check for size mismatch */
1874 if (dwType == REG_BINARY)
1876 DWORD cbExpect = 0;
1878 if ((dwFlags & RRF_RT_ANY) == RRF_RT_DWORD)
1879 cbExpect = 4;
1880 else if ((dwFlags & RRF_RT_ANY) == RRF_RT_QWORD)
1881 cbExpect = 8;
1883 if (cbExpect && cbData != cbExpect)
1884 *ret = ERROR_DATATYPE_MISMATCH;
1887 else *ret = ERROR_UNSUPPORTED_TYPE;
1892 /******************************************************************************
1893 * RegGetValueW (kernelbase.@)
1895 * Retrieves the type and data for a value name associated with a key,
1896 * optionally expanding its content and restricting its type.
1898 * PARAMS
1899 * hKey [I] Handle to an open key.
1900 * pszSubKey [I] Name of the subkey of hKey.
1901 * pszValue [I] Name of value under hKey/szSubKey to query.
1902 * dwFlags [I] Flags restricting the value type to retrieve.
1903 * pdwType [O] Destination for the values type, may be NULL.
1904 * pvData [O] Destination for the values content, may be NULL.
1905 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
1906 * retrieve the whole content, including the trailing '\0'
1907 * for strings.
1909 * RETURNS
1910 * Success: ERROR_SUCCESS
1911 * Failure: nonzero error code from Winerror.h
1913 * NOTES
1914 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
1915 * expanded and pdwType is set to REG_SZ instead.
1916 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1917 * without RRF_NOEXPAND is thus not allowed.
1918 * An exception is the case where RRF_RT_ANY is specified, because then
1919 * RRF_NOEXPAND is allowed.
1921 LSTATUS WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1922 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1923 LPDWORD pcbData )
1925 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1926 PVOID pvBuf = NULL;
1927 LONG ret;
1929 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1930 hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1931 pvData, pcbData, cbData);
1933 if (pvData && !pcbData)
1934 return ERROR_INVALID_PARAMETER;
1936 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1937 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1938 return ERROR_INVALID_PARAMETER;
1940 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1941 return ERROR_INVALID_PARAMETER;
1943 if (pszSubKey && pszSubKey[0])
1945 REGSAM samDesired = KEY_QUERY_VALUE;
1947 if (dwFlags & RRF_WOW64_MASK)
1948 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1950 ret = RegOpenKeyExW(hKey, pszSubKey, 0, samDesired, &hKey);
1951 if (ret != ERROR_SUCCESS) return ret;
1954 ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1956 /* If we are going to expand we need to read in the whole the value even
1957 * if the passed buffer was too small as the expanded string might be
1958 * smaller than the unexpanded one and could fit into cbData bytes. */
1959 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1960 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1962 do {
1963 heap_free(pvBuf);
1965 pvBuf = heap_alloc(cbData);
1966 if (!pvBuf)
1968 ret = ERROR_NOT_ENOUGH_MEMORY;
1969 break;
1972 if (ret == ERROR_MORE_DATA || !pvData)
1973 ret = RegQueryValueExW(hKey, pszValue, NULL,
1974 &dwType, pvBuf, &cbData);
1975 else
1977 /* Even if cbData was large enough we have to copy the
1978 * string since ExpandEnvironmentStrings can't handle
1979 * overlapping buffers. */
1980 CopyMemory(pvBuf, pvData, cbData);
1983 /* Both the type or the value itself could have been modified in
1984 * between so we have to keep retrying until the buffer is large
1985 * enough or we no longer have to expand the value. */
1986 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1988 if (ret == ERROR_SUCCESS)
1990 /* Recheck dwType in case it changed since the first call */
1991 if (dwType == REG_EXPAND_SZ)
1993 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1994 pcbData ? *pcbData : 0) * sizeof(WCHAR);
1995 dwType = REG_SZ;
1996 if(pvData && pcbData && cbData > *pcbData)
1997 ret = ERROR_MORE_DATA;
1999 else if (pvData)
2000 CopyMemory(pvData, pvBuf, *pcbData);
2003 heap_free(pvBuf);
2006 if (pszSubKey && pszSubKey[0])
2007 RegCloseKey(hKey);
2009 apply_restrictions(dwFlags, dwType, cbData, &ret);
2011 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
2012 ZeroMemory(pvData, *pcbData);
2014 if (pdwType) *pdwType = dwType;
2015 if (pcbData) *pcbData = cbData;
2017 return ret;
2021 /******************************************************************************
2022 * RegGetValueA (kernelbase.@)
2024 * See RegGetValueW.
2026 LSTATUS WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
2027 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
2028 LPDWORD pcbData )
2030 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
2031 PVOID pvBuf = NULL;
2032 LONG ret;
2034 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
2035 hKey, debugstr_a(pszSubKey), debugstr_a(pszValue), dwFlags,
2036 pdwType, pvData, pcbData, cbData);
2038 if (pvData && !pcbData)
2039 return ERROR_INVALID_PARAMETER;
2041 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
2042 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
2043 return ERROR_INVALID_PARAMETER;
2045 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
2046 return ERROR_INVALID_PARAMETER;
2048 if (pszSubKey && pszSubKey[0])
2050 REGSAM samDesired = KEY_QUERY_VALUE;
2052 if (dwFlags & RRF_WOW64_MASK)
2053 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
2055 ret = RegOpenKeyExA(hKey, pszSubKey, 0, samDesired, &hKey);
2056 if (ret != ERROR_SUCCESS) return ret;
2059 ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
2061 /* If we are going to expand we need to read in the whole the value even
2062 * if the passed buffer was too small as the expanded string might be
2063 * smaller than the unexpanded one and could fit into cbData bytes. */
2064 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
2065 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
2067 do {
2068 heap_free(pvBuf);
2070 pvBuf = heap_alloc(cbData);
2071 if (!pvBuf)
2073 ret = ERROR_NOT_ENOUGH_MEMORY;
2074 break;
2077 if (ret == ERROR_MORE_DATA || !pvData)
2078 ret = RegQueryValueExA(hKey, pszValue, NULL,
2079 &dwType, pvBuf, &cbData);
2080 else
2082 /* Even if cbData was large enough we have to copy the
2083 * string since ExpandEnvironmentStrings can't handle
2084 * overlapping buffers. */
2085 CopyMemory(pvBuf, pvData, cbData);
2088 /* Both the type or the value itself could have been modified in
2089 * between so we have to keep retrying until the buffer is large
2090 * enough or we no longer have to expand the value. */
2091 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
2093 if (ret == ERROR_SUCCESS)
2095 /* Recheck dwType in case it changed since the first call */
2096 if (dwType == REG_EXPAND_SZ)
2098 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
2099 pcbData ? *pcbData : 0);
2100 dwType = REG_SZ;
2101 if(pvData && pcbData && cbData > *pcbData)
2102 ret = ERROR_MORE_DATA;
2104 else if (pvData)
2105 CopyMemory(pvData, pvBuf, *pcbData);
2108 heap_free(pvBuf);
2111 if (pszSubKey && pszSubKey[0])
2112 RegCloseKey(hKey);
2114 apply_restrictions(dwFlags, dwType, cbData, &ret);
2116 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
2117 ZeroMemory(pvData, *pcbData);
2119 if (pdwType) *pdwType = dwType;
2120 if (pcbData) *pcbData = cbData;
2122 return ret;
2126 /******************************************************************************
2127 * RegEnumValueW (kernelbase.@)
2129 * Enumerates the values for the specified open registry key.
2131 * PARAMS
2132 * hkey [I] Handle to key to query
2133 * index [I] Index of value to query
2134 * value [O] Value string
2135 * val_count [I/O] Size of value buffer (in wchars)
2136 * reserved [I] Reserved
2137 * type [O] Type code
2138 * data [O] Value data
2139 * count [I/O] Size of data buffer (in bytes)
2141 * RETURNS
2142 * Success: ERROR_SUCCESS
2143 * Failure: nonzero error code from Winerror.h
2145 LSTATUS WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
2146 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
2148 NTSTATUS status;
2149 DWORD total_size;
2150 char buffer[256], *buf_ptr = buffer;
2151 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2152 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
2154 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2155 hkey, index, value, val_count, reserved, type, data, count );
2157 if ((data && !count) || reserved || !value || !val_count)
2158 return ERROR_INVALID_PARAMETER;
2159 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2161 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
2162 if (data) total_size += *count;
2163 total_size = min( sizeof(buffer), total_size );
2165 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2166 buffer, total_size, &total_size );
2168 /* retry with a dynamically allocated buffer */
2169 while (status == STATUS_BUFFER_OVERFLOW)
2171 if (buf_ptr != buffer) heap_free( buf_ptr );
2172 if (!(buf_ptr = heap_alloc( total_size )))
2173 return ERROR_NOT_ENOUGH_MEMORY;
2174 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
2175 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2176 buf_ptr, total_size, &total_size );
2179 if (status) goto done;
2181 if (info->NameLength/sizeof(WCHAR) >= *val_count)
2183 status = STATUS_BUFFER_OVERFLOW;
2184 goto overflow;
2186 memcpy( value, info->Name, info->NameLength );
2187 *val_count = info->NameLength / sizeof(WCHAR);
2188 value[*val_count] = 0;
2190 if (data)
2192 if (total_size - info->DataOffset > *count)
2194 status = STATUS_BUFFER_OVERFLOW;
2195 goto overflow;
2197 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2198 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
2200 /* if the type is REG_SZ and data is not 0-terminated
2201 * and there is enough space in the buffer NT appends a \0 */
2202 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
2203 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
2207 overflow:
2208 if (type) *type = info->Type;
2209 if (count) *count = info->DataLength;
2211 done:
2212 if (buf_ptr != buffer) heap_free( buf_ptr );
2213 return RtlNtStatusToDosError(status);
2217 /******************************************************************************
2218 * RegEnumValueA (kernelbase.@)
2220 * See RegEnumValueW.
2222 LSTATUS WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
2223 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
2225 NTSTATUS status;
2226 DWORD total_size;
2227 char buffer[256], *buf_ptr = buffer;
2228 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2229 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
2231 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2232 hkey, index, value, val_count, reserved, type, data, count );
2234 if ((data && !count) || reserved || !value || !val_count)
2235 return ERROR_INVALID_PARAMETER;
2236 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2238 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
2239 if (data) total_size += *count;
2240 total_size = min( sizeof(buffer), total_size );
2242 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2243 buffer, total_size, &total_size );
2245 /* we need to fetch the contents for a string type even if not requested,
2246 * because we need to compute the length of the ANSI string. */
2248 /* retry with a dynamically allocated buffer */
2249 while (status == STATUS_BUFFER_OVERFLOW)
2251 if (buf_ptr != buffer) heap_free( buf_ptr );
2252 if (!(buf_ptr = heap_alloc( total_size )))
2253 return ERROR_NOT_ENOUGH_MEMORY;
2254 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
2255 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2256 buf_ptr, total_size, &total_size );
2259 if (status) goto done;
2261 if (is_string(info->Type))
2263 DWORD len;
2264 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
2265 total_size - info->DataOffset );
2266 if (data && len)
2268 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
2269 else
2271 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
2272 total_size - info->DataOffset );
2273 /* if the type is REG_SZ and data is not 0-terminated
2274 * and there is enough space in the buffer NT appends a \0 */
2275 if (len < *count && data[len-1]) data[len] = 0;
2278 info->DataLength = len;
2280 else if (data)
2282 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
2283 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2286 if (!status)
2288 DWORD len;
2290 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
2291 if (len >= *val_count)
2293 status = STATUS_BUFFER_OVERFLOW;
2294 if (*val_count)
2296 len = *val_count - 1;
2297 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2298 value[len] = 0;
2301 else
2303 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2304 value[len] = 0;
2305 *val_count = len;
2309 if (type) *type = info->Type;
2310 if (count) *count = info->DataLength;
2312 done:
2313 if (buf_ptr != buffer) heap_free( buf_ptr );
2314 return RtlNtStatusToDosError(status);
2317 /******************************************************************************
2318 * RegDeleteValueW (kernelbase.@)
2320 * See RegDeleteValueA.
2322 LSTATUS WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
2324 return RegDeleteKeyValueW( hkey, NULL, name );
2327 /******************************************************************************
2328 * RegDeleteValueA (kernelbase.@)
2330 * Delete a value from the registry.
2332 * PARAMS
2333 * hkey [I] Registry handle of the key holding the value
2334 * name [I] Name of the value under hkey to delete
2336 * RETURNS
2337 * Success: ERROR_SUCCESS
2338 * Failure: nonzero error code from Winerror.h
2340 LSTATUS WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
2342 return RegDeleteKeyValueA( hkey, NULL, name );
2345 /******************************************************************************
2346 * RegDeleteKeyValueW (kernelbase.@)
2348 LONG WINAPI RegDeleteKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name )
2350 UNICODE_STRING nameW;
2351 HKEY hsubkey = 0;
2352 LONG ret;
2354 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2356 if (subkey)
2358 if ((ret = RegOpenKeyExW( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey )))
2359 return ret;
2360 hkey = hsubkey;
2363 RtlInitUnicodeString( &nameW, name );
2364 ret = RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
2365 if (hsubkey) RegCloseKey( hsubkey );
2366 return ret;
2369 /******************************************************************************
2370 * RegDeleteKeyValueA (kernelbase.@)
2372 LONG WINAPI RegDeleteKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name )
2374 UNICODE_STRING nameW;
2375 HKEY hsubkey = 0;
2376 ANSI_STRING nameA;
2377 NTSTATUS status;
2379 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2381 if (subkey)
2383 LONG ret = RegOpenKeyExA( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey );
2384 if (ret)
2385 return ret;
2386 hkey = hsubkey;
2389 RtlInitAnsiString( &nameA, name );
2390 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
2392 status = NtDeleteValueKey( hkey, &nameW );
2393 RtlFreeUnicodeString( &nameW );
2396 if (hsubkey) RegCloseKey( hsubkey );
2397 return RtlNtStatusToDosError( status );
2400 /******************************************************************************
2401 * RegLoadKeyW (kernelbase.@)
2403 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
2404 * registration information from a specified file into that subkey.
2406 * PARAMS
2407 * hkey [I] Handle of open key
2408 * subkey [I] Address of name of subkey
2409 * filename [I] Address of filename for registry information
2411 * RETURNS
2412 * Success: ERROR_SUCCESS
2413 * Failure: nonzero error code from Winerror.h
2415 LSTATUS WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
2417 OBJECT_ATTRIBUTES destkey, file;
2418 UNICODE_STRING subkeyW, filenameW;
2419 NTSTATUS status;
2421 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2423 destkey.Length = sizeof(destkey);
2424 destkey.RootDirectory = hkey; /* root key: HKLM or HKU */
2425 destkey.ObjectName = &subkeyW; /* name of the key */
2426 destkey.Attributes = 0;
2427 destkey.SecurityDescriptor = NULL;
2428 destkey.SecurityQualityOfService = NULL;
2429 RtlInitUnicodeString(&subkeyW, subkey);
2431 file.Length = sizeof(file);
2432 file.RootDirectory = NULL;
2433 file.ObjectName = &filenameW; /* file containing the hive */
2434 file.Attributes = OBJ_CASE_INSENSITIVE;
2435 file.SecurityDescriptor = NULL;
2436 file.SecurityQualityOfService = NULL;
2437 RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
2439 status = NtLoadKey(&destkey, &file);
2440 RtlFreeUnicodeString(&filenameW);
2441 return RtlNtStatusToDosError( status );
2445 /******************************************************************************
2446 * RegLoadKeyA (kernelbase.@)
2448 * See RegLoadKeyW.
2450 LSTATUS WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
2452 UNICODE_STRING subkeyW = { 0, 0, NULL }, filenameW = { 0, 0, NULL };
2453 STRING subkeyA, filenameA;
2454 NTSTATUS status;
2455 LONG ret;
2457 RtlInitAnsiString(&subkeyA, subkey);
2458 RtlInitAnsiString(&filenameA, filename);
2460 if (!(status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)) &&
2461 !(status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
2463 ret = RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
2465 else ret = RtlNtStatusToDosError(status);
2466 RtlFreeUnicodeString(&subkeyW);
2467 RtlFreeUnicodeString(&filenameW);
2468 return ret;
2472 /******************************************************************************
2473 * RegSaveKeyExW (kernelbase.@)
2475 LSTATUS WINAPI RegSaveKeyExW( HKEY hkey, LPCWSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2477 UNICODE_STRING nameW;
2478 OBJECT_ATTRIBUTES attr;
2479 IO_STATUS_BLOCK io;
2480 NTSTATUS status;
2481 HANDLE handle;
2483 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
2485 if (!file || !*file) return ERROR_INVALID_PARAMETER;
2486 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2488 if ((status = RtlDosPathNameToNtPathName_U_WithStatus( file, &nameW, NULL, NULL )))
2489 return RtlNtStatusToDosError( status );
2491 InitializeObjectAttributes( &attr, &nameW, OBJ_CASE_INSENSITIVE, 0, sa );
2492 status = NtCreateFile( &handle, GENERIC_WRITE | SYNCHRONIZE, &attr, &io, NULL, FILE_NON_DIRECTORY_FILE,
2493 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
2494 FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
2495 RtlFreeUnicodeString( &nameW );
2496 if (!status)
2498 status = NtSaveKey( hkey, handle );
2499 CloseHandle( handle );
2501 return RtlNtStatusToDosError( status );
2505 /******************************************************************************
2506 * RegSaveKeyExA (kernelbase.@)
2508 LSTATUS WINAPI RegSaveKeyExA( HKEY hkey, LPCSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2510 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
2511 NTSTATUS status;
2512 STRING fileA;
2514 RtlInitAnsiString(&fileA, file);
2515 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
2516 return RtlNtStatusToDosError( status );
2517 return RegSaveKeyExW(hkey, fileW->Buffer, sa, flags);
2521 /******************************************************************************
2522 * RegRestoreKeyW (kernelbase.@)
2524 * Read the registry information from a file and copy it over a key.
2526 * PARAMS
2527 * hkey [I] Handle of key where restore begins
2528 * lpFile [I] Address of filename containing saved tree
2529 * dwFlags [I] Optional flags
2531 * RETURNS
2532 * Success: ERROR_SUCCESS
2533 * Failure: nonzero error code from Winerror.h
2535 LSTATUS WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2537 TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
2539 /* It seems to do this check before the hkey check */
2540 if (!lpFile || !*lpFile)
2541 return ERROR_INVALID_PARAMETER;
2543 FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2545 /* Check for file existence */
2547 return ERROR_SUCCESS;
2551 /******************************************************************************
2552 * RegRestoreKeyA (kernelbase.@)
2554 * See RegRestoreKeyW.
2556 LSTATUS WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2558 UNICODE_STRING lpFileW;
2559 LONG ret;
2561 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2562 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2563 RtlFreeUnicodeString( &lpFileW );
2564 return ret;
2568 /******************************************************************************
2569 * RegUnLoadKeyW (kernelbase.@)
2571 * Unload a registry key and its subkeys from the registry.
2573 * PARAMS
2574 * hkey [I] Handle of open key
2575 * lpSubKey [I] Address of name of subkey to unload
2577 * RETURNS
2578 * Success: ERROR_SUCCESS
2579 * Failure: nonzero error code from Winerror.h
2581 LSTATUS WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2583 OBJECT_ATTRIBUTES attr;
2584 UNICODE_STRING subkey;
2586 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2588 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2590 RtlInitUnicodeString(&subkey, lpSubKey);
2591 InitializeObjectAttributes(&attr, &subkey, OBJ_CASE_INSENSITIVE, hkey, NULL);
2592 return RtlNtStatusToDosError( NtUnloadKey(&attr) );
2596 /******************************************************************************
2597 * RegUnLoadKeyA (kernelbase.@)
2599 * See RegUnLoadKeyW.
2601 LSTATUS WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2603 UNICODE_STRING lpSubKeyW;
2604 LONG ret;
2606 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2607 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2608 RtlFreeUnicodeString( &lpSubKeyW );
2609 return ret;
2613 /******************************************************************************
2614 * RegSetKeySecurity (kernelbase.@)
2616 * Set the security of an open registry key.
2618 * PARAMS
2619 * hkey [I] Open handle of key to set
2620 * SecurityInfo [I] Descriptor contents
2621 * pSecurityDesc [I] Address of descriptor for key
2623 * RETURNS
2624 * Success: ERROR_SUCCESS
2625 * Failure: nonzero error code from Winerror.h
2627 LSTATUS WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2628 PSECURITY_DESCRIPTOR pSecurityDesc )
2630 TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2632 /* It seems to perform this check before the hkey check */
2633 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2634 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2635 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2636 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2637 /* Param OK */
2638 } else
2639 return ERROR_INVALID_PARAMETER;
2641 if (!pSecurityDesc)
2642 return ERROR_INVALID_PARAMETER;
2644 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2646 return RtlNtStatusToDosError( NtSetSecurityObject( hkey, SecurityInfo, pSecurityDesc ) );
2650 /******************************************************************************
2651 * RegGetKeySecurity (kernelbase.@)
2653 * Get a copy of the security descriptor for a given registry key.
2655 * PARAMS
2656 * hkey [I] Open handle of key to set
2657 * SecurityInformation [I] Descriptor contents
2658 * pSecurityDescriptor [O] Address of descriptor for key
2659 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2661 * RETURNS
2662 * Success: ERROR_SUCCESS
2663 * Failure: Error code
2665 LSTATUS WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2666 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2667 LPDWORD lpcbSecurityDescriptor )
2669 TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
2670 *lpcbSecurityDescriptor);
2672 if (!(hkey = get_special_root_hkey( hkey ))) return ERROR_INVALID_HANDLE;
2674 return RtlNtStatusToDosError( NtQuerySecurityObject( hkey,
2675 SecurityInformation, pSecurityDescriptor,
2676 *lpcbSecurityDescriptor, lpcbSecurityDescriptor ) );
2680 /******************************************************************************
2681 * RegFlushKey (kernelbase.@)
2683 * Immediately write a registry key to registry.
2685 * PARAMS
2686 * hkey [I] Handle of key to write
2688 * RETURNS
2689 * Success: ERROR_SUCCESS
2690 * Failure: Error code
2692 LSTATUS WINAPI RegFlushKey( HKEY hkey )
2694 hkey = get_special_root_hkey( hkey );
2695 if (!hkey) return ERROR_INVALID_HANDLE;
2697 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2701 /******************************************************************************
2702 * RegNotifyChangeKeyValue (kernelbase.@)
2704 * Notify the caller about changes to the attributes or contents of a registry key.
2706 * PARAMS
2707 * hkey [I] Handle of key to watch
2708 * fWatchSubTree [I] Flag for subkey notification
2709 * fdwNotifyFilter [I] Changes to be reported
2710 * hEvent [I] Handle of signaled event
2711 * fAsync [I] Flag for asynchronous reporting
2713 * RETURNS
2714 * Success: ERROR_SUCCESS
2715 * Failure: nonzero error code from Winerror.h
2717 LSTATUS WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2718 DWORD fdwNotifyFilter, HANDLE hEvent,
2719 BOOL fAsync )
2721 NTSTATUS status;
2722 IO_STATUS_BLOCK iosb;
2724 hkey = get_special_root_hkey( hkey );
2725 if (!hkey) return ERROR_INVALID_HANDLE;
2727 TRACE("(%p,%i,%ld,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2728 hEvent, fAsync);
2730 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2731 fdwNotifyFilter, fWatchSubTree, NULL, 0,
2732 fAsync);
2734 if (status && status != STATUS_PENDING)
2735 return RtlNtStatusToDosError( status );
2737 return ERROR_SUCCESS;
2740 /******************************************************************************
2741 * RegOpenUserClassesRoot (kernelbase.@)
2743 * Open the HKEY_CLASSES_ROOT key for a user.
2745 * PARAMS
2746 * hToken [I] Handle of token representing the user
2747 * dwOptions [I] Reserved, must be 0
2748 * samDesired [I] Desired access rights
2749 * phkResult [O] Destination for the resulting key handle
2751 * RETURNS
2752 * Success: ERROR_SUCCESS
2753 * Failure: nonzero error code from Winerror.h
2755 * NOTES
2756 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2757 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2758 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2760 LSTATUS WINAPI RegOpenUserClassesRoot( HANDLE hToken, DWORD dwOptions, REGSAM samDesired, PHKEY phkResult )
2762 FIXME("(%p, 0x%lx, 0x%lx, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2764 *phkResult = HKEY_CLASSES_ROOT;
2765 return ERROR_SUCCESS;
2769 static void dump_mui_cache(void)
2771 struct mui_cache_entry *ent;
2773 TRACE("---------- MUI Cache ----------\n");
2774 LIST_FOR_EACH_ENTRY( ent, &reg_mui_cache, struct mui_cache_entry, entry )
2775 TRACE("entry=%p, %s,-%lu [%#lx] => %s\n",
2776 ent, wine_dbgstr_w(ent->file_name), ent->index, ent->locale, wine_dbgstr_w(ent->text));
2779 static inline void free_mui_cache_entry(struct mui_cache_entry *ent)
2781 heap_free(ent->file_name);
2782 heap_free(ent->text);
2783 heap_free(ent);
2786 /* critical section must be held */
2787 static int reg_mui_cache_get(const WCHAR *file_name, UINT index, WCHAR **buffer)
2789 struct mui_cache_entry *ent;
2791 TRACE("(%s %u %p)\n", wine_dbgstr_w(file_name), index, buffer);
2793 LIST_FOR_EACH_ENTRY(ent, &reg_mui_cache, struct mui_cache_entry, entry)
2795 if (ent->index == index && ent->locale == GetThreadLocale() &&
2796 !lstrcmpiW(ent->file_name, file_name))
2797 goto found;
2799 return 0;
2801 found:
2802 /* move to the list head */
2803 if (list_prev(&reg_mui_cache, &ent->entry)) {
2804 list_remove(&ent->entry);
2805 list_add_head(&reg_mui_cache, &ent->entry);
2808 TRACE("=> %s\n", wine_dbgstr_w(ent->text));
2809 *buffer = ent->text;
2810 return lstrlenW(ent->text);
2813 /* critical section must be held */
2814 static void reg_mui_cache_put(const WCHAR *file_name, UINT index, const WCHAR *buffer, INT size)
2816 struct mui_cache_entry *ent;
2817 TRACE("(%s %u %s %d)\n", wine_dbgstr_w(file_name), index, wine_dbgstr_wn(buffer, size), size);
2819 ent = heap_calloc(sizeof(*ent), 1);
2820 if (!ent)
2821 return;
2822 ent->file_name = heap_alloc((lstrlenW(file_name) + 1) * sizeof(WCHAR));
2823 if (!ent->file_name) {
2824 free_mui_cache_entry(ent);
2825 return;
2827 lstrcpyW(ent->file_name, file_name);
2828 ent->index = index;
2829 ent->locale = GetThreadLocale();
2830 ent->text = heap_alloc((size + 1) * sizeof(WCHAR));
2831 if (!ent->text) {
2832 free_mui_cache_entry(ent);
2833 return;
2835 memcpy(ent->text, buffer, size * sizeof(WCHAR));
2836 ent->text[size] = '\0';
2838 TRACE("add %p\n", ent);
2839 list_add_head(&reg_mui_cache, &ent->entry);
2840 if (reg_mui_cache_count > REG_MUI_CACHE_SIZE) {
2841 ent = LIST_ENTRY( list_tail( &reg_mui_cache ), struct mui_cache_entry, entry );
2842 TRACE("freeing %p\n", ent);
2843 list_remove(&ent->entry);
2844 free_mui_cache_entry(ent);
2846 else
2847 reg_mui_cache_count++;
2849 if (TRACE_ON(reg))
2850 dump_mui_cache();
2851 return;
2854 static LONG load_mui_string(const WCHAR *file_name, UINT res_id, WCHAR *buffer, INT max_chars, INT *req_chars, DWORD flags)
2856 HMODULE hModule = NULL;
2857 WCHAR *string = NULL, *full_name;
2858 int size;
2859 LONG result;
2861 /* Verify the file existence. i.e. We don't rely on PATH variable */
2862 if (GetFileAttributesW(file_name) == INVALID_FILE_ATTRIBUTES)
2863 return ERROR_FILE_NOT_FOUND;
2865 size = GetFullPathNameW(file_name, 0, NULL, NULL);
2866 if (!size)
2867 return GetLastError();
2868 full_name = heap_alloc(size * sizeof(WCHAR));
2869 if (!full_name)
2870 return ERROR_NOT_ENOUGH_MEMORY;
2871 GetFullPathNameW(file_name, size, full_name, NULL);
2873 RtlEnterCriticalSection(&reg_mui_cs);
2874 size = reg_mui_cache_get(full_name, res_id, &string);
2875 if (!size) {
2876 RtlLeaveCriticalSection(&reg_mui_cs);
2878 /* Load the file */
2879 hModule = LoadLibraryExW(full_name, NULL,
2880 LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
2881 if (!hModule)
2882 return GetLastError();
2884 size = LoadStringW(hModule, res_id, (WCHAR *)&string, 0);
2885 if (!size) {
2886 if (string) result = ERROR_NOT_FOUND;
2887 else result = GetLastError();
2888 goto cleanup;
2891 RtlEnterCriticalSection(&reg_mui_cs);
2892 reg_mui_cache_put(full_name, res_id, string, size);
2893 RtlLeaveCriticalSection(&reg_mui_cs);
2895 *req_chars = size + 1;
2897 /* If no buffer is given, skip copying. */
2898 if (!buffer) {
2899 result = ERROR_MORE_DATA;
2900 goto cleanup;
2903 /* Else copy over the string, respecting the buffer size. */
2904 if (size < max_chars)
2905 max_chars = size;
2906 else {
2907 if (flags & REG_MUI_STRING_TRUNCATE)
2908 max_chars--;
2909 else {
2910 result = ERROR_MORE_DATA;
2911 goto cleanup;
2914 if (max_chars >= 0) {
2915 memcpy(buffer, string, max_chars * sizeof(WCHAR));
2916 buffer[max_chars] = '\0';
2919 result = ERROR_SUCCESS;
2921 cleanup:
2922 if (hModule)
2923 FreeLibrary(hModule);
2924 else
2925 RtlLeaveCriticalSection(&reg_mui_cs);
2926 heap_free(full_name);
2927 return result;
2930 /******************************************************************************
2931 * RegLoadMUIStringW (kernelbase.@)
2933 * Load the localized version of a string resource from some PE, respective
2934 * id and path of which are given in the registry value in the format
2935 * @[path]\dllname,-resourceId
2937 * PARAMS
2938 * hKey [I] Key, of which to load the string value from.
2939 * pszValue [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2940 * pszBuffer [O] Buffer to store the localized string in.
2941 * cbBuffer [I] Size of the destination buffer in bytes.
2942 * pcbData [O] Number of bytes written to pszBuffer (optional, may be NULL).
2943 * dwFlags [I] Truncate output to fit the buffer if REG_MUI_STRING_TRUNCATE.
2944 * pszBaseDir [I] Base directory of loading path. If NULL, use the current directory.
2946 * RETURNS
2947 * Success: ERROR_SUCCESS,
2948 * Failure: nonzero error code from winerror.h
2950 LSTATUS WINAPI RegLoadMUIStringW(HKEY hKey, LPCWSTR pwszValue, LPWSTR pwszBuffer, DWORD cbBuffer,
2951 LPDWORD pcbData, DWORD dwFlags, LPCWSTR pwszBaseDir)
2953 DWORD dwValueType, cbData;
2954 LPWSTR pwszTempBuffer = NULL, pwszExpandedBuffer = NULL;
2955 LONG result;
2957 TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %ld, pcbData = %p, "
2958 "dwFlags = %lu, pwszBaseDir = %s)\n", hKey, debugstr_w(pwszValue), pwszBuffer,
2959 cbBuffer, pcbData, dwFlags, debugstr_w(pwszBaseDir));
2961 /* Parameter sanity checks. */
2962 if (!hKey || (!pwszBuffer && cbBuffer) || (cbBuffer % sizeof(WCHAR))
2963 || ((dwFlags & REG_MUI_STRING_TRUNCATE) && pcbData)
2964 || (dwFlags & ~REG_MUI_STRING_TRUNCATE))
2965 return ERROR_INVALID_PARAMETER;
2967 /* Check for value existence and correctness of its type, allocate a buffer and load it. */
2968 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, NULL, &cbData);
2969 if (result != ERROR_SUCCESS) goto cleanup;
2970 if (!(dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ) || !cbData) {
2971 result = ERROR_FILE_NOT_FOUND;
2972 goto cleanup;
2974 pwszTempBuffer = heap_alloc(cbData);
2975 if (!pwszTempBuffer) {
2976 result = ERROR_NOT_ENOUGH_MEMORY;
2977 goto cleanup;
2979 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, (LPBYTE)pwszTempBuffer, &cbData);
2980 if (result != ERROR_SUCCESS) goto cleanup;
2982 /* '@' is the prefix for resource based string entries. */
2983 if (*pwszTempBuffer != '@') {
2984 result = ERROR_INVALID_DATA;
2985 goto cleanup;
2988 /* Expand environment variables regardless of the type. */
2989 cbData = ExpandEnvironmentStringsW(pwszTempBuffer, NULL, 0) * sizeof(WCHAR);
2990 if (!cbData) goto cleanup;
2991 pwszExpandedBuffer = heap_alloc(cbData);
2992 if (!pwszExpandedBuffer) {
2993 result = ERROR_NOT_ENOUGH_MEMORY;
2994 goto cleanup;
2996 ExpandEnvironmentStringsW(pwszTempBuffer, pwszExpandedBuffer, cbData / sizeof(WCHAR));
2998 /* Parse the value and load the string. */
3000 WCHAR *pComma = wcsrchr(pwszExpandedBuffer, ','), *pNewBuffer;
3001 UINT uiStringId;
3002 DWORD baseDirLen;
3003 int reqChars;
3005 /* Format of the expanded value is 'path_to_dll,-resId' */
3006 if (!pComma || pComma[1] != '-') {
3007 result = ERROR_INVALID_DATA;
3008 goto cleanup;
3011 uiStringId = wcstol(pComma+2, NULL, 10);
3012 *pComma = '\0';
3014 /* Build a resource dll path. */
3015 baseDirLen = pwszBaseDir ? lstrlenW(pwszBaseDir) : 0;
3016 cbData = (baseDirLen + 1 + lstrlenW(pwszExpandedBuffer + 1) + 1) * sizeof(WCHAR);
3017 pNewBuffer = heap_realloc(pwszTempBuffer, cbData);
3018 if (!pNewBuffer) {
3019 result = ERROR_NOT_ENOUGH_MEMORY;
3020 goto cleanup;
3022 pwszTempBuffer = pNewBuffer;
3023 pwszTempBuffer[0] = '\0';
3024 if (baseDirLen) {
3025 lstrcpyW(pwszTempBuffer, pwszBaseDir);
3026 if (pwszBaseDir[baseDirLen - 1] != '\\')
3027 lstrcatW(pwszTempBuffer, L"\\");
3029 lstrcatW(pwszTempBuffer, pwszExpandedBuffer + 1);
3031 /* Load specified string from the file */
3032 reqChars = 0;
3033 result = load_mui_string(pwszTempBuffer, uiStringId, pwszBuffer, cbBuffer/sizeof(WCHAR), &reqChars, dwFlags);
3034 if (pcbData && (result == ERROR_SUCCESS || result == ERROR_MORE_DATA))
3035 *pcbData = reqChars * sizeof(WCHAR);
3038 cleanup:
3039 heap_free(pwszTempBuffer);
3040 heap_free(pwszExpandedBuffer);
3041 return result;
3044 /******************************************************************************
3045 * RegLoadMUIStringA (kernelbase.@)
3047 * Not implemented on native.
3049 LSTATUS WINAPI RegLoadMUIStringA(HKEY hKey, LPCSTR pszValue, LPSTR pszBuffer, DWORD cbBuffer,
3050 LPDWORD pcbData, DWORD dwFlags, LPCSTR pszBaseDir)
3052 return ERROR_CALL_NOT_IMPLEMENTED;
3056 /******************************************************************************
3057 * RegDeleteTreeW (kernelbase.@)
3060 LSTATUS WINAPI RegDeleteTreeW( HKEY hkey, const WCHAR *subkey )
3062 DWORD name_size, max_name, max_subkey;
3063 WCHAR *name_buf = NULL;
3064 LONG ret;
3066 TRACE( "(%p, %s)\n", hkey, debugstr_w(subkey) );
3068 if (subkey && *subkey)
3070 ret = RegOpenKeyExW( hkey, subkey, 0, KEY_READ, &hkey );
3071 if (ret) return ret;
3074 ret = RegQueryInfoKeyW( hkey, NULL, NULL, NULL, NULL, &max_subkey,
3075 NULL, NULL, &max_name, NULL, NULL, NULL );
3076 if (ret)
3077 goto cleanup;
3079 max_name = max( max_subkey, max_name ) + 1;
3080 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
3082 ret = ERROR_NOT_ENOUGH_MEMORY;
3083 goto cleanup;
3086 /* Recursively delete subkeys */
3087 for (;;)
3089 name_size = max_name;
3090 ret = RegEnumKeyExW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
3091 if (ret == ERROR_NO_MORE_ITEMS) break;
3092 if (ret) goto cleanup;
3093 ret = RegDeleteTreeW( hkey, name_buf );
3094 if (ret) goto cleanup;
3097 /* Delete the key itself */
3098 if (subkey && *subkey)
3100 ret = RegDeleteKeyExW( hkey, L"", 0, 0 );
3101 goto cleanup;
3104 /* Delete values */
3105 for (;;)
3107 name_size = max_name;
3108 ret = RegEnumValueW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
3109 if (ret == ERROR_NO_MORE_ITEMS) break;
3110 if (ret) goto cleanup;
3111 ret = RegDeleteValueW( hkey, name_buf );
3112 if (ret) goto cleanup;
3115 ret = ERROR_SUCCESS;
3117 cleanup:
3118 heap_free( name_buf );
3119 if (subkey && *subkey)
3120 RegCloseKey( hkey );
3121 return ret;
3125 /******************************************************************************
3126 * RegDeleteTreeA (kernelbase.@)
3129 LSTATUS WINAPI RegDeleteTreeA( HKEY hkey, const char *subkey )
3131 UNICODE_STRING subkeyW;
3132 LONG ret;
3134 if (subkey) RtlCreateUnicodeStringFromAsciiz( &subkeyW, subkey );
3135 else subkeyW.Buffer = NULL;
3136 ret = RegDeleteTreeW( hkey, subkeyW.Buffer );
3137 RtlFreeUnicodeString( &subkeyW );
3138 return ret;
3142 /******************************************************************************
3143 * RegCopyTreeW (kernelbase.@)
3146 LSTATUS WINAPI RegCopyTreeW( HKEY hsrc, const WCHAR *subkey, HKEY hdst )
3148 DWORD name_size, max_name;
3149 DWORD value_size, max_value;
3150 DWORD max_subkey, i, type;
3151 WCHAR *name_buf = NULL;
3152 BYTE *value_buf = NULL;
3153 HKEY hkey;
3154 LONG ret;
3156 TRACE( "(%p, %s, %p)\n", hsrc, debugstr_w(subkey), hdst );
3158 if (subkey)
3160 ret = RegOpenKeyExW( hsrc, subkey, 0, KEY_READ, &hsrc );
3161 if (ret) return ret;
3164 ret = RegQueryInfoKeyW( hsrc, NULL, NULL, NULL, NULL, &max_subkey,
3165 NULL, NULL, &max_name, &max_value, NULL, NULL );
3166 if (ret)
3167 goto cleanup;
3169 max_name = max( max_subkey, max_name ) + 1;
3170 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
3172 ret = ERROR_NOT_ENOUGH_MEMORY;
3173 goto cleanup;
3176 if (!(value_buf = heap_alloc( max_value )))
3178 ret = ERROR_NOT_ENOUGH_MEMORY;
3179 goto cleanup;
3182 /* Copy values */
3183 for (i = 0;; i++)
3185 name_size = max_name;
3186 value_size = max_value;
3187 ret = RegEnumValueW( hsrc, i, name_buf, &name_size, NULL, &type, value_buf, &value_size );
3188 if (ret == ERROR_NO_MORE_ITEMS) break;
3189 if (ret) goto cleanup;
3190 ret = RegSetValueExW( hdst, name_buf, 0, type, value_buf, value_size );
3191 if (ret) goto cleanup;
3194 /* Recursively copy subkeys */
3195 for (i = 0;; i++)
3197 name_size = max_name;
3198 ret = RegEnumKeyExW( hsrc, i, name_buf, &name_size, NULL, NULL, NULL, NULL );
3199 if (ret == ERROR_NO_MORE_ITEMS) break;
3200 if (ret) goto cleanup;
3201 ret = RegCreateKeyExW( hdst, name_buf, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL );
3202 if (ret) goto cleanup;
3203 ret = RegCopyTreeW( hsrc, name_buf, hkey );
3204 RegCloseKey( hkey );
3205 if (ret) goto cleanup;
3208 ret = ERROR_SUCCESS;
3210 cleanup:
3211 heap_free( name_buf );
3212 heap_free( value_buf );
3213 if (subkey)
3214 RegCloseKey( hsrc );
3215 return ret;
3219 /******************************************************************************
3220 * RegLoadAppKeyA (kernelbase.@)
3223 LSTATUS WINAPI RegLoadAppKeyA(const char *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3225 FIXME("%s %p %lu %lu %lu: stub\n", wine_dbgstr_a(file), result, sam, options, reserved);
3227 if (!file || reserved)
3228 return ERROR_INVALID_PARAMETER;
3230 *result = (HKEY)0xdeadbeef;
3231 return ERROR_SUCCESS;
3234 /******************************************************************************
3235 * RegLoadAppKeyW (kernelbase.@)
3238 LSTATUS WINAPI RegLoadAppKeyW(const WCHAR *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3240 FIXME("%s %p %lu %lu %lu: stub\n", wine_dbgstr_w(file), result, sam, options, reserved);
3242 if (!file || reserved)
3243 return ERROR_INVALID_PARAMETER;
3245 *result = (HKEY)0xdeadbeef;
3246 return ERROR_SUCCESS;
3250 /***********************************************************************
3251 * DnsHostnameToComputerNameExW (kernelbase.@)
3253 * FIXME: how is this different from the non-Ex function?
3255 BOOL WINAPI DECLSPEC_HOTPATCH DnsHostnameToComputerNameExW( const WCHAR *hostname, WCHAR *computername,
3256 DWORD *size )
3258 static const WCHAR allowed[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&')(-_{}";
3259 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3260 DWORD i, len;
3262 lstrcpynW( buffer, hostname, MAX_COMPUTERNAME_LENGTH + 1 );
3263 len = lstrlenW( buffer );
3264 if (*size < len + 1)
3266 *size = len;
3267 SetLastError( ERROR_MORE_DATA );
3268 return FALSE;
3270 *size = len;
3271 if (!computername) return FALSE;
3272 for (i = 0; i < len; i++)
3274 if (buffer[i] >= 'a' && buffer[i] <= 'z') computername[i] = buffer[i] + 'A' - 'a';
3275 else computername[i] = wcschr( allowed, buffer[i] ) ? buffer[i] : '_';
3277 computername[len] = 0;
3278 return TRUE;
3282 /***********************************************************************
3283 * GetComputerNameExA (kernelbase.@)
3285 BOOL WINAPI GetComputerNameExA( COMPUTER_NAME_FORMAT type, char *name, DWORD *len )
3287 BOOL ret = FALSE;
3288 DWORD lenA, lenW = 0;
3289 WCHAR *buffer;
3291 GetComputerNameExW( type, NULL, &lenW );
3292 if (GetLastError() != ERROR_MORE_DATA) return FALSE;
3294 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
3296 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3297 return FALSE;
3299 if (GetComputerNameExW( type, buffer, &lenW ))
3301 lenA = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
3302 if (lenA > *len)
3304 *len = lenA;
3305 SetLastError( ERROR_MORE_DATA );
3307 else
3309 WideCharToMultiByte( CP_ACP, 0, buffer, -1, name, *len, NULL, NULL );
3310 *len = lenA - 1;
3311 ret = TRUE;
3314 HeapFree( GetProcessHeap(), 0, buffer );
3315 return ret;
3319 /***********************************************************************
3320 * GetComputerNameExW (kernelbase.@)
3322 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, WCHAR *name, DWORD *len )
3324 const WCHAR *keyname, *valuename;
3325 LRESULT ret;
3326 HKEY key;
3328 switch (type)
3330 case ComputerNameNetBIOS:
3331 case ComputerNamePhysicalNetBIOS:
3332 keyname = L"System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName";
3333 valuename = L"ComputerName";
3334 break;
3335 case ComputerNameDnsHostname:
3336 case ComputerNamePhysicalDnsHostname:
3337 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3338 valuename = L"Hostname";
3339 break;
3340 case ComputerNameDnsDomain:
3341 case ComputerNamePhysicalDnsDomain:
3342 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3343 valuename = L"Domain";
3344 break;
3345 case ComputerNameDnsFullyQualified:
3346 case ComputerNamePhysicalDnsFullyQualified:
3348 WCHAR *domain, buffer[256];
3349 DWORD size = ARRAY_SIZE(buffer) - 1;
3351 if (!GetComputerNameExW( ComputerNameDnsHostname, buffer, &size )) return FALSE;
3352 domain = buffer + lstrlenW(buffer);
3353 *domain++ = '.';
3354 size = ARRAY_SIZE(buffer) - (domain - buffer);
3355 if (!GetComputerNameExW( ComputerNameDnsDomain, domain, &size )) return FALSE;
3356 if (!*domain) domain[-1] = 0;
3357 size = lstrlenW(buffer);
3358 if (name && size < *len)
3360 if (name) lstrcpyW( name, buffer );
3361 *len = size;
3362 return TRUE;
3364 *len = size + 1;
3365 SetLastError( ERROR_MORE_DATA );
3366 return FALSE;
3368 default:
3369 SetLastError( ERROR_INVALID_PARAMETER );
3370 return FALSE;
3373 if (!(ret = RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &key )))
3375 DWORD size = *len * sizeof(WCHAR);
3376 ret = RegQueryValueExW( key, valuename, NULL, NULL, (BYTE *)name, &size );
3377 if (!name) ret = ERROR_MORE_DATA;
3378 else if (!ret) size -= sizeof(WCHAR);
3379 *len = size / sizeof(WCHAR);
3380 RegCloseKey( key );
3382 TRACE("-> %Iu %s\n", ret, debugstr_w(name) );
3383 if (ret) SetLastError( ret );
3384 return !ret;
3388 /***********************************************************************
3389 * SetComputerNameA (kernelbase.@)
3391 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameA( const char *name )
3393 BOOL ret;
3394 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3395 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3397 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3398 ret = SetComputerNameExW( ComputerNamePhysicalNetBIOS, nameW );
3399 HeapFree( GetProcessHeap(), 0, nameW );
3400 return ret;
3404 /***********************************************************************
3405 * SetComputerNameW (kernelbase.@)
3407 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameW( const WCHAR *name )
3409 return SetComputerNameExW( ComputerNamePhysicalNetBIOS, name );
3413 /***********************************************************************
3414 * SetComputerNameExA (kernelbase.@)
3416 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExA( COMPUTER_NAME_FORMAT type, const char *name )
3418 BOOL ret;
3419 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3420 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3422 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3423 ret = SetComputerNameExW( type, nameW );
3424 HeapFree( GetProcessHeap(), 0, nameW );
3425 return ret;
3429 /***********************************************************************
3430 * SetComputerNameExW (kernelbase.@)
3432 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExW( COMPUTER_NAME_FORMAT type, const WCHAR *name )
3434 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3435 DWORD size;
3436 HKEY key;
3437 LRESULT ret;
3439 TRACE( "%u %s\n", type, debugstr_w( name ));
3441 switch (type)
3443 case ComputerNameDnsHostname:
3444 case ComputerNamePhysicalDnsHostname:
3445 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3446 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3447 if (ret) break;
3448 ret = RegSetValueExW( key, L"Hostname", 0, REG_SZ,
3449 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3450 RegCloseKey( key );
3451 /* fall through */
3453 case ComputerNameNetBIOS:
3454 case ComputerNamePhysicalNetBIOS:
3455 /* @@ Wine registry key: HKCU\Software\Wine\Network */
3456 if (!RegOpenKeyExW( HKEY_CURRENT_USER, L"Software\\Wine\\Network", 0, KEY_READ, &key ))
3458 BOOL use_dns = TRUE;
3459 size = sizeof(buffer);
3460 if (!RegQueryValueExW( key, L"UseDnsComputerName", NULL, NULL, (BYTE *)buffer, &size ))
3461 use_dns = IS_OPTION_TRUE( buffer[0] );
3462 RegCloseKey( key );
3463 if (!use_dns)
3465 ret = ERROR_ACCESS_DENIED;
3466 break;
3469 size = ARRAY_SIZE( buffer );
3470 if (!DnsHostnameToComputerNameExW( name, buffer, &size )) return FALSE;
3471 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
3472 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3473 if (ret) break;
3474 ret = RegSetValueExW( key, L"ComputerName", 0, REG_SZ,
3475 (BYTE *)buffer, (lstrlenW(buffer) + 1) * sizeof(WCHAR) );
3476 RegCloseKey( key );
3477 break;
3479 case ComputerNameDnsDomain:
3480 case ComputerNamePhysicalDnsDomain:
3481 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3482 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3483 if (ret) break;
3484 ret = RegSetValueExW( key, L"Domain", 0, REG_SZ,
3485 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3486 RegCloseKey( key );
3487 break;
3488 default:
3489 ret = ERROR_INVALID_PARAMETER;
3490 break;
3492 if (ret) SetLastError( ret );
3493 return !ret;
3496 struct USKEY
3498 HKEY HKCUstart; /* Start key in CU hive */
3499 HKEY HKCUkey; /* Opened key in CU hive */
3500 HKEY HKLMstart; /* Start key in LM hive */
3501 HKEY HKLMkey; /* Opened key in LM hive */
3502 WCHAR path[MAX_PATH];
3505 LONG WINAPI SHRegCreateUSKeyA(LPCSTR path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3507 WCHAR *pathW;
3508 LONG ret;
3510 TRACE("%s, %#lx, %p, %p, %#lx\n", debugstr_a(path), samDesired, relative_key, new_uskey, flags);
3512 if (path)
3514 INT len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
3515 pathW = heap_alloc(len * sizeof(WCHAR));
3516 if (!pathW)
3517 return ERROR_NOT_ENOUGH_MEMORY;
3518 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
3520 else
3521 pathW = NULL;
3523 ret = SHRegCreateUSKeyW(pathW, samDesired, relative_key, new_uskey, flags);
3524 HeapFree(GetProcessHeap(), 0, pathW);
3525 return ret;
3528 static HKEY reg_duplicate_hkey(HKEY hKey)
3530 HKEY newKey = 0;
3532 RegOpenKeyExW(hKey, 0, 0, MAXIMUM_ALLOWED, &newKey);
3533 return newKey;
3536 static HKEY reg_get_hkey_from_huskey(HUSKEY hUSKey, BOOL is_hkcu)
3538 struct USKEY *mihk = hUSKey;
3539 HKEY test = hUSKey;
3541 if (test == HKEY_CLASSES_ROOT
3542 || test == HKEY_CURRENT_CONFIG
3543 || test == HKEY_CURRENT_USER
3544 || test == HKEY_DYN_DATA
3545 || test == HKEY_LOCAL_MACHINE
3546 || test == HKEY_PERFORMANCE_DATA
3547 || test == HKEY_USERS)
3548 /* FIXME: need to define for Win2k, ME, XP
3549 * (test == HKEY_PERFORMANCE_TEXT) ||
3550 * (test == HKEY_PERFORMANCE_NLSTEXT) ||
3553 return test;
3556 return is_hkcu ? mihk->HKCUkey : mihk->HKLMkey;
3559 LONG WINAPI SHRegCreateUSKeyW(const WCHAR *path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3561 LONG ret = ERROR_CALL_NOT_IMPLEMENTED;
3562 struct USKEY *ret_key;
3564 TRACE("%s, %#lx, %p, %p, %#lx\n", debugstr_w(path), samDesired, relative_key, new_uskey, flags);
3566 if (!new_uskey)
3567 return ERROR_INVALID_PARAMETER;
3569 *new_uskey = NULL;
3571 if (flags & ~SHREGSET_FORCE_HKCU)
3573 FIXME("unsupported flags 0x%08lx\n", flags);
3574 return ERROR_SUCCESS;
3577 ret_key = heap_alloc_zero(sizeof(*ret_key));
3578 lstrcpynW(ret_key->path, path, ARRAY_SIZE(ret_key->path));
3580 if (relative_key)
3582 ret_key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3583 ret_key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3585 else
3587 ret_key->HKCUstart = HKEY_CURRENT_USER;
3588 ret_key->HKLMstart = HKEY_LOCAL_MACHINE;
3591 if (flags & SHREGSET_FORCE_HKCU)
3593 ret = RegCreateKeyExW(ret_key->HKCUstart, path, 0, NULL, 0, samDesired, NULL, &ret_key->HKCUkey, NULL);
3594 if (ret == ERROR_SUCCESS)
3595 *new_uskey = ret_key;
3596 else
3597 heap_free(ret_key);
3600 return ret;
3603 LONG WINAPI SHRegCloseUSKey(HUSKEY hUSKey)
3605 struct USKEY *key = hUSKey;
3606 LONG ret = ERROR_SUCCESS;
3608 if (!key)
3609 return ERROR_INVALID_PARAMETER;
3611 if (key->HKCUkey)
3612 ret = RegCloseKey(key->HKCUkey);
3613 if (key->HKCUstart && key->HKCUstart != HKEY_CURRENT_USER)
3614 ret = RegCloseKey(key->HKCUstart);
3615 if (key->HKLMkey)
3616 ret = RegCloseKey(key->HKLMkey);
3617 if (key->HKLMstart && key->HKLMstart != HKEY_LOCAL_MACHINE)
3618 ret = RegCloseKey(key->HKLMstart);
3620 heap_free(key);
3621 return ret;
3624 LONG WINAPI SHRegDeleteEmptyUSKeyA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3626 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3627 return ERROR_SUCCESS;
3630 LONG WINAPI SHRegDeleteEmptyUSKeyW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3632 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3633 return ERROR_SUCCESS;
3636 LONG WINAPI SHRegDeleteUSValueA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3638 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3639 return ERROR_SUCCESS;
3642 LONG WINAPI SHRegDeleteUSValueW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3644 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3645 return ERROR_SUCCESS;
3648 LONG WINAPI SHRegEnumUSValueA(HUSKEY hUSKey, DWORD index, char *value_name, DWORD *value_name_len, DWORD *type,
3649 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3651 HKEY dokey;
3653 TRACE("%p, %#lx, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3655 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3656 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3658 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3659 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3661 FIXME("no support for SHREGENUM_BOTH\n");
3662 return ERROR_INVALID_FUNCTION;
3665 LONG WINAPI SHRegEnumUSValueW(HUSKEY hUSKey, DWORD index, WCHAR *value_name, DWORD *value_name_len, DWORD *type,
3666 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3668 HKEY dokey;
3670 TRACE("%p, %#lx, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3672 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3673 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3675 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3676 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3678 FIXME("no support for SHREGENUM_BOTH\n");
3679 return ERROR_INVALID_FUNCTION;
3682 LONG WINAPI SHRegEnumUSKeyA(HUSKEY hUSKey, DWORD index, char *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3684 HKEY dokey;
3686 TRACE("%p, %ld, %p, %p(%ld), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3688 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3689 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3691 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3692 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3694 FIXME("no support for SHREGENUM_BOTH\n");
3695 return ERROR_INVALID_FUNCTION;
3698 LONG WINAPI SHRegEnumUSKeyW(HUSKEY hUSKey, DWORD index, WCHAR *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3700 HKEY dokey;
3702 TRACE("%p, %ld, %p, %p(%ld), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3704 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3705 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3707 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3708 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3710 FIXME("no support for SHREGENUM_BOTH\n");
3711 return ERROR_INVALID_FUNCTION;
3714 LONG WINAPI SHRegOpenUSKeyA(const char *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3716 WCHAR pathW[MAX_PATH];
3718 if (path)
3719 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, ARRAY_SIZE(pathW));
3721 return SHRegOpenUSKeyW(path ? pathW : NULL, access_mask, relative_key, uskey, ignore_hkcu);
3724 LONG WINAPI SHRegOpenUSKeyW(const WCHAR *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3726 LONG ret2, ret1 = ~ERROR_SUCCESS;
3727 struct USKEY *key;
3729 TRACE("%s, %#lx, %p, %p, %d\n", debugstr_w(path), access_mask, relative_key, uskey, ignore_hkcu);
3731 if (uskey)
3732 *uskey = NULL;
3734 /* Create internal HUSKEY */
3735 key = heap_alloc_zero(sizeof(*key));
3736 lstrcpynW(key->path, path, ARRAY_SIZE(key->path));
3738 if (relative_key)
3740 key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3741 key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3743 /* FIXME: if either of these keys is NULL, create the start key from
3744 * the relative keys start+path
3747 else
3749 key->HKCUstart = HKEY_CURRENT_USER;
3750 key->HKLMstart = HKEY_LOCAL_MACHINE;
3753 if (!ignore_hkcu)
3755 ret1 = RegOpenKeyExW(key->HKCUstart, key->path, 0, access_mask, &key->HKCUkey);
3756 if (ret1)
3757 key->HKCUkey = 0;
3760 ret2 = RegOpenKeyExW(key->HKLMstart, key->path, 0, access_mask, &key->HKLMkey);
3761 if (ret2)
3762 key->HKLMkey = 0;
3764 if (ret1 || ret2)
3765 TRACE("one or more opens failed: HKCU=%ld HKLM=%ld\n", ret1, ret2);
3767 if (ret1 && ret2)
3769 /* Neither open succeeded: fail */
3770 SHRegCloseUSKey(key);
3771 return ret2;
3774 TRACE("HUSKEY=%p\n", key);
3775 if (uskey)
3776 *uskey = key;
3778 return ERROR_SUCCESS;
3781 LONG WINAPI SHRegWriteUSValueA(HUSKEY hUSKey, const char *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3783 WCHAR valueW[MAX_PATH];
3785 if (value)
3786 MultiByteToWideChar(CP_ACP, 0, value, -1, valueW, ARRAY_SIZE(valueW));
3788 return SHRegWriteUSValueW(hUSKey, value ? valueW : NULL, type, data, data_len, flags);
3791 LONG WINAPI SHRegWriteUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3793 struct USKEY *hKey = hUSKey;
3794 LONG ret = ERROR_SUCCESS;
3795 DWORD dummy;
3797 TRACE("%p, %s, %ld, %p, %ld, %#lx\n", hUSKey, debugstr_w(value), type, data, data_len, flags);
3799 __TRY
3801 dummy = hKey->HKCUkey || hKey->HKLMkey;
3803 __EXCEPT_PAGE_FAULT
3805 return ERROR_INVALID_PARAMETER;
3807 __ENDTRY
3808 if (!(flags & (SHREGSET_FORCE_HKCU|SHREGSET_FORCE_HKLM))) return ERROR_INVALID_PARAMETER;
3810 if (flags & (SHREGSET_FORCE_HKCU | SHREGSET_HKCU))
3812 if (!hKey->HKCUkey)
3814 /* Create the key */
3815 ret = RegCreateKeyExW(hKey->HKCUstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3816 MAXIMUM_ALLOWED, NULL, &hKey->HKCUkey, NULL);
3817 TRACE("Creating HKCU key, ret = %ld\n", ret);
3818 if (ret && (flags & SHREGSET_FORCE_HKCU))
3820 hKey->HKCUkey = 0;
3821 return ret;
3825 if (!ret)
3827 if ((flags & SHREGSET_FORCE_HKCU) || RegQueryValueExW(hKey->HKCUkey, value, NULL, NULL, NULL, &dummy))
3829 /* Doesn't exist or we are forcing: Write value */
3830 ret = RegSetValueExW(hKey->HKCUkey, value, 0, type, data, data_len);
3831 TRACE("Writing HKCU value, ret = %ld\n", ret);
3836 if (flags & (SHREGSET_FORCE_HKLM | SHREGSET_HKLM))
3838 if (!hKey->HKLMkey)
3840 /* Create the key */
3841 ret = RegCreateKeyExW(hKey->HKLMstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3842 MAXIMUM_ALLOWED, NULL, &hKey->HKLMkey, NULL);
3843 TRACE("Creating HKLM key, ret = %ld\n", ret);
3844 if (ret && (flags & (SHREGSET_FORCE_HKLM)))
3846 hKey->HKLMkey = 0;
3847 return ret;
3851 if (!ret)
3853 if ((flags & SHREGSET_FORCE_HKLM) || RegQueryValueExW(hKey->HKLMkey, value, NULL, NULL, NULL, &dummy))
3855 /* Doesn't exist or we are forcing: Write value */
3856 ret = RegSetValueExW(hKey->HKLMkey, value, 0, type, data, data_len);
3857 TRACE("Writing HKLM value, ret = %ld\n", ret);
3862 return ret;
3865 LONG WINAPI SHRegSetUSValueA(const char *subkey, const char *value, DWORD type, void *data, DWORD data_len,
3866 DWORD flags)
3868 BOOL ignore_hkcu;
3869 HUSKEY hkey;
3870 LONG ret;
3872 TRACE("%s, %s, %ld, %p, %ld, %#lx\n", debugstr_a(subkey), debugstr_a(value), type, data, data_len, flags);
3874 if (!data)
3875 return ERROR_INVALID_FUNCTION;
3877 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3879 ret = SHRegOpenUSKeyA(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3880 if (ret == ERROR_SUCCESS)
3882 ret = SHRegWriteUSValueA(hkey, value, type, data, data_len, flags);
3883 SHRegCloseUSKey(hkey);
3886 return ret;
3889 LONG WINAPI SHRegSetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD type, void *data, DWORD data_len,
3890 DWORD flags)
3892 BOOL ignore_hkcu;
3893 HUSKEY hkey;
3894 LONG ret;
3896 TRACE("%s, %s, %ld, %p, %ld, %#lx\n", debugstr_w(subkey), debugstr_w(value), type, data, data_len, flags);
3898 if (!data)
3899 return ERROR_INVALID_FUNCTION;
3901 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3903 ret = SHRegOpenUSKeyW(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3904 if (ret == ERROR_SUCCESS)
3906 ret = SHRegWriteUSValueW(hkey, value, type, data, data_len, flags);
3907 SHRegCloseUSKey(hkey);
3910 return ret;
3913 LONG WINAPI SHRegQueryInfoUSKeyA(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3914 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3916 HKEY dokey;
3917 LONG ret;
3919 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3921 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3923 ret = RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3924 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3925 return ret;
3928 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3930 return RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3933 return ERROR_INVALID_FUNCTION;
3936 LONG WINAPI SHRegQueryInfoUSKeyW(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3937 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3939 HKEY dokey;
3940 LONG ret;
3942 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3944 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3946 ret = RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3947 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3948 return ret;
3951 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3953 return RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3956 return ERROR_INVALID_FUNCTION;
3959 LONG WINAPI SHRegQueryUSValueA(HUSKEY hUSKey, const char *value, DWORD *type, void *data, DWORD *data_len,
3960 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3962 LONG ret = ~ERROR_SUCCESS;
3963 DWORD move_len;
3964 HKEY dokey;
3966 /* If user wants HKCU, and it exists, then try it */
3967 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3969 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3970 TRACE("HKCU RegQueryValue returned %ld\n", ret);
3973 /* If HKCU did not work and HKLM exists, then try it */
3974 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3976 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3977 TRACE("HKLM RegQueryValue returned %ld\n", ret);
3980 /* If neither worked, and default data exists, then use it */
3981 if (ret != ERROR_SUCCESS)
3983 if (default_data && default_data_len)
3985 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3986 memmove(data, default_data, move_len);
3987 *data_len = move_len;
3988 TRACE("setting default data\n");
3989 ret = ERROR_SUCCESS;
3993 return ret;
3996 LONG WINAPI SHRegQueryUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3997 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3999 LONG ret = ~ERROR_SUCCESS;
4000 DWORD move_len;
4001 HKEY dokey;
4003 /* If user wants HKCU, and it exists, then try it */
4004 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
4006 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
4007 TRACE("HKCU RegQueryValue returned %ld\n", ret);
4010 /* If HKCU did not work and HKLM exists, then try it */
4011 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
4013 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
4014 TRACE("HKLM RegQueryValue returned %ld\n", ret);
4017 /* If neither worked, and default data exists, then use it */
4018 if (ret != ERROR_SUCCESS)
4020 if (default_data && default_data_len)
4022 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
4023 memmove(data, default_data, move_len);
4024 *data_len = move_len;
4025 TRACE("setting default data\n");
4026 ret = ERROR_SUCCESS;
4030 return ret;
4033 LONG WINAPI SHRegGetUSValueA(const char *subkey, const char *value, DWORD *type, void *data, DWORD *data_len,
4034 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
4036 HUSKEY myhuskey;
4037 LONG ret;
4039 if (!data || !data_len)
4040 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
4042 TRACE("%s, %s, %ld\n", debugstr_a(subkey), debugstr_a(value), *data_len);
4044 ret = SHRegOpenUSKeyA(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
4045 if (!ret)
4047 ret = SHRegQueryUSValueA(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
4048 SHRegCloseUSKey(myhuskey);
4051 return ret;
4054 LONG WINAPI SHRegGetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
4055 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
4057 HUSKEY myhuskey;
4058 LONG ret;
4060 if (!data || !data_len)
4061 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
4063 TRACE("%s, %s, %ld\n", debugstr_w(subkey), debugstr_w(value), *data_len);
4065 ret = SHRegOpenUSKeyW(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
4066 if (!ret)
4068 ret = SHRegQueryUSValueW(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
4069 SHRegCloseUSKey(myhuskey);
4072 return ret;
4075 BOOL WINAPI SHRegGetBoolUSValueA(const char *subkey, const char *value, BOOL ignore_hkcu, BOOL default_value)
4077 BOOL ret = default_value;
4078 DWORD type, datalen;
4079 char data[10];
4081 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), ignore_hkcu);
4083 datalen = ARRAY_SIZE(data) - 1;
4084 if (!SHRegGetUSValueA(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
4086 switch (type)
4088 case REG_SZ:
4089 data[9] = '\0';
4090 if (!lstrcmpiA(data, "YES") || !lstrcmpiA(data, "TRUE"))
4091 ret = TRUE;
4092 else if (!lstrcmpiA(data, "NO") || !lstrcmpiA(data, "FALSE"))
4093 ret = FALSE;
4094 break;
4095 case REG_DWORD:
4096 ret = *(DWORD *)data != 0;
4097 break;
4098 case REG_BINARY:
4099 if (datalen == 1)
4101 ret = !!data[0];
4102 break;
4104 default:
4105 FIXME("Unsupported registry data type %ld\n", type);
4106 ret = FALSE;
4108 TRACE("got value (type=%ld), returning %d\n", type, ret);
4110 else
4111 TRACE("returning default value %d\n", ret);
4113 return ret;
4116 BOOL WINAPI SHRegGetBoolUSValueW(const WCHAR *subkey, const WCHAR *value, BOOL ignore_hkcu, BOOL default_value)
4118 BOOL ret = default_value;
4119 DWORD type, datalen;
4120 WCHAR data[10];
4122 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), ignore_hkcu);
4124 datalen = (ARRAY_SIZE(data) - 1) * sizeof(WCHAR);
4125 if (!SHRegGetUSValueW(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
4127 switch (type)
4129 case REG_SZ:
4130 data[9] = '\0';
4131 if (!lstrcmpiW(data, L"yes") || !lstrcmpiW(data, L"true"))
4132 ret = TRUE;
4133 else if (!lstrcmpiW(data, L"no") || !lstrcmpiW(data, L"false"))
4134 ret = FALSE;
4135 break;
4136 case REG_DWORD:
4137 ret = *(DWORD *)data != 0;
4138 break;
4139 case REG_BINARY:
4140 if (datalen == 1)
4142 ret = !!data[0];
4143 break;
4145 default:
4146 FIXME("Unsupported registry data type %ld\n", type);
4147 ret = FALSE;
4149 TRACE("got value (type=%ld), returning %d\n", type, ret);
4151 else
4152 TRACE("returning default value %d\n", ret);
4154 return ret;