4 * Copyright (C) 1999 Alexandre Julliard
6 * Based on misc/registry.c code
7 * Copyright (C) 1996 Marcus Meissner
8 * Copyright (C) 1998 Matthew Becker
9 * Copyright (C) 1999 Sylvain St-Germain
11 * This file is concerned about handle management and interaction with the Wine server.
12 * Registry file I/O is in misc/registry.c.
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include "wine/unicode.h"
38 #include "wine/server.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(reg);
44 /* check if value type needs string conversion (Ansi<->Unicode) */
45 inline static int is_string( DWORD type )
47 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
50 /* check if current version is NT or Win95 */
51 inline static int is_version_nt(void)
53 return !(GetVersion() & 0x80000000);
56 /* allowed bits for access mask */
57 #define KEY_ACCESS_MASK (KEY_ALL_ACCESS | MAXIMUM_ALLOWED)
59 /******************************************************************************
60 * RegCreateKeyExW [ADVAPI32.@]
63 * hkey [I] Handle of an open key
64 * name [I] Address of subkey name
65 * reserved [I] Reserved - must be 0
66 * class [I] Address of class string
67 * options [I] Special options flag
68 * access [I] Desired security access
69 * sa [I] Address of key security structure
70 * retkey [O] Address of buffer for opened handle
71 * dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
74 * in case of failing retkey remains untouched
76 * FIXME MAXIMUM_ALLOWED in access mask not supported by server
78 DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
79 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
80 LPHKEY retkey, LPDWORD dispos )
82 OBJECT_ATTRIBUTES attr;
83 UNICODE_STRING nameW, classW;
85 if (reserved) return ERROR_INVALID_PARAMETER;
86 if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
88 attr.Length = sizeof(attr);
89 attr.RootDirectory = hkey;
90 attr.ObjectName = &nameW;
92 attr.SecurityDescriptor = NULL;
93 attr.SecurityQualityOfService = NULL;
94 RtlInitUnicodeString( &nameW, name );
95 RtlInitUnicodeString( &classW, class );
97 return RtlNtStatusToDosError( NtCreateKey( retkey, access, &attr, 0,
98 &classW, options, dispos ) );
102 /******************************************************************************
103 * RegCreateKeyExA [ADVAPI32.@]
105 * FIXME MAXIMUM_ALLOWED in access mask not supported by server
107 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
108 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
109 LPHKEY retkey, LPDWORD dispos )
111 OBJECT_ATTRIBUTES attr;
112 UNICODE_STRING classW;
113 ANSI_STRING nameA, classA;
116 if (reserved) return ERROR_INVALID_PARAMETER;
117 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
118 else if (!(access & KEY_ACCESS_MASK) || (access & ~KEY_ACCESS_MASK)) return ERROR_ACCESS_DENIED;
120 attr.Length = sizeof(attr);
121 attr.RootDirectory = hkey;
122 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
124 attr.SecurityDescriptor = NULL;
125 attr.SecurityQualityOfService = NULL;
126 RtlInitAnsiString( &nameA, name );
127 RtlInitAnsiString( &classA, class );
129 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
132 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
134 status = NtCreateKey( retkey, access, &attr, 0, &classW, options, dispos );
135 RtlFreeUnicodeString( &classW );
138 return RtlNtStatusToDosError( status );
142 /******************************************************************************
143 * RegCreateKeyW [ADVAPI32.@]
145 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
147 /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
148 /* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
149 return RegCreateKeyExW( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
150 KEY_ALL_ACCESS, NULL, retkey, NULL );
154 /******************************************************************************
155 * RegCreateKeyA [ADVAPI32.@]
157 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
159 return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
160 KEY_ALL_ACCESS, NULL, retkey, NULL );
165 /******************************************************************************
166 * RegOpenKeyExW [ADVAPI32.@]
168 * Opens the specified key
170 * Unlike RegCreateKeyEx, this does not create the key if it does not exist.
173 * hkey [I] Handle of open key
174 * name [I] Name of subkey to open
175 * reserved [I] Reserved - must be zero
176 * access [I] Security access mask
177 * retkey [O] Handle to open key
180 * Success: ERROR_SUCCESS
181 * Failure: Error code
184 * in case of failing is retkey = 0
186 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
188 OBJECT_ATTRIBUTES attr;
189 UNICODE_STRING nameW;
191 attr.Length = sizeof(attr);
192 attr.RootDirectory = hkey;
193 attr.ObjectName = &nameW;
195 attr.SecurityDescriptor = NULL;
196 attr.SecurityQualityOfService = NULL;
197 RtlInitUnicodeString( &nameW, name );
198 return RtlNtStatusToDosError( NtOpenKey( retkey, access, &attr ) );
202 /******************************************************************************
203 * RegOpenKeyExA [ADVAPI32.@]
205 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
207 OBJECT_ATTRIBUTES attr;
211 if (!is_version_nt()) access = KEY_ALL_ACCESS; /* Win95 ignores the access mask */
213 attr.Length = sizeof(attr);
214 attr.RootDirectory = hkey;
215 attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
217 attr.SecurityDescriptor = NULL;
218 attr.SecurityQualityOfService = NULL;
220 RtlInitAnsiString( &nameA, name );
221 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
224 status = NtOpenKey( retkey, access, &attr );
226 return RtlNtStatusToDosError( status );
230 /******************************************************************************
231 * RegOpenKeyW [ADVAPI32.@]
234 * hkey [I] Handle of open key
235 * name [I] Address of name of subkey to open
236 * retkey [O] Handle to open key
239 * Success: ERROR_SUCCESS
240 * Failure: Error code
243 * in case of failing is retkey = 0
245 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
247 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
251 /******************************************************************************
252 * RegOpenKeyA [ADVAPI32.@]
254 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
256 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
260 /******************************************************************************
261 * RegOpenCurrentUser [ADVAPI32.@]
262 * FIXME: This function is supposed to retrieve a handle to the
263 * HKEY_CURRENT_USER for the user the current thread is impersonating.
264 * Since Wine does not currently allow threads to impersonate other users,
265 * this stub should work fine.
267 DWORD WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
269 return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
274 /******************************************************************************
275 * RegEnumKeyExW [ADVAPI32.@]
278 * hkey [I] Handle to key to enumerate
279 * index [I] Index of subkey to enumerate
280 * name [O] Buffer for subkey name
281 * name_len [O] Size of subkey buffer
282 * reserved [I] Reserved
283 * class [O] Buffer for class string
284 * class_len [O] Size of class buffer
285 * ft [O] Time key last written to
287 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
288 LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
291 char buffer[256], *buf_ptr = buffer;
292 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
295 TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
296 name_len ? *name_len : -1, reserved, class, class_len, ft );
298 if (reserved) return ERROR_INVALID_PARAMETER;
300 if (!hkey) return ERROR_INVALID_HANDLE;
302 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
303 buffer, sizeof(buffer), &total_size );
305 while (status == STATUS_BUFFER_OVERFLOW)
307 /* retry with a dynamically allocated buffer */
308 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
309 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
310 return ERROR_NOT_ENOUGH_MEMORY;
311 info = (KEY_NODE_INFORMATION *)buf_ptr;
312 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
313 buf_ptr, total_size, &total_size );
318 DWORD len = info->NameLength / sizeof(WCHAR);
319 DWORD cls_len = info->ClassLength / sizeof(WCHAR);
321 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
323 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
324 status = STATUS_BUFFER_OVERFLOW;
328 memcpy( name, info->Name, info->NameLength );
332 *class_len = cls_len;
335 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
342 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
343 return RtlNtStatusToDosError( status );
347 /******************************************************************************
348 * RegEnumKeyExA [ADVAPI32.@]
350 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
351 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
354 char buffer[256], *buf_ptr = buffer;
355 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
358 TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
359 name_len ? *name_len : -1, reserved, class, class_len, ft );
361 if (reserved) return ERROR_INVALID_PARAMETER;
363 if (!hkey) return ERROR_INVALID_HANDLE;
365 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
366 buffer, sizeof(buffer), &total_size );
368 while (status == STATUS_BUFFER_OVERFLOW)
370 /* retry with a dynamically allocated buffer */
371 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
372 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
373 return ERROR_NOT_ENOUGH_MEMORY;
374 info = (KEY_NODE_INFORMATION *)buf_ptr;
375 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
376 buf_ptr, total_size, &total_size );
383 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
384 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
386 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
388 if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
389 status = STATUS_BUFFER_OVERFLOW;
393 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
397 *class_len = cls_len;
400 RtlUnicodeToMultiByteN( class, cls_len, NULL,
401 (WCHAR *)(buf_ptr + info->ClassOffset),
409 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
410 return RtlNtStatusToDosError( status );
414 /******************************************************************************
415 * RegEnumKeyW [ADVAPI32.@]
417 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
419 return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
423 /******************************************************************************
424 * RegEnumKeyA [ADVAPI32.@]
426 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
428 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
432 /******************************************************************************
433 * RegQueryInfoKeyW [ADVAPI32.@]
436 * hkey [I] Handle to key to query
437 * class [O] Buffer for class string
438 * class_len [O] Size of class string buffer
439 * reserved [I] Reserved
440 * subkeys [O] Buffer for number of subkeys
441 * max_subkey [O] Buffer for longest subkey name length
442 * max_class [O] Buffer for longest class string length
443 * values [O] Buffer for number of value entries
444 * max_value [O] Buffer for longest value name length
445 * max_data [O] Buffer for longest value data length
446 * security [O] Buffer for security descriptor length
447 * modif [O] Modification time
449 * - win95 allows class to be valid and class_len to be NULL
450 * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
451 * - both allow class to be NULL and class_len to be NULL
452 * (it's hard to test validity, so test !NULL instead)
454 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
455 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
456 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
457 LPDWORD security, FILETIME *modif )
460 char buffer[256], *buf_ptr = buffer;
461 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
464 TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
465 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
467 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
469 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
470 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
474 /* retry with a dynamically allocated buffer */
475 while (status == STATUS_BUFFER_OVERFLOW)
477 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
478 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
479 return ERROR_NOT_ENOUGH_MEMORY;
480 info = (KEY_FULL_INFORMATION *)buf_ptr;
481 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
484 if (status) goto done;
486 if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
488 status = STATUS_BUFFER_OVERFLOW;
492 memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
493 class[info->ClassLength/sizeof(WCHAR)] = 0;
496 else status = STATUS_SUCCESS;
498 if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
499 if (subkeys) *subkeys = info->SubKeys;
500 if (max_subkey) *max_subkey = info->MaxNameLen;
501 if (max_class) *max_class = info->MaxClassLen;
502 if (values) *values = info->Values;
503 if (max_value) *max_value = info->MaxValueNameLen;
504 if (max_data) *max_data = info->MaxValueDataLen;
505 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
508 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
509 return RtlNtStatusToDosError( status );
513 /******************************************************************************
514 * RegQueryMultipleValuesA [ADVAPI32.@]
516 DWORD WINAPI RegQueryMultipleValuesA(HKEY hkey, PVALENTA val_list, DWORD num_vals,
517 LPSTR lpValueBuf, LPDWORD ldwTotsize)
520 DWORD maxBytes = *ldwTotsize;
522 LPSTR bufptr = lpValueBuf;
525 TRACE("(%x,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
527 for(i=0; i < num_vals; ++i)
530 val_list[i].ve_valuelen=0;
531 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
532 if(status != ERROR_SUCCESS)
537 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
539 status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
540 bufptr, &val_list[i].ve_valuelen);
541 if(status != ERROR_SUCCESS)
546 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
548 bufptr += val_list[i].ve_valuelen;
551 *ldwTotsize += val_list[i].ve_valuelen;
553 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
557 /******************************************************************************
558 * RegQueryMultipleValuesW [ADVAPI32.@]
560 DWORD WINAPI RegQueryMultipleValuesW(HKEY hkey, PVALENTW val_list, DWORD num_vals,
561 LPWSTR lpValueBuf, LPDWORD ldwTotsize)
564 DWORD maxBytes = *ldwTotsize;
566 LPSTR bufptr = (LPSTR)lpValueBuf;
569 TRACE("(%x,%p,%ld,%p,%p=%ld)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
571 for(i=0; i < num_vals; ++i)
573 val_list[i].ve_valuelen=0;
574 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
575 if(status != ERROR_SUCCESS)
580 if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
582 status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
583 bufptr, &val_list[i].ve_valuelen);
584 if(status != ERROR_SUCCESS)
589 val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
591 bufptr += val_list[i].ve_valuelen;
594 *ldwTotsize += val_list[i].ve_valuelen;
596 return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
599 /******************************************************************************
600 * RegQueryInfoKeyA [ADVAPI32.@]
602 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
603 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
604 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
605 LPDWORD security, FILETIME *modif )
608 char buffer[256], *buf_ptr = buffer;
609 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
610 DWORD total_size, len;
612 TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
613 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
615 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
617 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
618 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
620 if (class || class_len)
622 /* retry with a dynamically allocated buffer */
623 while (status == STATUS_BUFFER_OVERFLOW)
625 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
626 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
627 return ERROR_NOT_ENOUGH_MEMORY;
628 info = (KEY_FULL_INFORMATION *)buf_ptr;
629 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
632 if (status) goto done;
634 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
637 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
640 if (class && !status)
642 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
647 else status = STATUS_SUCCESS;
649 if (subkeys) *subkeys = info->SubKeys;
650 if (max_subkey) *max_subkey = info->MaxNameLen;
651 if (max_class) *max_class = info->MaxClassLen;
652 if (values) *values = info->Values;
653 if (max_value) *max_value = info->MaxValueNameLen;
654 if (max_data) *max_data = info->MaxValueDataLen;
655 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
658 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
659 return RtlNtStatusToDosError( status );
663 /******************************************************************************
664 * RegCloseKey [ADVAPI32.@]
666 * Releases the handle of the specified key
669 * hkey [I] Handle of key to close
672 * Success: ERROR_SUCCESS
673 * Failure: Error code
675 DWORD WINAPI RegCloseKey( HKEY hkey )
677 if (!hkey || hkey >= 0x80000000) return ERROR_SUCCESS;
678 return RtlNtStatusToDosError( NtClose( hkey ) );
682 /******************************************************************************
683 * RegDeleteKeyW [ADVAPI32.@]
686 * hkey [I] Handle to open key
687 * name [I] Name of subkey to delete
690 * Success: ERROR_SUCCESS
691 * Failure: Error code
693 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
700 ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
702 else if (!(ret = RegOpenKeyExW( hkey, name, 0, 0, &tmp )))
704 if (!is_version_nt()) /* win95 does recursive key deletes */
706 WCHAR name[MAX_PATH];
708 while(!RegEnumKeyW(tmp, 0, name, sizeof name))
710 if(RegDeleteKeyW(tmp, name)) /* recurse */
714 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
717 TRACE("%s ret=%08lx\n", debugstr_w(name), ret);
722 /******************************************************************************
723 * RegDeleteKeyA [ADVAPI32.@]
725 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
732 ret = RtlNtStatusToDosError( NtDeleteKey( hkey ) );
734 else if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, &tmp )))
736 if (!is_version_nt()) /* win95 does recursive key deletes */
740 while(!RegEnumKeyA(tmp, 0, name, sizeof name))
742 if(RegDeleteKeyA(tmp, name)) /* recurse */
746 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
749 TRACE("%s ret=%08lx\n", debugstr_a(name), ret);
755 /******************************************************************************
756 * RegSetValueExW [ADVAPI32.@]
758 * Sets the data and type of a value under a register key
761 * hkey [I] Handle of key to set value for
762 * name [I] Name of value to set
763 * reserved [I] Reserved - must be zero
764 * type [I] Flag for value type
765 * data [I] Address of value data
766 * count [I] Size of value data
769 * Success: ERROR_SUCCESS
770 * Failure: Error code
773 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
774 * NT does definitely care (aj)
776 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
777 DWORD type, CONST BYTE *data, DWORD count )
779 UNICODE_STRING nameW;
781 if (!is_version_nt()) /* win95 */
783 if (type == REG_SZ) count = (strlenW( (WCHAR *)data ) + 1) * sizeof(WCHAR);
785 else if (count && is_string(type))
787 LPCWSTR str = (LPCWSTR)data;
788 /* if user forgot to count terminating null, add it (yes NT does this) */
789 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
790 count += sizeof(WCHAR);
793 RtlInitUnicodeString( &nameW, name );
794 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
798 /******************************************************************************
799 * RegSetValueExA [ADVAPI32.@]
801 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
802 CONST BYTE *data, DWORD count )
808 if (!is_version_nt()) /* win95 */
810 if (type == REG_SZ) count = strlen(data) + 1;
812 else if (count && is_string(type))
814 /* if user forgot to count terminating null, add it (yes NT does this) */
815 if (data[count-1] && !data[count]) count++;
818 if (is_string( type )) /* need to convert to Unicode */
821 RtlMultiByteToUnicodeSize( &lenW, data, count );
822 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
823 RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
825 data = (BYTE *)dataW;
828 RtlInitAnsiString( &nameA, name );
829 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
832 status = NtSetValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString, 0, type, data, count );
834 if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
835 return RtlNtStatusToDosError( status );
839 /******************************************************************************
840 * RegSetValueW [ADVAPI32.@]
842 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
847 TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
849 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
851 if (name && name[0]) /* need to create the subkey */
853 if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
856 ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
857 (strlenW( data ) + 1) * sizeof(WCHAR) );
858 if (subkey != hkey) RegCloseKey( subkey );
863 /******************************************************************************
864 * RegSetValueA [ADVAPI32.@]
866 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
871 TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
873 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
875 if (name && name[0]) /* need to create the subkey */
877 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
879 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
880 if (subkey != hkey) RegCloseKey( subkey );
886 /******************************************************************************
887 * RegQueryValueExW [ADVAPI32.@]
889 * Retrieves type and data for a specified name associated with an open key
892 * hkey [I] Handle of key to query
893 * name [I] Name of value to query
894 * reserved [I] Reserved - must be NULL
895 * type [O] Address of buffer for value type. If NULL, the type
897 * data [O] Address of data buffer. If NULL, the actual data is
899 * count [I/O] Address of data buffer size
902 * ERROR_SUCCESS: Success
903 * ERROR_MORE_DATA: !!! if the specified buffer is not big enough to hold the data
904 * buffer is left untouched. The MS-documentation is wrong (js) !!!
906 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
907 LPBYTE data, LPDWORD count )
910 UNICODE_STRING name_str;
912 char buffer[256], *buf_ptr = buffer;
913 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
914 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
916 TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
917 hkey, debugstr_w(name), reserved, type, data, count, count ? *count : 0 );
919 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
921 RtlInitUnicodeString( &name_str, name );
923 if (data) total_size = min( sizeof(buffer), *count + info_size );
924 else total_size = info_size;
926 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
927 buffer, total_size, &total_size );
928 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
932 /* retry with a dynamically allocated buffer */
933 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
935 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
936 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
937 return ERROR_NOT_ENOUGH_MEMORY;
938 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
939 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
940 buf_ptr, total_size, &total_size );
945 memcpy( data, buf_ptr + info_size, total_size - info_size );
946 /* if the type is REG_SZ and data is not 0-terminated
947 * and there is enough space in the buffer NT appends a \0 */
948 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
950 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
951 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
954 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
956 else status = STATUS_SUCCESS;
958 if (type) *type = info->Type;
959 if (count) *count = total_size - info_size;
962 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
963 return RtlNtStatusToDosError(status);
967 /******************************************************************************
968 * RegQueryValueExA [ADVAPI32.@]
971 * the documentation is wrong: if the buffer is too small it remains untouched
973 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
974 LPBYTE data, LPDWORD count )
979 char buffer[256], *buf_ptr = buffer;
980 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
981 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
983 TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
984 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
986 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
988 RtlInitAnsiString( &nameA, name );
989 if ((status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
991 return RtlNtStatusToDosError(status);
993 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
994 KeyValuePartialInformation, buffer, sizeof(buffer), &total_size );
995 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
997 /* we need to fetch the contents for a string type even if not requested,
998 * because we need to compute the length of the ASCII string. */
999 if (data || is_string(info->Type))
1001 /* retry with a dynamically allocated buffer */
1002 while (status == STATUS_BUFFER_OVERFLOW)
1004 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1005 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1007 status = STATUS_NO_MEMORY;
1010 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
1011 status = NtQueryValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString,
1012 KeyValuePartialInformation, buf_ptr, total_size, &total_size );
1015 if (status) goto done;
1017 if (is_string(info->Type))
1021 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info_size),
1022 total_size - info_size );
1025 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1028 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info_size),
1029 total_size - info_size );
1030 /* if the type is REG_SZ and data is not 0-terminated
1031 * and there is enough space in the buffer NT appends a \0 */
1032 if (len < *count && data[len-1]) data[len] = 0;
1035 total_size = len + info_size;
1039 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
1040 else memcpy( data, buf_ptr + info_size, total_size - info_size );
1043 else status = STATUS_SUCCESS;
1045 if (type) *type = info->Type;
1046 if (count) *count = total_size - info_size;
1049 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1050 return RtlNtStatusToDosError(status);
1054 /******************************************************************************
1055 * RegQueryValueW [ADVAPI32.@]
1057 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
1062 TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
1064 if (name && name[0])
1066 if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1068 ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1069 if (subkey != hkey) RegCloseKey( subkey );
1070 if (ret == ERROR_FILE_NOT_FOUND)
1072 /* return empty string if default value not found */
1073 if (data) *data = 0;
1074 if (count) *count = 1;
1075 ret = ERROR_SUCCESS;
1081 /******************************************************************************
1082 * RegQueryValueA [ADVAPI32.@]
1084 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
1089 TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
1091 if (name && name[0])
1093 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
1095 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
1096 if (subkey != hkey) RegCloseKey( subkey );
1097 if (ret == ERROR_FILE_NOT_FOUND)
1099 /* return empty string if default value not found */
1100 if (data) *data = 0;
1101 if (count) *count = 1;
1102 ret = ERROR_SUCCESS;
1108 /******************************************************************************
1109 * RegEnumValueW [ADVAPI32.@]
1112 * hkey [I] Handle to key to query
1113 * index [I] Index of value to query
1114 * value [O] Value string
1115 * val_count [I/O] Size of value buffer (in wchars)
1116 * reserved [I] Reserved
1117 * type [O] Type code
1118 * data [O] Value data
1119 * count [I/O] Size of data buffer (in bytes)
1122 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
1123 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1127 char buffer[256], *buf_ptr = buffer;
1128 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1129 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1131 TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
1132 hkey, index, value, val_count, reserved, type, data, count );
1134 /* NT only checks count, not val_count */
1135 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1137 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1138 if (data) total_size += *count;
1139 total_size = min( sizeof(buffer), total_size );
1141 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1142 buffer, total_size, &total_size );
1143 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1147 /* retry with a dynamically allocated buffer */
1148 while (status == STATUS_BUFFER_OVERFLOW)
1150 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1151 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1152 return ERROR_NOT_ENOUGH_MEMORY;
1153 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1154 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1155 buf_ptr, total_size, &total_size );
1158 if (status) goto done;
1162 if (info->NameLength/sizeof(WCHAR) >= *val_count)
1164 status = STATUS_BUFFER_OVERFLOW;
1167 memcpy( value, info->Name, info->NameLength );
1168 *val_count = info->NameLength / sizeof(WCHAR);
1169 value[*val_count] = 0;
1174 if (total_size - info->DataOffset > *count)
1176 status = STATUS_BUFFER_OVERFLOW;
1179 memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1180 if (total_size - info->DataOffset <= *count-sizeof(WCHAR) && is_string(info->Type))
1182 /* if the type is REG_SZ and data is not 0-terminated
1183 * and there is enough space in the buffer NT appends a \0 */
1184 WCHAR *ptr = (WCHAR *)(data + total_size - info->DataOffset);
1185 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
1189 else status = STATUS_SUCCESS;
1192 if (type) *type = info->Type;
1193 if (count) *count = info->DataLength;
1196 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1197 return RtlNtStatusToDosError(status);
1201 /******************************************************************************
1202 * RegEnumValueA [ADVAPI32.@]
1204 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
1205 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
1209 char buffer[256], *buf_ptr = buffer;
1210 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1211 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
1213 TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
1214 hkey, index, value, val_count, reserved, type, data, count );
1216 /* NT only checks count, not val_count */
1217 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
1219 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
1220 if (data) total_size += *count;
1221 total_size = min( sizeof(buffer), total_size );
1223 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1224 buffer, total_size, &total_size );
1225 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
1227 /* we need to fetch the contents for a string type even if not requested,
1228 * because we need to compute the length of the ASCII string. */
1229 if (value || data || is_string(info->Type))
1231 /* retry with a dynamically allocated buffer */
1232 while (status == STATUS_BUFFER_OVERFLOW)
1234 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1235 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
1236 return ERROR_NOT_ENOUGH_MEMORY;
1237 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
1238 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
1239 buf_ptr, total_size, &total_size );
1242 if (status) goto done;
1244 if (is_string(info->Type))
1247 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
1248 total_size - info->DataOffset );
1251 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
1254 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
1255 total_size - info->DataOffset );
1256 /* if the type is REG_SZ and data is not 0-terminated
1257 * and there is enough space in the buffer NT appends a \0 */
1258 if (len < *count && data[len-1]) data[len] = 0;
1261 info->DataLength = len;
1265 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
1266 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
1269 if (value && !status)
1273 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
1274 if (len >= *val_count)
1276 status = STATUS_BUFFER_OVERFLOW;
1279 len = *val_count - 1;
1280 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1286 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
1292 else status = STATUS_SUCCESS;
1294 if (type) *type = info->Type;
1295 if (count) *count = info->DataLength;
1298 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
1299 return RtlNtStatusToDosError(status);
1304 /******************************************************************************
1305 * RegDeleteValueW [ADVAPI32.@]
1308 * hkey [I] handle to key
1309 * name [I] name of value to delete
1314 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
1316 UNICODE_STRING nameW;
1317 RtlInitUnicodeString( &nameW, name );
1318 return RtlNtStatusToDosError( NtDeleteValueKey( hkey, &nameW ) );
1322 /******************************************************************************
1323 * RegDeleteValueA [ADVAPI32.@]
1325 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
1330 RtlInitAnsiString( &nameA, name );
1331 if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
1334 status = NtDeleteValueKey( hkey, &NtCurrentTeb()->StaticUnicodeString );
1336 return RtlNtStatusToDosError( status );
1340 /******************************************************************************
1341 * RegLoadKeyW [ADVAPI32.@]
1344 * hkey [I] Handle of open key
1345 * subkey [I] Address of name of subkey
1346 * filename [I] Address of filename for registry information
1348 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
1351 DWORD ret, len, err = GetLastError();
1353 TRACE( "(%x,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
1355 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1356 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1358 len = strlenW( subkey ) * sizeof(WCHAR);
1359 if (len > MAX_PATH*sizeof(WCHAR)) return ERROR_INVALID_PARAMETER;
1361 if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1362 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1364 ret = GetLastError();
1368 SERVER_START_REQ( load_registry )
1372 wine_server_add_data( req, subkey, len );
1373 ret = RtlNtStatusToDosError( wine_server_call(req) );
1376 CloseHandle( file );
1379 SetLastError( err ); /* restore the last error code */
1384 /******************************************************************************
1385 * RegLoadKeyA [ADVAPI32.@]
1387 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1389 WCHAR buffer[MAX_PATH];
1391 DWORD ret, len, err = GetLastError();
1393 TRACE( "(%x,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
1395 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1396 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1398 if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
1399 return ERROR_INVALID_PARAMETER;
1401 if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1402 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
1404 ret = GetLastError();
1408 SERVER_START_REQ( load_registry )
1412 wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
1413 ret = RtlNtStatusToDosError( wine_server_call(req) );
1416 CloseHandle( file );
1419 SetLastError( err ); /* restore the last error code */
1424 /******************************************************************************
1425 * RegSaveKeyA [ADVAPI32.@]
1428 * hkey [I] Handle of key where save begins
1429 * lpFile [I] Address of filename to save to
1430 * sa [I] Address of security structure
1432 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1440 TRACE( "(%x,%s,%p)\n", hkey, debugstr_a(file), sa );
1442 if (!file || !*file) return ERROR_INVALID_PARAMETER;
1444 err = GetLastError();
1445 GetFullPathNameA( file, sizeof(buffer), buffer, &name );
1448 sprintf( name, "reg%04x.tmp", count++ );
1449 handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
1450 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1451 if (handle != INVALID_HANDLE_VALUE) break;
1452 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
1454 /* Something gone haywire ? Please report if this happens abnormally */
1456 MESSAGE("Wow, we are already fiddling with a temp file %s with an ordinal as high as %d !\nYou might want to delete all corresponding temp files in that directory.\n", buffer, count);
1459 SERVER_START_REQ( save_registry )
1463 ret = RtlNtStatusToDosError( wine_server_call( req ) );
1467 CloseHandle( handle );
1470 if (!MoveFileExA( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1472 ERR( "Failed to move %s to %s\n", buffer, file );
1473 ret = GetLastError();
1476 if (ret) DeleteFileA( buffer );
1479 SetLastError( err ); /* restore last error code */
1484 /******************************************************************************
1485 * RegSaveKeyW [ADVAPI32.@]
1487 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1489 LPSTR fileA = HEAP_strdupWtoA( GetProcessHeap(), 0, file );
1490 DWORD ret = RegSaveKeyA( hkey, fileA, sa );
1491 if (fileA) HeapFree( GetProcessHeap(), 0, fileA );
1496 /******************************************************************************
1497 * RegRestoreKeyW [ADVAPI32.@]
1500 * hkey [I] Handle of key where restore begins
1501 * lpFile [I] Address of filename containing saved tree
1502 * dwFlags [I] Optional flags
1504 LONG WINAPI RegRestoreKeyW( HKEY hkey, LPCWSTR lpFile, DWORD dwFlags )
1506 TRACE("(%x,%s,%ld)\n",hkey,debugstr_w(lpFile),dwFlags);
1508 /* It seems to do this check before the hkey check */
1509 if (!lpFile || !*lpFile)
1510 return ERROR_INVALID_PARAMETER;
1512 FIXME("(%x,%s,%ld): stub\n",hkey,debugstr_w(lpFile),dwFlags);
1514 /* Check for file existence */
1516 return ERROR_SUCCESS;
1520 /******************************************************************************
1521 * RegRestoreKeyA [ADVAPI32.@]
1523 LONG WINAPI RegRestoreKeyA( HKEY hkey, LPCSTR lpFile, DWORD dwFlags )
1525 LPWSTR lpFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpFile );
1526 LONG ret = RegRestoreKeyW( hkey, lpFileW, dwFlags );
1527 HeapFree( GetProcessHeap(), 0, lpFileW );
1532 /******************************************************************************
1533 * RegUnLoadKeyW [ADVAPI32.@]
1536 * hkey [I] Handle of open key
1537 * lpSubKey [I] Address of name of subkey to unload
1539 LONG WINAPI RegUnLoadKeyW( HKEY hkey, LPCWSTR lpSubKey )
1541 FIXME("(%x,%s): stub\n",hkey, debugstr_w(lpSubKey));
1542 return ERROR_SUCCESS;
1546 /******************************************************************************
1547 * RegUnLoadKeyA [ADVAPI32.@]
1549 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
1551 LPWSTR lpSubKeyW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpSubKey );
1552 LONG ret = RegUnLoadKeyW( hkey, lpSubKeyW );
1553 if(lpSubKeyW) HeapFree( GetProcessHeap(), 0, lpSubKeyW);
1558 /******************************************************************************
1559 * RegReplaceKeyW [ADVAPI32.@]
1562 * hkey [I] Handle of open key
1563 * lpSubKey [I] Address of name of subkey
1564 * lpNewFile [I] Address of filename for file with new data
1565 * lpOldFile [I] Address of filename for backup file
1567 LONG WINAPI RegReplaceKeyW( HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpNewFile,
1570 FIXME("(%x,%s,%s,%s): stub\n", hkey, debugstr_w(lpSubKey),
1571 debugstr_w(lpNewFile),debugstr_w(lpOldFile));
1572 return ERROR_SUCCESS;
1576 /******************************************************************************
1577 * RegReplaceKeyA [ADVAPI32.@]
1579 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
1582 LPWSTR lpSubKeyW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpSubKey );
1583 LPWSTR lpNewFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpNewFile );
1584 LPWSTR lpOldFileW = HEAP_strdupAtoW( GetProcessHeap(), 0, lpOldFile );
1585 LONG ret = RegReplaceKeyW( hkey, lpSubKeyW, lpNewFileW, lpOldFileW );
1586 HeapFree( GetProcessHeap(), 0, lpOldFileW );
1587 HeapFree( GetProcessHeap(), 0, lpNewFileW );
1588 HeapFree( GetProcessHeap(), 0, lpSubKeyW );
1593 /******************************************************************************
1594 * RegSetKeySecurity [ADVAPI32.@]
1597 * hkey [I] Open handle of key to set
1598 * SecurityInfo [I] Descriptor contents
1599 * pSecurityDesc [I] Address of descriptor for key
1601 LONG WINAPI RegSetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInfo,
1602 PSECURITY_DESCRIPTOR pSecurityDesc )
1604 TRACE("(%x,%ld,%p)\n",hkey,SecurityInfo,pSecurityDesc);
1606 /* It seems to perform this check before the hkey check */
1607 if ((SecurityInfo & OWNER_SECURITY_INFORMATION) ||
1608 (SecurityInfo & GROUP_SECURITY_INFORMATION) ||
1609 (SecurityInfo & DACL_SECURITY_INFORMATION) ||
1610 (SecurityInfo & SACL_SECURITY_INFORMATION)) {
1613 return ERROR_INVALID_PARAMETER;
1616 return ERROR_INVALID_PARAMETER;
1618 FIXME(":(%x,%ld,%p): stub\n",hkey,SecurityInfo,pSecurityDesc);
1620 return ERROR_SUCCESS;
1624 /******************************************************************************
1625 * RegGetKeySecurity [ADVAPI32.@]
1626 * Retrieves a copy of security descriptor protecting the registry key
1629 * hkey [I] Open handle of key to set
1630 * SecurityInformation [I] Descriptor contents
1631 * pSecurityDescriptor [O] Address of descriptor for key
1632 * lpcbSecurityDescriptor [I/O] Address of size of buffer and description
1635 * Success: ERROR_SUCCESS
1636 * Failure: Error code
1638 LONG WINAPI RegGetKeySecurity( HKEY hkey, SECURITY_INFORMATION SecurityInformation,
1639 PSECURITY_DESCRIPTOR pSecurityDescriptor,
1640 LPDWORD lpcbSecurityDescriptor )
1642 TRACE("(%x,%ld,%p,%ld)\n",hkey,SecurityInformation,pSecurityDescriptor,
1643 lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1645 /* FIXME: Check for valid SecurityInformation values */
1647 if (*lpcbSecurityDescriptor < sizeof(SECURITY_DESCRIPTOR))
1648 return ERROR_INSUFFICIENT_BUFFER;
1650 FIXME("(%x,%ld,%p,%ld): stub\n",hkey,SecurityInformation,
1651 pSecurityDescriptor,lpcbSecurityDescriptor?*lpcbSecurityDescriptor:0);
1653 /* Do not leave security descriptor filled with garbage */
1654 RtlCreateSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1656 return ERROR_SUCCESS;
1660 /******************************************************************************
1661 * RegFlushKey [ADVAPI32.@]
1662 * Immediately writes key to registry.
1663 * Only returns after data has been written to disk.
1665 * FIXME: does it really wait until data is written ?
1668 * hkey [I] Handle of key to write
1671 * Success: ERROR_SUCCESS
1672 * Failure: Error code
1674 DWORD WINAPI RegFlushKey( HKEY hkey )
1676 FIXME( "(%x): stub\n", hkey );
1677 return ERROR_SUCCESS;
1681 /******************************************************************************
1682 * RegConnectRegistryW [ADVAPI32.@]
1685 * lpMachineName [I] Address of name of remote computer
1686 * hHey [I] Predefined registry handle
1687 * phkResult [I] Address of buffer for remote registry handle
1689 LONG WINAPI RegConnectRegistryW( LPCWSTR lpMachineName, HKEY hKey,
1692 TRACE("(%s,%x,%p): stub\n",debugstr_w(lpMachineName),hKey,phkResult);
1694 if (!lpMachineName || !*lpMachineName) {
1695 /* Use the local machine name */
1696 return RegOpenKeyA( hKey, "", phkResult );
1699 FIXME("Cannot connect to %s\n",debugstr_w(lpMachineName));
1700 return ERROR_BAD_NETPATH;
1704 /******************************************************************************
1705 * RegConnectRegistryA [ADVAPI32.@]
1707 LONG WINAPI RegConnectRegistryA( LPCSTR machine, HKEY hkey, LPHKEY reskey )
1709 LPWSTR machineW = HEAP_strdupAtoW( GetProcessHeap(), 0, machine );
1710 DWORD ret = RegConnectRegistryW( machineW, hkey, reskey );
1711 HeapFree( GetProcessHeap(), 0, machineW );
1716 /******************************************************************************
1717 * RegNotifyChangeKeyValue [ADVAPI32.@]
1720 * hkey [I] Handle of key to watch
1721 * fWatchSubTree [I] Flag for subkey notification
1722 * fdwNotifyFilter [I] Changes to be reported
1723 * hEvent [I] Handle of signaled event
1724 * fAsync [I] Flag for asynchronous reporting
1726 LONG WINAPI RegNotifyChangeKeyValue( HKEY hkey, BOOL fWatchSubTree,
1727 DWORD fdwNotifyFilter, HANDLE hEvent,
1730 FIXME("(%x,%i,%ld,%x,%i): stub\n",hkey,fWatchSubTree,fdwNotifyFilter,
1732 return ERROR_SUCCESS;