winegcc: Add entry symbol underscore when building linker command.
[wine.git] / dlls / kernelbase / registry.c
blobd1588d0b2651d48c2e9b4ef953a2c807a7b57f14
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 DWORD ret;
2353 HKEY shkey;
2354 OBJECT_ATTRIBUTES attr;
2355 UNICODE_STRING subkey;
2357 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2359 ret = RegOpenKeyExW( hkey, lpSubKey, 0, MAXIMUM_ALLOWED, &shkey );
2360 if( ret )
2361 return ERROR_INVALID_PARAMETER;
2363 RtlInitUnicodeString(&subkey, lpSubKey);
2364 InitializeObjectAttributes(&attr, &subkey, OBJ_CASE_INSENSITIVE, shkey, NULL);
2365 ret = RtlNtStatusToDosError(NtUnloadKey(&attr));
2367 RegCloseKey(shkey);
2369 return ret;
2373 /******************************************************************************
2374 * RegUnLoadKeyA (kernelbase.@)
2376 * See RegUnLoadKeyW.
2378 LSTATUS WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2380 UNICODE_STRING lpSubKeyW;
2381 LONG ret;
2383 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2384 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2385 RtlFreeUnicodeString( &lpSubKeyW );
2386 return ret;
2390 /******************************************************************************
2391 * RegSetKeySecurity (kernelbase.@)
2393 * Set the security of an open registry key.
2395 * PARAMS
2396 * hkey [I] Open handle of key to set
2397 * SecurityInfo [I] Descriptor contents
2398 * pSecurityDesc [I] Address of descriptor for key
2400 * RETURNS
2401 * Success: ERROR_SUCCESS
2402 * Failure: nonzero error code from Winerror.h
2404 LSTATUS WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2405 PSECURITY_DESCRIPTOR pSecurityDesc )
2407 TRACE("(%p,%d,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2409 /* It seems to perform this check before the hkey check */
2410 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2411 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2412 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2413 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2414 /* Param OK */
2415 } else
2416 return ERROR_INVALID_PARAMETER;
2418 if (!pSecurityDesc)
2419 return ERROR_INVALID_PARAMETER;
2421 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2423 return RtlNtStatusToDosError( NtSetSecurityObject( hkey, SecurityInfo, pSecurityDesc ) );
2427 /******************************************************************************
2428 * RegGetKeySecurity (kernelbase.@)
2430 * Get a copy of the security descriptor for a given registry key.
2432 * PARAMS
2433 * hkey [I] Open handle of key to set
2434 * SecurityInformation [I] Descriptor contents
2435 * pSecurityDescriptor [O] Address of descriptor for key
2436 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2438 * RETURNS
2439 * Success: ERROR_SUCCESS
2440 * Failure: Error code
2442 LSTATUS WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2443 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2444 LPDWORD lpcbSecurityDescriptor )
2446 TRACE("(%p,%d,%p,%d)\n",hkey,SecurityInformation,pSecurityDescriptor,
2447 *lpcbSecurityDescriptor);
2449 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2451 return RtlNtStatusToDosError( NtQuerySecurityObject( hkey,
2452 SecurityInformation, pSecurityDescriptor,
2453 *lpcbSecurityDescriptor, lpcbSecurityDescriptor ) );
2457 /******************************************************************************
2458 * RegFlushKey (kernelbase.@)
2460 * Immediately write a registry key to registry.
2462 * PARAMS
2463 * hkey [I] Handle of key to write
2465 * RETURNS
2466 * Success: ERROR_SUCCESS
2467 * Failure: Error code
2469 LSTATUS WINAPI RegFlushKey( HKEY hkey )
2471 hkey = get_special_root_hkey( hkey, 0 );
2472 if (!hkey) return ERROR_INVALID_HANDLE;
2474 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2478 /******************************************************************************
2479 * RegNotifyChangeKeyValue (kernelbase.@)
2481 * Notify the caller about changes to the attributes or contents of a registry key.
2483 * PARAMS
2484 * hkey [I] Handle of key to watch
2485 * fWatchSubTree [I] Flag for subkey notification
2486 * fdwNotifyFilter [I] Changes to be reported
2487 * hEvent [I] Handle of signaled event
2488 * fAsync [I] Flag for asynchronous reporting
2490 * RETURNS
2491 * Success: ERROR_SUCCESS
2492 * Failure: nonzero error code from Winerror.h
2494 LSTATUS WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2495 DWORD fdwNotifyFilter, HANDLE hEvent,
2496 BOOL fAsync )
2498 NTSTATUS status;
2499 IO_STATUS_BLOCK iosb;
2501 hkey = get_special_root_hkey( hkey, 0 );
2502 if (!hkey) return ERROR_INVALID_HANDLE;
2504 TRACE("(%p,%i,%d,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2505 hEvent, fAsync);
2507 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2508 fdwNotifyFilter, fWatchSubTree, NULL, 0,
2509 fAsync);
2511 if (status && status != STATUS_PENDING)
2512 return RtlNtStatusToDosError( status );
2514 return ERROR_SUCCESS;
2517 /******************************************************************************
2518 * RegOpenUserClassesRoot (kernelbase.@)
2520 * Open the HKEY_CLASSES_ROOT key for a user.
2522 * PARAMS
2523 * hToken [I] Handle of token representing the user
2524 * dwOptions [I] Reserved, must be 0
2525 * samDesired [I] Desired access rights
2526 * phkResult [O] Destination for the resulting key handle
2528 * RETURNS
2529 * Success: ERROR_SUCCESS
2530 * Failure: nonzero error code from Winerror.h
2532 * NOTES
2533 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2534 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2535 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2537 LSTATUS WINAPI RegOpenUserClassesRoot( HANDLE hToken, DWORD dwOptions, REGSAM samDesired, PHKEY phkResult )
2539 FIXME("(%p, 0x%x, 0x%x, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2541 *phkResult = HKEY_CLASSES_ROOT;
2542 return ERROR_SUCCESS;
2546 static void dump_mui_cache(void)
2548 struct mui_cache_entry *ent;
2550 TRACE("---------- MUI Cache ----------\n");
2551 LIST_FOR_EACH_ENTRY( ent, &reg_mui_cache, struct mui_cache_entry, entry )
2552 TRACE("entry=%p, %s,-%u [%#x] => %s\n",
2553 ent, wine_dbgstr_w(ent->file_name), ent->index, ent->locale, wine_dbgstr_w(ent->text));
2556 static inline void free_mui_cache_entry(struct mui_cache_entry *ent)
2558 heap_free(ent->file_name);
2559 heap_free(ent->text);
2560 heap_free(ent);
2563 /* critical section must be held */
2564 static int reg_mui_cache_get(const WCHAR *file_name, UINT index, WCHAR **buffer)
2566 struct mui_cache_entry *ent;
2568 TRACE("(%s %u %p)\n", wine_dbgstr_w(file_name), index, buffer);
2570 LIST_FOR_EACH_ENTRY(ent, &reg_mui_cache, struct mui_cache_entry, entry)
2572 if (ent->index == index && ent->locale == GetThreadLocale() &&
2573 !lstrcmpiW(ent->file_name, file_name))
2574 goto found;
2576 return 0;
2578 found:
2579 /* move to the list head */
2580 if (list_prev(&reg_mui_cache, &ent->entry)) {
2581 list_remove(&ent->entry);
2582 list_add_head(&reg_mui_cache, &ent->entry);
2585 TRACE("=> %s\n", wine_dbgstr_w(ent->text));
2586 *buffer = ent->text;
2587 return lstrlenW(ent->text);
2590 /* critical section must be held */
2591 static void reg_mui_cache_put(const WCHAR *file_name, UINT index, const WCHAR *buffer, INT size)
2593 struct mui_cache_entry *ent;
2594 TRACE("(%s %u %s %d)\n", wine_dbgstr_w(file_name), index, wine_dbgstr_wn(buffer, size), size);
2596 ent = heap_calloc(sizeof(*ent), 1);
2597 if (!ent)
2598 return;
2599 ent->file_name = heap_alloc((lstrlenW(file_name) + 1) * sizeof(WCHAR));
2600 if (!ent->file_name) {
2601 free_mui_cache_entry(ent);
2602 return;
2604 lstrcpyW(ent->file_name, file_name);
2605 ent->index = index;
2606 ent->locale = GetThreadLocale();
2607 ent->text = heap_alloc((size + 1) * sizeof(WCHAR));
2608 if (!ent->text) {
2609 free_mui_cache_entry(ent);
2610 return;
2612 memcpy(ent->text, buffer, size * sizeof(WCHAR));
2613 ent->text[size] = '\0';
2615 TRACE("add %p\n", ent);
2616 list_add_head(&reg_mui_cache, &ent->entry);
2617 if (reg_mui_cache_count > REG_MUI_CACHE_SIZE) {
2618 ent = LIST_ENTRY( list_tail( &reg_mui_cache ), struct mui_cache_entry, entry );
2619 TRACE("freeing %p\n", ent);
2620 list_remove(&ent->entry);
2621 free_mui_cache_entry(ent);
2623 else
2624 reg_mui_cache_count++;
2626 if (TRACE_ON(reg))
2627 dump_mui_cache();
2628 return;
2631 static LONG load_mui_string(const WCHAR *file_name, UINT res_id, WCHAR *buffer, INT max_chars, INT *req_chars, DWORD flags)
2633 HMODULE hModule = NULL;
2634 WCHAR *string = NULL, *full_name;
2635 int size;
2636 LONG result;
2638 /* Verify the file existence. i.e. We don't rely on PATH variable */
2639 if (GetFileAttributesW(file_name) == INVALID_FILE_ATTRIBUTES)
2640 return ERROR_FILE_NOT_FOUND;
2642 size = GetFullPathNameW(file_name, 0, NULL, NULL);
2643 if (!size)
2644 return GetLastError();
2645 full_name = heap_alloc(size * sizeof(WCHAR));
2646 if (!full_name)
2647 return ERROR_NOT_ENOUGH_MEMORY;
2648 GetFullPathNameW(file_name, size, full_name, NULL);
2650 RtlEnterCriticalSection(&reg_mui_cs);
2651 size = reg_mui_cache_get(full_name, res_id, &string);
2652 if (!size) {
2653 RtlLeaveCriticalSection(&reg_mui_cs);
2655 /* Load the file */
2656 hModule = LoadLibraryExW(full_name, NULL,
2657 LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
2658 if (!hModule)
2659 return GetLastError();
2661 size = LoadStringW(hModule, res_id, (WCHAR *)&string, 0);
2662 if (!size) {
2663 if (string) result = ERROR_NOT_FOUND;
2664 else result = GetLastError();
2665 goto cleanup;
2668 RtlEnterCriticalSection(&reg_mui_cs);
2669 reg_mui_cache_put(full_name, res_id, string, size);
2670 RtlLeaveCriticalSection(&reg_mui_cs);
2672 *req_chars = size + 1;
2674 /* If no buffer is given, skip copying. */
2675 if (!buffer) {
2676 result = ERROR_MORE_DATA;
2677 goto cleanup;
2680 /* Else copy over the string, respecting the buffer size. */
2681 if (size < max_chars)
2682 max_chars = size;
2683 else {
2684 if (flags & REG_MUI_STRING_TRUNCATE)
2685 max_chars--;
2686 else {
2687 result = ERROR_MORE_DATA;
2688 goto cleanup;
2691 if (max_chars >= 0) {
2692 memcpy(buffer, string, max_chars * sizeof(WCHAR));
2693 buffer[max_chars] = '\0';
2696 result = ERROR_SUCCESS;
2698 cleanup:
2699 if (hModule)
2700 FreeLibrary(hModule);
2701 else
2702 RtlLeaveCriticalSection(&reg_mui_cs);
2703 heap_free(full_name);
2704 return result;
2707 /******************************************************************************
2708 * RegLoadMUIStringW (kernelbase.@)
2710 * Load the localized version of a string resource from some PE, respective
2711 * id and path of which are given in the registry value in the format
2712 * @[path]\dllname,-resourceId
2714 * PARAMS
2715 * hKey [I] Key, of which to load the string value from.
2716 * pszValue [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2717 * pszBuffer [O] Buffer to store the localized string in.
2718 * cbBuffer [I] Size of the destination buffer in bytes.
2719 * pcbData [O] Number of bytes written to pszBuffer (optional, may be NULL).
2720 * dwFlags [I] Truncate output to fit the buffer if REG_MUI_STRING_TRUNCATE.
2721 * pszBaseDir [I] Base directory of loading path. If NULL, use the current directory.
2723 * RETURNS
2724 * Success: ERROR_SUCCESS,
2725 * Failure: nonzero error code from winerror.h
2727 LSTATUS WINAPI RegLoadMUIStringW(HKEY hKey, LPCWSTR pwszValue, LPWSTR pwszBuffer, DWORD cbBuffer,
2728 LPDWORD pcbData, DWORD dwFlags, LPCWSTR pwszBaseDir)
2730 DWORD dwValueType, cbData;
2731 LPWSTR pwszTempBuffer = NULL, pwszExpandedBuffer = NULL;
2732 LONG result;
2734 TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %d, pcbData = %p, "
2735 "dwFlags = %d, pwszBaseDir = %s)\n", hKey, debugstr_w(pwszValue), pwszBuffer,
2736 cbBuffer, pcbData, dwFlags, debugstr_w(pwszBaseDir));
2738 /* Parameter sanity checks. */
2739 if (!hKey || (!pwszBuffer && cbBuffer) || (cbBuffer % sizeof(WCHAR))
2740 || ((dwFlags & REG_MUI_STRING_TRUNCATE) && pcbData)
2741 || (dwFlags & ~REG_MUI_STRING_TRUNCATE))
2742 return ERROR_INVALID_PARAMETER;
2744 /* Check for value existence and correctness of its type, allocate a buffer and load it. */
2745 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, NULL, &cbData);
2746 if (result != ERROR_SUCCESS) goto cleanup;
2747 if (!(dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ) || !cbData) {
2748 result = ERROR_FILE_NOT_FOUND;
2749 goto cleanup;
2751 pwszTempBuffer = heap_alloc(cbData);
2752 if (!pwszTempBuffer) {
2753 result = ERROR_NOT_ENOUGH_MEMORY;
2754 goto cleanup;
2756 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, (LPBYTE)pwszTempBuffer, &cbData);
2757 if (result != ERROR_SUCCESS) goto cleanup;
2759 /* '@' is the prefix for resource based string entries. */
2760 if (*pwszTempBuffer != '@') {
2761 result = ERROR_INVALID_DATA;
2762 goto cleanup;
2765 /* Expand environment variables regardless of the type. */
2766 cbData = ExpandEnvironmentStringsW(pwszTempBuffer, NULL, 0) * sizeof(WCHAR);
2767 if (!cbData) goto cleanup;
2768 pwszExpandedBuffer = heap_alloc(cbData);
2769 if (!pwszExpandedBuffer) {
2770 result = ERROR_NOT_ENOUGH_MEMORY;
2771 goto cleanup;
2773 ExpandEnvironmentStringsW(pwszTempBuffer, pwszExpandedBuffer, cbData / sizeof(WCHAR));
2775 /* Parse the value and load the string. */
2777 WCHAR *pComma = wcsrchr(pwszExpandedBuffer, ','), *pNewBuffer;
2778 const WCHAR backslashW[] = {'\\',0};
2779 UINT uiStringId;
2780 DWORD baseDirLen;
2781 int reqChars;
2783 /* Format of the expanded value is 'path_to_dll,-resId' */
2784 if (!pComma || pComma[1] != '-') {
2785 result = ERROR_INVALID_DATA;
2786 goto cleanup;
2789 uiStringId = wcstol(pComma+2, NULL, 10);
2790 *pComma = '\0';
2792 /* Build a resource dll path. */
2793 baseDirLen = pwszBaseDir ? lstrlenW(pwszBaseDir) : 0;
2794 cbData = (baseDirLen + 1 + lstrlenW(pwszExpandedBuffer + 1) + 1) * sizeof(WCHAR);
2795 pNewBuffer = heap_realloc(pwszTempBuffer, cbData);
2796 if (!pNewBuffer) {
2797 result = ERROR_NOT_ENOUGH_MEMORY;
2798 goto cleanup;
2800 pwszTempBuffer = pNewBuffer;
2801 pwszTempBuffer[0] = '\0';
2802 if (baseDirLen) {
2803 lstrcpyW(pwszTempBuffer, pwszBaseDir);
2804 if (pwszBaseDir[baseDirLen - 1] != '\\')
2805 lstrcatW(pwszTempBuffer, backslashW);
2807 lstrcatW(pwszTempBuffer, pwszExpandedBuffer + 1);
2809 /* Load specified string from the file */
2810 reqChars = 0;
2811 result = load_mui_string(pwszTempBuffer, uiStringId, pwszBuffer, cbBuffer/sizeof(WCHAR), &reqChars, dwFlags);
2812 if (pcbData && (result == ERROR_SUCCESS || result == ERROR_MORE_DATA))
2813 *pcbData = reqChars * sizeof(WCHAR);
2816 cleanup:
2817 heap_free(pwszTempBuffer);
2818 heap_free(pwszExpandedBuffer);
2819 return result;
2822 /******************************************************************************
2823 * RegLoadMUIStringA (kernelbase.@)
2825 * Not implemented on native.
2827 LSTATUS WINAPI RegLoadMUIStringA(HKEY hKey, LPCSTR pszValue, LPSTR pszBuffer, DWORD cbBuffer,
2828 LPDWORD pcbData, DWORD dwFlags, LPCSTR pszBaseDir)
2830 return ERROR_CALL_NOT_IMPLEMENTED;
2834 /******************************************************************************
2835 * RegDeleteTreeW (kernelbase.@)
2838 LSTATUS WINAPI RegDeleteTreeW( HKEY hkey, const WCHAR *subkey )
2840 DWORD name_size, max_name, max_subkey;
2841 WCHAR *name_buf = NULL;
2842 LONG ret;
2844 TRACE( "(%p, %s)\n", hkey, debugstr_w(subkey) );
2846 if (subkey && *subkey)
2848 ret = RegOpenKeyExW( hkey, subkey, 0, KEY_READ, &hkey );
2849 if (ret) return ret;
2852 ret = RegQueryInfoKeyW( hkey, NULL, NULL, NULL, NULL, &max_subkey,
2853 NULL, NULL, &max_name, NULL, NULL, NULL );
2854 if (ret)
2855 goto cleanup;
2857 max_name = max( max_subkey, max_name ) + 1;
2858 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
2860 ret = ERROR_NOT_ENOUGH_MEMORY;
2861 goto cleanup;
2864 /* Recursively delete subkeys */
2865 for (;;)
2867 name_size = max_name;
2868 ret = RegEnumKeyExW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2869 if (ret == ERROR_NO_MORE_ITEMS) break;
2870 if (ret) goto cleanup;
2871 ret = RegDeleteTreeW( hkey, name_buf );
2872 if (ret) goto cleanup;
2875 /* Delete the key itself */
2876 if (subkey && *subkey)
2878 ret = RegDeleteKeyExW( hkey, L"", 0, 0 );
2879 goto cleanup;
2882 /* Delete values */
2883 for (;;)
2885 name_size = max_name;
2886 ret = RegEnumValueW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2887 if (ret == ERROR_NO_MORE_ITEMS) break;
2888 if (ret) goto cleanup;
2889 ret = RegDeleteValueW( hkey, name_buf );
2890 if (ret) goto cleanup;
2893 ret = ERROR_SUCCESS;
2895 cleanup:
2896 heap_free( name_buf );
2897 if (subkey && *subkey)
2898 RegCloseKey( hkey );
2899 return ret;
2903 /******************************************************************************
2904 * RegDeleteTreeA (kernelbase.@)
2907 LSTATUS WINAPI RegDeleteTreeA( HKEY hkey, const char *subkey )
2909 UNICODE_STRING subkeyW;
2910 LONG ret;
2912 if (subkey) RtlCreateUnicodeStringFromAsciiz( &subkeyW, subkey );
2913 else subkeyW.Buffer = NULL;
2914 ret = RegDeleteTreeW( hkey, subkeyW.Buffer );
2915 RtlFreeUnicodeString( &subkeyW );
2916 return ret;
2920 /******************************************************************************
2921 * RegCopyTreeW (kernelbase.@)
2924 LSTATUS WINAPI RegCopyTreeW( HKEY hsrc, const WCHAR *subkey, HKEY hdst )
2926 DWORD name_size, max_name;
2927 DWORD value_size, max_value;
2928 DWORD max_subkey, i, type;
2929 WCHAR *name_buf = NULL;
2930 BYTE *value_buf = NULL;
2931 HKEY hkey;
2932 LONG ret;
2934 TRACE( "(%p, %s, %p)\n", hsrc, debugstr_w(subkey), hdst );
2936 if (subkey)
2938 ret = RegOpenKeyExW( hsrc, subkey, 0, KEY_READ, &hsrc );
2939 if (ret) return ret;
2942 ret = RegQueryInfoKeyW( hsrc, NULL, NULL, NULL, NULL, &max_subkey,
2943 NULL, NULL, &max_name, &max_value, NULL, NULL );
2944 if (ret)
2945 goto cleanup;
2947 max_name = max( max_subkey, max_name ) + 1;
2948 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
2950 ret = ERROR_NOT_ENOUGH_MEMORY;
2951 goto cleanup;
2954 if (!(value_buf = heap_alloc( max_value )))
2956 ret = ERROR_NOT_ENOUGH_MEMORY;
2957 goto cleanup;
2960 /* Copy values */
2961 for (i = 0;; i++)
2963 name_size = max_name;
2964 value_size = max_value;
2965 ret = RegEnumValueW( hsrc, i, name_buf, &name_size, NULL, &type, value_buf, &value_size );
2966 if (ret == ERROR_NO_MORE_ITEMS) break;
2967 if (ret) goto cleanup;
2968 ret = RegSetValueExW( hdst, name_buf, 0, type, value_buf, value_size );
2969 if (ret) goto cleanup;
2972 /* Recursively copy subkeys */
2973 for (i = 0;; i++)
2975 name_size = max_name;
2976 ret = RegEnumKeyExW( hsrc, i, name_buf, &name_size, NULL, NULL, NULL, NULL );
2977 if (ret == ERROR_NO_MORE_ITEMS) break;
2978 if (ret) goto cleanup;
2979 ret = RegCreateKeyExW( hdst, name_buf, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL );
2980 if (ret) goto cleanup;
2981 ret = RegCopyTreeW( hsrc, name_buf, hkey );
2982 RegCloseKey( hkey );
2983 if (ret) goto cleanup;
2986 ret = ERROR_SUCCESS;
2988 cleanup:
2989 heap_free( name_buf );
2990 heap_free( value_buf );
2991 if (subkey)
2992 RegCloseKey( hsrc );
2993 return ret;
2997 /******************************************************************************
2998 * RegLoadAppKeyA (kernelbase.@)
3001 LSTATUS WINAPI RegLoadAppKeyA(const char *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3003 FIXME("%s %p %u %u %u: stub\n", wine_dbgstr_a(file), result, sam, options, reserved);
3005 if (!file || reserved)
3006 return ERROR_INVALID_PARAMETER;
3008 *result = (HKEY)0xdeadbeef;
3009 return ERROR_SUCCESS;
3012 /******************************************************************************
3013 * RegLoadAppKeyW (kernelbase.@)
3016 LSTATUS WINAPI RegLoadAppKeyW(const WCHAR *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3018 FIXME("%s %p %u %u %u: stub\n", wine_dbgstr_w(file), result, sam, options, reserved);
3020 if (!file || reserved)
3021 return ERROR_INVALID_PARAMETER;
3023 *result = (HKEY)0xdeadbeef;
3024 return ERROR_SUCCESS;
3028 /***********************************************************************
3029 * DnsHostnameToComputerNameExW (kernelbase.@)
3031 * FIXME: how is this different from the non-Ex function?
3033 BOOL WINAPI DECLSPEC_HOTPATCH DnsHostnameToComputerNameExW( const WCHAR *hostname, WCHAR *computername,
3034 DWORD *size )
3036 static const WCHAR allowed[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&')(-_{}";
3037 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3038 DWORD i, len;
3040 lstrcpynW( buffer, hostname, MAX_COMPUTERNAME_LENGTH + 1 );
3041 len = lstrlenW( buffer );
3042 if (*size < len + 1)
3044 *size = len;
3045 SetLastError( ERROR_MORE_DATA );
3046 return FALSE;
3048 *size = len;
3049 if (!computername) return FALSE;
3050 for (i = 0; i < len; i++)
3052 if (buffer[i] >= 'a' && buffer[i] <= 'z') computername[i] = buffer[i] + 'A' - 'a';
3053 else computername[i] = wcschr( allowed, buffer[i] ) ? buffer[i] : '_';
3055 computername[len] = 0;
3056 return TRUE;
3060 /***********************************************************************
3061 * GetComputerNameExA (kernelbase.@)
3063 BOOL WINAPI GetComputerNameExA( COMPUTER_NAME_FORMAT type, char *name, DWORD *len )
3065 BOOL ret = FALSE;
3066 DWORD lenA, lenW = 0;
3067 WCHAR *buffer;
3069 GetComputerNameExW( type, NULL, &lenW );
3070 if (GetLastError() != ERROR_MORE_DATA) return FALSE;
3072 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
3074 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3075 return FALSE;
3077 if (GetComputerNameExW( type, buffer, &lenW ))
3079 lenA = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
3080 if (lenA > *len)
3082 *len = lenA;
3083 SetLastError( ERROR_MORE_DATA );
3085 else
3087 WideCharToMultiByte( CP_ACP, 0, buffer, -1, name, *len, NULL, NULL );
3088 *len = lenA - 1;
3089 ret = TRUE;
3092 HeapFree( GetProcessHeap(), 0, buffer );
3093 return ret;
3097 /***********************************************************************
3098 * GetComputerNameExW (kernelbase.@)
3100 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, WCHAR *name, DWORD *len )
3102 const WCHAR *keyname, *valuename;
3103 LRESULT ret;
3104 HKEY key;
3106 switch (type)
3108 case ComputerNameNetBIOS:
3109 case ComputerNamePhysicalNetBIOS:
3110 keyname = L"System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName";
3111 valuename = L"ComputerName";
3112 break;
3113 case ComputerNameDnsHostname:
3114 case ComputerNamePhysicalDnsHostname:
3115 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3116 valuename = L"Hostname";
3117 break;
3118 case ComputerNameDnsDomain:
3119 case ComputerNamePhysicalDnsDomain:
3120 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3121 valuename = L"Domain";
3122 break;
3123 case ComputerNameDnsFullyQualified:
3124 case ComputerNamePhysicalDnsFullyQualified:
3126 WCHAR *domain, buffer[256];
3127 DWORD size = ARRAY_SIZE(buffer) - 1;
3129 if (!GetComputerNameExW( ComputerNameDnsHostname, buffer, &size )) return FALSE;
3130 domain = buffer + lstrlenW(buffer);
3131 *domain++ = '.';
3132 size = ARRAY_SIZE(buffer) - (domain - buffer);
3133 if (!GetComputerNameExW( ComputerNameDnsDomain, domain, &size )) return FALSE;
3134 if (!*domain) domain[-1] = 0;
3135 size = lstrlenW(buffer);
3136 if (name && size < *len)
3138 if (name) lstrcpyW( name, buffer );
3139 *len = size;
3140 return TRUE;
3142 *len = size + 1;
3143 SetLastError( ERROR_MORE_DATA );
3144 return FALSE;
3146 default:
3147 SetLastError( ERROR_INVALID_PARAMETER );
3148 return FALSE;
3151 if (!(ret = RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &key )))
3153 DWORD size = *len * sizeof(WCHAR);
3154 ret = RegQueryValueExW( key, valuename, NULL, NULL, (BYTE *)name, &size );
3155 if (!name) ret = ERROR_MORE_DATA;
3156 else if (!ret) size -= sizeof(WCHAR);
3157 *len = size / sizeof(WCHAR);
3158 RegCloseKey( key );
3160 TRACE("-> %lu %s\n", ret, debugstr_w(name) );
3161 if (ret) SetLastError( ret );
3162 return !ret;
3166 /***********************************************************************
3167 * SetComputerNameA (kernelbase.@)
3169 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameA( const char *name )
3171 BOOL ret;
3172 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3173 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3175 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3176 ret = SetComputerNameExW( ComputerNamePhysicalNetBIOS, nameW );
3177 HeapFree( GetProcessHeap(), 0, nameW );
3178 return ret;
3182 /***********************************************************************
3183 * SetComputerNameW (kernelbase.@)
3185 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameW( const WCHAR *name )
3187 return SetComputerNameExW( ComputerNamePhysicalNetBIOS, name );
3191 /***********************************************************************
3192 * SetComputerNameExA (kernelbase.@)
3194 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExA( COMPUTER_NAME_FORMAT type, const char *name )
3196 BOOL ret;
3197 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3198 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3200 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3201 ret = SetComputerNameExW( type, nameW );
3202 HeapFree( GetProcessHeap(), 0, nameW );
3203 return ret;
3207 /***********************************************************************
3208 * SetComputerNameExW (kernelbase.@)
3210 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExW( COMPUTER_NAME_FORMAT type, const WCHAR *name )
3212 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3213 DWORD size;
3214 HKEY key;
3215 LRESULT ret;
3217 TRACE( "%u %s\n", type, debugstr_w( name ));
3219 switch (type)
3221 case ComputerNameDnsHostname:
3222 case ComputerNamePhysicalDnsHostname:
3223 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3224 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3225 if (ret) break;
3226 ret = RegSetValueExW( key, L"Hostname", 0, REG_SZ,
3227 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3228 RegCloseKey( key );
3229 /* fall through */
3231 case ComputerNameNetBIOS:
3232 case ComputerNamePhysicalNetBIOS:
3233 /* @@ Wine registry key: HKCU\Software\Wine\Network */
3234 if (!RegOpenKeyExW( HKEY_CURRENT_USER, L"Software\\Wine\\Network", 0, KEY_READ, &key ))
3236 BOOL use_dns = TRUE;
3237 size = sizeof(buffer);
3238 if (!RegQueryValueExW( key, L"UseDnsComputerName", NULL, NULL, (BYTE *)buffer, &size ))
3239 use_dns = IS_OPTION_TRUE( buffer[0] );
3240 RegCloseKey( key );
3241 if (!use_dns)
3243 ret = ERROR_ACCESS_DENIED;
3244 break;
3247 size = ARRAY_SIZE( buffer );
3248 if (!DnsHostnameToComputerNameExW( name, buffer, &size )) return FALSE;
3249 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
3250 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3251 if (ret) break;
3252 ret = RegSetValueExW( key, L"ComputerName", 0, REG_SZ,
3253 (BYTE *)buffer, (lstrlenW(buffer) + 1) * sizeof(WCHAR) );
3254 RegCloseKey( key );
3255 break;
3257 case ComputerNameDnsDomain:
3258 case ComputerNamePhysicalDnsDomain:
3259 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3260 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3261 if (ret) break;
3262 ret = RegSetValueExW( key, L"Domain", 0, REG_SZ,
3263 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3264 RegCloseKey( key );
3265 break;
3266 default:
3267 ret = ERROR_INVALID_PARAMETER;
3268 break;
3270 if (ret) SetLastError( ret );
3271 return !ret;
3274 struct USKEY
3276 HKEY HKCUstart; /* Start key in CU hive */
3277 HKEY HKCUkey; /* Opened key in CU hive */
3278 HKEY HKLMstart; /* Start key in LM hive */
3279 HKEY HKLMkey; /* Opened key in LM hive */
3280 WCHAR path[MAX_PATH];
3283 LONG WINAPI SHRegCreateUSKeyA(LPCSTR path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3285 WCHAR *pathW;
3286 LONG ret;
3288 TRACE("%s, %#x, %p, %p, %#x\n", debugstr_a(path), samDesired, relative_key, new_uskey, flags);
3290 if (path)
3292 INT len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
3293 pathW = heap_alloc(len * sizeof(WCHAR));
3294 if (!pathW)
3295 return ERROR_NOT_ENOUGH_MEMORY;
3296 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
3298 else
3299 pathW = NULL;
3301 ret = SHRegCreateUSKeyW(pathW, samDesired, relative_key, new_uskey, flags);
3302 HeapFree(GetProcessHeap(), 0, pathW);
3303 return ret;
3306 static HKEY reg_duplicate_hkey(HKEY hKey)
3308 HKEY newKey = 0;
3310 RegOpenKeyExW(hKey, 0, 0, MAXIMUM_ALLOWED, &newKey);
3311 return newKey;
3314 static HKEY reg_get_hkey_from_huskey(HUSKEY hUSKey, BOOL is_hkcu)
3316 struct USKEY *mihk = hUSKey;
3317 HKEY test = hUSKey;
3319 if (test == HKEY_CLASSES_ROOT
3320 || test == HKEY_CURRENT_CONFIG
3321 || test == HKEY_CURRENT_USER
3322 || test == HKEY_DYN_DATA
3323 || test == HKEY_LOCAL_MACHINE
3324 || test == HKEY_PERFORMANCE_DATA
3325 || test == HKEY_USERS)
3326 /* FIXME: need to define for Win2k, ME, XP
3327 * (test == HKEY_PERFORMANCE_TEXT) ||
3328 * (test == HKEY_PERFORMANCE_NLSTEXT) ||
3331 return test;
3334 return is_hkcu ? mihk->HKCUkey : mihk->HKLMkey;
3337 LONG WINAPI SHRegCreateUSKeyW(const WCHAR *path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3339 LONG ret = ERROR_CALL_NOT_IMPLEMENTED;
3340 struct USKEY *ret_key;
3342 TRACE("%s, %#x, %p, %p, %#x\n", debugstr_w(path), samDesired, relative_key, new_uskey, flags);
3344 if (!new_uskey)
3345 return ERROR_INVALID_PARAMETER;
3347 *new_uskey = NULL;
3349 if (flags & ~SHREGSET_FORCE_HKCU)
3351 FIXME("unsupported flags 0x%08x\n", flags);
3352 return ERROR_SUCCESS;
3355 ret_key = heap_alloc_zero(sizeof(*ret_key));
3356 lstrcpynW(ret_key->path, path, ARRAY_SIZE(ret_key->path));
3358 if (relative_key)
3360 ret_key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3361 ret_key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3363 else
3365 ret_key->HKCUstart = HKEY_CURRENT_USER;
3366 ret_key->HKLMstart = HKEY_LOCAL_MACHINE;
3369 if (flags & SHREGSET_FORCE_HKCU)
3371 ret = RegCreateKeyExW(ret_key->HKCUstart, path, 0, NULL, 0, samDesired, NULL, &ret_key->HKCUkey, NULL);
3372 if (ret == ERROR_SUCCESS)
3373 *new_uskey = ret_key;
3374 else
3375 heap_free(ret_key);
3378 return ret;
3381 LONG WINAPI SHRegCloseUSKey(HUSKEY hUSKey)
3383 struct USKEY *key = hUSKey;
3384 LONG ret = ERROR_SUCCESS;
3386 if (!key)
3387 return ERROR_INVALID_PARAMETER;
3389 if (key->HKCUkey)
3390 ret = RegCloseKey(key->HKCUkey);
3391 if (key->HKCUstart && key->HKCUstart != HKEY_CURRENT_USER)
3392 ret = RegCloseKey(key->HKCUstart);
3393 if (key->HKLMkey)
3394 ret = RegCloseKey(key->HKLMkey);
3395 if (key->HKLMstart && key->HKLMstart != HKEY_LOCAL_MACHINE)
3396 ret = RegCloseKey(key->HKLMstart);
3398 heap_free(key);
3399 return ret;
3402 LONG WINAPI SHRegDeleteEmptyUSKeyA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3404 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3405 return ERROR_SUCCESS;
3408 LONG WINAPI SHRegDeleteEmptyUSKeyW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3410 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3411 return ERROR_SUCCESS;
3414 LONG WINAPI SHRegDeleteUSValueA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3416 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3417 return ERROR_SUCCESS;
3420 LONG WINAPI SHRegDeleteUSValueW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3422 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3423 return ERROR_SUCCESS;
3426 LONG WINAPI SHRegEnumUSValueA(HUSKEY hUSKey, DWORD index, char *value_name, DWORD *value_name_len, DWORD *type,
3427 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3429 HKEY dokey;
3431 TRACE("%p, %#x, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3433 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3434 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3436 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3437 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3439 FIXME("no support for SHREGENUM_BOTH\n");
3440 return ERROR_INVALID_FUNCTION;
3443 LONG WINAPI SHRegEnumUSValueW(HUSKEY hUSKey, DWORD index, WCHAR *value_name, DWORD *value_name_len, DWORD *type,
3444 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3446 HKEY dokey;
3448 TRACE("%p, %#x, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3450 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3451 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3453 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3454 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3456 FIXME("no support for SHREGENUM_BOTH\n");
3457 return ERROR_INVALID_FUNCTION;
3460 LONG WINAPI SHRegEnumUSKeyA(HUSKEY hUSKey, DWORD index, char *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3462 HKEY dokey;
3464 TRACE("%p, %d, %p, %p(%d), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3466 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3467 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3469 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3470 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3472 FIXME("no support for SHREGENUM_BOTH\n");
3473 return ERROR_INVALID_FUNCTION;
3476 LONG WINAPI SHRegEnumUSKeyW(HUSKEY hUSKey, DWORD index, WCHAR *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3478 HKEY dokey;
3480 TRACE("%p, %d, %p, %p(%d), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3482 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3483 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3485 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3486 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3488 FIXME("no support for SHREGENUM_BOTH\n");
3489 return ERROR_INVALID_FUNCTION;
3492 LONG WINAPI SHRegOpenUSKeyA(const char *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3494 WCHAR pathW[MAX_PATH];
3496 if (path)
3497 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, ARRAY_SIZE(pathW));
3499 return SHRegOpenUSKeyW(path ? pathW : NULL, access_mask, relative_key, uskey, ignore_hkcu);
3502 LONG WINAPI SHRegOpenUSKeyW(const WCHAR *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3504 LONG ret2, ret1 = ~ERROR_SUCCESS;
3505 struct USKEY *key;
3507 TRACE("%s, %#x, %p, %p, %d\n", debugstr_w(path), access_mask, relative_key, uskey, ignore_hkcu);
3509 if (uskey)
3510 *uskey = NULL;
3512 /* Create internal HUSKEY */
3513 key = heap_alloc_zero(sizeof(*key));
3514 lstrcpynW(key->path, path, ARRAY_SIZE(key->path));
3516 if (relative_key)
3518 key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3519 key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3521 /* FIXME: if either of these keys is NULL, create the start key from
3522 * the relative keys start+path
3525 else
3527 key->HKCUstart = HKEY_CURRENT_USER;
3528 key->HKLMstart = HKEY_LOCAL_MACHINE;
3531 if (!ignore_hkcu)
3533 ret1 = RegOpenKeyExW(key->HKCUstart, key->path, 0, access_mask, &key->HKCUkey);
3534 if (ret1)
3535 key->HKCUkey = 0;
3538 ret2 = RegOpenKeyExW(key->HKLMstart, key->path, 0, access_mask, &key->HKLMkey);
3539 if (ret2)
3540 key->HKLMkey = 0;
3542 if (ret1 || ret2)
3543 TRACE("one or more opens failed: HKCU=%d HKLM=%d\n", ret1, ret2);
3545 if (ret1 && ret2)
3547 /* Neither open succeeded: fail */
3548 SHRegCloseUSKey(key);
3549 return ret2;
3552 TRACE("HUSKEY=%p\n", key);
3553 if (uskey)
3554 *uskey = key;
3556 return ERROR_SUCCESS;
3559 LONG WINAPI SHRegWriteUSValueA(HUSKEY hUSKey, const char *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3561 WCHAR valueW[MAX_PATH];
3563 if (value)
3564 MultiByteToWideChar(CP_ACP, 0, value, -1, valueW, ARRAY_SIZE(valueW));
3566 return SHRegWriteUSValueW(hUSKey, value ? valueW : NULL, type, data, data_len, flags);
3569 LONG WINAPI SHRegWriteUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3571 struct USKEY *hKey = hUSKey;
3572 LONG ret = ERROR_SUCCESS;
3573 DWORD dummy;
3575 TRACE("%p, %s, %d, %p, %d, %#x\n", hUSKey, debugstr_w(value), type, data, data_len, flags);
3577 __TRY
3579 dummy = hKey->HKCUkey || hKey->HKLMkey;
3581 __EXCEPT_PAGE_FAULT
3583 return ERROR_INVALID_PARAMETER;
3585 __ENDTRY
3586 if (!(flags & (SHREGSET_FORCE_HKCU|SHREGSET_FORCE_HKLM))) return ERROR_INVALID_PARAMETER;
3588 if (flags & (SHREGSET_FORCE_HKCU | SHREGSET_HKCU))
3590 if (!hKey->HKCUkey)
3592 /* Create the key */
3593 ret = RegCreateKeyExW(hKey->HKCUstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3594 MAXIMUM_ALLOWED, NULL, &hKey->HKCUkey, NULL);
3595 TRACE("Creating HKCU key, ret = %d\n", ret);
3596 if (ret && (flags & SHREGSET_FORCE_HKCU))
3598 hKey->HKCUkey = 0;
3599 return ret;
3603 if (!ret)
3605 if ((flags & SHREGSET_FORCE_HKCU) || RegQueryValueExW(hKey->HKCUkey, value, NULL, NULL, NULL, &dummy))
3607 /* Doesn't exist or we are forcing: Write value */
3608 ret = RegSetValueExW(hKey->HKCUkey, value, 0, type, data, data_len);
3609 TRACE("Writing HKCU value, ret = %d\n", ret);
3614 if (flags & (SHREGSET_FORCE_HKLM | SHREGSET_HKLM))
3616 if (!hKey->HKLMkey)
3618 /* Create the key */
3619 ret = RegCreateKeyExW(hKey->HKLMstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3620 MAXIMUM_ALLOWED, NULL, &hKey->HKLMkey, NULL);
3621 TRACE("Creating HKLM key, ret = %d\n", ret);
3622 if (ret && (flags & (SHREGSET_FORCE_HKLM)))
3624 hKey->HKLMkey = 0;
3625 return ret;
3629 if (!ret)
3631 if ((flags & SHREGSET_FORCE_HKLM) || RegQueryValueExW(hKey->HKLMkey, value, NULL, NULL, NULL, &dummy))
3633 /* Doesn't exist or we are forcing: Write value */
3634 ret = RegSetValueExW(hKey->HKLMkey, value, 0, type, data, data_len);
3635 TRACE("Writing HKLM value, ret = %d\n", ret);
3640 return ret;
3643 LONG WINAPI SHRegSetUSValueA(const char *subkey, const char *value, DWORD type, void *data, DWORD data_len,
3644 DWORD flags)
3646 BOOL ignore_hkcu;
3647 HUSKEY hkey;
3648 LONG ret;
3650 TRACE("%s, %s, %d, %p, %d, %#x\n", debugstr_a(subkey), debugstr_a(value), type, data, data_len, flags);
3652 if (!data)
3653 return ERROR_INVALID_FUNCTION;
3655 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3657 ret = SHRegOpenUSKeyA(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3658 if (ret == ERROR_SUCCESS)
3660 ret = SHRegWriteUSValueA(hkey, value, type, data, data_len, flags);
3661 SHRegCloseUSKey(hkey);
3664 return ret;
3667 LONG WINAPI SHRegSetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD type, void *data, DWORD data_len,
3668 DWORD flags)
3670 BOOL ignore_hkcu;
3671 HUSKEY hkey;
3672 LONG ret;
3674 TRACE("%s, %s, %d, %p, %d, %#x\n", debugstr_w(subkey), debugstr_w(value), type, data, data_len, flags);
3676 if (!data)
3677 return ERROR_INVALID_FUNCTION;
3679 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3681 ret = SHRegOpenUSKeyW(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3682 if (ret == ERROR_SUCCESS)
3684 ret = SHRegWriteUSValueW(hkey, value, type, data, data_len, flags);
3685 SHRegCloseUSKey(hkey);
3688 return ret;
3691 LONG WINAPI SHRegQueryInfoUSKeyA(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3692 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3694 HKEY dokey;
3695 LONG ret;
3697 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3699 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3701 ret = RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3702 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3703 return ret;
3706 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3708 return RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3711 return ERROR_INVALID_FUNCTION;
3714 LONG WINAPI SHRegQueryInfoUSKeyW(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3715 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3717 HKEY dokey;
3718 LONG ret;
3720 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3722 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3724 ret = RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3725 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3726 return ret;
3729 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3731 return RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3734 return ERROR_INVALID_FUNCTION;
3737 LONG WINAPI SHRegQueryUSValueA(HUSKEY hUSKey, const char *value, DWORD *type, void *data, DWORD *data_len,
3738 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3740 LONG ret = ~ERROR_SUCCESS;
3741 DWORD move_len;
3742 HKEY dokey;
3744 /* If user wants HKCU, and it exists, then try it */
3745 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3747 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3748 TRACE("HKCU RegQueryValue returned %d\n", ret);
3751 /* If HKCU did not work and HKLM exists, then try it */
3752 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3754 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3755 TRACE("HKLM RegQueryValue returned %d\n", ret);
3758 /* If neither worked, and default data exists, then use it */
3759 if (ret != ERROR_SUCCESS)
3761 if (default_data && default_data_len)
3763 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3764 memmove(data, default_data, move_len);
3765 *data_len = move_len;
3766 TRACE("setting default data\n");
3767 ret = ERROR_SUCCESS;
3771 return ret;
3774 LONG WINAPI SHRegQueryUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3775 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3777 LONG ret = ~ERROR_SUCCESS;
3778 DWORD move_len;
3779 HKEY dokey;
3781 /* If user wants HKCU, and it exists, then try it */
3782 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3784 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3785 TRACE("HKCU RegQueryValue returned %d\n", ret);
3788 /* If HKCU did not work and HKLM exists, then try it */
3789 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3791 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3792 TRACE("HKLM RegQueryValue returned %d\n", ret);
3795 /* If neither worked, and default data exists, then use it */
3796 if (ret != ERROR_SUCCESS)
3798 if (default_data && default_data_len)
3800 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3801 memmove(data, default_data, move_len);
3802 *data_len = move_len;
3803 TRACE("setting default data\n");
3804 ret = ERROR_SUCCESS;
3808 return ret;
3811 LONG WINAPI SHRegGetUSValueA(const char *subkey, const char *value, DWORD *type, void *data, DWORD *data_len,
3812 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3814 HUSKEY myhuskey;
3815 LONG ret;
3817 if (!data || !data_len)
3818 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3820 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), *data_len);
3822 ret = SHRegOpenUSKeyA(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3823 if (!ret)
3825 ret = SHRegQueryUSValueA(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3826 SHRegCloseUSKey(myhuskey);
3829 return ret;
3832 LONG WINAPI SHRegGetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3833 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3835 HUSKEY myhuskey;
3836 LONG ret;
3838 if (!data || !data_len)
3839 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3841 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), *data_len);
3843 ret = SHRegOpenUSKeyW(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3844 if (!ret)
3846 ret = SHRegQueryUSValueW(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3847 SHRegCloseUSKey(myhuskey);
3850 return ret;
3853 BOOL WINAPI SHRegGetBoolUSValueA(const char *subkey, const char *value, BOOL ignore_hkcu, BOOL default_value)
3855 BOOL ret = default_value;
3856 DWORD type, datalen;
3857 char data[10];
3859 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), ignore_hkcu);
3861 datalen = ARRAY_SIZE(data) - 1;
3862 if (!SHRegGetUSValueA(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3864 switch (type)
3866 case REG_SZ:
3867 data[9] = '\0';
3868 if (!lstrcmpiA(data, "YES") || !lstrcmpiA(data, "TRUE"))
3869 ret = TRUE;
3870 else if (!lstrcmpiA(data, "NO") || !lstrcmpiA(data, "FALSE"))
3871 ret = FALSE;
3872 break;
3873 case REG_DWORD:
3874 ret = *(DWORD *)data != 0;
3875 break;
3876 case REG_BINARY:
3877 if (datalen == 1)
3879 ret = !!data[0];
3880 break;
3882 default:
3883 FIXME("Unsupported registry data type %d\n", type);
3884 ret = FALSE;
3886 TRACE("got value (type=%d), returning %d\n", type, ret);
3888 else
3889 TRACE("returning default value %d\n", ret);
3891 return ret;
3894 BOOL WINAPI SHRegGetBoolUSValueW(const WCHAR *subkey, const WCHAR *value, BOOL ignore_hkcu, BOOL default_value)
3896 BOOL ret = default_value;
3897 DWORD type, datalen;
3898 WCHAR data[10];
3900 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), ignore_hkcu);
3902 datalen = (ARRAY_SIZE(data) - 1) * sizeof(WCHAR);
3903 if (!SHRegGetUSValueW(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3905 switch (type)
3907 case REG_SZ:
3908 data[9] = '\0';
3909 if (!lstrcmpiW(data, L"yes") || !lstrcmpiW(data, L"true"))
3910 ret = TRUE;
3911 else if (!lstrcmpiW(data, L"no") || !lstrcmpiW(data, L"false"))
3912 ret = FALSE;
3913 break;
3914 case REG_DWORD:
3915 ret = *(DWORD *)data != 0;
3916 break;
3917 case REG_BINARY:
3918 if (datalen == 1)
3920 ret = !!data[0];
3921 break;
3923 default:
3924 FIXME("Unsupported registry data type %d\n", type);
3925 ret = FALSE;
3927 TRACE("got value (type=%d), returning %d\n", type, ret);
3929 else
3930 TRACE("returning default value %d\n", ret);
3932 return ret;