kernelbase: Do not map HKEY_PERFORMANCE_DATA to \Registry\PerfData.
[wine.git] / dlls / kernelbase / registry.c
blob3a551ecbec562259623e9236c4c5f9411d727f05
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 return (name->Length == 11 * sizeof(WCHAR) && !wcsnicmp( name->Buffer, L"Wow6432Node", 11 ));
106 /* open the Wow6432Node subkey of the specified key */
107 static HANDLE open_wow6432node( HANDLE key )
109 OBJECT_ATTRIBUTES attr;
110 UNICODE_STRING nameW;
111 HANDLE ret;
113 attr.Length = sizeof(attr);
114 attr.RootDirectory = key;
115 attr.ObjectName = &nameW;
116 attr.Attributes = 0;
117 attr.SecurityDescriptor = NULL;
118 attr.SecurityQualityOfService = NULL;
119 RtlInitUnicodeString( &nameW, L"Wow6432Node" );
120 if (NtOpenKeyEx( &ret, MAXIMUM_ALLOWED, &attr, 0 )) ret = 0;
121 return ret;
124 static HKEY get_perflib_key( HANDLE key )
126 static const WCHAR performance_text[] =
127 L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009";
128 char buffer[200];
129 OBJECT_NAME_INFORMATION *info = (OBJECT_NAME_INFORMATION *)buffer;
131 if (!NtQueryObject( key, ObjectNameInformation, buffer, sizeof(buffer), NULL ))
133 if (!wcsicmp( info->Name.Buffer, performance_text ))
135 NtClose( key );
136 return HKEY_PERFORMANCE_TEXT;
140 return key;
143 /* wrapper for NtCreateKey that creates the key recursively if necessary */
144 static NTSTATUS create_key( HKEY *retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
145 const UNICODE_STRING *class, ULONG options, PULONG dispos )
147 BOOL force_wow32 = is_win64 && (access & KEY_WOW64_32KEY);
148 NTSTATUS status = STATUS_OBJECT_NAME_NOT_FOUND;
149 HANDLE subkey, root = attr->RootDirectory;
151 if (!force_wow32) status = NtCreateKey( &subkey, access, attr, 0, class, options, dispos );
153 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
155 WCHAR *buffer = attr->ObjectName->Buffer;
156 DWORD attrs, pos = 0, i = 0, len = attr->ObjectName->Length / sizeof(WCHAR);
157 UNICODE_STRING str;
159 /* don't try to create registry root */
160 if (!attr->RootDirectory && len > 10 && !wcsnicmp( buffer, L"\\Registry\\", 10 )) i += 10;
162 while (i < len && buffer[i] != '\\') i++;
163 if (i == len && !force_wow32) return status;
165 attrs = attr->Attributes;
166 attr->ObjectName = &str;
168 for (;;)
170 str.Buffer = buffer + pos;
171 str.Length = (i - pos) * sizeof(WCHAR);
172 if (force_wow32 && pos)
174 if (is_wow6432node( &str )) force_wow32 = FALSE;
175 else if ((subkey = open_wow6432node( attr->RootDirectory )))
177 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
178 attr->RootDirectory = subkey;
179 force_wow32 = FALSE;
182 if (i == len)
184 attr->Attributes = attrs;
185 status = NtCreateKey( &subkey, access, attr, 0, class, options, dispos );
187 else
189 attr->Attributes = attrs & ~OBJ_OPENLINK;
190 status = NtCreateKey( &subkey, access, attr, 0, class,
191 options & ~REG_OPTION_CREATE_LINK, dispos );
193 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
194 if (!NT_SUCCESS(status)) return status;
195 if (i == len) break;
196 attr->RootDirectory = subkey;
197 while (i < len && buffer[i] == '\\') i++;
198 pos = i;
199 while (i < len && buffer[i] != '\\') i++;
202 attr->RootDirectory = subkey;
203 if (force_wow32 && (subkey = open_wow6432node( attr->RootDirectory )))
205 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
206 attr->RootDirectory = subkey;
208 if (status == STATUS_PREDEFINED_HANDLE)
210 attr->RootDirectory = get_perflib_key( attr->RootDirectory );
211 status = STATUS_SUCCESS;
213 *retkey = attr->RootDirectory;
214 return status;
217 /* wrapper for NtOpenKeyEx to handle Wow6432 nodes */
218 static NTSTATUS open_key( HKEY *retkey, DWORD options, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
220 NTSTATUS status;
221 BOOL force_wow32 = is_win64 && (access & KEY_WOW64_32KEY);
222 HANDLE subkey, root = attr->RootDirectory;
223 WCHAR *buffer = attr->ObjectName->Buffer;
224 DWORD pos = 0, i = 0, len = attr->ObjectName->Length / sizeof(WCHAR);
225 UNICODE_STRING str;
227 *retkey = NULL;
229 if (!force_wow32)
231 if (options & REG_OPTION_OPEN_LINK) attr->Attributes |= OBJ_OPENLINK;
232 status = NtOpenKeyEx( (HANDLE *)retkey, access, attr, options );
233 if (status == STATUS_PREDEFINED_HANDLE)
235 *retkey = get_perflib_key( *retkey );
236 status = STATUS_SUCCESS;
238 return status;
241 if (len && buffer[0] == '\\') return STATUS_OBJECT_PATH_INVALID;
242 while (i < len && buffer[i] != '\\') i++;
243 attr->ObjectName = &str;
245 for (;;)
247 str.Buffer = buffer + pos;
248 str.Length = (i - pos) * sizeof(WCHAR);
249 if (force_wow32 && pos)
251 if (is_wow6432node( &str )) force_wow32 = FALSE;
252 else if ((subkey = open_wow6432node( attr->RootDirectory )))
254 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
255 attr->RootDirectory = subkey;
256 force_wow32 = FALSE;
259 if (i == len)
261 if (options & REG_OPTION_OPEN_LINK) attr->Attributes |= OBJ_OPENLINK;
262 status = NtOpenKeyEx( &subkey, access, attr, options );
264 else
266 if (!(options & REG_OPTION_OPEN_LINK)) attr->Attributes &= ~OBJ_OPENLINK;
267 status = NtOpenKeyEx( &subkey, access, attr, options & ~REG_OPTION_OPEN_LINK );
269 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
270 if (status) return status;
271 attr->RootDirectory = subkey;
272 if (i == len) break;
273 while (i < len && buffer[i] == '\\') i++;
274 pos = i;
275 while (i < len && buffer[i] != '\\') i++;
277 if (force_wow32 && (subkey = open_wow6432node( attr->RootDirectory )))
279 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
280 attr->RootDirectory = subkey;
282 if (status == STATUS_PREDEFINED_HANDLE)
284 attr->RootDirectory = get_perflib_key( attr->RootDirectory );
285 status = STATUS_SUCCESS;
287 *retkey = attr->RootDirectory;
288 return status;
291 /* create one of the HKEY_* special root keys */
292 static HKEY create_special_root_hkey( HKEY hkey, DWORD access )
294 HKEY ret = 0;
295 int idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
297 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CURRENT_USER))
299 if (RtlOpenCurrentUser( access, (HANDLE *)&hkey )) return 0;
300 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
302 else
304 OBJECT_ATTRIBUTES attr;
305 UNICODE_STRING name;
307 attr.Length = sizeof(attr);
308 attr.RootDirectory = 0;
309 attr.ObjectName = &name;
310 attr.Attributes = 0;
311 attr.SecurityDescriptor = NULL;
312 attr.SecurityQualityOfService = NULL;
313 RtlInitUnicodeString( &name, root_key_names[idx] );
314 if (create_key( &hkey, access, &attr, NULL, 0, NULL )) return 0;
315 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
318 if (!cache_disabled[idx] && !(access & (KEY_WOW64_64KEY | KEY_WOW64_32KEY)))
320 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
321 ret = hkey;
322 else
323 NtClose( hkey ); /* somebody beat us to it */
325 else
326 ret = hkey;
327 return ret;
330 /* map the hkey from special root to normal key if necessary */
331 static inline HKEY get_special_root_hkey( HKEY hkey, REGSAM access )
333 unsigned int index = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
334 DWORD wow64_flags = access & (KEY_WOW64_32KEY | KEY_WOW64_64KEY);
336 switch (HandleToUlong(hkey))
338 case (LONG)(LONG_PTR)HKEY_CLASSES_ROOT:
339 if (wow64_flags)
340 return create_special_root_hkey( hkey, MAXIMUM_ALLOWED | wow64_flags );
341 /* fall through */
343 case (LONG)(LONG_PTR)HKEY_CURRENT_USER:
344 case (LONG)(LONG_PTR)HKEY_LOCAL_MACHINE:
345 case (LONG)(LONG_PTR)HKEY_USERS:
346 case (LONG)(LONG_PTR)HKEY_CURRENT_CONFIG:
347 case (LONG)(LONG_PTR)HKEY_DYN_DATA:
348 if (special_root_keys[index])
349 return special_root_keys[index];
350 return create_special_root_hkey( hkey, MAXIMUM_ALLOWED );
352 default:
353 return hkey;
358 /******************************************************************************
359 * RemapPredefinedHandleInternal (kernelbase.@)
361 NTSTATUS WINAPI RemapPredefinedHandleInternal( HKEY hkey, HKEY override )
363 HKEY old_key;
364 int idx;
366 TRACE("(%p %p)\n", hkey, override);
368 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
369 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
370 return STATUS_INVALID_HANDLE;
371 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
373 if (override)
375 NTSTATUS status = NtDuplicateObject( GetCurrentProcess(), override,
376 GetCurrentProcess(), (HANDLE *)&override,
377 0, 0, DUPLICATE_SAME_ACCESS );
378 if (status) return status;
381 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], override );
382 if (old_key) NtClose( old_key );
383 return STATUS_SUCCESS;
387 /******************************************************************************
388 * DisablePredefinedHandleTableInternal (kernelbase.@)
390 NTSTATUS WINAPI DisablePredefinedHandleTableInternal( HKEY hkey )
392 HKEY old_key;
393 int idx;
395 TRACE("(%p)\n", hkey);
397 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
398 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
399 return STATUS_INVALID_HANDLE;
400 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
402 cache_disabled[idx] = TRUE;
404 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], NULL );
405 if (old_key) NtClose( old_key );
406 return STATUS_SUCCESS;
410 /******************************************************************************
411 * RegCreateKeyExW (kernelbase.@)
413 * See RegCreateKeyExA.
415 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
416 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
417 PHKEY retkey, LPDWORD dispos )
419 OBJECT_ATTRIBUTES attr;
420 UNICODE_STRING nameW, classW;
422 if (reserved) return ERROR_INVALID_PARAMETER;
423 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
425 attr.Length = sizeof(attr);
426 attr.RootDirectory = hkey;
427 attr.ObjectName = &nameW;
428 attr.Attributes = 0;
429 attr.SecurityDescriptor = NULL;
430 attr.SecurityQualityOfService = NULL;
431 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
432 RtlInitUnicodeString( &nameW, name );
433 RtlInitUnicodeString( &classW, class );
435 return RtlNtStatusToDosError( create_key( retkey, access, &attr, &classW, options, dispos ) );
439 /******************************************************************************
440 * RegCreateKeyExA (kernelbase.@)
442 * Open a registry key, creating it if it doesn't exist.
444 * PARAMS
445 * hkey [I] Handle of the parent registry key
446 * name [I] Name of the new key to open or create
447 * reserved [I] Reserved, pass 0
448 * class [I] The object type of the new key
449 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
450 * access [I] Access level desired
451 * sa [I] Security attributes for the key
452 * retkey [O] Destination for the resulting handle
453 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
455 * RETURNS
456 * Success: ERROR_SUCCESS.
457 * Failure: A standard Win32 error code. retkey remains untouched.
459 * FIXME
460 * MAXIMUM_ALLOWED in access mask not supported by server
462 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
463 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
464 PHKEY retkey, LPDWORD dispos )
466 OBJECT_ATTRIBUTES attr;
467 UNICODE_STRING classW;
468 ANSI_STRING nameA, classA;
469 NTSTATUS status;
471 if (reserved) return ERROR_INVALID_PARAMETER;
472 if (!is_version_nt())
474 access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
475 if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
477 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
479 attr.Length = sizeof(attr);
480 attr.RootDirectory = hkey;
481 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
482 attr.Attributes = 0;
483 attr.SecurityDescriptor = NULL;
484 attr.SecurityQualityOfService = NULL;
485 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
486 RtlInitAnsiString( &nameA, name );
487 RtlInitAnsiString( &classA, class );
489 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
490 &nameA, FALSE )))
492 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
494 status = create_key( retkey, access, &attr, &classW, options, dispos );
495 RtlFreeUnicodeString( &classW );
498 return RtlNtStatusToDosError( status );
502 /******************************************************************************
503 * RegOpenKeyExW (kernelbase.@)
505 * See RegOpenKeyExA.
507 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD options, REGSAM access, PHKEY retkey )
509 OBJECT_ATTRIBUTES attr;
510 UNICODE_STRING nameW;
512 if (retkey && (!name || !name[0]) &&
513 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
514 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
516 *retkey = hkey;
517 return ERROR_SUCCESS;
520 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
521 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
523 if (!retkey) return ERROR_INVALID_PARAMETER;
524 *retkey = NULL;
525 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
527 attr.Length = sizeof(attr);
528 attr.RootDirectory = hkey;
529 attr.ObjectName = &nameW;
530 attr.Attributes = 0;
531 attr.SecurityDescriptor = NULL;
532 attr.SecurityQualityOfService = NULL;
533 RtlInitUnicodeString( &nameW, name );
534 return RtlNtStatusToDosError( open_key( retkey, options, access, &attr ) );
538 /******************************************************************************
539 * RegOpenKeyExA (kernelbase.@)
541 * Open a registry key.
543 * PARAMS
544 * hkey [I] Handle of open key
545 * name [I] Name of subkey to open
546 * options [I] Open options (can be set to REG_OPTION_OPEN_LINK)
547 * access [I] Security access mask
548 * retkey [O] Handle to open key
550 * RETURNS
551 * Success: ERROR_SUCCESS
552 * Failure: A standard Win32 error code. retkey is set to 0.
554 * NOTES
555 * Unlike RegCreateKeyExA(), this function will not create the key if it
556 * does not exist.
558 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD options, REGSAM access, PHKEY retkey )
560 OBJECT_ATTRIBUTES attr;
561 STRING nameA;
562 NTSTATUS status;
564 if (retkey && (!name || !name[0]) &&
565 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
566 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
568 *retkey = hkey;
569 return ERROR_SUCCESS;
572 if (!is_version_nt()) access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
573 else
575 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
576 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
579 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
581 attr.Length = sizeof(attr);
582 attr.RootDirectory = hkey;
583 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
584 attr.Attributes = 0;
585 attr.SecurityDescriptor = NULL;
586 attr.SecurityQualityOfService = NULL;
588 RtlInitAnsiString( &nameA, name );
589 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
590 &nameA, FALSE )))
592 status = open_key( retkey, options, access, &attr );
594 return RtlNtStatusToDosError( status );
598 /******************************************************************************
599 * RegOpenCurrentUser (kernelbase.@)
601 * Get a handle to the HKEY_CURRENT_USER key for the user
602 * the current thread is impersonating.
604 * PARAMS
605 * access [I] Desired access rights to the key
606 * retkey [O] Handle to the opened key
608 * RETURNS
609 * Success: ERROR_SUCCESS
610 * Failure: nonzero error code from Winerror.h
612 * FIXME
613 * This function is supposed to retrieve a handle to the
614 * HKEY_CURRENT_USER for the user the current thread is impersonating.
615 * Since Wine does not currently allow threads to impersonate other users,
616 * this stub should work fine.
618 LSTATUS WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
620 void *data[20];
621 TOKEN_USER *info = (TOKEN_USER *)data;
622 HANDLE token;
623 DWORD len = 0;
625 /* get current user SID */
626 if (OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, FALSE, &token ))
628 len = sizeof(data);
629 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
630 CloseHandle( token );
632 if (!len)
634 ImpersonateSelf(SecurityIdentification);
635 if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &token))
637 len = sizeof(data);
638 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
639 CloseHandle( token );
641 RevertToSelf();
644 if (len)
646 WCHAR buffer[200];
647 UNICODE_STRING string = { 0, sizeof(buffer), buffer };
649 RtlConvertSidToUnicodeString( &string, info->User.Sid, FALSE );
650 return RegOpenKeyExW( HKEY_USERS, string.Buffer, 0, access, retkey );
653 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
658 /******************************************************************************
659 * RegEnumKeyExW (kernelbase.@)
661 * Enumerate subkeys of the specified open registry key.
663 * PARAMS
664 * hkey [I] Handle to key to enumerate
665 * index [I] Index of subkey to enumerate
666 * name [O] Buffer for subkey name
667 * name_len [O] Size of subkey buffer
668 * reserved [I] Reserved
669 * class [O] Buffer for class string
670 * class_len [O] Size of class buffer
671 * ft [O] Time key last written to
673 * RETURNS
674 * Success: ERROR_SUCCESS
675 * Failure: System error code. If there are no more subkeys available, the
676 * function returns ERROR_NO_MORE_ITEMS.
678 LSTATUS WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
679 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
681 NTSTATUS status;
682 char buffer[256], *buf_ptr = buffer;
683 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
684 DWORD total_size;
686 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
687 name_len ? *name_len : 0, reserved, class, class_len, ft );
689 if (reserved) return ERROR_INVALID_PARAMETER;
690 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
692 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
693 buffer, sizeof(buffer), &total_size );
695 while (status == STATUS_BUFFER_OVERFLOW)
697 /* retry with a dynamically allocated buffer */
698 if (buf_ptr != buffer) heap_free( buf_ptr );
699 if (!(buf_ptr = heap_alloc( total_size )))
700 return ERROR_NOT_ENOUGH_MEMORY;
701 info = (KEY_NODE_INFORMATION *)buf_ptr;
702 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
703 buf_ptr, total_size, &total_size );
706 if (!status)
708 DWORD len = info->NameLength / sizeof(WCHAR);
709 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
711 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
713 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
714 status = STATUS_BUFFER_OVERFLOW;
715 else
717 *name_len = len;
718 memcpy( name, info->Name, info->NameLength );
719 name[len] = 0;
720 if (class_len)
722 *class_len = cls_len;
723 if (class)
725 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
726 class[cls_len] = 0;
732 if (buf_ptr != buffer) heap_free( buf_ptr );
733 return RtlNtStatusToDosError( status );
737 /******************************************************************************
738 * RegEnumKeyExA (kernelbase.@)
740 * See RegEnumKeyExW.
742 LSTATUS WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
743 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
745 NTSTATUS status;
746 char buffer[256], *buf_ptr = buffer;
747 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
748 DWORD total_size;
750 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
751 name_len ? *name_len : 0, reserved, class, class_len, ft );
753 if (reserved) return ERROR_INVALID_PARAMETER;
754 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
756 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
757 buffer, sizeof(buffer), &total_size );
759 while (status == STATUS_BUFFER_OVERFLOW)
761 /* retry with a dynamically allocated buffer */
762 if (buf_ptr != buffer) heap_free( buf_ptr );
763 if (!(buf_ptr = heap_alloc( total_size )))
764 return ERROR_NOT_ENOUGH_MEMORY;
765 info = (KEY_NODE_INFORMATION *)buf_ptr;
766 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
767 buf_ptr, total_size, &total_size );
770 if (!status)
772 DWORD len, cls_len;
774 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
775 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
776 info->ClassLength );
777 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
779 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
780 status = STATUS_BUFFER_OVERFLOW;
781 else
783 *name_len = len;
784 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
785 name[len] = 0;
786 if (class_len)
788 *class_len = cls_len;
789 if (class)
791 RtlUnicodeToMultiByteN( class, cls_len, NULL,
792 (WCHAR *)(buf_ptr + info->ClassOffset),
793 info->ClassLength );
794 class[cls_len] = 0;
800 if (buf_ptr != buffer) heap_free( buf_ptr );
801 return RtlNtStatusToDosError( status );
805 /******************************************************************************
806 * RegQueryInfoKeyW (kernelbase.@)
808 * Retrieves information about the specified registry key.
810 * PARAMS
811 * hkey [I] Handle to key to query
812 * class [O] Buffer for class string
813 * class_len [O] Size of class string buffer
814 * reserved [I] Reserved
815 * subkeys [O] Buffer for number of subkeys
816 * max_subkey [O] Buffer for longest subkey name length
817 * max_class [O] Buffer for longest class string length
818 * values [O] Buffer for number of value entries
819 * max_value [O] Buffer for longest value name length
820 * max_data [O] Buffer for longest value data length
821 * security [O] Buffer for security descriptor length
822 * modif [O] Modification time
824 * RETURNS
825 * Success: ERROR_SUCCESS
826 * Failure: system error code.
828 * NOTES
829 * - win95 allows class to be valid and class_len to be NULL
830 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
831 * - both allow class to be NULL and class_len to be NULL
832 * (it's hard to test validity, so test !NULL instead)
834 LSTATUS WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
835 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
836 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
837 LPDWORD security, FILETIME *modif )
839 NTSTATUS status;
840 char buffer[256], *buf_ptr = buffer;
841 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
842 DWORD total_size;
844 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
845 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
847 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
848 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
850 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
851 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
853 if (class && class_len && *class_len)
855 /* retry with a dynamically allocated buffer */
856 while (status == STATUS_BUFFER_OVERFLOW)
858 if (buf_ptr != buffer) heap_free( buf_ptr );
859 if (!(buf_ptr = heap_alloc( total_size )))
860 return ERROR_NOT_ENOUGH_MEMORY;
861 info = (KEY_FULL_INFORMATION *)buf_ptr;
862 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
865 if (status) goto done;
867 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
869 status = STATUS_BUFFER_TOO_SMALL;
871 else
873 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
874 class[info->ClassLength/sizeof(WCHAR)] = 0;
877 else status = STATUS_SUCCESS;
879 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
880 if (subkeys) *subkeys = info->SubKeys;
881 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
882 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
883 if (values) *values = info->Values;
884 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
885 if (max_data) *max_data = info->MaxValueDataLen;
886 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
888 if (security)
890 FIXME( "security argument not supported.\n");
891 *security = 0;
894 done:
895 if (buf_ptr != buffer) heap_free( buf_ptr );
896 return RtlNtStatusToDosError( status );
900 /******************************************************************************
901 * RegQueryInfoKeyA (kernelbase.@)
903 * Retrieves information about a registry key.
905 * PARAMS
906 * hKey [I] Handle to an open key.
907 * lpClass [O] Class string of the key.
908 * lpcClass [I/O] size of lpClass.
909 * lpReserved [I] Reserved; must be NULL.
910 * lpcSubKeys [O] Number of subkeys contained by the key.
911 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
912 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
913 * class in TCHARS.
914 * lpcValues [O] Number of values associated with the key.
915 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
916 * lpcMaxValueLen [O] Longest data component among the key's values
917 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
918 * lpftLastWriteTime [O] FILETIME structure that is the last write time.
920 * RETURNS
921 * Success: ERROR_SUCCESS
922 * Failure: nonzero error code from Winerror.h
924 LSTATUS WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
925 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
926 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
927 LPDWORD security, FILETIME *modif )
929 NTSTATUS status;
930 char buffer[256], *buf_ptr = buffer;
931 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
932 DWORD total_size;
934 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
935 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
937 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
938 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
940 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
941 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
943 if (class || class_len)
945 /* retry with a dynamically allocated buffer */
946 while (status == STATUS_BUFFER_OVERFLOW)
948 if (buf_ptr != buffer) heap_free( buf_ptr );
949 if (!(buf_ptr = heap_alloc( total_size )))
950 return ERROR_NOT_ENOUGH_MEMORY;
951 info = (KEY_FULL_INFORMATION *)buf_ptr;
952 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
955 if (status) goto done;
957 if (class && class_len && *class_len)
959 DWORD len = *class_len;
960 RtlUnicodeToMultiByteN( class, len, class_len,
961 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
962 if (*class_len == len)
964 status = STATUS_BUFFER_OVERFLOW;
965 *class_len -= 1;
967 class[*class_len] = 0;
969 else if (class_len)
970 RtlUnicodeToMultiByteSize( class_len,
971 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
973 else status = STATUS_SUCCESS;
975 if (subkeys) *subkeys = info->SubKeys;
976 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
977 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
978 if (values) *values = info->Values;
979 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
980 if (max_data) *max_data = info->MaxValueDataLen;
981 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
983 if (security)
985 FIXME( "security argument not supported.\n");
986 *security = 0;
989 done:
990 if (buf_ptr != buffer) heap_free( buf_ptr );
991 return RtlNtStatusToDosError( status );
994 /******************************************************************************
995 * RegCloseKey (kernelbase.@)
997 * Close an open registry key.
999 * PARAMS
1000 * hkey [I] Handle of key to close
1002 * RETURNS
1003 * Success: ERROR_SUCCESS
1004 * Failure: Error code
1006 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCloseKey( HKEY hkey )
1008 if (!hkey) return ERROR_INVALID_HANDLE;
1009 if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
1010 return RtlNtStatusToDosError( NtClose( hkey ) );
1014 /******************************************************************************
1015 * RegDeleteKeyExW (kernelbase.@)
1017 LSTATUS WINAPI RegDeleteKeyExW( HKEY hkey, LPCWSTR name, REGSAM access, DWORD reserved )
1019 DWORD ret;
1020 HKEY tmp;
1022 if (!name) return ERROR_INVALID_PARAMETER;
1024 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
1026 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1027 if (!(ret = RegOpenKeyExW( hkey, name, 0, access | DELETE, &tmp )))
1029 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1030 RegCloseKey( tmp );
1032 TRACE("%s ret=%08x\n", debugstr_w(name), ret);
1033 return ret;
1037 /******************************************************************************
1038 * RegDeleteKeyExA (kernelbase.@)
1040 LSTATUS WINAPI RegDeleteKeyExA( HKEY hkey, LPCSTR name, REGSAM access, DWORD reserved )
1042 DWORD ret;
1043 HKEY tmp;
1045 if (!name) return ERROR_INVALID_PARAMETER;
1047 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
1049 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1050 if (!(ret = RegOpenKeyExA( hkey, name, 0, access | DELETE, &tmp )))
1052 if (!is_version_nt()) /* win95 does recursive key deletes */
1054 CHAR sub[MAX_PATH];
1055 DWORD len = sizeof(sub);
1056 while(!RegEnumKeyExA(tmp, 0, sub, &len, NULL, NULL, NULL, NULL))
1058 if(RegDeleteKeyExA(tmp, sub, access, reserved)) /* recurse */
1059 break;
1062 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1063 RegCloseKey( tmp );
1065 TRACE("%s ret=%08x\n", debugstr_a(name), ret);
1066 return ret;
1069 /******************************************************************************
1070 * RegSetValueExW (kernelbase.@)
1072 * Set the data and contents of a registry value.
1074 * PARAMS
1075 * hkey [I] Handle of key to set value for
1076 * name [I] Name of value to set
1077 * reserved [I] Reserved, must be zero
1078 * type [I] Type of the value being set
1079 * data [I] The new contents of the value to set
1080 * count [I] Size of data
1082 * RETURNS
1083 * Success: ERROR_SUCCESS
1084 * Failure: Error code
1086 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
1087 DWORD type, const BYTE *data, DWORD count )
1089 UNICODE_STRING nameW;
1091 /* no need for version check, not implemented on win9x anyway */
1093 if ((data && ((ULONG_PTR)data >> 16) == 0) || (!data && count)) return ERROR_NOACCESS;
1095 if (count && is_string(type))
1097 LPCWSTR str = (LPCWSTR)data;
1098 /* if user forgot to count terminating null, add it (yes NT does this) */
1099 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
1100 count += sizeof(WCHAR);
1102 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1104 RtlInitUnicodeString( &nameW, name );
1105 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
1109 /******************************************************************************
1110 * RegSetValueExA (kernelbase.@)
1112 * See RegSetValueExW.
1114 * NOTES
1115 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1116 * NT does definitely care (aj)
1118 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1119 const BYTE *data, DWORD count )
1121 ANSI_STRING nameA;
1122 UNICODE_STRING nameW;
1123 WCHAR *dataW = NULL;
1124 NTSTATUS status;
1126 if (!is_version_nt()) /* win95 */
1128 if (type == REG_SZ)
1130 if (!data) return ERROR_INVALID_PARAMETER;
1131 count = strlen((const char *)data) + 1;
1134 else if (count && is_string(type))
1136 /* if user forgot to count terminating null, add it (yes NT does this) */
1137 if (data[count-1] && !data[count]) count++;
1140 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1142 if (is_string( type )) /* need to convert to Unicode */
1144 DWORD lenW;
1145 RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1146 if (!(dataW = heap_alloc( lenW ))) return ERROR_OUTOFMEMORY;
1147 RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1148 count = lenW;
1149 data = (BYTE *)dataW;
1152 RtlInitAnsiString( &nameA, name );
1153 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1155 status = NtSetValueKey( hkey, &nameW, 0, type, data, count );
1156 RtlFreeUnicodeString( &nameW );
1158 heap_free( dataW );
1159 return RtlNtStatusToDosError( status );
1163 /******************************************************************************
1164 * RegSetKeyValueW (kernelbase.@)
1166 LONG WINAPI RegSetKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name, DWORD type, const void *data, DWORD len )
1168 HKEY hsubkey = NULL;
1169 DWORD ret;
1171 TRACE("(%p,%s,%s,%d,%p,%d)\n", hkey, debugstr_w(subkey), debugstr_w(name), type, data, len );
1173 if (subkey && subkey[0]) /* need to create the subkey */
1175 if ((ret = RegCreateKeyExW( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1176 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1177 hkey = hsubkey;
1180 ret = RegSetValueExW( hkey, name, 0, type, (const BYTE*)data, len );
1181 if (hsubkey) RegCloseKey( hsubkey );
1182 return ret;
1185 /******************************************************************************
1186 * RegSetKeyValueA (kernelbase.@)
1188 LONG WINAPI RegSetKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name, DWORD type, const void *data, DWORD len )
1190 HKEY hsubkey = NULL;
1191 DWORD ret;
1193 TRACE("(%p,%s,%s,%d,%p,%d)\n", hkey, debugstr_a(subkey), debugstr_a(name), type, data, len );
1195 if (subkey && subkey[0]) /* need to create the subkey */
1197 if ((ret = RegCreateKeyExA( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1198 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1199 hkey = hsubkey;
1202 ret = RegSetValueExA( hkey, name, 0, type, (const BYTE*)data, len );
1203 if (hsubkey) RegCloseKey( hsubkey );
1204 return ret;
1207 struct perf_provider
1209 HMODULE perflib;
1210 WCHAR linkage[MAX_PATH];
1211 WCHAR objects[MAX_PATH];
1212 PM_OPEN_PROC *pOpen;
1213 PM_CLOSE_PROC *pClose;
1214 PM_COLLECT_PROC *pCollect;
1217 static void *get_provider_entry(HKEY perf, HMODULE perflib, const char *name)
1219 char buf[MAX_PATH];
1220 DWORD err, type, len;
1222 len = sizeof(buf) - 1;
1223 err = RegQueryValueExA(perf, name, NULL, &type, (BYTE *)buf, &len);
1224 if (err != ERROR_SUCCESS || type != REG_SZ)
1225 return NULL;
1227 buf[len] = 0;
1228 TRACE("Loading function pointer for %s: %s\n", name, debugstr_a(buf));
1230 return GetProcAddress(perflib, buf);
1233 static BOOL load_provider(HKEY root, const WCHAR *name, struct perf_provider *provider)
1235 WCHAR buf[MAX_PATH], buf2[MAX_PATH];
1236 DWORD err, type, len;
1237 HKEY service, perf;
1239 err = RegOpenKeyExW(root, name, 0, KEY_READ, &service);
1240 if (err != ERROR_SUCCESS)
1241 return FALSE;
1243 provider->linkage[0] = 0;
1244 err = RegOpenKeyExW(service, L"Linkage", 0, KEY_READ, &perf);
1245 if (err == ERROR_SUCCESS)
1247 len = sizeof(buf) - sizeof(WCHAR);
1248 err = RegQueryValueExW(perf, L"Export", NULL, &type, (BYTE *)buf, &len);
1249 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1251 memcpy(provider->linkage, buf, len);
1252 provider->linkage[len / sizeof(WCHAR)] = 0;
1253 TRACE("Export: %s\n", debugstr_w(provider->linkage));
1255 RegCloseKey(perf);
1258 err = RegOpenKeyExW(service, L"Performance", 0, KEY_READ, &perf);
1259 RegCloseKey(service);
1260 if (err != ERROR_SUCCESS)
1261 return FALSE;
1263 provider->objects[0] = 0;
1264 len = sizeof(buf) - sizeof(WCHAR);
1265 err = RegQueryValueExW(perf, L"Object List", NULL, &type, (BYTE *)buf, &len);
1266 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1268 memcpy(provider->objects, buf, len);
1269 provider->objects[len / sizeof(WCHAR)] = 0;
1270 TRACE("Object List: %s\n", debugstr_w(provider->objects));
1273 len = sizeof(buf) - sizeof(WCHAR);
1274 err = RegQueryValueExW(perf, L"Library", NULL, &type, (BYTE *)buf, &len);
1275 if (err != ERROR_SUCCESS || !(type == REG_SZ || type == REG_EXPAND_SZ))
1276 goto error;
1278 buf[len / sizeof(WCHAR)] = 0;
1279 if (type == REG_EXPAND_SZ)
1281 len = ExpandEnvironmentStringsW(buf, buf2, MAX_PATH);
1282 if (!len || len > MAX_PATH) goto error;
1283 lstrcpyW(buf, buf2);
1286 if (!(provider->perflib = LoadLibraryW(buf)))
1288 WARN("Failed to load %s\n", debugstr_w(buf));
1289 goto error;
1292 GetModuleFileNameW(provider->perflib, buf, MAX_PATH);
1293 TRACE("Loaded provider %s\n", wine_dbgstr_w(buf));
1295 provider->pOpen = get_provider_entry(perf, provider->perflib, "Open");
1296 provider->pClose = get_provider_entry(perf, provider->perflib, "Close");
1297 provider->pCollect = get_provider_entry(perf, provider->perflib, "Collect");
1298 if (provider->pOpen && provider->pClose && provider->pCollect)
1300 RegCloseKey(perf);
1301 return TRUE;
1304 TRACE("Provider is missing required exports\n");
1305 FreeLibrary(provider->perflib);
1307 error:
1308 RegCloseKey(perf);
1309 return FALSE;
1312 static DWORD collect_data(struct perf_provider *provider, const WCHAR *query, void **data, DWORD *size, DWORD *obj_count)
1314 WCHAR *linkage = provider->linkage[0] ? provider->linkage : NULL;
1315 DWORD err;
1317 if (!query || !query[0])
1318 query = L"Global";
1320 err = provider->pOpen(linkage);
1321 if (err != ERROR_SUCCESS)
1323 TRACE("Open(%s) error %u (%#x)\n", debugstr_w(linkage), err, err);
1324 return err;
1327 *obj_count = 0;
1328 err = provider->pCollect((WCHAR *)query, data, size, obj_count);
1329 if (err != ERROR_SUCCESS)
1331 TRACE("Collect error %u (%#x)\n", err, err);
1332 *obj_count = 0;
1335 provider->pClose();
1336 return err;
1339 #define MAX_SERVICE_NAME 260
1341 static DWORD query_perf_data(const WCHAR *query, DWORD *type, void *data, DWORD *ret_size)
1343 DWORD err, i, data_size;
1344 HKEY root;
1345 PERF_DATA_BLOCK *pdb;
1347 if (!ret_size)
1348 return ERROR_INVALID_PARAMETER;
1350 data_size = *ret_size;
1351 *ret_size = 0;
1353 if (type)
1354 *type = REG_BINARY;
1356 if (!data || data_size < sizeof(*pdb))
1357 return ERROR_MORE_DATA;
1359 pdb = data;
1361 pdb->Signature[0] = 'P';
1362 pdb->Signature[1] = 'E';
1363 pdb->Signature[2] = 'R';
1364 pdb->Signature[3] = 'F';
1365 #ifdef WORDS_BIGENDIAN
1366 pdb->LittleEndian = FALSE;
1367 #else
1368 pdb->LittleEndian = TRUE;
1369 #endif
1370 pdb->Version = PERF_DATA_VERSION;
1371 pdb->Revision = PERF_DATA_REVISION;
1372 pdb->TotalByteLength = 0;
1373 pdb->HeaderLength = sizeof(*pdb);
1374 pdb->NumObjectTypes = 0;
1375 pdb->DefaultObject = 0;
1376 NtQueryPerformanceCounter( &pdb->PerfTime, &pdb->PerfFreq );
1378 data = pdb + 1;
1379 pdb->SystemNameOffset = sizeof(*pdb);
1380 pdb->SystemNameLength = (data_size - sizeof(*pdb)) / sizeof(WCHAR);
1381 if (!GetComputerNameExW(ComputerNameNetBIOS, data, &pdb->SystemNameLength))
1382 return ERROR_MORE_DATA;
1384 pdb->SystemNameLength++;
1385 pdb->SystemNameLength *= sizeof(WCHAR);
1387 pdb->HeaderLength += pdb->SystemNameLength;
1389 /* align to 8 bytes */
1390 if (pdb->SystemNameLength & 7)
1391 pdb->HeaderLength += 8 - (pdb->SystemNameLength & 7);
1393 if (data_size < pdb->HeaderLength)
1394 return ERROR_MORE_DATA;
1396 pdb->TotalByteLength = pdb->HeaderLength;
1398 data_size -= pdb->HeaderLength;
1399 data = (char *)data + pdb->HeaderLength;
1401 err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services", 0, KEY_READ, &root);
1402 if (err != ERROR_SUCCESS)
1403 return err;
1405 i = 0;
1406 for (;;)
1408 DWORD collected_size = data_size, obj_count = 0;
1409 struct perf_provider provider;
1410 WCHAR name[MAX_SERVICE_NAME];
1411 DWORD len = ARRAY_SIZE( name );
1412 void *collected_data = data;
1414 err = RegEnumKeyExW(root, i++, name, &len, NULL, NULL, NULL, NULL);
1415 if (err == ERROR_NO_MORE_ITEMS)
1417 err = ERROR_SUCCESS;
1418 break;
1421 if (err != ERROR_SUCCESS)
1422 continue;
1424 if (!load_provider(root, name, &provider))
1425 continue;
1427 err = collect_data(&provider, query, &collected_data, &collected_size, &obj_count);
1428 FreeLibrary(provider.perflib);
1430 if (err == ERROR_MORE_DATA)
1431 break;
1433 if (err == ERROR_SUCCESS)
1435 PERF_OBJECT_TYPE *obj = (PERF_OBJECT_TYPE *)data;
1437 TRACE("Collect: obj->TotalByteLength %u, collected_size %u\n",
1438 obj->TotalByteLength, collected_size);
1440 data_size -= collected_size;
1441 data = collected_data;
1443 pdb->TotalByteLength += collected_size;
1444 pdb->NumObjectTypes += obj_count;
1448 RegCloseKey(root);
1450 if (err == ERROR_SUCCESS)
1452 *ret_size = pdb->TotalByteLength;
1454 GetSystemTime(&pdb->SystemTime);
1455 GetSystemTimeAsFileTime((FILETIME *)&pdb->PerfTime100nSec);
1458 return err;
1461 /******************************************************************************
1462 * RegQueryValueExW (kernelbase.@)
1464 * See RegQueryValueExA.
1466 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1467 LPBYTE data, LPDWORD count )
1469 NTSTATUS status;
1470 UNICODE_STRING name_str;
1471 DWORD total_size;
1472 char buffer[256], *buf_ptr = buffer;
1473 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1474 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1476 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1477 hkey, debugstr_w(name), reserved, type, data, count,
1478 (count && data) ? *count : 0 );
1480 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1482 if (hkey == HKEY_PERFORMANCE_DATA)
1483 return query_perf_data(name, type, data, count);
1485 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1487 RtlInitUnicodeString( &name_str, name );
1489 if (data) total_size = min( sizeof(buffer), *count + info_size );
1490 else
1492 total_size = info_size;
1493 if (count) *count = 0;
1496 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1497 buffer, total_size, &total_size );
1498 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1500 if (data)
1502 /* retry with a dynamically allocated buffer */
1503 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1505 if (buf_ptr != buffer) heap_free( buf_ptr );
1506 if (!(buf_ptr = heap_alloc( total_size )))
1507 return ERROR_NOT_ENOUGH_MEMORY;
1508 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1509 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1510 buf_ptr, total_size, &total_size );
1513 if (!status)
1515 memcpy( data, buf_ptr + info_size, total_size - info_size );
1516 /* if the type is REG_SZ and data is not 0-terminated
1517 * and there is enough space in the buffer NT appends a \0 */
1518 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1520 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1521 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1524 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1526 else status = STATUS_SUCCESS;
1528 if (type) *type = info->Type;
1529 if (count) *count = total_size - info_size;
1531 done:
1532 if (buf_ptr != buffer) heap_free( buf_ptr );
1533 return RtlNtStatusToDosError(status);
1537 /******************************************************************************
1538 * RegQueryValueExA (kernelbase.@)
1540 * Get the type and contents of a specified value under with a key.
1542 * PARAMS
1543 * hkey [I] Handle of the key to query
1544 * name [I] Name of value under hkey to query
1545 * reserved [I] Reserved - must be NULL
1546 * type [O] Destination for the value type, or NULL if not required
1547 * data [O] Destination for the values contents, or NULL if not required
1548 * count [I/O] Size of data, updated with the number of bytes returned
1550 * RETURNS
1551 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1552 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1553 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1554 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1556 * NOTES
1557 * MSDN states that if data is too small it is partially filled. In reality
1558 * it remains untouched.
1560 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved,
1561 LPDWORD type, LPBYTE data, LPDWORD count )
1563 NTSTATUS status;
1564 ANSI_STRING nameA;
1565 UNICODE_STRING nameW;
1566 DWORD total_size, datalen = 0;
1567 char buffer[256], *buf_ptr = buffer;
1568 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1569 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1571 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1572 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1574 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1575 if (!(hkey = get_special_root_hkey( hkey, 0 )))
1576 return ERROR_INVALID_HANDLE;
1578 if (count) datalen = *count;
1579 if (!data && count) *count = 0;
1581 /* this matches Win9x behaviour - NT sets *type to a random value */
1582 if (type) *type = REG_NONE;
1584 RtlInitAnsiString( &nameA, name );
1585 if ((status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1586 return RtlNtStatusToDosError(status);
1588 if (hkey == HKEY_PERFORMANCE_DATA)
1590 DWORD ret = query_perf_data( nameW.Buffer, type, data, count );
1591 RtlFreeUnicodeString( &nameW );
1592 return ret;
1595 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1596 buffer, sizeof(buffer), &total_size );
1597 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1599 /* we need to fetch the contents for a string type even if not requested,
1600 * because we need to compute the length of the ASCII string. */
1601 if (data || is_string(info->Type))
1603 /* retry with a dynamically allocated buffer */
1604 while (status == STATUS_BUFFER_OVERFLOW)
1606 if (buf_ptr != buffer) heap_free( buf_ptr );
1607 if (!(buf_ptr = heap_alloc( total_size )))
1609 status = STATUS_NO_MEMORY;
1610 goto done;
1612 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1613 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1614 buf_ptr, total_size, &total_size );
1617 if (status) goto done;
1619 if (is_string(info->Type))
1621 DWORD len;
1623 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1624 total_size - info_size );
1625 if (data && len)
1627 if (len > datalen) status = STATUS_BUFFER_OVERFLOW;
1628 else
1630 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1631 total_size - info_size );
1632 /* if the type is REG_SZ and data is not 0-terminated
1633 * and there is enough space in the buffer NT appends a \0 */
1634 if (len < datalen && data[len-1]) data[len] = 0;
1637 total_size = len + info_size;
1639 else if (data)
1641 if (total_size - info_size > datalen) status = STATUS_BUFFER_OVERFLOW;
1642 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1645 else status = STATUS_SUCCESS;
1647 if (type) *type = info->Type;
1648 if (count) *count = total_size - info_size;
1650 done:
1651 if (buf_ptr != buffer) heap_free( buf_ptr );
1652 RtlFreeUnicodeString( &nameW );
1653 return RtlNtStatusToDosError(status);
1657 /******************************************************************************
1658 * apply_restrictions [internal]
1660 * Helper function for RegGetValueA/W.
1662 static void apply_restrictions( DWORD dwFlags, DWORD dwType, DWORD cbData, PLONG ret )
1664 /* Check if the type is restricted by the passed flags */
1665 if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1667 DWORD dwMask = 0;
1669 switch (dwType)
1671 case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1672 case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1673 case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1674 case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1675 case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1676 case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1677 case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1680 if (dwFlags & dwMask)
1682 /* Type is not restricted, check for size mismatch */
1683 if (dwType == REG_BINARY)
1685 DWORD cbExpect = 0;
1687 if ((dwFlags & RRF_RT_ANY) == RRF_RT_DWORD)
1688 cbExpect = 4;
1689 else if ((dwFlags & RRF_RT_ANY) == RRF_RT_QWORD)
1690 cbExpect = 8;
1692 if (cbExpect && cbData != cbExpect)
1693 *ret = ERROR_DATATYPE_MISMATCH;
1696 else *ret = ERROR_UNSUPPORTED_TYPE;
1701 /******************************************************************************
1702 * RegGetValueW (kernelbase.@)
1704 * Retrieves the type and data for a value name associated with a key,
1705 * optionally expanding its content and restricting its type.
1707 * PARAMS
1708 * hKey [I] Handle to an open key.
1709 * pszSubKey [I] Name of the subkey of hKey.
1710 * pszValue [I] Name of value under hKey/szSubKey to query.
1711 * dwFlags [I] Flags restricting the value type to retrieve.
1712 * pdwType [O] Destination for the values type, may be NULL.
1713 * pvData [O] Destination for the values content, may be NULL.
1714 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
1715 * retrieve the whole content, including the trailing '\0'
1716 * for strings.
1718 * RETURNS
1719 * Success: ERROR_SUCCESS
1720 * Failure: nonzero error code from Winerror.h
1722 * NOTES
1723 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
1724 * expanded and pdwType is set to REG_SZ instead.
1725 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1726 * without RRF_NOEXPAND is thus not allowed.
1727 * An exception is the case where RRF_RT_ANY is specified, because then
1728 * RRF_NOEXPAND is allowed.
1730 LSTATUS WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1731 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1732 LPDWORD pcbData )
1734 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1735 PVOID pvBuf = NULL;
1736 LONG ret;
1738 TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n",
1739 hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1740 pvData, pcbData, cbData);
1742 if (pvData && !pcbData)
1743 return ERROR_INVALID_PARAMETER;
1745 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1746 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1747 return ERROR_INVALID_PARAMETER;
1749 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1750 return ERROR_INVALID_PARAMETER;
1752 if (pszSubKey && pszSubKey[0])
1754 REGSAM samDesired = KEY_QUERY_VALUE;
1756 if (dwFlags & RRF_WOW64_MASK)
1757 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1759 ret = RegOpenKeyExW(hKey, pszSubKey, 0, samDesired, &hKey);
1760 if (ret != ERROR_SUCCESS) return ret;
1763 ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1765 /* If we are going to expand we need to read in the whole the value even
1766 * if the passed buffer was too small as the expanded string might be
1767 * smaller than the unexpanded one and could fit into cbData bytes. */
1768 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1769 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1771 do {
1772 heap_free(pvBuf);
1774 pvBuf = heap_alloc(cbData);
1775 if (!pvBuf)
1777 ret = ERROR_NOT_ENOUGH_MEMORY;
1778 break;
1781 if (ret == ERROR_MORE_DATA || !pvData)
1782 ret = RegQueryValueExW(hKey, pszValue, NULL,
1783 &dwType, pvBuf, &cbData);
1784 else
1786 /* Even if cbData was large enough we have to copy the
1787 * string since ExpandEnvironmentStrings can't handle
1788 * overlapping buffers. */
1789 CopyMemory(pvBuf, pvData, cbData);
1792 /* Both the type or the value itself could have been modified in
1793 * between so we have to keep retrying until the buffer is large
1794 * enough or we no longer have to expand the value. */
1795 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1797 if (ret == ERROR_SUCCESS)
1799 /* Recheck dwType in case it changed since the first call */
1800 if (dwType == REG_EXPAND_SZ)
1802 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1803 pcbData ? *pcbData : 0) * sizeof(WCHAR);
1804 dwType = REG_SZ;
1805 if(pvData && pcbData && cbData > *pcbData)
1806 ret = ERROR_MORE_DATA;
1808 else if (pvData)
1809 CopyMemory(pvData, pvBuf, *pcbData);
1812 heap_free(pvBuf);
1815 if (pszSubKey && pszSubKey[0])
1816 RegCloseKey(hKey);
1818 apply_restrictions(dwFlags, dwType, cbData, &ret);
1820 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1821 ZeroMemory(pvData, *pcbData);
1823 if (pdwType) *pdwType = dwType;
1824 if (pcbData) *pcbData = cbData;
1826 return ret;
1830 /******************************************************************************
1831 * RegGetValueA (kernelbase.@)
1833 * See RegGetValueW.
1835 LSTATUS WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
1836 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1837 LPDWORD pcbData )
1839 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1840 PVOID pvBuf = NULL;
1841 LONG ret;
1843 TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n",
1844 hKey, debugstr_a(pszSubKey), debugstr_a(pszValue), dwFlags,
1845 pdwType, pvData, pcbData, cbData);
1847 if (pvData && !pcbData)
1848 return ERROR_INVALID_PARAMETER;
1850 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1851 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1852 return ERROR_INVALID_PARAMETER;
1854 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1855 return ERROR_INVALID_PARAMETER;
1857 if (pszSubKey && pszSubKey[0])
1859 REGSAM samDesired = KEY_QUERY_VALUE;
1861 if (dwFlags & RRF_WOW64_MASK)
1862 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1864 ret = RegOpenKeyExA(hKey, pszSubKey, 0, samDesired, &hKey);
1865 if (ret != ERROR_SUCCESS) return ret;
1868 ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1870 /* If we are going to expand we need to read in the whole the value even
1871 * if the passed buffer was too small as the expanded string might be
1872 * smaller than the unexpanded one and could fit into cbData bytes. */
1873 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1874 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1876 do {
1877 heap_free(pvBuf);
1879 pvBuf = heap_alloc(cbData);
1880 if (!pvBuf)
1882 ret = ERROR_NOT_ENOUGH_MEMORY;
1883 break;
1886 if (ret == ERROR_MORE_DATA || !pvData)
1887 ret = RegQueryValueExA(hKey, pszValue, NULL,
1888 &dwType, pvBuf, &cbData);
1889 else
1891 /* Even if cbData was large enough we have to copy the
1892 * string since ExpandEnvironmentStrings can't handle
1893 * overlapping buffers. */
1894 CopyMemory(pvBuf, pvData, cbData);
1897 /* Both the type or the value itself could have been modified in
1898 * between so we have to keep retrying until the buffer is large
1899 * enough or we no longer have to expand the value. */
1900 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1902 if (ret == ERROR_SUCCESS)
1904 /* Recheck dwType in case it changed since the first call */
1905 if (dwType == REG_EXPAND_SZ)
1907 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
1908 pcbData ? *pcbData : 0);
1909 dwType = REG_SZ;
1910 if(pvData && pcbData && cbData > *pcbData)
1911 ret = ERROR_MORE_DATA;
1913 else if (pvData)
1914 CopyMemory(pvData, pvBuf, *pcbData);
1917 heap_free(pvBuf);
1920 if (pszSubKey && pszSubKey[0])
1921 RegCloseKey(hKey);
1923 apply_restrictions(dwFlags, dwType, cbData, &ret);
1925 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1926 ZeroMemory(pvData, *pcbData);
1928 if (pdwType) *pdwType = dwType;
1929 if (pcbData) *pcbData = cbData;
1931 return ret;
1935 /******************************************************************************
1936 * RegEnumValueW (kernelbase.@)
1938 * Enumerates the values for the specified open registry key.
1940 * PARAMS
1941 * hkey [I] Handle to key to query
1942 * index [I] Index of value to query
1943 * value [O] Value string
1944 * val_count [I/O] Size of value buffer (in wchars)
1945 * reserved [I] Reserved
1946 * type [O] Type code
1947 * data [O] Value data
1948 * count [I/O] Size of data buffer (in bytes)
1950 * RETURNS
1951 * Success: ERROR_SUCCESS
1952 * Failure: nonzero error code from Winerror.h
1954 LSTATUS WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1955 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1957 NTSTATUS status;
1958 DWORD total_size;
1959 char buffer[256], *buf_ptr = buffer;
1960 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1961 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1963 TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
1964 hkey, index, value, val_count, reserved, type, data, count );
1966 if ((data && !count) || reserved || !value || !val_count)
1967 return ERROR_INVALID_PARAMETER;
1968 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1970 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1971 if (data) total_size += *count;
1972 total_size = min( sizeof(buffer), total_size );
1974 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1975 buffer, total_size, &total_size );
1977 /* retry with a dynamically allocated buffer */
1978 while (status == STATUS_BUFFER_OVERFLOW)
1980 if (buf_ptr != buffer) heap_free( buf_ptr );
1981 if (!(buf_ptr = heap_alloc( total_size )))
1982 return ERROR_NOT_ENOUGH_MEMORY;
1983 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1984 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1985 buf_ptr, total_size, &total_size );
1988 if (status) goto done;
1990 if (info->NameLength/sizeof(WCHAR) >= *val_count)
1992 status = STATUS_BUFFER_OVERFLOW;
1993 goto overflow;
1995 memcpy( value, info->Name, info->NameLength );
1996 *val_count = info->NameLength / sizeof(WCHAR);
1997 value[*val_count] = 0;
1999 if (data)
2001 if (total_size - info->DataOffset > *count)
2003 status = STATUS_BUFFER_OVERFLOW;
2004 goto overflow;
2006 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2007 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
2009 /* if the type is REG_SZ and data is not 0-terminated
2010 * and there is enough space in the buffer NT appends a \0 */
2011 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
2012 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
2016 overflow:
2017 if (type) *type = info->Type;
2018 if (count) *count = info->DataLength;
2020 done:
2021 if (buf_ptr != buffer) heap_free( buf_ptr );
2022 return RtlNtStatusToDosError(status);
2026 /******************************************************************************
2027 * RegEnumValueA (kernelbase.@)
2029 * See RegEnumValueW.
2031 LSTATUS WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
2032 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
2034 NTSTATUS status;
2035 DWORD total_size;
2036 char buffer[256], *buf_ptr = buffer;
2037 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2038 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
2040 TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
2041 hkey, index, value, val_count, reserved, type, data, count );
2043 if ((data && !count) || reserved || !value || !val_count)
2044 return ERROR_INVALID_PARAMETER;
2045 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2047 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
2048 if (data) total_size += *count;
2049 total_size = min( sizeof(buffer), total_size );
2051 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2052 buffer, total_size, &total_size );
2054 /* we need to fetch the contents for a string type even if not requested,
2055 * because we need to compute the length of the ASCII string. */
2057 /* retry with a dynamically allocated buffer */
2058 while (status == STATUS_BUFFER_OVERFLOW)
2060 if (buf_ptr != buffer) heap_free( buf_ptr );
2061 if (!(buf_ptr = heap_alloc( total_size )))
2062 return ERROR_NOT_ENOUGH_MEMORY;
2063 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
2064 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2065 buf_ptr, total_size, &total_size );
2068 if (status) goto done;
2070 if (is_string(info->Type))
2072 DWORD len;
2073 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
2074 total_size - info->DataOffset );
2075 if (data && len)
2077 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
2078 else
2080 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
2081 total_size - info->DataOffset );
2082 /* if the type is REG_SZ and data is not 0-terminated
2083 * and there is enough space in the buffer NT appends a \0 */
2084 if (len < *count && data[len-1]) data[len] = 0;
2087 info->DataLength = len;
2089 else if (data)
2091 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
2092 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2095 if (!status)
2097 DWORD len;
2099 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
2100 if (len >= *val_count)
2102 status = STATUS_BUFFER_OVERFLOW;
2103 if (*val_count)
2105 len = *val_count - 1;
2106 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2107 value[len] = 0;
2110 else
2112 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2113 value[len] = 0;
2114 *val_count = len;
2118 if (type) *type = info->Type;
2119 if (count) *count = info->DataLength;
2121 done:
2122 if (buf_ptr != buffer) heap_free( buf_ptr );
2123 return RtlNtStatusToDosError(status);
2126 /******************************************************************************
2127 * RegDeleteValueW (kernelbase.@)
2129 * See RegDeleteValueA.
2131 LSTATUS WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
2133 return RegDeleteKeyValueW( hkey, NULL, name );
2136 /******************************************************************************
2137 * RegDeleteValueA (kernelbase.@)
2139 * Delete a value from the registry.
2141 * PARAMS
2142 * hkey [I] Registry handle of the key holding the value
2143 * name [I] Name of the value under hkey to delete
2145 * RETURNS
2146 * Success: ERROR_SUCCESS
2147 * Failure: nonzero error code from Winerror.h
2149 LSTATUS WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
2151 return RegDeleteKeyValueA( hkey, NULL, name );
2154 /******************************************************************************
2155 * RegDeleteKeyValueW (kernelbase.@)
2157 LONG WINAPI RegDeleteKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name )
2159 UNICODE_STRING nameW;
2160 HKEY hsubkey = 0;
2161 LONG ret;
2163 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2165 if (subkey)
2167 if ((ret = RegOpenKeyExW( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey )))
2168 return ret;
2169 hkey = hsubkey;
2172 RtlInitUnicodeString( &nameW, name );
2173 ret = RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
2174 if (hsubkey) RegCloseKey( hsubkey );
2175 return ret;
2178 /******************************************************************************
2179 * RegDeleteKeyValueA (kernelbase.@)
2181 LONG WINAPI RegDeleteKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name )
2183 UNICODE_STRING nameW;
2184 HKEY hsubkey = 0;
2185 ANSI_STRING nameA;
2186 NTSTATUS status;
2188 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2190 if (subkey)
2192 LONG ret = RegOpenKeyExA( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey );
2193 if (ret)
2194 return ret;
2195 hkey = hsubkey;
2198 RtlInitAnsiString( &nameA, name );
2199 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
2201 status = NtDeleteValueKey( hkey, &nameW );
2202 RtlFreeUnicodeString( &nameW );
2205 if (hsubkey) RegCloseKey( hsubkey );
2206 return RtlNtStatusToDosError( status );
2209 /******************************************************************************
2210 * RegLoadKeyW (kernelbase.@)
2212 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
2213 * registration information from a specified file into that subkey.
2215 * PARAMS
2216 * hkey [I] Handle of open key
2217 * subkey [I] Address of name of subkey
2218 * filename [I] Address of filename for registry information
2220 * RETURNS
2221 * Success: ERROR_SUCCESS
2222 * Failure: nonzero error code from Winerror.h
2224 LSTATUS WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
2226 OBJECT_ATTRIBUTES destkey, file;
2227 UNICODE_STRING subkeyW, filenameW;
2228 NTSTATUS status;
2230 if (!(hkey = get_special_root_hkey(hkey, 0))) return ERROR_INVALID_HANDLE;
2232 destkey.Length = sizeof(destkey);
2233 destkey.RootDirectory = hkey; /* root key: HKLM or HKU */
2234 destkey.ObjectName = &subkeyW; /* name of the key */
2235 destkey.Attributes = 0;
2236 destkey.SecurityDescriptor = NULL;
2237 destkey.SecurityQualityOfService = NULL;
2238 RtlInitUnicodeString(&subkeyW, subkey);
2240 file.Length = sizeof(file);
2241 file.RootDirectory = NULL;
2242 file.ObjectName = &filenameW; /* file containing the hive */
2243 file.Attributes = OBJ_CASE_INSENSITIVE;
2244 file.SecurityDescriptor = NULL;
2245 file.SecurityQualityOfService = NULL;
2246 RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
2248 status = NtLoadKey(&destkey, &file);
2249 RtlFreeUnicodeString(&filenameW);
2250 return RtlNtStatusToDosError( status );
2254 /******************************************************************************
2255 * RegLoadKeyA (kernelbase.@)
2257 * See RegLoadKeyW.
2259 LSTATUS WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
2261 UNICODE_STRING subkeyW, filenameW;
2262 STRING subkeyA, filenameA;
2263 NTSTATUS status;
2264 LONG ret;
2266 RtlInitAnsiString(&subkeyA, subkey);
2267 RtlInitAnsiString(&filenameA, filename);
2269 RtlInitUnicodeString(&subkeyW, NULL);
2270 RtlInitUnicodeString(&filenameW, NULL);
2271 if (!(status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)) &&
2272 !(status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
2274 ret = RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
2276 else ret = RtlNtStatusToDosError(status);
2277 RtlFreeUnicodeString(&subkeyW);
2278 RtlFreeUnicodeString(&filenameW);
2279 return ret;
2283 /******************************************************************************
2284 * RegSaveKeyExW (kernelbase.@)
2286 LSTATUS WINAPI RegSaveKeyExW( HKEY hkey, LPCWSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2288 UNICODE_STRING nameW;
2289 OBJECT_ATTRIBUTES attr;
2290 IO_STATUS_BLOCK io;
2291 NTSTATUS status;
2292 HANDLE handle;
2294 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
2296 if (!file || !*file) return ERROR_INVALID_PARAMETER;
2297 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2299 if ((status = RtlDosPathNameToNtPathName_U_WithStatus( file, &nameW, NULL, NULL )))
2300 return RtlNtStatusToDosError( status );
2302 InitializeObjectAttributes( &attr, &nameW, OBJ_CASE_INSENSITIVE, 0, sa );
2303 status = NtCreateFile( &handle, GENERIC_WRITE | SYNCHRONIZE, &attr, &io, NULL, FILE_NON_DIRECTORY_FILE,
2304 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
2305 FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
2306 RtlFreeUnicodeString( &nameW );
2307 if (!status)
2309 status = NtSaveKey( hkey, handle );
2310 CloseHandle( handle );
2312 return RtlNtStatusToDosError( status );
2316 /******************************************************************************
2317 * RegSaveKeyExA (kernelbase.@)
2319 LSTATUS WINAPI RegSaveKeyExA( HKEY hkey, LPCSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2321 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
2322 NTSTATUS status;
2323 STRING fileA;
2325 RtlInitAnsiString(&fileA, file);
2326 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
2327 return RtlNtStatusToDosError( status );
2328 return RegSaveKeyExW(hkey, fileW->Buffer, sa, flags);
2332 /******************************************************************************
2333 * RegRestoreKeyW (kernelbase.@)
2335 * Read the registry information from a file and copy it over a key.
2337 * PARAMS
2338 * hkey [I] Handle of key where restore begins
2339 * lpFile [I] Address of filename containing saved tree
2340 * dwFlags [I] Optional flags
2342 * RETURNS
2343 * Success: ERROR_SUCCESS
2344 * Failure: nonzero error code from Winerror.h
2346 LSTATUS WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2348 TRACE("(%p,%s,%d)\n",hkey,debugstr_w(lpFile),dwFlags);
2350 /* It seems to do this check before the hkey check */
2351 if (!lpFile || !*lpFile)
2352 return ERROR_INVALID_PARAMETER;
2354 FIXME("(%p,%s,%d): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2356 /* Check for file existence */
2358 return ERROR_SUCCESS;
2362 /******************************************************************************
2363 * RegRestoreKeyA (kernelbase.@)
2365 * See RegRestoreKeyW.
2367 LSTATUS WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2369 UNICODE_STRING lpFileW;
2370 LONG ret;
2372 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2373 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2374 RtlFreeUnicodeString( &lpFileW );
2375 return ret;
2379 /******************************************************************************
2380 * RegUnLoadKeyW (kernelbase.@)
2382 * Unload a registry key and its subkeys from the registry.
2384 * PARAMS
2385 * hkey [I] Handle of open key
2386 * lpSubKey [I] Address of name of subkey to unload
2388 * RETURNS
2389 * Success: ERROR_SUCCESS
2390 * Failure: nonzero error code from Winerror.h
2392 LSTATUS WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2394 OBJECT_ATTRIBUTES attr;
2395 UNICODE_STRING subkey;
2397 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2399 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2401 RtlInitUnicodeString(&subkey, lpSubKey);
2402 InitializeObjectAttributes(&attr, &subkey, OBJ_CASE_INSENSITIVE, hkey, NULL);
2403 return RtlNtStatusToDosError( NtUnloadKey(&attr) );
2407 /******************************************************************************
2408 * RegUnLoadKeyA (kernelbase.@)
2410 * See RegUnLoadKeyW.
2412 LSTATUS WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2414 UNICODE_STRING lpSubKeyW;
2415 LONG ret;
2417 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2418 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2419 RtlFreeUnicodeString( &lpSubKeyW );
2420 return ret;
2424 /******************************************************************************
2425 * RegSetKeySecurity (kernelbase.@)
2427 * Set the security of an open registry key.
2429 * PARAMS
2430 * hkey [I] Open handle of key to set
2431 * SecurityInfo [I] Descriptor contents
2432 * pSecurityDesc [I] Address of descriptor for key
2434 * RETURNS
2435 * Success: ERROR_SUCCESS
2436 * Failure: nonzero error code from Winerror.h
2438 LSTATUS WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2439 PSECURITY_DESCRIPTOR pSecurityDesc )
2441 TRACE("(%p,%d,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2443 /* It seems to perform this check before the hkey check */
2444 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2445 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2446 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2447 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2448 /* Param OK */
2449 } else
2450 return ERROR_INVALID_PARAMETER;
2452 if (!pSecurityDesc)
2453 return ERROR_INVALID_PARAMETER;
2455 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2457 return RtlNtStatusToDosError( NtSetSecurityObject( hkey, SecurityInfo, pSecurityDesc ) );
2461 /******************************************************************************
2462 * RegGetKeySecurity (kernelbase.@)
2464 * Get a copy of the security descriptor for a given registry key.
2466 * PARAMS
2467 * hkey [I] Open handle of key to set
2468 * SecurityInformation [I] Descriptor contents
2469 * pSecurityDescriptor [O] Address of descriptor for key
2470 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2472 * RETURNS
2473 * Success: ERROR_SUCCESS
2474 * Failure: Error code
2476 LSTATUS WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2477 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2478 LPDWORD lpcbSecurityDescriptor )
2480 TRACE("(%p,%d,%p,%d)\n",hkey,SecurityInformation,pSecurityDescriptor,
2481 *lpcbSecurityDescriptor);
2483 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2485 return RtlNtStatusToDosError( NtQuerySecurityObject( hkey,
2486 SecurityInformation, pSecurityDescriptor,
2487 *lpcbSecurityDescriptor, lpcbSecurityDescriptor ) );
2491 /******************************************************************************
2492 * RegFlushKey (kernelbase.@)
2494 * Immediately write a registry key to registry.
2496 * PARAMS
2497 * hkey [I] Handle of key to write
2499 * RETURNS
2500 * Success: ERROR_SUCCESS
2501 * Failure: Error code
2503 LSTATUS WINAPI RegFlushKey( HKEY hkey )
2505 hkey = get_special_root_hkey( hkey, 0 );
2506 if (!hkey) return ERROR_INVALID_HANDLE;
2508 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2512 /******************************************************************************
2513 * RegNotifyChangeKeyValue (kernelbase.@)
2515 * Notify the caller about changes to the attributes or contents of a registry key.
2517 * PARAMS
2518 * hkey [I] Handle of key to watch
2519 * fWatchSubTree [I] Flag for subkey notification
2520 * fdwNotifyFilter [I] Changes to be reported
2521 * hEvent [I] Handle of signaled event
2522 * fAsync [I] Flag for asynchronous reporting
2524 * RETURNS
2525 * Success: ERROR_SUCCESS
2526 * Failure: nonzero error code from Winerror.h
2528 LSTATUS WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2529 DWORD fdwNotifyFilter, HANDLE hEvent,
2530 BOOL fAsync )
2532 NTSTATUS status;
2533 IO_STATUS_BLOCK iosb;
2535 hkey = get_special_root_hkey( hkey, 0 );
2536 if (!hkey) return ERROR_INVALID_HANDLE;
2538 TRACE("(%p,%i,%d,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2539 hEvent, fAsync);
2541 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2542 fdwNotifyFilter, fWatchSubTree, NULL, 0,
2543 fAsync);
2545 if (status && status != STATUS_PENDING)
2546 return RtlNtStatusToDosError( status );
2548 return ERROR_SUCCESS;
2551 /******************************************************************************
2552 * RegOpenUserClassesRoot (kernelbase.@)
2554 * Open the HKEY_CLASSES_ROOT key for a user.
2556 * PARAMS
2557 * hToken [I] Handle of token representing the user
2558 * dwOptions [I] Reserved, must be 0
2559 * samDesired [I] Desired access rights
2560 * phkResult [O] Destination for the resulting key handle
2562 * RETURNS
2563 * Success: ERROR_SUCCESS
2564 * Failure: nonzero error code from Winerror.h
2566 * NOTES
2567 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2568 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2569 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2571 LSTATUS WINAPI RegOpenUserClassesRoot( HANDLE hToken, DWORD dwOptions, REGSAM samDesired, PHKEY phkResult )
2573 FIXME("(%p, 0x%x, 0x%x, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2575 *phkResult = HKEY_CLASSES_ROOT;
2576 return ERROR_SUCCESS;
2580 static void dump_mui_cache(void)
2582 struct mui_cache_entry *ent;
2584 TRACE("---------- MUI Cache ----------\n");
2585 LIST_FOR_EACH_ENTRY( ent, &reg_mui_cache, struct mui_cache_entry, entry )
2586 TRACE("entry=%p, %s,-%u [%#x] => %s\n",
2587 ent, wine_dbgstr_w(ent->file_name), ent->index, ent->locale, wine_dbgstr_w(ent->text));
2590 static inline void free_mui_cache_entry(struct mui_cache_entry *ent)
2592 heap_free(ent->file_name);
2593 heap_free(ent->text);
2594 heap_free(ent);
2597 /* critical section must be held */
2598 static int reg_mui_cache_get(const WCHAR *file_name, UINT index, WCHAR **buffer)
2600 struct mui_cache_entry *ent;
2602 TRACE("(%s %u %p)\n", wine_dbgstr_w(file_name), index, buffer);
2604 LIST_FOR_EACH_ENTRY(ent, &reg_mui_cache, struct mui_cache_entry, entry)
2606 if (ent->index == index && ent->locale == GetThreadLocale() &&
2607 !lstrcmpiW(ent->file_name, file_name))
2608 goto found;
2610 return 0;
2612 found:
2613 /* move to the list head */
2614 if (list_prev(&reg_mui_cache, &ent->entry)) {
2615 list_remove(&ent->entry);
2616 list_add_head(&reg_mui_cache, &ent->entry);
2619 TRACE("=> %s\n", wine_dbgstr_w(ent->text));
2620 *buffer = ent->text;
2621 return lstrlenW(ent->text);
2624 /* critical section must be held */
2625 static void reg_mui_cache_put(const WCHAR *file_name, UINT index, const WCHAR *buffer, INT size)
2627 struct mui_cache_entry *ent;
2628 TRACE("(%s %u %s %d)\n", wine_dbgstr_w(file_name), index, wine_dbgstr_wn(buffer, size), size);
2630 ent = heap_calloc(sizeof(*ent), 1);
2631 if (!ent)
2632 return;
2633 ent->file_name = heap_alloc((lstrlenW(file_name) + 1) * sizeof(WCHAR));
2634 if (!ent->file_name) {
2635 free_mui_cache_entry(ent);
2636 return;
2638 lstrcpyW(ent->file_name, file_name);
2639 ent->index = index;
2640 ent->locale = GetThreadLocale();
2641 ent->text = heap_alloc((size + 1) * sizeof(WCHAR));
2642 if (!ent->text) {
2643 free_mui_cache_entry(ent);
2644 return;
2646 memcpy(ent->text, buffer, size * sizeof(WCHAR));
2647 ent->text[size] = '\0';
2649 TRACE("add %p\n", ent);
2650 list_add_head(&reg_mui_cache, &ent->entry);
2651 if (reg_mui_cache_count > REG_MUI_CACHE_SIZE) {
2652 ent = LIST_ENTRY( list_tail( &reg_mui_cache ), struct mui_cache_entry, entry );
2653 TRACE("freeing %p\n", ent);
2654 list_remove(&ent->entry);
2655 free_mui_cache_entry(ent);
2657 else
2658 reg_mui_cache_count++;
2660 if (TRACE_ON(reg))
2661 dump_mui_cache();
2662 return;
2665 static LONG load_mui_string(const WCHAR *file_name, UINT res_id, WCHAR *buffer, INT max_chars, INT *req_chars, DWORD flags)
2667 HMODULE hModule = NULL;
2668 WCHAR *string = NULL, *full_name;
2669 int size;
2670 LONG result;
2672 /* Verify the file existence. i.e. We don't rely on PATH variable */
2673 if (GetFileAttributesW(file_name) == INVALID_FILE_ATTRIBUTES)
2674 return ERROR_FILE_NOT_FOUND;
2676 size = GetFullPathNameW(file_name, 0, NULL, NULL);
2677 if (!size)
2678 return GetLastError();
2679 full_name = heap_alloc(size * sizeof(WCHAR));
2680 if (!full_name)
2681 return ERROR_NOT_ENOUGH_MEMORY;
2682 GetFullPathNameW(file_name, size, full_name, NULL);
2684 RtlEnterCriticalSection(&reg_mui_cs);
2685 size = reg_mui_cache_get(full_name, res_id, &string);
2686 if (!size) {
2687 RtlLeaveCriticalSection(&reg_mui_cs);
2689 /* Load the file */
2690 hModule = LoadLibraryExW(full_name, NULL,
2691 LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
2692 if (!hModule)
2693 return GetLastError();
2695 size = LoadStringW(hModule, res_id, (WCHAR *)&string, 0);
2696 if (!size) {
2697 if (string) result = ERROR_NOT_FOUND;
2698 else result = GetLastError();
2699 goto cleanup;
2702 RtlEnterCriticalSection(&reg_mui_cs);
2703 reg_mui_cache_put(full_name, res_id, string, size);
2704 RtlLeaveCriticalSection(&reg_mui_cs);
2706 *req_chars = size + 1;
2708 /* If no buffer is given, skip copying. */
2709 if (!buffer) {
2710 result = ERROR_MORE_DATA;
2711 goto cleanup;
2714 /* Else copy over the string, respecting the buffer size. */
2715 if (size < max_chars)
2716 max_chars = size;
2717 else {
2718 if (flags & REG_MUI_STRING_TRUNCATE)
2719 max_chars--;
2720 else {
2721 result = ERROR_MORE_DATA;
2722 goto cleanup;
2725 if (max_chars >= 0) {
2726 memcpy(buffer, string, max_chars * sizeof(WCHAR));
2727 buffer[max_chars] = '\0';
2730 result = ERROR_SUCCESS;
2732 cleanup:
2733 if (hModule)
2734 FreeLibrary(hModule);
2735 else
2736 RtlLeaveCriticalSection(&reg_mui_cs);
2737 heap_free(full_name);
2738 return result;
2741 /******************************************************************************
2742 * RegLoadMUIStringW (kernelbase.@)
2744 * Load the localized version of a string resource from some PE, respective
2745 * id and path of which are given in the registry value in the format
2746 * @[path]\dllname,-resourceId
2748 * PARAMS
2749 * hKey [I] Key, of which to load the string value from.
2750 * pszValue [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2751 * pszBuffer [O] Buffer to store the localized string in.
2752 * cbBuffer [I] Size of the destination buffer in bytes.
2753 * pcbData [O] Number of bytes written to pszBuffer (optional, may be NULL).
2754 * dwFlags [I] Truncate output to fit the buffer if REG_MUI_STRING_TRUNCATE.
2755 * pszBaseDir [I] Base directory of loading path. If NULL, use the current directory.
2757 * RETURNS
2758 * Success: ERROR_SUCCESS,
2759 * Failure: nonzero error code from winerror.h
2761 LSTATUS WINAPI RegLoadMUIStringW(HKEY hKey, LPCWSTR pwszValue, LPWSTR pwszBuffer, DWORD cbBuffer,
2762 LPDWORD pcbData, DWORD dwFlags, LPCWSTR pwszBaseDir)
2764 DWORD dwValueType, cbData;
2765 LPWSTR pwszTempBuffer = NULL, pwszExpandedBuffer = NULL;
2766 LONG result;
2768 TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %d, pcbData = %p, "
2769 "dwFlags = %d, pwszBaseDir = %s)\n", hKey, debugstr_w(pwszValue), pwszBuffer,
2770 cbBuffer, pcbData, dwFlags, debugstr_w(pwszBaseDir));
2772 /* Parameter sanity checks. */
2773 if (!hKey || (!pwszBuffer && cbBuffer) || (cbBuffer % sizeof(WCHAR))
2774 || ((dwFlags & REG_MUI_STRING_TRUNCATE) && pcbData)
2775 || (dwFlags & ~REG_MUI_STRING_TRUNCATE))
2776 return ERROR_INVALID_PARAMETER;
2778 /* Check for value existence and correctness of its type, allocate a buffer and load it. */
2779 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, NULL, &cbData);
2780 if (result != ERROR_SUCCESS) goto cleanup;
2781 if (!(dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ) || !cbData) {
2782 result = ERROR_FILE_NOT_FOUND;
2783 goto cleanup;
2785 pwszTempBuffer = heap_alloc(cbData);
2786 if (!pwszTempBuffer) {
2787 result = ERROR_NOT_ENOUGH_MEMORY;
2788 goto cleanup;
2790 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, (LPBYTE)pwszTempBuffer, &cbData);
2791 if (result != ERROR_SUCCESS) goto cleanup;
2793 /* '@' is the prefix for resource based string entries. */
2794 if (*pwszTempBuffer != '@') {
2795 result = ERROR_INVALID_DATA;
2796 goto cleanup;
2799 /* Expand environment variables regardless of the type. */
2800 cbData = ExpandEnvironmentStringsW(pwszTempBuffer, NULL, 0) * sizeof(WCHAR);
2801 if (!cbData) goto cleanup;
2802 pwszExpandedBuffer = heap_alloc(cbData);
2803 if (!pwszExpandedBuffer) {
2804 result = ERROR_NOT_ENOUGH_MEMORY;
2805 goto cleanup;
2807 ExpandEnvironmentStringsW(pwszTempBuffer, pwszExpandedBuffer, cbData / sizeof(WCHAR));
2809 /* Parse the value and load the string. */
2811 WCHAR *pComma = wcsrchr(pwszExpandedBuffer, ','), *pNewBuffer;
2812 UINT uiStringId;
2813 DWORD baseDirLen;
2814 int reqChars;
2816 /* Format of the expanded value is 'path_to_dll,-resId' */
2817 if (!pComma || pComma[1] != '-') {
2818 result = ERROR_INVALID_DATA;
2819 goto cleanup;
2822 uiStringId = wcstol(pComma+2, NULL, 10);
2823 *pComma = '\0';
2825 /* Build a resource dll path. */
2826 baseDirLen = pwszBaseDir ? lstrlenW(pwszBaseDir) : 0;
2827 cbData = (baseDirLen + 1 + lstrlenW(pwszExpandedBuffer + 1) + 1) * sizeof(WCHAR);
2828 pNewBuffer = heap_realloc(pwszTempBuffer, cbData);
2829 if (!pNewBuffer) {
2830 result = ERROR_NOT_ENOUGH_MEMORY;
2831 goto cleanup;
2833 pwszTempBuffer = pNewBuffer;
2834 pwszTempBuffer[0] = '\0';
2835 if (baseDirLen) {
2836 lstrcpyW(pwszTempBuffer, pwszBaseDir);
2837 if (pwszBaseDir[baseDirLen - 1] != '\\')
2838 lstrcatW(pwszTempBuffer, L"\\");
2840 lstrcatW(pwszTempBuffer, pwszExpandedBuffer + 1);
2842 /* Load specified string from the file */
2843 reqChars = 0;
2844 result = load_mui_string(pwszTempBuffer, uiStringId, pwszBuffer, cbBuffer/sizeof(WCHAR), &reqChars, dwFlags);
2845 if (pcbData && (result == ERROR_SUCCESS || result == ERROR_MORE_DATA))
2846 *pcbData = reqChars * sizeof(WCHAR);
2849 cleanup:
2850 heap_free(pwszTempBuffer);
2851 heap_free(pwszExpandedBuffer);
2852 return result;
2855 /******************************************************************************
2856 * RegLoadMUIStringA (kernelbase.@)
2858 * Not implemented on native.
2860 LSTATUS WINAPI RegLoadMUIStringA(HKEY hKey, LPCSTR pszValue, LPSTR pszBuffer, DWORD cbBuffer,
2861 LPDWORD pcbData, DWORD dwFlags, LPCSTR pszBaseDir)
2863 return ERROR_CALL_NOT_IMPLEMENTED;
2867 /******************************************************************************
2868 * RegDeleteTreeW (kernelbase.@)
2871 LSTATUS WINAPI RegDeleteTreeW( HKEY hkey, const WCHAR *subkey )
2873 DWORD name_size, max_name, max_subkey;
2874 WCHAR *name_buf = NULL;
2875 LONG ret;
2877 TRACE( "(%p, %s)\n", hkey, debugstr_w(subkey) );
2879 if (subkey && *subkey)
2881 ret = RegOpenKeyExW( hkey, subkey, 0, KEY_READ, &hkey );
2882 if (ret) return ret;
2885 ret = RegQueryInfoKeyW( hkey, NULL, NULL, NULL, NULL, &max_subkey,
2886 NULL, NULL, &max_name, NULL, NULL, NULL );
2887 if (ret)
2888 goto cleanup;
2890 max_name = max( max_subkey, max_name ) + 1;
2891 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
2893 ret = ERROR_NOT_ENOUGH_MEMORY;
2894 goto cleanup;
2897 /* Recursively delete subkeys */
2898 for (;;)
2900 name_size = max_name;
2901 ret = RegEnumKeyExW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2902 if (ret == ERROR_NO_MORE_ITEMS) break;
2903 if (ret) goto cleanup;
2904 ret = RegDeleteTreeW( hkey, name_buf );
2905 if (ret) goto cleanup;
2908 /* Delete the key itself */
2909 if (subkey && *subkey)
2911 ret = RegDeleteKeyExW( hkey, L"", 0, 0 );
2912 goto cleanup;
2915 /* Delete values */
2916 for (;;)
2918 name_size = max_name;
2919 ret = RegEnumValueW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2920 if (ret == ERROR_NO_MORE_ITEMS) break;
2921 if (ret) goto cleanup;
2922 ret = RegDeleteValueW( hkey, name_buf );
2923 if (ret) goto cleanup;
2926 ret = ERROR_SUCCESS;
2928 cleanup:
2929 heap_free( name_buf );
2930 if (subkey && *subkey)
2931 RegCloseKey( hkey );
2932 return ret;
2936 /******************************************************************************
2937 * RegDeleteTreeA (kernelbase.@)
2940 LSTATUS WINAPI RegDeleteTreeA( HKEY hkey, const char *subkey )
2942 UNICODE_STRING subkeyW;
2943 LONG ret;
2945 if (subkey) RtlCreateUnicodeStringFromAsciiz( &subkeyW, subkey );
2946 else subkeyW.Buffer = NULL;
2947 ret = RegDeleteTreeW( hkey, subkeyW.Buffer );
2948 RtlFreeUnicodeString( &subkeyW );
2949 return ret;
2953 /******************************************************************************
2954 * RegCopyTreeW (kernelbase.@)
2957 LSTATUS WINAPI RegCopyTreeW( HKEY hsrc, const WCHAR *subkey, HKEY hdst )
2959 DWORD name_size, max_name;
2960 DWORD value_size, max_value;
2961 DWORD max_subkey, i, type;
2962 WCHAR *name_buf = NULL;
2963 BYTE *value_buf = NULL;
2964 HKEY hkey;
2965 LONG ret;
2967 TRACE( "(%p, %s, %p)\n", hsrc, debugstr_w(subkey), hdst );
2969 if (subkey)
2971 ret = RegOpenKeyExW( hsrc, subkey, 0, KEY_READ, &hsrc );
2972 if (ret) return ret;
2975 ret = RegQueryInfoKeyW( hsrc, NULL, NULL, NULL, NULL, &max_subkey,
2976 NULL, NULL, &max_name, &max_value, NULL, NULL );
2977 if (ret)
2978 goto cleanup;
2980 max_name = max( max_subkey, max_name ) + 1;
2981 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
2983 ret = ERROR_NOT_ENOUGH_MEMORY;
2984 goto cleanup;
2987 if (!(value_buf = heap_alloc( max_value )))
2989 ret = ERROR_NOT_ENOUGH_MEMORY;
2990 goto cleanup;
2993 /* Copy values */
2994 for (i = 0;; i++)
2996 name_size = max_name;
2997 value_size = max_value;
2998 ret = RegEnumValueW( hsrc, i, name_buf, &name_size, NULL, &type, value_buf, &value_size );
2999 if (ret == ERROR_NO_MORE_ITEMS) break;
3000 if (ret) goto cleanup;
3001 ret = RegSetValueExW( hdst, name_buf, 0, type, value_buf, value_size );
3002 if (ret) goto cleanup;
3005 /* Recursively copy subkeys */
3006 for (i = 0;; i++)
3008 name_size = max_name;
3009 ret = RegEnumKeyExW( hsrc, i, name_buf, &name_size, NULL, NULL, NULL, NULL );
3010 if (ret == ERROR_NO_MORE_ITEMS) break;
3011 if (ret) goto cleanup;
3012 ret = RegCreateKeyExW( hdst, name_buf, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL );
3013 if (ret) goto cleanup;
3014 ret = RegCopyTreeW( hsrc, name_buf, hkey );
3015 RegCloseKey( hkey );
3016 if (ret) goto cleanup;
3019 ret = ERROR_SUCCESS;
3021 cleanup:
3022 heap_free( name_buf );
3023 heap_free( value_buf );
3024 if (subkey)
3025 RegCloseKey( hsrc );
3026 return ret;
3030 /******************************************************************************
3031 * RegLoadAppKeyA (kernelbase.@)
3034 LSTATUS WINAPI RegLoadAppKeyA(const char *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3036 FIXME("%s %p %u %u %u: stub\n", wine_dbgstr_a(file), result, sam, options, reserved);
3038 if (!file || reserved)
3039 return ERROR_INVALID_PARAMETER;
3041 *result = (HKEY)0xdeadbeef;
3042 return ERROR_SUCCESS;
3045 /******************************************************************************
3046 * RegLoadAppKeyW (kernelbase.@)
3049 LSTATUS WINAPI RegLoadAppKeyW(const WCHAR *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3051 FIXME("%s %p %u %u %u: stub\n", wine_dbgstr_w(file), result, sam, options, reserved);
3053 if (!file || reserved)
3054 return ERROR_INVALID_PARAMETER;
3056 *result = (HKEY)0xdeadbeef;
3057 return ERROR_SUCCESS;
3061 /***********************************************************************
3062 * DnsHostnameToComputerNameExW (kernelbase.@)
3064 * FIXME: how is this different from the non-Ex function?
3066 BOOL WINAPI DECLSPEC_HOTPATCH DnsHostnameToComputerNameExW( const WCHAR *hostname, WCHAR *computername,
3067 DWORD *size )
3069 static const WCHAR allowed[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&')(-_{}";
3070 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3071 DWORD i, len;
3073 lstrcpynW( buffer, hostname, MAX_COMPUTERNAME_LENGTH + 1 );
3074 len = lstrlenW( buffer );
3075 if (*size < len + 1)
3077 *size = len;
3078 SetLastError( ERROR_MORE_DATA );
3079 return FALSE;
3081 *size = len;
3082 if (!computername) return FALSE;
3083 for (i = 0; i < len; i++)
3085 if (buffer[i] >= 'a' && buffer[i] <= 'z') computername[i] = buffer[i] + 'A' - 'a';
3086 else computername[i] = wcschr( allowed, buffer[i] ) ? buffer[i] : '_';
3088 computername[len] = 0;
3089 return TRUE;
3093 /***********************************************************************
3094 * GetComputerNameExA (kernelbase.@)
3096 BOOL WINAPI GetComputerNameExA( COMPUTER_NAME_FORMAT type, char *name, DWORD *len )
3098 BOOL ret = FALSE;
3099 DWORD lenA, lenW = 0;
3100 WCHAR *buffer;
3102 GetComputerNameExW( type, NULL, &lenW );
3103 if (GetLastError() != ERROR_MORE_DATA) return FALSE;
3105 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
3107 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3108 return FALSE;
3110 if (GetComputerNameExW( type, buffer, &lenW ))
3112 lenA = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
3113 if (lenA > *len)
3115 *len = lenA;
3116 SetLastError( ERROR_MORE_DATA );
3118 else
3120 WideCharToMultiByte( CP_ACP, 0, buffer, -1, name, *len, NULL, NULL );
3121 *len = lenA - 1;
3122 ret = TRUE;
3125 HeapFree( GetProcessHeap(), 0, buffer );
3126 return ret;
3130 /***********************************************************************
3131 * GetComputerNameExW (kernelbase.@)
3133 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, WCHAR *name, DWORD *len )
3135 const WCHAR *keyname, *valuename;
3136 LRESULT ret;
3137 HKEY key;
3139 switch (type)
3141 case ComputerNameNetBIOS:
3142 case ComputerNamePhysicalNetBIOS:
3143 keyname = L"System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName";
3144 valuename = L"ComputerName";
3145 break;
3146 case ComputerNameDnsHostname:
3147 case ComputerNamePhysicalDnsHostname:
3148 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3149 valuename = L"Hostname";
3150 break;
3151 case ComputerNameDnsDomain:
3152 case ComputerNamePhysicalDnsDomain:
3153 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3154 valuename = L"Domain";
3155 break;
3156 case ComputerNameDnsFullyQualified:
3157 case ComputerNamePhysicalDnsFullyQualified:
3159 WCHAR *domain, buffer[256];
3160 DWORD size = ARRAY_SIZE(buffer) - 1;
3162 if (!GetComputerNameExW( ComputerNameDnsHostname, buffer, &size )) return FALSE;
3163 domain = buffer + lstrlenW(buffer);
3164 *domain++ = '.';
3165 size = ARRAY_SIZE(buffer) - (domain - buffer);
3166 if (!GetComputerNameExW( ComputerNameDnsDomain, domain, &size )) return FALSE;
3167 if (!*domain) domain[-1] = 0;
3168 size = lstrlenW(buffer);
3169 if (name && size < *len)
3171 if (name) lstrcpyW( name, buffer );
3172 *len = size;
3173 return TRUE;
3175 *len = size + 1;
3176 SetLastError( ERROR_MORE_DATA );
3177 return FALSE;
3179 default:
3180 SetLastError( ERROR_INVALID_PARAMETER );
3181 return FALSE;
3184 if (!(ret = RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &key )))
3186 DWORD size = *len * sizeof(WCHAR);
3187 ret = RegQueryValueExW( key, valuename, NULL, NULL, (BYTE *)name, &size );
3188 if (!name) ret = ERROR_MORE_DATA;
3189 else if (!ret) size -= sizeof(WCHAR);
3190 *len = size / sizeof(WCHAR);
3191 RegCloseKey( key );
3193 TRACE("-> %lu %s\n", ret, debugstr_w(name) );
3194 if (ret) SetLastError( ret );
3195 return !ret;
3199 /***********************************************************************
3200 * SetComputerNameA (kernelbase.@)
3202 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameA( const char *name )
3204 BOOL ret;
3205 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3206 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3208 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3209 ret = SetComputerNameExW( ComputerNamePhysicalNetBIOS, nameW );
3210 HeapFree( GetProcessHeap(), 0, nameW );
3211 return ret;
3215 /***********************************************************************
3216 * SetComputerNameW (kernelbase.@)
3218 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameW( const WCHAR *name )
3220 return SetComputerNameExW( ComputerNamePhysicalNetBIOS, name );
3224 /***********************************************************************
3225 * SetComputerNameExA (kernelbase.@)
3227 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExA( COMPUTER_NAME_FORMAT type, const char *name )
3229 BOOL ret;
3230 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3231 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3233 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3234 ret = SetComputerNameExW( type, nameW );
3235 HeapFree( GetProcessHeap(), 0, nameW );
3236 return ret;
3240 /***********************************************************************
3241 * SetComputerNameExW (kernelbase.@)
3243 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExW( COMPUTER_NAME_FORMAT type, const WCHAR *name )
3245 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3246 DWORD size;
3247 HKEY key;
3248 LRESULT ret;
3250 TRACE( "%u %s\n", type, debugstr_w( name ));
3252 switch (type)
3254 case ComputerNameDnsHostname:
3255 case ComputerNamePhysicalDnsHostname:
3256 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3257 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3258 if (ret) break;
3259 ret = RegSetValueExW( key, L"Hostname", 0, REG_SZ,
3260 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3261 RegCloseKey( key );
3262 /* fall through */
3264 case ComputerNameNetBIOS:
3265 case ComputerNamePhysicalNetBIOS:
3266 /* @@ Wine registry key: HKCU\Software\Wine\Network */
3267 if (!RegOpenKeyExW( HKEY_CURRENT_USER, L"Software\\Wine\\Network", 0, KEY_READ, &key ))
3269 BOOL use_dns = TRUE;
3270 size = sizeof(buffer);
3271 if (!RegQueryValueExW( key, L"UseDnsComputerName", NULL, NULL, (BYTE *)buffer, &size ))
3272 use_dns = IS_OPTION_TRUE( buffer[0] );
3273 RegCloseKey( key );
3274 if (!use_dns)
3276 ret = ERROR_ACCESS_DENIED;
3277 break;
3280 size = ARRAY_SIZE( buffer );
3281 if (!DnsHostnameToComputerNameExW( name, buffer, &size )) return FALSE;
3282 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
3283 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3284 if (ret) break;
3285 ret = RegSetValueExW( key, L"ComputerName", 0, REG_SZ,
3286 (BYTE *)buffer, (lstrlenW(buffer) + 1) * sizeof(WCHAR) );
3287 RegCloseKey( key );
3288 break;
3290 case ComputerNameDnsDomain:
3291 case ComputerNamePhysicalDnsDomain:
3292 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3293 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3294 if (ret) break;
3295 ret = RegSetValueExW( key, L"Domain", 0, REG_SZ,
3296 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3297 RegCloseKey( key );
3298 break;
3299 default:
3300 ret = ERROR_INVALID_PARAMETER;
3301 break;
3303 if (ret) SetLastError( ret );
3304 return !ret;
3307 struct USKEY
3309 HKEY HKCUstart; /* Start key in CU hive */
3310 HKEY HKCUkey; /* Opened key in CU hive */
3311 HKEY HKLMstart; /* Start key in LM hive */
3312 HKEY HKLMkey; /* Opened key in LM hive */
3313 WCHAR path[MAX_PATH];
3316 LONG WINAPI SHRegCreateUSKeyA(LPCSTR path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3318 WCHAR *pathW;
3319 LONG ret;
3321 TRACE("%s, %#x, %p, %p, %#x\n", debugstr_a(path), samDesired, relative_key, new_uskey, flags);
3323 if (path)
3325 INT len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
3326 pathW = heap_alloc(len * sizeof(WCHAR));
3327 if (!pathW)
3328 return ERROR_NOT_ENOUGH_MEMORY;
3329 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
3331 else
3332 pathW = NULL;
3334 ret = SHRegCreateUSKeyW(pathW, samDesired, relative_key, new_uskey, flags);
3335 HeapFree(GetProcessHeap(), 0, pathW);
3336 return ret;
3339 static HKEY reg_duplicate_hkey(HKEY hKey)
3341 HKEY newKey = 0;
3343 RegOpenKeyExW(hKey, 0, 0, MAXIMUM_ALLOWED, &newKey);
3344 return newKey;
3347 static HKEY reg_get_hkey_from_huskey(HUSKEY hUSKey, BOOL is_hkcu)
3349 struct USKEY *mihk = hUSKey;
3350 HKEY test = hUSKey;
3352 if (test == HKEY_CLASSES_ROOT
3353 || test == HKEY_CURRENT_CONFIG
3354 || test == HKEY_CURRENT_USER
3355 || test == HKEY_DYN_DATA
3356 || test == HKEY_LOCAL_MACHINE
3357 || test == HKEY_PERFORMANCE_DATA
3358 || test == HKEY_USERS)
3359 /* FIXME: need to define for Win2k, ME, XP
3360 * (test == HKEY_PERFORMANCE_TEXT) ||
3361 * (test == HKEY_PERFORMANCE_NLSTEXT) ||
3364 return test;
3367 return is_hkcu ? mihk->HKCUkey : mihk->HKLMkey;
3370 LONG WINAPI SHRegCreateUSKeyW(const WCHAR *path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3372 LONG ret = ERROR_CALL_NOT_IMPLEMENTED;
3373 struct USKEY *ret_key;
3375 TRACE("%s, %#x, %p, %p, %#x\n", debugstr_w(path), samDesired, relative_key, new_uskey, flags);
3377 if (!new_uskey)
3378 return ERROR_INVALID_PARAMETER;
3380 *new_uskey = NULL;
3382 if (flags & ~SHREGSET_FORCE_HKCU)
3384 FIXME("unsupported flags 0x%08x\n", flags);
3385 return ERROR_SUCCESS;
3388 ret_key = heap_alloc_zero(sizeof(*ret_key));
3389 lstrcpynW(ret_key->path, path, ARRAY_SIZE(ret_key->path));
3391 if (relative_key)
3393 ret_key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3394 ret_key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3396 else
3398 ret_key->HKCUstart = HKEY_CURRENT_USER;
3399 ret_key->HKLMstart = HKEY_LOCAL_MACHINE;
3402 if (flags & SHREGSET_FORCE_HKCU)
3404 ret = RegCreateKeyExW(ret_key->HKCUstart, path, 0, NULL, 0, samDesired, NULL, &ret_key->HKCUkey, NULL);
3405 if (ret == ERROR_SUCCESS)
3406 *new_uskey = ret_key;
3407 else
3408 heap_free(ret_key);
3411 return ret;
3414 LONG WINAPI SHRegCloseUSKey(HUSKEY hUSKey)
3416 struct USKEY *key = hUSKey;
3417 LONG ret = ERROR_SUCCESS;
3419 if (!key)
3420 return ERROR_INVALID_PARAMETER;
3422 if (key->HKCUkey)
3423 ret = RegCloseKey(key->HKCUkey);
3424 if (key->HKCUstart && key->HKCUstart != HKEY_CURRENT_USER)
3425 ret = RegCloseKey(key->HKCUstart);
3426 if (key->HKLMkey)
3427 ret = RegCloseKey(key->HKLMkey);
3428 if (key->HKLMstart && key->HKLMstart != HKEY_LOCAL_MACHINE)
3429 ret = RegCloseKey(key->HKLMstart);
3431 heap_free(key);
3432 return ret;
3435 LONG WINAPI SHRegDeleteEmptyUSKeyA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3437 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3438 return ERROR_SUCCESS;
3441 LONG WINAPI SHRegDeleteEmptyUSKeyW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3443 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3444 return ERROR_SUCCESS;
3447 LONG WINAPI SHRegDeleteUSValueA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3449 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3450 return ERROR_SUCCESS;
3453 LONG WINAPI SHRegDeleteUSValueW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3455 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3456 return ERROR_SUCCESS;
3459 LONG WINAPI SHRegEnumUSValueA(HUSKEY hUSKey, DWORD index, char *value_name, DWORD *value_name_len, DWORD *type,
3460 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3462 HKEY dokey;
3464 TRACE("%p, %#x, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3466 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3467 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3469 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3470 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3472 FIXME("no support for SHREGENUM_BOTH\n");
3473 return ERROR_INVALID_FUNCTION;
3476 LONG WINAPI SHRegEnumUSValueW(HUSKEY hUSKey, DWORD index, WCHAR *value_name, DWORD *value_name_len, DWORD *type,
3477 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3479 HKEY dokey;
3481 TRACE("%p, %#x, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3483 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3484 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3486 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3487 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3489 FIXME("no support for SHREGENUM_BOTH\n");
3490 return ERROR_INVALID_FUNCTION;
3493 LONG WINAPI SHRegEnumUSKeyA(HUSKEY hUSKey, DWORD index, char *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3495 HKEY dokey;
3497 TRACE("%p, %d, %p, %p(%d), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3499 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3500 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3502 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3503 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3505 FIXME("no support for SHREGENUM_BOTH\n");
3506 return ERROR_INVALID_FUNCTION;
3509 LONG WINAPI SHRegEnumUSKeyW(HUSKEY hUSKey, DWORD index, WCHAR *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3511 HKEY dokey;
3513 TRACE("%p, %d, %p, %p(%d), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3515 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3516 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3518 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3519 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3521 FIXME("no support for SHREGENUM_BOTH\n");
3522 return ERROR_INVALID_FUNCTION;
3525 LONG WINAPI SHRegOpenUSKeyA(const char *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3527 WCHAR pathW[MAX_PATH];
3529 if (path)
3530 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, ARRAY_SIZE(pathW));
3532 return SHRegOpenUSKeyW(path ? pathW : NULL, access_mask, relative_key, uskey, ignore_hkcu);
3535 LONG WINAPI SHRegOpenUSKeyW(const WCHAR *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3537 LONG ret2, ret1 = ~ERROR_SUCCESS;
3538 struct USKEY *key;
3540 TRACE("%s, %#x, %p, %p, %d\n", debugstr_w(path), access_mask, relative_key, uskey, ignore_hkcu);
3542 if (uskey)
3543 *uskey = NULL;
3545 /* Create internal HUSKEY */
3546 key = heap_alloc_zero(sizeof(*key));
3547 lstrcpynW(key->path, path, ARRAY_SIZE(key->path));
3549 if (relative_key)
3551 key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3552 key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3554 /* FIXME: if either of these keys is NULL, create the start key from
3555 * the relative keys start+path
3558 else
3560 key->HKCUstart = HKEY_CURRENT_USER;
3561 key->HKLMstart = HKEY_LOCAL_MACHINE;
3564 if (!ignore_hkcu)
3566 ret1 = RegOpenKeyExW(key->HKCUstart, key->path, 0, access_mask, &key->HKCUkey);
3567 if (ret1)
3568 key->HKCUkey = 0;
3571 ret2 = RegOpenKeyExW(key->HKLMstart, key->path, 0, access_mask, &key->HKLMkey);
3572 if (ret2)
3573 key->HKLMkey = 0;
3575 if (ret1 || ret2)
3576 TRACE("one or more opens failed: HKCU=%d HKLM=%d\n", ret1, ret2);
3578 if (ret1 && ret2)
3580 /* Neither open succeeded: fail */
3581 SHRegCloseUSKey(key);
3582 return ret2;
3585 TRACE("HUSKEY=%p\n", key);
3586 if (uskey)
3587 *uskey = key;
3589 return ERROR_SUCCESS;
3592 LONG WINAPI SHRegWriteUSValueA(HUSKEY hUSKey, const char *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3594 WCHAR valueW[MAX_PATH];
3596 if (value)
3597 MultiByteToWideChar(CP_ACP, 0, value, -1, valueW, ARRAY_SIZE(valueW));
3599 return SHRegWriteUSValueW(hUSKey, value ? valueW : NULL, type, data, data_len, flags);
3602 LONG WINAPI SHRegWriteUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3604 struct USKEY *hKey = hUSKey;
3605 LONG ret = ERROR_SUCCESS;
3606 DWORD dummy;
3608 TRACE("%p, %s, %d, %p, %d, %#x\n", hUSKey, debugstr_w(value), type, data, data_len, flags);
3610 __TRY
3612 dummy = hKey->HKCUkey || hKey->HKLMkey;
3614 __EXCEPT_PAGE_FAULT
3616 return ERROR_INVALID_PARAMETER;
3618 __ENDTRY
3619 if (!(flags & (SHREGSET_FORCE_HKCU|SHREGSET_FORCE_HKLM))) return ERROR_INVALID_PARAMETER;
3621 if (flags & (SHREGSET_FORCE_HKCU | SHREGSET_HKCU))
3623 if (!hKey->HKCUkey)
3625 /* Create the key */
3626 ret = RegCreateKeyExW(hKey->HKCUstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3627 MAXIMUM_ALLOWED, NULL, &hKey->HKCUkey, NULL);
3628 TRACE("Creating HKCU key, ret = %d\n", ret);
3629 if (ret && (flags & SHREGSET_FORCE_HKCU))
3631 hKey->HKCUkey = 0;
3632 return ret;
3636 if (!ret)
3638 if ((flags & SHREGSET_FORCE_HKCU) || RegQueryValueExW(hKey->HKCUkey, value, NULL, NULL, NULL, &dummy))
3640 /* Doesn't exist or we are forcing: Write value */
3641 ret = RegSetValueExW(hKey->HKCUkey, value, 0, type, data, data_len);
3642 TRACE("Writing HKCU value, ret = %d\n", ret);
3647 if (flags & (SHREGSET_FORCE_HKLM | SHREGSET_HKLM))
3649 if (!hKey->HKLMkey)
3651 /* Create the key */
3652 ret = RegCreateKeyExW(hKey->HKLMstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3653 MAXIMUM_ALLOWED, NULL, &hKey->HKLMkey, NULL);
3654 TRACE("Creating HKLM key, ret = %d\n", ret);
3655 if (ret && (flags & (SHREGSET_FORCE_HKLM)))
3657 hKey->HKLMkey = 0;
3658 return ret;
3662 if (!ret)
3664 if ((flags & SHREGSET_FORCE_HKLM) || RegQueryValueExW(hKey->HKLMkey, value, NULL, NULL, NULL, &dummy))
3666 /* Doesn't exist or we are forcing: Write value */
3667 ret = RegSetValueExW(hKey->HKLMkey, value, 0, type, data, data_len);
3668 TRACE("Writing HKLM value, ret = %d\n", ret);
3673 return ret;
3676 LONG WINAPI SHRegSetUSValueA(const char *subkey, const char *value, DWORD type, void *data, DWORD data_len,
3677 DWORD flags)
3679 BOOL ignore_hkcu;
3680 HUSKEY hkey;
3681 LONG ret;
3683 TRACE("%s, %s, %d, %p, %d, %#x\n", debugstr_a(subkey), debugstr_a(value), type, data, data_len, flags);
3685 if (!data)
3686 return ERROR_INVALID_FUNCTION;
3688 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3690 ret = SHRegOpenUSKeyA(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3691 if (ret == ERROR_SUCCESS)
3693 ret = SHRegWriteUSValueA(hkey, value, type, data, data_len, flags);
3694 SHRegCloseUSKey(hkey);
3697 return ret;
3700 LONG WINAPI SHRegSetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD type, void *data, DWORD data_len,
3701 DWORD flags)
3703 BOOL ignore_hkcu;
3704 HUSKEY hkey;
3705 LONG ret;
3707 TRACE("%s, %s, %d, %p, %d, %#x\n", debugstr_w(subkey), debugstr_w(value), type, data, data_len, flags);
3709 if (!data)
3710 return ERROR_INVALID_FUNCTION;
3712 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3714 ret = SHRegOpenUSKeyW(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3715 if (ret == ERROR_SUCCESS)
3717 ret = SHRegWriteUSValueW(hkey, value, type, data, data_len, flags);
3718 SHRegCloseUSKey(hkey);
3721 return ret;
3724 LONG WINAPI SHRegQueryInfoUSKeyA(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3725 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3727 HKEY dokey;
3728 LONG ret;
3730 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3732 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3734 ret = RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3735 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3736 return ret;
3739 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3741 return RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3744 return ERROR_INVALID_FUNCTION;
3747 LONG WINAPI SHRegQueryInfoUSKeyW(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3748 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3750 HKEY dokey;
3751 LONG ret;
3753 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3755 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3757 ret = RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3758 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3759 return ret;
3762 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3764 return RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3767 return ERROR_INVALID_FUNCTION;
3770 LONG WINAPI SHRegQueryUSValueA(HUSKEY hUSKey, const char *value, DWORD *type, void *data, DWORD *data_len,
3771 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3773 LONG ret = ~ERROR_SUCCESS;
3774 DWORD move_len;
3775 HKEY dokey;
3777 /* If user wants HKCU, and it exists, then try it */
3778 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3780 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3781 TRACE("HKCU RegQueryValue returned %d\n", ret);
3784 /* If HKCU did not work and HKLM exists, then try it */
3785 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3787 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3788 TRACE("HKLM RegQueryValue returned %d\n", ret);
3791 /* If neither worked, and default data exists, then use it */
3792 if (ret != ERROR_SUCCESS)
3794 if (default_data && default_data_len)
3796 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3797 memmove(data, default_data, move_len);
3798 *data_len = move_len;
3799 TRACE("setting default data\n");
3800 ret = ERROR_SUCCESS;
3804 return ret;
3807 LONG WINAPI SHRegQueryUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3808 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3810 LONG ret = ~ERROR_SUCCESS;
3811 DWORD move_len;
3812 HKEY dokey;
3814 /* If user wants HKCU, and it exists, then try it */
3815 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3817 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3818 TRACE("HKCU RegQueryValue returned %d\n", ret);
3821 /* If HKCU did not work and HKLM exists, then try it */
3822 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3824 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3825 TRACE("HKLM RegQueryValue returned %d\n", ret);
3828 /* If neither worked, and default data exists, then use it */
3829 if (ret != ERROR_SUCCESS)
3831 if (default_data && default_data_len)
3833 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3834 memmove(data, default_data, move_len);
3835 *data_len = move_len;
3836 TRACE("setting default data\n");
3837 ret = ERROR_SUCCESS;
3841 return ret;
3844 LONG WINAPI SHRegGetUSValueA(const char *subkey, const char *value, DWORD *type, void *data, DWORD *data_len,
3845 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3847 HUSKEY myhuskey;
3848 LONG ret;
3850 if (!data || !data_len)
3851 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3853 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), *data_len);
3855 ret = SHRegOpenUSKeyA(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3856 if (!ret)
3858 ret = SHRegQueryUSValueA(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3859 SHRegCloseUSKey(myhuskey);
3862 return ret;
3865 LONG WINAPI SHRegGetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3866 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3868 HUSKEY myhuskey;
3869 LONG ret;
3871 if (!data || !data_len)
3872 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3874 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), *data_len);
3876 ret = SHRegOpenUSKeyW(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3877 if (!ret)
3879 ret = SHRegQueryUSValueW(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3880 SHRegCloseUSKey(myhuskey);
3883 return ret;
3886 BOOL WINAPI SHRegGetBoolUSValueA(const char *subkey, const char *value, BOOL ignore_hkcu, BOOL default_value)
3888 BOOL ret = default_value;
3889 DWORD type, datalen;
3890 char data[10];
3892 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), ignore_hkcu);
3894 datalen = ARRAY_SIZE(data) - 1;
3895 if (!SHRegGetUSValueA(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3897 switch (type)
3899 case REG_SZ:
3900 data[9] = '\0';
3901 if (!lstrcmpiA(data, "YES") || !lstrcmpiA(data, "TRUE"))
3902 ret = TRUE;
3903 else if (!lstrcmpiA(data, "NO") || !lstrcmpiA(data, "FALSE"))
3904 ret = FALSE;
3905 break;
3906 case REG_DWORD:
3907 ret = *(DWORD *)data != 0;
3908 break;
3909 case REG_BINARY:
3910 if (datalen == 1)
3912 ret = !!data[0];
3913 break;
3915 default:
3916 FIXME("Unsupported registry data type %d\n", type);
3917 ret = FALSE;
3919 TRACE("got value (type=%d), returning %d\n", type, ret);
3921 else
3922 TRACE("returning default value %d\n", ret);
3924 return ret;
3927 BOOL WINAPI SHRegGetBoolUSValueW(const WCHAR *subkey, const WCHAR *value, BOOL ignore_hkcu, BOOL default_value)
3929 BOOL ret = default_value;
3930 DWORD type, datalen;
3931 WCHAR data[10];
3933 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), ignore_hkcu);
3935 datalen = (ARRAY_SIZE(data) - 1) * sizeof(WCHAR);
3936 if (!SHRegGetUSValueW(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3938 switch (type)
3940 case REG_SZ:
3941 data[9] = '\0';
3942 if (!lstrcmpiW(data, L"yes") || !lstrcmpiW(data, L"true"))
3943 ret = TRUE;
3944 else if (!lstrcmpiW(data, L"no") || !lstrcmpiW(data, L"false"))
3945 ret = FALSE;
3946 break;
3947 case REG_DWORD:
3948 ret = *(DWORD *)data != 0;
3949 break;
3950 case REG_BINARY:
3951 if (datalen == 1)
3953 ret = !!data[0];
3954 break;
3956 default:
3957 FIXME("Unsupported registry data type %d\n", type);
3958 ret = FALSE;
3960 TRACE("got value (type=%d), returning %d\n", type, ret);
3962 else
3963 TRACE("returning default value %d\n", ret);
3965 return ret;