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
40 #include "wine/winbase16.h"
41 #include "wine/unicode.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(reg);
48 /* check if value type needs string conversion (Ansi<->Unicode) */
49 inline static int is_string( DWORD type )
51 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
54 /* check if current version is NT or Win95 */
55 inline static int is_version_nt(void)
57 return !(GetVersion() & 0x80000000);
60 /******************************************************************************
61 * RegCreateKeyExA [ADVAPI32.@]
63 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
64 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
65 LPHKEY retkey, LPDWORD dispos )
67 OBJECT_ATTRIBUTES attr;
68 UNICODE_STRING nameW, classW;
69 ANSI_STRING nameA, classA;
72 if (reserved) return ERROR_INVALID_PARAMETER;
73 if (!(access & KEY_ALL_ACCESS) || (access & ~KEY_ALL_ACCESS)) return ERROR_ACCESS_DENIED;
75 attr.Length = sizeof(attr);
76 attr.RootDirectory = hkey;
77 attr.ObjectName = &nameW;
79 attr.SecurityDescriptor = NULL;
80 attr.SecurityQualityOfService = NULL;
81 RtlInitAnsiString( &nameA, name );
82 RtlInitAnsiString( &classA, class );
84 /* FIXME: should use Unicode buffer in TEB */
85 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
87 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
89 status = NtCreateKey( retkey, access, &attr, 0, &classW, options, dispos );
90 RtlFreeUnicodeString( &classW );
92 RtlFreeUnicodeString( &nameW );
94 return RtlNtStatusToDosError( status );
98 /******************************************************************************
99 * RegOpenKeyW [ADVAPI32.@]
102 * hkey [I] Handle of open key
103 * name [I] Address of name of subkey to open
104 * retkey [O] Handle to open key
107 * Success: ERROR_SUCCESS
108 * Failure: Error code
111 * in case of failing is retkey = 0
113 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
115 return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
119 /******************************************************************************
120 * RegCreateKeyA [ADVAPI32.@]
122 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
124 return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
125 KEY_ALL_ACCESS, NULL, retkey, NULL );
130 /******************************************************************************
131 * RegOpenKeyExW [ADVAPI32.@]
133 * Opens the specified key
135 * Unlike RegCreateKeyEx, this does not create the key if it does not exist.
138 * hkey [I] Handle of open key
139 * name [I] Name of subkey to open
140 * reserved [I] Reserved - must be zero
141 * access [I] Security access mask
142 * retkey [O] Handle to open key
145 * Success: ERROR_SUCCESS
146 * Failure: Error code
149 * in case of failing is retkey = 0
151 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
153 OBJECT_ATTRIBUTES attr;
154 UNICODE_STRING nameW;
156 attr.Length = sizeof(attr);
157 attr.RootDirectory = hkey;
158 attr.ObjectName = &nameW;
160 attr.SecurityDescriptor = NULL;
161 attr.SecurityQualityOfService = NULL;
162 RtlInitUnicodeString( &nameW, name );
163 return RtlNtStatusToDosError( NtOpenKey( retkey, access, &attr ) );
167 /******************************************************************************
168 * RegOpenKeyExA [ADVAPI32.@]
170 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
172 OBJECT_ATTRIBUTES attr;
173 UNICODE_STRING nameW;
177 attr.Length = sizeof(attr);
178 attr.RootDirectory = hkey;
179 attr.ObjectName = &nameW;
181 attr.SecurityDescriptor = NULL;
182 attr.SecurityQualityOfService = NULL;
184 RtlInitAnsiString( &nameA, name );
185 /* FIXME: should use Unicode buffer in TEB */
186 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
188 status = NtOpenKey( retkey, access, &attr );
189 RtlFreeUnicodeString( &nameW );
191 return RtlNtStatusToDosError( status );
195 /******************************************************************************
196 * RegOpenKeyA [ADVAPI32.@]
198 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
200 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
204 /******************************************************************************
205 * RegEnumKeyExA [ADVAPI32.@]
207 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
208 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
211 char buffer[256], *buf_ptr = buffer;
212 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
215 TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
216 name_len ? *name_len : -1, reserved, class, class_len, ft );
218 if (reserved) return ERROR_INVALID_PARAMETER;
220 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
221 buffer, sizeof(buffer), &total_size );
223 while (status == STATUS_BUFFER_OVERFLOW)
225 /* retry with a dynamically allocated buffer */
226 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
227 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
228 return ERROR_NOT_ENOUGH_MEMORY;
229 info = (KEY_NODE_INFORMATION *)buf_ptr;
230 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
231 buf_ptr, total_size, &total_size );
238 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
239 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
241 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
243 if (len >= *name_len || (class_len && (cls_len >= *class_len)))
244 status = STATUS_BUFFER_OVERFLOW;
248 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
252 *class_len = cls_len;
255 RtlUnicodeToMultiByteN( class, cls_len, NULL,
256 (WCHAR *)(buf_ptr + info->ClassOffset),
264 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
265 return RtlNtStatusToDosError( status );
269 /******************************************************************************
270 * RegEnumKeyA [ADVAPI32.@]
272 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
274 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
278 /******************************************************************************
279 * RegQueryInfoKeyA [ADVAPI32.@]
281 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
282 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
283 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
284 LPDWORD security, FILETIME *modif )
287 char buffer[256], *buf_ptr = buffer;
288 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
289 DWORD total_size, len;
291 TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
292 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
294 if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
296 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
297 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
299 if (class || class_len)
301 /* retry with a dynamically allocated buffer */
302 while (status == STATUS_BUFFER_OVERFLOW)
304 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
305 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
306 return ERROR_NOT_ENOUGH_MEMORY;
307 info = (KEY_FULL_INFORMATION *)buf_ptr;
308 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
311 if (status) goto done;
313 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
316 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
319 if (class && !status)
321 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
326 else status = STATUS_SUCCESS;
328 if (subkeys) *subkeys = info->SubKeys;
329 if (max_subkey) *max_subkey = info->MaxNameLen;
330 if (max_class) *max_class = info->MaxClassLen;
331 if (values) *values = info->Values;
332 if (max_value) *max_value = info->MaxValueNameLen;
333 if (max_data) *max_data = info->MaxValueDataLen;
334 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
337 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
338 return RtlNtStatusToDosError( status );
342 /******************************************************************************
343 * RegCloseKey [ADVAPI32.@]
345 * Releases the handle of the specified key
348 * hkey [I] Handle of key to close
351 * Success: ERROR_SUCCESS
352 * Failure: Error code
354 DWORD WINAPI RegCloseKey( HKEY hkey )
356 if (!hkey || hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
357 return RtlNtStatusToDosError( NtClose( hkey ) );
361 /******************************************************************************
362 * RegDeleteKeyA [ADVAPI32.@]
364 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
369 if (!name || !*name) return NtDeleteKey( hkey );
370 if (!(ret = RegOpenKeyExA( hkey, name, 0, 0, &tmp )))
372 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
379 /******************************************************************************
380 * RegSetValueExW [ADVAPI32.@]
382 * Sets the data and type of a value under a register key
385 * hkey [I] Handle of key to set value for
386 * name [I] Name of value to set
387 * reserved [I] Reserved - must be zero
388 * type [I] Flag for value type
389 * data [I] Address of value data
390 * count [I] Size of value data
393 * Success: ERROR_SUCCESS
394 * Failure: Error code
397 * win95 does not care about count for REG_SZ and finds out the len by itself (js)
398 * NT does definitely care (aj)
400 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
401 DWORD type, CONST BYTE *data, DWORD count )
403 UNICODE_STRING nameW;
405 if (!is_version_nt()) /* win95 */
407 if (type == REG_SZ) count = (strlenW( (WCHAR *)data ) + 1) * sizeof(WCHAR);
409 else if (count && is_string(type))
411 LPCWSTR str = (LPCWSTR)data;
412 /* if user forgot to count terminating null, add it (yes NT does this) */
413 if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
414 count += sizeof(WCHAR);
417 RtlInitUnicodeString( &nameW, name );
418 return RtlNtStatusToDosError( NtSetValueKey( hkey, &nameW, 0, type, data, count ) );
422 /******************************************************************************
423 * RegSetValueExA [ADVAPI32.@]
425 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
426 CONST BYTE *data, DWORD count )
428 UNICODE_STRING nameW;
433 if (count && is_string(type))
435 /* if user forgot to count terminating null, add it (yes NT does this) */
436 if (data[count-1] && !data[count]) count++;
439 if (is_string( type )) /* need to convert to Unicode */
442 RtlMultiByteToUnicodeSize( &lenW, data, count );
443 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
444 RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
446 data = (BYTE *)dataW;
449 RtlInitAnsiString( &nameA, name );
450 /* FIXME: should use Unicode buffer in TEB */
451 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
453 status = NtSetValueKey( hkey, &nameW, 0, type, data, count );
454 RtlFreeUnicodeString( &nameW );
456 if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
457 return RtlNtStatusToDosError( status );
461 /******************************************************************************
462 * RegSetValueA [ADVAPI32.@]
464 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
469 TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
471 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
473 if (name && name[0]) /* need to create the subkey */
475 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
477 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
478 if (subkey != hkey) RegCloseKey( subkey );
483 /******************************************************************************
484 * RegQueryValueExW [ADVAPI32.@]
486 * Retrieves type and data for a specified name associated with an open key
489 * hkey [I] Handle of key to query
490 * name [I] Name of value to query
491 * reserved [I] Reserved - must be NULL
492 * type [O] Address of buffer for value type. If NULL, the type
494 * data [O] Address of data buffer. If NULL, the actual data is
496 * count [I/O] Address of data buffer size
499 * ERROR_SUCCESS: Success
500 * ERROR_MORE_DATA: !!! if the specified buffer is not big enough to hold the data
501 * buffer is left untouched. The MS-documentation is wrong (js) !!!
503 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
504 LPBYTE data, LPDWORD count )
507 UNICODE_STRING name_str;
509 char buffer[256], *buf_ptr = buffer;
510 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
511 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
513 TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
514 hkey, debugstr_w(name), reserved, type, data, count, count ? *count : 0 );
516 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
518 RtlInitUnicodeString( &name_str, name );
520 if (data) total_size = min( sizeof(buffer), *count + info_size );
521 else total_size = info_size;
523 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
524 buffer, total_size, &total_size );
525 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
529 /* retry with a dynamically allocated buffer */
530 while (status == STATUS_BUFFER_OVERFLOW && total_size - info_size <= *count)
532 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
533 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
534 return ERROR_NOT_ENOUGH_MEMORY;
535 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
536 status = NtQueryValueKey( hkey, &name_str, KeyValuePartialInformation,
537 buf_ptr, total_size, &total_size );
542 memcpy( data, buf_ptr + info_size, total_size - info_size );
543 /* if the type is REG_SZ and data is not 0-terminated
544 * and there is enough space in the buffer NT appends a \0 */
545 if (total_size - info_size <= *count-sizeof(WCHAR) && is_string(info->Type))
547 WCHAR *ptr = (WCHAR *)(data + total_size - info_size);
548 if (ptr > (WCHAR *)data && ptr[-1]) *ptr = 0;
551 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
553 else status = STATUS_SUCCESS;
555 if (type) *type = info->Type;
556 if (count) *count = total_size - info_size;
559 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
560 return RtlNtStatusToDosError(status);
564 /******************************************************************************
565 * RegQueryValueExA [ADVAPI32.@]
568 * the documentation is wrong: if the buffer is too small it remains untouched
570 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
571 LPBYTE data, LPDWORD count )
575 UNICODE_STRING nameW;
577 char buffer[256], *buf_ptr = buffer;
578 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
579 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
581 TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
582 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
584 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
586 RtlInitAnsiString( &nameA, name );
587 /* FIXME: should use Unicode buffer in TEB */
588 if ((status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
589 return RtlNtStatusToDosError(status);
591 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
592 buffer, sizeof(buffer), &total_size );
593 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
595 /* we need to fetch the contents for a string type even if not requested,
596 * because we need to compute the length of the ASCII string. */
597 if (data || is_string(info->Type))
599 /* retry with a dynamically allocated buffer */
600 while (status == STATUS_BUFFER_OVERFLOW)
602 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
603 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
605 status = STATUS_NO_MEMORY;
608 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
609 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
610 buf_ptr, total_size, &total_size );
615 if (is_string(info->Type))
617 DWORD len = WideCharToMultiByte( CP_ACP, 0, (WCHAR *)(buf_ptr + info_size),
618 (total_size - info_size) /sizeof(WCHAR),
619 NULL, 0, NULL, NULL );
622 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
625 WideCharToMultiByte( CP_ACP, 0, (WCHAR *)(buf_ptr + info_size),
626 (total_size - info_size) /sizeof(WCHAR),
627 data, len, NULL, NULL );
628 /* if the type is REG_SZ and data is not 0-terminated
629 * and there is enough space in the buffer NT appends a \0 */
630 if (len < *count && data[len-1]) data[len] = 0;
633 total_size = len + info_size;
637 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
638 else memcpy( data, buf_ptr + info_size, total_size - info_size );
641 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
644 if (type) *type = info->Type;
645 if (count) *count = total_size - info_size;
648 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
649 RtlFreeUnicodeString( &nameW );
650 return RtlNtStatusToDosError(status);
654 /******************************************************************************
655 * RegQueryValueA [ADVAPI32.@]
657 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
662 TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
666 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
668 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
669 if (subkey != hkey) RegCloseKey( subkey );
670 if (ret == ERROR_FILE_NOT_FOUND)
672 /* return empty string if default value not found */
674 if (count) *count = 1;
681 /******************************************************************************
682 * RegEnumValueA [ADVAPI32.@]
684 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
685 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
689 char buffer[256], *buf_ptr = buffer;
690 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
691 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
693 TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
694 hkey, index, value, val_count, reserved, type, data, count );
696 /* NT only checks count, not val_count */
697 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
699 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
700 if (data) total_size += *count;
701 total_size = min( sizeof(buffer), total_size );
703 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
704 buffer, total_size, &total_size );
705 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
707 /* we need to fetch the contents for a string type even if not requested,
708 * because we need to compute the length of the ASCII string. */
709 if (value || data || is_string(info->Type))
711 /* retry with a dynamically allocated buffer */
712 while (status == STATUS_BUFFER_OVERFLOW)
714 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
715 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
716 return ERROR_NOT_ENOUGH_MEMORY;
717 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
718 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
719 buf_ptr, total_size, &total_size );
722 if (status) goto done;
724 if (is_string(info->Type))
727 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
728 total_size - info->DataOffset );
731 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
734 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
735 total_size - info->DataOffset );
736 /* if the type is REG_SZ and data is not 0-terminated
737 * and there is enough space in the buffer NT appends a \0 */
738 if (len < *count && data[len-1]) data[len] = 0;
741 info->DataLength = len;
745 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
746 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
749 if (value && !status)
753 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
754 if (len >= *val_count)
756 status = STATUS_BUFFER_OVERFLOW;
759 len = *val_count - 1;
760 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
766 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
772 else status = STATUS_SUCCESS;
774 if (type) *type = info->Type;
775 if (count) *count = info->DataLength;
778 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
779 return RtlNtStatusToDosError(status);
784 /******************************************************************************
785 * RegDeleteValueA [ADVAPI32.@]
787 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
789 UNICODE_STRING nameW;
793 RtlInitAnsiString( &nameA, name );
794 /* FIXME: should use Unicode buffer in TEB */
795 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
797 status = NtDeleteValueKey( hkey, &nameW );
798 RtlFreeUnicodeString( &nameW );
800 return RtlNtStatusToDosError( status );
804 /******************************************************************************
805 * RegLoadKeyA [ADVAPI32.@]
807 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
810 WCHAR buffer[MAX_PATH];
811 DWORD ret, len, err = GetLastError();
813 TRACE( "(%x,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
815 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
816 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
818 if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
819 return ERROR_INVALID_PARAMETER;
821 if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
822 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
824 ret = GetLastError();
828 SERVER_START_REQ( load_registry )
832 wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
833 ret = RtlNtStatusToDosError( wine_server_call(req) );
839 SetLastError( err ); /* restore the last error code */
844 /******************************************************************************
845 * RegSaveKeyA [ADVAPI32.@]
848 * hkey [I] Handle of key where save begins
849 * lpFile [I] Address of filename to save to
850 * sa [I] Address of security structure
852 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
860 TRACE( "(%x,%s,%p)\n", hkey, debugstr_a(file), sa );
862 if (!file || !*file) return ERROR_INVALID_PARAMETER;
864 err = GetLastError();
865 GetFullPathNameA( file, sizeof(buffer), buffer, &name );
868 sprintf( name, "reg%04x.tmp", count++ );
869 handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
870 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
871 if (handle != INVALID_HANDLE_VALUE) break;
872 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
874 /* Something gone haywire ? Please report if this happens abnormally */
876 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);
879 SERVER_START_REQ( save_registry )
883 ret = RtlNtStatusToDosError( wine_server_call( req ) );
887 CloseHandle( handle );
890 if (!MoveFileExA( buffer, file, MOVEFILE_REPLACE_EXISTING ))
892 ERR( "Failed to move %s to %s\n", buffer, file );
893 ret = GetLastError();
896 if (ret) DeleteFileA( buffer );
899 SetLastError( err ); /* restore last error code */
904 /******************************************************************************
905 * RegUnLoadKeyA [ADVAPI32.@]
908 * hkey [I] Handle of open key
909 * lpSubKey [I] Address of name of subkey to unload
911 LONG WINAPI RegUnLoadKeyA( HKEY hkey, LPCSTR lpSubKey )
913 FIXME("(%x,%s): stub\n",hkey, debugstr_a(lpSubKey));
914 return ERROR_SUCCESS;
918 /******************************************************************************
919 * RegReplaceKeyA [ADVAPI32.@]
922 * hkey [I] Handle of open key
923 * lpSubKey [I] Address of name of subkey
924 * lpNewFile [I] Address of filename for file with new data
925 * lpOldFile [I] Address of filename for backup file
927 LONG WINAPI RegReplaceKeyA( HKEY hkey, LPCSTR lpSubKey, LPCSTR lpNewFile,
930 FIXME("(%x,%s,%s,%s): stub\n", hkey, debugstr_a(lpSubKey),
931 debugstr_a(lpNewFile),debugstr_a(lpOldFile));
932 return ERROR_SUCCESS;
936 /******************************************************************************
937 * RegFlushKey [ADVAPI32.@]
938 * Immediately writes key to registry.
939 * Only returns after data has been written to disk.
941 * FIXME: does it really wait until data is written ?
944 * hkey [I] Handle of key to write
947 * Success: ERROR_SUCCESS
948 * Failure: Error code
950 DWORD WINAPI RegFlushKey( HKEY hkey )
952 FIXME( "(%x): stub\n", hkey );
953 return ERROR_SUCCESS;