kernelbase: Implement reading performance counter help strings.
[wine.git] / dlls / kernelbase / registry.c
blobc3d5cdbc6e04bad992755d310d7ad12a85f5893b
1 /*
2 * Registry management
4 * Copyright 1996 Marcus Meissner
5 * Copyright 1998 Matthew Becker
6 * Copyright 1999 Sylvain St-Germain
7 * Copyright 1999 Alexandre Julliard
8 * Copyright 2017 Dmitry Timoshkov
9 * Copyright 2019 Nikolay Sivov for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "winerror.h"
36 #include "winternl.h"
37 #include "winperf.h"
38 #include "winuser.h"
39 #include "shlwapi.h"
40 #include "sddl.h"
42 #include "kernelbase.h"
43 #include "wine/debug.h"
44 #include "wine/exception.h"
45 #include "wine/heap.h"
46 #include "wine/list.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(reg);
50 #define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
51 #define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
53 static const WCHAR * const root_key_names[] =
55 L"\\Registry\\Machine\\Software\\Classes",
56 NULL, /* HKEY_CURRENT_USER is determined dynamically */
57 L"\\Registry\\Machine",
58 L"\\Registry\\User",
59 NULL, /* HKEY_PERFORMANCE_DATA is not a real key */
60 L"\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles\\Current",
61 L"\\Registry\\DynData"
64 static HKEY special_root_keys[ARRAY_SIZE(root_key_names)];
65 static BOOL cache_disabled[ARRAY_SIZE(root_key_names)];
67 static CRITICAL_SECTION reg_mui_cs;
68 static CRITICAL_SECTION_DEBUG reg_mui_cs_debug =
70 0, 0, &reg_mui_cs,
71 { &reg_mui_cs_debug.ProcessLocksList,
72 &reg_mui_cs_debug.ProcessLocksList },
73 0, 0, { (DWORD_PTR)(__FILE__ ": reg_mui_cs") }
75 static CRITICAL_SECTION reg_mui_cs = { &reg_mui_cs_debug, -1, 0, 0, 0, 0 };
76 struct mui_cache_entry {
77 struct list entry;
78 WCHAR *file_name; /* full path name */
79 DWORD index;
80 LCID locale;
81 WCHAR *text;
83 static struct list reg_mui_cache = LIST_INIT(reg_mui_cache); /* MRU */
84 static unsigned int reg_mui_cache_count;
85 #define REG_MUI_CACHE_SIZE 8
87 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
89 /* check if value type needs string conversion (Ansi<->Unicode) */
90 static inline BOOL is_string( DWORD type )
92 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
95 /* check if current version is NT or Win95 */
96 static inline BOOL is_version_nt(void)
98 return !(GetVersion() & 0x80000000);
101 static BOOL is_wow6432node( const UNICODE_STRING *name )
103 return (name->Length == 11 * sizeof(WCHAR) && !wcsnicmp( name->Buffer, L"Wow6432Node", 11 ));
106 /* open the Wow6432Node subkey of the specified key */
107 static HANDLE open_wow6432node( HANDLE key )
109 OBJECT_ATTRIBUTES attr;
110 UNICODE_STRING nameW;
111 HANDLE ret;
113 attr.Length = sizeof(attr);
114 attr.RootDirectory = key;
115 attr.ObjectName = &nameW;
116 attr.Attributes = 0;
117 attr.SecurityDescriptor = NULL;
118 attr.SecurityQualityOfService = NULL;
119 RtlInitUnicodeString( &nameW, L"Wow6432Node" );
120 if (NtOpenKeyEx( &ret, MAXIMUM_ALLOWED, &attr, 0 )) ret = 0;
121 return ret;
124 static HKEY get_perflib_key( HANDLE key )
126 static const WCHAR performance_text[] =
127 L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009";
128 char buffer[200];
129 OBJECT_NAME_INFORMATION *info = (OBJECT_NAME_INFORMATION *)buffer;
131 if (!NtQueryObject( key, ObjectNameInformation, buffer, sizeof(buffer), NULL ))
133 if (!wcsicmp( info->Name.Buffer, performance_text ))
135 NtClose( key );
136 return HKEY_PERFORMANCE_TEXT;
140 return key;
143 /* wrapper for NtCreateKey that creates the key recursively if necessary */
144 static NTSTATUS create_key( HKEY *retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
145 const UNICODE_STRING *class, ULONG options, PULONG dispos )
147 BOOL force_wow32 = is_win64 && (access & KEY_WOW64_32KEY);
148 NTSTATUS status = STATUS_OBJECT_NAME_NOT_FOUND;
149 HANDLE subkey, root = attr->RootDirectory;
151 if (!force_wow32) status = NtCreateKey( &subkey, access, attr, 0, class, options, dispos );
153 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
155 WCHAR *buffer = attr->ObjectName->Buffer;
156 DWORD attrs, pos = 0, i = 0, len = attr->ObjectName->Length / sizeof(WCHAR);
157 UNICODE_STRING str;
159 /* don't try to create registry root */
160 if (!attr->RootDirectory && len > 10 && !wcsnicmp( buffer, L"\\Registry\\", 10 )) i += 10;
162 while (i < len && buffer[i] != '\\') i++;
163 if (i == len && !force_wow32) return status;
165 attrs = attr->Attributes;
166 attr->ObjectName = &str;
168 for (;;)
170 str.Buffer = buffer + pos;
171 str.Length = (i - pos) * sizeof(WCHAR);
172 if (force_wow32 && pos)
174 if (is_wow6432node( &str )) force_wow32 = FALSE;
175 else if ((subkey = open_wow6432node( attr->RootDirectory )))
177 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
178 attr->RootDirectory = subkey;
179 force_wow32 = FALSE;
182 if (i == len)
184 attr->Attributes = attrs;
185 status = NtCreateKey( &subkey, access, attr, 0, class, options, dispos );
187 else
189 attr->Attributes = attrs & ~OBJ_OPENLINK;
190 status = NtCreateKey( &subkey, access, attr, 0, class,
191 options & ~REG_OPTION_CREATE_LINK, dispos );
193 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
194 if (!NT_SUCCESS(status)) return status;
195 if (i == len) break;
196 attr->RootDirectory = subkey;
197 while (i < len && buffer[i] == '\\') i++;
198 pos = i;
199 while (i < len && buffer[i] != '\\') i++;
202 attr->RootDirectory = subkey;
203 if (force_wow32 && (subkey = open_wow6432node( attr->RootDirectory )))
205 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
206 attr->RootDirectory = subkey;
208 if (status == STATUS_PREDEFINED_HANDLE)
210 attr->RootDirectory = get_perflib_key( attr->RootDirectory );
211 status = STATUS_SUCCESS;
213 *retkey = attr->RootDirectory;
214 return status;
217 /* wrapper for NtOpenKeyEx to handle Wow6432 nodes */
218 static NTSTATUS open_key( HKEY *retkey, DWORD options, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
220 NTSTATUS status;
221 BOOL force_wow32 = is_win64 && (access & KEY_WOW64_32KEY);
222 HANDLE subkey, root = attr->RootDirectory;
223 WCHAR *buffer = attr->ObjectName->Buffer;
224 DWORD pos = 0, i = 0, len = attr->ObjectName->Length / sizeof(WCHAR);
225 UNICODE_STRING str;
227 *retkey = NULL;
229 if (!force_wow32)
231 if (options & REG_OPTION_OPEN_LINK) attr->Attributes |= OBJ_OPENLINK;
232 status = NtOpenKeyEx( (HANDLE *)retkey, access, attr, options );
233 if (status == STATUS_PREDEFINED_HANDLE)
235 *retkey = get_perflib_key( *retkey );
236 status = STATUS_SUCCESS;
238 return status;
241 if (len && buffer[0] == '\\') return STATUS_OBJECT_PATH_INVALID;
242 while (i < len && buffer[i] != '\\') i++;
243 attr->ObjectName = &str;
245 for (;;)
247 str.Buffer = buffer + pos;
248 str.Length = (i - pos) * sizeof(WCHAR);
249 if (force_wow32 && pos)
251 if (is_wow6432node( &str )) force_wow32 = FALSE;
252 else if ((subkey = open_wow6432node( attr->RootDirectory )))
254 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
255 attr->RootDirectory = subkey;
256 force_wow32 = FALSE;
259 if (i == len)
261 if (options & REG_OPTION_OPEN_LINK) attr->Attributes |= OBJ_OPENLINK;
262 status = NtOpenKeyEx( &subkey, access, attr, options );
264 else
266 if (!(options & REG_OPTION_OPEN_LINK)) attr->Attributes &= ~OBJ_OPENLINK;
267 status = NtOpenKeyEx( &subkey, access, attr, options & ~REG_OPTION_OPEN_LINK );
269 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
270 if (status) return status;
271 attr->RootDirectory = subkey;
272 if (i == len) break;
273 while (i < len && buffer[i] == '\\') i++;
274 pos = i;
275 while (i < len && buffer[i] != '\\') i++;
277 if (force_wow32 && (subkey = open_wow6432node( attr->RootDirectory )))
279 if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
280 attr->RootDirectory = subkey;
282 if (status == STATUS_PREDEFINED_HANDLE)
284 attr->RootDirectory = get_perflib_key( attr->RootDirectory );
285 status = STATUS_SUCCESS;
287 *retkey = attr->RootDirectory;
288 return status;
291 /* create one of the HKEY_* special root keys */
292 static HKEY create_special_root_hkey( HKEY hkey, DWORD access )
294 HKEY ret = 0;
295 int idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
297 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CURRENT_USER))
299 if (RtlOpenCurrentUser( access, (HANDLE *)&hkey )) return 0;
300 TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
302 else
304 OBJECT_ATTRIBUTES attr;
305 UNICODE_STRING name;
307 attr.Length = sizeof(attr);
308 attr.RootDirectory = 0;
309 attr.ObjectName = &name;
310 attr.Attributes = 0;
311 attr.SecurityDescriptor = NULL;
312 attr.SecurityQualityOfService = NULL;
313 RtlInitUnicodeString( &name, root_key_names[idx] );
314 if (create_key( &hkey, access, &attr, NULL, 0, NULL )) return 0;
315 TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
318 if (!cache_disabled[idx] && !(access & (KEY_WOW64_64KEY | KEY_WOW64_32KEY)))
320 if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
321 ret = hkey;
322 else
323 NtClose( hkey ); /* somebody beat us to it */
325 else
326 ret = hkey;
327 return ret;
330 /* map the hkey from special root to normal key if necessary */
331 static inline HKEY get_special_root_hkey( HKEY hkey, REGSAM access )
333 unsigned int index = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
334 DWORD wow64_flags = access & (KEY_WOW64_32KEY | KEY_WOW64_64KEY);
336 switch (HandleToUlong(hkey))
338 case (LONG)(LONG_PTR)HKEY_CLASSES_ROOT:
339 if (wow64_flags)
340 return create_special_root_hkey( hkey, MAXIMUM_ALLOWED | wow64_flags );
341 /* fall through */
343 case (LONG)(LONG_PTR)HKEY_CURRENT_USER:
344 case (LONG)(LONG_PTR)HKEY_LOCAL_MACHINE:
345 case (LONG)(LONG_PTR)HKEY_USERS:
346 case (LONG)(LONG_PTR)HKEY_CURRENT_CONFIG:
347 case (LONG)(LONG_PTR)HKEY_DYN_DATA:
348 if (special_root_keys[index])
349 return special_root_keys[index];
350 return create_special_root_hkey( hkey, MAXIMUM_ALLOWED );
352 default:
353 return hkey;
358 /******************************************************************************
359 * RemapPredefinedHandleInternal (kernelbase.@)
361 NTSTATUS WINAPI RemapPredefinedHandleInternal( HKEY hkey, HKEY override )
363 HKEY old_key;
364 int idx;
366 TRACE("(%p %p)\n", hkey, override);
368 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
369 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
370 return STATUS_INVALID_HANDLE;
371 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
373 if (override)
375 NTSTATUS status = NtDuplicateObject( GetCurrentProcess(), override,
376 GetCurrentProcess(), (HANDLE *)&override,
377 0, 0, DUPLICATE_SAME_ACCESS );
378 if (status) return status;
381 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], override );
382 if (old_key) NtClose( old_key );
383 return STATUS_SUCCESS;
387 /******************************************************************************
388 * DisablePredefinedHandleTableInternal (kernelbase.@)
390 NTSTATUS WINAPI DisablePredefinedHandleTableInternal( HKEY hkey )
392 HKEY old_key;
393 int idx;
395 TRACE("(%p)\n", hkey);
397 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
398 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
399 return STATUS_INVALID_HANDLE;
400 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
402 cache_disabled[idx] = TRUE;
404 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], NULL );
405 if (old_key) NtClose( old_key );
406 return STATUS_SUCCESS;
410 /******************************************************************************
411 * RegCreateKeyExW (kernelbase.@)
413 * See RegCreateKeyExA.
415 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
416 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
417 PHKEY retkey, LPDWORD dispos )
419 OBJECT_ATTRIBUTES attr;
420 UNICODE_STRING nameW, classW;
422 if (reserved) return ERROR_INVALID_PARAMETER;
423 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
425 attr.Length = sizeof(attr);
426 attr.RootDirectory = hkey;
427 attr.ObjectName = &nameW;
428 attr.Attributes = 0;
429 attr.SecurityDescriptor = NULL;
430 attr.SecurityQualityOfService = NULL;
431 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
432 RtlInitUnicodeString( &nameW, name );
433 RtlInitUnicodeString( &classW, class );
435 return RtlNtStatusToDosError( create_key( retkey, access, &attr, &classW, options, dispos ) );
439 /******************************************************************************
440 * RegCreateKeyExA (kernelbase.@)
442 * Open a registry key, creating it if it doesn't exist.
444 * PARAMS
445 * hkey [I] Handle of the parent registry key
446 * name [I] Name of the new key to open or create
447 * reserved [I] Reserved, pass 0
448 * class [I] The object type of the new key
449 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
450 * access [I] Access level desired
451 * sa [I] Security attributes for the key
452 * retkey [O] Destination for the resulting handle
453 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
455 * RETURNS
456 * Success: ERROR_SUCCESS.
457 * Failure: A standard Win32 error code. retkey remains untouched.
459 * FIXME
460 * MAXIMUM_ALLOWED in access mask not supported by server
462 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
463 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
464 PHKEY retkey, LPDWORD dispos )
466 OBJECT_ATTRIBUTES attr;
467 UNICODE_STRING classW;
468 ANSI_STRING nameA, classA;
469 NTSTATUS status;
471 if (reserved) return ERROR_INVALID_PARAMETER;
472 if (!is_version_nt())
474 access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
475 if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
477 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
479 attr.Length = sizeof(attr);
480 attr.RootDirectory = hkey;
481 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
482 attr.Attributes = 0;
483 attr.SecurityDescriptor = NULL;
484 attr.SecurityQualityOfService = NULL;
485 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
486 RtlInitAnsiString( &nameA, name );
487 RtlInitAnsiString( &classA, class );
489 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
490 &nameA, FALSE )))
492 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
494 status = create_key( retkey, access, &attr, &classW, options, dispos );
495 RtlFreeUnicodeString( &classW );
498 return RtlNtStatusToDosError( status );
502 /******************************************************************************
503 * RegOpenKeyExW (kernelbase.@)
505 * See RegOpenKeyExA.
507 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD options, REGSAM access, PHKEY retkey )
509 OBJECT_ATTRIBUTES attr;
510 UNICODE_STRING nameW;
512 if (retkey && (!name || !name[0]) &&
513 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
514 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
516 *retkey = hkey;
517 return ERROR_SUCCESS;
520 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
521 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
523 if (!retkey) return ERROR_INVALID_PARAMETER;
524 *retkey = NULL;
525 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
527 attr.Length = sizeof(attr);
528 attr.RootDirectory = hkey;
529 attr.ObjectName = &nameW;
530 attr.Attributes = 0;
531 attr.SecurityDescriptor = NULL;
532 attr.SecurityQualityOfService = NULL;
533 RtlInitUnicodeString( &nameW, name );
534 return RtlNtStatusToDosError( open_key( retkey, options, access, &attr ) );
538 /******************************************************************************
539 * RegOpenKeyExA (kernelbase.@)
541 * Open a registry key.
543 * PARAMS
544 * hkey [I] Handle of open key
545 * name [I] Name of subkey to open
546 * options [I] Open options (can be set to REG_OPTION_OPEN_LINK)
547 * access [I] Security access mask
548 * retkey [O] Handle to open key
550 * RETURNS
551 * Success: ERROR_SUCCESS
552 * Failure: A standard Win32 error code. retkey is set to 0.
554 * NOTES
555 * Unlike RegCreateKeyExA(), this function will not create the key if it
556 * does not exist.
558 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD options, REGSAM access, PHKEY retkey )
560 OBJECT_ATTRIBUTES attr;
561 STRING nameA;
562 NTSTATUS status;
564 if (retkey && (!name || !name[0]) &&
565 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
566 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
568 *retkey = hkey;
569 return ERROR_SUCCESS;
572 if (!is_version_nt()) access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
573 else
575 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
576 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
579 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
581 attr.Length = sizeof(attr);
582 attr.RootDirectory = hkey;
583 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
584 attr.Attributes = 0;
585 attr.SecurityDescriptor = NULL;
586 attr.SecurityQualityOfService = NULL;
588 RtlInitAnsiString( &nameA, name );
589 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
590 &nameA, FALSE )))
592 status = open_key( retkey, options, access, &attr );
594 return RtlNtStatusToDosError( status );
598 /******************************************************************************
599 * RegOpenCurrentUser (kernelbase.@)
601 * Get a handle to the HKEY_CURRENT_USER key for the user
602 * the current thread is impersonating.
604 * PARAMS
605 * access [I] Desired access rights to the key
606 * retkey [O] Handle to the opened key
608 * RETURNS
609 * Success: ERROR_SUCCESS
610 * Failure: nonzero error code from Winerror.h
612 * FIXME
613 * This function is supposed to retrieve a handle to the
614 * HKEY_CURRENT_USER for the user the current thread is impersonating.
615 * Since Wine does not currently allow threads to impersonate other users,
616 * this stub should work fine.
618 LSTATUS WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
620 void *data[20];
621 TOKEN_USER *info = (TOKEN_USER *)data;
622 HANDLE token;
623 DWORD len = 0;
625 /* get current user SID */
626 if (OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, FALSE, &token ))
628 len = sizeof(data);
629 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
630 CloseHandle( token );
632 if (!len)
634 ImpersonateSelf(SecurityIdentification);
635 if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &token))
637 len = sizeof(data);
638 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
639 CloseHandle( token );
641 RevertToSelf();
644 if (len)
646 WCHAR buffer[200];
647 UNICODE_STRING string = { 0, sizeof(buffer), buffer };
649 RtlConvertSidToUnicodeString( &string, info->User.Sid, FALSE );
650 return RegOpenKeyExW( HKEY_USERS, string.Buffer, 0, access, retkey );
653 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
658 /******************************************************************************
659 * RegEnumKeyExW (kernelbase.@)
661 * Enumerate subkeys of the specified open registry key.
663 * PARAMS
664 * hkey [I] Handle to key to enumerate
665 * index [I] Index of subkey to enumerate
666 * name [O] Buffer for subkey name
667 * name_len [O] Size of subkey buffer
668 * reserved [I] Reserved
669 * class [O] Buffer for class string
670 * class_len [O] Size of class buffer
671 * ft [O] Time key last written to
673 * RETURNS
674 * Success: ERROR_SUCCESS
675 * Failure: System error code. If there are no more subkeys available, the
676 * function returns ERROR_NO_MORE_ITEMS.
678 LSTATUS WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
679 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
681 NTSTATUS status;
682 char buffer[256], *buf_ptr = buffer;
683 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
684 DWORD total_size;
686 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
687 name_len ? *name_len : 0, reserved, class, class_len, ft );
689 if (reserved) return ERROR_INVALID_PARAMETER;
690 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
692 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
693 buffer, sizeof(buffer), &total_size );
695 while (status == STATUS_BUFFER_OVERFLOW)
697 /* retry with a dynamically allocated buffer */
698 if (buf_ptr != buffer) heap_free( buf_ptr );
699 if (!(buf_ptr = heap_alloc( total_size )))
700 return ERROR_NOT_ENOUGH_MEMORY;
701 info = (KEY_NODE_INFORMATION *)buf_ptr;
702 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
703 buf_ptr, total_size, &total_size );
706 if (!status)
708 DWORD len = info->NameLength / sizeof(WCHAR);
709 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
711 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
713 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
714 status = STATUS_BUFFER_OVERFLOW;
715 else
717 *name_len = len;
718 memcpy( name, info->Name, info->NameLength );
719 name[len] = 0;
720 if (class_len)
722 *class_len = cls_len;
723 if (class)
725 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
726 class[cls_len] = 0;
732 if (buf_ptr != buffer) heap_free( buf_ptr );
733 return RtlNtStatusToDosError( status );
737 /******************************************************************************
738 * RegEnumKeyExA (kernelbase.@)
740 * See RegEnumKeyExW.
742 LSTATUS WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
743 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
745 NTSTATUS status;
746 char buffer[256], *buf_ptr = buffer;
747 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
748 DWORD total_size;
750 TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
751 name_len ? *name_len : 0, reserved, class, class_len, ft );
753 if (reserved) return ERROR_INVALID_PARAMETER;
754 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
756 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
757 buffer, sizeof(buffer), &total_size );
759 while (status == STATUS_BUFFER_OVERFLOW)
761 /* retry with a dynamically allocated buffer */
762 if (buf_ptr != buffer) heap_free( buf_ptr );
763 if (!(buf_ptr = heap_alloc( total_size )))
764 return ERROR_NOT_ENOUGH_MEMORY;
765 info = (KEY_NODE_INFORMATION *)buf_ptr;
766 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
767 buf_ptr, total_size, &total_size );
770 if (!status)
772 DWORD len, cls_len;
774 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
775 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
776 info->ClassLength );
777 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
779 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
780 status = STATUS_BUFFER_OVERFLOW;
781 else
783 *name_len = len;
784 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
785 name[len] = 0;
786 if (class_len)
788 *class_len = cls_len;
789 if (class)
791 RtlUnicodeToMultiByteN( class, cls_len, NULL,
792 (WCHAR *)(buf_ptr + info->ClassOffset),
793 info->ClassLength );
794 class[cls_len] = 0;
800 if (buf_ptr != buffer) heap_free( buf_ptr );
801 return RtlNtStatusToDosError( status );
805 /******************************************************************************
806 * RegQueryInfoKeyW (kernelbase.@)
808 * Retrieves information about the specified registry key.
810 * PARAMS
811 * hkey [I] Handle to key to query
812 * class [O] Buffer for class string
813 * class_len [O] Size of class string buffer
814 * reserved [I] Reserved
815 * subkeys [O] Buffer for number of subkeys
816 * max_subkey [O] Buffer for longest subkey name length
817 * max_class [O] Buffer for longest class string length
818 * values [O] Buffer for number of value entries
819 * max_value [O] Buffer for longest value name length
820 * max_data [O] Buffer for longest value data length
821 * security [O] Buffer for security descriptor length
822 * modif [O] Modification time
824 * RETURNS
825 * Success: ERROR_SUCCESS
826 * Failure: system error code.
828 * NOTES
829 * - win95 allows class to be valid and class_len to be NULL
830 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
831 * - both allow class to be NULL and class_len to be NULL
832 * (it's hard to test validity, so test !NULL instead)
834 LSTATUS WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
835 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
836 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
837 LPDWORD security, FILETIME *modif )
839 NTSTATUS status;
840 char buffer[256], *buf_ptr = buffer;
841 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
842 DWORD total_size;
844 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
845 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
847 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
848 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
850 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
851 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
853 if (class && class_len && *class_len)
855 /* retry with a dynamically allocated buffer */
856 while (status == STATUS_BUFFER_OVERFLOW)
858 if (buf_ptr != buffer) heap_free( buf_ptr );
859 if (!(buf_ptr = heap_alloc( total_size )))
860 return ERROR_NOT_ENOUGH_MEMORY;
861 info = (KEY_FULL_INFORMATION *)buf_ptr;
862 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
865 if (status) goto done;
867 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
869 status = STATUS_BUFFER_TOO_SMALL;
871 else
873 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
874 class[info->ClassLength/sizeof(WCHAR)] = 0;
877 else status = STATUS_SUCCESS;
879 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
880 if (subkeys) *subkeys = info->SubKeys;
881 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
882 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
883 if (values) *values = info->Values;
884 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
885 if (max_data) *max_data = info->MaxValueDataLen;
886 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
888 if (security)
890 FIXME( "security argument not supported.\n");
891 *security = 0;
894 done:
895 if (buf_ptr != buffer) heap_free( buf_ptr );
896 return RtlNtStatusToDosError( status );
900 /******************************************************************************
901 * RegQueryInfoKeyA (kernelbase.@)
903 * Retrieves information about a registry key.
905 * PARAMS
906 * hKey [I] Handle to an open key.
907 * lpClass [O] Class string of the key.
908 * lpcClass [I/O] size of lpClass.
909 * lpReserved [I] Reserved; must be NULL.
910 * lpcSubKeys [O] Number of subkeys contained by the key.
911 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
912 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
913 * class in TCHARS.
914 * lpcValues [O] Number of values associated with the key.
915 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
916 * lpcMaxValueLen [O] Longest data component among the key's values
917 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
918 * lpftLastWriteTime [O] FILETIME structure that is the last write time.
920 * RETURNS
921 * Success: ERROR_SUCCESS
922 * Failure: nonzero error code from Winerror.h
924 LSTATUS WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
925 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
926 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
927 LPDWORD security, FILETIME *modif )
929 NTSTATUS status;
930 char buffer[256], *buf_ptr = buffer;
931 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
932 DWORD total_size;
934 TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
935 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
937 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
938 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
940 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
941 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
943 if (class || class_len)
945 /* retry with a dynamically allocated buffer */
946 while (status == STATUS_BUFFER_OVERFLOW)
948 if (buf_ptr != buffer) heap_free( buf_ptr );
949 if (!(buf_ptr = heap_alloc( total_size )))
950 return ERROR_NOT_ENOUGH_MEMORY;
951 info = (KEY_FULL_INFORMATION *)buf_ptr;
952 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
955 if (status) goto done;
957 if (class && class_len && *class_len)
959 DWORD len = *class_len;
960 RtlUnicodeToMultiByteN( class, len, class_len,
961 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
962 if (*class_len == len)
964 status = STATUS_BUFFER_OVERFLOW;
965 *class_len -= 1;
967 class[*class_len] = 0;
969 else if (class_len)
970 RtlUnicodeToMultiByteSize( class_len,
971 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
973 else status = STATUS_SUCCESS;
975 if (subkeys) *subkeys = info->SubKeys;
976 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
977 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
978 if (values) *values = info->Values;
979 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
980 if (max_data) *max_data = info->MaxValueDataLen;
981 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
983 if (security)
985 FIXME( "security argument not supported.\n");
986 *security = 0;
989 done:
990 if (buf_ptr != buffer) heap_free( buf_ptr );
991 return RtlNtStatusToDosError( status );
994 /******************************************************************************
995 * RegCloseKey (kernelbase.@)
997 * Close an open registry key.
999 * PARAMS
1000 * hkey [I] Handle of key to close
1002 * RETURNS
1003 * Success: ERROR_SUCCESS
1004 * Failure: Error code
1006 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCloseKey( HKEY hkey )
1008 if (!hkey) return ERROR_INVALID_HANDLE;
1009 if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
1010 return RtlNtStatusToDosError( NtClose( hkey ) );
1014 /******************************************************************************
1015 * RegDeleteKeyExW (kernelbase.@)
1017 LSTATUS WINAPI RegDeleteKeyExW( HKEY hkey, LPCWSTR name, REGSAM access, DWORD reserved )
1019 DWORD ret;
1020 HKEY tmp;
1022 if (!name) return ERROR_INVALID_PARAMETER;
1024 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
1026 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1027 if (!(ret = RegOpenKeyExW( hkey, name, 0, access | DELETE, &tmp )))
1029 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1030 RegCloseKey( tmp );
1032 TRACE("%s ret=%08x\n", debugstr_w(name), ret);
1033 return ret;
1037 /******************************************************************************
1038 * RegDeleteKeyExA (kernelbase.@)
1040 LSTATUS WINAPI RegDeleteKeyExA( HKEY hkey, LPCSTR name, REGSAM access, DWORD reserved )
1042 DWORD ret;
1043 HKEY tmp;
1045 if (!name) return ERROR_INVALID_PARAMETER;
1047 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
1049 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1050 if (!(ret = RegOpenKeyExA( hkey, name, 0, access | DELETE, &tmp )))
1052 if (!is_version_nt()) /* win95 does recursive key deletes */
1054 CHAR sub[MAX_PATH];
1055 DWORD len = sizeof(sub);
1056 while(!RegEnumKeyExA(tmp, 0, sub, &len, NULL, NULL, NULL, NULL))
1058 if(RegDeleteKeyExA(tmp, sub, access, reserved)) /* recurse */
1059 break;
1062 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1063 RegCloseKey( tmp );
1065 TRACE("%s ret=%08x\n", debugstr_a(name), ret);
1066 return ret;
1069 /******************************************************************************
1070 * RegSetValueExW (kernelbase.@)
1072 * Set the data and contents of a registry value.
1074 * PARAMS
1075 * hkey [I] Handle of key to set value for
1076 * name [I] Name of value to set
1077 * reserved [I] Reserved, must be zero
1078 * type [I] Type of the value being set
1079 * data [I] The new contents of the value to set
1080 * count [I] Size of data
1082 * RETURNS
1083 * Success: ERROR_SUCCESS
1084 * Failure: Error code
1086 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
1087 DWORD type, const BYTE *data, DWORD count )
1089 UNICODE_STRING nameW;
1091 /* no need for version check, not implemented on win9x anyway */
1093 if ((data && ((ULONG_PTR)data >> 16) == 0) || (!data && count)) return ERROR_NOACCESS;
1095 if (count && is_string(type))
1097 LPCWSTR str = (LPCWSTR)data;
1098 /* if user forgot to count terminating null, add it (yes NT does this) */
1099 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
1100 count += sizeof(WCHAR);
1102 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1104 RtlInitUnicodeString( &nameW, name );
1105 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
1109 /******************************************************************************
1110 * RegSetValueExA (kernelbase.@)
1112 * See RegSetValueExW.
1114 * NOTES
1115 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1116 * NT does definitely care (aj)
1118 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1119 const BYTE *data, DWORD count )
1121 ANSI_STRING nameA;
1122 UNICODE_STRING nameW;
1123 WCHAR *dataW = NULL;
1124 NTSTATUS status;
1126 if (!is_version_nt()) /* win95 */
1128 if (type == REG_SZ)
1130 if (!data) return ERROR_INVALID_PARAMETER;
1131 count = strlen((const char *)data) + 1;
1134 else if (count && is_string(type))
1136 /* if user forgot to count terminating null, add it (yes NT does this) */
1137 if (data[count-1] && !data[count]) count++;
1140 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1142 if (is_string( type )) /* need to convert to Unicode */
1144 DWORD lenW;
1145 RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1146 if (!(dataW = heap_alloc( lenW ))) return ERROR_OUTOFMEMORY;
1147 RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1148 count = lenW;
1149 data = (BYTE *)dataW;
1152 RtlInitAnsiString( &nameA, name );
1153 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1155 status = NtSetValueKey( hkey, &nameW, 0, type, data, count );
1156 RtlFreeUnicodeString( &nameW );
1158 heap_free( dataW );
1159 return RtlNtStatusToDosError( status );
1163 /******************************************************************************
1164 * RegSetKeyValueW (kernelbase.@)
1166 LONG WINAPI RegSetKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name, DWORD type, const void *data, DWORD len )
1168 HKEY hsubkey = NULL;
1169 DWORD ret;
1171 TRACE("(%p,%s,%s,%d,%p,%d)\n", hkey, debugstr_w(subkey), debugstr_w(name), type, data, len );
1173 if (subkey && subkey[0]) /* need to create the subkey */
1175 if ((ret = RegCreateKeyExW( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1176 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1177 hkey = hsubkey;
1180 ret = RegSetValueExW( hkey, name, 0, type, (const BYTE*)data, len );
1181 if (hsubkey) RegCloseKey( hsubkey );
1182 return ret;
1185 /******************************************************************************
1186 * RegSetKeyValueA (kernelbase.@)
1188 LONG WINAPI RegSetKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name, DWORD type, const void *data, DWORD len )
1190 HKEY hsubkey = NULL;
1191 DWORD ret;
1193 TRACE("(%p,%s,%s,%d,%p,%d)\n", hkey, debugstr_a(subkey), debugstr_a(name), type, data, len );
1195 if (subkey && subkey[0]) /* need to create the subkey */
1197 if ((ret = RegCreateKeyExA( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1198 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1199 hkey = hsubkey;
1202 ret = RegSetValueExA( hkey, name, 0, type, (const BYTE*)data, len );
1203 if (hsubkey) RegCloseKey( hsubkey );
1204 return ret;
1207 /* FIXME: we should read data from system32/perf009c.dat (or perf###c depending
1208 * on locale) instead */
1209 static DWORD query_perf_names( DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1211 static const WCHAR names[] = L"1\0" "1847\0" "1846\0End Marker\0";
1212 DWORD size = *ret_size;
1214 if (type) *type = REG_MULTI_SZ;
1215 *ret_size = sizeof(names);
1216 if (!unicode) *ret_size /= sizeof(WCHAR);
1218 if (!data) return ERROR_SUCCESS;
1219 if (size < *ret_size) return ERROR_MORE_DATA;
1221 if (unicode)
1222 memcpy( data, names, sizeof(names) );
1223 else
1224 RtlUnicodeToMultiByteN( data, size, NULL, names, sizeof(names) );
1225 return ERROR_SUCCESS;
1228 /* FIXME: we should read data from system32/perf009h.dat (or perf###h depending
1229 * on locale) instead */
1230 static DWORD query_perf_help( DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1232 static const WCHAR names[] = L"1847\0End Marker\0";
1233 DWORD size = *ret_size;
1235 if (type) *type = REG_MULTI_SZ;
1236 *ret_size = sizeof(names);
1237 if (!unicode) *ret_size /= sizeof(WCHAR);
1239 if (!data) return ERROR_SUCCESS;
1240 if (size < *ret_size) return ERROR_MORE_DATA;
1242 if (unicode)
1243 memcpy( data, names, sizeof(names) );
1244 else
1245 RtlUnicodeToMultiByteN( data, size, NULL, names, sizeof(names) );
1246 return ERROR_SUCCESS;
1249 struct perf_provider
1251 HMODULE perflib;
1252 WCHAR linkage[MAX_PATH];
1253 WCHAR objects[MAX_PATH];
1254 PM_OPEN_PROC *pOpen;
1255 PM_CLOSE_PROC *pClose;
1256 PM_COLLECT_PROC *pCollect;
1259 static void *get_provider_entry(HKEY perf, HMODULE perflib, const char *name)
1261 char buf[MAX_PATH];
1262 DWORD err, type, len;
1264 len = sizeof(buf) - 1;
1265 err = RegQueryValueExA(perf, name, NULL, &type, (BYTE *)buf, &len);
1266 if (err != ERROR_SUCCESS || type != REG_SZ)
1267 return NULL;
1269 buf[len] = 0;
1270 TRACE("Loading function pointer for %s: %s\n", name, debugstr_a(buf));
1272 return GetProcAddress(perflib, buf);
1275 static BOOL load_provider(HKEY root, const WCHAR *name, struct perf_provider *provider)
1277 WCHAR buf[MAX_PATH], buf2[MAX_PATH];
1278 DWORD err, type, len;
1279 HKEY service, perf;
1281 err = RegOpenKeyExW(root, name, 0, KEY_READ, &service);
1282 if (err != ERROR_SUCCESS)
1283 return FALSE;
1285 provider->linkage[0] = 0;
1286 err = RegOpenKeyExW(service, L"Linkage", 0, KEY_READ, &perf);
1287 if (err == ERROR_SUCCESS)
1289 len = sizeof(buf) - sizeof(WCHAR);
1290 err = RegQueryValueExW(perf, L"Export", NULL, &type, (BYTE *)buf, &len);
1291 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1293 memcpy(provider->linkage, buf, len);
1294 provider->linkage[len / sizeof(WCHAR)] = 0;
1295 TRACE("Export: %s\n", debugstr_w(provider->linkage));
1297 RegCloseKey(perf);
1300 err = RegOpenKeyExW(service, L"Performance", 0, KEY_READ, &perf);
1301 RegCloseKey(service);
1302 if (err != ERROR_SUCCESS)
1303 return FALSE;
1305 provider->objects[0] = 0;
1306 len = sizeof(buf) - sizeof(WCHAR);
1307 err = RegQueryValueExW(perf, L"Object List", NULL, &type, (BYTE *)buf, &len);
1308 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1310 memcpy(provider->objects, buf, len);
1311 provider->objects[len / sizeof(WCHAR)] = 0;
1312 TRACE("Object List: %s\n", debugstr_w(provider->objects));
1315 len = sizeof(buf) - sizeof(WCHAR);
1316 err = RegQueryValueExW(perf, L"Library", NULL, &type, (BYTE *)buf, &len);
1317 if (err != ERROR_SUCCESS || !(type == REG_SZ || type == REG_EXPAND_SZ))
1318 goto error;
1320 buf[len / sizeof(WCHAR)] = 0;
1321 if (type == REG_EXPAND_SZ)
1323 len = ExpandEnvironmentStringsW(buf, buf2, MAX_PATH);
1324 if (!len || len > MAX_PATH) goto error;
1325 lstrcpyW(buf, buf2);
1328 if (!(provider->perflib = LoadLibraryW(buf)))
1330 WARN("Failed to load %s\n", debugstr_w(buf));
1331 goto error;
1334 GetModuleFileNameW(provider->perflib, buf, MAX_PATH);
1335 TRACE("Loaded provider %s\n", wine_dbgstr_w(buf));
1337 provider->pOpen = get_provider_entry(perf, provider->perflib, "Open");
1338 provider->pClose = get_provider_entry(perf, provider->perflib, "Close");
1339 provider->pCollect = get_provider_entry(perf, provider->perflib, "Collect");
1340 if (provider->pOpen && provider->pClose && provider->pCollect)
1342 RegCloseKey(perf);
1343 return TRUE;
1346 TRACE("Provider is missing required exports\n");
1347 FreeLibrary(provider->perflib);
1349 error:
1350 RegCloseKey(perf);
1351 return FALSE;
1354 static DWORD collect_data(struct perf_provider *provider, const WCHAR *query, void **data, DWORD *size, DWORD *obj_count)
1356 WCHAR *linkage = provider->linkage[0] ? provider->linkage : NULL;
1357 DWORD err;
1359 if (!query || !query[0])
1360 query = L"Global";
1362 err = provider->pOpen(linkage);
1363 if (err != ERROR_SUCCESS)
1365 TRACE("Open(%s) error %u (%#x)\n", debugstr_w(linkage), err, err);
1366 return err;
1369 *obj_count = 0;
1370 err = provider->pCollect((WCHAR *)query, data, size, obj_count);
1371 if (err != ERROR_SUCCESS)
1373 TRACE("Collect error %u (%#x)\n", err, err);
1374 *obj_count = 0;
1377 provider->pClose();
1378 return err;
1381 #define MAX_SERVICE_NAME 260
1383 static DWORD query_perf_data( const WCHAR *query, DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1385 DWORD err, i, data_size;
1386 HKEY root;
1387 PERF_DATA_BLOCK *pdb;
1389 if (!ret_size)
1390 return ERROR_INVALID_PARAMETER;
1392 if (!wcsnicmp( query, L"counter", 7 ))
1393 return query_perf_names( type, data, ret_size, unicode );
1394 if (!wcsnicmp( query, L"help", 4 ))
1395 return query_perf_help( type, data, ret_size, unicode );
1397 data_size = *ret_size;
1398 *ret_size = 0;
1400 if (type)
1401 *type = REG_BINARY;
1403 if (!data || data_size < sizeof(*pdb))
1404 return ERROR_MORE_DATA;
1406 pdb = data;
1408 pdb->Signature[0] = 'P';
1409 pdb->Signature[1] = 'E';
1410 pdb->Signature[2] = 'R';
1411 pdb->Signature[3] = 'F';
1412 #ifdef WORDS_BIGENDIAN
1413 pdb->LittleEndian = FALSE;
1414 #else
1415 pdb->LittleEndian = TRUE;
1416 #endif
1417 pdb->Version = PERF_DATA_VERSION;
1418 pdb->Revision = PERF_DATA_REVISION;
1419 pdb->TotalByteLength = 0;
1420 pdb->HeaderLength = sizeof(*pdb);
1421 pdb->NumObjectTypes = 0;
1422 pdb->DefaultObject = 0;
1423 NtQueryPerformanceCounter( &pdb->PerfTime, &pdb->PerfFreq );
1425 data = pdb + 1;
1426 pdb->SystemNameOffset = sizeof(*pdb);
1427 pdb->SystemNameLength = (data_size - sizeof(*pdb)) / sizeof(WCHAR);
1428 if (!GetComputerNameExW(ComputerNameNetBIOS, data, &pdb->SystemNameLength))
1429 return ERROR_MORE_DATA;
1431 pdb->SystemNameLength++;
1432 pdb->SystemNameLength *= sizeof(WCHAR);
1434 pdb->HeaderLength += pdb->SystemNameLength;
1436 /* align to 8 bytes */
1437 if (pdb->SystemNameLength & 7)
1438 pdb->HeaderLength += 8 - (pdb->SystemNameLength & 7);
1440 if (data_size < pdb->HeaderLength)
1441 return ERROR_MORE_DATA;
1443 pdb->TotalByteLength = pdb->HeaderLength;
1445 data_size -= pdb->HeaderLength;
1446 data = (char *)data + pdb->HeaderLength;
1448 err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services", 0, KEY_READ, &root);
1449 if (err != ERROR_SUCCESS)
1450 return err;
1452 i = 0;
1453 for (;;)
1455 DWORD collected_size = data_size, obj_count = 0;
1456 struct perf_provider provider;
1457 WCHAR name[MAX_SERVICE_NAME];
1458 DWORD len = ARRAY_SIZE( name );
1459 void *collected_data = data;
1461 err = RegEnumKeyExW(root, i++, name, &len, NULL, NULL, NULL, NULL);
1462 if (err == ERROR_NO_MORE_ITEMS)
1464 err = ERROR_SUCCESS;
1465 break;
1468 if (err != ERROR_SUCCESS)
1469 continue;
1471 if (!load_provider(root, name, &provider))
1472 continue;
1474 err = collect_data(&provider, query, &collected_data, &collected_size, &obj_count);
1475 FreeLibrary(provider.perflib);
1477 if (err == ERROR_MORE_DATA)
1478 break;
1480 if (err == ERROR_SUCCESS)
1482 PERF_OBJECT_TYPE *obj = (PERF_OBJECT_TYPE *)data;
1484 TRACE("Collect: obj->TotalByteLength %u, collected_size %u\n",
1485 obj->TotalByteLength, collected_size);
1487 data_size -= collected_size;
1488 data = collected_data;
1490 pdb->TotalByteLength += collected_size;
1491 pdb->NumObjectTypes += obj_count;
1495 RegCloseKey(root);
1497 if (err == ERROR_SUCCESS)
1499 *ret_size = pdb->TotalByteLength;
1501 GetSystemTime(&pdb->SystemTime);
1502 GetSystemTimeAsFileTime((FILETIME *)&pdb->PerfTime100nSec);
1505 return err;
1508 /******************************************************************************
1509 * RegQueryValueExW (kernelbase.@)
1511 * See RegQueryValueExA.
1513 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1514 LPBYTE data, LPDWORD count )
1516 NTSTATUS status;
1517 UNICODE_STRING name_str;
1518 DWORD total_size;
1519 char buffer[256], *buf_ptr = buffer;
1520 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1521 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1523 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1524 hkey, debugstr_w(name), reserved, type, data, count,
1525 (count && data) ? *count : 0 );
1527 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1529 if (hkey == HKEY_PERFORMANCE_DATA)
1530 return query_perf_data( name, type, data, count, TRUE );
1532 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1534 RtlInitUnicodeString( &name_str, name );
1536 if (data) total_size = min( sizeof(buffer), *count + info_size );
1537 else
1539 total_size = info_size;
1540 if (count) *count = 0;
1543 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1544 buffer, total_size, &total_size );
1545 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1547 if (data)
1549 /* retry with a dynamically allocated buffer */
1550 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1552 if (buf_ptr != buffer) heap_free( buf_ptr );
1553 if (!(buf_ptr = heap_alloc( total_size )))
1554 return ERROR_NOT_ENOUGH_MEMORY;
1555 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1556 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1557 buf_ptr, total_size, &total_size );
1560 if (!status)
1562 memcpy( data, buf_ptr + info_size, total_size - info_size );
1563 /* if the type is REG_SZ and data is not 0-terminated
1564 * and there is enough space in the buffer NT appends a \0 */
1565 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1567 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1568 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1571 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1573 else status = STATUS_SUCCESS;
1575 if (type) *type = info->Type;
1576 if (count) *count = total_size - info_size;
1578 done:
1579 if (buf_ptr != buffer) heap_free( buf_ptr );
1580 return RtlNtStatusToDosError(status);
1584 /******************************************************************************
1585 * RegQueryValueExA (kernelbase.@)
1587 * Get the type and contents of a specified value under with a key.
1589 * PARAMS
1590 * hkey [I] Handle of the key to query
1591 * name [I] Name of value under hkey to query
1592 * reserved [I] Reserved - must be NULL
1593 * type [O] Destination for the value type, or NULL if not required
1594 * data [O] Destination for the values contents, or NULL if not required
1595 * count [I/O] Size of data, updated with the number of bytes returned
1597 * RETURNS
1598 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1599 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1600 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1601 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1603 * NOTES
1604 * MSDN states that if data is too small it is partially filled. In reality
1605 * it remains untouched.
1607 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved,
1608 LPDWORD type, LPBYTE data, LPDWORD count )
1610 NTSTATUS status;
1611 ANSI_STRING nameA;
1612 UNICODE_STRING nameW;
1613 DWORD total_size, datalen = 0;
1614 char buffer[256], *buf_ptr = buffer;
1615 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1616 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1618 TRACE("(%p,%s,%p,%p,%p,%p=%d)\n",
1619 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1621 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1622 if (!(hkey = get_special_root_hkey( hkey, 0 )))
1623 return ERROR_INVALID_HANDLE;
1625 if (count) datalen = *count;
1626 if (!data && count) *count = 0;
1628 /* this matches Win9x behaviour - NT sets *type to a random value */
1629 if (type) *type = REG_NONE;
1631 RtlInitAnsiString( &nameA, name );
1632 if ((status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1633 return RtlNtStatusToDosError(status);
1635 if (hkey == HKEY_PERFORMANCE_DATA)
1637 DWORD ret = query_perf_data( nameW.Buffer, type, data, count, FALSE );
1638 RtlFreeUnicodeString( &nameW );
1639 return ret;
1642 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1643 buffer, sizeof(buffer), &total_size );
1644 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1646 /* we need to fetch the contents for a string type even if not requested,
1647 * because we need to compute the length of the ASCII string. */
1648 if (data || is_string(info->Type))
1650 /* retry with a dynamically allocated buffer */
1651 while (status == STATUS_BUFFER_OVERFLOW)
1653 if (buf_ptr != buffer) heap_free( buf_ptr );
1654 if (!(buf_ptr = heap_alloc( total_size )))
1656 status = STATUS_NO_MEMORY;
1657 goto done;
1659 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1660 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1661 buf_ptr, total_size, &total_size );
1664 if (status) goto done;
1666 if (is_string(info->Type))
1668 DWORD len;
1670 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1671 total_size - info_size );
1672 if (data && len)
1674 if (len > datalen) status = STATUS_BUFFER_OVERFLOW;
1675 else
1677 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1678 total_size - info_size );
1679 /* if the type is REG_SZ and data is not 0-terminated
1680 * and there is enough space in the buffer NT appends a \0 */
1681 if (len < datalen && data[len-1]) data[len] = 0;
1684 total_size = len + info_size;
1686 else if (data)
1688 if (total_size - info_size > datalen) status = STATUS_BUFFER_OVERFLOW;
1689 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1692 else status = STATUS_SUCCESS;
1694 if (type) *type = info->Type;
1695 if (count) *count = total_size - info_size;
1697 done:
1698 if (buf_ptr != buffer) heap_free( buf_ptr );
1699 RtlFreeUnicodeString( &nameW );
1700 return RtlNtStatusToDosError(status);
1704 /******************************************************************************
1705 * apply_restrictions [internal]
1707 * Helper function for RegGetValueA/W.
1709 static void apply_restrictions( DWORD dwFlags, DWORD dwType, DWORD cbData, PLONG ret )
1711 /* Check if the type is restricted by the passed flags */
1712 if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1714 DWORD dwMask = 0;
1716 switch (dwType)
1718 case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1719 case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1720 case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1721 case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1722 case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1723 case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1724 case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1727 if (dwFlags & dwMask)
1729 /* Type is not restricted, check for size mismatch */
1730 if (dwType == REG_BINARY)
1732 DWORD cbExpect = 0;
1734 if ((dwFlags & RRF_RT_ANY) == RRF_RT_DWORD)
1735 cbExpect = 4;
1736 else if ((dwFlags & RRF_RT_ANY) == RRF_RT_QWORD)
1737 cbExpect = 8;
1739 if (cbExpect && cbData != cbExpect)
1740 *ret = ERROR_DATATYPE_MISMATCH;
1743 else *ret = ERROR_UNSUPPORTED_TYPE;
1748 /******************************************************************************
1749 * RegGetValueW (kernelbase.@)
1751 * Retrieves the type and data for a value name associated with a key,
1752 * optionally expanding its content and restricting its type.
1754 * PARAMS
1755 * hKey [I] Handle to an open key.
1756 * pszSubKey [I] Name of the subkey of hKey.
1757 * pszValue [I] Name of value under hKey/szSubKey to query.
1758 * dwFlags [I] Flags restricting the value type to retrieve.
1759 * pdwType [O] Destination for the values type, may be NULL.
1760 * pvData [O] Destination for the values content, may be NULL.
1761 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
1762 * retrieve the whole content, including the trailing '\0'
1763 * for strings.
1765 * RETURNS
1766 * Success: ERROR_SUCCESS
1767 * Failure: nonzero error code from Winerror.h
1769 * NOTES
1770 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
1771 * expanded and pdwType is set to REG_SZ instead.
1772 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1773 * without RRF_NOEXPAND is thus not allowed.
1774 * An exception is the case where RRF_RT_ANY is specified, because then
1775 * RRF_NOEXPAND is allowed.
1777 LSTATUS WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1778 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1779 LPDWORD pcbData )
1781 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1782 PVOID pvBuf = NULL;
1783 LONG ret;
1785 TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n",
1786 hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1787 pvData, pcbData, cbData);
1789 if (pvData && !pcbData)
1790 return ERROR_INVALID_PARAMETER;
1792 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1793 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1794 return ERROR_INVALID_PARAMETER;
1796 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1797 return ERROR_INVALID_PARAMETER;
1799 if (pszSubKey && pszSubKey[0])
1801 REGSAM samDesired = KEY_QUERY_VALUE;
1803 if (dwFlags & RRF_WOW64_MASK)
1804 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1806 ret = RegOpenKeyExW(hKey, pszSubKey, 0, samDesired, &hKey);
1807 if (ret != ERROR_SUCCESS) return ret;
1810 ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1812 /* If we are going to expand we need to read in the whole the value even
1813 * if the passed buffer was too small as the expanded string might be
1814 * smaller than the unexpanded one and could fit into cbData bytes. */
1815 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1816 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1818 do {
1819 heap_free(pvBuf);
1821 pvBuf = heap_alloc(cbData);
1822 if (!pvBuf)
1824 ret = ERROR_NOT_ENOUGH_MEMORY;
1825 break;
1828 if (ret == ERROR_MORE_DATA || !pvData)
1829 ret = RegQueryValueExW(hKey, pszValue, NULL,
1830 &dwType, pvBuf, &cbData);
1831 else
1833 /* Even if cbData was large enough we have to copy the
1834 * string since ExpandEnvironmentStrings can't handle
1835 * overlapping buffers. */
1836 CopyMemory(pvBuf, pvData, cbData);
1839 /* Both the type or the value itself could have been modified in
1840 * between so we have to keep retrying until the buffer is large
1841 * enough or we no longer have to expand the value. */
1842 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1844 if (ret == ERROR_SUCCESS)
1846 /* Recheck dwType in case it changed since the first call */
1847 if (dwType == REG_EXPAND_SZ)
1849 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1850 pcbData ? *pcbData : 0) * sizeof(WCHAR);
1851 dwType = REG_SZ;
1852 if(pvData && pcbData && cbData > *pcbData)
1853 ret = ERROR_MORE_DATA;
1855 else if (pvData)
1856 CopyMemory(pvData, pvBuf, *pcbData);
1859 heap_free(pvBuf);
1862 if (pszSubKey && pszSubKey[0])
1863 RegCloseKey(hKey);
1865 apply_restrictions(dwFlags, dwType, cbData, &ret);
1867 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1868 ZeroMemory(pvData, *pcbData);
1870 if (pdwType) *pdwType = dwType;
1871 if (pcbData) *pcbData = cbData;
1873 return ret;
1877 /******************************************************************************
1878 * RegGetValueA (kernelbase.@)
1880 * See RegGetValueW.
1882 LSTATUS WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
1883 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1884 LPDWORD pcbData )
1886 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1887 PVOID pvBuf = NULL;
1888 LONG ret;
1890 TRACE("(%p,%s,%s,%d,%p,%p,%p=%d)\n",
1891 hKey, debugstr_a(pszSubKey), debugstr_a(pszValue), dwFlags,
1892 pdwType, pvData, pcbData, cbData);
1894 if (pvData && !pcbData)
1895 return ERROR_INVALID_PARAMETER;
1897 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1898 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1899 return ERROR_INVALID_PARAMETER;
1901 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1902 return ERROR_INVALID_PARAMETER;
1904 if (pszSubKey && pszSubKey[0])
1906 REGSAM samDesired = KEY_QUERY_VALUE;
1908 if (dwFlags & RRF_WOW64_MASK)
1909 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1911 ret = RegOpenKeyExA(hKey, pszSubKey, 0, samDesired, &hKey);
1912 if (ret != ERROR_SUCCESS) return ret;
1915 ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1917 /* If we are going to expand we need to read in the whole the value even
1918 * if the passed buffer was too small as the expanded string might be
1919 * smaller than the unexpanded one and could fit into cbData bytes. */
1920 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1921 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1923 do {
1924 heap_free(pvBuf);
1926 pvBuf = heap_alloc(cbData);
1927 if (!pvBuf)
1929 ret = ERROR_NOT_ENOUGH_MEMORY;
1930 break;
1933 if (ret == ERROR_MORE_DATA || !pvData)
1934 ret = RegQueryValueExA(hKey, pszValue, NULL,
1935 &dwType, pvBuf, &cbData);
1936 else
1938 /* Even if cbData was large enough we have to copy the
1939 * string since ExpandEnvironmentStrings can't handle
1940 * overlapping buffers. */
1941 CopyMemory(pvBuf, pvData, cbData);
1944 /* Both the type or the value itself could have been modified in
1945 * between so we have to keep retrying until the buffer is large
1946 * enough or we no longer have to expand the value. */
1947 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1949 if (ret == ERROR_SUCCESS)
1951 /* Recheck dwType in case it changed since the first call */
1952 if (dwType == REG_EXPAND_SZ)
1954 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
1955 pcbData ? *pcbData : 0);
1956 dwType = REG_SZ;
1957 if(pvData && pcbData && cbData > *pcbData)
1958 ret = ERROR_MORE_DATA;
1960 else if (pvData)
1961 CopyMemory(pvData, pvBuf, *pcbData);
1964 heap_free(pvBuf);
1967 if (pszSubKey && pszSubKey[0])
1968 RegCloseKey(hKey);
1970 apply_restrictions(dwFlags, dwType, cbData, &ret);
1972 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1973 ZeroMemory(pvData, *pcbData);
1975 if (pdwType) *pdwType = dwType;
1976 if (pcbData) *pcbData = cbData;
1978 return ret;
1982 /******************************************************************************
1983 * RegEnumValueW (kernelbase.@)
1985 * Enumerates the values for the specified open registry key.
1987 * PARAMS
1988 * hkey [I] Handle to key to query
1989 * index [I] Index of value to query
1990 * value [O] Value string
1991 * val_count [I/O] Size of value buffer (in wchars)
1992 * reserved [I] Reserved
1993 * type [O] Type code
1994 * data [O] Value data
1995 * count [I/O] Size of data buffer (in bytes)
1997 * RETURNS
1998 * Success: ERROR_SUCCESS
1999 * Failure: nonzero error code from Winerror.h
2001 LSTATUS WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
2002 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
2004 NTSTATUS status;
2005 DWORD total_size;
2006 char buffer[256], *buf_ptr = buffer;
2007 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2008 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
2010 TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
2011 hkey, index, value, val_count, reserved, type, data, count );
2013 if ((data && !count) || reserved || !value || !val_count)
2014 return ERROR_INVALID_PARAMETER;
2015 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2017 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
2018 if (data) total_size += *count;
2019 total_size = min( sizeof(buffer), total_size );
2021 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2022 buffer, total_size, &total_size );
2024 /* retry with a dynamically allocated buffer */
2025 while (status == STATUS_BUFFER_OVERFLOW)
2027 if (buf_ptr != buffer) heap_free( buf_ptr );
2028 if (!(buf_ptr = heap_alloc( total_size )))
2029 return ERROR_NOT_ENOUGH_MEMORY;
2030 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
2031 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2032 buf_ptr, total_size, &total_size );
2035 if (status) goto done;
2037 if (info->NameLength/sizeof(WCHAR) >= *val_count)
2039 status = STATUS_BUFFER_OVERFLOW;
2040 goto overflow;
2042 memcpy( value, info->Name, info->NameLength );
2043 *val_count = info->NameLength / sizeof(WCHAR);
2044 value[*val_count] = 0;
2046 if (data)
2048 if (total_size - info->DataOffset > *count)
2050 status = STATUS_BUFFER_OVERFLOW;
2051 goto overflow;
2053 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2054 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
2056 /* if the type is REG_SZ and data is not 0-terminated
2057 * and there is enough space in the buffer NT appends a \0 */
2058 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
2059 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
2063 overflow:
2064 if (type) *type = info->Type;
2065 if (count) *count = info->DataLength;
2067 done:
2068 if (buf_ptr != buffer) heap_free( buf_ptr );
2069 return RtlNtStatusToDosError(status);
2073 /******************************************************************************
2074 * RegEnumValueA (kernelbase.@)
2076 * See RegEnumValueW.
2078 LSTATUS WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
2079 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
2081 NTSTATUS status;
2082 DWORD total_size;
2083 char buffer[256], *buf_ptr = buffer;
2084 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2085 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
2087 TRACE("(%p,%d,%p,%p,%p,%p,%p,%p)\n",
2088 hkey, index, value, val_count, reserved, type, data, count );
2090 if ((data && !count) || reserved || !value || !val_count)
2091 return ERROR_INVALID_PARAMETER;
2092 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2094 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
2095 if (data) total_size += *count;
2096 total_size = min( sizeof(buffer), total_size );
2098 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2099 buffer, total_size, &total_size );
2101 /* we need to fetch the contents for a string type even if not requested,
2102 * because we need to compute the length of the ASCII string. */
2104 /* retry with a dynamically allocated buffer */
2105 while (status == STATUS_BUFFER_OVERFLOW)
2107 if (buf_ptr != buffer) heap_free( buf_ptr );
2108 if (!(buf_ptr = heap_alloc( total_size )))
2109 return ERROR_NOT_ENOUGH_MEMORY;
2110 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
2111 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2112 buf_ptr, total_size, &total_size );
2115 if (status) goto done;
2117 if (is_string(info->Type))
2119 DWORD len;
2120 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
2121 total_size - info->DataOffset );
2122 if (data && len)
2124 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
2125 else
2127 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
2128 total_size - info->DataOffset );
2129 /* if the type is REG_SZ and data is not 0-terminated
2130 * and there is enough space in the buffer NT appends a \0 */
2131 if (len < *count && data[len-1]) data[len] = 0;
2134 info->DataLength = len;
2136 else if (data)
2138 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
2139 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2142 if (!status)
2144 DWORD len;
2146 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
2147 if (len >= *val_count)
2149 status = STATUS_BUFFER_OVERFLOW;
2150 if (*val_count)
2152 len = *val_count - 1;
2153 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2154 value[len] = 0;
2157 else
2159 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2160 value[len] = 0;
2161 *val_count = len;
2165 if (type) *type = info->Type;
2166 if (count) *count = info->DataLength;
2168 done:
2169 if (buf_ptr != buffer) heap_free( buf_ptr );
2170 return RtlNtStatusToDosError(status);
2173 /******************************************************************************
2174 * RegDeleteValueW (kernelbase.@)
2176 * See RegDeleteValueA.
2178 LSTATUS WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
2180 return RegDeleteKeyValueW( hkey, NULL, name );
2183 /******************************************************************************
2184 * RegDeleteValueA (kernelbase.@)
2186 * Delete a value from the registry.
2188 * PARAMS
2189 * hkey [I] Registry handle of the key holding the value
2190 * name [I] Name of the value under hkey to delete
2192 * RETURNS
2193 * Success: ERROR_SUCCESS
2194 * Failure: nonzero error code from Winerror.h
2196 LSTATUS WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
2198 return RegDeleteKeyValueA( hkey, NULL, name );
2201 /******************************************************************************
2202 * RegDeleteKeyValueW (kernelbase.@)
2204 LONG WINAPI RegDeleteKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name )
2206 UNICODE_STRING nameW;
2207 HKEY hsubkey = 0;
2208 LONG ret;
2210 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2212 if (subkey)
2214 if ((ret = RegOpenKeyExW( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey )))
2215 return ret;
2216 hkey = hsubkey;
2219 RtlInitUnicodeString( &nameW, name );
2220 ret = RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
2221 if (hsubkey) RegCloseKey( hsubkey );
2222 return ret;
2225 /******************************************************************************
2226 * RegDeleteKeyValueA (kernelbase.@)
2228 LONG WINAPI RegDeleteKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name )
2230 UNICODE_STRING nameW;
2231 HKEY hsubkey = 0;
2232 ANSI_STRING nameA;
2233 NTSTATUS status;
2235 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2237 if (subkey)
2239 LONG ret = RegOpenKeyExA( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey );
2240 if (ret)
2241 return ret;
2242 hkey = hsubkey;
2245 RtlInitAnsiString( &nameA, name );
2246 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
2248 status = NtDeleteValueKey( hkey, &nameW );
2249 RtlFreeUnicodeString( &nameW );
2252 if (hsubkey) RegCloseKey( hsubkey );
2253 return RtlNtStatusToDosError( status );
2256 /******************************************************************************
2257 * RegLoadKeyW (kernelbase.@)
2259 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
2260 * registration information from a specified file into that subkey.
2262 * PARAMS
2263 * hkey [I] Handle of open key
2264 * subkey [I] Address of name of subkey
2265 * filename [I] Address of filename for registry information
2267 * RETURNS
2268 * Success: ERROR_SUCCESS
2269 * Failure: nonzero error code from Winerror.h
2271 LSTATUS WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
2273 OBJECT_ATTRIBUTES destkey, file;
2274 UNICODE_STRING subkeyW, filenameW;
2275 NTSTATUS status;
2277 if (!(hkey = get_special_root_hkey(hkey, 0))) return ERROR_INVALID_HANDLE;
2279 destkey.Length = sizeof(destkey);
2280 destkey.RootDirectory = hkey; /* root key: HKLM or HKU */
2281 destkey.ObjectName = &subkeyW; /* name of the key */
2282 destkey.Attributes = 0;
2283 destkey.SecurityDescriptor = NULL;
2284 destkey.SecurityQualityOfService = NULL;
2285 RtlInitUnicodeString(&subkeyW, subkey);
2287 file.Length = sizeof(file);
2288 file.RootDirectory = NULL;
2289 file.ObjectName = &filenameW; /* file containing the hive */
2290 file.Attributes = OBJ_CASE_INSENSITIVE;
2291 file.SecurityDescriptor = NULL;
2292 file.SecurityQualityOfService = NULL;
2293 RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
2295 status = NtLoadKey(&destkey, &file);
2296 RtlFreeUnicodeString(&filenameW);
2297 return RtlNtStatusToDosError( status );
2301 /******************************************************************************
2302 * RegLoadKeyA (kernelbase.@)
2304 * See RegLoadKeyW.
2306 LSTATUS WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
2308 UNICODE_STRING subkeyW, filenameW;
2309 STRING subkeyA, filenameA;
2310 NTSTATUS status;
2311 LONG ret;
2313 RtlInitAnsiString(&subkeyA, subkey);
2314 RtlInitAnsiString(&filenameA, filename);
2316 RtlInitUnicodeString(&subkeyW, NULL);
2317 RtlInitUnicodeString(&filenameW, NULL);
2318 if (!(status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)) &&
2319 !(status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
2321 ret = RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
2323 else ret = RtlNtStatusToDosError(status);
2324 RtlFreeUnicodeString(&subkeyW);
2325 RtlFreeUnicodeString(&filenameW);
2326 return ret;
2330 /******************************************************************************
2331 * RegSaveKeyExW (kernelbase.@)
2333 LSTATUS WINAPI RegSaveKeyExW( HKEY hkey, LPCWSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2335 UNICODE_STRING nameW;
2336 OBJECT_ATTRIBUTES attr;
2337 IO_STATUS_BLOCK io;
2338 NTSTATUS status;
2339 HANDLE handle;
2341 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
2343 if (!file || !*file) return ERROR_INVALID_PARAMETER;
2344 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2346 if ((status = RtlDosPathNameToNtPathName_U_WithStatus( file, &nameW, NULL, NULL )))
2347 return RtlNtStatusToDosError( status );
2349 InitializeObjectAttributes( &attr, &nameW, OBJ_CASE_INSENSITIVE, 0, sa );
2350 status = NtCreateFile( &handle, GENERIC_WRITE | SYNCHRONIZE, &attr, &io, NULL, FILE_NON_DIRECTORY_FILE,
2351 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
2352 FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
2353 RtlFreeUnicodeString( &nameW );
2354 if (!status)
2356 status = NtSaveKey( hkey, handle );
2357 CloseHandle( handle );
2359 return RtlNtStatusToDosError( status );
2363 /******************************************************************************
2364 * RegSaveKeyExA (kernelbase.@)
2366 LSTATUS WINAPI RegSaveKeyExA( HKEY hkey, LPCSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2368 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
2369 NTSTATUS status;
2370 STRING fileA;
2372 RtlInitAnsiString(&fileA, file);
2373 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
2374 return RtlNtStatusToDosError( status );
2375 return RegSaveKeyExW(hkey, fileW->Buffer, sa, flags);
2379 /******************************************************************************
2380 * RegRestoreKeyW (kernelbase.@)
2382 * Read the registry information from a file and copy it over a key.
2384 * PARAMS
2385 * hkey [I] Handle of key where restore begins
2386 * lpFile [I] Address of filename containing saved tree
2387 * dwFlags [I] Optional flags
2389 * RETURNS
2390 * Success: ERROR_SUCCESS
2391 * Failure: nonzero error code from Winerror.h
2393 LSTATUS WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2395 TRACE("(%p,%s,%d)\n",hkey,debugstr_w(lpFile),dwFlags);
2397 /* It seems to do this check before the hkey check */
2398 if (!lpFile || !*lpFile)
2399 return ERROR_INVALID_PARAMETER;
2401 FIXME("(%p,%s,%d): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2403 /* Check for file existence */
2405 return ERROR_SUCCESS;
2409 /******************************************************************************
2410 * RegRestoreKeyA (kernelbase.@)
2412 * See RegRestoreKeyW.
2414 LSTATUS WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2416 UNICODE_STRING lpFileW;
2417 LONG ret;
2419 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2420 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2421 RtlFreeUnicodeString( &lpFileW );
2422 return ret;
2426 /******************************************************************************
2427 * RegUnLoadKeyW (kernelbase.@)
2429 * Unload a registry key and its subkeys from the registry.
2431 * PARAMS
2432 * hkey [I] Handle of open key
2433 * lpSubKey [I] Address of name of subkey to unload
2435 * RETURNS
2436 * Success: ERROR_SUCCESS
2437 * Failure: nonzero error code from Winerror.h
2439 LSTATUS WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2441 OBJECT_ATTRIBUTES attr;
2442 UNICODE_STRING subkey;
2444 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2446 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2448 RtlInitUnicodeString(&subkey, lpSubKey);
2449 InitializeObjectAttributes(&attr, &subkey, OBJ_CASE_INSENSITIVE, hkey, NULL);
2450 return RtlNtStatusToDosError( NtUnloadKey(&attr) );
2454 /******************************************************************************
2455 * RegUnLoadKeyA (kernelbase.@)
2457 * See RegUnLoadKeyW.
2459 LSTATUS WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2461 UNICODE_STRING lpSubKeyW;
2462 LONG ret;
2464 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2465 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2466 RtlFreeUnicodeString( &lpSubKeyW );
2467 return ret;
2471 /******************************************************************************
2472 * RegSetKeySecurity (kernelbase.@)
2474 * Set the security of an open registry key.
2476 * PARAMS
2477 * hkey [I] Open handle of key to set
2478 * SecurityInfo [I] Descriptor contents
2479 * pSecurityDesc [I] Address of descriptor for key
2481 * RETURNS
2482 * Success: ERROR_SUCCESS
2483 * Failure: nonzero error code from Winerror.h
2485 LSTATUS WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2486 PSECURITY_DESCRIPTOR pSecurityDesc )
2488 TRACE("(%p,%d,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2490 /* It seems to perform this check before the hkey check */
2491 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2492 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2493 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2494 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2495 /* Param OK */
2496 } else
2497 return ERROR_INVALID_PARAMETER;
2499 if (!pSecurityDesc)
2500 return ERROR_INVALID_PARAMETER;
2502 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2504 return RtlNtStatusToDosError( NtSetSecurityObject( hkey, SecurityInfo, pSecurityDesc ) );
2508 /******************************************************************************
2509 * RegGetKeySecurity (kernelbase.@)
2511 * Get a copy of the security descriptor for a given registry key.
2513 * PARAMS
2514 * hkey [I] Open handle of key to set
2515 * SecurityInformation [I] Descriptor contents
2516 * pSecurityDescriptor [O] Address of descriptor for key
2517 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2519 * RETURNS
2520 * Success: ERROR_SUCCESS
2521 * Failure: Error code
2523 LSTATUS WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2524 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2525 LPDWORD lpcbSecurityDescriptor )
2527 TRACE("(%p,%d,%p,%d)\n",hkey,SecurityInformation,pSecurityDescriptor,
2528 *lpcbSecurityDescriptor);
2530 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2532 return RtlNtStatusToDosError( NtQuerySecurityObject( hkey,
2533 SecurityInformation, pSecurityDescriptor,
2534 *lpcbSecurityDescriptor, lpcbSecurityDescriptor ) );
2538 /******************************************************************************
2539 * RegFlushKey (kernelbase.@)
2541 * Immediately write a registry key to registry.
2543 * PARAMS
2544 * hkey [I] Handle of key to write
2546 * RETURNS
2547 * Success: ERROR_SUCCESS
2548 * Failure: Error code
2550 LSTATUS WINAPI RegFlushKey( HKEY hkey )
2552 hkey = get_special_root_hkey( hkey, 0 );
2553 if (!hkey) return ERROR_INVALID_HANDLE;
2555 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2559 /******************************************************************************
2560 * RegNotifyChangeKeyValue (kernelbase.@)
2562 * Notify the caller about changes to the attributes or contents of a registry key.
2564 * PARAMS
2565 * hkey [I] Handle of key to watch
2566 * fWatchSubTree [I] Flag for subkey notification
2567 * fdwNotifyFilter [I] Changes to be reported
2568 * hEvent [I] Handle of signaled event
2569 * fAsync [I] Flag for asynchronous reporting
2571 * RETURNS
2572 * Success: ERROR_SUCCESS
2573 * Failure: nonzero error code from Winerror.h
2575 LSTATUS WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2576 DWORD fdwNotifyFilter, HANDLE hEvent,
2577 BOOL fAsync )
2579 NTSTATUS status;
2580 IO_STATUS_BLOCK iosb;
2582 hkey = get_special_root_hkey( hkey, 0 );
2583 if (!hkey) return ERROR_INVALID_HANDLE;
2585 TRACE("(%p,%i,%d,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2586 hEvent, fAsync);
2588 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2589 fdwNotifyFilter, fWatchSubTree, NULL, 0,
2590 fAsync);
2592 if (status && status != STATUS_PENDING)
2593 return RtlNtStatusToDosError( status );
2595 return ERROR_SUCCESS;
2598 /******************************************************************************
2599 * RegOpenUserClassesRoot (kernelbase.@)
2601 * Open the HKEY_CLASSES_ROOT key for a user.
2603 * PARAMS
2604 * hToken [I] Handle of token representing the user
2605 * dwOptions [I] Reserved, must be 0
2606 * samDesired [I] Desired access rights
2607 * phkResult [O] Destination for the resulting key handle
2609 * RETURNS
2610 * Success: ERROR_SUCCESS
2611 * Failure: nonzero error code from Winerror.h
2613 * NOTES
2614 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2615 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2616 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2618 LSTATUS WINAPI RegOpenUserClassesRoot( HANDLE hToken, DWORD dwOptions, REGSAM samDesired, PHKEY phkResult )
2620 FIXME("(%p, 0x%x, 0x%x, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2622 *phkResult = HKEY_CLASSES_ROOT;
2623 return ERROR_SUCCESS;
2627 static void dump_mui_cache(void)
2629 struct mui_cache_entry *ent;
2631 TRACE("---------- MUI Cache ----------\n");
2632 LIST_FOR_EACH_ENTRY( ent, &reg_mui_cache, struct mui_cache_entry, entry )
2633 TRACE("entry=%p, %s,-%u [%#x] => %s\n",
2634 ent, wine_dbgstr_w(ent->file_name), ent->index, ent->locale, wine_dbgstr_w(ent->text));
2637 static inline void free_mui_cache_entry(struct mui_cache_entry *ent)
2639 heap_free(ent->file_name);
2640 heap_free(ent->text);
2641 heap_free(ent);
2644 /* critical section must be held */
2645 static int reg_mui_cache_get(const WCHAR *file_name, UINT index, WCHAR **buffer)
2647 struct mui_cache_entry *ent;
2649 TRACE("(%s %u %p)\n", wine_dbgstr_w(file_name), index, buffer);
2651 LIST_FOR_EACH_ENTRY(ent, &reg_mui_cache, struct mui_cache_entry, entry)
2653 if (ent->index == index && ent->locale == GetThreadLocale() &&
2654 !lstrcmpiW(ent->file_name, file_name))
2655 goto found;
2657 return 0;
2659 found:
2660 /* move to the list head */
2661 if (list_prev(&reg_mui_cache, &ent->entry)) {
2662 list_remove(&ent->entry);
2663 list_add_head(&reg_mui_cache, &ent->entry);
2666 TRACE("=> %s\n", wine_dbgstr_w(ent->text));
2667 *buffer = ent->text;
2668 return lstrlenW(ent->text);
2671 /* critical section must be held */
2672 static void reg_mui_cache_put(const WCHAR *file_name, UINT index, const WCHAR *buffer, INT size)
2674 struct mui_cache_entry *ent;
2675 TRACE("(%s %u %s %d)\n", wine_dbgstr_w(file_name), index, wine_dbgstr_wn(buffer, size), size);
2677 ent = heap_calloc(sizeof(*ent), 1);
2678 if (!ent)
2679 return;
2680 ent->file_name = heap_alloc((lstrlenW(file_name) + 1) * sizeof(WCHAR));
2681 if (!ent->file_name) {
2682 free_mui_cache_entry(ent);
2683 return;
2685 lstrcpyW(ent->file_name, file_name);
2686 ent->index = index;
2687 ent->locale = GetThreadLocale();
2688 ent->text = heap_alloc((size + 1) * sizeof(WCHAR));
2689 if (!ent->text) {
2690 free_mui_cache_entry(ent);
2691 return;
2693 memcpy(ent->text, buffer, size * sizeof(WCHAR));
2694 ent->text[size] = '\0';
2696 TRACE("add %p\n", ent);
2697 list_add_head(&reg_mui_cache, &ent->entry);
2698 if (reg_mui_cache_count > REG_MUI_CACHE_SIZE) {
2699 ent = LIST_ENTRY( list_tail( &reg_mui_cache ), struct mui_cache_entry, entry );
2700 TRACE("freeing %p\n", ent);
2701 list_remove(&ent->entry);
2702 free_mui_cache_entry(ent);
2704 else
2705 reg_mui_cache_count++;
2707 if (TRACE_ON(reg))
2708 dump_mui_cache();
2709 return;
2712 static LONG load_mui_string(const WCHAR *file_name, UINT res_id, WCHAR *buffer, INT max_chars, INT *req_chars, DWORD flags)
2714 HMODULE hModule = NULL;
2715 WCHAR *string = NULL, *full_name;
2716 int size;
2717 LONG result;
2719 /* Verify the file existence. i.e. We don't rely on PATH variable */
2720 if (GetFileAttributesW(file_name) == INVALID_FILE_ATTRIBUTES)
2721 return ERROR_FILE_NOT_FOUND;
2723 size = GetFullPathNameW(file_name, 0, NULL, NULL);
2724 if (!size)
2725 return GetLastError();
2726 full_name = heap_alloc(size * sizeof(WCHAR));
2727 if (!full_name)
2728 return ERROR_NOT_ENOUGH_MEMORY;
2729 GetFullPathNameW(file_name, size, full_name, NULL);
2731 RtlEnterCriticalSection(&reg_mui_cs);
2732 size = reg_mui_cache_get(full_name, res_id, &string);
2733 if (!size) {
2734 RtlLeaveCriticalSection(&reg_mui_cs);
2736 /* Load the file */
2737 hModule = LoadLibraryExW(full_name, NULL,
2738 LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
2739 if (!hModule)
2740 return GetLastError();
2742 size = LoadStringW(hModule, res_id, (WCHAR *)&string, 0);
2743 if (!size) {
2744 if (string) result = ERROR_NOT_FOUND;
2745 else result = GetLastError();
2746 goto cleanup;
2749 RtlEnterCriticalSection(&reg_mui_cs);
2750 reg_mui_cache_put(full_name, res_id, string, size);
2751 RtlLeaveCriticalSection(&reg_mui_cs);
2753 *req_chars = size + 1;
2755 /* If no buffer is given, skip copying. */
2756 if (!buffer) {
2757 result = ERROR_MORE_DATA;
2758 goto cleanup;
2761 /* Else copy over the string, respecting the buffer size. */
2762 if (size < max_chars)
2763 max_chars = size;
2764 else {
2765 if (flags & REG_MUI_STRING_TRUNCATE)
2766 max_chars--;
2767 else {
2768 result = ERROR_MORE_DATA;
2769 goto cleanup;
2772 if (max_chars >= 0) {
2773 memcpy(buffer, string, max_chars * sizeof(WCHAR));
2774 buffer[max_chars] = '\0';
2777 result = ERROR_SUCCESS;
2779 cleanup:
2780 if (hModule)
2781 FreeLibrary(hModule);
2782 else
2783 RtlLeaveCriticalSection(&reg_mui_cs);
2784 heap_free(full_name);
2785 return result;
2788 /******************************************************************************
2789 * RegLoadMUIStringW (kernelbase.@)
2791 * Load the localized version of a string resource from some PE, respective
2792 * id and path of which are given in the registry value in the format
2793 * @[path]\dllname,-resourceId
2795 * PARAMS
2796 * hKey [I] Key, of which to load the string value from.
2797 * pszValue [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2798 * pszBuffer [O] Buffer to store the localized string in.
2799 * cbBuffer [I] Size of the destination buffer in bytes.
2800 * pcbData [O] Number of bytes written to pszBuffer (optional, may be NULL).
2801 * dwFlags [I] Truncate output to fit the buffer if REG_MUI_STRING_TRUNCATE.
2802 * pszBaseDir [I] Base directory of loading path. If NULL, use the current directory.
2804 * RETURNS
2805 * Success: ERROR_SUCCESS,
2806 * Failure: nonzero error code from winerror.h
2808 LSTATUS WINAPI RegLoadMUIStringW(HKEY hKey, LPCWSTR pwszValue, LPWSTR pwszBuffer, DWORD cbBuffer,
2809 LPDWORD pcbData, DWORD dwFlags, LPCWSTR pwszBaseDir)
2811 DWORD dwValueType, cbData;
2812 LPWSTR pwszTempBuffer = NULL, pwszExpandedBuffer = NULL;
2813 LONG result;
2815 TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %d, pcbData = %p, "
2816 "dwFlags = %d, pwszBaseDir = %s)\n", hKey, debugstr_w(pwszValue), pwszBuffer,
2817 cbBuffer, pcbData, dwFlags, debugstr_w(pwszBaseDir));
2819 /* Parameter sanity checks. */
2820 if (!hKey || (!pwszBuffer && cbBuffer) || (cbBuffer % sizeof(WCHAR))
2821 || ((dwFlags & REG_MUI_STRING_TRUNCATE) && pcbData)
2822 || (dwFlags & ~REG_MUI_STRING_TRUNCATE))
2823 return ERROR_INVALID_PARAMETER;
2825 /* Check for value existence and correctness of its type, allocate a buffer and load it. */
2826 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, NULL, &cbData);
2827 if (result != ERROR_SUCCESS) goto cleanup;
2828 if (!(dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ) || !cbData) {
2829 result = ERROR_FILE_NOT_FOUND;
2830 goto cleanup;
2832 pwszTempBuffer = heap_alloc(cbData);
2833 if (!pwszTempBuffer) {
2834 result = ERROR_NOT_ENOUGH_MEMORY;
2835 goto cleanup;
2837 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, (LPBYTE)pwszTempBuffer, &cbData);
2838 if (result != ERROR_SUCCESS) goto cleanup;
2840 /* '@' is the prefix for resource based string entries. */
2841 if (*pwszTempBuffer != '@') {
2842 result = ERROR_INVALID_DATA;
2843 goto cleanup;
2846 /* Expand environment variables regardless of the type. */
2847 cbData = ExpandEnvironmentStringsW(pwszTempBuffer, NULL, 0) * sizeof(WCHAR);
2848 if (!cbData) goto cleanup;
2849 pwszExpandedBuffer = heap_alloc(cbData);
2850 if (!pwszExpandedBuffer) {
2851 result = ERROR_NOT_ENOUGH_MEMORY;
2852 goto cleanup;
2854 ExpandEnvironmentStringsW(pwszTempBuffer, pwszExpandedBuffer, cbData / sizeof(WCHAR));
2856 /* Parse the value and load the string. */
2858 WCHAR *pComma = wcsrchr(pwszExpandedBuffer, ','), *pNewBuffer;
2859 UINT uiStringId;
2860 DWORD baseDirLen;
2861 int reqChars;
2863 /* Format of the expanded value is 'path_to_dll,-resId' */
2864 if (!pComma || pComma[1] != '-') {
2865 result = ERROR_INVALID_DATA;
2866 goto cleanup;
2869 uiStringId = wcstol(pComma+2, NULL, 10);
2870 *pComma = '\0';
2872 /* Build a resource dll path. */
2873 baseDirLen = pwszBaseDir ? lstrlenW(pwszBaseDir) : 0;
2874 cbData = (baseDirLen + 1 + lstrlenW(pwszExpandedBuffer + 1) + 1) * sizeof(WCHAR);
2875 pNewBuffer = heap_realloc(pwszTempBuffer, cbData);
2876 if (!pNewBuffer) {
2877 result = ERROR_NOT_ENOUGH_MEMORY;
2878 goto cleanup;
2880 pwszTempBuffer = pNewBuffer;
2881 pwszTempBuffer[0] = '\0';
2882 if (baseDirLen) {
2883 lstrcpyW(pwszTempBuffer, pwszBaseDir);
2884 if (pwszBaseDir[baseDirLen - 1] != '\\')
2885 lstrcatW(pwszTempBuffer, L"\\");
2887 lstrcatW(pwszTempBuffer, pwszExpandedBuffer + 1);
2889 /* Load specified string from the file */
2890 reqChars = 0;
2891 result = load_mui_string(pwszTempBuffer, uiStringId, pwszBuffer, cbBuffer/sizeof(WCHAR), &reqChars, dwFlags);
2892 if (pcbData && (result == ERROR_SUCCESS || result == ERROR_MORE_DATA))
2893 *pcbData = reqChars * sizeof(WCHAR);
2896 cleanup:
2897 heap_free(pwszTempBuffer);
2898 heap_free(pwszExpandedBuffer);
2899 return result;
2902 /******************************************************************************
2903 * RegLoadMUIStringA (kernelbase.@)
2905 * Not implemented on native.
2907 LSTATUS WINAPI RegLoadMUIStringA(HKEY hKey, LPCSTR pszValue, LPSTR pszBuffer, DWORD cbBuffer,
2908 LPDWORD pcbData, DWORD dwFlags, LPCSTR pszBaseDir)
2910 return ERROR_CALL_NOT_IMPLEMENTED;
2914 /******************************************************************************
2915 * RegDeleteTreeW (kernelbase.@)
2918 LSTATUS WINAPI RegDeleteTreeW( HKEY hkey, const WCHAR *subkey )
2920 DWORD name_size, max_name, max_subkey;
2921 WCHAR *name_buf = NULL;
2922 LONG ret;
2924 TRACE( "(%p, %s)\n", hkey, debugstr_w(subkey) );
2926 if (subkey && *subkey)
2928 ret = RegOpenKeyExW( hkey, subkey, 0, KEY_READ, &hkey );
2929 if (ret) return ret;
2932 ret = RegQueryInfoKeyW( hkey, NULL, NULL, NULL, NULL, &max_subkey,
2933 NULL, NULL, &max_name, NULL, NULL, NULL );
2934 if (ret)
2935 goto cleanup;
2937 max_name = max( max_subkey, max_name ) + 1;
2938 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
2940 ret = ERROR_NOT_ENOUGH_MEMORY;
2941 goto cleanup;
2944 /* Recursively delete subkeys */
2945 for (;;)
2947 name_size = max_name;
2948 ret = RegEnumKeyExW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2949 if (ret == ERROR_NO_MORE_ITEMS) break;
2950 if (ret) goto cleanup;
2951 ret = RegDeleteTreeW( hkey, name_buf );
2952 if (ret) goto cleanup;
2955 /* Delete the key itself */
2956 if (subkey && *subkey)
2958 ret = RegDeleteKeyExW( hkey, L"", 0, 0 );
2959 goto cleanup;
2962 /* Delete values */
2963 for (;;)
2965 name_size = max_name;
2966 ret = RegEnumValueW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2967 if (ret == ERROR_NO_MORE_ITEMS) break;
2968 if (ret) goto cleanup;
2969 ret = RegDeleteValueW( hkey, name_buf );
2970 if (ret) goto cleanup;
2973 ret = ERROR_SUCCESS;
2975 cleanup:
2976 heap_free( name_buf );
2977 if (subkey && *subkey)
2978 RegCloseKey( hkey );
2979 return ret;
2983 /******************************************************************************
2984 * RegDeleteTreeA (kernelbase.@)
2987 LSTATUS WINAPI RegDeleteTreeA( HKEY hkey, const char *subkey )
2989 UNICODE_STRING subkeyW;
2990 LONG ret;
2992 if (subkey) RtlCreateUnicodeStringFromAsciiz( &subkeyW, subkey );
2993 else subkeyW.Buffer = NULL;
2994 ret = RegDeleteTreeW( hkey, subkeyW.Buffer );
2995 RtlFreeUnicodeString( &subkeyW );
2996 return ret;
3000 /******************************************************************************
3001 * RegCopyTreeW (kernelbase.@)
3004 LSTATUS WINAPI RegCopyTreeW( HKEY hsrc, const WCHAR *subkey, HKEY hdst )
3006 DWORD name_size, max_name;
3007 DWORD value_size, max_value;
3008 DWORD max_subkey, i, type;
3009 WCHAR *name_buf = NULL;
3010 BYTE *value_buf = NULL;
3011 HKEY hkey;
3012 LONG ret;
3014 TRACE( "(%p, %s, %p)\n", hsrc, debugstr_w(subkey), hdst );
3016 if (subkey)
3018 ret = RegOpenKeyExW( hsrc, subkey, 0, KEY_READ, &hsrc );
3019 if (ret) return ret;
3022 ret = RegQueryInfoKeyW( hsrc, NULL, NULL, NULL, NULL, &max_subkey,
3023 NULL, NULL, &max_name, &max_value, NULL, NULL );
3024 if (ret)
3025 goto cleanup;
3027 max_name = max( max_subkey, max_name ) + 1;
3028 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
3030 ret = ERROR_NOT_ENOUGH_MEMORY;
3031 goto cleanup;
3034 if (!(value_buf = heap_alloc( max_value )))
3036 ret = ERROR_NOT_ENOUGH_MEMORY;
3037 goto cleanup;
3040 /* Copy values */
3041 for (i = 0;; i++)
3043 name_size = max_name;
3044 value_size = max_value;
3045 ret = RegEnumValueW( hsrc, i, name_buf, &name_size, NULL, &type, value_buf, &value_size );
3046 if (ret == ERROR_NO_MORE_ITEMS) break;
3047 if (ret) goto cleanup;
3048 ret = RegSetValueExW( hdst, name_buf, 0, type, value_buf, value_size );
3049 if (ret) goto cleanup;
3052 /* Recursively copy subkeys */
3053 for (i = 0;; i++)
3055 name_size = max_name;
3056 ret = RegEnumKeyExW( hsrc, i, name_buf, &name_size, NULL, NULL, NULL, NULL );
3057 if (ret == ERROR_NO_MORE_ITEMS) break;
3058 if (ret) goto cleanup;
3059 ret = RegCreateKeyExW( hdst, name_buf, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL );
3060 if (ret) goto cleanup;
3061 ret = RegCopyTreeW( hsrc, name_buf, hkey );
3062 RegCloseKey( hkey );
3063 if (ret) goto cleanup;
3066 ret = ERROR_SUCCESS;
3068 cleanup:
3069 heap_free( name_buf );
3070 heap_free( value_buf );
3071 if (subkey)
3072 RegCloseKey( hsrc );
3073 return ret;
3077 /******************************************************************************
3078 * RegLoadAppKeyA (kernelbase.@)
3081 LSTATUS WINAPI RegLoadAppKeyA(const char *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3083 FIXME("%s %p %u %u %u: stub\n", wine_dbgstr_a(file), result, sam, options, reserved);
3085 if (!file || reserved)
3086 return ERROR_INVALID_PARAMETER;
3088 *result = (HKEY)0xdeadbeef;
3089 return ERROR_SUCCESS;
3092 /******************************************************************************
3093 * RegLoadAppKeyW (kernelbase.@)
3096 LSTATUS WINAPI RegLoadAppKeyW(const WCHAR *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3098 FIXME("%s %p %u %u %u: stub\n", wine_dbgstr_w(file), result, sam, options, reserved);
3100 if (!file || reserved)
3101 return ERROR_INVALID_PARAMETER;
3103 *result = (HKEY)0xdeadbeef;
3104 return ERROR_SUCCESS;
3108 /***********************************************************************
3109 * DnsHostnameToComputerNameExW (kernelbase.@)
3111 * FIXME: how is this different from the non-Ex function?
3113 BOOL WINAPI DECLSPEC_HOTPATCH DnsHostnameToComputerNameExW( const WCHAR *hostname, WCHAR *computername,
3114 DWORD *size )
3116 static const WCHAR allowed[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&')(-_{}";
3117 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3118 DWORD i, len;
3120 lstrcpynW( buffer, hostname, MAX_COMPUTERNAME_LENGTH + 1 );
3121 len = lstrlenW( buffer );
3122 if (*size < len + 1)
3124 *size = len;
3125 SetLastError( ERROR_MORE_DATA );
3126 return FALSE;
3128 *size = len;
3129 if (!computername) return FALSE;
3130 for (i = 0; i < len; i++)
3132 if (buffer[i] >= 'a' && buffer[i] <= 'z') computername[i] = buffer[i] + 'A' - 'a';
3133 else computername[i] = wcschr( allowed, buffer[i] ) ? buffer[i] : '_';
3135 computername[len] = 0;
3136 return TRUE;
3140 /***********************************************************************
3141 * GetComputerNameExA (kernelbase.@)
3143 BOOL WINAPI GetComputerNameExA( COMPUTER_NAME_FORMAT type, char *name, DWORD *len )
3145 BOOL ret = FALSE;
3146 DWORD lenA, lenW = 0;
3147 WCHAR *buffer;
3149 GetComputerNameExW( type, NULL, &lenW );
3150 if (GetLastError() != ERROR_MORE_DATA) return FALSE;
3152 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
3154 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3155 return FALSE;
3157 if (GetComputerNameExW( type, buffer, &lenW ))
3159 lenA = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
3160 if (lenA > *len)
3162 *len = lenA;
3163 SetLastError( ERROR_MORE_DATA );
3165 else
3167 WideCharToMultiByte( CP_ACP, 0, buffer, -1, name, *len, NULL, NULL );
3168 *len = lenA - 1;
3169 ret = TRUE;
3172 HeapFree( GetProcessHeap(), 0, buffer );
3173 return ret;
3177 /***********************************************************************
3178 * GetComputerNameExW (kernelbase.@)
3180 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, WCHAR *name, DWORD *len )
3182 const WCHAR *keyname, *valuename;
3183 LRESULT ret;
3184 HKEY key;
3186 switch (type)
3188 case ComputerNameNetBIOS:
3189 case ComputerNamePhysicalNetBIOS:
3190 keyname = L"System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName";
3191 valuename = L"ComputerName";
3192 break;
3193 case ComputerNameDnsHostname:
3194 case ComputerNamePhysicalDnsHostname:
3195 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3196 valuename = L"Hostname";
3197 break;
3198 case ComputerNameDnsDomain:
3199 case ComputerNamePhysicalDnsDomain:
3200 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3201 valuename = L"Domain";
3202 break;
3203 case ComputerNameDnsFullyQualified:
3204 case ComputerNamePhysicalDnsFullyQualified:
3206 WCHAR *domain, buffer[256];
3207 DWORD size = ARRAY_SIZE(buffer) - 1;
3209 if (!GetComputerNameExW( ComputerNameDnsHostname, buffer, &size )) return FALSE;
3210 domain = buffer + lstrlenW(buffer);
3211 *domain++ = '.';
3212 size = ARRAY_SIZE(buffer) - (domain - buffer);
3213 if (!GetComputerNameExW( ComputerNameDnsDomain, domain, &size )) return FALSE;
3214 if (!*domain) domain[-1] = 0;
3215 size = lstrlenW(buffer);
3216 if (name && size < *len)
3218 if (name) lstrcpyW( name, buffer );
3219 *len = size;
3220 return TRUE;
3222 *len = size + 1;
3223 SetLastError( ERROR_MORE_DATA );
3224 return FALSE;
3226 default:
3227 SetLastError( ERROR_INVALID_PARAMETER );
3228 return FALSE;
3231 if (!(ret = RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &key )))
3233 DWORD size = *len * sizeof(WCHAR);
3234 ret = RegQueryValueExW( key, valuename, NULL, NULL, (BYTE *)name, &size );
3235 if (!name) ret = ERROR_MORE_DATA;
3236 else if (!ret) size -= sizeof(WCHAR);
3237 *len = size / sizeof(WCHAR);
3238 RegCloseKey( key );
3240 TRACE("-> %lu %s\n", ret, debugstr_w(name) );
3241 if (ret) SetLastError( ret );
3242 return !ret;
3246 /***********************************************************************
3247 * SetComputerNameA (kernelbase.@)
3249 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameA( const char *name )
3251 BOOL ret;
3252 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3253 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3255 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3256 ret = SetComputerNameExW( ComputerNamePhysicalNetBIOS, nameW );
3257 HeapFree( GetProcessHeap(), 0, nameW );
3258 return ret;
3262 /***********************************************************************
3263 * SetComputerNameW (kernelbase.@)
3265 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameW( const WCHAR *name )
3267 return SetComputerNameExW( ComputerNamePhysicalNetBIOS, name );
3271 /***********************************************************************
3272 * SetComputerNameExA (kernelbase.@)
3274 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExA( COMPUTER_NAME_FORMAT type, const char *name )
3276 BOOL ret;
3277 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3278 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3280 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3281 ret = SetComputerNameExW( type, nameW );
3282 HeapFree( GetProcessHeap(), 0, nameW );
3283 return ret;
3287 /***********************************************************************
3288 * SetComputerNameExW (kernelbase.@)
3290 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExW( COMPUTER_NAME_FORMAT type, const WCHAR *name )
3292 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3293 DWORD size;
3294 HKEY key;
3295 LRESULT ret;
3297 TRACE( "%u %s\n", type, debugstr_w( name ));
3299 switch (type)
3301 case ComputerNameDnsHostname:
3302 case ComputerNamePhysicalDnsHostname:
3303 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3304 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3305 if (ret) break;
3306 ret = RegSetValueExW( key, L"Hostname", 0, REG_SZ,
3307 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3308 RegCloseKey( key );
3309 /* fall through */
3311 case ComputerNameNetBIOS:
3312 case ComputerNamePhysicalNetBIOS:
3313 /* @@ Wine registry key: HKCU\Software\Wine\Network */
3314 if (!RegOpenKeyExW( HKEY_CURRENT_USER, L"Software\\Wine\\Network", 0, KEY_READ, &key ))
3316 BOOL use_dns = TRUE;
3317 size = sizeof(buffer);
3318 if (!RegQueryValueExW( key, L"UseDnsComputerName", NULL, NULL, (BYTE *)buffer, &size ))
3319 use_dns = IS_OPTION_TRUE( buffer[0] );
3320 RegCloseKey( key );
3321 if (!use_dns)
3323 ret = ERROR_ACCESS_DENIED;
3324 break;
3327 size = ARRAY_SIZE( buffer );
3328 if (!DnsHostnameToComputerNameExW( name, buffer, &size )) return FALSE;
3329 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
3330 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3331 if (ret) break;
3332 ret = RegSetValueExW( key, L"ComputerName", 0, REG_SZ,
3333 (BYTE *)buffer, (lstrlenW(buffer) + 1) * sizeof(WCHAR) );
3334 RegCloseKey( key );
3335 break;
3337 case ComputerNameDnsDomain:
3338 case ComputerNamePhysicalDnsDomain:
3339 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3340 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3341 if (ret) break;
3342 ret = RegSetValueExW( key, L"Domain", 0, REG_SZ,
3343 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3344 RegCloseKey( key );
3345 break;
3346 default:
3347 ret = ERROR_INVALID_PARAMETER;
3348 break;
3350 if (ret) SetLastError( ret );
3351 return !ret;
3354 struct USKEY
3356 HKEY HKCUstart; /* Start key in CU hive */
3357 HKEY HKCUkey; /* Opened key in CU hive */
3358 HKEY HKLMstart; /* Start key in LM hive */
3359 HKEY HKLMkey; /* Opened key in LM hive */
3360 WCHAR path[MAX_PATH];
3363 LONG WINAPI SHRegCreateUSKeyA(LPCSTR path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3365 WCHAR *pathW;
3366 LONG ret;
3368 TRACE("%s, %#x, %p, %p, %#x\n", debugstr_a(path), samDesired, relative_key, new_uskey, flags);
3370 if (path)
3372 INT len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
3373 pathW = heap_alloc(len * sizeof(WCHAR));
3374 if (!pathW)
3375 return ERROR_NOT_ENOUGH_MEMORY;
3376 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
3378 else
3379 pathW = NULL;
3381 ret = SHRegCreateUSKeyW(pathW, samDesired, relative_key, new_uskey, flags);
3382 HeapFree(GetProcessHeap(), 0, pathW);
3383 return ret;
3386 static HKEY reg_duplicate_hkey(HKEY hKey)
3388 HKEY newKey = 0;
3390 RegOpenKeyExW(hKey, 0, 0, MAXIMUM_ALLOWED, &newKey);
3391 return newKey;
3394 static HKEY reg_get_hkey_from_huskey(HUSKEY hUSKey, BOOL is_hkcu)
3396 struct USKEY *mihk = hUSKey;
3397 HKEY test = hUSKey;
3399 if (test == HKEY_CLASSES_ROOT
3400 || test == HKEY_CURRENT_CONFIG
3401 || test == HKEY_CURRENT_USER
3402 || test == HKEY_DYN_DATA
3403 || test == HKEY_LOCAL_MACHINE
3404 || test == HKEY_PERFORMANCE_DATA
3405 || test == HKEY_USERS)
3406 /* FIXME: need to define for Win2k, ME, XP
3407 * (test == HKEY_PERFORMANCE_TEXT) ||
3408 * (test == HKEY_PERFORMANCE_NLSTEXT) ||
3411 return test;
3414 return is_hkcu ? mihk->HKCUkey : mihk->HKLMkey;
3417 LONG WINAPI SHRegCreateUSKeyW(const WCHAR *path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3419 LONG ret = ERROR_CALL_NOT_IMPLEMENTED;
3420 struct USKEY *ret_key;
3422 TRACE("%s, %#x, %p, %p, %#x\n", debugstr_w(path), samDesired, relative_key, new_uskey, flags);
3424 if (!new_uskey)
3425 return ERROR_INVALID_PARAMETER;
3427 *new_uskey = NULL;
3429 if (flags & ~SHREGSET_FORCE_HKCU)
3431 FIXME("unsupported flags 0x%08x\n", flags);
3432 return ERROR_SUCCESS;
3435 ret_key = heap_alloc_zero(sizeof(*ret_key));
3436 lstrcpynW(ret_key->path, path, ARRAY_SIZE(ret_key->path));
3438 if (relative_key)
3440 ret_key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3441 ret_key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3443 else
3445 ret_key->HKCUstart = HKEY_CURRENT_USER;
3446 ret_key->HKLMstart = HKEY_LOCAL_MACHINE;
3449 if (flags & SHREGSET_FORCE_HKCU)
3451 ret = RegCreateKeyExW(ret_key->HKCUstart, path, 0, NULL, 0, samDesired, NULL, &ret_key->HKCUkey, NULL);
3452 if (ret == ERROR_SUCCESS)
3453 *new_uskey = ret_key;
3454 else
3455 heap_free(ret_key);
3458 return ret;
3461 LONG WINAPI SHRegCloseUSKey(HUSKEY hUSKey)
3463 struct USKEY *key = hUSKey;
3464 LONG ret = ERROR_SUCCESS;
3466 if (!key)
3467 return ERROR_INVALID_PARAMETER;
3469 if (key->HKCUkey)
3470 ret = RegCloseKey(key->HKCUkey);
3471 if (key->HKCUstart && key->HKCUstart != HKEY_CURRENT_USER)
3472 ret = RegCloseKey(key->HKCUstart);
3473 if (key->HKLMkey)
3474 ret = RegCloseKey(key->HKLMkey);
3475 if (key->HKLMstart && key->HKLMstart != HKEY_LOCAL_MACHINE)
3476 ret = RegCloseKey(key->HKLMstart);
3478 heap_free(key);
3479 return ret;
3482 LONG WINAPI SHRegDeleteEmptyUSKeyA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3484 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3485 return ERROR_SUCCESS;
3488 LONG WINAPI SHRegDeleteEmptyUSKeyW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3490 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3491 return ERROR_SUCCESS;
3494 LONG WINAPI SHRegDeleteUSValueA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3496 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3497 return ERROR_SUCCESS;
3500 LONG WINAPI SHRegDeleteUSValueW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3502 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3503 return ERROR_SUCCESS;
3506 LONG WINAPI SHRegEnumUSValueA(HUSKEY hUSKey, DWORD index, char *value_name, DWORD *value_name_len, DWORD *type,
3507 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3509 HKEY dokey;
3511 TRACE("%p, %#x, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3513 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3514 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3516 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3517 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3519 FIXME("no support for SHREGENUM_BOTH\n");
3520 return ERROR_INVALID_FUNCTION;
3523 LONG WINAPI SHRegEnumUSValueW(HUSKEY hUSKey, DWORD index, WCHAR *value_name, DWORD *value_name_len, DWORD *type,
3524 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3526 HKEY dokey;
3528 TRACE("%p, %#x, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3530 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3531 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3533 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3534 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3536 FIXME("no support for SHREGENUM_BOTH\n");
3537 return ERROR_INVALID_FUNCTION;
3540 LONG WINAPI SHRegEnumUSKeyA(HUSKEY hUSKey, DWORD index, char *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3542 HKEY dokey;
3544 TRACE("%p, %d, %p, %p(%d), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3546 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3547 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3549 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3550 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3552 FIXME("no support for SHREGENUM_BOTH\n");
3553 return ERROR_INVALID_FUNCTION;
3556 LONG WINAPI SHRegEnumUSKeyW(HUSKEY hUSKey, DWORD index, WCHAR *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3558 HKEY dokey;
3560 TRACE("%p, %d, %p, %p(%d), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3562 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3563 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3565 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3566 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3568 FIXME("no support for SHREGENUM_BOTH\n");
3569 return ERROR_INVALID_FUNCTION;
3572 LONG WINAPI SHRegOpenUSKeyA(const char *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3574 WCHAR pathW[MAX_PATH];
3576 if (path)
3577 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, ARRAY_SIZE(pathW));
3579 return SHRegOpenUSKeyW(path ? pathW : NULL, access_mask, relative_key, uskey, ignore_hkcu);
3582 LONG WINAPI SHRegOpenUSKeyW(const WCHAR *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3584 LONG ret2, ret1 = ~ERROR_SUCCESS;
3585 struct USKEY *key;
3587 TRACE("%s, %#x, %p, %p, %d\n", debugstr_w(path), access_mask, relative_key, uskey, ignore_hkcu);
3589 if (uskey)
3590 *uskey = NULL;
3592 /* Create internal HUSKEY */
3593 key = heap_alloc_zero(sizeof(*key));
3594 lstrcpynW(key->path, path, ARRAY_SIZE(key->path));
3596 if (relative_key)
3598 key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3599 key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3601 /* FIXME: if either of these keys is NULL, create the start key from
3602 * the relative keys start+path
3605 else
3607 key->HKCUstart = HKEY_CURRENT_USER;
3608 key->HKLMstart = HKEY_LOCAL_MACHINE;
3611 if (!ignore_hkcu)
3613 ret1 = RegOpenKeyExW(key->HKCUstart, key->path, 0, access_mask, &key->HKCUkey);
3614 if (ret1)
3615 key->HKCUkey = 0;
3618 ret2 = RegOpenKeyExW(key->HKLMstart, key->path, 0, access_mask, &key->HKLMkey);
3619 if (ret2)
3620 key->HKLMkey = 0;
3622 if (ret1 || ret2)
3623 TRACE("one or more opens failed: HKCU=%d HKLM=%d\n", ret1, ret2);
3625 if (ret1 && ret2)
3627 /* Neither open succeeded: fail */
3628 SHRegCloseUSKey(key);
3629 return ret2;
3632 TRACE("HUSKEY=%p\n", key);
3633 if (uskey)
3634 *uskey = key;
3636 return ERROR_SUCCESS;
3639 LONG WINAPI SHRegWriteUSValueA(HUSKEY hUSKey, const char *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3641 WCHAR valueW[MAX_PATH];
3643 if (value)
3644 MultiByteToWideChar(CP_ACP, 0, value, -1, valueW, ARRAY_SIZE(valueW));
3646 return SHRegWriteUSValueW(hUSKey, value ? valueW : NULL, type, data, data_len, flags);
3649 LONG WINAPI SHRegWriteUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3651 struct USKEY *hKey = hUSKey;
3652 LONG ret = ERROR_SUCCESS;
3653 DWORD dummy;
3655 TRACE("%p, %s, %d, %p, %d, %#x\n", hUSKey, debugstr_w(value), type, data, data_len, flags);
3657 __TRY
3659 dummy = hKey->HKCUkey || hKey->HKLMkey;
3661 __EXCEPT_PAGE_FAULT
3663 return ERROR_INVALID_PARAMETER;
3665 __ENDTRY
3666 if (!(flags & (SHREGSET_FORCE_HKCU|SHREGSET_FORCE_HKLM))) return ERROR_INVALID_PARAMETER;
3668 if (flags & (SHREGSET_FORCE_HKCU | SHREGSET_HKCU))
3670 if (!hKey->HKCUkey)
3672 /* Create the key */
3673 ret = RegCreateKeyExW(hKey->HKCUstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3674 MAXIMUM_ALLOWED, NULL, &hKey->HKCUkey, NULL);
3675 TRACE("Creating HKCU key, ret = %d\n", ret);
3676 if (ret && (flags & SHREGSET_FORCE_HKCU))
3678 hKey->HKCUkey = 0;
3679 return ret;
3683 if (!ret)
3685 if ((flags & SHREGSET_FORCE_HKCU) || RegQueryValueExW(hKey->HKCUkey, value, NULL, NULL, NULL, &dummy))
3687 /* Doesn't exist or we are forcing: Write value */
3688 ret = RegSetValueExW(hKey->HKCUkey, value, 0, type, data, data_len);
3689 TRACE("Writing HKCU value, ret = %d\n", ret);
3694 if (flags & (SHREGSET_FORCE_HKLM | SHREGSET_HKLM))
3696 if (!hKey->HKLMkey)
3698 /* Create the key */
3699 ret = RegCreateKeyExW(hKey->HKLMstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3700 MAXIMUM_ALLOWED, NULL, &hKey->HKLMkey, NULL);
3701 TRACE("Creating HKLM key, ret = %d\n", ret);
3702 if (ret && (flags & (SHREGSET_FORCE_HKLM)))
3704 hKey->HKLMkey = 0;
3705 return ret;
3709 if (!ret)
3711 if ((flags & SHREGSET_FORCE_HKLM) || RegQueryValueExW(hKey->HKLMkey, value, NULL, NULL, NULL, &dummy))
3713 /* Doesn't exist or we are forcing: Write value */
3714 ret = RegSetValueExW(hKey->HKLMkey, value, 0, type, data, data_len);
3715 TRACE("Writing HKLM value, ret = %d\n", ret);
3720 return ret;
3723 LONG WINAPI SHRegSetUSValueA(const char *subkey, const char *value, DWORD type, void *data, DWORD data_len,
3724 DWORD flags)
3726 BOOL ignore_hkcu;
3727 HUSKEY hkey;
3728 LONG ret;
3730 TRACE("%s, %s, %d, %p, %d, %#x\n", debugstr_a(subkey), debugstr_a(value), type, data, data_len, flags);
3732 if (!data)
3733 return ERROR_INVALID_FUNCTION;
3735 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3737 ret = SHRegOpenUSKeyA(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3738 if (ret == ERROR_SUCCESS)
3740 ret = SHRegWriteUSValueA(hkey, value, type, data, data_len, flags);
3741 SHRegCloseUSKey(hkey);
3744 return ret;
3747 LONG WINAPI SHRegSetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD type, void *data, DWORD data_len,
3748 DWORD flags)
3750 BOOL ignore_hkcu;
3751 HUSKEY hkey;
3752 LONG ret;
3754 TRACE("%s, %s, %d, %p, %d, %#x\n", debugstr_w(subkey), debugstr_w(value), type, data, data_len, flags);
3756 if (!data)
3757 return ERROR_INVALID_FUNCTION;
3759 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3761 ret = SHRegOpenUSKeyW(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3762 if (ret == ERROR_SUCCESS)
3764 ret = SHRegWriteUSValueW(hkey, value, type, data, data_len, flags);
3765 SHRegCloseUSKey(hkey);
3768 return ret;
3771 LONG WINAPI SHRegQueryInfoUSKeyA(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3772 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3774 HKEY dokey;
3775 LONG ret;
3777 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3779 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3781 ret = RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3782 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3783 return ret;
3786 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3788 return RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3791 return ERROR_INVALID_FUNCTION;
3794 LONG WINAPI SHRegQueryInfoUSKeyW(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3795 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3797 HKEY dokey;
3798 LONG ret;
3800 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3802 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3804 ret = RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3805 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3806 return ret;
3809 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3811 return RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3814 return ERROR_INVALID_FUNCTION;
3817 LONG WINAPI SHRegQueryUSValueA(HUSKEY hUSKey, const char *value, DWORD *type, void *data, DWORD *data_len,
3818 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3820 LONG ret = ~ERROR_SUCCESS;
3821 DWORD move_len;
3822 HKEY dokey;
3824 /* If user wants HKCU, and it exists, then try it */
3825 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3827 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3828 TRACE("HKCU RegQueryValue returned %d\n", ret);
3831 /* If HKCU did not work and HKLM exists, then try it */
3832 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3834 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3835 TRACE("HKLM RegQueryValue returned %d\n", ret);
3838 /* If neither worked, and default data exists, then use it */
3839 if (ret != ERROR_SUCCESS)
3841 if (default_data && default_data_len)
3843 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3844 memmove(data, default_data, move_len);
3845 *data_len = move_len;
3846 TRACE("setting default data\n");
3847 ret = ERROR_SUCCESS;
3851 return ret;
3854 LONG WINAPI SHRegQueryUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3855 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3857 LONG ret = ~ERROR_SUCCESS;
3858 DWORD move_len;
3859 HKEY dokey;
3861 /* If user wants HKCU, and it exists, then try it */
3862 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3864 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3865 TRACE("HKCU RegQueryValue returned %d\n", ret);
3868 /* If HKCU did not work and HKLM exists, then try it */
3869 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3871 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3872 TRACE("HKLM RegQueryValue returned %d\n", ret);
3875 /* If neither worked, and default data exists, then use it */
3876 if (ret != ERROR_SUCCESS)
3878 if (default_data && default_data_len)
3880 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3881 memmove(data, default_data, move_len);
3882 *data_len = move_len;
3883 TRACE("setting default data\n");
3884 ret = ERROR_SUCCESS;
3888 return ret;
3891 LONG WINAPI SHRegGetUSValueA(const char *subkey, const char *value, DWORD *type, void *data, DWORD *data_len,
3892 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3894 HUSKEY myhuskey;
3895 LONG ret;
3897 if (!data || !data_len)
3898 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3900 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), *data_len);
3902 ret = SHRegOpenUSKeyA(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3903 if (!ret)
3905 ret = SHRegQueryUSValueA(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3906 SHRegCloseUSKey(myhuskey);
3909 return ret;
3912 LONG WINAPI SHRegGetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3913 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3915 HUSKEY myhuskey;
3916 LONG ret;
3918 if (!data || !data_len)
3919 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3921 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), *data_len);
3923 ret = SHRegOpenUSKeyW(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3924 if (!ret)
3926 ret = SHRegQueryUSValueW(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3927 SHRegCloseUSKey(myhuskey);
3930 return ret;
3933 BOOL WINAPI SHRegGetBoolUSValueA(const char *subkey, const char *value, BOOL ignore_hkcu, BOOL default_value)
3935 BOOL ret = default_value;
3936 DWORD type, datalen;
3937 char data[10];
3939 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), ignore_hkcu);
3941 datalen = ARRAY_SIZE(data) - 1;
3942 if (!SHRegGetUSValueA(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3944 switch (type)
3946 case REG_SZ:
3947 data[9] = '\0';
3948 if (!lstrcmpiA(data, "YES") || !lstrcmpiA(data, "TRUE"))
3949 ret = TRUE;
3950 else if (!lstrcmpiA(data, "NO") || !lstrcmpiA(data, "FALSE"))
3951 ret = FALSE;
3952 break;
3953 case REG_DWORD:
3954 ret = *(DWORD *)data != 0;
3955 break;
3956 case REG_BINARY:
3957 if (datalen == 1)
3959 ret = !!data[0];
3960 break;
3962 default:
3963 FIXME("Unsupported registry data type %d\n", type);
3964 ret = FALSE;
3966 TRACE("got value (type=%d), returning %d\n", type, ret);
3968 else
3969 TRACE("returning default value %d\n", ret);
3971 return ret;
3974 BOOL WINAPI SHRegGetBoolUSValueW(const WCHAR *subkey, const WCHAR *value, BOOL ignore_hkcu, BOOL default_value)
3976 BOOL ret = default_value;
3977 DWORD type, datalen;
3978 WCHAR data[10];
3980 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), ignore_hkcu);
3982 datalen = (ARRAY_SIZE(data) - 1) * sizeof(WCHAR);
3983 if (!SHRegGetUSValueW(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3985 switch (type)
3987 case REG_SZ:
3988 data[9] = '\0';
3989 if (!lstrcmpiW(data, L"yes") || !lstrcmpiW(data, L"true"))
3990 ret = TRUE;
3991 else if (!lstrcmpiW(data, L"no") || !lstrcmpiW(data, L"false"))
3992 ret = FALSE;
3993 break;
3994 case REG_DWORD:
3995 ret = *(DWORD *)data != 0;
3996 break;
3997 case REG_BINARY:
3998 if (datalen == 1)
4000 ret = !!data[0];
4001 break;
4003 default:
4004 FIXME("Unsupported registry data type %d\n", type);
4005 ret = FALSE;
4007 TRACE("got value (type=%d), returning %d\n", type, ret);
4009 else
4010 TRACE("returning default value %d\n", ret);
4012 return ret;