Correct the number of parameters for StrRStrIA/W.
[wine.git] / memory / registry.c
blobed1b5f0a7f52586e4035aafda0492511eff911ae
1 /*
2 * Registry management
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
29 #include "config.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
37 #include "winbase.h"
38 #include "winreg.h"
39 #include "winerror.h"
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 static inline int is_string( DWORD type )
51 return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
55 /******************************************************************************
56 * RegCreateKeyExA [ADVAPI32.@]
58 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
59 DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
60 LPHKEY retkey, LPDWORD dispos )
62 OBJECT_ATTRIBUTES attr;
63 UNICODE_STRING nameW, classW;
64 ANSI_STRING nameA, classA;
65 NTSTATUS status;
67 if (reserved) return ERROR_INVALID_PARAMETER;
68 if (!(access & KEY_ALL_ACCESS) || (access & ~KEY_ALL_ACCESS)) return ERROR_ACCESS_DENIED;
70 attr.Length = sizeof(attr);
71 attr.RootDirectory = hkey;
72 attr.ObjectName = &nameW;
73 attr.Attributes = 0;
74 attr.SecurityDescriptor = NULL;
75 attr.SecurityQualityOfService = NULL;
76 RtlInitAnsiString( &nameA, name );
77 RtlInitAnsiString( &classA, class );
79 /* FIXME: should use Unicode buffer in TEB */
80 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
82 if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
84 status = NtCreateKey( retkey, access, &attr, 0, &classW, options, dispos );
85 RtlFreeUnicodeString( &classW );
87 RtlFreeUnicodeString( &nameW );
89 return RtlNtStatusToDosError( status );
93 /******************************************************************************
94 * RegCreateKeyA [ADVAPI32.@]
96 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
98 return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
99 KEY_ALL_ACCESS, NULL, retkey, NULL );
104 /******************************************************************************
105 * RegOpenKeyExA [ADVAPI32.@]
107 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
109 OBJECT_ATTRIBUTES attr;
110 UNICODE_STRING nameW;
111 STRING nameA;
112 NTSTATUS status;
114 attr.Length = sizeof(attr);
115 attr.RootDirectory = hkey;
116 attr.ObjectName = &nameW;
117 attr.Attributes = 0;
118 attr.SecurityDescriptor = NULL;
119 attr.SecurityQualityOfService = NULL;
121 RtlInitAnsiString( &nameA, name );
122 /* FIXME: should use Unicode buffer in TEB */
123 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
125 status = NtOpenKey( retkey, access, &attr );
126 RtlFreeUnicodeString( &nameW );
128 return RtlNtStatusToDosError( status );
132 /******************************************************************************
133 * RegOpenKeyA [ADVAPI32.@]
135 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
137 return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
141 /******************************************************************************
142 * RegEnumKeyExA [ADVAPI32.@]
144 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
145 LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
147 NTSTATUS status;
148 char buffer[256], *buf_ptr = buffer;
149 KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
150 DWORD total_size;
152 TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
153 name_len ? *name_len : -1, reserved, class, class_len, ft );
155 if (reserved) return ERROR_INVALID_PARAMETER;
157 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
158 buffer, sizeof(buffer), &total_size );
160 while (status == STATUS_BUFFER_OVERFLOW)
162 /* retry with a dynamically allocated buffer */
163 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
164 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
165 return ERROR_NOT_ENOUGH_MEMORY;
166 info = (KEY_NODE_INFORMATION *)buf_ptr;
167 status = NtEnumerateKey( hkey, index, KeyNodeInformation,
168 buf_ptr, total_size, &total_size );
171 if (!status)
173 DWORD len, cls_len;
175 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
176 RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
177 info->ClassLength );
178 if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
180 if (len >= *name_len || (class_len && (cls_len >= *class_len)))
181 status = STATUS_BUFFER_OVERFLOW;
182 else
184 *name_len = len;
185 RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
186 name[len] = 0;
187 if (class_len)
189 *class_len = cls_len;
190 if (class)
192 RtlUnicodeToMultiByteN( class, cls_len, NULL,
193 (WCHAR *)(buf_ptr + info->ClassOffset),
194 info->ClassLength );
195 class[cls_len] = 0;
201 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
202 return RtlNtStatusToDosError( status );
206 /******************************************************************************
207 * RegEnumKeyA [ADVAPI32.@]
209 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
211 return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
215 /******************************************************************************
216 * RegQueryInfoKeyA [ADVAPI32.@]
218 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
219 LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
220 LPDWORD values, LPDWORD max_value, LPDWORD max_data,
221 LPDWORD security, FILETIME *modif )
223 NTSTATUS status;
224 char buffer[256], *buf_ptr = buffer;
225 KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
226 DWORD total_size, len;
228 TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
229 reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
231 if (class && !class_len && !(GetVersion() & 0x80000000 /*NT*/))
232 return ERROR_INVALID_PARAMETER;
234 status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
235 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
237 if (class || class_len)
239 /* retry with a dynamically allocated buffer */
240 while (status == STATUS_BUFFER_OVERFLOW)
242 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
243 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
244 return ERROR_NOT_ENOUGH_MEMORY;
245 info = (KEY_FULL_INFORMATION *)buf_ptr;
246 status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
249 if (status) goto done;
251 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->ClassOffset), info->ClassLength);
252 if (class_len)
254 if (len + 1 > *class_len) status = STATUS_BUFFER_OVERFLOW;
255 *class_len = len;
257 if (class && !status)
259 RtlUnicodeToMultiByteN( class, len, NULL, (WCHAR *)(buf_ptr + info->ClassOffset),
260 info->ClassLength );
261 class[len] = 0;
264 else status = STATUS_SUCCESS;
266 if (subkeys) *subkeys = info->SubKeys;
267 if (max_subkey) *max_subkey = info->MaxNameLen;
268 if (max_class) *max_class = info->MaxClassLen;
269 if (values) *values = info->Values;
270 if (max_value) *max_value = info->MaxValueNameLen;
271 if (max_data) *max_data = info->MaxValueDataLen;
272 if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
274 done:
275 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
276 return RtlNtStatusToDosError( status );
280 /******************************************************************************
281 * RegCloseKey [ADVAPI32.@]
283 * Releases the handle of the specified key
285 * PARAMS
286 * hkey [I] Handle of key to close
288 * RETURNS
289 * Success: ERROR_SUCCESS
290 * Failure: Error code
292 DWORD WINAPI RegCloseKey( HKEY hkey )
294 if (!hkey || hkey >= (HKEY)0x80000000) return ERROR_SUCCESS;
295 return RtlNtStatusToDosError( NtClose( hkey ) );
299 /******************************************************************************
300 * RegDeleteKeyA [ADVAPI32.@]
302 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
304 DWORD ret;
305 HKEY tmp;
307 if (!name || !*name) return NtDeleteKey( hkey );
308 if (!(ret = RegOpenKeyExA( hkey, name, 0, 0, &tmp )))
310 ret = RtlNtStatusToDosError( NtDeleteKey( tmp ) );
311 RegCloseKey( tmp );
313 return ret;
318 /******************************************************************************
319 * RegSetValueExA [ADVAPI32.@]
321 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
322 CONST BYTE *data, DWORD count )
324 UNICODE_STRING nameW;
325 ANSI_STRING nameA;
326 WCHAR *dataW = NULL;
327 NTSTATUS status;
329 if (count && is_string(type))
331 /* if user forgot to count terminating null, add it (yes NT does this) */
332 if (data[count-1] && !data[count]) count++;
335 if (is_string( type )) /* need to convert to Unicode */
337 DWORD lenW;
338 RtlMultiByteToUnicodeSize( &lenW, data, count );
339 if (!(dataW = HeapAlloc( GetProcessHeap(), 0, lenW ))) return ERROR_OUTOFMEMORY;
340 RtlMultiByteToUnicodeN( dataW, lenW, NULL, data, count );
341 count = lenW;
342 data = (BYTE *)dataW;
345 RtlInitAnsiString( &nameA, name );
346 /* FIXME: should use Unicode buffer in TEB */
347 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
349 status = NtSetValueKey( hkey, &nameW, 0, type, data, count );
350 RtlFreeUnicodeString( &nameW );
352 if (dataW) HeapFree( GetProcessHeap(), 0, dataW );
353 return RtlNtStatusToDosError( status );
357 /******************************************************************************
358 * RegSetValueA [ADVAPI32.@]
360 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
362 HKEY subkey = hkey;
363 DWORD ret;
365 TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
367 if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
369 if (name && name[0]) /* need to create the subkey */
371 if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
373 ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
374 if (subkey != hkey) RegCloseKey( subkey );
375 return ret;
380 /******************************************************************************
381 * RegQueryValueExA [ADVAPI32.@]
383 * NOTES:
384 * the documentation is wrong: if the buffer is too small it remains untouched
386 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
387 LPBYTE data, LPDWORD count )
389 NTSTATUS status;
390 ANSI_STRING nameA;
391 UNICODE_STRING nameW;
392 DWORD total_size;
393 char buffer[256], *buf_ptr = buffer;
394 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
395 static const int info_size = offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data );
397 TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
398 hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
400 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
402 RtlInitAnsiString( &nameA, name );
403 /* FIXME: should use Unicode buffer in TEB */
404 if ((status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
405 return RtlNtStatusToDosError(status);
407 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
408 buffer, sizeof(buffer), &total_size );
409 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
411 /* we need to fetch the contents for a string type even if not requested,
412 * because we need to compute the length of the ASCII string. */
413 if (data || is_string(info->Type))
415 /* retry with a dynamically allocated buffer */
416 while (status == STATUS_BUFFER_OVERFLOW)
418 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
419 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
421 status = STATUS_NO_MEMORY;
422 goto done;
424 info = (KEY_VALUE_PARTIAL_INFORMATION *)buf_ptr;
425 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
426 buf_ptr, total_size, &total_size );
429 if (!status)
431 if (is_string(info->Type))
433 DWORD len = WideCharToMultiByte( CP_ACP, 0, (WCHAR *)(buf_ptr + info_size),
434 (total_size - info_size) /sizeof(WCHAR),
435 NULL, 0, NULL, NULL );
436 if (data && len)
438 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
439 else
441 WideCharToMultiByte( CP_ACP, 0, (WCHAR *)(buf_ptr + info_size),
442 (total_size - info_size) /sizeof(WCHAR),
443 data, len, NULL, NULL );
444 /* if the type is REG_SZ and data is not 0-terminated
445 * and there is enough space in the buffer NT appends a \0 */
446 if (len < *count && data[len-1]) data[len] = 0;
449 total_size = len + info_size;
451 else if (data)
453 if (total_size - info_size > *count) status = STATUS_BUFFER_OVERFLOW;
454 else memcpy( data, buf_ptr + info_size, total_size - info_size );
457 else if (status != STATUS_BUFFER_OVERFLOW) goto done;
460 if (type) *type = info->Type;
461 if (count) *count = total_size - info_size;
463 done:
464 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
465 RtlFreeUnicodeString( &nameW );
466 return RtlNtStatusToDosError(status);
470 /******************************************************************************
471 * RegQueryValueA [ADVAPI32.@]
473 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
475 DWORD ret;
476 HKEY subkey = hkey;
478 TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
480 if (name && name[0])
482 if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
484 ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
485 if (subkey != hkey) RegCloseKey( subkey );
486 if (ret == ERROR_FILE_NOT_FOUND)
488 /* return empty string if default value not found */
489 if (data) *data = 0;
490 if (count) *count = 1;
491 ret = ERROR_SUCCESS;
493 return ret;
497 /******************************************************************************
498 * RegEnumValueA [ADVAPI32.@]
500 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
501 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
503 NTSTATUS status;
504 DWORD total_size;
505 char buffer[256], *buf_ptr = buffer;
506 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
507 static const int info_size = offsetof( KEY_VALUE_FULL_INFORMATION, Name );
509 TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
510 hkey, index, value, val_count, reserved, type, data, count );
512 /* NT only checks count, not val_count */
513 if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
515 total_size = info_size + (MAX_PATH + 1) * sizeof(WCHAR);
516 if (data) total_size += *count;
517 total_size = min( sizeof(buffer), total_size );
519 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
520 buffer, total_size, &total_size );
521 if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
523 /* we need to fetch the contents for a string type even if not requested,
524 * because we need to compute the length of the ASCII string. */
525 if (value || data || is_string(info->Type))
527 /* retry with a dynamically allocated buffer */
528 while (status == STATUS_BUFFER_OVERFLOW)
530 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
531 if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
532 return ERROR_NOT_ENOUGH_MEMORY;
533 info = (KEY_VALUE_FULL_INFORMATION *)buf_ptr;
534 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
535 buf_ptr, total_size, &total_size );
538 if (status) goto done;
540 if (is_string(info->Type))
542 DWORD len;
543 RtlUnicodeToMultiByteSize( &len, (WCHAR *)(buf_ptr + info->DataOffset),
544 total_size - info->DataOffset );
545 if (data && len)
547 if (len > *count) status = STATUS_BUFFER_OVERFLOW;
548 else
550 RtlUnicodeToMultiByteN( data, len, NULL, (WCHAR *)(buf_ptr + info->DataOffset),
551 total_size - info->DataOffset );
552 /* if the type is REG_SZ and data is not 0-terminated
553 * and there is enough space in the buffer NT appends a \0 */
554 if (len < *count && data[len-1]) data[len] = 0;
557 info->DataLength = len;
559 else if (data)
561 if (total_size - info->DataOffset > *count) status = STATUS_BUFFER_OVERFLOW;
562 else memcpy( data, buf_ptr + info->DataOffset, total_size - info->DataOffset );
565 if (value && !status)
567 DWORD len;
569 RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
570 if (len >= *val_count)
572 status = STATUS_BUFFER_OVERFLOW;
573 if (*val_count)
575 len = *val_count - 1;
576 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
577 value[len] = 0;
580 else
582 RtlUnicodeToMultiByteN( value, len, NULL, info->Name, info->NameLength );
583 value[len] = 0;
584 *val_count = len;
588 else status = STATUS_SUCCESS;
590 if (type) *type = info->Type;
591 if (count) *count = info->DataLength;
593 done:
594 if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
595 return RtlNtStatusToDosError(status);
600 /******************************************************************************
601 * RegDeleteValueA [ADVAPI32.@]
603 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
605 UNICODE_STRING nameW;
606 STRING nameA;
607 NTSTATUS status;
609 RtlInitAnsiString( &nameA, name );
610 /* FIXME: should use Unicode buffer in TEB */
611 if (!(status = RtlAnsiStringToUnicodeString( &nameW, &nameA, TRUE )))
613 status = NtDeleteValueKey( hkey, &nameW );
614 RtlFreeUnicodeString( &nameW );
616 return RtlNtStatusToDosError( status );
620 /******************************************************************************
621 * RegLoadKeyA [ADVAPI32.@]
623 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
625 HANDLE file;
626 WCHAR buffer[MAX_PATH];
627 DWORD ret, len, err = GetLastError();
629 TRACE( "(%x,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
631 if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
632 if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
634 if (!(len = MultiByteToWideChar( CP_ACP, 0, subkey, strlen(subkey), buffer, MAX_PATH )))
635 return ERROR_INVALID_PARAMETER;
637 if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
638 FILE_ATTRIBUTE_NORMAL, 0 )) == INVALID_HANDLE_VALUE)
640 ret = GetLastError();
641 goto done;
644 SERVER_START_REQ( load_registry )
646 req->hkey = hkey;
647 req->file = file;
648 wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
649 ret = RtlNtStatusToDosError( wine_server_call(req) );
651 SERVER_END_REQ;
652 CloseHandle( file );
654 done:
655 SetLastError( err ); /* restore the last error code */
656 return ret;
660 /******************************************************************************
661 * RegSaveKeyA [ADVAPI32.@]
663 * PARAMS
664 * hkey [I] Handle of key where save begins
665 * lpFile [I] Address of filename to save to
666 * sa [I] Address of security structure
668 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
670 char buffer[1024];
671 int count = 0;
672 LPSTR name;
673 DWORD ret, err;
674 HANDLE handle;
676 TRACE( "(%x,%s,%p)\n", hkey, debugstr_a(file), sa );
678 if (!file || !*file) return ERROR_INVALID_PARAMETER;
680 err = GetLastError();
681 GetFullPathNameA( file, sizeof(buffer), buffer, &name );
682 for (;;)
684 sprintf( name, "reg%04x.tmp", count++ );
685 handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
686 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
687 if (handle != INVALID_HANDLE_VALUE) break;
688 if ((ret = GetLastError()) != ERROR_ALREADY_EXISTS) goto done;
690 /* Something gone haywire ? Please report if this happens abnormally */
691 if (count >= 100)
692 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);
695 SERVER_START_REQ( save_registry )
697 req->hkey = hkey;
698 req->file = handle;
699 ret = RtlNtStatusToDosError( wine_server_call( req ) );
701 SERVER_END_REQ;
703 CloseHandle( handle );
704 if (!ret)
706 if (!MoveFileExA( buffer, file, MOVEFILE_REPLACE_EXISTING ))
708 ERR( "Failed to move %s to %s\n", buffer, file );
709 ret = GetLastError();
712 if (ret) DeleteFileA( buffer );
714 done:
715 SetLastError( err ); /* restore last error code */
716 return ret;