No longer directly accessing debuggee memory.
[wine] / dlls / advapi32 / registry.c
1 /*
2  * Registry management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
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
10  *
11  * This file is concerned about handle management and interaction with the Wine server.
12  * Registry file I/O is in misc/registry.c.
13  */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18
19 #include "winbase.h"
20 #include "winreg.h"
21 #include "winerror.h"
22 #include "wine/winbase16.h"
23 #include "winversion.h"
24 #include "file.h"
25 #include "heap.h"
26 #include "server.h"
27 #include "debugtools.h"
28
29 DEFAULT_DEBUG_CHANNEL(reg);
30
31
32 /* Ansi->Unicode conversion without string delimiters */
33 static LPWSTR memcpyAtoW( LPWSTR dst, LPCSTR src, INT n )
34 {
35     LPWSTR p = dst;
36     while (n-- > 0) *p++ = (WCHAR)*src++;
37     return dst;
38 }
39
40 /* Unicode->Ansi conversion without string delimiters */
41 static LPSTR memcpyWtoA( LPSTR dst, LPCWSTR src, INT n )
42 {
43     LPSTR p = dst;
44     while (n-- > 0) *p++ = (CHAR)*src++;
45     return dst;
46 }
47
48 /* check if value type needs string conversion (Ansi<->Unicode) */
49 static inline int is_string( DWORD type )
50 {
51     return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
52 }
53
54 /* copy key value data into a user-specified buffer
55  *
56  * 'len' is the total length of the data
57  * 'count' is the size of the user-specified buffer
58  * and is updated to reflect the length copied
59  *
60  * if the type is REG_SZ and data is not 0-terminated and there is enough space in the
61  * buffer nt appends a \0
62  */
63 static DWORD copy_data( void *data, const void *src, DWORD len, DWORD *count, DWORD type )
64 {
65     DWORD ret = ERROR_SUCCESS;
66     if (data)
67     {
68         if (*count < len) ret = ERROR_MORE_DATA;
69         else memcpy( data, src, len );
70     }
71     if (count) 
72     {
73         if (len && data && is_string( type ) && (len < *count) && ((WCHAR *)data)[len-1])
74             ((WCHAR *)data)[len] = 0;
75         *count = len;
76     }
77     return ret;
78 }
79
80 /* same as copy_data but with optional Unicode->Ascii conversion depending on the type */
81 static DWORD copy_data_WtoA( void *data, const void *src, DWORD len, DWORD *count, DWORD type )
82 {
83     DWORD ret = ERROR_SUCCESS;
84     if (is_string( type ))
85     {
86         /* need to convert from Unicode */
87         len /= sizeof(WCHAR);
88         if (data)
89         {
90             if (*count < len) ret = ERROR_MORE_DATA;
91             else if (len)
92             {
93                 memcpyWtoA( data, src, len );
94                 if ((len < *count) && ((char*)data)[len-1]) ((char *)data)[len] = 0;
95             }
96         }
97     }
98     else if (data)
99     {
100         if (*count < len) ret = ERROR_MORE_DATA;
101         else memcpy( data, src, len );
102     }
103     if (count) *count = len;
104     return ret;
105 }
106
107 /* copy a key name into the request buffer */
108 static inline DWORD copy_nameW( LPWSTR dest, LPCWSTR name )
109 {
110     if (name)
111     {
112         if (lstrlenW(name) > MAX_PATH) return ERROR_MORE_DATA;
113         lstrcpyW( dest, name );
114     }
115     else dest[0] = 0;
116     return ERROR_SUCCESS;
117 }
118
119 /* copy a key name into the request buffer */
120 static inline DWORD copy_nameAtoW( LPWSTR dest, LPCSTR name )
121 {
122     if (name)
123     {
124         if (strlen(name) > MAX_PATH) return ERROR_MORE_DATA;
125         lstrcpyAtoW( dest, name );
126     }
127     else dest[0] = 0;
128     return ERROR_SUCCESS;
129 }
130
131 /* do a server call without setting the last error code */
132 static inline int reg_server_call( enum request req )
133 {
134     unsigned int res = server_call_noerr( req );
135     if (res) res = RtlNtStatusToDosError(res);
136     return res;
137 }
138
139 /******************************************************************************
140  *           RegCreateKeyExW   [ADVAPI32.131]
141  *
142  * PARAMS
143  *    hkey       [I] Handle of an open key
144  *    name       [I] Address of subkey name
145  *    reserved   [I] Reserved - must be 0
146  *    class      [I] Address of class string
147  *    options    [I] Special options flag
148  *    access     [I] Desired security access
149  *    sa         [I] Address of key security structure
150  *    retkey     [O] Address of buffer for opened handle
151  *    dispos     [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
152  *
153  * NOTES
154  *  in case of failing retkey remains untouched
155  */
156 DWORD WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
157                               DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa, 
158                               LPHKEY retkey, LPDWORD dispos )
159 {
160     DWORD ret;
161     struct create_key_request *req = get_req_buffer();
162
163     TRACE( "(0x%x,%s,%ld,%s,%lx,%lx,%p,%p,%p)\n", hkey, debugstr_w(name), reserved,
164            debugstr_w(class), options, access, sa, retkey, dispos );
165
166     if (reserved) return ERROR_INVALID_PARAMETER;
167     if (!(access & KEY_ALL_ACCESS) || (access & ~KEY_ALL_ACCESS)) return ERROR_ACCESS_DENIED;
168
169     req->parent  = hkey;
170     req->access  = access;
171     req->options = options;
172     req->modif   = time(NULL);
173     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
174     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
175     lstrcpynW( req->class, class ? class : (LPWSTR)"\0\0",
176                server_remaining(req->class) / sizeof(WCHAR) );
177     if ((ret = reg_server_call( REQ_CREATE_KEY )) == ERROR_SUCCESS)
178     {
179         *retkey = req->hkey;
180         if (dispos) *dispos = req->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
181     }
182     return ret;
183 }
184
185
186 /******************************************************************************
187  *           RegCreateKeyExA   [ADVAPI32.130]
188  */
189 DWORD WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
190                               DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa, 
191                               LPHKEY retkey, LPDWORD dispos )
192 {
193     DWORD ret;
194     struct create_key_request *req = get_req_buffer();
195
196     TRACE( "(0x%x,%s,%ld,%s,%lx,%lx,%p,%p,%p)\n", hkey, debugstr_a(name), reserved,
197            debugstr_a(class), options, access, sa, retkey, dispos );
198
199     if (reserved) return ERROR_INVALID_PARAMETER;
200     if (!(access & KEY_ALL_ACCESS) || (access & ~KEY_ALL_ACCESS)) return ERROR_ACCESS_DENIED;
201
202     req->parent  = hkey;
203     req->access  = access;
204     req->options = options;
205     req->modif   = time(NULL);
206     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
207     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
208     lstrcpynAtoW( req->class, class ? class : "",
209                   server_remaining(req->class) / sizeof(WCHAR) );
210     if ((ret = reg_server_call( REQ_CREATE_KEY )) == ERROR_SUCCESS)
211     {
212         *retkey = req->hkey;
213         if (dispos) *dispos = req->created ? REG_CREATED_NEW_KEY : REG_OPENED_EXISTING_KEY;
214     }
215     return ret;
216 }
217
218
219 /******************************************************************************
220  *           RegCreateKeyW   [ADVAPI32.132]
221  */
222 DWORD WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
223 {
224     /* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
225     /* but at least my version of NT (4.0 SP5) doesn't do this.  -- AJ */
226     return RegCreateKeyExW( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
227                             KEY_ALL_ACCESS, NULL, retkey, NULL );
228 }
229
230
231 /******************************************************************************
232  *           RegCreateKeyA   [ADVAPI32.129]
233  */
234 DWORD WINAPI RegCreateKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
235 {
236     return RegCreateKeyExA( hkey, name, 0, NULL, REG_OPTION_NON_VOLATILE,
237                             KEY_ALL_ACCESS, NULL, retkey, NULL );
238 }
239
240
241
242 /******************************************************************************
243  *           RegOpenKeyExW   [ADVAPI32.150]
244  *
245  * Opens the specified key
246  *
247  * Unlike RegCreateKeyEx, this does not create the key if it does not exist.
248  *
249  * PARAMS
250  *    hkey       [I] Handle of open key
251  *    name       [I] Name of subkey to open
252  *    reserved   [I] Reserved - must be zero
253  *    access     [I] Security access mask
254  *    retkey     [O] Handle to open key
255  *
256  * RETURNS
257  *    Success: ERROR_SUCCESS
258  *    Failure: Error code
259  *
260  * NOTES
261  *  in case of failing is retkey = 0
262  */
263 DWORD WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
264 {
265     DWORD ret;
266     struct open_key_request *req = get_req_buffer();
267
268     TRACE( "(0x%x,%s,%ld,%lx,%p)\n", hkey, debugstr_w(name), reserved, access, retkey );
269
270     if (!retkey) return ERROR_INVALID_PARAMETER;
271     *retkey = 0;
272
273     req->parent = hkey;
274     req->access = access;
275     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
276     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
277     if ((ret = reg_server_call( REQ_OPEN_KEY )) == ERROR_SUCCESS) *retkey = req->hkey;
278     return ret;
279 }
280
281
282 /******************************************************************************
283  *           RegOpenKeyExA   [ADVAPI32.149]
284  */
285 DWORD WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, REGSAM access, LPHKEY retkey )
286 {
287     DWORD ret;
288     struct open_key_request *req = get_req_buffer();
289
290     TRACE( "(0x%x,%s,%ld,%lx,%p)\n", hkey, debugstr_a(name), reserved, access, retkey );
291
292     if (!retkey) return ERROR_INVALID_PARAMETER;
293     *retkey = 0;
294
295     req->parent = hkey;
296     req->access = access;
297     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
298     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
299     if ((ret = reg_server_call( REQ_OPEN_KEY )) == ERROR_SUCCESS) *retkey = req->hkey;
300     return ret;
301 }
302
303
304 /******************************************************************************
305  *           RegOpenKeyW   [ADVAPI32.151]
306  *
307  * PARAMS
308  *    hkey    [I] Handle of open key
309  *    name    [I] Address of name of subkey to open
310  *    retkey  [O] Handle to open key
311  *
312  * RETURNS
313  *    Success: ERROR_SUCCESS
314  *    Failure: Error code
315  *
316  * NOTES
317  *  in case of failing is retkey = 0
318  */
319 DWORD WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, LPHKEY retkey )
320 {
321     return RegOpenKeyExW( hkey, name, 0, KEY_ALL_ACCESS, retkey );
322 }
323
324
325 /******************************************************************************
326  *           RegOpenKeyA   [ADVAPI32.148]
327  */
328 DWORD WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, LPHKEY retkey )
329 {
330     return RegOpenKeyExA( hkey, name, 0, KEY_ALL_ACCESS, retkey );
331 }
332
333
334
335 /******************************************************************************
336  *           RegEnumKeyExW   [ADVAPI32.139]
337  *
338  * PARAMS
339  *    hkey         [I] Handle to key to enumerate
340  *    index        [I] Index of subkey to enumerate
341  *    name         [O] Buffer for subkey name
342  *    name_len     [O] Size of subkey buffer
343  *    reserved     [I] Reserved
344  *    class        [O] Buffer for class string
345  *    class_len    [O] Size of class buffer
346  *    ft           [O] Time key last written to
347  */
348 DWORD WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
349                             LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
350 {
351     DWORD ret, len, cls_len;
352     struct enum_key_request *req = get_req_buffer();
353
354     TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
355            name_len ? *name_len : -1, reserved, class, class_len, ft );
356
357     if (reserved) return ERROR_INVALID_PARAMETER;
358
359     req->hkey = hkey;
360     req->index = index;
361     if ((ret = reg_server_call( REQ_ENUM_KEY )) != ERROR_SUCCESS) return ret;
362
363     len = lstrlenW( req->name ) + 1;
364     cls_len = lstrlenW( req->class ) + 1;
365     if (len > *name_len) return ERROR_MORE_DATA;
366     if (class_len && (cls_len > *class_len)) return ERROR_MORE_DATA;
367
368     memcpy( name, req->name, len * sizeof(WCHAR) );
369     *name_len = len - 1;
370     if (class_len)
371     {
372         if (class) memcpy( class, req->class, cls_len * sizeof(WCHAR) );
373         *class_len = cls_len - 1;
374     }
375     if (ft) DOSFS_UnixTimeToFileTime( req->modif, ft, 0 );
376     return ERROR_SUCCESS;
377 }
378
379
380 /******************************************************************************
381  *           RegEnumKeyExA   [ADVAPI32.138]
382  */
383 DWORD WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
384                             LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
385 {
386     DWORD ret, len, cls_len;
387     struct enum_key_request *req = get_req_buffer();
388
389     TRACE( "(0x%x,%ld,%p,%p(%ld),%p,%p,%p,%p)\n", hkey, index, name, name_len,
390            name_len ? *name_len : -1, reserved, class, class_len, ft );
391
392     if (reserved) return ERROR_INVALID_PARAMETER;
393
394     req->hkey = hkey;
395     req->index = index;
396     if ((ret = reg_server_call( REQ_ENUM_KEY )) != ERROR_SUCCESS) return ret;
397
398     len = lstrlenW( req->name ) + 1;
399     cls_len = lstrlenW( req->class ) + 1;
400     if (len > *name_len) return ERROR_MORE_DATA;
401     if (class_len && (cls_len > *class_len)) return ERROR_MORE_DATA;
402
403     memcpyWtoA( name, req->name, len );
404     *name_len = len - 1;
405     if (class_len)
406     {
407         if (class) memcpyWtoA( class, req->class, cls_len );
408         *class_len = cls_len - 1;
409     }
410     if (ft) DOSFS_UnixTimeToFileTime( req->modif, ft, 0 );
411     return ERROR_SUCCESS;
412 }
413
414
415 /******************************************************************************
416  *           RegEnumKeyW   [ADVAPI32.140]
417  */
418 DWORD WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
419 {
420     return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
421 }
422
423
424 /******************************************************************************
425  *           RegEnumKeyA   [ADVAPI32.137]
426  */
427 DWORD WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
428 {
429     return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
430 }
431
432
433 /******************************************************************************
434  *           RegQueryInfoKeyW   [ADVAPI32.153]
435  *
436  * PARAMS
437  *    hkey       [I] Handle to key to query
438  *    class      [O] Buffer for class string
439  *    class_len  [O] Size of class string buffer
440  *    reserved   [I] Reserved
441  *    subkeys    [O] Buffer for number of subkeys
442  *    max_subkey [O] Buffer for longest subkey name length
443  *    max_class  [O] Buffer for longest class string length
444  *    values     [O] Buffer for number of value entries
445  *    max_value  [O] Buffer for longest value name length
446  *    max_data   [O] Buffer for longest value data length
447  *    security   [O] Buffer for security descriptor length
448  *    modif      [O] Modification time
449  *
450  * - win95 allows class to be valid and class_len to be NULL 
451  * - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
452  * - both allow class to be NULL and class_len to be NULL 
453  * (it's hard to test validity, so test !NULL instead)
454  */
455 DWORD WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
456                                LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
457                                LPDWORD values, LPDWORD max_value, LPDWORD max_data,
458                                LPDWORD security, FILETIME *modif )
459 {
460     DWORD ret;
461     struct query_key_info_request *req = get_req_buffer();
462
463
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 );
466
467     if (class && !class_len && (VERSION_GetVersion() == NT40))
468         return ERROR_INVALID_PARAMETER;
469
470     req->hkey = hkey;
471     if ((ret = reg_server_call( REQ_QUERY_KEY_INFO )) != ERROR_SUCCESS) return ret;
472
473     if (class)
474     {
475         if (class_len && (lstrlenW(req->class) + 1 > *class_len))
476         {
477             *class_len = lstrlenW(req->class);
478             return ERROR_MORE_DATA;
479         }
480         lstrcpyW( class, req->class );
481     }
482     if (class_len) *class_len = lstrlenW( req->class );
483     if (subkeys) *subkeys = req->subkeys;
484     if (max_subkey) *max_subkey = req->max_subkey;
485     if (max_class) *max_class = req->max_class;
486     if (values) *values = req->values;
487     if (max_value) *max_value = req->max_value;
488     if (max_data) *max_data = req->max_data;
489     if (modif) DOSFS_UnixTimeToFileTime( req->modif, modif, 0 );
490     return ERROR_SUCCESS;
491 }
492
493
494 /******************************************************************************
495  *           RegQueryInfoKeyA   [ADVAPI32.152]
496  */
497 DWORD WINAPI RegQueryInfoKeyA( HKEY hkey, LPSTR class, LPDWORD class_len, LPDWORD reserved,
498                                LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
499                                LPDWORD values, LPDWORD max_value, LPDWORD max_data,
500                                LPDWORD security, FILETIME *modif )
501 {
502     DWORD ret;
503     struct query_key_info_request *req = get_req_buffer();
504
505
506     TRACE( "(0x%x,%p,%ld,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
507            reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
508
509     if (class && !class_len && (VERSION_GetVersion() == NT40))
510         return ERROR_INVALID_PARAMETER;
511
512     req->hkey = hkey;
513     if ((ret = reg_server_call( REQ_QUERY_KEY_INFO )) != ERROR_SUCCESS) return ret;
514
515     if (class)
516     {
517         if (class_len && (lstrlenW(req->class) + 1 > *class_len))
518         {
519             *class_len = lstrlenW(req->class);
520             return ERROR_MORE_DATA;
521         }
522         lstrcpyWtoA( class, req->class );
523     }
524     if (class_len) *class_len = lstrlenW( req->class );
525     if (subkeys) *subkeys = req->subkeys;
526     if (max_subkey) *max_subkey = req->max_subkey;
527     if (max_class) *max_class = req->max_class;
528     if (values) *values = req->values;
529     if (max_value) *max_value = req->max_value;
530     if (max_data) *max_data = req->max_data;
531     if (modif) DOSFS_UnixTimeToFileTime( req->modif, modif, 0 );
532     return ERROR_SUCCESS;
533 }
534
535
536 /******************************************************************************
537  *           RegCloseKey   [ADVAPI32.126]
538  *
539  * Releases the handle of the specified key
540  *
541  * PARAMS
542  *    hkey [I] Handle of key to close
543  *
544  * RETURNS
545  *    Success: ERROR_SUCCESS
546  *    Failure: Error code
547  */
548 DWORD WINAPI RegCloseKey( HKEY hkey )
549 {
550     struct close_key_request *req = get_req_buffer();
551     TRACE( "(0x%x)\n", hkey );
552     req->hkey = hkey;
553     return reg_server_call( REQ_CLOSE_KEY );
554 }
555
556
557 /******************************************************************************
558  *           RegDeleteKeyW   [ADVAPI32.134]
559  *
560  * PARAMS
561  *    hkey   [I] Handle to open key
562  *    name   [I] Name of subkey to delete
563  *
564  * RETURNS
565  *    Success: ERROR_SUCCESS
566  *    Failure: Error code
567  */
568 DWORD WINAPI RegDeleteKeyW( HKEY hkey, LPCWSTR name )
569 {
570     DWORD ret;
571     struct delete_key_request *req = get_req_buffer();
572
573     TRACE( "(0x%x,%s)\n", hkey, debugstr_w(name) );
574
575     req->hkey = hkey;
576     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
577     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
578     return reg_server_call( REQ_DELETE_KEY );
579 }
580
581
582 /******************************************************************************
583  *           RegDeleteKeyA   [ADVAPI32.133]
584  */
585 DWORD WINAPI RegDeleteKeyA( HKEY hkey, LPCSTR name )
586 {
587     DWORD ret;
588     struct delete_key_request *req = get_req_buffer();
589
590     TRACE( "(0x%x,%s)\n", hkey, debugstr_a(name) );
591
592     req->hkey = hkey;
593     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
594     if (req->name[0] == '\\') return ERROR_BAD_PATHNAME;
595     return reg_server_call( REQ_DELETE_KEY );
596 }
597
598
599
600 /******************************************************************************
601  *           RegSetValueExW   [ADVAPI32.170]
602  *
603  * Sets the data and type of a value under a register key
604  *
605  * PARAMS
606  *    hkey       [I] Handle of key to set value for
607  *    name       [I] Name of value to set
608  *    reserved   [I] Reserved - must be zero
609  *    type       [I] Flag for value type
610  *    data       [I] Address of value data
611  *    count      [I] Size of value data
612  *
613  * RETURNS
614  *    Success: ERROR_SUCCESS
615  *    Failure: Error code
616  *
617  * NOTES
618  *   win95 does not care about count for REG_SZ and finds out the len by itself (js) 
619  *   NT does definitely care (aj)
620  */
621 DWORD WINAPI RegSetValueExW( HKEY hkey, LPCWSTR name, DWORD reserved,
622                              DWORD type, CONST BYTE *data, DWORD count )
623 {
624     DWORD ret;
625     struct set_key_value_request *req = get_req_buffer();
626
627     TRACE( "(0x%x,%s,%ld,%ld,%p,%ld)\n", hkey, debugstr_w(name), reserved, type, data, count );
628
629     if (reserved) return ERROR_INVALID_PARAMETER;
630
631     if (count && type == REG_SZ)
632     {
633         LPCWSTR str = (LPCWSTR)data;
634         /* if user forgot to count terminating null, add it (yes NT does this) */
635         if (str[count / sizeof(WCHAR) - 1] && !str[count / sizeof(WCHAR)])
636             count += sizeof(WCHAR);
637     }
638     if (count >= server_remaining( req->data )) return ERROR_OUTOFMEMORY;  /* FIXME */
639     req->hkey = hkey;
640     req->type = type;
641     req->len = count;
642     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
643     memcpy( req->data, data, count );
644     return reg_server_call( REQ_SET_KEY_VALUE );
645 }
646
647
648 /******************************************************************************
649  *           RegSetValueExA   [ADVAPI32.169]
650  */
651 DWORD WINAPI RegSetValueExA( HKEY hkey, LPCSTR name, DWORD reserved, DWORD type,
652                              CONST BYTE *data, DWORD count )
653 {
654     DWORD ret;
655     struct set_key_value_request *req = get_req_buffer();
656
657     TRACE( "(0x%x,%s,%ld,%ld,%p,%ld)\n", hkey, debugstr_a(name), reserved, type, data, count );
658
659     if (reserved) return ERROR_INVALID_PARAMETER;
660
661     if (count && type == REG_SZ)
662     {
663         /* if user forgot to count terminating null, add it (yes NT does this) */
664         if (data[count-1] && !data[count]) count++;
665     }
666     if (is_string( type ))
667     {
668         /* need to convert to Unicode */
669         count *= sizeof(WCHAR);
670         if (count >= server_remaining( req->data )) return ERROR_OUTOFMEMORY;  /* FIXME */
671         memcpyAtoW( (LPWSTR)req->data, data, count / sizeof(WCHAR) );
672     }
673     else
674     {
675         if (count >= server_remaining( req->data )) return ERROR_OUTOFMEMORY;  /* FIXME */
676         memcpy( req->data, data, count );
677     }
678     req->hkey = hkey;
679     req->type = type;
680     req->len = count;
681     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
682     return reg_server_call( REQ_SET_KEY_VALUE );
683 }
684
685
686 /******************************************************************************
687  *           RegSetValueW   [ADVAPI32.171]
688  */
689 DWORD WINAPI RegSetValueW( HKEY hkey, LPCWSTR name, DWORD type, LPCWSTR data, DWORD count )
690 {
691     HKEY subkey = hkey;
692     DWORD ret;
693
694     TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_w(name), type, debugstr_w(data), count );
695
696     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
697
698     if (name && name[0])  /* need to create the subkey */
699     {
700         if ((ret = RegCreateKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
701     }
702
703     ret = RegSetValueExW( subkey, NULL, 0, REG_SZ, (LPBYTE)data,
704                           (lstrlenW( data ) + 1) * sizeof(WCHAR) );
705     if (subkey != hkey) RegCloseKey( subkey );
706     return ret;
707 }
708
709
710 /******************************************************************************
711  *           RegSetValueA   [ADVAPI32.168]
712  */
713 DWORD WINAPI RegSetValueA( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
714 {
715     HKEY subkey = hkey;
716     DWORD ret;
717
718     TRACE("(0x%x,%s,%ld,%s,%ld)\n", hkey, debugstr_a(name), type, debugstr_a(data), count );
719
720     if (type != REG_SZ) return ERROR_INVALID_PARAMETER;
721
722     if (name && name[0])  /* need to create the subkey */
723     {
724         if ((ret = RegCreateKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
725     }
726     ret = RegSetValueExA( subkey, NULL, 0, REG_SZ, (LPBYTE)data, strlen(data)+1 );
727     if (subkey != hkey) RegCloseKey( subkey );
728     return ret;
729 }
730
731
732
733 /******************************************************************************
734  *           RegQueryValueExW   [ADVAPI32.158]
735  *
736  * Retrieves type and data for a specified name associated with an open key
737  *
738  * PARAMS
739  *    hkey      [I]   Handle of key to query
740  *    name      [I]   Name of value to query
741  *    reserved  [I]   Reserved - must be NULL
742  *    type      [O]   Address of buffer for value type.  If NULL, the type
743  *                        is not required.
744  *    data      [O]   Address of data buffer.  If NULL, the actual data is
745  *                        not required.
746  *    count     [I/O] Address of data buffer size
747  *
748  * RETURNS 
749  *    ERROR_SUCCESS:   Success
750  *    ERROR_MORE_DATA: !!! if the specified buffer is not big enough to hold the data
751  *                     buffer is left untouched. The MS-documentation is wrong (js) !!!
752  */
753 DWORD WINAPI RegQueryValueExW( HKEY hkey, LPCWSTR name, LPDWORD reserved, LPDWORD type,
754                                LPBYTE data, LPDWORD count )
755 {
756     DWORD ret;
757     struct get_key_value_request *req = get_req_buffer();
758
759     TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
760           hkey, debugstr_w(name), reserved, type, data, count, count ? *count : 0 );
761
762     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
763
764     req->hkey = hkey;
765     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
766     if ((ret = reg_server_call( REQ_GET_KEY_VALUE )) == ERROR_SUCCESS)
767     {
768         if (type) *type = req->type;
769         ret = copy_data( data, req->data, req->len, count, req->type );
770     }
771     return ret;
772 }
773
774
775 /******************************************************************************
776  *           RegQueryValueExA   [ADVAPI32.157]
777  *
778  * NOTES:
779  * the documentation is wrong: if the buffer is to small it remains untouched 
780  */
781 DWORD WINAPI RegQueryValueExA( HKEY hkey, LPCSTR name, LPDWORD reserved, LPDWORD type,
782                                LPBYTE data, LPDWORD count )
783 {
784     DWORD ret;
785     struct get_key_value_request *req = get_req_buffer();
786
787     TRACE("(0x%x,%s,%p,%p,%p,%p=%ld)\n",
788           hkey, debugstr_a(name), reserved, type, data, count, count ? *count : 0 );
789
790     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
791
792     req->hkey = hkey;
793     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
794     if ((ret = reg_server_call( REQ_GET_KEY_VALUE )) == ERROR_SUCCESS)
795     {
796         if (type) *type = req->type;
797         ret = copy_data_WtoA( data, req->data, req->len, count, req->type );
798     }
799     return ret;
800 }
801
802
803 /******************************************************************************
804  *           RegQueryValueW   [ADVAPI32.159]
805  */
806 DWORD WINAPI RegQueryValueW( HKEY hkey, LPCWSTR name, LPWSTR data, LPLONG count )
807 {
808     DWORD ret;
809     HKEY subkey = hkey;
810
811     TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_w(name), data, count ? *count : 0 );
812
813     if (name && name[0])
814     {
815         if ((ret = RegOpenKeyW( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
816     }
817     ret = RegQueryValueExW( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
818     if (subkey != hkey) RegCloseKey( subkey );
819     if (ret == ERROR_FILE_NOT_FOUND)
820     {
821         /* return empty string if default value not found */
822         if (data) *data = 0;
823         if (count) *count = 1;
824         ret = ERROR_SUCCESS;
825     }
826     return ret;
827 }
828
829
830 /******************************************************************************
831  *           RegQueryValueA   [ADVAPI32.156]
832  */
833 DWORD WINAPI RegQueryValueA( HKEY hkey, LPCSTR name, LPSTR data, LPLONG count )
834 {
835     DWORD ret;
836     HKEY subkey = hkey;
837
838     TRACE("(%x,%s,%p,%ld)\n", hkey, debugstr_a(name), data, count ? *count : 0 );
839
840     if (name && name[0])
841     {
842         if ((ret = RegOpenKeyA( hkey, name, &subkey )) != ERROR_SUCCESS) return ret;
843     }
844     ret = RegQueryValueExA( subkey, NULL, NULL, NULL, (LPBYTE)data, count );
845     if (subkey != hkey) RegCloseKey( subkey );
846     if (ret == ERROR_FILE_NOT_FOUND)
847     {
848         /* return empty string if default value not found */
849         if (data) *data = 0;
850         if (count) *count = 1;
851         ret = ERROR_SUCCESS;
852     }
853     return ret;
854 }
855
856
857 /******************************************************************************
858  *           RegEnumValueW   [ADVAPI32.142]
859  *
860  * PARAMS
861  *    hkey       [I] Handle to key to query
862  *    index      [I] Index of value to query
863  *    value      [O] Value string
864  *    val_count  [I/O] Size of value buffer (in wchars)
865  *    reserved   [I] Reserved
866  *    type       [O] Type code
867  *    data       [O] Value data
868  *    count      [I/O] Size of data buffer (in bytes)
869  */
870
871 DWORD WINAPI RegEnumValueW( HKEY hkey, DWORD index, LPWSTR value, LPDWORD val_count,
872                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
873 {
874     DWORD ret, len;
875     struct enum_key_value_request *req = get_req_buffer();
876
877     TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
878           hkey, index, value, val_count, reserved, type, data, count );
879
880     /* NT only checks count, not val_count */
881     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
882
883     req->hkey = hkey;
884     req->index = index;
885     if ((ret = reg_server_call( REQ_ENUM_KEY_VALUE )) != ERROR_SUCCESS) return ret;
886
887     len = lstrlenW( req->name ) + 1;
888     if (len > *val_count) return ERROR_MORE_DATA;
889     memcpy( value, req->name, len * sizeof(WCHAR) );
890     *val_count = len - 1;
891
892     if (type) *type = req->type;
893     return copy_data( data, req->data, req->len, count, req->type );
894 }
895
896
897 /******************************************************************************
898  *           RegEnumValueA   [ADVAPI32.141]
899  */
900 DWORD WINAPI RegEnumValueA( HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count,
901                             LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count )
902 {
903     DWORD ret, len;
904     struct enum_key_value_request *req = get_req_buffer();
905
906     TRACE("(%x,%ld,%p,%p,%p,%p,%p,%p)\n",
907           hkey, index, value, val_count, reserved, type, data, count );
908
909     /* NT only checks count, not val_count */
910     if ((data && !count) || reserved) return ERROR_INVALID_PARAMETER;
911
912     req->hkey = hkey;
913     req->index = index;
914     if ((ret = reg_server_call( REQ_ENUM_KEY_VALUE )) != ERROR_SUCCESS) return ret;
915
916     len = lstrlenW( req->name ) + 1;
917     if (len > *val_count) return ERROR_MORE_DATA;
918     memcpyWtoA( value, req->name, len );
919     *val_count = len - 1;
920
921     if (type) *type = req->type;
922     return copy_data_WtoA( data, req->data, req->len, count, req->type );
923 }
924
925
926
927 /******************************************************************************
928  *           RegDeleteValueW   [ADVAPI32.136]
929  *
930  * PARAMS
931  *    hkey   [I] handle to key
932  *    name   [I] name of value to delete
933  *
934  * RETURNS
935  *    error status
936  */
937 DWORD WINAPI RegDeleteValueW( HKEY hkey, LPCWSTR name )
938 {
939     DWORD ret;
940     struct delete_key_value_request *req = get_req_buffer();
941
942     TRACE( "(0x%x,%s)\n", hkey, debugstr_w(name) );
943
944     req->hkey = hkey;
945     if ((ret = copy_nameW( req->name, name )) != ERROR_SUCCESS) return ret;
946     return reg_server_call( REQ_DELETE_KEY_VALUE );
947 }
948
949
950 /******************************************************************************
951  *           RegDeleteValueA   [ADVAPI32.135]
952  */
953 DWORD WINAPI RegDeleteValueA( HKEY hkey, LPCSTR name )
954 {
955     DWORD ret;
956     struct delete_key_value_request *req = get_req_buffer();
957
958     TRACE( "(0x%x,%s)\n", hkey, debugstr_a(name) );
959
960     req->hkey = hkey;
961     if ((ret = copy_nameAtoW( req->name, name )) != ERROR_SUCCESS) return ret;
962     return reg_server_call( REQ_DELETE_KEY_VALUE );
963 }
964
965
966 /******************************************************************************
967  *           RegLoadKeyW   [ADVAPI32.185]
968  *
969  * PARAMS
970  *    hkey      [I] Handle of open key
971  *    subkey    [I] Address of name of subkey
972  *    filename  [I] Address of filename for registry information
973  */
974 LONG WINAPI RegLoadKeyW( HKEY hkey, LPCWSTR subkey, LPCWSTR filename )
975 {
976     struct load_registry_request *req = get_req_buffer();
977     HANDLE file;
978     DWORD ret, err = GetLastError();
979
980     TRACE( "(%x,%s,%s)\n", hkey, debugstr_w(subkey), debugstr_w(filename) );
981
982     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
983     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
984
985     if ((file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
986                              FILE_ATTRIBUTE_NORMAL, -1 )) == INVALID_HANDLE_VALUE)
987     {
988         ret = GetLastError();
989         goto done;
990     }
991     req->hkey  = hkey;
992     req->file  = file;
993     if ((ret = copy_nameW( req->name, subkey )) != ERROR_SUCCESS) goto done;
994     ret = reg_server_call( REQ_LOAD_REGISTRY );
995     CloseHandle( file );
996
997  done:
998     SetLastError( err );  /* restore the last error code */
999     return ret;
1000 }
1001
1002
1003 /******************************************************************************
1004  *           RegLoadKeyA   [ADVAPI32.184]
1005  */
1006 LONG WINAPI RegLoadKeyA( HKEY hkey, LPCSTR subkey, LPCSTR filename )
1007 {
1008     struct load_registry_request *req = get_req_buffer();
1009     HANDLE file;
1010     DWORD ret, err = GetLastError();
1011
1012     TRACE( "(%x,%s,%s)\n", hkey, debugstr_a(subkey), debugstr_a(filename) );
1013
1014     if (!filename || !*filename) return ERROR_INVALID_PARAMETER;
1015     if (!subkey || !*subkey) return ERROR_INVALID_PARAMETER;
1016
1017     if ((file = CreateFileA( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1018                              FILE_ATTRIBUTE_NORMAL, -1 )) == INVALID_HANDLE_VALUE)
1019     {
1020         ret = GetLastError();
1021         goto done;
1022     }
1023     req->hkey  = hkey;
1024     req->file  = file;
1025     if ((ret = copy_nameAtoW( req->name, subkey )) != ERROR_SUCCESS) goto done;
1026     ret = reg_server_call( REQ_LOAD_REGISTRY );
1027     CloseHandle( file );
1028
1029  done:
1030     SetLastError( err );  /* restore the last error code */
1031     return ret;
1032 }
1033
1034
1035 /******************************************************************************
1036  *           RegSaveKeyA   [ADVAPI32.165]
1037  *
1038  * PARAMS
1039  *    hkey   [I] Handle of key where save begins
1040  *    lpFile [I] Address of filename to save to
1041  *    sa     [I] Address of security structure
1042  */
1043 LONG WINAPI RegSaveKeyA( HKEY hkey, LPCSTR file, LPSECURITY_ATTRIBUTES sa )
1044 {
1045     struct save_registry_request *req = get_req_buffer();
1046     char buffer[1024];
1047     int count = 0;
1048     LPSTR name;
1049     DWORD ret, err;
1050     HFILE handle;
1051
1052     TRACE( "(%x,%s,%p)\n", hkey, debugstr_a(file), sa );
1053
1054     if (!file || !*file) return ERROR_INVALID_PARAMETER;
1055
1056     err = GetLastError();
1057     GetFullPathNameA( file, sizeof(buffer), buffer, &name );
1058     for (;;)
1059     {
1060         sprintf( name, "reg%04x.tmp", count++ );
1061         handle = CreateFileA( buffer, GENERIC_WRITE, 0, NULL,
1062                             CREATE_NEW, FILE_ATTRIBUTE_NORMAL, -1 );
1063         if (handle != INVALID_HANDLE_VALUE) break;
1064         if ((ret = GetLastError()) != ERROR_FILE_EXISTS) goto done;
1065     }
1066
1067     req->hkey = hkey;
1068     req->file = handle;
1069     ret = reg_server_call( REQ_SAVE_REGISTRY );
1070     CloseHandle( handle );
1071     if (!ret)
1072     {
1073         if (!MoveFileExA( buffer, file, MOVEFILE_REPLACE_EXISTING ))
1074         {
1075             ERR( "Failed to move %s to %s\n", buffer, file );
1076             ret = GetLastError();
1077         }
1078     }
1079     if (ret) DeleteFileA( buffer );
1080
1081 done:
1082     SetLastError( err );  /* restore last error code */
1083     return ret;
1084 }
1085
1086
1087 /******************************************************************************
1088  *           RegSaveKeyW   [ADVAPI32.166]
1089  */
1090 LONG WINAPI RegSaveKeyW( HKEY hkey, LPCWSTR file, LPSECURITY_ATTRIBUTES sa )
1091 {
1092     LPSTR fileA = HEAP_strdupWtoA( GetProcessHeap(), 0, file );
1093     DWORD ret = RegSaveKeyA( hkey, fileA, sa );
1094     if (fileA) HeapFree( GetProcessHeap(), 0, fileA );
1095     return ret;
1096 }