gdiplus: Add GdipGetPenCompoundCount implementation.
[wine.git] / dlls / kernelbase / registry.c
blob91462d80e065a1b5edc1d8847ccd4c27deddcee7
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;
357 static BOOL is_perf_key( HKEY key )
359 return HandleToUlong(key) == HandleToUlong(HKEY_PERFORMANCE_DATA)
360 || HandleToUlong(key) == HandleToUlong(HKEY_PERFORMANCE_TEXT)
361 || HandleToUlong(key) == HandleToUlong(HKEY_PERFORMANCE_NLSTEXT);
365 /******************************************************************************
366 * RemapPredefinedHandleInternal (kernelbase.@)
368 NTSTATUS WINAPI RemapPredefinedHandleInternal( HKEY hkey, HKEY override )
370 HKEY old_key;
371 int idx;
373 TRACE("(%p %p)\n", hkey, override);
375 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
376 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
377 return STATUS_INVALID_HANDLE;
378 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
380 if (override)
382 NTSTATUS status = NtDuplicateObject( GetCurrentProcess(), override,
383 GetCurrentProcess(), (HANDLE *)&override,
384 0, 0, DUPLICATE_SAME_ACCESS );
385 if (status) return status;
388 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], override );
389 if (old_key) NtClose( old_key );
390 return STATUS_SUCCESS;
394 /******************************************************************************
395 * DisablePredefinedHandleTableInternal (kernelbase.@)
397 NTSTATUS WINAPI DisablePredefinedHandleTableInternal( HKEY hkey )
399 HKEY old_key;
400 int idx;
402 TRACE("(%p)\n", hkey);
404 if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
405 || (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
406 return STATUS_INVALID_HANDLE;
407 idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
409 cache_disabled[idx] = TRUE;
411 old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], NULL );
412 if (old_key) NtClose( old_key );
413 return STATUS_SUCCESS;
417 /******************************************************************************
418 * RegCreateKeyExW (kernelbase.@)
420 * See RegCreateKeyExA.
422 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
423 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
424 PHKEY retkey, LPDWORD dispos )
426 OBJECT_ATTRIBUTES attr;
427 UNICODE_STRING nameW, classW;
429 if (reserved) return ERROR_INVALID_PARAMETER;
430 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
432 attr.Length = sizeof(attr);
433 attr.RootDirectory = hkey;
434 attr.ObjectName = &nameW;
435 attr.Attributes = 0;
436 attr.SecurityDescriptor = NULL;
437 attr.SecurityQualityOfService = NULL;
438 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
439 RtlInitUnicodeString( &nameW, name );
440 RtlInitUnicodeString( &classW, class );
442 return RtlNtStatusToDosError( create_key( retkey, access, &attr, &classW, options, dispos ) );
446 /******************************************************************************
447 * RegCreateKeyExA (kernelbase.@)
449 * Open a registry key, creating it if it doesn't exist.
451 * PARAMS
452 * hkey [I] Handle of the parent registry key
453 * name [I] Name of the new key to open or create
454 * reserved [I] Reserved, pass 0
455 * class [I] The object type of the new key
456 * options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
457 * access [I] Access level desired
458 * sa [I] Security attributes for the key
459 * retkey [O] Destination for the resulting handle
460 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
462 * RETURNS
463 * Success: ERROR_SUCCESS.
464 * Failure: A standard Win32 error code. retkey remains untouched.
466 * FIXME
467 * MAXIMUM_ALLOWED in access mask not supported by server
469 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
470 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
471 PHKEY retkey, LPDWORD dispos )
473 OBJECT_ATTRIBUTES attr;
474 UNICODE_STRING classW;
475 ANSI_STRING nameA, classA;
476 NTSTATUS status;
478 if (reserved) return ERROR_INVALID_PARAMETER;
479 if (!is_version_nt())
481 access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
482 if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
484 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
486 attr.Length = sizeof(attr);
487 attr.RootDirectory = hkey;
488 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
489 attr.Attributes = 0;
490 attr.SecurityDescriptor = NULL;
491 attr.SecurityQualityOfService = NULL;
492 if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
493 RtlInitAnsiString( &nameA, name );
494 RtlInitAnsiString( &classA, class );
496 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
497 &nameA, FALSE )))
499 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
501 status = create_key( retkey, access, &attr, &classW, options, dispos );
502 RtlFreeUnicodeString( &classW );
505 return RtlNtStatusToDosError( status );
509 /******************************************************************************
510 * RegOpenKeyExW (kernelbase.@)
512 * See RegOpenKeyExA.
514 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD options, REGSAM access, PHKEY retkey )
516 OBJECT_ATTRIBUTES attr;
517 UNICODE_STRING nameW;
519 if (retkey && (!name || !name[0]) &&
520 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
521 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
523 *retkey = hkey;
524 return ERROR_SUCCESS;
527 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
528 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
530 if (!retkey) return ERROR_INVALID_PARAMETER;
531 *retkey = NULL;
532 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
534 attr.Length = sizeof(attr);
535 attr.RootDirectory = hkey;
536 attr.ObjectName = &nameW;
537 attr.Attributes = 0;
538 attr.SecurityDescriptor = NULL;
539 attr.SecurityQualityOfService = NULL;
540 RtlInitUnicodeString( &nameW, name );
541 return RtlNtStatusToDosError( open_key( retkey, options, access, &attr ) );
545 /******************************************************************************
546 * RegOpenKeyExA (kernelbase.@)
548 * Open a registry key.
550 * PARAMS
551 * hkey [I] Handle of open key
552 * name [I] Name of subkey to open
553 * options [I] Open options (can be set to REG_OPTION_OPEN_LINK)
554 * access [I] Security access mask
555 * retkey [O] Handle to open key
557 * RETURNS
558 * Success: ERROR_SUCCESS
559 * Failure: A standard Win32 error code. retkey is set to 0.
561 * NOTES
562 * Unlike RegCreateKeyExA(), this function will not create the key if it
563 * does not exist.
565 LSTATUS WINAPI DECLSPEC_HOTPATCH RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD options, REGSAM access, PHKEY retkey )
567 OBJECT_ATTRIBUTES attr;
568 STRING nameA;
569 NTSTATUS status;
571 if (retkey && (!name || !name[0]) &&
572 (HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)) &&
573 (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
575 *retkey = hkey;
576 return ERROR_SUCCESS;
579 if (!is_version_nt()) access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
580 else
582 /* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
583 if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
586 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
588 attr.Length = sizeof(attr);
589 attr.RootDirectory = hkey;
590 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
591 attr.Attributes = 0;
592 attr.SecurityDescriptor = NULL;
593 attr.SecurityQualityOfService = NULL;
595 RtlInitAnsiString( &nameA, name );
596 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
597 &nameA, FALSE )))
599 status = open_key( retkey, options, access, &attr );
601 return RtlNtStatusToDosError( status );
605 /******************************************************************************
606 * RegOpenCurrentUser (kernelbase.@)
608 * Get a handle to the HKEY_CURRENT_USER key for the user
609 * the current thread is impersonating.
611 * PARAMS
612 * access [I] Desired access rights to the key
613 * retkey [O] Handle to the opened key
615 * RETURNS
616 * Success: ERROR_SUCCESS
617 * Failure: nonzero error code from Winerror.h
619 * FIXME
620 * This function is supposed to retrieve a handle to the
621 * HKEY_CURRENT_USER for the user the current thread is impersonating.
622 * Since Wine does not currently allow threads to impersonate other users,
623 * this stub should work fine.
625 LSTATUS WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
627 void *data[20];
628 TOKEN_USER *info = (TOKEN_USER *)data;
629 HANDLE token;
630 DWORD len = 0;
632 /* get current user SID */
633 if (OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, FALSE, &token ))
635 len = sizeof(data);
636 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
637 CloseHandle( token );
639 if (!len)
641 ImpersonateSelf(SecurityIdentification);
642 if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &token))
644 len = sizeof(data);
645 if (!GetTokenInformation( token, TokenUser, info, len, &len )) len = 0;
646 CloseHandle( token );
648 RevertToSelf();
651 if (len)
653 WCHAR buffer[200];
654 UNICODE_STRING string = { 0, sizeof(buffer), buffer };
656 RtlConvertSidToUnicodeString( &string, info->User.Sid, FALSE );
657 return RegOpenKeyExW( HKEY_USERS, string.Buffer, 0, access, retkey );
660 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
665 /******************************************************************************
666 * RegEnumKeyExW (kernelbase.@)
668 * Enumerate subkeys of the specified open registry key.
670 * PARAMS
671 * hkey [I] Handle to key to enumerate
672 * index [I] Index of subkey to enumerate
673 * name [O] Buffer for subkey name
674 * name_len [O] Size of subkey buffer
675 * reserved [I] Reserved
676 * class [O] Buffer for class string
677 * class_len [O] Size of class buffer
678 * ft [O] Time key last written to
680 * RETURNS
681 * Success: ERROR_SUCCESS
682 * Failure: System error code. If there are no more subkeys available, the
683 * function returns ERROR_NO_MORE_ITEMS.
685 LSTATUS WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
686 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
688 NTSTATUS status;
689 char buffer[256], *buf_ptr = buffer;
690 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
691 DWORD total_size;
693 TRACE( "(%p,%ld,%p,%p(%lu),%p,%p,%p,%p)\n", hkey, index, name, name_len,
694 name_len ? *name_len : 0, reserved, class, class_len, ft );
696 if (reserved) return ERROR_INVALID_PARAMETER;
697 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
699 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
700 buffer, sizeof(buffer), &total_size );
702 while (status == STATUS_BUFFER_OVERFLOW)
704 /* retry with a dynamically allocated buffer */
705 if (buf_ptr != buffer) heap_free( buf_ptr );
706 if (!(buf_ptr = heap_alloc( total_size )))
707 return ERROR_NOT_ENOUGH_MEMORY;
708 info = (KEY_NODE_INFORMATION *)buf_ptr;
709 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
710 buf_ptr, total_size, &total_size );
713 if (!status)
715 DWORD len = info->NameLength / sizeof(WCHAR);
716 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
718 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
720 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
721 status = STATUS_BUFFER_OVERFLOW;
722 else
724 *name_len = len;
725 memcpy( name, info->Name, info->NameLength );
726 name[len] = 0;
727 if (class_len)
729 *class_len = cls_len;
730 if (class)
732 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
733 class[cls_len] = 0;
739 if (buf_ptr != buffer) heap_free( buf_ptr );
740 return RtlNtStatusToDosError( status );
744 /******************************************************************************
745 * RegEnumKeyExA (kernelbase.@)
747 * See RegEnumKeyExW.
749 LSTATUS WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
750 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
752 NTSTATUS status;
753 char buffer[256], *buf_ptr = buffer;
754 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
755 DWORD total_size;
757 TRACE( "(%p,%ld,%p,%p(%lu),%p,%p,%p,%p)\n", hkey, index, name, name_len,
758 name_len ? *name_len : 0, reserved, class, class_len, ft );
760 if (reserved) return ERROR_INVALID_PARAMETER;
761 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
763 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
764 buffer, sizeof(buffer), &total_size );
766 while (status == STATUS_BUFFER_OVERFLOW)
768 /* retry with a dynamically allocated buffer */
769 if (buf_ptr != buffer) heap_free( buf_ptr );
770 if (!(buf_ptr = heap_alloc( total_size )))
771 return ERROR_NOT_ENOUGH_MEMORY;
772 info = (KEY_NODE_INFORMATION *)buf_ptr;
773 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
774 buf_ptr, total_size, &total_size );
777 if (!status)
779 DWORD len, cls_len;
781 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
782 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
783 info->ClassLength );
784 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
786 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
787 status = STATUS_BUFFER_OVERFLOW;
788 else
790 *name_len = len;
791 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
792 name[len] = 0;
793 if (class_len)
795 *class_len = cls_len;
796 if (class)
798 RtlUnicodeToMultiByteN( class, cls_len, NULL,
799 (WCHAR *)(buf_ptr + info->ClassOffset),
800 info->ClassLength );
801 class[cls_len] = 0;
807 if (buf_ptr != buffer) heap_free( buf_ptr );
808 return RtlNtStatusToDosError( status );
812 /******************************************************************************
813 * RegQueryInfoKeyW (kernelbase.@)
815 * Retrieves information about the specified registry key.
817 * PARAMS
818 * hkey [I] Handle to key to query
819 * class [O] Buffer for class string
820 * class_len [O] Size of class string buffer
821 * reserved [I] Reserved
822 * subkeys [O] Buffer for number of subkeys
823 * max_subkey [O] Buffer for longest subkey name length
824 * max_class [O] Buffer for longest class string length
825 * values [O] Buffer for number of value entries
826 * max_value [O] Buffer for longest value name length
827 * max_data [O] Buffer for longest value data length
828 * security [O] Buffer for security descriptor length
829 * modif [O] Modification time
831 * RETURNS
832 * Success: ERROR_SUCCESS
833 * Failure: system error code.
835 * NOTES
836 * - win95 allows class to be valid and class_len to be NULL
837 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
838 * - both allow class to be NULL and class_len to be NULL
839 * (it's hard to test validity, so test !NULL instead)
841 LSTATUS WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
842 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
843 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
844 LPDWORD security, FILETIME *modif )
846 NTSTATUS status;
847 char buffer[256], *buf_ptr = buffer;
848 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
849 DWORD total_size;
851 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
852 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
854 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
855 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
857 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
858 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
860 if (class && class_len && *class_len)
862 /* retry with a dynamically allocated buffer */
863 while (status == STATUS_BUFFER_OVERFLOW)
865 if (buf_ptr != buffer) heap_free( buf_ptr );
866 if (!(buf_ptr = heap_alloc( total_size )))
867 return ERROR_NOT_ENOUGH_MEMORY;
868 info = (KEY_FULL_INFORMATION *)buf_ptr;
869 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
872 if (status) goto done;
874 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
876 status = STATUS_BUFFER_TOO_SMALL;
878 else
880 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
881 class[info->ClassLength/sizeof(WCHAR)] = 0;
884 else status = STATUS_SUCCESS;
886 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
887 if (subkeys) *subkeys = info->SubKeys;
888 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
889 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
890 if (values) *values = info->Values;
891 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
892 if (max_data) *max_data = info->MaxValueDataLen;
893 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
895 if (security)
897 FIXME( "security argument not supported.\n");
898 *security = 0;
901 done:
902 if (buf_ptr != buffer) heap_free( buf_ptr );
903 return RtlNtStatusToDosError( status );
907 /******************************************************************************
908 * RegQueryInfoKeyA (kernelbase.@)
910 * Retrieves information about a registry key.
912 * PARAMS
913 * hKey [I] Handle to an open key.
914 * lpClass [O] Class string of the key.
915 * lpcClass [I/O] size of lpClass.
916 * lpReserved [I] Reserved; must be NULL.
917 * lpcSubKeys [O] Number of subkeys contained by the key.
918 * lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
919 * lpcMaxClassLen [O] Size of the longest string specifying a subkey
920 * class in TCHARS.
921 * lpcValues [O] Number of values associated with the key.
922 * lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
923 * lpcMaxValueLen [O] Longest data component among the key's values
924 * lpcbSecurityDescriptor [O] Size of the key's security descriptor.
925 * lpftLastWriteTime [O] FILETIME structure that is the last write time.
927 * RETURNS
928 * Success: ERROR_SUCCESS
929 * Failure: nonzero error code from Winerror.h
931 LSTATUS WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
932 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
933 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
934 LPDWORD security, FILETIME *modif )
936 NTSTATUS status;
937 char buffer[256], *buf_ptr = buffer;
938 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
939 DWORD total_size;
941 TRACE( "(%p,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
942 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
944 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
945 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
947 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
948 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
950 if (class || class_len)
952 /* retry with a dynamically allocated buffer */
953 while (status == STATUS_BUFFER_OVERFLOW)
955 if (buf_ptr != buffer) heap_free( buf_ptr );
956 if (!(buf_ptr = heap_alloc( total_size )))
957 return ERROR_NOT_ENOUGH_MEMORY;
958 info = (KEY_FULL_INFORMATION *)buf_ptr;
959 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
962 if (status) goto done;
964 if (class && class_len && *class_len)
966 DWORD len = *class_len;
967 RtlUnicodeToMultiByteN( class, len, class_len,
968 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
969 if (*class_len == len)
971 status = STATUS_BUFFER_OVERFLOW;
972 *class_len -= 1;
974 class[*class_len] = 0;
976 else if (class_len)
977 RtlUnicodeToMultiByteSize( class_len,
978 (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength );
980 else status = STATUS_SUCCESS;
982 if (subkeys) *subkeys = info->SubKeys;
983 if (max_subkey) *max_subkey = info->MaxNameLen / sizeof(WCHAR);
984 if (max_class) *max_class = info->MaxClassLen / sizeof(WCHAR);
985 if (values) *values = info->Values;
986 if (max_value) *max_value = info->MaxValueNameLen / sizeof(WCHAR);
987 if (max_data) *max_data = info->MaxValueDataLen;
988 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
990 if (security)
992 FIXME( "security argument not supported.\n");
993 *security = 0;
996 done:
997 if (buf_ptr != buffer) heap_free( buf_ptr );
998 return RtlNtStatusToDosError( status );
1001 /******************************************************************************
1002 * RegCloseKey (kernelbase.@)
1004 * Close an open registry key.
1006 * PARAMS
1007 * hkey [I] Handle of key to close
1009 * RETURNS
1010 * Success: ERROR_SUCCESS
1011 * Failure: Error code
1013 LSTATUS WINAPI DECLSPEC_HOTPATCH RegCloseKey( HKEY hkey )
1015 if (!hkey) return ERROR_INVALID_HANDLE;
1016 if (hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
1017 return RtlNtStatusToDosError( NtClose( hkey ) );
1021 /******************************************************************************
1022 * RegDeleteKeyExW (kernelbase.@)
1024 LSTATUS WINAPI RegDeleteKeyExW( HKEY hkey, LPCWSTR name, REGSAM access, DWORD reserved )
1026 DWORD ret;
1027 HKEY tmp;
1029 if (!name) return ERROR_INVALID_PARAMETER;
1031 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
1033 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1034 if (!(ret = RegOpenKeyExW( hkey, name, 0, access | DELETE, &tmp )))
1036 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1037 RegCloseKey( tmp );
1039 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
1040 return ret;
1044 /******************************************************************************
1045 * RegDeleteKeyExA (kernelbase.@)
1047 LSTATUS WINAPI RegDeleteKeyExA( HKEY hkey, LPCSTR name, REGSAM access, DWORD reserved )
1049 DWORD ret;
1050 HKEY tmp;
1052 if (!name) return ERROR_INVALID_PARAMETER;
1054 if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
1056 access &= KEY_WOW64_64KEY | KEY_WOW64_32KEY;
1057 if (!(ret = RegOpenKeyExA( hkey, name, 0, access | DELETE, &tmp )))
1059 if (!is_version_nt()) /* win95 does recursive key deletes */
1061 CHAR sub[MAX_PATH];
1062 DWORD len = sizeof(sub);
1063 while(!RegEnumKeyExA(tmp, 0, sub, &len, NULL, NULL, NULL, NULL))
1065 if(RegDeleteKeyExA(tmp, sub, access, reserved)) /* recurse */
1066 break;
1069 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
1070 RegCloseKey( tmp );
1072 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
1073 return ret;
1076 /******************************************************************************
1077 * RegSetValueExW (kernelbase.@)
1079 * Set the data and contents of a registry value.
1081 * PARAMS
1082 * hkey [I] Handle of key to set value for
1083 * name [I] Name of value to set
1084 * reserved [I] Reserved, must be zero
1085 * type [I] Type of the value being set
1086 * data [I] The new contents of the value to set
1087 * count [I] Size of data
1089 * RETURNS
1090 * Success: ERROR_SUCCESS
1091 * Failure: Error code
1093 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
1094 DWORD type, const BYTE *data, DWORD count )
1096 UNICODE_STRING nameW;
1098 /* no need for version check, not implemented on win9x anyway */
1100 if ((data && ((ULONG_PTR)data >> 16) == 0) || (!data && count)) return ERROR_NOACCESS;
1102 if (count && is_string(type))
1104 LPCWSTR str = (LPCWSTR)data;
1105 /* if user forgot to count terminating null, add it (yes NT does this) */
1106 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
1107 count += sizeof(WCHAR);
1109 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1111 RtlInitUnicodeString( &nameW, name );
1112 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
1116 /******************************************************************************
1117 * RegSetValueExA (kernelbase.@)
1119 * See RegSetValueExW.
1121 * NOTES
1122 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
1123 * NT does definitely care (aj)
1125 LSTATUS WINAPI DECLSPEC_HOTPATCH RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
1126 const BYTE *data, DWORD count )
1128 ANSI_STRING nameA;
1129 UNICODE_STRING nameW;
1130 WCHAR *dataW = NULL;
1131 NTSTATUS status;
1133 if (!is_version_nt()) /* win95 */
1135 if (type == REG_SZ)
1137 if (!data) return ERROR_INVALID_PARAMETER;
1138 count = strlen((const char *)data) + 1;
1141 else if (count && is_string(type))
1143 /* if user forgot to count terminating null, add it (yes NT does this) */
1144 if (data[count-1] && !data[count]) count++;
1147 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1149 if (is_string( type )) /* need to convert to Unicode */
1151 DWORD lenW;
1152 RtlMultiByteToUnicodeSize( &lenW, (const char *)data, count );
1153 if (!(dataW = heap_alloc( lenW ))) return ERROR_OUTOFMEMORY;
1154 RtlMultiByteToUnicodeN( dataW, lenW, NULL, (const char *)data, count );
1155 count = lenW;
1156 data = (BYTE *)dataW;
1159 RtlInitAnsiString( &nameA, name );
1160 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1162 status = NtSetValueKey( hkey, &nameW, 0, type, data, count );
1163 RtlFreeUnicodeString( &nameW );
1165 heap_free( dataW );
1166 return RtlNtStatusToDosError( status );
1170 /******************************************************************************
1171 * RegSetKeyValueW (kernelbase.@)
1173 LONG WINAPI RegSetKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name, DWORD type, const void *data, DWORD len )
1175 HKEY hsubkey = NULL;
1176 DWORD ret;
1178 TRACE("(%p,%s,%s,%ld,%p,%ld)\n", hkey, debugstr_w(subkey), debugstr_w(name), type, data, len );
1180 if (subkey && subkey[0]) /* need to create the subkey */
1182 if ((ret = RegCreateKeyExW( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1183 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1184 hkey = hsubkey;
1187 ret = RegSetValueExW( hkey, name, 0, type, (const BYTE*)data, len );
1188 if (hsubkey) RegCloseKey( hsubkey );
1189 return ret;
1192 /******************************************************************************
1193 * RegSetKeyValueA (kernelbase.@)
1195 LONG WINAPI RegSetKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name, DWORD type, const void *data, DWORD len )
1197 HKEY hsubkey = NULL;
1198 DWORD ret;
1200 TRACE("(%p,%s,%s,%ld,%p,%ld)\n", hkey, debugstr_a(subkey), debugstr_a(name), type, data, len );
1202 if (subkey && subkey[0]) /* need to create the subkey */
1204 if ((ret = RegCreateKeyExA( hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
1205 KEY_SET_VALUE, NULL, &hsubkey, NULL )) != ERROR_SUCCESS) return ret;
1206 hkey = hsubkey;
1209 ret = RegSetValueExA( hkey, name, 0, type, (const BYTE*)data, len );
1210 if (hsubkey) RegCloseKey( hsubkey );
1211 return ret;
1214 /* FIXME: we should read data from system32/perf009c.dat (or perf###c depending
1215 * on locale) instead */
1216 static DWORD query_perf_names( DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1218 static const WCHAR names[] = L"1\0" "1847\0" "1846\0End Marker\0";
1219 DWORD size = *ret_size;
1221 if (type) *type = REG_MULTI_SZ;
1222 *ret_size = sizeof(names);
1223 if (!unicode) *ret_size /= sizeof(WCHAR);
1225 if (!data) return ERROR_SUCCESS;
1226 if (size < *ret_size) return ERROR_MORE_DATA;
1228 if (unicode)
1229 memcpy( data, names, sizeof(names) );
1230 else
1231 RtlUnicodeToMultiByteN( data, size, NULL, names, sizeof(names) );
1232 return ERROR_SUCCESS;
1235 /* FIXME: we should read data from system32/perf009h.dat (or perf###h depending
1236 * on locale) instead */
1237 static DWORD query_perf_help( DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1239 static const WCHAR names[] = L"1847\0End Marker\0";
1240 DWORD size = *ret_size;
1242 if (type) *type = REG_MULTI_SZ;
1243 *ret_size = sizeof(names);
1244 if (!unicode) *ret_size /= sizeof(WCHAR);
1246 if (!data) return ERROR_SUCCESS;
1247 if (size < *ret_size) return ERROR_MORE_DATA;
1249 if (unicode)
1250 memcpy( data, names, sizeof(names) );
1251 else
1252 RtlUnicodeToMultiByteN( data, size, NULL, names, sizeof(names) );
1253 return ERROR_SUCCESS;
1256 struct perf_provider
1258 HMODULE perflib;
1259 WCHAR linkage[MAX_PATH];
1260 WCHAR objects[MAX_PATH];
1261 PM_OPEN_PROC *pOpen;
1262 PM_CLOSE_PROC *pClose;
1263 PM_COLLECT_PROC *pCollect;
1266 static void *get_provider_entry(HKEY perf, HMODULE perflib, const char *name)
1268 char buf[MAX_PATH];
1269 DWORD err, type, len;
1271 len = sizeof(buf) - 1;
1272 err = RegQueryValueExA(perf, name, NULL, &type, (BYTE *)buf, &len);
1273 if (err != ERROR_SUCCESS || type != REG_SZ)
1274 return NULL;
1276 buf[len] = 0;
1277 TRACE("Loading function pointer for %s: %s\n", name, debugstr_a(buf));
1279 return GetProcAddress(perflib, buf);
1282 static BOOL load_provider(HKEY root, const WCHAR *name, struct perf_provider *provider)
1284 WCHAR buf[MAX_PATH], buf2[MAX_PATH];
1285 DWORD err, type, len;
1286 HKEY service, perf;
1288 err = RegOpenKeyExW(root, name, 0, KEY_READ, &service);
1289 if (err != ERROR_SUCCESS)
1290 return FALSE;
1292 provider->linkage[0] = 0;
1293 err = RegOpenKeyExW(service, L"Linkage", 0, KEY_READ, &perf);
1294 if (err == ERROR_SUCCESS)
1296 len = sizeof(buf) - sizeof(WCHAR);
1297 err = RegQueryValueExW(perf, L"Export", NULL, &type, (BYTE *)buf, &len);
1298 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1300 memcpy(provider->linkage, buf, len);
1301 provider->linkage[len / sizeof(WCHAR)] = 0;
1302 TRACE("Export: %s\n", debugstr_w(provider->linkage));
1304 RegCloseKey(perf);
1307 err = RegOpenKeyExW(service, L"Performance", 0, KEY_READ, &perf);
1308 RegCloseKey(service);
1309 if (err != ERROR_SUCCESS)
1310 return FALSE;
1312 provider->objects[0] = 0;
1313 len = sizeof(buf) - sizeof(WCHAR);
1314 err = RegQueryValueExW(perf, L"Object List", NULL, &type, (BYTE *)buf, &len);
1315 if (err == ERROR_SUCCESS && (type == REG_SZ || type == REG_MULTI_SZ))
1317 memcpy(provider->objects, buf, len);
1318 provider->objects[len / sizeof(WCHAR)] = 0;
1319 TRACE("Object List: %s\n", debugstr_w(provider->objects));
1322 len = sizeof(buf) - sizeof(WCHAR);
1323 err = RegQueryValueExW(perf, L"Library", NULL, &type, (BYTE *)buf, &len);
1324 if (err != ERROR_SUCCESS || !(type == REG_SZ || type == REG_EXPAND_SZ))
1325 goto error;
1327 buf[len / sizeof(WCHAR)] = 0;
1328 if (type == REG_EXPAND_SZ)
1330 len = ExpandEnvironmentStringsW(buf, buf2, MAX_PATH);
1331 if (!len || len > MAX_PATH) goto error;
1332 lstrcpyW(buf, buf2);
1335 if (!(provider->perflib = LoadLibraryW(buf)))
1337 WARN("Failed to load %s\n", debugstr_w(buf));
1338 goto error;
1341 GetModuleFileNameW(provider->perflib, buf, MAX_PATH);
1342 TRACE("Loaded provider %s\n", wine_dbgstr_w(buf));
1344 provider->pOpen = get_provider_entry(perf, provider->perflib, "Open");
1345 provider->pClose = get_provider_entry(perf, provider->perflib, "Close");
1346 provider->pCollect = get_provider_entry(perf, provider->perflib, "Collect");
1347 if (provider->pOpen && provider->pClose && provider->pCollect)
1349 RegCloseKey(perf);
1350 return TRUE;
1353 TRACE("Provider is missing required exports\n");
1354 FreeLibrary(provider->perflib);
1356 error:
1357 RegCloseKey(perf);
1358 return FALSE;
1361 static DWORD collect_data(struct perf_provider *provider, const WCHAR *query, void **data, DWORD *size, DWORD *obj_count)
1363 WCHAR *linkage = provider->linkage[0] ? provider->linkage : NULL;
1364 DWORD err;
1366 if (!query || !query[0])
1367 query = L"Global";
1369 err = provider->pOpen(linkage);
1370 if (err != ERROR_SUCCESS)
1372 TRACE("Open(%s) error %lu (%#lx)\n", debugstr_w(linkage), err, err);
1373 return err;
1376 *obj_count = 0;
1377 err = provider->pCollect((WCHAR *)query, data, size, obj_count);
1378 if (err != ERROR_SUCCESS)
1380 TRACE("Collect error %lu (%#lx)\n", err, err);
1381 *obj_count = 0;
1384 provider->pClose();
1385 return err;
1388 #define MAX_SERVICE_NAME 260
1390 static DWORD query_perf_data( const WCHAR *query, DWORD *type, void *data, DWORD *ret_size, BOOL unicode )
1392 DWORD err, i, data_size;
1393 HKEY root;
1394 PERF_DATA_BLOCK *pdb;
1396 if (!ret_size)
1397 return ERROR_INVALID_PARAMETER;
1399 if (!wcsnicmp( query, L"counter", 7 ))
1400 return query_perf_names( type, data, ret_size, unicode );
1401 if (!wcsnicmp( query, L"help", 4 ))
1402 return query_perf_help( type, data, ret_size, unicode );
1404 data_size = *ret_size;
1405 *ret_size = 0;
1407 if (type)
1408 *type = REG_BINARY;
1410 if (!data || data_size < sizeof(*pdb))
1411 return ERROR_MORE_DATA;
1413 pdb = data;
1415 pdb->Signature[0] = 'P';
1416 pdb->Signature[1] = 'E';
1417 pdb->Signature[2] = 'R';
1418 pdb->Signature[3] = 'F';
1419 #ifdef WORDS_BIGENDIAN
1420 pdb->LittleEndian = FALSE;
1421 #else
1422 pdb->LittleEndian = TRUE;
1423 #endif
1424 pdb->Version = PERF_DATA_VERSION;
1425 pdb->Revision = PERF_DATA_REVISION;
1426 pdb->TotalByteLength = 0;
1427 pdb->HeaderLength = sizeof(*pdb);
1428 pdb->NumObjectTypes = 0;
1429 pdb->DefaultObject = 0;
1430 NtQueryPerformanceCounter( &pdb->PerfTime, &pdb->PerfFreq );
1432 data = pdb + 1;
1433 pdb->SystemNameOffset = sizeof(*pdb);
1434 pdb->SystemNameLength = (data_size - sizeof(*pdb)) / sizeof(WCHAR);
1435 if (!GetComputerNameExW(ComputerNameNetBIOS, data, &pdb->SystemNameLength))
1436 return ERROR_MORE_DATA;
1438 pdb->SystemNameLength++;
1439 pdb->SystemNameLength *= sizeof(WCHAR);
1441 pdb->HeaderLength += pdb->SystemNameLength;
1443 /* align to 8 bytes */
1444 if (pdb->SystemNameLength & 7)
1445 pdb->HeaderLength += 8 - (pdb->SystemNameLength & 7);
1447 if (data_size < pdb->HeaderLength)
1448 return ERROR_MORE_DATA;
1450 pdb->TotalByteLength = pdb->HeaderLength;
1452 data_size -= pdb->HeaderLength;
1453 data = (char *)data + pdb->HeaderLength;
1455 err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services", 0, KEY_READ, &root);
1456 if (err != ERROR_SUCCESS)
1457 return err;
1459 i = 0;
1460 for (;;)
1462 DWORD collected_size = data_size, obj_count = 0;
1463 struct perf_provider provider;
1464 WCHAR name[MAX_SERVICE_NAME];
1465 DWORD len = ARRAY_SIZE( name );
1466 void *collected_data = data;
1468 err = RegEnumKeyExW(root, i++, name, &len, NULL, NULL, NULL, NULL);
1469 if (err == ERROR_NO_MORE_ITEMS)
1471 err = ERROR_SUCCESS;
1472 break;
1475 if (err != ERROR_SUCCESS)
1476 continue;
1478 if (!load_provider(root, name, &provider))
1479 continue;
1481 err = collect_data(&provider, query, &collected_data, &collected_size, &obj_count);
1482 FreeLibrary(provider.perflib);
1484 if (err == ERROR_MORE_DATA)
1485 break;
1487 if (err == ERROR_SUCCESS)
1489 PERF_OBJECT_TYPE *obj = (PERF_OBJECT_TYPE *)data;
1491 TRACE("Collect: obj->TotalByteLength %lu, collected_size %lu\n",
1492 obj->TotalByteLength, collected_size);
1494 data_size -= collected_size;
1495 data = collected_data;
1497 pdb->TotalByteLength += collected_size;
1498 pdb->NumObjectTypes += obj_count;
1502 RegCloseKey(root);
1504 if (err == ERROR_SUCCESS)
1506 *ret_size = pdb->TotalByteLength;
1508 GetSystemTime(&pdb->SystemTime);
1509 GetSystemTimeAsFileTime((FILETIME *)&pdb->PerfTime100nSec);
1512 return err;
1515 /******************************************************************************
1516 * RegQueryValueExW (kernelbase.@)
1518 * See RegQueryValueExA.
1520 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
1521 LPBYTE data, LPDWORD count )
1523 NTSTATUS status;
1524 UNICODE_STRING name_str;
1525 DWORD total_size;
1526 char buffer[256], *buf_ptr = buffer;
1527 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1528 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1530 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1531 hkey, debugstr_w(name), reserved, type, data, count,
1532 (count && data) ? *count : 0 );
1534 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1536 if (is_perf_key( hkey ))
1537 return query_perf_data( name, type, data, count, TRUE );
1539 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
1541 RtlInitUnicodeString( &name_str, name );
1543 if (data) total_size = min( sizeof(buffer), *count + info_size );
1544 else
1546 total_size = info_size;
1547 if (count) *count = 0;
1550 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1551 buffer, total_size, &total_size );
1552 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1554 if (data)
1556 /* retry with a dynamically allocated buffer */
1557 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
1559 if (buf_ptr != buffer) heap_free( buf_ptr );
1560 if (!(buf_ptr = heap_alloc( total_size )))
1561 return ERROR_NOT_ENOUGH_MEMORY;
1562 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1563 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
1564 buf_ptr, total_size, &total_size );
1567 if (!status)
1569 memcpy( data, buf_ptr + info_size, total_size - info_size );
1570 /* if the type is REG_SZ and data is not 0-terminated
1571 * and there is enough space in the buffer NT appends a \0 */
1572 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
1574 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
1575 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1578 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
1580 else status = STATUS_SUCCESS;
1582 if (type) *type = info->Type;
1583 if (count) *count = total_size - info_size;
1585 done:
1586 if (buf_ptr != buffer) heap_free( buf_ptr );
1587 return RtlNtStatusToDosError(status);
1591 /******************************************************************************
1592 * RegQueryValueExA (kernelbase.@)
1594 * Get the type and contents of a specified value under with a key.
1596 * PARAMS
1597 * hkey [I] Handle of the key to query
1598 * name [I] Name of value under hkey to query
1599 * reserved [I] Reserved - must be NULL
1600 * type [O] Destination for the value type, or NULL if not required
1601 * data [O] Destination for the values contents, or NULL if not required
1602 * count [I/O] Size of data, updated with the number of bytes returned
1604 * RETURNS
1605 * Success: ERROR_SUCCESS. *count is updated with the number of bytes copied to data.
1606 * Failure: ERROR_INVALID_HANDLE, if hkey is invalid.
1607 * ERROR_INVALID_PARAMETER, if any other parameter is invalid.
1608 * ERROR_MORE_DATA, if on input *count is too small to hold the contents.
1610 * NOTES
1611 * MSDN states that if data is too small it is partially filled. In reality
1612 * it remains untouched.
1614 LSTATUS WINAPI DECLSPEC_HOTPATCH RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved,
1615 LPDWORD type, LPBYTE data, LPDWORD count )
1617 NTSTATUS status;
1618 ANSI_STRING nameA;
1619 UNICODE_STRING nameW;
1620 DWORD total_size, datalen = 0;
1621 char buffer[256], *buf_ptr = buffer;
1622 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1623 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
1625 TRACE("(%p,%s,%p,%p,%p,%p=%ld)\n",
1626 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
1628 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1629 if (!(hkey = get_special_root_hkey( hkey, 0 )))
1630 return ERROR_INVALID_HANDLE;
1632 if (count) datalen = *count;
1633 if (!data && count) *count = 0;
1635 /* this matches Win9x behaviour - NT sets *type to a random value */
1636 if (type) *type = REG_NONE;
1638 RtlInitAnsiString( &nameA, name );
1639 if ((status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
1640 return RtlNtStatusToDosError(status);
1642 if (is_perf_key( hkey ))
1644 DWORD ret = query_perf_data( nameW.Buffer, type, data, count, FALSE );
1645 RtlFreeUnicodeString( &nameW );
1646 return ret;
1649 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1650 buffer, sizeof(buffer), &total_size );
1651 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1653 /* we need to fetch the contents for a string type even if not requested,
1654 * because we need to compute the length of the ANSI string. */
1655 if (data || is_string(info->Type))
1657 /* retry with a dynamically allocated buffer */
1658 while (status == STATUS_BUFFER_OVERFLOW)
1660 if (buf_ptr != buffer) heap_free( buf_ptr );
1661 if (!(buf_ptr = heap_alloc( total_size )))
1663 status = STATUS_NO_MEMORY;
1664 goto done;
1666 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1667 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
1668 buf_ptr, total_size, &total_size );
1671 if (status) goto done;
1673 if (is_string(info->Type))
1675 DWORD len;
1677 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1678 total_size - info_size );
1679 if (data && len)
1681 if (len > datalen) status = STATUS_BUFFER_OVERFLOW;
1682 else
1684 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1685 total_size - info_size );
1686 /* if the type is REG_SZ and data is not 0-terminated
1687 * and there is enough space in the buffer NT appends a \0 */
1688 if (len < datalen && data[len-1]) data[len] = 0;
1691 total_size = len + info_size;
1693 else if (data)
1695 if (total_size - info_size > datalen) status = STATUS_BUFFER_OVERFLOW;
1696 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1699 else status = STATUS_SUCCESS;
1701 if (type) *type = info->Type;
1702 if (count) *count = total_size - info_size;
1704 done:
1705 if (buf_ptr != buffer) heap_free( buf_ptr );
1706 RtlFreeUnicodeString( &nameW );
1707 return RtlNtStatusToDosError(status);
1711 /******************************************************************************
1712 * apply_restrictions [internal]
1714 * Helper function for RegGetValueA/W.
1716 static void apply_restrictions( DWORD dwFlags, DWORD dwType, DWORD cbData, PLONG ret )
1718 /* Check if the type is restricted by the passed flags */
1719 if (*ret == ERROR_SUCCESS || *ret == ERROR_MORE_DATA)
1721 DWORD dwMask = 0;
1723 switch (dwType)
1725 case REG_NONE: dwMask = RRF_RT_REG_NONE; break;
1726 case REG_SZ: dwMask = RRF_RT_REG_SZ; break;
1727 case REG_EXPAND_SZ: dwMask = RRF_RT_REG_EXPAND_SZ; break;
1728 case REG_MULTI_SZ: dwMask = RRF_RT_REG_MULTI_SZ; break;
1729 case REG_BINARY: dwMask = RRF_RT_REG_BINARY; break;
1730 case REG_DWORD: dwMask = RRF_RT_REG_DWORD; break;
1731 case REG_QWORD: dwMask = RRF_RT_REG_QWORD; break;
1734 if (dwFlags & dwMask)
1736 /* Type is not restricted, check for size mismatch */
1737 if (dwType == REG_BINARY)
1739 DWORD cbExpect = 0;
1741 if ((dwFlags & RRF_RT_ANY) == RRF_RT_DWORD)
1742 cbExpect = 4;
1743 else if ((dwFlags & RRF_RT_ANY) == RRF_RT_QWORD)
1744 cbExpect = 8;
1746 if (cbExpect && cbData != cbExpect)
1747 *ret = ERROR_DATATYPE_MISMATCH;
1750 else *ret = ERROR_UNSUPPORTED_TYPE;
1755 /******************************************************************************
1756 * RegGetValueW (kernelbase.@)
1758 * Retrieves the type and data for a value name associated with a key,
1759 * optionally expanding its content and restricting its type.
1761 * PARAMS
1762 * hKey [I] Handle to an open key.
1763 * pszSubKey [I] Name of the subkey of hKey.
1764 * pszValue [I] Name of value under hKey/szSubKey to query.
1765 * dwFlags [I] Flags restricting the value type to retrieve.
1766 * pdwType [O] Destination for the values type, may be NULL.
1767 * pvData [O] Destination for the values content, may be NULL.
1768 * pcbData [I/O] Size of pvData, updated with the size in bytes required to
1769 * retrieve the whole content, including the trailing '\0'
1770 * for strings.
1772 * RETURNS
1773 * Success: ERROR_SUCCESS
1774 * Failure: nonzero error code from Winerror.h
1776 * NOTES
1777 * - Unless RRF_NOEXPAND is specified, REG_EXPAND_SZ values are automatically
1778 * expanded and pdwType is set to REG_SZ instead.
1779 * - Restrictions are applied after expanding, using RRF_RT_REG_EXPAND_SZ
1780 * without RRF_NOEXPAND is thus not allowed.
1781 * An exception is the case where RRF_RT_ANY is specified, because then
1782 * RRF_NOEXPAND is allowed.
1784 LSTATUS WINAPI RegGetValueW( HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue,
1785 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1786 LPDWORD pcbData )
1788 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1789 PVOID pvBuf = NULL;
1790 LONG ret;
1792 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1793 hKey, debugstr_w(pszSubKey), debugstr_w(pszValue), dwFlags, pdwType,
1794 pvData, pcbData, cbData);
1796 if (pvData && !pcbData)
1797 return ERROR_INVALID_PARAMETER;
1799 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1800 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1801 return ERROR_INVALID_PARAMETER;
1803 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1804 return ERROR_INVALID_PARAMETER;
1806 if (pszSubKey && pszSubKey[0])
1808 REGSAM samDesired = KEY_QUERY_VALUE;
1810 if (dwFlags & RRF_WOW64_MASK)
1811 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1813 ret = RegOpenKeyExW(hKey, pszSubKey, 0, samDesired, &hKey);
1814 if (ret != ERROR_SUCCESS) return ret;
1817 ret = RegQueryValueExW(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1819 /* If we are going to expand we need to read in the whole the value even
1820 * if the passed buffer was too small as the expanded string might be
1821 * smaller than the unexpanded one and could fit into cbData bytes. */
1822 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1823 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1825 do {
1826 heap_free(pvBuf);
1828 pvBuf = heap_alloc(cbData);
1829 if (!pvBuf)
1831 ret = ERROR_NOT_ENOUGH_MEMORY;
1832 break;
1835 if (ret == ERROR_MORE_DATA || !pvData)
1836 ret = RegQueryValueExW(hKey, pszValue, NULL,
1837 &dwType, pvBuf, &cbData);
1838 else
1840 /* Even if cbData was large enough we have to copy the
1841 * string since ExpandEnvironmentStrings can't handle
1842 * overlapping buffers. */
1843 CopyMemory(pvBuf, pvData, cbData);
1846 /* Both the type or the value itself could have been modified in
1847 * between so we have to keep retrying until the buffer is large
1848 * enough or we no longer have to expand the value. */
1849 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1851 if (ret == ERROR_SUCCESS)
1853 /* Recheck dwType in case it changed since the first call */
1854 if (dwType == REG_EXPAND_SZ)
1856 cbData = ExpandEnvironmentStringsW(pvBuf, pvData,
1857 pcbData ? *pcbData : 0) * sizeof(WCHAR);
1858 dwType = REG_SZ;
1859 if(pvData && pcbData && cbData > *pcbData)
1860 ret = ERROR_MORE_DATA;
1862 else if (pvData)
1863 CopyMemory(pvData, pvBuf, *pcbData);
1866 heap_free(pvBuf);
1869 if (pszSubKey && pszSubKey[0])
1870 RegCloseKey(hKey);
1872 apply_restrictions(dwFlags, dwType, cbData, &ret);
1874 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1875 ZeroMemory(pvData, *pcbData);
1877 if (pdwType) *pdwType = dwType;
1878 if (pcbData) *pcbData = cbData;
1880 return ret;
1884 /******************************************************************************
1885 * RegGetValueA (kernelbase.@)
1887 * See RegGetValueW.
1889 LSTATUS WINAPI RegGetValueA( HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue,
1890 DWORD dwFlags, LPDWORD pdwType, PVOID pvData,
1891 LPDWORD pcbData )
1893 DWORD dwType, cbData = (pvData && pcbData) ? *pcbData : 0;
1894 PVOID pvBuf = NULL;
1895 LONG ret;
1897 TRACE("(%p,%s,%s,%ld,%p,%p,%p=%ld)\n",
1898 hKey, debugstr_a(pszSubKey), debugstr_a(pszValue), dwFlags,
1899 pdwType, pvData, pcbData, cbData);
1901 if (pvData && !pcbData)
1902 return ERROR_INVALID_PARAMETER;
1904 if ((dwFlags & RRF_RT_REG_EXPAND_SZ) && !(dwFlags & RRF_NOEXPAND) &&
1905 ((dwFlags & RRF_RT_ANY) != RRF_RT_ANY))
1906 return ERROR_INVALID_PARAMETER;
1908 if ((dwFlags & RRF_WOW64_MASK) == RRF_WOW64_MASK)
1909 return ERROR_INVALID_PARAMETER;
1911 if (pszSubKey && pszSubKey[0])
1913 REGSAM samDesired = KEY_QUERY_VALUE;
1915 if (dwFlags & RRF_WOW64_MASK)
1916 samDesired |= (dwFlags & RRF_SUBKEY_WOW6432KEY) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
1918 ret = RegOpenKeyExA(hKey, pszSubKey, 0, samDesired, &hKey);
1919 if (ret != ERROR_SUCCESS) return ret;
1922 ret = RegQueryValueExA(hKey, pszValue, NULL, &dwType, pvData, &cbData);
1924 /* If we are going to expand we need to read in the whole the value even
1925 * if the passed buffer was too small as the expanded string might be
1926 * smaller than the unexpanded one and could fit into cbData bytes. */
1927 if ((ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) &&
1928 dwType == REG_EXPAND_SZ && !(dwFlags & RRF_NOEXPAND))
1930 do {
1931 heap_free(pvBuf);
1933 pvBuf = heap_alloc(cbData);
1934 if (!pvBuf)
1936 ret = ERROR_NOT_ENOUGH_MEMORY;
1937 break;
1940 if (ret == ERROR_MORE_DATA || !pvData)
1941 ret = RegQueryValueExA(hKey, pszValue, NULL,
1942 &dwType, pvBuf, &cbData);
1943 else
1945 /* Even if cbData was large enough we have to copy the
1946 * string since ExpandEnvironmentStrings can't handle
1947 * overlapping buffers. */
1948 CopyMemory(pvBuf, pvData, cbData);
1951 /* Both the type or the value itself could have been modified in
1952 * between so we have to keep retrying until the buffer is large
1953 * enough or we no longer have to expand the value. */
1954 } while (dwType == REG_EXPAND_SZ && ret == ERROR_MORE_DATA);
1956 if (ret == ERROR_SUCCESS)
1958 /* Recheck dwType in case it changed since the first call */
1959 if (dwType == REG_EXPAND_SZ)
1961 cbData = ExpandEnvironmentStringsA(pvBuf, pvData,
1962 pcbData ? *pcbData : 0);
1963 dwType = REG_SZ;
1964 if(pvData && pcbData && cbData > *pcbData)
1965 ret = ERROR_MORE_DATA;
1967 else if (pvData)
1968 CopyMemory(pvData, pvBuf, *pcbData);
1971 heap_free(pvBuf);
1974 if (pszSubKey && pszSubKey[0])
1975 RegCloseKey(hKey);
1977 apply_restrictions(dwFlags, dwType, cbData, &ret);
1979 if (pvData && ret != ERROR_SUCCESS && (dwFlags & RRF_ZEROONFAILURE))
1980 ZeroMemory(pvData, *pcbData);
1982 if (pdwType) *pdwType = dwType;
1983 if (pcbData) *pcbData = cbData;
1985 return ret;
1989 /******************************************************************************
1990 * RegEnumValueW (kernelbase.@)
1992 * Enumerates the values for the specified open registry key.
1994 * PARAMS
1995 * hkey [I] Handle to key to query
1996 * index [I] Index of value to query
1997 * value [O] Value string
1998 * val_count [I/O] Size of value buffer (in wchars)
1999 * reserved [I] Reserved
2000 * type [O] Type code
2001 * data [O] Value data
2002 * count [I/O] Size of data buffer (in bytes)
2004 * RETURNS
2005 * Success: ERROR_SUCCESS
2006 * Failure: nonzero error code from Winerror.h
2008 LSTATUS WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
2009 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
2011 NTSTATUS status;
2012 DWORD total_size;
2013 char buffer[256], *buf_ptr = buffer;
2014 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2015 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
2017 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2018 hkey, index, value, val_count, reserved, type, data, count );
2020 if ((data && !count) || reserved || !value || !val_count)
2021 return ERROR_INVALID_PARAMETER;
2022 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2024 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
2025 if (data) total_size += *count;
2026 total_size = min( sizeof(buffer), total_size );
2028 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2029 buffer, total_size, &total_size );
2031 /* retry with a dynamically allocated buffer */
2032 while (status == STATUS_BUFFER_OVERFLOW)
2034 if (buf_ptr != buffer) heap_free( buf_ptr );
2035 if (!(buf_ptr = heap_alloc( total_size )))
2036 return ERROR_NOT_ENOUGH_MEMORY;
2037 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
2038 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2039 buf_ptr, total_size, &total_size );
2042 if (status) goto done;
2044 if (info->NameLength/sizeof(WCHAR) >= *val_count)
2046 status = STATUS_BUFFER_OVERFLOW;
2047 goto overflow;
2049 memcpy( value, info->Name, info->NameLength );
2050 *val_count = info->NameLength / sizeof(WCHAR);
2051 value[*val_count] = 0;
2053 if (data)
2055 if (total_size - info->DataOffset > *count)
2057 status = STATUS_BUFFER_OVERFLOW;
2058 goto overflow;
2060 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2061 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
2063 /* if the type is REG_SZ and data is not 0-terminated
2064 * and there is enough space in the buffer NT appends a \0 */
2065 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
2066 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
2070 overflow:
2071 if (type) *type = info->Type;
2072 if (count) *count = info->DataLength;
2074 done:
2075 if (buf_ptr != buffer) heap_free( buf_ptr );
2076 return RtlNtStatusToDosError(status);
2080 /******************************************************************************
2081 * RegEnumValueA (kernelbase.@)
2083 * See RegEnumValueW.
2085 LSTATUS WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
2086 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
2088 NTSTATUS status;
2089 DWORD total_size;
2090 char buffer[256], *buf_ptr = buffer;
2091 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2092 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
2094 TRACE("(%p,%ld,%p,%p,%p,%p,%p,%p)\n",
2095 hkey, index, value, val_count, reserved, type, data, count );
2097 if ((data && !count) || reserved || !value || !val_count)
2098 return ERROR_INVALID_PARAMETER;
2099 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2101 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
2102 if (data) total_size += *count;
2103 total_size = min( sizeof(buffer), total_size );
2105 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2106 buffer, total_size, &total_size );
2108 /* we need to fetch the contents for a string type even if not requested,
2109 * because we need to compute the length of the ANSI string. */
2111 /* retry with a dynamically allocated buffer */
2112 while (status == STATUS_BUFFER_OVERFLOW)
2114 if (buf_ptr != buffer) heap_free( buf_ptr );
2115 if (!(buf_ptr = heap_alloc( total_size )))
2116 return ERROR_NOT_ENOUGH_MEMORY;
2117 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
2118 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
2119 buf_ptr, total_size, &total_size );
2122 if (status) goto done;
2124 if (is_string(info->Type))
2126 DWORD len;
2127 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
2128 total_size - info->DataOffset );
2129 if (data && len)
2131 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
2132 else
2134 RtlUnicodeToMultiByteN( (char*)data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
2135 total_size - info->DataOffset );
2136 /* if the type is REG_SZ and data is not 0-terminated
2137 * and there is enough space in the buffer NT appends a \0 */
2138 if (len < *count && data[len-1]) data[len] = 0;
2141 info->DataLength = len;
2143 else if (data)
2145 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
2146 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
2149 if (!status)
2151 DWORD len;
2153 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
2154 if (len >= *val_count)
2156 status = STATUS_BUFFER_OVERFLOW;
2157 if (*val_count)
2159 len = *val_count - 1;
2160 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2161 value[len] = 0;
2164 else
2166 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
2167 value[len] = 0;
2168 *val_count = len;
2172 if (type) *type = info->Type;
2173 if (count) *count = info->DataLength;
2175 done:
2176 if (buf_ptr != buffer) heap_free( buf_ptr );
2177 return RtlNtStatusToDosError(status);
2180 /******************************************************************************
2181 * RegDeleteValueW (kernelbase.@)
2183 * See RegDeleteValueA.
2185 LSTATUS WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
2187 return RegDeleteKeyValueW( hkey, NULL, name );
2190 /******************************************************************************
2191 * RegDeleteValueA (kernelbase.@)
2193 * Delete a value from the registry.
2195 * PARAMS
2196 * hkey [I] Registry handle of the key holding the value
2197 * name [I] Name of the value under hkey to delete
2199 * RETURNS
2200 * Success: ERROR_SUCCESS
2201 * Failure: nonzero error code from Winerror.h
2203 LSTATUS WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
2205 return RegDeleteKeyValueA( hkey, NULL, name );
2208 /******************************************************************************
2209 * RegDeleteKeyValueW (kernelbase.@)
2211 LONG WINAPI RegDeleteKeyValueW( HKEY hkey, LPCWSTR subkey, LPCWSTR name )
2213 UNICODE_STRING nameW;
2214 HKEY hsubkey = 0;
2215 LONG ret;
2217 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2219 if (subkey)
2221 if ((ret = RegOpenKeyExW( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey )))
2222 return ret;
2223 hkey = hsubkey;
2226 RtlInitUnicodeString( &nameW, name );
2227 ret = RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
2228 if (hsubkey) RegCloseKey( hsubkey );
2229 return ret;
2232 /******************************************************************************
2233 * RegDeleteKeyValueA (kernelbase.@)
2235 LONG WINAPI RegDeleteKeyValueA( HKEY hkey, LPCSTR subkey, LPCSTR name )
2237 UNICODE_STRING nameW;
2238 HKEY hsubkey = 0;
2239 ANSI_STRING nameA;
2240 NTSTATUS status;
2242 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2244 if (subkey)
2246 LONG ret = RegOpenKeyExA( hkey, subkey, 0, KEY_SET_VALUE, &hsubkey );
2247 if (ret)
2248 return ret;
2249 hkey = hsubkey;
2252 RtlInitAnsiString( &nameA, name );
2253 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
2255 status = NtDeleteValueKey( hkey, &nameW );
2256 RtlFreeUnicodeString( &nameW );
2259 if (hsubkey) RegCloseKey( hsubkey );
2260 return RtlNtStatusToDosError( status );
2263 /******************************************************************************
2264 * RegLoadKeyW (kernelbase.@)
2266 * Create a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and store
2267 * registration information from a specified file into that subkey.
2269 * PARAMS
2270 * hkey [I] Handle of open key
2271 * subkey [I] Address of name of subkey
2272 * filename [I] Address of filename for registry information
2274 * RETURNS
2275 * Success: ERROR_SUCCESS
2276 * Failure: nonzero error code from Winerror.h
2278 LSTATUS WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
2280 OBJECT_ATTRIBUTES destkey, file;
2281 UNICODE_STRING subkeyW, filenameW;
2282 NTSTATUS status;
2284 if (!(hkey = get_special_root_hkey(hkey, 0))) return ERROR_INVALID_HANDLE;
2286 destkey.Length = sizeof(destkey);
2287 destkey.RootDirectory = hkey; /* root key: HKLM or HKU */
2288 destkey.ObjectName = &subkeyW; /* name of the key */
2289 destkey.Attributes = 0;
2290 destkey.SecurityDescriptor = NULL;
2291 destkey.SecurityQualityOfService = NULL;
2292 RtlInitUnicodeString(&subkeyW, subkey);
2294 file.Length = sizeof(file);
2295 file.RootDirectory = NULL;
2296 file.ObjectName = &filenameW; /* file containing the hive */
2297 file.Attributes = OBJ_CASE_INSENSITIVE;
2298 file.SecurityDescriptor = NULL;
2299 file.SecurityQualityOfService = NULL;
2300 RtlDosPathNameToNtPathName_U(filename, &filenameW, NULL, NULL);
2302 status = NtLoadKey(&destkey, &file);
2303 RtlFreeUnicodeString(&filenameW);
2304 return RtlNtStatusToDosError( status );
2308 /******************************************************************************
2309 * RegLoadKeyA (kernelbase.@)
2311 * See RegLoadKeyW.
2313 LSTATUS WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
2315 UNICODE_STRING subkeyW, filenameW;
2316 STRING subkeyA, filenameA;
2317 NTSTATUS status;
2318 LONG ret;
2320 RtlInitAnsiString(&subkeyA, subkey);
2321 RtlInitAnsiString(&filenameA, filename);
2323 RtlInitUnicodeString(&subkeyW, NULL);
2324 RtlInitUnicodeString(&filenameW, NULL);
2325 if (!(status = RtlAnsiStringToUnicodeString(&subkeyW, &subkeyA, TRUE)) &&
2326 !(status = RtlAnsiStringToUnicodeString(&filenameW, &filenameA, TRUE)))
2328 ret = RegLoadKeyW(hkey, subkeyW.Buffer, filenameW.Buffer);
2330 else ret = RtlNtStatusToDosError(status);
2331 RtlFreeUnicodeString(&subkeyW);
2332 RtlFreeUnicodeString(&filenameW);
2333 return ret;
2337 /******************************************************************************
2338 * RegSaveKeyExW (kernelbase.@)
2340 LSTATUS WINAPI RegSaveKeyExW( HKEY hkey, LPCWSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2342 UNICODE_STRING nameW;
2343 OBJECT_ATTRIBUTES attr;
2344 IO_STATUS_BLOCK io;
2345 NTSTATUS status;
2346 HANDLE handle;
2348 TRACE( "(%p,%s,%p)\n", hkey, debugstr_w(file), sa );
2350 if (!file || !*file) return ERROR_INVALID_PARAMETER;
2351 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2353 if ((status = RtlDosPathNameToNtPathName_U_WithStatus( file, &nameW, NULL, NULL )))
2354 return RtlNtStatusToDosError( status );
2356 InitializeObjectAttributes( &attr, &nameW, OBJ_CASE_INSENSITIVE, 0, sa );
2357 status = NtCreateFile( &handle, GENERIC_WRITE | SYNCHRONIZE, &attr, &io, NULL, FILE_NON_DIRECTORY_FILE,
2358 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
2359 FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
2360 RtlFreeUnicodeString( &nameW );
2361 if (!status)
2363 status = NtSaveKey( hkey, handle );
2364 CloseHandle( handle );
2366 return RtlNtStatusToDosError( status );
2370 /******************************************************************************
2371 * RegSaveKeyExA (kernelbase.@)
2373 LSTATUS WINAPI RegSaveKeyExA( HKEY hkey, LPCSTR file, SECURITY_ATTRIBUTES *sa, DWORD flags )
2375 UNICODE_STRING *fileW = &NtCurrentTeb()->StaticUnicodeString;
2376 NTSTATUS status;
2377 STRING fileA;
2379 RtlInitAnsiString(&fileA, file);
2380 if ((status = RtlAnsiStringToUnicodeString(fileW, &fileA, FALSE)))
2381 return RtlNtStatusToDosError( status );
2382 return RegSaveKeyExW(hkey, fileW->Buffer, sa, flags);
2386 /******************************************************************************
2387 * RegRestoreKeyW (kernelbase.@)
2389 * Read the registry information from a file and copy it over a key.
2391 * PARAMS
2392 * hkey [I] Handle of key where restore begins
2393 * lpFile [I] Address of filename containing saved tree
2394 * dwFlags [I] Optional flags
2396 * RETURNS
2397 * Success: ERROR_SUCCESS
2398 * Failure: nonzero error code from Winerror.h
2400 LSTATUS WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
2402 TRACE("(%p,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
2404 /* It seems to do this check before the hkey check */
2405 if (!lpFile || !*lpFile)
2406 return ERROR_INVALID_PARAMETER;
2408 FIXME("(%p,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
2410 /* Check for file existence */
2412 return ERROR_SUCCESS;
2416 /******************************************************************************
2417 * RegRestoreKeyA (kernelbase.@)
2419 * See RegRestoreKeyW.
2421 LSTATUS WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
2423 UNICODE_STRING lpFileW;
2424 LONG ret;
2426 RtlCreateUnicodeStringFromAsciiz( &lpFileW, lpFile );
2427 ret = RegRestoreKeyW( hkey, lpFileW.Buffer, dwFlags );
2428 RtlFreeUnicodeString( &lpFileW );
2429 return ret;
2433 /******************************************************************************
2434 * RegUnLoadKeyW (kernelbase.@)
2436 * Unload a registry key and its subkeys from the registry.
2438 * PARAMS
2439 * hkey [I] Handle of open key
2440 * lpSubKey [I] Address of name of subkey to unload
2442 * RETURNS
2443 * Success: ERROR_SUCCESS
2444 * Failure: nonzero error code from Winerror.h
2446 LSTATUS WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
2448 OBJECT_ATTRIBUTES attr;
2449 UNICODE_STRING subkey;
2451 TRACE("(%p,%s)\n",hkey, debugstr_w(lpSubKey));
2453 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2455 RtlInitUnicodeString(&subkey, lpSubKey);
2456 InitializeObjectAttributes(&attr, &subkey, OBJ_CASE_INSENSITIVE, hkey, NULL);
2457 return RtlNtStatusToDosError( NtUnloadKey(&attr) );
2461 /******************************************************************************
2462 * RegUnLoadKeyA (kernelbase.@)
2464 * See RegUnLoadKeyW.
2466 LSTATUS WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
2468 UNICODE_STRING lpSubKeyW;
2469 LONG ret;
2471 RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, lpSubKey );
2472 ret = RegUnLoadKeyW( hkey, lpSubKeyW.Buffer );
2473 RtlFreeUnicodeString( &lpSubKeyW );
2474 return ret;
2478 /******************************************************************************
2479 * RegSetKeySecurity (kernelbase.@)
2481 * Set the security of an open registry key.
2483 * PARAMS
2484 * hkey [I] Open handle of key to set
2485 * SecurityInfo [I] Descriptor contents
2486 * pSecurityDesc [I] Address of descriptor for key
2488 * RETURNS
2489 * Success: ERROR_SUCCESS
2490 * Failure: nonzero error code from Winerror.h
2492 LSTATUS WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
2493 PSECURITY_DESCRIPTOR pSecurityDesc )
2495 TRACE("(%p,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
2497 /* It seems to perform this check before the hkey check */
2498 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
2499 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
2500 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
2501 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
2502 /* Param OK */
2503 } else
2504 return ERROR_INVALID_PARAMETER;
2506 if (!pSecurityDesc)
2507 return ERROR_INVALID_PARAMETER;
2509 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2511 return RtlNtStatusToDosError( NtSetSecurityObject( hkey, SecurityInfo, pSecurityDesc ) );
2515 /******************************************************************************
2516 * RegGetKeySecurity (kernelbase.@)
2518 * Get a copy of the security descriptor for a given registry key.
2520 * PARAMS
2521 * hkey [I] Open handle of key to set
2522 * SecurityInformation [I] Descriptor contents
2523 * pSecurityDescriptor [O] Address of descriptor for key
2524 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
2526 * RETURNS
2527 * Success: ERROR_SUCCESS
2528 * Failure: Error code
2530 LSTATUS WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
2531 PSECURITY_DESCRIPTOR pSecurityDescriptor,
2532 LPDWORD lpcbSecurityDescriptor )
2534 TRACE("(%p,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
2535 *lpcbSecurityDescriptor);
2537 if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
2539 return RtlNtStatusToDosError( NtQuerySecurityObject( hkey,
2540 SecurityInformation, pSecurityDescriptor,
2541 *lpcbSecurityDescriptor, lpcbSecurityDescriptor ) );
2545 /******************************************************************************
2546 * RegFlushKey (kernelbase.@)
2548 * Immediately write a registry key to registry.
2550 * PARAMS
2551 * hkey [I] Handle of key to write
2553 * RETURNS
2554 * Success: ERROR_SUCCESS
2555 * Failure: Error code
2557 LSTATUS WINAPI RegFlushKey( HKEY hkey )
2559 hkey = get_special_root_hkey( hkey, 0 );
2560 if (!hkey) return ERROR_INVALID_HANDLE;
2562 return RtlNtStatusToDosError( NtFlushKey( hkey ) );
2566 /******************************************************************************
2567 * RegNotifyChangeKeyValue (kernelbase.@)
2569 * Notify the caller about changes to the attributes or contents of a registry key.
2571 * PARAMS
2572 * hkey [I] Handle of key to watch
2573 * fWatchSubTree [I] Flag for subkey notification
2574 * fdwNotifyFilter [I] Changes to be reported
2575 * hEvent [I] Handle of signaled event
2576 * fAsync [I] Flag for asynchronous reporting
2578 * RETURNS
2579 * Success: ERROR_SUCCESS
2580 * Failure: nonzero error code from Winerror.h
2582 LSTATUS WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
2583 DWORD fdwNotifyFilter, HANDLE hEvent,
2584 BOOL fAsync )
2586 NTSTATUS status;
2587 IO_STATUS_BLOCK iosb;
2589 hkey = get_special_root_hkey( hkey, 0 );
2590 if (!hkey) return ERROR_INVALID_HANDLE;
2592 TRACE("(%p,%i,%ld,%p,%i)\n", hkey, fWatchSubTree, fdwNotifyFilter,
2593 hEvent, fAsync);
2595 status = NtNotifyChangeKey( hkey, hEvent, NULL, NULL, &iosb,
2596 fdwNotifyFilter, fWatchSubTree, NULL, 0,
2597 fAsync);
2599 if (status && status != STATUS_PENDING)
2600 return RtlNtStatusToDosError( status );
2602 return ERROR_SUCCESS;
2605 /******************************************************************************
2606 * RegOpenUserClassesRoot (kernelbase.@)
2608 * Open the HKEY_CLASSES_ROOT key for a user.
2610 * PARAMS
2611 * hToken [I] Handle of token representing the user
2612 * dwOptions [I] Reserved, must be 0
2613 * samDesired [I] Desired access rights
2614 * phkResult [O] Destination for the resulting key handle
2616 * RETURNS
2617 * Success: ERROR_SUCCESS
2618 * Failure: nonzero error code from Winerror.h
2620 * NOTES
2621 * On Windows 2000 and upwards the HKEY_CLASSES_ROOT key is a view of the
2622 * "HKEY_LOCAL_MACHINE\Software\Classes" and the
2623 * "HKEY_CURRENT_USER\Software\Classes" keys merged together.
2625 LSTATUS WINAPI RegOpenUserClassesRoot( HANDLE hToken, DWORD dwOptions, REGSAM samDesired, PHKEY phkResult )
2627 FIXME("(%p, 0x%lx, 0x%lx, %p) semi-stub\n", hToken, dwOptions, samDesired, phkResult);
2629 *phkResult = HKEY_CLASSES_ROOT;
2630 return ERROR_SUCCESS;
2634 static void dump_mui_cache(void)
2636 struct mui_cache_entry *ent;
2638 TRACE("---------- MUI Cache ----------\n");
2639 LIST_FOR_EACH_ENTRY( ent, &reg_mui_cache, struct mui_cache_entry, entry )
2640 TRACE("entry=%p, %s,-%lu [%#lx] => %s\n",
2641 ent, wine_dbgstr_w(ent->file_name), ent->index, ent->locale, wine_dbgstr_w(ent->text));
2644 static inline void free_mui_cache_entry(struct mui_cache_entry *ent)
2646 heap_free(ent->file_name);
2647 heap_free(ent->text);
2648 heap_free(ent);
2651 /* critical section must be held */
2652 static int reg_mui_cache_get(const WCHAR *file_name, UINT index, WCHAR **buffer)
2654 struct mui_cache_entry *ent;
2656 TRACE("(%s %u %p)\n", wine_dbgstr_w(file_name), index, buffer);
2658 LIST_FOR_EACH_ENTRY(ent, &reg_mui_cache, struct mui_cache_entry, entry)
2660 if (ent->index == index && ent->locale == GetThreadLocale() &&
2661 !lstrcmpiW(ent->file_name, file_name))
2662 goto found;
2664 return 0;
2666 found:
2667 /* move to the list head */
2668 if (list_prev(&reg_mui_cache, &ent->entry)) {
2669 list_remove(&ent->entry);
2670 list_add_head(&reg_mui_cache, &ent->entry);
2673 TRACE("=> %s\n", wine_dbgstr_w(ent->text));
2674 *buffer = ent->text;
2675 return lstrlenW(ent->text);
2678 /* critical section must be held */
2679 static void reg_mui_cache_put(const WCHAR *file_name, UINT index, const WCHAR *buffer, INT size)
2681 struct mui_cache_entry *ent;
2682 TRACE("(%s %u %s %d)\n", wine_dbgstr_w(file_name), index, wine_dbgstr_wn(buffer, size), size);
2684 ent = heap_calloc(sizeof(*ent), 1);
2685 if (!ent)
2686 return;
2687 ent->file_name = heap_alloc((lstrlenW(file_name) + 1) * sizeof(WCHAR));
2688 if (!ent->file_name) {
2689 free_mui_cache_entry(ent);
2690 return;
2692 lstrcpyW(ent->file_name, file_name);
2693 ent->index = index;
2694 ent->locale = GetThreadLocale();
2695 ent->text = heap_alloc((size + 1) * sizeof(WCHAR));
2696 if (!ent->text) {
2697 free_mui_cache_entry(ent);
2698 return;
2700 memcpy(ent->text, buffer, size * sizeof(WCHAR));
2701 ent->text[size] = '\0';
2703 TRACE("add %p\n", ent);
2704 list_add_head(&reg_mui_cache, &ent->entry);
2705 if (reg_mui_cache_count > REG_MUI_CACHE_SIZE) {
2706 ent = LIST_ENTRY( list_tail( &reg_mui_cache ), struct mui_cache_entry, entry );
2707 TRACE("freeing %p\n", ent);
2708 list_remove(&ent->entry);
2709 free_mui_cache_entry(ent);
2711 else
2712 reg_mui_cache_count++;
2714 if (TRACE_ON(reg))
2715 dump_mui_cache();
2716 return;
2719 static LONG load_mui_string(const WCHAR *file_name, UINT res_id, WCHAR *buffer, INT max_chars, INT *req_chars, DWORD flags)
2721 HMODULE hModule = NULL;
2722 WCHAR *string = NULL, *full_name;
2723 int size;
2724 LONG result;
2726 /* Verify the file existence. i.e. We don't rely on PATH variable */
2727 if (GetFileAttributesW(file_name) == INVALID_FILE_ATTRIBUTES)
2728 return ERROR_FILE_NOT_FOUND;
2730 size = GetFullPathNameW(file_name, 0, NULL, NULL);
2731 if (!size)
2732 return GetLastError();
2733 full_name = heap_alloc(size * sizeof(WCHAR));
2734 if (!full_name)
2735 return ERROR_NOT_ENOUGH_MEMORY;
2736 GetFullPathNameW(file_name, size, full_name, NULL);
2738 RtlEnterCriticalSection(&reg_mui_cs);
2739 size = reg_mui_cache_get(full_name, res_id, &string);
2740 if (!size) {
2741 RtlLeaveCriticalSection(&reg_mui_cs);
2743 /* Load the file */
2744 hModule = LoadLibraryExW(full_name, NULL,
2745 LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
2746 if (!hModule)
2747 return GetLastError();
2749 size = LoadStringW(hModule, res_id, (WCHAR *)&string, 0);
2750 if (!size) {
2751 if (string) result = ERROR_NOT_FOUND;
2752 else result = GetLastError();
2753 goto cleanup;
2756 RtlEnterCriticalSection(&reg_mui_cs);
2757 reg_mui_cache_put(full_name, res_id, string, size);
2758 RtlLeaveCriticalSection(&reg_mui_cs);
2760 *req_chars = size + 1;
2762 /* If no buffer is given, skip copying. */
2763 if (!buffer) {
2764 result = ERROR_MORE_DATA;
2765 goto cleanup;
2768 /* Else copy over the string, respecting the buffer size. */
2769 if (size < max_chars)
2770 max_chars = size;
2771 else {
2772 if (flags & REG_MUI_STRING_TRUNCATE)
2773 max_chars--;
2774 else {
2775 result = ERROR_MORE_DATA;
2776 goto cleanup;
2779 if (max_chars >= 0) {
2780 memcpy(buffer, string, max_chars * sizeof(WCHAR));
2781 buffer[max_chars] = '\0';
2784 result = ERROR_SUCCESS;
2786 cleanup:
2787 if (hModule)
2788 FreeLibrary(hModule);
2789 else
2790 RtlLeaveCriticalSection(&reg_mui_cs);
2791 heap_free(full_name);
2792 return result;
2795 /******************************************************************************
2796 * RegLoadMUIStringW (kernelbase.@)
2798 * Load the localized version of a string resource from some PE, respective
2799 * id and path of which are given in the registry value in the format
2800 * @[path]\dllname,-resourceId
2802 * PARAMS
2803 * hKey [I] Key, of which to load the string value from.
2804 * pszValue [I] The value to be loaded (Has to be of REG_EXPAND_SZ or REG_SZ type).
2805 * pszBuffer [O] Buffer to store the localized string in.
2806 * cbBuffer [I] Size of the destination buffer in bytes.
2807 * pcbData [O] Number of bytes written to pszBuffer (optional, may be NULL).
2808 * dwFlags [I] Truncate output to fit the buffer if REG_MUI_STRING_TRUNCATE.
2809 * pszBaseDir [I] Base directory of loading path. If NULL, use the current directory.
2811 * RETURNS
2812 * Success: ERROR_SUCCESS,
2813 * Failure: nonzero error code from winerror.h
2815 LSTATUS WINAPI RegLoadMUIStringW(HKEY hKey, LPCWSTR pwszValue, LPWSTR pwszBuffer, DWORD cbBuffer,
2816 LPDWORD pcbData, DWORD dwFlags, LPCWSTR pwszBaseDir)
2818 DWORD dwValueType, cbData;
2819 LPWSTR pwszTempBuffer = NULL, pwszExpandedBuffer = NULL;
2820 LONG result;
2822 TRACE("(hKey = %p, pwszValue = %s, pwszBuffer = %p, cbBuffer = %ld, pcbData = %p, "
2823 "dwFlags = %lu, pwszBaseDir = %s)\n", hKey, debugstr_w(pwszValue), pwszBuffer,
2824 cbBuffer, pcbData, dwFlags, debugstr_w(pwszBaseDir));
2826 /* Parameter sanity checks. */
2827 if (!hKey || (!pwszBuffer && cbBuffer) || (cbBuffer % sizeof(WCHAR))
2828 || ((dwFlags & REG_MUI_STRING_TRUNCATE) && pcbData)
2829 || (dwFlags & ~REG_MUI_STRING_TRUNCATE))
2830 return ERROR_INVALID_PARAMETER;
2832 /* Check for value existence and correctness of its type, allocate a buffer and load it. */
2833 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, NULL, &cbData);
2834 if (result != ERROR_SUCCESS) goto cleanup;
2835 if (!(dwValueType == REG_SZ || dwValueType == REG_EXPAND_SZ) || !cbData) {
2836 result = ERROR_FILE_NOT_FOUND;
2837 goto cleanup;
2839 pwszTempBuffer = heap_alloc(cbData);
2840 if (!pwszTempBuffer) {
2841 result = ERROR_NOT_ENOUGH_MEMORY;
2842 goto cleanup;
2844 result = RegQueryValueExW(hKey, pwszValue, NULL, &dwValueType, (LPBYTE)pwszTempBuffer, &cbData);
2845 if (result != ERROR_SUCCESS) goto cleanup;
2847 /* '@' is the prefix for resource based string entries. */
2848 if (*pwszTempBuffer != '@') {
2849 result = ERROR_INVALID_DATA;
2850 goto cleanup;
2853 /* Expand environment variables regardless of the type. */
2854 cbData = ExpandEnvironmentStringsW(pwszTempBuffer, NULL, 0) * sizeof(WCHAR);
2855 if (!cbData) goto cleanup;
2856 pwszExpandedBuffer = heap_alloc(cbData);
2857 if (!pwszExpandedBuffer) {
2858 result = ERROR_NOT_ENOUGH_MEMORY;
2859 goto cleanup;
2861 ExpandEnvironmentStringsW(pwszTempBuffer, pwszExpandedBuffer, cbData / sizeof(WCHAR));
2863 /* Parse the value and load the string. */
2865 WCHAR *pComma = wcsrchr(pwszExpandedBuffer, ','), *pNewBuffer;
2866 UINT uiStringId;
2867 DWORD baseDirLen;
2868 int reqChars;
2870 /* Format of the expanded value is 'path_to_dll,-resId' */
2871 if (!pComma || pComma[1] != '-') {
2872 result = ERROR_INVALID_DATA;
2873 goto cleanup;
2876 uiStringId = wcstol(pComma+2, NULL, 10);
2877 *pComma = '\0';
2879 /* Build a resource dll path. */
2880 baseDirLen = pwszBaseDir ? lstrlenW(pwszBaseDir) : 0;
2881 cbData = (baseDirLen + 1 + lstrlenW(pwszExpandedBuffer + 1) + 1) * sizeof(WCHAR);
2882 pNewBuffer = heap_realloc(pwszTempBuffer, cbData);
2883 if (!pNewBuffer) {
2884 result = ERROR_NOT_ENOUGH_MEMORY;
2885 goto cleanup;
2887 pwszTempBuffer = pNewBuffer;
2888 pwszTempBuffer[0] = '\0';
2889 if (baseDirLen) {
2890 lstrcpyW(pwszTempBuffer, pwszBaseDir);
2891 if (pwszBaseDir[baseDirLen - 1] != '\\')
2892 lstrcatW(pwszTempBuffer, L"\\");
2894 lstrcatW(pwszTempBuffer, pwszExpandedBuffer + 1);
2896 /* Load specified string from the file */
2897 reqChars = 0;
2898 result = load_mui_string(pwszTempBuffer, uiStringId, pwszBuffer, cbBuffer/sizeof(WCHAR), &reqChars, dwFlags);
2899 if (pcbData && (result == ERROR_SUCCESS || result == ERROR_MORE_DATA))
2900 *pcbData = reqChars * sizeof(WCHAR);
2903 cleanup:
2904 heap_free(pwszTempBuffer);
2905 heap_free(pwszExpandedBuffer);
2906 return result;
2909 /******************************************************************************
2910 * RegLoadMUIStringA (kernelbase.@)
2912 * Not implemented on native.
2914 LSTATUS WINAPI RegLoadMUIStringA(HKEY hKey, LPCSTR pszValue, LPSTR pszBuffer, DWORD cbBuffer,
2915 LPDWORD pcbData, DWORD dwFlags, LPCSTR pszBaseDir)
2917 return ERROR_CALL_NOT_IMPLEMENTED;
2921 /******************************************************************************
2922 * RegDeleteTreeW (kernelbase.@)
2925 LSTATUS WINAPI RegDeleteTreeW( HKEY hkey, const WCHAR *subkey )
2927 DWORD name_size, max_name, max_subkey;
2928 WCHAR *name_buf = NULL;
2929 LONG ret;
2931 TRACE( "(%p, %s)\n", hkey, debugstr_w(subkey) );
2933 if (subkey && *subkey)
2935 ret = RegOpenKeyExW( hkey, subkey, 0, KEY_READ, &hkey );
2936 if (ret) return ret;
2939 ret = RegQueryInfoKeyW( hkey, NULL, NULL, NULL, NULL, &max_subkey,
2940 NULL, NULL, &max_name, NULL, NULL, NULL );
2941 if (ret)
2942 goto cleanup;
2944 max_name = max( max_subkey, max_name ) + 1;
2945 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
2947 ret = ERROR_NOT_ENOUGH_MEMORY;
2948 goto cleanup;
2951 /* Recursively delete subkeys */
2952 for (;;)
2954 name_size = max_name;
2955 ret = RegEnumKeyExW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2956 if (ret == ERROR_NO_MORE_ITEMS) break;
2957 if (ret) goto cleanup;
2958 ret = RegDeleteTreeW( hkey, name_buf );
2959 if (ret) goto cleanup;
2962 /* Delete the key itself */
2963 if (subkey && *subkey)
2965 ret = RegDeleteKeyExW( hkey, L"", 0, 0 );
2966 goto cleanup;
2969 /* Delete values */
2970 for (;;)
2972 name_size = max_name;
2973 ret = RegEnumValueW( hkey, 0, name_buf, &name_size, NULL, NULL, NULL, NULL );
2974 if (ret == ERROR_NO_MORE_ITEMS) break;
2975 if (ret) goto cleanup;
2976 ret = RegDeleteValueW( hkey, name_buf );
2977 if (ret) goto cleanup;
2980 ret = ERROR_SUCCESS;
2982 cleanup:
2983 heap_free( name_buf );
2984 if (subkey && *subkey)
2985 RegCloseKey( hkey );
2986 return ret;
2990 /******************************************************************************
2991 * RegDeleteTreeA (kernelbase.@)
2994 LSTATUS WINAPI RegDeleteTreeA( HKEY hkey, const char *subkey )
2996 UNICODE_STRING subkeyW;
2997 LONG ret;
2999 if (subkey) RtlCreateUnicodeStringFromAsciiz( &subkeyW, subkey );
3000 else subkeyW.Buffer = NULL;
3001 ret = RegDeleteTreeW( hkey, subkeyW.Buffer );
3002 RtlFreeUnicodeString( &subkeyW );
3003 return ret;
3007 /******************************************************************************
3008 * RegCopyTreeW (kernelbase.@)
3011 LSTATUS WINAPI RegCopyTreeW( HKEY hsrc, const WCHAR *subkey, HKEY hdst )
3013 DWORD name_size, max_name;
3014 DWORD value_size, max_value;
3015 DWORD max_subkey, i, type;
3016 WCHAR *name_buf = NULL;
3017 BYTE *value_buf = NULL;
3018 HKEY hkey;
3019 LONG ret;
3021 TRACE( "(%p, %s, %p)\n", hsrc, debugstr_w(subkey), hdst );
3023 if (subkey)
3025 ret = RegOpenKeyExW( hsrc, subkey, 0, KEY_READ, &hsrc );
3026 if (ret) return ret;
3029 ret = RegQueryInfoKeyW( hsrc, NULL, NULL, NULL, NULL, &max_subkey,
3030 NULL, NULL, &max_name, &max_value, NULL, NULL );
3031 if (ret)
3032 goto cleanup;
3034 max_name = max( max_subkey, max_name ) + 1;
3035 if (!(name_buf = heap_alloc( max_name * sizeof(WCHAR) )))
3037 ret = ERROR_NOT_ENOUGH_MEMORY;
3038 goto cleanup;
3041 if (!(value_buf = heap_alloc( max_value )))
3043 ret = ERROR_NOT_ENOUGH_MEMORY;
3044 goto cleanup;
3047 /* Copy values */
3048 for (i = 0;; i++)
3050 name_size = max_name;
3051 value_size = max_value;
3052 ret = RegEnumValueW( hsrc, i, name_buf, &name_size, NULL, &type, value_buf, &value_size );
3053 if (ret == ERROR_NO_MORE_ITEMS) break;
3054 if (ret) goto cleanup;
3055 ret = RegSetValueExW( hdst, name_buf, 0, type, value_buf, value_size );
3056 if (ret) goto cleanup;
3059 /* Recursively copy subkeys */
3060 for (i = 0;; i++)
3062 name_size = max_name;
3063 ret = RegEnumKeyExW( hsrc, i, name_buf, &name_size, NULL, NULL, NULL, NULL );
3064 if (ret == ERROR_NO_MORE_ITEMS) break;
3065 if (ret) goto cleanup;
3066 ret = RegCreateKeyExW( hdst, name_buf, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL );
3067 if (ret) goto cleanup;
3068 ret = RegCopyTreeW( hsrc, name_buf, hkey );
3069 RegCloseKey( hkey );
3070 if (ret) goto cleanup;
3073 ret = ERROR_SUCCESS;
3075 cleanup:
3076 heap_free( name_buf );
3077 heap_free( value_buf );
3078 if (subkey)
3079 RegCloseKey( hsrc );
3080 return ret;
3084 /******************************************************************************
3085 * RegLoadAppKeyA (kernelbase.@)
3088 LSTATUS WINAPI RegLoadAppKeyA(const char *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3090 FIXME("%s %p %lu %lu %lu: stub\n", wine_dbgstr_a(file), result, sam, options, reserved);
3092 if (!file || reserved)
3093 return ERROR_INVALID_PARAMETER;
3095 *result = (HKEY)0xdeadbeef;
3096 return ERROR_SUCCESS;
3099 /******************************************************************************
3100 * RegLoadAppKeyW (kernelbase.@)
3103 LSTATUS WINAPI RegLoadAppKeyW(const WCHAR *file, HKEY *result, REGSAM sam, DWORD options, DWORD reserved)
3105 FIXME("%s %p %lu %lu %lu: stub\n", wine_dbgstr_w(file), result, sam, options, reserved);
3107 if (!file || reserved)
3108 return ERROR_INVALID_PARAMETER;
3110 *result = (HKEY)0xdeadbeef;
3111 return ERROR_SUCCESS;
3115 /***********************************************************************
3116 * DnsHostnameToComputerNameExW (kernelbase.@)
3118 * FIXME: how is this different from the non-Ex function?
3120 BOOL WINAPI DECLSPEC_HOTPATCH DnsHostnameToComputerNameExW( const WCHAR *hostname, WCHAR *computername,
3121 DWORD *size )
3123 static const WCHAR allowed[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&')(-_{}";
3124 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3125 DWORD i, len;
3127 lstrcpynW( buffer, hostname, MAX_COMPUTERNAME_LENGTH + 1 );
3128 len = lstrlenW( buffer );
3129 if (*size < len + 1)
3131 *size = len;
3132 SetLastError( ERROR_MORE_DATA );
3133 return FALSE;
3135 *size = len;
3136 if (!computername) return FALSE;
3137 for (i = 0; i < len; i++)
3139 if (buffer[i] >= 'a' && buffer[i] <= 'z') computername[i] = buffer[i] + 'A' - 'a';
3140 else computername[i] = wcschr( allowed, buffer[i] ) ? buffer[i] : '_';
3142 computername[len] = 0;
3143 return TRUE;
3147 /***********************************************************************
3148 * GetComputerNameExA (kernelbase.@)
3150 BOOL WINAPI GetComputerNameExA( COMPUTER_NAME_FORMAT type, char *name, DWORD *len )
3152 BOOL ret = FALSE;
3153 DWORD lenA, lenW = 0;
3154 WCHAR *buffer;
3156 GetComputerNameExW( type, NULL, &lenW );
3157 if (GetLastError() != ERROR_MORE_DATA) return FALSE;
3159 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
3161 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3162 return FALSE;
3164 if (GetComputerNameExW( type, buffer, &lenW ))
3166 lenA = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
3167 if (lenA > *len)
3169 *len = lenA;
3170 SetLastError( ERROR_MORE_DATA );
3172 else
3174 WideCharToMultiByte( CP_ACP, 0, buffer, -1, name, *len, NULL, NULL );
3175 *len = lenA - 1;
3176 ret = TRUE;
3179 HeapFree( GetProcessHeap(), 0, buffer );
3180 return ret;
3184 /***********************************************************************
3185 * GetComputerNameExW (kernelbase.@)
3187 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, WCHAR *name, DWORD *len )
3189 const WCHAR *keyname, *valuename;
3190 LRESULT ret;
3191 HKEY key;
3193 switch (type)
3195 case ComputerNameNetBIOS:
3196 case ComputerNamePhysicalNetBIOS:
3197 keyname = L"System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName";
3198 valuename = L"ComputerName";
3199 break;
3200 case ComputerNameDnsHostname:
3201 case ComputerNamePhysicalDnsHostname:
3202 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3203 valuename = L"Hostname";
3204 break;
3205 case ComputerNameDnsDomain:
3206 case ComputerNamePhysicalDnsDomain:
3207 keyname = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
3208 valuename = L"Domain";
3209 break;
3210 case ComputerNameDnsFullyQualified:
3211 case ComputerNamePhysicalDnsFullyQualified:
3213 WCHAR *domain, buffer[256];
3214 DWORD size = ARRAY_SIZE(buffer) - 1;
3216 if (!GetComputerNameExW( ComputerNameDnsHostname, buffer, &size )) return FALSE;
3217 domain = buffer + lstrlenW(buffer);
3218 *domain++ = '.';
3219 size = ARRAY_SIZE(buffer) - (domain - buffer);
3220 if (!GetComputerNameExW( ComputerNameDnsDomain, domain, &size )) return FALSE;
3221 if (!*domain) domain[-1] = 0;
3222 size = lstrlenW(buffer);
3223 if (name && size < *len)
3225 if (name) lstrcpyW( name, buffer );
3226 *len = size;
3227 return TRUE;
3229 *len = size + 1;
3230 SetLastError( ERROR_MORE_DATA );
3231 return FALSE;
3233 default:
3234 SetLastError( ERROR_INVALID_PARAMETER );
3235 return FALSE;
3238 if (!(ret = RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &key )))
3240 DWORD size = *len * sizeof(WCHAR);
3241 ret = RegQueryValueExW( key, valuename, NULL, NULL, (BYTE *)name, &size );
3242 if (!name) ret = ERROR_MORE_DATA;
3243 else if (!ret) size -= sizeof(WCHAR);
3244 *len = size / sizeof(WCHAR);
3245 RegCloseKey( key );
3247 TRACE("-> %Iu %s\n", ret, debugstr_w(name) );
3248 if (ret) SetLastError( ret );
3249 return !ret;
3253 /***********************************************************************
3254 * SetComputerNameA (kernelbase.@)
3256 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameA( const char *name )
3258 BOOL ret;
3259 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3260 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3262 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3263 ret = SetComputerNameExW( ComputerNamePhysicalNetBIOS, nameW );
3264 HeapFree( GetProcessHeap(), 0, nameW );
3265 return ret;
3269 /***********************************************************************
3270 * SetComputerNameW (kernelbase.@)
3272 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameW( const WCHAR *name )
3274 return SetComputerNameExW( ComputerNamePhysicalNetBIOS, name );
3278 /***********************************************************************
3279 * SetComputerNameExA (kernelbase.@)
3281 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExA( COMPUTER_NAME_FORMAT type, const char *name )
3283 BOOL ret;
3284 DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
3285 WCHAR *nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3287 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, len );
3288 ret = SetComputerNameExW( type, nameW );
3289 HeapFree( GetProcessHeap(), 0, nameW );
3290 return ret;
3294 /***********************************************************************
3295 * SetComputerNameExW (kernelbase.@)
3297 BOOL WINAPI DECLSPEC_HOTPATCH SetComputerNameExW( COMPUTER_NAME_FORMAT type, const WCHAR *name )
3299 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 1];
3300 DWORD size;
3301 HKEY key;
3302 LRESULT ret;
3304 TRACE( "%u %s\n", type, debugstr_w( name ));
3306 switch (type)
3308 case ComputerNameDnsHostname:
3309 case ComputerNamePhysicalDnsHostname:
3310 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3311 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3312 if (ret) break;
3313 ret = RegSetValueExW( key, L"Hostname", 0, REG_SZ,
3314 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3315 RegCloseKey( key );
3316 /* fall through */
3318 case ComputerNameNetBIOS:
3319 case ComputerNamePhysicalNetBIOS:
3320 /* @@ Wine registry key: HKCU\Software\Wine\Network */
3321 if (!RegOpenKeyExW( HKEY_CURRENT_USER, L"Software\\Wine\\Network", 0, KEY_READ, &key ))
3323 BOOL use_dns = TRUE;
3324 size = sizeof(buffer);
3325 if (!RegQueryValueExW( key, L"UseDnsComputerName", NULL, NULL, (BYTE *)buffer, &size ))
3326 use_dns = IS_OPTION_TRUE( buffer[0] );
3327 RegCloseKey( key );
3328 if (!use_dns)
3330 ret = ERROR_ACCESS_DENIED;
3331 break;
3334 size = ARRAY_SIZE( buffer );
3335 if (!DnsHostnameToComputerNameExW( name, buffer, &size )) return FALSE;
3336 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
3337 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3338 if (ret) break;
3339 ret = RegSetValueExW( key, L"ComputerName", 0, REG_SZ,
3340 (BYTE *)buffer, (lstrlenW(buffer) + 1) * sizeof(WCHAR) );
3341 RegCloseKey( key );
3342 break;
3344 case ComputerNameDnsDomain:
3345 case ComputerNamePhysicalDnsDomain:
3346 ret = RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
3347 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
3348 if (ret) break;
3349 ret = RegSetValueExW( key, L"Domain", 0, REG_SZ,
3350 (BYTE *)name, (lstrlenW(name) + 1) * sizeof(WCHAR) );
3351 RegCloseKey( key );
3352 break;
3353 default:
3354 ret = ERROR_INVALID_PARAMETER;
3355 break;
3357 if (ret) SetLastError( ret );
3358 return !ret;
3361 struct USKEY
3363 HKEY HKCUstart; /* Start key in CU hive */
3364 HKEY HKCUkey; /* Opened key in CU hive */
3365 HKEY HKLMstart; /* Start key in LM hive */
3366 HKEY HKLMkey; /* Opened key in LM hive */
3367 WCHAR path[MAX_PATH];
3370 LONG WINAPI SHRegCreateUSKeyA(LPCSTR path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3372 WCHAR *pathW;
3373 LONG ret;
3375 TRACE("%s, %#lx, %p, %p, %#lx\n", debugstr_a(path), samDesired, relative_key, new_uskey, flags);
3377 if (path)
3379 INT len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
3380 pathW = heap_alloc(len * sizeof(WCHAR));
3381 if (!pathW)
3382 return ERROR_NOT_ENOUGH_MEMORY;
3383 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
3385 else
3386 pathW = NULL;
3388 ret = SHRegCreateUSKeyW(pathW, samDesired, relative_key, new_uskey, flags);
3389 HeapFree(GetProcessHeap(), 0, pathW);
3390 return ret;
3393 static HKEY reg_duplicate_hkey(HKEY hKey)
3395 HKEY newKey = 0;
3397 RegOpenKeyExW(hKey, 0, 0, MAXIMUM_ALLOWED, &newKey);
3398 return newKey;
3401 static HKEY reg_get_hkey_from_huskey(HUSKEY hUSKey, BOOL is_hkcu)
3403 struct USKEY *mihk = hUSKey;
3404 HKEY test = hUSKey;
3406 if (test == HKEY_CLASSES_ROOT
3407 || test == HKEY_CURRENT_CONFIG
3408 || test == HKEY_CURRENT_USER
3409 || test == HKEY_DYN_DATA
3410 || test == HKEY_LOCAL_MACHINE
3411 || test == HKEY_PERFORMANCE_DATA
3412 || test == HKEY_USERS)
3413 /* FIXME: need to define for Win2k, ME, XP
3414 * (test == HKEY_PERFORMANCE_TEXT) ||
3415 * (test == HKEY_PERFORMANCE_NLSTEXT) ||
3418 return test;
3421 return is_hkcu ? mihk->HKCUkey : mihk->HKLMkey;
3424 LONG WINAPI SHRegCreateUSKeyW(const WCHAR *path, REGSAM samDesired, HUSKEY relative_key, PHUSKEY new_uskey, DWORD flags)
3426 LONG ret = ERROR_CALL_NOT_IMPLEMENTED;
3427 struct USKEY *ret_key;
3429 TRACE("%s, %#lx, %p, %p, %#lx\n", debugstr_w(path), samDesired, relative_key, new_uskey, flags);
3431 if (!new_uskey)
3432 return ERROR_INVALID_PARAMETER;
3434 *new_uskey = NULL;
3436 if (flags & ~SHREGSET_FORCE_HKCU)
3438 FIXME("unsupported flags 0x%08lx\n", flags);
3439 return ERROR_SUCCESS;
3442 ret_key = heap_alloc_zero(sizeof(*ret_key));
3443 lstrcpynW(ret_key->path, path, ARRAY_SIZE(ret_key->path));
3445 if (relative_key)
3447 ret_key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3448 ret_key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3450 else
3452 ret_key->HKCUstart = HKEY_CURRENT_USER;
3453 ret_key->HKLMstart = HKEY_LOCAL_MACHINE;
3456 if (flags & SHREGSET_FORCE_HKCU)
3458 ret = RegCreateKeyExW(ret_key->HKCUstart, path, 0, NULL, 0, samDesired, NULL, &ret_key->HKCUkey, NULL);
3459 if (ret == ERROR_SUCCESS)
3460 *new_uskey = ret_key;
3461 else
3462 heap_free(ret_key);
3465 return ret;
3468 LONG WINAPI SHRegCloseUSKey(HUSKEY hUSKey)
3470 struct USKEY *key = hUSKey;
3471 LONG ret = ERROR_SUCCESS;
3473 if (!key)
3474 return ERROR_INVALID_PARAMETER;
3476 if (key->HKCUkey)
3477 ret = RegCloseKey(key->HKCUkey);
3478 if (key->HKCUstart && key->HKCUstart != HKEY_CURRENT_USER)
3479 ret = RegCloseKey(key->HKCUstart);
3480 if (key->HKLMkey)
3481 ret = RegCloseKey(key->HKLMkey);
3482 if (key->HKLMstart && key->HKLMstart != HKEY_LOCAL_MACHINE)
3483 ret = RegCloseKey(key->HKLMstart);
3485 heap_free(key);
3486 return ret;
3489 LONG WINAPI SHRegDeleteEmptyUSKeyA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3491 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3492 return ERROR_SUCCESS;
3495 LONG WINAPI SHRegDeleteEmptyUSKeyW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3497 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3498 return ERROR_SUCCESS;
3501 LONG WINAPI SHRegDeleteUSValueA(HUSKEY hUSKey, const char *value, SHREGDEL_FLAGS flags)
3503 FIXME("%p, %s, %#x\n", hUSKey, debugstr_a(value), flags);
3504 return ERROR_SUCCESS;
3507 LONG WINAPI SHRegDeleteUSValueW(HUSKEY hUSKey, const WCHAR *value, SHREGDEL_FLAGS flags)
3509 FIXME("%p, %s, %#x\n", hUSKey, debugstr_w(value), flags);
3510 return ERROR_SUCCESS;
3513 LONG WINAPI SHRegEnumUSValueA(HUSKEY hUSKey, DWORD index, char *value_name, DWORD *value_name_len, DWORD *type,
3514 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3516 HKEY dokey;
3518 TRACE("%p, %#lx, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3520 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3521 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3523 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3524 return RegEnumValueA(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3526 FIXME("no support for SHREGENUM_BOTH\n");
3527 return ERROR_INVALID_FUNCTION;
3530 LONG WINAPI SHRegEnumUSValueW(HUSKEY hUSKey, DWORD index, WCHAR *value_name, DWORD *value_name_len, DWORD *type,
3531 void *data, DWORD *data_len, SHREGENUM_FLAGS flags)
3533 HKEY dokey;
3535 TRACE("%p, %#lx, %p, %p, %p, %p, %p, %#x\n", hUSKey, index, value_name, value_name_len, type, data, data_len, flags);
3537 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3538 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3540 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3541 return RegEnumValueW(dokey, index, value_name, value_name_len, NULL, type, data, data_len);
3543 FIXME("no support for SHREGENUM_BOTH\n");
3544 return ERROR_INVALID_FUNCTION;
3547 LONG WINAPI SHRegEnumUSKeyA(HUSKEY hUSKey, DWORD index, char *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3549 HKEY dokey;
3551 TRACE("%p, %ld, %p, %p(%ld), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3553 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3554 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3556 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3557 return RegEnumKeyExA(dokey, index, name, name_len, 0, 0, 0, 0);
3559 FIXME("no support for SHREGENUM_BOTH\n");
3560 return ERROR_INVALID_FUNCTION;
3563 LONG WINAPI SHRegEnumUSKeyW(HUSKEY hUSKey, DWORD index, WCHAR *name, DWORD *name_len, SHREGENUM_FLAGS flags)
3565 HKEY dokey;
3567 TRACE("%p, %ld, %p, %p(%ld), %d\n", hUSKey, index, name, name_len, *name_len, flags);
3569 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3570 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3572 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3573 return RegEnumKeyExW(dokey, index, name, name_len, 0, 0, 0, 0);
3575 FIXME("no support for SHREGENUM_BOTH\n");
3576 return ERROR_INVALID_FUNCTION;
3579 LONG WINAPI SHRegOpenUSKeyA(const char *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3581 WCHAR pathW[MAX_PATH];
3583 if (path)
3584 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, ARRAY_SIZE(pathW));
3586 return SHRegOpenUSKeyW(path ? pathW : NULL, access_mask, relative_key, uskey, ignore_hkcu);
3589 LONG WINAPI SHRegOpenUSKeyW(const WCHAR *path, REGSAM access_mask, HUSKEY relative_key, HUSKEY *uskey, BOOL ignore_hkcu)
3591 LONG ret2, ret1 = ~ERROR_SUCCESS;
3592 struct USKEY *key;
3594 TRACE("%s, %#lx, %p, %p, %d\n", debugstr_w(path), access_mask, relative_key, uskey, ignore_hkcu);
3596 if (uskey)
3597 *uskey = NULL;
3599 /* Create internal HUSKEY */
3600 key = heap_alloc_zero(sizeof(*key));
3601 lstrcpynW(key->path, path, ARRAY_SIZE(key->path));
3603 if (relative_key)
3605 key->HKCUstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, TRUE));
3606 key->HKLMstart = reg_duplicate_hkey(reg_get_hkey_from_huskey(relative_key, FALSE));
3608 /* FIXME: if either of these keys is NULL, create the start key from
3609 * the relative keys start+path
3612 else
3614 key->HKCUstart = HKEY_CURRENT_USER;
3615 key->HKLMstart = HKEY_LOCAL_MACHINE;
3618 if (!ignore_hkcu)
3620 ret1 = RegOpenKeyExW(key->HKCUstart, key->path, 0, access_mask, &key->HKCUkey);
3621 if (ret1)
3622 key->HKCUkey = 0;
3625 ret2 = RegOpenKeyExW(key->HKLMstart, key->path, 0, access_mask, &key->HKLMkey);
3626 if (ret2)
3627 key->HKLMkey = 0;
3629 if (ret1 || ret2)
3630 TRACE("one or more opens failed: HKCU=%ld HKLM=%ld\n", ret1, ret2);
3632 if (ret1 && ret2)
3634 /* Neither open succeeded: fail */
3635 SHRegCloseUSKey(key);
3636 return ret2;
3639 TRACE("HUSKEY=%p\n", key);
3640 if (uskey)
3641 *uskey = key;
3643 return ERROR_SUCCESS;
3646 LONG WINAPI SHRegWriteUSValueA(HUSKEY hUSKey, const char *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3648 WCHAR valueW[MAX_PATH];
3650 if (value)
3651 MultiByteToWideChar(CP_ACP, 0, value, -1, valueW, ARRAY_SIZE(valueW));
3653 return SHRegWriteUSValueW(hUSKey, value ? valueW : NULL, type, data, data_len, flags);
3656 LONG WINAPI SHRegWriteUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD type, void *data, DWORD data_len, DWORD flags)
3658 struct USKEY *hKey = hUSKey;
3659 LONG ret = ERROR_SUCCESS;
3660 DWORD dummy;
3662 TRACE("%p, %s, %ld, %p, %ld, %#lx\n", hUSKey, debugstr_w(value), type, data, data_len, flags);
3664 __TRY
3666 dummy = hKey->HKCUkey || hKey->HKLMkey;
3668 __EXCEPT_PAGE_FAULT
3670 return ERROR_INVALID_PARAMETER;
3672 __ENDTRY
3673 if (!(flags & (SHREGSET_FORCE_HKCU|SHREGSET_FORCE_HKLM))) return ERROR_INVALID_PARAMETER;
3675 if (flags & (SHREGSET_FORCE_HKCU | SHREGSET_HKCU))
3677 if (!hKey->HKCUkey)
3679 /* Create the key */
3680 ret = RegCreateKeyExW(hKey->HKCUstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3681 MAXIMUM_ALLOWED, NULL, &hKey->HKCUkey, NULL);
3682 TRACE("Creating HKCU key, ret = %ld\n", ret);
3683 if (ret && (flags & SHREGSET_FORCE_HKCU))
3685 hKey->HKCUkey = 0;
3686 return ret;
3690 if (!ret)
3692 if ((flags & SHREGSET_FORCE_HKCU) || RegQueryValueExW(hKey->HKCUkey, value, NULL, NULL, NULL, &dummy))
3694 /* Doesn't exist or we are forcing: Write value */
3695 ret = RegSetValueExW(hKey->HKCUkey, value, 0, type, data, data_len);
3696 TRACE("Writing HKCU value, ret = %ld\n", ret);
3701 if (flags & (SHREGSET_FORCE_HKLM | SHREGSET_HKLM))
3703 if (!hKey->HKLMkey)
3705 /* Create the key */
3706 ret = RegCreateKeyExW(hKey->HKLMstart, hKey->path, 0, NULL, REG_OPTION_NON_VOLATILE,
3707 MAXIMUM_ALLOWED, NULL, &hKey->HKLMkey, NULL);
3708 TRACE("Creating HKLM key, ret = %ld\n", ret);
3709 if (ret && (flags & (SHREGSET_FORCE_HKLM)))
3711 hKey->HKLMkey = 0;
3712 return ret;
3716 if (!ret)
3718 if ((flags & SHREGSET_FORCE_HKLM) || RegQueryValueExW(hKey->HKLMkey, value, NULL, NULL, NULL, &dummy))
3720 /* Doesn't exist or we are forcing: Write value */
3721 ret = RegSetValueExW(hKey->HKLMkey, value, 0, type, data, data_len);
3722 TRACE("Writing HKLM value, ret = %ld\n", ret);
3727 return ret;
3730 LONG WINAPI SHRegSetUSValueA(const char *subkey, const char *value, DWORD type, void *data, DWORD data_len,
3731 DWORD flags)
3733 BOOL ignore_hkcu;
3734 HUSKEY hkey;
3735 LONG ret;
3737 TRACE("%s, %s, %ld, %p, %ld, %#lx\n", debugstr_a(subkey), debugstr_a(value), type, data, data_len, flags);
3739 if (!data)
3740 return ERROR_INVALID_FUNCTION;
3742 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3744 ret = SHRegOpenUSKeyA(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3745 if (ret == ERROR_SUCCESS)
3747 ret = SHRegWriteUSValueA(hkey, value, type, data, data_len, flags);
3748 SHRegCloseUSKey(hkey);
3751 return ret;
3754 LONG WINAPI SHRegSetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD type, void *data, DWORD data_len,
3755 DWORD flags)
3757 BOOL ignore_hkcu;
3758 HUSKEY hkey;
3759 LONG ret;
3761 TRACE("%s, %s, %ld, %p, %ld, %#lx\n", debugstr_w(subkey), debugstr_w(value), type, data, data_len, flags);
3763 if (!data)
3764 return ERROR_INVALID_FUNCTION;
3766 ignore_hkcu = !(flags & SHREGSET_HKCU || flags & SHREGSET_FORCE_HKCU);
3768 ret = SHRegOpenUSKeyW(subkey, KEY_ALL_ACCESS, 0, &hkey, ignore_hkcu);
3769 if (ret == ERROR_SUCCESS)
3771 ret = SHRegWriteUSValueW(hkey, value, type, data, data_len, flags);
3772 SHRegCloseUSKey(hkey);
3775 return ret;
3778 LONG WINAPI SHRegQueryInfoUSKeyA(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3779 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3781 HKEY dokey;
3782 LONG ret;
3784 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3786 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3788 ret = RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3789 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3790 return ret;
3793 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3795 return RegQueryInfoKeyA(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3798 return ERROR_INVALID_FUNCTION;
3801 LONG WINAPI SHRegQueryInfoUSKeyW(HUSKEY hUSKey, DWORD *subkeys, DWORD *max_subkey_len, DWORD *values,
3802 DWORD *max_value_name_len, SHREGENUM_FLAGS flags)
3804 HKEY dokey;
3805 LONG ret;
3807 TRACE("%p, %p, %p, %p, %p, %#x\n", hUSKey, subkeys, max_subkey_len, values, max_value_name_len, flags);
3809 if ((flags == SHREGENUM_HKCU || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3811 ret = RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3812 if (ret == ERROR_SUCCESS || flags == SHREGENUM_HKCU)
3813 return ret;
3816 if ((flags == SHREGENUM_HKLM || flags == SHREGENUM_DEFAULT) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3818 return RegQueryInfoKeyW(dokey, 0, 0, 0, subkeys, max_subkey_len, 0, values, max_value_name_len, 0, 0, 0);
3821 return ERROR_INVALID_FUNCTION;
3824 LONG WINAPI SHRegQueryUSValueA(HUSKEY hUSKey, const char *value, DWORD *type, void *data, DWORD *data_len,
3825 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3827 LONG ret = ~ERROR_SUCCESS;
3828 DWORD move_len;
3829 HKEY dokey;
3831 /* If user wants HKCU, and it exists, then try it */
3832 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3834 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3835 TRACE("HKCU RegQueryValue returned %ld\n", ret);
3838 /* If HKCU did not work and HKLM exists, then try it */
3839 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3841 ret = RegQueryValueExA(dokey, value, 0, type, data, data_len);
3842 TRACE("HKLM RegQueryValue returned %ld\n", ret);
3845 /* If neither worked, and default data exists, then use it */
3846 if (ret != ERROR_SUCCESS)
3848 if (default_data && default_data_len)
3850 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3851 memmove(data, default_data, move_len);
3852 *data_len = move_len;
3853 TRACE("setting default data\n");
3854 ret = ERROR_SUCCESS;
3858 return ret;
3861 LONG WINAPI SHRegQueryUSValueW(HUSKEY hUSKey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3862 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3864 LONG ret = ~ERROR_SUCCESS;
3865 DWORD move_len;
3866 HKEY dokey;
3868 /* If user wants HKCU, and it exists, then try it */
3869 if (!ignore_hkcu && (dokey = reg_get_hkey_from_huskey(hUSKey, TRUE)))
3871 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3872 TRACE("HKCU RegQueryValue returned %ld\n", ret);
3875 /* If HKCU did not work and HKLM exists, then try it */
3876 if ((ret != ERROR_SUCCESS) && (dokey = reg_get_hkey_from_huskey(hUSKey, FALSE)))
3878 ret = RegQueryValueExW(dokey, value, 0, type, data, data_len);
3879 TRACE("HKLM RegQueryValue returned %ld\n", ret);
3882 /* If neither worked, and default data exists, then use it */
3883 if (ret != ERROR_SUCCESS)
3885 if (default_data && default_data_len)
3887 move_len = default_data_len >= *data_len ? *data_len : default_data_len;
3888 memmove(data, default_data, move_len);
3889 *data_len = move_len;
3890 TRACE("setting default data\n");
3891 ret = ERROR_SUCCESS;
3895 return ret;
3898 LONG WINAPI SHRegGetUSValueA(const char *subkey, const char *value, DWORD *type, void *data, DWORD *data_len,
3899 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3901 HUSKEY myhuskey;
3902 LONG ret;
3904 if (!data || !data_len)
3905 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3907 TRACE("%s, %s, %ld\n", debugstr_a(subkey), debugstr_a(value), *data_len);
3909 ret = SHRegOpenUSKeyA(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3910 if (!ret)
3912 ret = SHRegQueryUSValueA(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3913 SHRegCloseUSKey(myhuskey);
3916 return ret;
3919 LONG WINAPI SHRegGetUSValueW(const WCHAR *subkey, const WCHAR *value, DWORD *type, void *data, DWORD *data_len,
3920 BOOL ignore_hkcu, void *default_data, DWORD default_data_len)
3922 HUSKEY myhuskey;
3923 LONG ret;
3925 if (!data || !data_len)
3926 return ERROR_INVALID_FUNCTION; /* FIXME:wrong*/
3928 TRACE("%s, %s, %ld\n", debugstr_w(subkey), debugstr_w(value), *data_len);
3930 ret = SHRegOpenUSKeyW(subkey, KEY_QUERY_VALUE, 0, &myhuskey, ignore_hkcu);
3931 if (!ret)
3933 ret = SHRegQueryUSValueW(myhuskey, value, type, data, data_len, ignore_hkcu, default_data, default_data_len);
3934 SHRegCloseUSKey(myhuskey);
3937 return ret;
3940 BOOL WINAPI SHRegGetBoolUSValueA(const char *subkey, const char *value, BOOL ignore_hkcu, BOOL default_value)
3942 BOOL ret = default_value;
3943 DWORD type, datalen;
3944 char data[10];
3946 TRACE("%s, %s, %d\n", debugstr_a(subkey), debugstr_a(value), ignore_hkcu);
3948 datalen = ARRAY_SIZE(data) - 1;
3949 if (!SHRegGetUSValueA(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3951 switch (type)
3953 case REG_SZ:
3954 data[9] = '\0';
3955 if (!lstrcmpiA(data, "YES") || !lstrcmpiA(data, "TRUE"))
3956 ret = TRUE;
3957 else if (!lstrcmpiA(data, "NO") || !lstrcmpiA(data, "FALSE"))
3958 ret = FALSE;
3959 break;
3960 case REG_DWORD:
3961 ret = *(DWORD *)data != 0;
3962 break;
3963 case REG_BINARY:
3964 if (datalen == 1)
3966 ret = !!data[0];
3967 break;
3969 default:
3970 FIXME("Unsupported registry data type %ld\n", type);
3971 ret = FALSE;
3973 TRACE("got value (type=%ld), returning %d\n", type, ret);
3975 else
3976 TRACE("returning default value %d\n", ret);
3978 return ret;
3981 BOOL WINAPI SHRegGetBoolUSValueW(const WCHAR *subkey, const WCHAR *value, BOOL ignore_hkcu, BOOL default_value)
3983 BOOL ret = default_value;
3984 DWORD type, datalen;
3985 WCHAR data[10];
3987 TRACE("%s, %s, %d\n", debugstr_w(subkey), debugstr_w(value), ignore_hkcu);
3989 datalen = (ARRAY_SIZE(data) - 1) * sizeof(WCHAR);
3990 if (!SHRegGetUSValueW(subkey, value, &type, data, &datalen, ignore_hkcu, 0, 0))
3992 switch (type)
3994 case REG_SZ:
3995 data[9] = '\0';
3996 if (!lstrcmpiW(data, L"yes") || !lstrcmpiW(data, L"true"))
3997 ret = TRUE;
3998 else if (!lstrcmpiW(data, L"no") || !lstrcmpiW(data, L"false"))
3999 ret = FALSE;
4000 break;
4001 case REG_DWORD:
4002 ret = *(DWORD *)data != 0;
4003 break;
4004 case REG_BINARY:
4005 if (datalen == 1)
4007 ret = !!data[0];
4008 break;
4010 default:
4011 FIXME("Unsupported registry data type %ld\n", type);
4012 ret = FALSE;
4014 TRACE("got value (type=%ld), returning %d\n", type, ret);
4016 else
4017 TRACE("returning default value %d\n", ret);
4019 return ret;