mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / kernelbase / registry.c
blob2ed5e2681199bdc581d39154d8c693fd43a93666
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 L"\\Registry\\PerfData",
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 /* wrapper for NtCreateKey that creates the key recursively if necessary */
125 static NTSTATUS create_key( HKEY *retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
126 const UNICODE_STRING *class, ULONG options, PULONG dispos )
128 BOOL force_wow32 = is_win64 && (access & KEY_WOW64_32KEY);
129 NTSTATUS status = STATUS_OBJECT_NAME_NOT_FOUND;
130 HANDLE subkey, root = attr->RootDirectory;
132 if (!force_wow32) status = NtCreateKey( &subkey, access, attr, 0, class, options, dispos );
134 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
136 WCHAR *buffer = attr->ObjectName->Buffer;
137 DWORD attrs, pos = 0, i = 0, len = attr->ObjectName->Length / sizeof(WCHAR);
138 UNICODE_STRING str;
140 /* don't try to create registry root */
141 if (!attr->RootDirectory && len > 10 && !wcsnicmp( buffer, L"\\Registry\\", 10 )) i += 10;
143 while (i < len && buffer[i] != '\\') i++;
144 if (i == len && !force_wow32) return status;
146 attrs = attr->Attributes;
147 attr->ObjectName = &str;
149 for (;;)
151 str.Buffer = buffer + pos;
152 str.Length = (i - pos) * sizeof(WCHAR);
153 if (force_wow32 && pos)
155 if (is_wow6432node( &str )) force_wow32 = FALSE;
156 else if ((subkey = open_wow6432node( attr->RootDirectory )))
158 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
159 attr->RootDirectory = subkey;
160 force_wow32 = FALSE;
163 if (i == len)
165 attr->Attributes = attrs;
166 status = NtCreateKey( &subkey, access, attr, 0, class, options, dispos );
168 else
170 attr->Attributes = attrs & ~OBJ_OPENLINK;
171 status = NtCreateKey( &subkey, access, attr, 0, class,
172 options & ~REG_OPTION_CREATE_LINK, dispos );
174 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
175 if (status) return status;
176 if (i == len) break;
177 attr->RootDirectory = subkey;
178 while (i < len && buffer[i] == '\\') i++;
179 pos = i;
180 while (i < len && buffer[i] != '\\') i++;
183 attr->RootDirectory = subkey;
184 if (force_wow32 && (subkey = open_wow6432node( attr->RootDirectory )))
186 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
187 attr->RootDirectory = subkey;
189 *retkey = attr->RootDirectory;
190 return status;
193 /* wrapper for NtOpenKeyEx to handle Wow6432 nodes */
194 static NTSTATUS open_key( HKEY *retkey, DWORD options, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
196 NTSTATUS status;
197 BOOL force_wow32 = is_win64 && (access & KEY_WOW64_32KEY);
198 HANDLE subkey, root = attr->RootDirectory;
199 WCHAR *buffer = attr->ObjectName->Buffer;
200 DWORD pos = 0, i = 0, len = attr->ObjectName->Length / sizeof(WCHAR);
201 UNICODE_STRING str;
203 *retkey = NULL;
205 if (!force_wow32)
207 if (options & REG_OPTION_OPEN_LINK) attr->Attributes |= OBJ_OPENLINK;
208 return NtOpenKeyEx( (HANDLE *)retkey, access, attr, options );
211 if (len && buffer[0] == '\\') return STATUS_OBJECT_PATH_INVALID;
212 while (i < len && buffer[i] != '\\') i++;
213 attr->ObjectName = &str;
215 for (;;)
217 str.Buffer = buffer + pos;
218 str.Length = (i - pos) * sizeof(WCHAR);
219 if (force_wow32 && pos)
221 if (is_wow6432node( &str )) force_wow32 = FALSE;
222 else if ((subkey = open_wow6432node( attr->RootDirectory )))
224 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
225 attr->RootDirectory = subkey;
226 force_wow32 = FALSE;
229 if (i == len)
231 if (options & REG_OPTION_OPEN_LINK) attr->Attributes |= OBJ_OPENLINK;
232 status = NtOpenKeyEx( &subkey, access, attr, options );
234 else
236 if (!(options & REG_OPTION_OPEN_LINK)) attr->Attributes &= ~OBJ_OPENLINK;
237 status = NtOpenKeyEx( &subkey, access, attr, options & ~REG_OPTION_OPEN_LINK );
239 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
240 if (status) return status;
241 attr->RootDirectory = subkey;
242 if (i == len) break;
243 while (i < len && buffer[i] == '\\') i++;
244 pos = i;
245 while (i < len && buffer[i] != '\\') i++;
247 if (force_wow32 && (subkey = open_wow6432node( attr->RootDirectory )))
249 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
250 attr->RootDirectory = subkey;
252 *retkey = attr->RootDirectory;
253 return status;
256 /* create one of the HKEY_* special root keys */
257 static HKEY create_special_root_hkey( HKEY hkey, DWORD access )
259 HKEY ret = 0;
260 int idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
262 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CURRENT_USER))
264 if (RtlOpenCurrentUser( access, (HANDLE *)&hkey )) return 0;
265 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
267 else
269 OBJECT_ATTRIBUTES attr;
270 UNICODE_STRING name;
272 attr.Length = sizeof(attr);
273 attr.RootDirectory = 0;
274 attr.ObjectName = &name;
275 attr.Attributes = 0;
276 attr.SecurityDescriptor = NULL;
277 attr.SecurityQualityOfService = NULL;
278 RtlInitUnicodeString( &name, root_key_names[idx] );
279 if (create_key( &hkey, access, &attr, NULL, 0, NULL )) return 0;
280 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
283 if (!cache_disabled[idx] && !(access & (KEY_WOW64_64KEY | KEY_WOW64_32KEY)))
285 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
286 ret = hkey;
287 else
288 NtClose( hkey ); /* somebody beat us to it */
290 else
291 ret = hkey;
292 return ret;
295 /* map the hkey from special root to normal key if necessary */
296 static inline HKEY get_special_root_hkey( HKEY hkey, REGSAM access )
298 HKEY ret = hkey;
300 if ((HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
301 && (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
303 REGSAM mask = 0;
305 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT))
306 mask = KEY_WOW64_32KEY | KEY_WOW64_64KEY;
308 if ((access & mask) ||
309 !(ret = special_root_keys[HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)]))
310 ret = create_special_root_hkey( hkey, MAXIMUM_ALLOWED | (access & mask) );
312 return ret;
316 /******************************************************************************
317 * RemapPredefinedHandleInternal (kernelbase.@)
319 NTSTATUS WINAPI RemapPredefinedHandleInternal( HKEY hkey, HKEY override )
321 HKEY old_key;
322 int idx;
324 TRACE("(%p %p)\n", hkey, override);
326 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
327 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
328 return STATUS_INVALID_HANDLE;
329 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
331 if (override)
333 NTSTATUS status = NtDuplicateObject( GetCurrentProcess(), override,
334 GetCurrentProcess(), (HANDLE *)&override,
335 0, 0, DUPLICATE_SAME_ACCESS );
336 if (status) return status;
339 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], override );
340 if (old_key) NtClose( old_key );
341 return STATUS_SUCCESS;
345 /******************************************************************************
346 * DisablePredefinedHandleTableInternal (kernelbase.@)
348 NTSTATUS WINAPI DisablePredefinedHandleTableInternal( HKEY hkey )
350 HKEY old_key;
351 int idx;
353 TRACE("(%p)\n", hkey);
355 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
356 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
357 return STATUS_INVALID_HANDLE;
358 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
360 cache_disabled[idx] = TRUE;
362 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], NULL );
363 if (old_key) NtClose( old_key );
364 return STATUS_SUCCESS;
368 /******************************************************************************
369 * RegCreateKeyExW (kernelbase.@)
371 * See RegCreateKeyExA.
373 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
374 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
375 PHKEY retkey, LPDWORD dispos )
377 OBJECT_ATTRIBUTES attr;
378 UNICODE_STRING nameW, classW;
380 if (reserved) return ERROR_INVALID_PARAMETER;
381 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
383 attr.Length = sizeof(attr);
384 attr.RootDirectory = hkey;
385 attr.ObjectName = &nameW;
386 attr.Attributes = 0;
387 attr.SecurityDescriptor = NULL;
388 attr.SecurityQualityOfService = NULL;
389 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
390 RtlInitUnicodeString( &nameW, name );
391 RtlInitUnicodeString( &classW, class );
393 return RtlNtStatusToDosError( create_key( retkey, access, &attr, &classW, options, dispos ) );
397 /******************************************************************************
398 * RegCreateKeyExA (kernelbase.@)
400 * Open a registry key, creating it if it doesn't exist.
402 * PARAMS
403 * hkey [I] Handle of the parent registry key
404 * name [I] Name of the new key to open or create
405 * reserved [I] Reserved, pass 0
406 * class [I] The object type of the new key
407 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
408 * access [I] Access level desired
409 * sa [I] Security attributes for the key
410 * retkey [O] Destination for the resulting handle
411 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
413 * RETURNS
414 * Success: ERROR_SUCCESS.
415 * Failure: A standard Win32 error code. retkey remains untouched.
417 * FIXME
418 * MAXIMUM_ALLOWED in access mask not supported by server
420 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
421 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
422 PHKEY retkey, LPDWORD dispos )
424 OBJECT_ATTRIBUTES attr;
425 UNICODE_STRING classW;
426 ANSI_STRING nameA, classA;
427 NTSTATUS status;
429 if (reserved) return ERROR_INVALID_PARAMETER;
430 if (!is_version_nt())
432 access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
433 if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
435 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
437 attr.Length = sizeof(attr);
438 attr.RootDirectory = hkey;
439 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
440 attr.Attributes = 0;
441 attr.SecurityDescriptor = NULL;
442 attr.SecurityQualityOfService = NULL;
443 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
444 RtlInitAnsiString( &nameA, name );
445 RtlInitAnsiString( &classA, class );
447 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
448 &nameA, FALSE )))
450 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
452 status = create_key( retkey, access, &attr, &classW, options, dispos );
453 RtlFreeUnicodeString( &classW );
456 return RtlNtStatusToDosError( status );
460 /******************************************************************************
461 * RegOpenKeyExW (kernelbase.@)
463 * See RegOpenKeyExA.
465 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD options, REGSAM access, PHKEY retkey )
467 OBJECT_ATTRIBUTES attr;
468 UNICODE_STRING nameW;
470 if (retkey && (!name || !name[0]) &&
471 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
472 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
474 *retkey = hkey;
475 return ERROR_SUCCESS;
478 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
479 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
481 if (!retkey) return ERROR_INVALID_PARAMETER;
482 *retkey = NULL;
483 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
485 attr.Length = sizeof(attr);
486 attr.RootDirectory = hkey;
487 attr.ObjectName = &nameW;
488 attr.Attributes = 0;
489 attr.SecurityDescriptor = NULL;
490 attr.SecurityQualityOfService = NULL;
491 RtlInitUnicodeString( &nameW, name );
492 return RtlNtStatusToDosError( open_key( retkey, options, access, &attr ) );
496 /******************************************************************************
497 * RegOpenKeyExA (kernelbase.@)
499 * Open a registry key.
501 * PARAMS
502 * hkey [I] Handle of open key
503 * name [I] Name of subkey to open
504 * options [I] Open options (can be set to REG_OPTION_OPEN_LINK)
505 * access [I] Security access mask
506 * retkey [O] Handle to open key
508 * RETURNS
509 * Success: ERROR_SUCCESS
510 * Failure: A standard Win32 error code. retkey is set to 0.
512 * NOTES
513 * Unlike RegCreateKeyExA(), this function will not create the key if it
514 * does not exist.
516 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD options, REGSAM access, PHKEY retkey )
518 OBJECT_ATTRIBUTES attr;
519 STRING nameA;
520 NTSTATUS status;
522 if (retkey && (!name || !name[0]) &&
523 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
524 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
526 *retkey = hkey;
527 return ERROR_SUCCESS;
530 if (!is_version_nt()) access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
531 else
533 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
534 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
537 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
539 attr.Length = sizeof(attr);
540 attr.RootDirectory = hkey;
541 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
542 attr.Attributes = 0;
543 attr.SecurityDescriptor = NULL;
544 attr.SecurityQualityOfService = NULL;
546 RtlInitAnsiString( &nameA, name );
547 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
548 &nameA, FALSE )))
550 status = open_key( retkey, options, access, &attr );
552 return RtlNtStatusToDosError( status );
556 /******************************************************************************
557 * RegOpenCurrentUser (kernelbase.@)
559 * Get a handle to the HKEY_CURRENT_USER key for the user
560 * the current thread is impersonating.
562 * PARAMS
563 * access [I] Desired access rights to the key
564 * retkey [O] Handle to the opened key
566 * RETURNS
567 * Success: ERROR_SUCCESS
568 * Failure: nonzero error code from Winerror.h
570 * FIXME
571 * This function is supposed to retrieve a handle to the
572 * HKEY_CURRENT_USER for the user the current thread is impersonating.
573 * Since Wine does not currently allow threads to impersonate other users,
574 * this stub should work fine.
576 LSTATUS WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
578 void *data[20];
579 TOKEN_USER *info = (TOKEN_USER *)data;
580 HANDLE token;
581 DWORD len = 0;
583 /* get current user SID */
584 if (OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, FALSE, &token ))
586 len = sizeof(data);
587 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
588 CloseHandle( token );
590 if (!len)
592 ImpersonateSelf(SecurityIdentification);
593 if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &token))
595 len = sizeof(data);
596 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
597 CloseHandle( token );
599 RevertToSelf();
602 if (len)
604 WCHAR buffer[200];
605 UNICODE_STRING string = { 0, sizeof(buffer), buffer };
607 RtlConvertSidToUnicodeString( &string, info->User.Sid, FALSE );
608 return RegOpenKeyExW( HKEY_USERS, string.Buffer, 0, access, retkey );
611 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
616 /******************************************************************************
617 * RegEnumKeyExW (kernelbase.@)
619 * Enumerate subkeys of the specified open registry key.
621 * PARAMS
622 * hkey [I] Handle to key to enumerate
623 * index [I] Index of subkey to enumerate
624 * name [O] Buffer for subkey name
625 * name_len [O] Size of subkey buffer
626 * reserved [I] Reserved
627 * class [O] Buffer for class string
628 * class_len [O] Size of class buffer
629 * ft [O] Time key last written to
631 * RETURNS
632 * Success: ERROR_SUCCESS
633 * Failure: System error code. If there are no more subkeys available, the
634 * function returns ERROR_NO_MORE_ITEMS.
636 LSTATUS WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
637 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
639 NTSTATUS status;
640 char buffer[256], *buf_ptr = buffer;
641 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
642 DWORD total_size;
644 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
645 name_len ? *name_len : 0, reserved, class, class_len, ft );
647 if (reserved) return ERROR_INVALID_PARAMETER;
648 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
650 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
651 buffer, sizeof(buffer), &total_size );
653 while (status == STATUS_BUFFER_OVERFLOW)
655 /* retry with a dynamically allocated buffer */
656 if (buf_ptr != buffer) heap_free( buf_ptr );
657 if (!(buf_ptr = heap_alloc( total_size )))
658 return ERROR_NOT_ENOUGH_MEMORY;
659 info = (KEY_NODE_INFORMATION *)buf_ptr;
660 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
661 buf_ptr, total_size, &total_size );
664 if (!status)
666 DWORD len = info->NameLength / sizeof(WCHAR);
667 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
669 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
671 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
672 status = STATUS_BUFFER_OVERFLOW;
673 else
675 *name_len = len;
676 memcpy( name, info->Name, info->NameLength );
677 name[len] = 0;
678 if (class_len)
680 *class_len = cls_len;
681 if (class)
683 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
684 class[cls_len] = 0;
690 if (buf_ptr != buffer) heap_free( buf_ptr );
691 return RtlNtStatusToDosError( status );
695 /******************************************************************************
696 * RegEnumKeyExA (kernelbase.@)
698 * See RegEnumKeyExW.
700 LSTATUS WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
701 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
703 NTSTATUS status;
704 char buffer[256], *buf_ptr = buffer;
705 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
706 DWORD total_size;
708 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
709 name_len ? *name_len : 0, reserved, class, class_len, ft );
711 if (reserved) return ERROR_INVALID_PARAMETER;
712 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
714 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
715 buffer, sizeof(buffer), &total_size );
717 while (status == STATUS_BUFFER_OVERFLOW)
719 /* retry with a dynamically allocated buffer */
720 if (buf_ptr != buffer) heap_free( buf_ptr );
721 if (!(buf_ptr = heap_alloc( total_size )))
722 return ERROR_NOT_ENOUGH_MEMORY;
723 info = (KEY_NODE_INFORMATION *)buf_ptr;
724 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
725 buf_ptr, total_size, &total_size );
728 if (!status)
730 DWORD len, cls_len;
732 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
733 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
734 info->ClassLength );
735 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
737 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
738 status = STATUS_BUFFER_OVERFLOW;
739 else
741 *name_len = len;
742 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
743 name[len] = 0;
744 if (class_len)
746 *class_len = cls_len;
747 if (class)
749 RtlUnicodeToMultiByteN( class, cls_len, NULL,
750 (WCHAR *)(buf_ptr + info->ClassOffset),
751 info->ClassLength );
752 class[cls_len] = 0;
758 if (buf_ptr != buffer) heap_free( buf_ptr );
759 return RtlNtStatusToDosError( status );
763 /******************************************************************************
764 * RegQueryInfoKeyW (kernelbase.@)
766 * Retrieves information about the specified registry key.
768 * PARAMS
769 * hkey [I] Handle to key to query
770 * class [O] Buffer for class string
771 * class_len [O] Size of class string buffer
772 * reserved [I] Reserved
773 * subkeys [O] Buffer for number of subkeys
774 * max_subkey [O] Buffer for longest subkey name length
775 * max_class [O] Buffer for longest class string length
776 * values [O] Buffer for number of value entries
777 * max_value [O] Buffer for longest value name length
778 * max_data [O] Buffer for longest value data length
779 * security [O] Buffer for security descriptor length
780 * modif [O] Modification time
782 * RETURNS
783 * Success: ERROR_SUCCESS
784 * Failure: system error code.
786 * NOTES
787 * - win95 allows class to be valid and class_len to be NULL
788 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
789 * - both allow class to be NULL and class_len to be NULL
790 * (it's hard to test validity, so test !NULL instead)
792 LSTATUS WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
793 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
794 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
795 LPDWORD security, FILETIME *modif )
797 NTSTATUS status;
798 char buffer[256], *buf_ptr = buffer;
799 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
800 DWORD total_size;
802 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
803 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
805 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
806 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
808 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
809 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
811 if (class && class_len && *class_len)
813 /* retry with a dynamically allocated buffer */
814 while (status == STATUS_BUFFER_OVERFLOW)
816 if (buf_ptr != buffer) heap_free( buf_ptr );
817 if (!(buf_ptr = heap_alloc( total_size )))
818 return ERROR_NOT_ENOUGH_MEMORY;
819 info = (KEY_FULL_INFORMATION *)buf_ptr;
820 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
823 if (status) goto done;
825 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
827 status = STATUS_BUFFER_TOO_SMALL;
829 else
831 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
832 class[info->ClassLength/sizeof(WCHAR)] = 0;
835 else status = STATUS_SUCCESS;
837 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
838 if (subkeys) *subkeys = info->SubKeys;
839 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
840 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
841 if (values) *values = info->Values;
842 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
843 if (max_data) *max_data = info->MaxValueDataLen;
844 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
846 if (security)
848 FIXME( "security argument not supported.\n");
849 *security = 0;
852 done:
853 if (buf_ptr != buffer) heap_free( buf_ptr );
854 return RtlNtStatusToDosError( status );
858 /******************************************************************************
859 * RegQueryInfoKeyA (kernelbase.@)
861 * Retrieves information about a registry key.
863 * PARAMS
864 * hKey [I] Handle to an open key.
865 * lpClass [O] Class string of the key.
866 * lpcClass [I/O] size of lpClass.
867 * lpReserved [I] Reserved; must be NULL.
868 * lpcSubKeys [O] Number of subkeys contained by the key.
869 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
870 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
871 * class in TCHARS.
872 * lpcValues [O] Number of values associated with the key.
873 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
874 * lpcMaxValueLen [O] Longest data component among the key's values
875 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
876 * lpftLastWriteTime [O] FILETIME structure that is the last write time.
878 * RETURNS
879 * Success: ERROR_SUCCESS
880 * Failure: nonzero error code from Winerror.h
882 LSTATUS WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
883 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
884 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
885 LPDWORD security, FILETIME *modif )
887 NTSTATUS status;
888 char buffer[256], *buf_ptr = buffer;
889 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
890 DWORD total_size;
892 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
893 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
895 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
896 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
898 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
899 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
901 if (class || class_len)
903 /* retry with a dynamically allocated buffer */
904 while (status == STATUS_BUFFER_OVERFLOW)
906 if (buf_ptr != buffer) heap_free( buf_ptr );
907 if (!(buf_ptr = heap_alloc( total_size )))
908 return ERROR_NOT_ENOUGH_MEMORY;
909 info = (KEY_FULL_INFORMATION *)buf_ptr;
910 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
913 if (status) goto done;
915 if (class && class_len && *class_len)
917 DWORD len = *class_len;
918 RtlUnicodeToMultiByteN( class, len, class_len,
919 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
920 if (*class_len == len)
922 status = STATUS_BUFFER_OVERFLOW;
923 *class_len -= 1;
925 class[*class_len] = 0;
927 else if (class_len)
928 RtlUnicodeToMultiByteSize( class_len,
929 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
931 else status = STATUS_SUCCESS;
933 if (subkeys) *subkeys = info->SubKeys;
934 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
935 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
936 if (values) *values = info->Values;
937 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
938 if (max_data) *max_data = info->MaxValueDataLen;
939 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
941 if (security)
943 FIXME( "security argument not supported.\n");
944 *security = 0;
947 done:
948 if (buf_ptr != buffer) heap_free( buf_ptr );
949 return RtlNtStatusToDosError( status );
952 /******************************************************************************
953 * RegCloseKey (kernelbase.@)
955 * Close an open registry key.
957 * PARAMS
958 * hkey [I] Handle of key to close
960 * RETURNS
961 * Success: ERROR_SUCCESS
962 * Failure: Error code
964 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCloseKey( HKEY hkey )
966 if (!hkey) return ERROR_INVALID_HANDLE;
967 if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
968 return RtlNtStatusToDosError( NtClose( hkey ) );
972 /******************************************************************************
973 * RegDeleteKeyExW (kernelbase.@)
975 LSTATUS WINAPI RegDeleteKeyExW( HKEY hkey, LPCWSTR name, REGSAM access, DWORD reserved )
977 DWORD ret;
978 HKEY tmp;
980 if (!name) return ERROR_INVALID_PARAMETER;
982 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
984 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
985 if (!(ret = RegOpenKeyExW( hkey, name, 0, access | DELETE, &tmp )))
987 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
988 RegCloseKey( tmp );
990 TRACE("%s ret=%08x\n", debugstr_w(name), ret);
991 return ret;
995 /******************************************************************************
996 * RegDeleteKeyExA (kernelbase.@)
998 LSTATUS WINAPI RegDeleteKeyExA( HKEY hkey, LPCSTR name, REGSAM access, DWORD reserved )
1000 DWORD ret;
1001 HKEY tmp;
1003 if (!name) return ERROR_INVALID_PARAMETER;
1005 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
1007 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1008 if (!(ret = RegOpenKeyExA( hkey, name, 0, access | DELETE, &tmp )))
1010 if (!is_version_nt()) /* win95 does recursive key deletes */
1012 CHAR sub[MAX_PATH];
1013 DWORD len = sizeof(sub);
1014 while(!RegEnumKeyExA(tmp, 0, sub, &len, NULL, NULL, NULL, NULL))
1016 if(RegDeleteKeyExA(tmp, sub, access, reserved)) /* recurse */
1017 break;
1020 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1021 RegCloseKey( tmp );
1023 TRACE("%s ret=%08x\n", debugstr_a(name), ret);
1024 return ret;
1027 /******************************************************************************
1028 * RegSetValueExW (kernelbase.@)
1030 * Set the data and contents of a registry value.
1032 * PARAMS
1033 * hkey [I] Handle of key to set value for
1034 * name [I] Name of value to set
1035 * reserved [I] Reserved, must be zero
1036 * type [I] Type of the value being set
1037 * data [I] The new contents of the value to set
1038 * count [I] Size of data
1040 * RETURNS
1041 * Success: ERROR_SUCCESS
1042 * Failure: Error code
1044 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
1045 DWORD type, const BYTE *data, DWORD count )
1047 UNICODE_STRING nameW;
1049 /* no need for version check, not implemented on win9x anyway */
1051 if ((data && ((ULONG_PTR)data >> 16) == 0) || (!data && count)) return ERROR_NOACCESS;
1053 if (count && is_string(type))
1055 LPCWSTR str = (LPCWSTR)data;
1056 /* if user forgot to count terminating null, add it (yes NT does this) */
1057 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
1058 count += sizeof(WCHAR);
1060 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1062 RtlInitUnicodeString( &nameW, name );
1063 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
1067 /******************************************************************************
1068 * RegSetValueExA (kernelbase.@)
1070 * See RegSetValueExW.
1072 * NOTES
1073 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1074 * NT does definitely care (aj)
1076 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1077 const BYTE *data, DWORD count )
1079 ANSI_STRING nameA;
1080 UNICODE_STRING nameW;
1081 WCHAR *dataW = NULL;
1082 NTSTATUS status;
1084 if (!is_version_nt()) /* win95 */
1086 if (type == REG_SZ)
1088 if (!data) return ERROR_INVALID_PARAMETER;
1089 count = strlen((const char *)data) + 1;
1092 else if (count && is_string(type))
1094 /* if user forgot to count terminating null, add it (yes NT does this) */
1095 if (data[count-1] && !data[count]) count++;
1098 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1100 if (is_string( type )) /* need to convert to Unicode */
1102 DWORD lenW;
1103 RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1104 if (!(dataW = heap_alloc( lenW ))) return ERROR_OUTOFMEMORY;
1105 RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1106 count = lenW;
1107 data = (BYTE *)dataW;
1110 RtlInitAnsiString( &nameA, name );
1111 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1113 status = NtSetValueKey( hkey, &nameW, 0, type, data, count );
1114 RtlFreeUnicodeString( &nameW );
1116 heap_free( dataW );
1117 return RtlNtStatusToDosError( status );
1121 /******************************************************************************
1122 * RegSetKeyValueW (kernelbase.@)
1124 LONG WINAPI RegSetKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name, DWORD type, const void *data, DWORD len )
1126 HKEY hsubkey = NULL;
1127 DWORD ret;
1129 TRACE("(%p,%s,%s,%d,%p,%d)\n", hkey, debugstr_w(subkey), debugstr_w(name), type, data, len );
1131 if (subkey && subkey[0]) /* need to create the subkey */
1133 if ((ret = RegCreateKeyExW( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1134 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1135 hkey = hsubkey;
1138 ret = RegSetValueExW( hkey, name, 0, type, (const BYTE*)data, len );
1139 if (hsubkey) RegCloseKey( hsubkey );
1140 return ret;
1143 /******************************************************************************
1144 * RegSetKeyValueA (kernelbase.@)
1146 LONG WINAPI RegSetKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name, DWORD type, const void *data, DWORD len )
1148 HKEY hsubkey = NULL;
1149 DWORD ret;
1151 TRACE("(%p,%s,%s,%d,%p,%d)\n", hkey, debugstr_a(subkey), debugstr_a(name), type, data, len );
1153 if (subkey && subkey[0]) /* need to create the subkey */
1155 if ((ret = RegCreateKeyExA( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1156 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1157 hkey = hsubkey;
1160 ret = RegSetValueExA( hkey, name, 0, type, (const BYTE*)data, len );
1161 if (hsubkey) RegCloseKey( hsubkey );
1162 return ret;
1165 struct perf_provider
1167 HMODULE perflib;
1168 WCHAR linkage[MAX_PATH];
1169 WCHAR objects[MAX_PATH];
1170 PM_OPEN_PROC *pOpen;
1171 PM_CLOSE_PROC *pClose;
1172 PM_COLLECT_PROC *pCollect;
1175 static void *get_provider_entry(HKEY perf, HMODULE perflib, const char *name)
1177 char buf[MAX_PATH];
1178 DWORD err, type, len;
1180 len = sizeof(buf) - 1;
1181 err = RegQueryValueExA(perf, name, NULL, &type, (BYTE *)buf, &len);
1182 if (err != ERROR_SUCCESS || type != REG_SZ)
1183 return NULL;
1185 buf[len] = 0;
1186 TRACE("Loading function pointer for %s: %s\n", name, debugstr_a(buf));
1188 return GetProcAddress(perflib, buf);
1191 static BOOL load_provider(HKEY root, const WCHAR *name, struct perf_provider *provider)
1193 WCHAR buf[MAX_PATH], buf2[MAX_PATH];
1194 DWORD err, type, len;
1195 HKEY service, perf;
1197 err = RegOpenKeyExW(root, name, 0, KEY_READ, &service);
1198 if (err != ERROR_SUCCESS)
1199 return FALSE;
1201 provider->linkage[0] = 0;
1202 err = RegOpenKeyExW(service, L"Linkage", 0, KEY_READ, &perf);
1203 if (err == ERROR_SUCCESS)
1205 len = sizeof(buf) - sizeof(WCHAR);
1206 err = RegQueryValueExW(perf, L"Export", NULL, &type, (BYTE *)buf, &len);
1207 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1209 memcpy(provider->linkage, buf, len);
1210 provider->linkage[len / sizeof(WCHAR)] = 0;
1211 TRACE("Export: %s\n", debugstr_w(provider->linkage));
1213 RegCloseKey(perf);
1216 err = RegOpenKeyExW(service, L"Performance", 0, KEY_READ, &perf);
1217 RegCloseKey(service);
1218 if (err != ERROR_SUCCESS)
1219 return FALSE;
1221 provider->objects[0] = 0;
1222 len = sizeof(buf) - sizeof(WCHAR);
1223 err = RegQueryValueExW(perf, L"Object List", NULL, &type, (BYTE *)buf, &len);
1224 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1226 memcpy(provider->objects, buf, len);
1227 provider->objects[len / sizeof(WCHAR)] = 0;
1228 TRACE("Object List: %s\n", debugstr_w(provider->objects));
1231 len = sizeof(buf) - sizeof(WCHAR);
1232 err = RegQueryValueExW(perf, L"Library", NULL, &type, (BYTE *)buf, &len);
1233 if (err != ERROR_SUCCESS || !(type == REG_SZ || type == REG_EXPAND_SZ))
1234 goto error;
1236 buf[len / sizeof(WCHAR)] = 0;
1237 if (type == REG_EXPAND_SZ)
1239 len = ExpandEnvironmentStringsW(buf, buf2, MAX_PATH);
1240 if (!len || len > MAX_PATH) goto error;
1241 lstrcpyW(buf, buf2);
1244 if (!(provider->perflib = LoadLibraryW(buf)))
1246 WARN("Failed to load %s\n", debugstr_w(buf));
1247 goto error;
1250 GetModuleFileNameW(provider->perflib, buf, MAX_PATH);
1251 TRACE("Loaded provider %s\n", wine_dbgstr_w(buf));
1253 provider->pOpen = get_provider_entry(perf, provider->perflib, "Open");
1254 provider->pClose = get_provider_entry(perf, provider->perflib, "Close");
1255 provider->pCollect = get_provider_entry(perf, provider->perflib, "Collect");
1256 if (provider->pOpen && provider->pClose && provider->pCollect)
1258 RegCloseKey(perf);
1259 return TRUE;
1262 TRACE("Provider is missing required exports\n");
1263 FreeLibrary(provider->perflib);
1265 error:
1266 RegCloseKey(perf);
1267 return FALSE;
1270 static DWORD collect_data(struct perf_provider *provider, const WCHAR *query, void **data, DWORD *size, DWORD *obj_count)
1272 WCHAR *linkage = provider->linkage[0] ? provider->linkage : NULL;
1273 DWORD err;
1275 if (!query || !query[0])
1276 query = L"Global";
1278 err = provider->pOpen(linkage);
1279 if (err != ERROR_SUCCESS)
1281 TRACE("Open(%s) error %u (%#x)\n", debugstr_w(linkage), err, err);
1282 return err;
1285 *obj_count = 0;
1286 err = provider->pCollect((WCHAR *)query, data, size, obj_count);
1287 if (err != ERROR_SUCCESS)
1289 TRACE("Collect error %u (%#x)\n", err, err);
1290 *obj_count = 0;
1293 provider->pClose();
1294 return err;
1297 #define MAX_SERVICE_NAME 260
1299 static DWORD query_perf_data(const WCHAR *query, DWORD *type, void *data, DWORD *ret_size)
1301 DWORD err, i, data_size;
1302 HKEY root;
1303 PERF_DATA_BLOCK *pdb;
1305 if (!ret_size)
1306 return ERROR_INVALID_PARAMETER;
1308 data_size = *ret_size;
1309 *ret_size = 0;
1311 if (type)
1312 *type = REG_BINARY;
1314 if (!data || data_size < sizeof(*pdb))
1315 return ERROR_MORE_DATA;
1317 pdb = data;
1319 pdb->Signature[0] = 'P';
1320 pdb->Signature[1] = 'E';
1321 pdb->Signature[2] = 'R';
1322 pdb->Signature[3] = 'F';
1323 #ifdef WORDS_BIGENDIAN
1324 pdb->LittleEndian = FALSE;
1325 #else
1326 pdb->LittleEndian = TRUE;
1327 #endif
1328 pdb->Version = PERF_DATA_VERSION;
1329 pdb->Revision = PERF_DATA_REVISION;
1330 pdb->TotalByteLength = 0;
1331 pdb->HeaderLength = sizeof(*pdb);
1332 pdb->NumObjectTypes = 0;
1333 pdb->DefaultObject = 0;
1334 NtQueryPerformanceCounter( &pdb->PerfTime, &pdb->PerfFreq );
1336 data = pdb + 1;
1337 pdb->SystemNameOffset = sizeof(*pdb);
1338 pdb->SystemNameLength = (data_size - sizeof(*pdb)) / sizeof(WCHAR);
1339 if (!GetComputerNameExW(ComputerNameNetBIOS, data, &pdb->SystemNameLength))
1340 return ERROR_MORE_DATA;
1342 pdb->SystemNameLength++;
1343 pdb->SystemNameLength *= sizeof(WCHAR);
1345 pdb->HeaderLength += pdb->SystemNameLength;
1347 /* align to 8 bytes */
1348 if (pdb->SystemNameLength & 7)
1349 pdb->HeaderLength += 8 - (pdb->SystemNameLength & 7);
1351 if (data_size < pdb->HeaderLength)
1352 return ERROR_MORE_DATA;
1354 pdb->TotalByteLength = pdb->HeaderLength;
1356 data_size -= pdb->HeaderLength;
1357 data = (char *)data + pdb->HeaderLength;
1359 err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services", 0, KEY_READ, &root);
1360 if (err != ERROR_SUCCESS)
1361 return err;
1363 i = 0;
1364 for (;;)
1366 DWORD collected_size = data_size, obj_count = 0;
1367 struct perf_provider provider;
1368 WCHAR name[MAX_SERVICE_NAME];
1369 DWORD len = ARRAY_SIZE( name );
1370 void *collected_data = data;
1372 err = RegEnumKeyExW(root, i++, name, &len, NULL, NULL, NULL, NULL);
1373 if (err == ERROR_NO_MORE_ITEMS)
1375 err = ERROR_SUCCESS;
1376 break;
1379 if (err != ERROR_SUCCESS)
1380 continue;
1382 if (!load_provider(root, name, &provider))
1383 continue;
1385 err = collect_data(&provider, query, &collected_data, &collected_size, &obj_count);
1386 FreeLibrary(provider.perflib);
1388 if (err == ERROR_MORE_DATA)
1389 break;
1391 if (err == ERROR_SUCCESS)
1393 PERF_OBJECT_TYPE *obj = (PERF_OBJECT_TYPE *)data;
1395 TRACE("Collect: obj->TotalByteLength %u, collected_size %u\n",
1396 obj->TotalByteLength, collected_size);
1398 data_size -= collected_size;
1399 data = collected_data;
1401 pdb->TotalByteLength += collected_size;
1402 pdb->NumObjectTypes += obj_count;
1406 RegCloseKey(root);
1408 if (err == ERROR_SUCCESS)
1410 *ret_size = pdb->TotalByteLength;
1412 GetSystemTime(&pdb->SystemTime);
1413 GetSystemTimeAsFileTime((FILETIME *)&pdb->PerfTime100nSec);
1416 return err;
1419 /******************************************************************************
1420 * RegQueryValueExW (kernelbase.@)
1422 * See RegQueryValueExA.
1424 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1425 LPBYTE data, LPDWORD count )
1427 NTSTATUS status;
1428 UNICODE_STRING name_str;
1429 DWORD total_size;
1430 char buffer[256], *buf_ptr = buffer;
1431 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1432 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1434 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1435 hkey, debugstr_w(name), reserved, type, data, count,
1436 (count && data) ? *count : 0 );
1438 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1440 if (hkey == HKEY_PERFORMANCE_DATA)
1441 return query_perf_data(name, type, data, count);
1443 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1445 RtlInitUnicodeString( &name_str, name );
1447 if (data) total_size = min( sizeof(buffer), *count + info_size );
1448 else
1450 total_size = info_size;
1451 if (count) *count = 0;
1454 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1455 buffer, total_size, &total_size );
1456 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1458 if (data)
1460 /* retry with a dynamically allocated buffer */
1461 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1463 if (buf_ptr != buffer) heap_free( buf_ptr );
1464 if (!(buf_ptr = heap_alloc( total_size )))
1465 return ERROR_NOT_ENOUGH_MEMORY;
1466 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1467 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1468 buf_ptr, total_size, &total_size );
1471 if (!status)
1473 memcpy( data, buf_ptr + info_size, total_size - info_size );
1474 /* if the type is REG_SZ and data is not 0-terminated
1475 * and there is enough space in the buffer NT appends a \0 */
1476 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1478 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1479 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1482 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1484 else status = STATUS_SUCCESS;
1486 if (type) *type = info->Type;
1487 if (count) *count = total_size - info_size;
1489 done:
1490 if (buf_ptr != buffer) heap_free( buf_ptr );
1491 return RtlNtStatusToDosError(status);
1495 /******************************************************************************
1496 * RegQueryValueExA (kernelbase.@)
1498 * Get the type and contents of a specified value under with a key.
1500 * PARAMS
1501 * hkey [I] Handle of the key to query
1502 * name [I] Name of value under hkey to query
1503 * reserved [I] Reserved - must be NULL
1504 * type [O] Destination for the value type, or NULL if not required
1505 * data [O] Destination for the values contents, or NULL if not required
1506 * count [I/O] Size of data, updated with the number of bytes returned
1508 * RETURNS
1509 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1510 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1511 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1512 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1514 * NOTES
1515 * MSDN states that if data is too small it is partially filled. In reality
1516 * it remains untouched.
1518 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved,
1519 LPDWORD type, LPBYTE data, LPDWORD count )
1521 NTSTATUS status;
1522 ANSI_STRING nameA;
1523 UNICODE_STRING nameW;
1524 DWORD total_size, datalen = 0;
1525 char buffer[256], *buf_ptr = buffer;
1526 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1527 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1529 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1530 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1532 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1533 if (hkey != HKEY_PERFORMANCE_DATA && !(hkey = get_special_root_hkey( hkey, 0 )))
1534 return ERROR_INVALID_HANDLE;
1536 if (count) datalen = *count;
1537 if (!data && count) *count = 0;
1539 /* this matches Win9x behaviour - NT sets *type to a random value */
1540 if (type) *type = REG_NONE;
1542 RtlInitAnsiString( &nameA, name );
1543 if ((status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1544 return RtlNtStatusToDosError(status);
1546 if (hkey == HKEY_PERFORMANCE_DATA)
1548 DWORD ret = query_perf_data( nameW.Buffer, type, data, count );
1549 RtlFreeUnicodeString( &nameW );
1550 return ret;
1553 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1554 buffer, sizeof(buffer), &total_size );
1555 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1557 /* we need to fetch the contents for a string type even if not requested,
1558 * because we need to compute the length of the ASCII string. */
1559 if (data || is_string(info->Type))
1561 /* retry with a dynamically allocated buffer */
1562 while (status == STATUS_BUFFER_OVERFLOW)
1564 if (buf_ptr != buffer) heap_free( buf_ptr );
1565 if (!(buf_ptr = heap_alloc( total_size )))
1567 status = STATUS_NO_MEMORY;
1568 goto done;
1570 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1571 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1572 buf_ptr, total_size, &total_size );
1575 if (status) goto done;
1577 if (is_string(info->Type))
1579 DWORD len;
1581 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1582 total_size - info_size );
1583 if (data && len)
1585 if (len > datalen) status = STATUS_BUFFER_OVERFLOW;
1586 else
1588 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1589 total_size - info_size );
1590 /* if the type is REG_SZ and data is not 0-terminated
1591 * and there is enough space in the buffer NT appends a \0 */
1592 if (len < datalen && data[len-1]) data[len] = 0;
1595 total_size = len + info_size;
1597 else if (data)
1599 if (total_size - info_size > datalen) status = STATUS_BUFFER_OVERFLOW;
1600 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1603 else status = STATUS_SUCCESS;
1605 if (type) *type = info->Type;
1606 if (count) *count = total_size - info_size;
1608 done:
1609 if (buf_ptr != buffer) heap_free( buf_ptr );
1610 RtlFreeUnicodeString( &nameW );
1611 return RtlNtStatusToDosError(status);
1615 /******************************************************************************
1616 * apply_restrictions [internal]
1618 * Helper function for RegGetValueA/W.
1620 static void apply_restrictions( DWORD dwFlags, DWORD dwType, DWORD cbData, PLONG ret )
1622 /* Check if the type is restricted by the passed flags */
1623 if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1625 DWORD dwMask = 0;
1627 switch (dwType)
1629 case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1630 case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1631 case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1632 case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1633 case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1634 case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1635 case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1638 if (dwFlags & dwMask)
1640 /* Type is not restricted, check for size mismatch */
1641 if (dwType == REG_BINARY)
1643 DWORD cbExpect = 0;
1645 if ((dwFlags & RRF_RT_ANY) == RRF_RT_DWORD)
1646 cbExpect = 4;
1647 else if ((dwFlags & RRF_RT_ANY) == RRF_RT_QWORD)
1648 cbExpect = 8;
1650 if (cbExpect && cbData != cbExpect)
1651 *ret = ERROR_DATATYPE_MISMATCH;
1654 else *ret = ERROR_UNSUPPORTED_TYPE;
1659 /******************************************************************************
1660 * RegGetValueW (kernelbase.@)
1662 * Retrieves the type and data for a value name associated with a key,
1663 * optionally expanding its content and restricting its type.
1665 * PARAMS
1666 * hKey [I] Handle to an open key.
1667 * pszSubKey [I] Name of the subkey of hKey.
1668 * pszValue [I] Name of value under hKey/szSubKey to query.
1669 * dwFlags [I] Flags restricting the value type to retrieve.
1670 * pdwType [O] Destination for the values type, may be NULL.
1671 * pvData [O] Destination for the values content, may be NULL.
1672 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
1673 * retrieve the whole content, including the trailing '\0'
1674 * for strings.
1676 * RETURNS
1677 * Success: ERROR_SUCCESS
1678 * Failure: nonzero error code from Winerror.h
1680 * NOTES
1681 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
1682 * expanded and pdwType is set to REG_SZ instead.
1683 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1684 * without RRF_NOEXPAND is thus not allowed.
1685 * An exception is the case where RRF_RT_ANY is specified, because then
1686 * RRF_NOEXPAND is allowed.
1688 LSTATUS WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1689 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1690 LPDWORD pcbData )
1692 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1693 PVOID pvBuf = NULL;
1694 LONG ret;
1696 TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n",
1697 hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1698 pvData, pcbData, cbData);
1700 if (pvData && !pcbData)
1701 return ERROR_INVALID_PARAMETER;
1703 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1704 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1705 return ERROR_INVALID_PARAMETER;
1707 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1708 return ERROR_INVALID_PARAMETER;
1710 if (pszSubKey && pszSubKey[0])
1712 REGSAM samDesired = KEY_QUERY_VALUE;
1714 if (dwFlags & RRF_WOW64_MASK)
1715 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1717 ret = RegOpenKeyExW(hKey, pszSubKey, 0, samDesired, &hKey);
1718 if (ret != ERROR_SUCCESS) return ret;
1721 ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1723 /* If we are going to expand we need to read in the whole the value even
1724 * if the passed buffer was too small as the expanded string might be
1725 * smaller than the unexpanded one and could fit into cbData bytes. */
1726 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1727 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1729 do {
1730 heap_free(pvBuf);
1732 pvBuf = heap_alloc(cbData);
1733 if (!pvBuf)
1735 ret = ERROR_NOT_ENOUGH_MEMORY;
1736 break;
1739 if (ret == ERROR_MORE_DATA || !pvData)
1740 ret = RegQueryValueExW(hKey, pszValue, NULL,
1741 &dwType, pvBuf, &cbData);
1742 else
1744 /* Even if cbData was large enough we have to copy the
1745 * string since ExpandEnvironmentStrings can't handle
1746 * overlapping buffers. */
1747 CopyMemory(pvBuf, pvData, cbData);
1750 /* Both the type or the value itself could have been modified in
1751 * between so we have to keep retrying until the buffer is large
1752 * enough or we no longer have to expand the value. */
1753 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1755 if (ret == ERROR_SUCCESS)
1757 /* Recheck dwType in case it changed since the first call */
1758 if (dwType == REG_EXPAND_SZ)
1760 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1761 pcbData ? *pcbData : 0) * sizeof(WCHAR);
1762 dwType = REG_SZ;
1763 if(pvData && pcbData && cbData > *pcbData)
1764 ret = ERROR_MORE_DATA;
1766 else if (pvData)
1767 CopyMemory(pvData, pvBuf, *pcbData);
1770 heap_free(pvBuf);
1773 if (pszSubKey && pszSubKey[0])
1774 RegCloseKey(hKey);
1776 apply_restrictions(dwFlags, dwType, cbData, &ret);
1778 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1779 ZeroMemory(pvData, *pcbData);
1781 if (pdwType) *pdwType = dwType;
1782 if (pcbData) *pcbData = cbData;
1784 return ret;
1788 /******************************************************************************
1789 * RegGetValueA (kernelbase.@)
1791 * See RegGetValueW.
1793 LSTATUS WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
1794 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1795 LPDWORD pcbData )
1797 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1798 PVOID pvBuf = NULL;
1799 LONG ret;
1801 TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n",
1802 hKey, debugstr_a(pszSubKey), debugstr_a(pszValue), dwFlags,
1803 pdwType, pvData, pcbData, cbData);
1805 if (pvData && !pcbData)
1806 return ERROR_INVALID_PARAMETER;
1808 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1809 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1810 return ERROR_INVALID_PARAMETER;
1812 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1813 return ERROR_INVALID_PARAMETER;
1815 if (pszSubKey && pszSubKey[0])
1817 REGSAM samDesired = KEY_QUERY_VALUE;
1819 if (dwFlags & RRF_WOW64_MASK)
1820 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1822 ret = RegOpenKeyExA(hKey, pszSubKey, 0, samDesired, &hKey);
1823 if (ret != ERROR_SUCCESS) return ret;
1826 ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1828 /* If we are going to expand we need to read in the whole the value even
1829 * if the passed buffer was too small as the expanded string might be
1830 * smaller than the unexpanded one and could fit into cbData bytes. */
1831 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1832 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1834 do {
1835 heap_free(pvBuf);
1837 pvBuf = heap_alloc(cbData);
1838 if (!pvBuf)
1840 ret = ERROR_NOT_ENOUGH_MEMORY;
1841 break;
1844 if (ret == ERROR_MORE_DATA || !pvData)
1845 ret = RegQueryValueExA(hKey, pszValue, NULL,
1846 &dwType, pvBuf, &cbData);
1847 else
1849 /* Even if cbData was large enough we have to copy the
1850 * string since ExpandEnvironmentStrings can't handle
1851 * overlapping buffers. */
1852 CopyMemory(pvBuf, pvData, cbData);
1855 /* Both the type or the value itself could have been modified in
1856 * between so we have to keep retrying until the buffer is large
1857 * enough or we no longer have to expand the value. */
1858 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1860 if (ret == ERROR_SUCCESS)
1862 /* Recheck dwType in case it changed since the first call */
1863 if (dwType == REG_EXPAND_SZ)
1865 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
1866 pcbData ? *pcbData : 0);
1867 dwType = REG_SZ;
1868 if(pvData && pcbData && cbData > *pcbData)
1869 ret = ERROR_MORE_DATA;
1871 else if (pvData)
1872 CopyMemory(pvData, pvBuf, *pcbData);
1875 heap_free(pvBuf);
1878 if (pszSubKey && pszSubKey[0])
1879 RegCloseKey(hKey);
1881 apply_restrictions(dwFlags, dwType, cbData, &ret);
1883 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1884 ZeroMemory(pvData, *pcbData);
1886 if (pdwType) *pdwType = dwType;
1887 if (pcbData) *pcbData = cbData;
1889 return ret;
1893 /******************************************************************************
1894 * RegEnumValueW (kernelbase.@)
1896 * Enumerates the values for the specified open registry key.
1898 * PARAMS
1899 * hkey [I] Handle to key to query
1900 * index [I] Index of value to query
1901 * value [O] Value string
1902 * val_count [I/O] Size of value buffer (in wchars)
1903 * reserved [I] Reserved
1904 * type [O] Type code
1905 * data [O] Value data
1906 * count [I/O] Size of data buffer (in bytes)
1908 * RETURNS
1909 * Success: ERROR_SUCCESS
1910 * Failure: nonzero error code from Winerror.h
1912 LSTATUS WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1913 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1915 NTSTATUS status;
1916 DWORD total_size;
1917 char buffer[256], *buf_ptr = buffer;
1918 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1919 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1921 TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
1922 hkey, index, value, val_count, reserved, type, data, count );
1924 if ((data && !count) || reserved || !value || !val_count)
1925 return ERROR_INVALID_PARAMETER;
1926 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1928 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1929 if (data) total_size += *count;
1930 total_size = min( sizeof(buffer), total_size );
1932 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1933 buffer, total_size, &total_size );
1935 /* retry with a dynamically allocated buffer */
1936 while (status == STATUS_BUFFER_OVERFLOW)
1938 if (buf_ptr != buffer) heap_free( buf_ptr );
1939 if (!(buf_ptr = heap_alloc( total_size )))
1940 return ERROR_NOT_ENOUGH_MEMORY;
1941 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1942 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1943 buf_ptr, total_size, &total_size );
1946 if (status) goto done;
1948 if (info->NameLength/sizeof(WCHAR) >= *val_count)
1950 status = STATUS_BUFFER_OVERFLOW;
1951 goto overflow;
1953 memcpy( value, info->Name, info->NameLength );
1954 *val_count = info->NameLength / sizeof(WCHAR);
1955 value[*val_count] = 0;
1957 if (data)
1959 if (total_size - info->DataOffset > *count)
1961 status = STATUS_BUFFER_OVERFLOW;
1962 goto overflow;
1964 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1965 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1967 /* if the type is REG_SZ and data is not 0-terminated
1968 * and there is enough space in the buffer NT appends a \0 */
1969 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1970 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1974 overflow:
1975 if (type) *type = info->Type;
1976 if (count) *count = info->DataLength;
1978 done:
1979 if (buf_ptr != buffer) heap_free( buf_ptr );
1980 return RtlNtStatusToDosError(status);
1984 /******************************************************************************
1985 * RegEnumValueA (kernelbase.@)
1987 * See RegEnumValueW.
1989 LSTATUS WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1990 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1992 NTSTATUS status;
1993 DWORD total_size;
1994 char buffer[256], *buf_ptr = buffer;
1995 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1996 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1998 TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
1999 hkey, index, value, val_count, reserved, type, data, count );
2001 if ((data && !count) || reserved || !value || !val_count)
2002 return ERROR_INVALID_PARAMETER;
2003 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2005 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
2006 if (data) total_size += *count;
2007 total_size = min( sizeof(buffer), total_size );
2009 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2010 buffer, total_size, &total_size );
2012 /* we need to fetch the contents for a string type even if not requested,
2013 * because we need to compute the length of the ASCII string. */
2015 /* retry with a dynamically allocated buffer */
2016 while (status == STATUS_BUFFER_OVERFLOW)
2018 if (buf_ptr != buffer) heap_free( buf_ptr );
2019 if (!(buf_ptr = heap_alloc( total_size )))
2020 return ERROR_NOT_ENOUGH_MEMORY;
2021 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
2022 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2023 buf_ptr, total_size, &total_size );
2026 if (status) goto done;
2028 if (is_string(info->Type))
2030 DWORD len;
2031 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
2032 total_size - info->DataOffset );
2033 if (data && len)
2035 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
2036 else
2038 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
2039 total_size - info->DataOffset );
2040 /* if the type is REG_SZ and data is not 0-terminated
2041 * and there is enough space in the buffer NT appends a \0 */
2042 if (len < *count && data[len-1]) data[len] = 0;
2045 info->DataLength = len;
2047 else if (data)
2049 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
2050 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2053 if (!status)
2055 DWORD len;
2057 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
2058 if (len >= *val_count)
2060 status = STATUS_BUFFER_OVERFLOW;
2061 if (*val_count)
2063 len = *val_count - 1;
2064 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2065 value[len] = 0;
2068 else
2070 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2071 value[len] = 0;
2072 *val_count = len;
2076 if (type) *type = info->Type;
2077 if (count) *count = info->DataLength;
2079 done:
2080 if (buf_ptr != buffer) heap_free( buf_ptr );
2081 return RtlNtStatusToDosError(status);
2084 /******************************************************************************
2085 * RegDeleteValueW (kernelbase.@)
2087 * See RegDeleteValueA.
2089 LSTATUS WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
2091 return RegDeleteKeyValueW( hkey, NULL, name );
2094 /******************************************************************************
2095 * RegDeleteValueA (kernelbase.@)
2097 * Delete a value from the registry.
2099 * PARAMS
2100 * hkey [I] Registry handle of the key holding the value
2101 * name [I] Name of the value under hkey to delete
2103 * RETURNS
2104 * Success: ERROR_SUCCESS
2105 * Failure: nonzero error code from Winerror.h
2107 LSTATUS WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
2109 return RegDeleteKeyValueA( hkey, NULL, name );
2112 /******************************************************************************
2113 * RegDeleteKeyValueW (kernelbase.@)
2115 LONG WINAPI RegDeleteKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name )
2117 UNICODE_STRING nameW;
2118 HKEY hsubkey = 0;
2119 LONG ret;
2121 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2123 if (subkey)
2125 if ((ret = RegOpenKeyExW( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey )))
2126 return ret;
2127 hkey = hsubkey;
2130 RtlInitUnicodeString( &nameW, name );
2131 ret = RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
2132 if (hsubkey) RegCloseKey( hsubkey );
2133 return ret;
2136 /******************************************************************************
2137 * RegDeleteKeyValueA (kernelbase.@)
2139 LONG WINAPI RegDeleteKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name )
2141 UNICODE_STRING nameW;
2142 HKEY hsubkey = 0;
2143 ANSI_STRING nameA;
2144 NTSTATUS status;
2146 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2148 if (subkey)
2150 LONG ret = RegOpenKeyExA( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey );
2151 if (ret)
2152 return ret;
2153 hkey = hsubkey;
2156 RtlInitAnsiString( &nameA, name );
2157 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
2159 status = NtDeleteValueKey( hkey, &nameW );
2160 RtlFreeUnicodeString( &nameW );
2163 if (hsubkey) RegCloseKey( hsubkey );
2164 return RtlNtStatusToDosError( status );
2167 /******************************************************************************
2168 * RegLoadKeyW (kernelbase.@)
2170 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
2171 * registration information from a specified file into that subkey.
2173 * PARAMS
2174 * hkey [I] Handle of open key
2175 * subkey [I] Address of name of subkey
2176 * filename [I] Address of filename for registry information
2178 * RETURNS
2179 * Success: ERROR_SUCCESS
2180 * Failure: nonzero error code from Winerror.h
2182 LSTATUS WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
2184 OBJECT_ATTRIBUTES destkey, file;
2185 UNICODE_STRING subkeyW, filenameW;
2186 NTSTATUS status;
2188 if (!(hkey = get_special_root_hkey(hkey, 0))) return ERROR_INVALID_HANDLE;
2190 destkey.Length = sizeof(destkey);
2191 destkey.RootDirectory = hkey; /* root key: HKLM or HKU */
2192 destkey.ObjectName = &subkeyW; /* name of the key */
2193 destkey.Attributes = 0;
2194 destkey.SecurityDescriptor = NULL;
2195 destkey.SecurityQualityOfService = NULL;
2196 RtlInitUnicodeString(&subkeyW, subkey);
2198 file.Length = sizeof(file);
2199 file.RootDirectory = NULL;
2200 file.ObjectName = &filenameW; /* file containing the hive */
2201 file.Attributes = OBJ_CASE_INSENSITIVE;
2202 file.SecurityDescriptor = NULL;
2203 file.SecurityQualityOfService = NULL;
2204 RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
2206 status = NtLoadKey(&destkey, &file);
2207 RtlFreeUnicodeString(&filenameW);
2208 return RtlNtStatusToDosError( status );
2212 /******************************************************************************
2213 * RegLoadKeyA (kernelbase.@)
2215 * See RegLoadKeyW.
2217 LSTATUS WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
2219 UNICODE_STRING subkeyW, filenameW;
2220 STRING subkeyA, filenameA;
2221 NTSTATUS status;
2222 LONG ret;
2224 RtlInitAnsiString(&subkeyA, subkey);
2225 RtlInitAnsiString(&filenameA, filename);
2227 RtlInitUnicodeString(&subkeyW, NULL);
2228 RtlInitUnicodeString(&filenameW, NULL);
2229 if (!(status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)) &&
2230 !(status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
2232 ret = RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
2234 else ret = RtlNtStatusToDosError(status);
2235 RtlFreeUnicodeString(&subkeyW);
2236 RtlFreeUnicodeString(&filenameW);
2237 return ret;
2241 /******************************************************************************
2242 * RegSaveKeyExW (kernelbase.@)
2244 LSTATUS WINAPI RegSaveKeyExW( HKEY hkey, LPCWSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2246 UNICODE_STRING nameW;
2247 OBJECT_ATTRIBUTES attr;
2248 IO_STATUS_BLOCK io;
2249 NTSTATUS status;
2250 HANDLE handle;
2252 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
2254 if (!file || !*file) return ERROR_INVALID_PARAMETER;
2255 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2257 if ((status = RtlDosPathNameToNtPathName_U_WithStatus( file, &nameW, NULL, NULL )))
2258 return RtlNtStatusToDosError( status );
2260 InitializeObjectAttributes( &attr, &nameW, OBJ_CASE_INSENSITIVE, 0, sa );
2261 status = NtCreateFile( &handle, GENERIC_WRITE | SYNCHRONIZE, &attr, &io, NULL, FILE_NON_DIRECTORY_FILE,
2262 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
2263 FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
2264 RtlFreeUnicodeString( &nameW );
2265 if (!status)
2267 status = NtSaveKey( hkey, handle );
2268 CloseHandle( handle );
2270 return RtlNtStatusToDosError( status );
2274 /******************************************************************************
2275 * RegSaveKeyExA (kernelbase.@)
2277 LSTATUS WINAPI RegSaveKeyExA( HKEY hkey, LPCSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2279 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
2280 NTSTATUS status;
2281 STRING fileA;
2283 RtlInitAnsiString(&fileA, file);
2284 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
2285 return RtlNtStatusToDosError( status );
2286 return RegSaveKeyExW(hkey, fileW->Buffer, sa, flags);
2290 /******************************************************************************
2291 * RegRestoreKeyW (kernelbase.@)
2293 * Read the registry information from a file and copy it over a key.
2295 * PARAMS
2296 * hkey [I] Handle of key where restore begins
2297 * lpFile [I] Address of filename containing saved tree
2298 * dwFlags [I] Optional flags
2300 * RETURNS
2301 * Success: ERROR_SUCCESS
2302 * Failure: nonzero error code from Winerror.h
2304 LSTATUS WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2306 TRACE("(%p,%s,%d)\n",hkey,debugstr_w(lpFile),dwFlags);
2308 /* It seems to do this check before the hkey check */
2309 if (!lpFile || !*lpFile)
2310 return ERROR_INVALID_PARAMETER;
2312 FIXME("(%p,%s,%d): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2314 /* Check for file existence */
2316 return ERROR_SUCCESS;
2320 /******************************************************************************
2321 * RegRestoreKeyA (kernelbase.@)
2323 * See RegRestoreKeyW.
2325 LSTATUS WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2327 UNICODE_STRING lpFileW;
2328 LONG ret;
2330 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2331 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2332 RtlFreeUnicodeString( &lpFileW );
2333 return ret;
2337 /******************************************************************************
2338 * RegUnLoadKeyW (kernelbase.@)
2340 * Unload a registry key and its subkeys from the registry.
2342 * PARAMS
2343 * hkey [I] Handle of open key
2344 * lpSubKey [I] Address of name of subkey to unload
2346 * RETURNS
2347 * Success: ERROR_SUCCESS
2348 * Failure: nonzero error code from Winerror.h
2350 LSTATUS WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2352 OBJECT_ATTRIBUTES attr;
2353 UNICODE_STRING subkey;
2355 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2357 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2359 RtlInitUnicodeString(&subkey, lpSubKey);
2360 InitializeObjectAttributes(&attr, &subkey, OBJ_CASE_INSENSITIVE, hkey, NULL);
2361 return RtlNtStatusToDosError( NtUnloadKey(&attr) );
2365 /******************************************************************************
2366 * RegUnLoadKeyA (kernelbase.@)
2368 * See RegUnLoadKeyW.
2370 LSTATUS WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2372 UNICODE_STRING lpSubKeyW;
2373 LONG ret;
2375 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2376 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2377 RtlFreeUnicodeString( &lpSubKeyW );
2378 return ret;
2382 /******************************************************************************
2383 * RegSetKeySecurity (kernelbase.@)
2385 * Set the security of an open registry key.
2387 * PARAMS
2388 * hkey [I] Open handle of key to set
2389 * SecurityInfo [I] Descriptor contents
2390 * pSecurityDesc [I] Address of descriptor for key
2392 * RETURNS
2393 * Success: ERROR_SUCCESS
2394 * Failure: nonzero error code from Winerror.h
2396 LSTATUS WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2397 PSECURITY_DESCRIPTOR pSecurityDesc )
2399 TRACE("(%p,%d,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2401 /* It seems to perform this check before the hkey check */
2402 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2403 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2404 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2405 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2406 /* Param OK */
2407 } else
2408 return ERROR_INVALID_PARAMETER;
2410 if (!pSecurityDesc)
2411 return ERROR_INVALID_PARAMETER;
2413 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2415 return RtlNtStatusToDosError( NtSetSecurityObject( hkey, SecurityInfo, pSecurityDesc ) );
2419 /******************************************************************************
2420 * RegGetKeySecurity (kernelbase.@)
2422 * Get a copy of the security descriptor for a given registry key.
2424 * PARAMS
2425 * hkey [I] Open handle of key to set
2426 * SecurityInformation [I] Descriptor contents
2427 * pSecurityDescriptor [O] Address of descriptor for key
2428 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2430 * RETURNS
2431 * Success: ERROR_SUCCESS
2432 * Failure: Error code
2434 LSTATUS WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2435 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2436 LPDWORD lpcbSecurityDescriptor )
2438 TRACE("(%p,%d,%p,%d)\n",hkey,SecurityInformation,pSecurityDescriptor,
2439 *lpcbSecurityDescriptor);
2441 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2443 return RtlNtStatusToDosError( NtQuerySecurityObject( hkey,
2444 SecurityInformation, pSecurityDescriptor,
2445 *lpcbSecurityDescriptor, lpcbSecurityDescriptor ) );
2449 /******************************************************************************
2450 * RegFlushKey (kernelbase.@)
2452 * Immediately write a registry key to registry.
2454 * PARAMS
2455 * hkey [I] Handle of key to write
2457 * RETURNS
2458 * Success: ERROR_SUCCESS
2459 * Failure: Error code
2461 LSTATUS WINAPI RegFlushKey( HKEY hkey )
2463 hkey = get_special_root_hkey( hkey, 0 );
2464 if (!hkey) return ERROR_INVALID_HANDLE;
2466 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2470 /******************************************************************************
2471 * RegNotifyChangeKeyValue (kernelbase.@)
2473 * Notify the caller about changes to the attributes or contents of a registry key.
2475 * PARAMS
2476 * hkey [I] Handle of key to watch
2477 * fWatchSubTree [I] Flag for subkey notification
2478 * fdwNotifyFilter [I] Changes to be reported
2479 * hEvent [I] Handle of signaled event
2480 * fAsync [I] Flag for asynchronous reporting
2482 * RETURNS
2483 * Success: ERROR_SUCCESS
2484 * Failure: nonzero error code from Winerror.h
2486 LSTATUS WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2487 DWORD fdwNotifyFilter, HANDLE hEvent,
2488 BOOL fAsync )
2490 NTSTATUS status;
2491 IO_STATUS_BLOCK iosb;
2493 hkey = get_special_root_hkey( hkey, 0 );
2494 if (!hkey) return ERROR_INVALID_HANDLE;
2496 TRACE("(%p,%i,%d,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2497 hEvent, fAsync);
2499 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2500 fdwNotifyFilter, fWatchSubTree, NULL, 0,
2501 fAsync);
2503 if (status && status != STATUS_PENDING)
2504 return RtlNtStatusToDosError( status );
2506 return ERROR_SUCCESS;
2509 /******************************************************************************
2510 * RegOpenUserClassesRoot (kernelbase.@)
2512 * Open the HKEY_CLASSES_ROOT key for a user.
2514 * PARAMS
2515 * hToken [I] Handle of token representing the user
2516 * dwOptions [I] Reserved, must be 0
2517 * samDesired [I] Desired access rights
2518 * phkResult [O] Destination for the resulting key handle
2520 * RETURNS
2521 * Success: ERROR_SUCCESS
2522 * Failure: nonzero error code from Winerror.h
2524 * NOTES
2525 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2526 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2527 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2529 LSTATUS WINAPI RegOpenUserClassesRoot( HANDLE hToken, DWORD dwOptions, REGSAM samDesired, PHKEY phkResult )
2531 FIXME("(%p, 0x%x, 0x%x, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2533 *phkResult = HKEY_CLASSES_ROOT;
2534 return ERROR_SUCCESS;
2538 static void dump_mui_cache(void)
2540 struct mui_cache_entry *ent;
2542 TRACE("---------- MUI Cache ----------\n");
2543 LIST_FOR_EACH_ENTRY( ent, &reg_mui_cache, struct mui_cache_entry, entry )
2544 TRACE("entry=%p, %s,-%u [%#x] => %s\n",
2545 ent, wine_dbgstr_w(ent->file_name), ent->index, ent->locale, wine_dbgstr_w(ent->text));
2548 static inline void free_mui_cache_entry(struct mui_cache_entry *ent)
2550 heap_free(ent->file_name);
2551 heap_free(ent->text);
2552 heap_free(ent);
2555 /* critical section must be held */
2556 static int reg_mui_cache_get(const WCHAR *file_name, UINT index, WCHAR **buffer)
2558 struct mui_cache_entry *ent;
2560 TRACE("(%s %u %p)\n", wine_dbgstr_w(file_name), index, buffer);
2562 LIST_FOR_EACH_ENTRY(ent, &reg_mui_cache, struct mui_cache_entry, entry)
2564 if (ent->index == index && ent->locale == GetThreadLocale() &&
2565 !lstrcmpiW(ent->file_name, file_name))
2566 goto found;
2568 return 0;
2570 found:
2571 /* move to the list head */
2572 if (list_prev(&reg_mui_cache, &ent->entry)) {
2573 list_remove(&ent->entry);
2574 list_add_head(&reg_mui_cache, &ent->entry);
2577 TRACE("=> %s\n", wine_dbgstr_w(ent->text));
2578 *buffer = ent->text;
2579 return lstrlenW(ent->text);
2582 /* critical section must be held */
2583 static void reg_mui_cache_put(const WCHAR *file_name, UINT index, const WCHAR *buffer, INT size)
2585 struct mui_cache_entry *ent;
2586 TRACE("(%s %u %s %d)\n", wine_dbgstr_w(file_name), index, wine_dbgstr_wn(buffer, size), size);
2588 ent = heap_calloc(sizeof(*ent), 1);
2589 if (!ent)
2590 return;
2591 ent->file_name = heap_alloc((lstrlenW(file_name) + 1) * sizeof(WCHAR));
2592 if (!ent->file_name) {
2593 free_mui_cache_entry(ent);
2594 return;
2596 lstrcpyW(ent->file_name, file_name);
2597 ent->index = index;
2598 ent->locale = GetThreadLocale();
2599 ent->text = heap_alloc((size + 1) * sizeof(WCHAR));
2600 if (!ent->text) {
2601 free_mui_cache_entry(ent);
2602 return;
2604 memcpy(ent->text, buffer, size * sizeof(WCHAR));
2605 ent->text[size] = '\0';
2607 TRACE("add %p\n", ent);
2608 list_add_head(&reg_mui_cache, &ent->entry);
2609 if (reg_mui_cache_count > REG_MUI_CACHE_SIZE) {
2610 ent = LIST_ENTRY( list_tail( &reg_mui_cache ), struct mui_cache_entry, entry );
2611 TRACE("freeing %p\n", ent);
2612 list_remove(&ent->entry);
2613 free_mui_cache_entry(ent);
2615 else
2616 reg_mui_cache_count++;
2618 if (TRACE_ON(reg))
2619 dump_mui_cache();
2620 return;
2623 static LONG load_mui_string(const WCHAR *file_name, UINT res_id, WCHAR *buffer, INT max_chars, INT *req_chars, DWORD flags)
2625 HMODULE hModule = NULL;
2626 WCHAR *string = NULL, *full_name;
2627 int size;
2628 LONG result;
2630 /* Verify the file existence. i.e. We don't rely on PATH variable */
2631 if (GetFileAttributesW(file_name) == INVALID_FILE_ATTRIBUTES)
2632 return ERROR_FILE_NOT_FOUND;
2634 size = GetFullPathNameW(file_name, 0, NULL, NULL);
2635 if (!size)
2636 return GetLastError();
2637 full_name = heap_alloc(size * sizeof(WCHAR));
2638 if (!full_name)
2639 return ERROR_NOT_ENOUGH_MEMORY;
2640 GetFullPathNameW(file_name, size, full_name, NULL);
2642 RtlEnterCriticalSection(&reg_mui_cs);
2643 size = reg_mui_cache_get(full_name, res_id, &string);
2644 if (!size) {
2645 RtlLeaveCriticalSection(&reg_mui_cs);
2647 /* Load the file */
2648 hModule = LoadLibraryExW(full_name, NULL,
2649 LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
2650 if (!hModule)
2651 return GetLastError();
2653 size = LoadStringW(hModule, res_id, (WCHAR *)&string, 0);
2654 if (!size) {
2655 if (string) result = ERROR_NOT_FOUND;
2656 else result = GetLastError();
2657 goto cleanup;
2660 RtlEnterCriticalSection(&reg_mui_cs);
2661 reg_mui_cache_put(full_name, res_id, string, size);
2662 RtlLeaveCriticalSection(&reg_mui_cs);
2664 *req_chars = size + 1;
2666 /* If no buffer is given, skip copying. */
2667 if (!buffer) {
2668 result = ERROR_MORE_DATA;
2669 goto cleanup;
2672 /* Else copy over the string, respecting the buffer size. */
2673 if (size < max_chars)
2674 max_chars = size;
2675 else {
2676 if (flags & REG_MUI_STRING_TRUNCATE)
2677 max_chars--;
2678 else {
2679 result = ERROR_MORE_DATA;
2680 goto cleanup;
2683 if (max_chars >= 0) {
2684 memcpy(buffer, string, max_chars * sizeof(WCHAR));
2685 buffer[max_chars] = '\0';
2688 result = ERROR_SUCCESS;
2690 cleanup:
2691 if (hModule)
2692 FreeLibrary(hModule);
2693 else
2694 RtlLeaveCriticalSection(&reg_mui_cs);
2695 heap_free(full_name);
2696 return result;
2699 /******************************************************************************
2700 * RegLoadMUIStringW (kernelbase.@)
2702 * Load the localized version of a string resource from some PE, respective
2703 * id and path of which are given in the registry value in the format
2704 * @[path]\dllname,-resourceId
2706 * PARAMS
2707 * hKey [I] Key, of which to load the string value from.
2708 * pszValue [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2709 * pszBuffer [O] Buffer to store the localized string in.
2710 * cbBuffer [I] Size of the destination buffer in bytes.
2711 * pcbData [O] Number of bytes written to pszBuffer (optional, may be NULL).
2712 * dwFlags [I] Truncate output to fit the buffer if REG_MUI_STRING_TRUNCATE.
2713 * pszBaseDir [I] Base directory of loading path. If NULL, use the current directory.
2715 * RETURNS
2716 * Success: ERROR_SUCCESS,
2717 * Failure: nonzero error code from winerror.h
2719 LSTATUS WINAPI RegLoadMUIStringW(HKEY hKey, LPCWSTR pwszValue, LPWSTR pwszBuffer, DWORD cbBuffer,
2720 LPDWORD pcbData, DWORD dwFlags, LPCWSTR pwszBaseDir)
2722 DWORD dwValueType, cbData;
2723 LPWSTR pwszTempBuffer = NULL, pwszExpandedBuffer = NULL;
2724 LONG result;
2726 TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %d, pcbData = %p, "
2727 "dwFlags = %d, pwszBaseDir = %s)\n", hKey, debugstr_w(pwszValue), pwszBuffer,
2728 cbBuffer, pcbData, dwFlags, debugstr_w(pwszBaseDir));
2730 /* Parameter sanity checks. */
2731 if (!hKey || (!pwszBuffer && cbBuffer) || (cbBuffer % sizeof(WCHAR))
2732 || ((dwFlags & REG_MUI_STRING_TRUNCATE) && pcbData)
2733 || (dwFlags & ~REG_MUI_STRING_TRUNCATE))
2734 return ERROR_INVALID_PARAMETER;
2736 /* Check for value existence and correctness of its type, allocate a buffer and load it. */
2737 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, NULL, &cbData);
2738 if (result != ERROR_SUCCESS) goto cleanup;
2739 if (!(dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ) || !cbData) {
2740 result = ERROR_FILE_NOT_FOUND;
2741 goto cleanup;
2743 pwszTempBuffer = heap_alloc(cbData);
2744 if (!pwszTempBuffer) {
2745 result = ERROR_NOT_ENOUGH_MEMORY;
2746 goto cleanup;
2748 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, (LPBYTE)pwszTempBuffer, &cbData);
2749 if (result != ERROR_SUCCESS) goto cleanup;
2751 /* '@' is the prefix for resource based string entries. */
2752 if (*pwszTempBuffer != '@') {
2753 result = ERROR_INVALID_DATA;
2754 goto cleanup;
2757 /* Expand environment variables regardless of the type. */
2758 cbData = ExpandEnvironmentStringsW(pwszTempBuffer, NULL, 0) * sizeof(WCHAR);
2759 if (!cbData) goto cleanup;
2760 pwszExpandedBuffer = heap_alloc(cbData);
2761 if (!pwszExpandedBuffer) {
2762 result = ERROR_NOT_ENOUGH_MEMORY;
2763 goto cleanup;
2765 ExpandEnvironmentStringsW(pwszTempBuffer, pwszExpandedBuffer, cbData / sizeof(WCHAR));
2767 /* Parse the value and load the string. */
2769 WCHAR *pComma = wcsrchr(pwszExpandedBuffer, ','), *pNewBuffer;
2770 UINT uiStringId;
2771 DWORD baseDirLen;
2772 int reqChars;
2774 /* Format of the expanded value is 'path_to_dll,-resId' */
2775 if (!pComma || pComma[1] != '-') {
2776 result = ERROR_INVALID_DATA;
2777 goto cleanup;
2780 uiStringId = wcstol(pComma+2, NULL, 10);
2781 *pComma = '\0';
2783 /* Build a resource dll path. */
2784 baseDirLen = pwszBaseDir ? lstrlenW(pwszBaseDir) : 0;
2785 cbData = (baseDirLen + 1 + lstrlenW(pwszExpandedBuffer + 1) + 1) * sizeof(WCHAR);
2786 pNewBuffer = heap_realloc(pwszTempBuffer, cbData);
2787 if (!pNewBuffer) {
2788 result = ERROR_NOT_ENOUGH_MEMORY;
2789 goto cleanup;
2791 pwszTempBuffer = pNewBuffer;
2792 pwszTempBuffer[0] = '\0';
2793 if (baseDirLen) {
2794 lstrcpyW(pwszTempBuffer, pwszBaseDir);
2795 if (pwszBaseDir[baseDirLen - 1] != '\\')
2796 lstrcatW(pwszTempBuffer, L"\\");
2798 lstrcatW(pwszTempBuffer, pwszExpandedBuffer + 1);
2800 /* Load specified string from the file */
2801 reqChars = 0;
2802 result = load_mui_string(pwszTempBuffer, uiStringId, pwszBuffer, cbBuffer/sizeof(WCHAR), &reqChars, dwFlags);
2803 if (pcbData && (result == ERROR_SUCCESS || result == ERROR_MORE_DATA))
2804 *pcbData = reqChars * sizeof(WCHAR);
2807 cleanup:
2808 heap_free(pwszTempBuffer);
2809 heap_free(pwszExpandedBuffer);
2810 return result;
2813 /******************************************************************************
2814 * RegLoadMUIStringA (kernelbase.@)
2816 * Not implemented on native.
2818 LSTATUS WINAPI RegLoadMUIStringA(HKEY hKey, LPCSTR pszValue, LPSTR pszBuffer, DWORD cbBuffer,
2819 LPDWORD pcbData, DWORD dwFlags, LPCSTR pszBaseDir)
2821 return ERROR_CALL_NOT_IMPLEMENTED;
2825 /******************************************************************************
2826 * RegDeleteTreeW (kernelbase.@)
2829 LSTATUS WINAPI RegDeleteTreeW( HKEY hkey, const WCHAR *subkey )
2831 DWORD name_size, max_name, max_subkey;
2832 WCHAR *name_buf = NULL;
2833 LONG ret;
2835 TRACE( "(%p, %s)\n", hkey, debugstr_w(subkey) );
2837 if (subkey && *subkey)
2839 ret = RegOpenKeyExW( hkey, subkey, 0, KEY_READ, &hkey );
2840 if (ret) return ret;
2843 ret = RegQueryInfoKeyW( hkey, NULL, NULL, NULL, NULL, &max_subkey,
2844 NULL, NULL, &max_name, NULL, NULL, NULL );
2845 if (ret)
2846 goto cleanup;
2848 max_name = max( max_subkey, max_name ) + 1;
2849 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
2851 ret = ERROR_NOT_ENOUGH_MEMORY;
2852 goto cleanup;
2855 /* Recursively delete subkeys */
2856 for (;;)
2858 name_size = max_name;
2859 ret = RegEnumKeyExW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2860 if (ret == ERROR_NO_MORE_ITEMS) break;
2861 if (ret) goto cleanup;
2862 ret = RegDeleteTreeW( hkey, name_buf );
2863 if (ret) goto cleanup;
2866 /* Delete the key itself */
2867 if (subkey && *subkey)
2869 ret = RegDeleteKeyExW( hkey, L"", 0, 0 );
2870 goto cleanup;
2873 /* Delete values */
2874 for (;;)
2876 name_size = max_name;
2877 ret = RegEnumValueW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2878 if (ret == ERROR_NO_MORE_ITEMS) break;
2879 if (ret) goto cleanup;
2880 ret = RegDeleteValueW( hkey, name_buf );
2881 if (ret) goto cleanup;
2884 ret = ERROR_SUCCESS;
2886 cleanup:
2887 heap_free( name_buf );
2888 if (subkey && *subkey)
2889 RegCloseKey( hkey );
2890 return ret;
2894 /******************************************************************************
2895 * RegDeleteTreeA (kernelbase.@)
2898 LSTATUS WINAPI RegDeleteTreeA( HKEY hkey, const char *subkey )
2900 UNICODE_STRING subkeyW;
2901 LONG ret;
2903 if (subkey) RtlCreateUnicodeStringFromAsciiz( &subkeyW, subkey );
2904 else subkeyW.Buffer = NULL;
2905 ret = RegDeleteTreeW( hkey, subkeyW.Buffer );
2906 RtlFreeUnicodeString( &subkeyW );
2907 return ret;
2911 /******************************************************************************
2912 * RegCopyTreeW (kernelbase.@)
2915 LSTATUS WINAPI RegCopyTreeW( HKEY hsrc, const WCHAR *subkey, HKEY hdst )
2917 DWORD name_size, max_name;
2918 DWORD value_size, max_value;
2919 DWORD max_subkey, i, type;
2920 WCHAR *name_buf = NULL;
2921 BYTE *value_buf = NULL;
2922 HKEY hkey;
2923 LONG ret;
2925 TRACE( "(%p, %s, %p)\n", hsrc, debugstr_w(subkey), hdst );
2927 if (subkey)
2929 ret = RegOpenKeyExW( hsrc, subkey, 0, KEY_READ, &hsrc );
2930 if (ret) return ret;
2933 ret = RegQueryInfoKeyW( hsrc, NULL, NULL, NULL, NULL, &max_subkey,
2934 NULL, NULL, &max_name, &max_value, NULL, NULL );
2935 if (ret)
2936 goto cleanup;
2938 max_name = max( max_subkey, max_name ) + 1;
2939 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
2941 ret = ERROR_NOT_ENOUGH_MEMORY;
2942 goto cleanup;
2945 if (!(value_buf = heap_alloc( max_value )))
2947 ret = ERROR_NOT_ENOUGH_MEMORY;
2948 goto cleanup;
2951 /* Copy values */
2952 for (i = 0;; i++)
2954 name_size = max_name;
2955 value_size = max_value;
2956 ret = RegEnumValueW( hsrc, i, name_buf, &name_size, NULL, &type, value_buf, &value_size );
2957 if (ret == ERROR_NO_MORE_ITEMS) break;
2958 if (ret) goto cleanup;
2959 ret = RegSetValueExW( hdst, name_buf, 0, type, value_buf, value_size );
2960 if (ret) goto cleanup;
2963 /* Recursively copy subkeys */
2964 for (i = 0;; i++)
2966 name_size = max_name;
2967 ret = RegEnumKeyExW( hsrc, i, name_buf, &name_size, NULL, NULL, NULL, NULL );
2968 if (ret == ERROR_NO_MORE_ITEMS) break;
2969 if (ret) goto cleanup;
2970 ret = RegCreateKeyExW( hdst, name_buf, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL );
2971 if (ret) goto cleanup;
2972 ret = RegCopyTreeW( hsrc, name_buf, hkey );
2973 RegCloseKey( hkey );
2974 if (ret) goto cleanup;
2977 ret = ERROR_SUCCESS;
2979 cleanup:
2980 heap_free( name_buf );
2981 heap_free( value_buf );
2982 if (subkey)
2983 RegCloseKey( hsrc );
2984 return ret;
2988 /******************************************************************************
2989 * RegLoadAppKeyA (kernelbase.@)
2992 LSTATUS WINAPI RegLoadAppKeyA(const char *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
2994 FIXME("%s %p %u %u %u: stub\n", wine_dbgstr_a(file), result, sam, options, reserved);
2996 if (!file || reserved)
2997 return ERROR_INVALID_PARAMETER;
2999 *result = (HKEY)0xdeadbeef;
3000 return ERROR_SUCCESS;
3003 /******************************************************************************
3004 * RegLoadAppKeyW (kernelbase.@)
3007 LSTATUS WINAPI RegLoadAppKeyW(const WCHAR *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3009 FIXME("%s %p %u %u %u: stub\n", wine_dbgstr_w(file), result, sam, options, reserved);
3011 if (!file || reserved)
3012 return ERROR_INVALID_PARAMETER;
3014 *result = (HKEY)0xdeadbeef;
3015 return ERROR_SUCCESS;
3019 /***********************************************************************
3020 * DnsHostnameToComputerNameExW (kernelbase.@)
3022 * FIXME: how is this different from the non-Ex function?
3024 BOOL WINAPI DECLSPEC_HOTPATCH DnsHostnameToComputerNameExW( const WCHAR *hostname, WCHAR *computername,
3025 DWORD *size )
3027 static const WCHAR allowed[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&')(-_{}";
3028 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3029 DWORD i, len;
3031 lstrcpynW( buffer, hostname, MAX_COMPUTERNAME_LENGTH + 1 );
3032 len = lstrlenW( buffer );
3033 if (*size < len + 1)
3035 *size = len;
3036 SetLastError( ERROR_MORE_DATA );
3037 return FALSE;
3039 *size = len;
3040 if (!computername) return FALSE;
3041 for (i = 0; i < len; i++)
3043 if (buffer[i] >= 'a' && buffer[i] <= 'z') computername[i] = buffer[i] + 'A' - 'a';
3044 else computername[i] = wcschr( allowed, buffer[i] ) ? buffer[i] : '_';
3046 computername[len] = 0;
3047 return TRUE;
3051 /***********************************************************************
3052 * GetComputerNameExA (kernelbase.@)
3054 BOOL WINAPI GetComputerNameExA( COMPUTER_NAME_FORMAT type, char *name, DWORD *len )
3056 BOOL ret = FALSE;
3057 DWORD lenA, lenW = 0;
3058 WCHAR *buffer;
3060 GetComputerNameExW( type, NULL, &lenW );
3061 if (GetLastError() != ERROR_MORE_DATA) return FALSE;
3063 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
3065 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3066 return FALSE;
3068 if (GetComputerNameExW( type, buffer, &lenW ))
3070 lenA = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
3071 if (lenA > *len)
3073 *len = lenA;
3074 SetLastError( ERROR_MORE_DATA );
3076 else
3078 WideCharToMultiByte( CP_ACP, 0, buffer, -1, name, *len, NULL, NULL );
3079 *len = lenA - 1;
3080 ret = TRUE;
3083 HeapFree( GetProcessHeap(), 0, buffer );
3084 return ret;
3088 /***********************************************************************
3089 * GetComputerNameExW (kernelbase.@)
3091 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, WCHAR *name, DWORD *len )
3093 const WCHAR *keyname, *valuename;
3094 LRESULT ret;
3095 HKEY key;
3097 switch (type)
3099 case ComputerNameNetBIOS:
3100 case ComputerNamePhysicalNetBIOS:
3101 keyname = L"System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName";
3102 valuename = L"ComputerName";
3103 break;
3104 case ComputerNameDnsHostname:
3105 case ComputerNamePhysicalDnsHostname:
3106 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3107 valuename = L"Hostname";
3108 break;
3109 case ComputerNameDnsDomain:
3110 case ComputerNamePhysicalDnsDomain:
3111 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3112 valuename = L"Domain";
3113 break;
3114 case ComputerNameDnsFullyQualified:
3115 case ComputerNamePhysicalDnsFullyQualified:
3117 WCHAR *domain, buffer[256];
3118 DWORD size = ARRAY_SIZE(buffer) - 1;
3120 if (!GetComputerNameExW( ComputerNameDnsHostname, buffer, &size )) return FALSE;
3121 domain = buffer + lstrlenW(buffer);
3122 *domain++ = '.';
3123 size = ARRAY_SIZE(buffer) - (domain - buffer);
3124 if (!GetComputerNameExW( ComputerNameDnsDomain, domain, &size )) return FALSE;
3125 if (!*domain) domain[-1] = 0;
3126 size = lstrlenW(buffer);
3127 if (name && size < *len)
3129 if (name) lstrcpyW( name, buffer );
3130 *len = size;
3131 return TRUE;
3133 *len = size + 1;
3134 SetLastError( ERROR_MORE_DATA );
3135 return FALSE;
3137 default:
3138 SetLastError( ERROR_INVALID_PARAMETER );
3139 return FALSE;
3142 if (!(ret = RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &key )))
3144 DWORD size = *len * sizeof(WCHAR);
3145 ret = RegQueryValueExW( key, valuename, NULL, NULL, (BYTE *)name, &size );
3146 if (!name) ret = ERROR_MORE_DATA;
3147 else if (!ret) size -= sizeof(WCHAR);
3148 *len = size / sizeof(WCHAR);
3149 RegCloseKey( key );
3151 TRACE("-> %lu %s\n", ret, debugstr_w(name) );
3152 if (ret) SetLastError( ret );
3153 return !ret;
3157 /***********************************************************************
3158 * SetComputerNameA (kernelbase.@)
3160 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameA( const char *name )
3162 BOOL ret;
3163 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3164 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3166 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3167 ret = SetComputerNameExW( ComputerNamePhysicalNetBIOS, nameW );
3168 HeapFree( GetProcessHeap(), 0, nameW );
3169 return ret;
3173 /***********************************************************************
3174 * SetComputerNameW (kernelbase.@)
3176 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameW( const WCHAR *name )
3178 return SetComputerNameExW( ComputerNamePhysicalNetBIOS, name );
3182 /***********************************************************************
3183 * SetComputerNameExA (kernelbase.@)
3185 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExA( COMPUTER_NAME_FORMAT type, const char *name )
3187 BOOL ret;
3188 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3189 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3191 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3192 ret = SetComputerNameExW( type, nameW );
3193 HeapFree( GetProcessHeap(), 0, nameW );
3194 return ret;
3198 /***********************************************************************
3199 * SetComputerNameExW (kernelbase.@)
3201 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExW( COMPUTER_NAME_FORMAT type, const WCHAR *name )
3203 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3204 DWORD size;
3205 HKEY key;
3206 LRESULT ret;
3208 TRACE( "%u %s\n", type, debugstr_w( name ));
3210 switch (type)
3212 case ComputerNameDnsHostname:
3213 case ComputerNamePhysicalDnsHostname:
3214 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3215 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3216 if (ret) break;
3217 ret = RegSetValueExW( key, L"Hostname", 0, REG_SZ,
3218 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3219 RegCloseKey( key );
3220 /* fall through */
3222 case ComputerNameNetBIOS:
3223 case ComputerNamePhysicalNetBIOS:
3224 /* @@ Wine registry key: HKCU\Software\Wine\Network */
3225 if (!RegOpenKeyExW( HKEY_CURRENT_USER, L"Software\\Wine\\Network", 0, KEY_READ, &key ))
3227 BOOL use_dns = TRUE;
3228 size = sizeof(buffer);
3229 if (!RegQueryValueExW( key, L"UseDnsComputerName", NULL, NULL, (BYTE *)buffer, &size ))
3230 use_dns = IS_OPTION_TRUE( buffer[0] );
3231 RegCloseKey( key );
3232 if (!use_dns)
3234 ret = ERROR_ACCESS_DENIED;
3235 break;
3238 size = ARRAY_SIZE( buffer );
3239 if (!DnsHostnameToComputerNameExW( name, buffer, &size )) return FALSE;
3240 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
3241 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3242 if (ret) break;
3243 ret = RegSetValueExW( key, L"ComputerName", 0, REG_SZ,
3244 (BYTE *)buffer, (lstrlenW(buffer) + 1) * sizeof(WCHAR) );
3245 RegCloseKey( key );
3246 break;
3248 case ComputerNameDnsDomain:
3249 case ComputerNamePhysicalDnsDomain:
3250 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3251 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3252 if (ret) break;
3253 ret = RegSetValueExW( key, L"Domain", 0, REG_SZ,
3254 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3255 RegCloseKey( key );
3256 break;
3257 default:
3258 ret = ERROR_INVALID_PARAMETER;
3259 break;
3261 if (ret) SetLastError( ret );
3262 return !ret;
3265 struct USKEY
3267 HKEY HKCUstart; /* Start key in CU hive */
3268 HKEY HKCUkey; /* Opened key in CU hive */
3269 HKEY HKLMstart; /* Start key in LM hive */
3270 HKEY HKLMkey; /* Opened key in LM hive */
3271 WCHAR path[MAX_PATH];
3274 LONG WINAPI SHRegCreateUSKeyA(LPCSTR path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3276 WCHAR *pathW;
3277 LONG ret;
3279 TRACE("%s, %#x, %p, %p, %#x\n", debugstr_a(path), samDesired, relative_key, new_uskey, flags);
3281 if (path)
3283 INT len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
3284 pathW = heap_alloc(len * sizeof(WCHAR));
3285 if (!pathW)
3286 return ERROR_NOT_ENOUGH_MEMORY;
3287 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
3289 else
3290 pathW = NULL;
3292 ret = SHRegCreateUSKeyW(pathW, samDesired, relative_key, new_uskey, flags);
3293 HeapFree(GetProcessHeap(), 0, pathW);
3294 return ret;
3297 static HKEY reg_duplicate_hkey(HKEY hKey)
3299 HKEY newKey = 0;
3301 RegOpenKeyExW(hKey, 0, 0, MAXIMUM_ALLOWED, &newKey);
3302 return newKey;
3305 static HKEY reg_get_hkey_from_huskey(HUSKEY hUSKey, BOOL is_hkcu)
3307 struct USKEY *mihk = hUSKey;
3308 HKEY test = hUSKey;
3310 if (test == HKEY_CLASSES_ROOT
3311 || test == HKEY_CURRENT_CONFIG
3312 || test == HKEY_CURRENT_USER
3313 || test == HKEY_DYN_DATA
3314 || test == HKEY_LOCAL_MACHINE
3315 || test == HKEY_PERFORMANCE_DATA
3316 || test == HKEY_USERS)
3317 /* FIXME: need to define for Win2k, ME, XP
3318 * (test == HKEY_PERFORMANCE_TEXT) ||
3319 * (test == HKEY_PERFORMANCE_NLSTEXT) ||
3322 return test;
3325 return is_hkcu ? mihk->HKCUkey : mihk->HKLMkey;
3328 LONG WINAPI SHRegCreateUSKeyW(const WCHAR *path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3330 LONG ret = ERROR_CALL_NOT_IMPLEMENTED;
3331 struct USKEY *ret_key;
3333 TRACE("%s, %#x, %p, %p, %#x\n", debugstr_w(path), samDesired, relative_key, new_uskey, flags);
3335 if (!new_uskey)
3336 return ERROR_INVALID_PARAMETER;
3338 *new_uskey = NULL;
3340 if (flags & ~SHREGSET_FORCE_HKCU)
3342 FIXME("unsupported flags 0x%08x\n", flags);
3343 return ERROR_SUCCESS;
3346 ret_key = heap_alloc_zero(sizeof(*ret_key));
3347 lstrcpynW(ret_key->path, path, ARRAY_SIZE(ret_key->path));
3349 if (relative_key)
3351 ret_key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3352 ret_key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3354 else
3356 ret_key->HKCUstart = HKEY_CURRENT_USER;
3357 ret_key->HKLMstart = HKEY_LOCAL_MACHINE;
3360 if (flags & SHREGSET_FORCE_HKCU)
3362 ret = RegCreateKeyExW(ret_key->HKCUstart, path, 0, NULL, 0, samDesired, NULL, &ret_key->HKCUkey, NULL);
3363 if (ret == ERROR_SUCCESS)
3364 *new_uskey = ret_key;
3365 else
3366 heap_free(ret_key);
3369 return ret;
3372 LONG WINAPI SHRegCloseUSKey(HUSKEY hUSKey)
3374 struct USKEY *key = hUSKey;
3375 LONG ret = ERROR_SUCCESS;
3377 if (!key)
3378 return ERROR_INVALID_PARAMETER;
3380 if (key->HKCUkey)
3381 ret = RegCloseKey(key->HKCUkey);
3382 if (key->HKCUstart && key->HKCUstart != HKEY_CURRENT_USER)
3383 ret = RegCloseKey(key->HKCUstart);
3384 if (key->HKLMkey)
3385 ret = RegCloseKey(key->HKLMkey);
3386 if (key->HKLMstart && key->HKLMstart != HKEY_LOCAL_MACHINE)
3387 ret = RegCloseKey(key->HKLMstart);
3389 heap_free(key);
3390 return ret;
3393 LONG WINAPI SHRegDeleteEmptyUSKeyA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3395 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3396 return ERROR_SUCCESS;
3399 LONG WINAPI SHRegDeleteEmptyUSKeyW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3401 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3402 return ERROR_SUCCESS;
3405 LONG WINAPI SHRegDeleteUSValueA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3407 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3408 return ERROR_SUCCESS;
3411 LONG WINAPI SHRegDeleteUSValueW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3413 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3414 return ERROR_SUCCESS;
3417 LONG WINAPI SHRegEnumUSValueA(HUSKEY hUSKey, DWORD index, char *value_name, DWORD *value_name_len, DWORD *type,
3418 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3420 HKEY dokey;
3422 TRACE("%p, %#x, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3424 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3425 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3427 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3428 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3430 FIXME("no support for SHREGENUM_BOTH\n");
3431 return ERROR_INVALID_FUNCTION;
3434 LONG WINAPI SHRegEnumUSValueW(HUSKEY hUSKey, DWORD index, WCHAR *value_name, DWORD *value_name_len, DWORD *type,
3435 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3437 HKEY dokey;
3439 TRACE("%p, %#x, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3441 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3442 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3444 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3445 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3447 FIXME("no support for SHREGENUM_BOTH\n");
3448 return ERROR_INVALID_FUNCTION;
3451 LONG WINAPI SHRegEnumUSKeyA(HUSKEY hUSKey, DWORD index, char *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3453 HKEY dokey;
3455 TRACE("%p, %d, %p, %p(%d), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3457 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3458 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3460 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3461 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3463 FIXME("no support for SHREGENUM_BOTH\n");
3464 return ERROR_INVALID_FUNCTION;
3467 LONG WINAPI SHRegEnumUSKeyW(HUSKEY hUSKey, DWORD index, WCHAR *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3469 HKEY dokey;
3471 TRACE("%p, %d, %p, %p(%d), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3473 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3474 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3476 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3477 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3479 FIXME("no support for SHREGENUM_BOTH\n");
3480 return ERROR_INVALID_FUNCTION;
3483 LONG WINAPI SHRegOpenUSKeyA(const char *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3485 WCHAR pathW[MAX_PATH];
3487 if (path)
3488 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, ARRAY_SIZE(pathW));
3490 return SHRegOpenUSKeyW(path ? pathW : NULL, access_mask, relative_key, uskey, ignore_hkcu);
3493 LONG WINAPI SHRegOpenUSKeyW(const WCHAR *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3495 LONG ret2, ret1 = ~ERROR_SUCCESS;
3496 struct USKEY *key;
3498 TRACE("%s, %#x, %p, %p, %d\n", debugstr_w(path), access_mask, relative_key, uskey, ignore_hkcu);
3500 if (uskey)
3501 *uskey = NULL;
3503 /* Create internal HUSKEY */
3504 key = heap_alloc_zero(sizeof(*key));
3505 lstrcpynW(key->path, path, ARRAY_SIZE(key->path));
3507 if (relative_key)
3509 key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3510 key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3512 /* FIXME: if either of these keys is NULL, create the start key from
3513 * the relative keys start+path
3516 else
3518 key->HKCUstart = HKEY_CURRENT_USER;
3519 key->HKLMstart = HKEY_LOCAL_MACHINE;
3522 if (!ignore_hkcu)
3524 ret1 = RegOpenKeyExW(key->HKCUstart, key->path, 0, access_mask, &key->HKCUkey);
3525 if (ret1)
3526 key->HKCUkey = 0;
3529 ret2 = RegOpenKeyExW(key->HKLMstart, key->path, 0, access_mask, &key->HKLMkey);
3530 if (ret2)
3531 key->HKLMkey = 0;
3533 if (ret1 || ret2)
3534 TRACE("one or more opens failed: HKCU=%d HKLM=%d\n", ret1, ret2);
3536 if (ret1 && ret2)
3538 /* Neither open succeeded: fail */
3539 SHRegCloseUSKey(key);
3540 return ret2;
3543 TRACE("HUSKEY=%p\n", key);
3544 if (uskey)
3545 *uskey = key;
3547 return ERROR_SUCCESS;
3550 LONG WINAPI SHRegWriteUSValueA(HUSKEY hUSKey, const char *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3552 WCHAR valueW[MAX_PATH];
3554 if (value)
3555 MultiByteToWideChar(CP_ACP, 0, value, -1, valueW, ARRAY_SIZE(valueW));
3557 return SHRegWriteUSValueW(hUSKey, value ? valueW : NULL, type, data, data_len, flags);
3560 LONG WINAPI SHRegWriteUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3562 struct USKEY *hKey = hUSKey;
3563 LONG ret = ERROR_SUCCESS;
3564 DWORD dummy;
3566 TRACE("%p, %s, %d, %p, %d, %#x\n", hUSKey, debugstr_w(value), type, data, data_len, flags);
3568 __TRY
3570 dummy = hKey->HKCUkey || hKey->HKLMkey;
3572 __EXCEPT_PAGE_FAULT
3574 return ERROR_INVALID_PARAMETER;
3576 __ENDTRY
3577 if (!(flags & (SHREGSET_FORCE_HKCU|SHREGSET_FORCE_HKLM))) return ERROR_INVALID_PARAMETER;
3579 if (flags & (SHREGSET_FORCE_HKCU | SHREGSET_HKCU))
3581 if (!hKey->HKCUkey)
3583 /* Create the key */
3584 ret = RegCreateKeyExW(hKey->HKCUstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3585 MAXIMUM_ALLOWED, NULL, &hKey->HKCUkey, NULL);
3586 TRACE("Creating HKCU key, ret = %d\n", ret);
3587 if (ret && (flags & SHREGSET_FORCE_HKCU))
3589 hKey->HKCUkey = 0;
3590 return ret;
3594 if (!ret)
3596 if ((flags & SHREGSET_FORCE_HKCU) || RegQueryValueExW(hKey->HKCUkey, value, NULL, NULL, NULL, &dummy))
3598 /* Doesn't exist or we are forcing: Write value */
3599 ret = RegSetValueExW(hKey->HKCUkey, value, 0, type, data, data_len);
3600 TRACE("Writing HKCU value, ret = %d\n", ret);
3605 if (flags & (SHREGSET_FORCE_HKLM | SHREGSET_HKLM))
3607 if (!hKey->HKLMkey)
3609 /* Create the key */
3610 ret = RegCreateKeyExW(hKey->HKLMstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3611 MAXIMUM_ALLOWED, NULL, &hKey->HKLMkey, NULL);
3612 TRACE("Creating HKLM key, ret = %d\n", ret);
3613 if (ret && (flags & (SHREGSET_FORCE_HKLM)))
3615 hKey->HKLMkey = 0;
3616 return ret;
3620 if (!ret)
3622 if ((flags & SHREGSET_FORCE_HKLM) || RegQueryValueExW(hKey->HKLMkey, value, NULL, NULL, NULL, &dummy))
3624 /* Doesn't exist or we are forcing: Write value */
3625 ret = RegSetValueExW(hKey->HKLMkey, value, 0, type, data, data_len);
3626 TRACE("Writing HKLM value, ret = %d\n", ret);
3631 return ret;
3634 LONG WINAPI SHRegSetUSValueA(const char *subkey, const char *value, DWORD type, void *data, DWORD data_len,
3635 DWORD flags)
3637 BOOL ignore_hkcu;
3638 HUSKEY hkey;
3639 LONG ret;
3641 TRACE("%s, %s, %d, %p, %d, %#x\n", debugstr_a(subkey), debugstr_a(value), type, data, data_len, flags);
3643 if (!data)
3644 return ERROR_INVALID_FUNCTION;
3646 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3648 ret = SHRegOpenUSKeyA(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3649 if (ret == ERROR_SUCCESS)
3651 ret = SHRegWriteUSValueA(hkey, value, type, data, data_len, flags);
3652 SHRegCloseUSKey(hkey);
3655 return ret;
3658 LONG WINAPI SHRegSetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD type, void *data, DWORD data_len,
3659 DWORD flags)
3661 BOOL ignore_hkcu;
3662 HUSKEY hkey;
3663 LONG ret;
3665 TRACE("%s, %s, %d, %p, %d, %#x\n", debugstr_w(subkey), debugstr_w(value), type, data, data_len, flags);
3667 if (!data)
3668 return ERROR_INVALID_FUNCTION;
3670 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3672 ret = SHRegOpenUSKeyW(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3673 if (ret == ERROR_SUCCESS)
3675 ret = SHRegWriteUSValueW(hkey, value, type, data, data_len, flags);
3676 SHRegCloseUSKey(hkey);
3679 return ret;
3682 LONG WINAPI SHRegQueryInfoUSKeyA(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3683 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3685 HKEY dokey;
3686 LONG ret;
3688 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3690 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3692 ret = RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3693 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3694 return ret;
3697 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3699 return RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3702 return ERROR_INVALID_FUNCTION;
3705 LONG WINAPI SHRegQueryInfoUSKeyW(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3706 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3708 HKEY dokey;
3709 LONG ret;
3711 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3713 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3715 ret = RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3716 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3717 return ret;
3720 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3722 return RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3725 return ERROR_INVALID_FUNCTION;
3728 LONG WINAPI SHRegQueryUSValueA(HUSKEY hUSKey, const char *value, DWORD *type, void *data, DWORD *data_len,
3729 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3731 LONG ret = ~ERROR_SUCCESS;
3732 DWORD move_len;
3733 HKEY dokey;
3735 /* If user wants HKCU, and it exists, then try it */
3736 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3738 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3739 TRACE("HKCU RegQueryValue returned %d\n", ret);
3742 /* If HKCU did not work and HKLM exists, then try it */
3743 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3745 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3746 TRACE("HKLM RegQueryValue returned %d\n", ret);
3749 /* If neither worked, and default data exists, then use it */
3750 if (ret != ERROR_SUCCESS)
3752 if (default_data && default_data_len)
3754 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3755 memmove(data, default_data, move_len);
3756 *data_len = move_len;
3757 TRACE("setting default data\n");
3758 ret = ERROR_SUCCESS;
3762 return ret;
3765 LONG WINAPI SHRegQueryUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3766 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3768 LONG ret = ~ERROR_SUCCESS;
3769 DWORD move_len;
3770 HKEY dokey;
3772 /* If user wants HKCU, and it exists, then try it */
3773 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3775 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3776 TRACE("HKCU RegQueryValue returned %d\n", ret);
3779 /* If HKCU did not work and HKLM exists, then try it */
3780 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3782 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3783 TRACE("HKLM RegQueryValue returned %d\n", ret);
3786 /* If neither worked, and default data exists, then use it */
3787 if (ret != ERROR_SUCCESS)
3789 if (default_data && default_data_len)
3791 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3792 memmove(data, default_data, move_len);
3793 *data_len = move_len;
3794 TRACE("setting default data\n");
3795 ret = ERROR_SUCCESS;
3799 return ret;
3802 LONG WINAPI SHRegGetUSValueA(const char *subkey, const char *value, DWORD *type, void *data, DWORD *data_len,
3803 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3805 HUSKEY myhuskey;
3806 LONG ret;
3808 if (!data || !data_len)
3809 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3811 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), *data_len);
3813 ret = SHRegOpenUSKeyA(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3814 if (!ret)
3816 ret = SHRegQueryUSValueA(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3817 SHRegCloseUSKey(myhuskey);
3820 return ret;
3823 LONG WINAPI SHRegGetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3824 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3826 HUSKEY myhuskey;
3827 LONG ret;
3829 if (!data || !data_len)
3830 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3832 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), *data_len);
3834 ret = SHRegOpenUSKeyW(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3835 if (!ret)
3837 ret = SHRegQueryUSValueW(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3838 SHRegCloseUSKey(myhuskey);
3841 return ret;
3844 BOOL WINAPI SHRegGetBoolUSValueA(const char *subkey, const char *value, BOOL ignore_hkcu, BOOL default_value)
3846 BOOL ret = default_value;
3847 DWORD type, datalen;
3848 char data[10];
3850 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), ignore_hkcu);
3852 datalen = ARRAY_SIZE(data) - 1;
3853 if (!SHRegGetUSValueA(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3855 switch (type)
3857 case REG_SZ:
3858 data[9] = '\0';
3859 if (!lstrcmpiA(data, "YES") || !lstrcmpiA(data, "TRUE"))
3860 ret = TRUE;
3861 else if (!lstrcmpiA(data, "NO") || !lstrcmpiA(data, "FALSE"))
3862 ret = FALSE;
3863 break;
3864 case REG_DWORD:
3865 ret = *(DWORD *)data != 0;
3866 break;
3867 case REG_BINARY:
3868 if (datalen == 1)
3870 ret = !!data[0];
3871 break;
3873 default:
3874 FIXME("Unsupported registry data type %d\n", type);
3875 ret = FALSE;
3877 TRACE("got value (type=%d), returning %d\n", type, ret);
3879 else
3880 TRACE("returning default value %d\n", ret);
3882 return ret;
3885 BOOL WINAPI SHRegGetBoolUSValueW(const WCHAR *subkey, const WCHAR *value, BOOL ignore_hkcu, BOOL default_value)
3887 BOOL ret = default_value;
3888 DWORD type, datalen;
3889 WCHAR data[10];
3891 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), ignore_hkcu);
3893 datalen = (ARRAY_SIZE(data) - 1) * sizeof(WCHAR);
3894 if (!SHRegGetUSValueW(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3896 switch (type)
3898 case REG_SZ:
3899 data[9] = '\0';
3900 if (!lstrcmpiW(data, L"yes") || !lstrcmpiW(data, L"true"))
3901 ret = TRUE;
3902 else if (!lstrcmpiW(data, L"no") || !lstrcmpiW(data, L"false"))
3903 ret = FALSE;
3904 break;
3905 case REG_DWORD:
3906 ret = *(DWORD *)data != 0;
3907 break;
3908 case REG_BINARY:
3909 if (datalen == 1)
3911 ret = !!data[0];
3912 break;
3914 default:
3915 FIXME("Unsupported registry data type %d\n", type);
3916 ret = FALSE;
3918 TRACE("got value (type=%d), returning %d\n", type, ret);
3920 else
3921 TRACE("returning default value %d\n", ret);
3923 return ret;