msi: Set all folders' source paths to the root directory if the source type is compre...
[wine] / dlls / advapi32 / tests / registry.c
1 /*
2  * Unit tests for registry functions
3  *
4  * Copyright (c) 2002 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "wine/test.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winsvc.h"
29 #include "winerror.h"
30
31 static HKEY hkey_main;
32 static DWORD GLE;
33
34 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
35 static const char * sTestpath2 = "%FOO%\\subdir1";
36
37 static HMODULE hadvapi32;
38 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
39 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
40
41
42
43 /* Debugging functions from wine/libs/wine/debug.c */
44
45 /* allocate some tmp string space */
46 /* FIXME: this is not 100% thread-safe */
47 static char *get_temp_buffer( int size )
48 {
49     static char *list[32];
50     static long pos;
51     char *ret;
52     int idx;
53
54     idx = ++pos % (sizeof(list)/sizeof(list[0]));
55     if ((ret = realloc( list[idx], size ))) list[idx] = ret;
56     return ret;
57 }
58
59 static const char *wine_debugstr_an( const char *str, int n )
60 {
61     static const char hex[16] = "0123456789abcdef";
62     char *dst, *res;
63     size_t size;
64
65     if (!((ULONG_PTR)str >> 16))
66     {
67         if (!str) return "(null)";
68         res = get_temp_buffer( 6 );
69         sprintf( res, "#%04x", LOWORD(str) );
70         return res;
71     }
72     if (n == -1) n = strlen(str);
73     if (n < 0) n = 0;
74     size = 10 + min( 300, n * 4 );
75     dst = res = get_temp_buffer( size );
76     *dst++ = '"';
77     while (n-- > 0 && dst <= res + size - 9)
78     {
79         unsigned char c = *str++;
80         switch (c)
81         {
82         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
83         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
84         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
85         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
86         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
87         default:
88             if (c >= ' ' && c <= 126)
89                 *dst++ = c;
90             else
91             {
92                 *dst++ = '\\';
93                 *dst++ = 'x';
94                 *dst++ = hex[(c >> 4) & 0x0f];
95                 *dst++ = hex[c & 0x0f];
96             }
97         }
98     }
99     *dst++ = '"';
100     if (n > 0)
101     {
102         *dst++ = '.';
103         *dst++ = '.';
104         *dst++ = '.';
105     }
106     *dst++ = 0;
107     return res;
108 }
109
110 static const char *wine_debugstr_wn( const WCHAR *str, int n )
111 {
112     char *dst, *res;
113     size_t size;
114
115     if (!HIWORD(str))
116     {
117         if (!str) return "(null)";
118         res = get_temp_buffer( 6 );
119         sprintf( res, "#%04x", LOWORD(str) );
120         return res;
121     }
122     if (n == -1) n = lstrlenW(str);
123     if (n < 0) n = 0;
124     size = 12 + min( 300, n * 5);
125     dst = res = get_temp_buffer( n * 5 + 7 );
126     *dst++ = 'L';
127     *dst++ = '"';
128     while (n-- > 0 && dst <= res + size - 10)
129     {
130         WCHAR c = *str++;
131         switch (c)
132         {
133         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
134         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
135         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
136         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
137         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
138         default:
139             if (c >= ' ' && c <= 126)
140                 *dst++ = (char)c;
141             else
142             {
143                 *dst++ = '\\';
144                 sprintf(dst,"%04x",c);
145                 dst+=4;
146             }
147         }
148     }
149     *dst++ = '"';
150     if (n > 0)
151     {
152         *dst++ = '.';
153         *dst++ = '.';
154         *dst++ = '.';
155     }
156     *dst = 0;
157     return res;
158 }
159
160
161 #define ADVAPI32_GET_PROC(func) \
162     p ## func = (void*)GetProcAddress(hadvapi32, #func); \
163     if(!p ## func) \
164       trace("GetProcAddress(%s) failed\n", #func);
165
166 static void InitFunctionPtrs(void)
167 {
168     hadvapi32 = GetModuleHandleA("advapi32.dll");
169
170     /* This function was introduced with Windows 2003 SP1 */
171     ADVAPI32_GET_PROC(RegGetValueA)
172     ADVAPI32_GET_PROC(RegDeleteTreeA)
173 }
174
175 /* delete key and all its subkeys */
176 static DWORD delete_key( HKEY hkey )
177 {
178     char name[MAX_PATH];
179     DWORD ret;
180
181     while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
182     {
183         HKEY tmp;
184         if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
185         {
186             ret = delete_key( tmp );
187             RegCloseKey( tmp );
188         }
189         if (ret) break;
190     }
191     if (ret != ERROR_NO_MORE_ITEMS) return ret;
192     RegDeleteKeyA( hkey, "" );
193     return 0;
194 }
195
196 static void setup_main_key(void)
197 {
198     if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
199
200     assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
201 }
202
203 static void test_hkey_main_Value_A(LPCSTR name, LPCSTR string,
204                                    DWORD full_byte_len)
205 {
206     DWORD ret, type, cbData;
207     DWORD str_byte_len;
208     LPSTR value;
209     static const char nA[]={'N', 0};
210
211     type=0xdeadbeef;
212     cbData=0xdeadbeef;
213     /* When successful RegQueryValueExA() leaves GLE as is,
214      * so we must reset it to detect unimplemented functions.
215      */
216     SetLastError(0xdeadbeef);
217     ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
218     GLE = GetLastError();
219     ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
220     /* It is wrong for the Ansi version to not be implemented */
221     ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
222     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
223
224     str_byte_len = (string ? lstrlenA(string) : 0) + 1;
225     ok(type == REG_SZ, "RegQueryValueExA returned type %d\n", type);
226     ok(cbData == full_byte_len || cbData == str_byte_len /* Win9x */,
227         "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
228
229     value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
230     strcpy(value, nA);
231     type=0xdeadbeef;
232     ret = RegQueryValueExA(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
233     GLE = GetLastError();
234     ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
235     if (!string)
236     {
237         /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
238         ok(strcmp(value, nA) == 0 || (cbData == 1 && *value == '\0') /* Win9x */,
239            "RegQueryValueExA failed: '%s' != '%s'\n", value, string);
240     }
241     else
242     {
243         ok(memcmp(value, string, cbData) == 0, "RegQueryValueExA failed: %s/%d != %s/%d\n",
244            wine_debugstr_an(value, cbData), cbData,
245            wine_debugstr_an(string, full_byte_len), full_byte_len);
246     }
247     HeapFree(GetProcessHeap(), 0, value);
248 }
249
250 static void test_hkey_main_Value_W(LPCWSTR name, LPCWSTR string,
251                                    DWORD full_byte_len)
252 {
253     DWORD ret, type, cbData;
254     LPWSTR value;
255     static const WCHAR nW[]={'N', 0};
256
257     type=0xdeadbeef;
258     cbData=0xdeadbeef;
259     /* When successful RegQueryValueExW() leaves GLE as is,
260      * so we must reset it to detect unimplemented functions.
261      */
262     SetLastError(0xdeadbeef);
263     ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
264     GLE = GetLastError();
265     ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
266     if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
267     {
268         win_skip("RegQueryValueExW() is not implemented\n");
269         return;
270     }
271
272     ok(type == REG_SZ, "RegQueryValueExW returned type %d\n", type);
273     ok(cbData == full_byte_len,
274         "cbData=%d instead of %d\n", cbData, full_byte_len);
275
276     value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
277     lstrcpyW(value, nW);
278     type=0xdeadbeef;
279     ret = RegQueryValueExW(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
280     GLE = GetLastError();
281     ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
282     if (!string)
283     {
284         /* When cbData == 0, RegQueryValueExW() should not modify the buffer */
285         string=nW;
286     }
287     ok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
288        wine_debugstr_wn(value, cbData / sizeof(WCHAR)), cbData,
289        wine_debugstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
290     HeapFree(GetProcessHeap(), 0, value);
291 }
292
293 static void test_set_value(void)
294 {
295     DWORD ret;
296
297     static const WCHAR name1W[] =   {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
298     static const WCHAR name2W[] =   {'S','o','m','e','I','n','t','r','a','Z','e','r','o','e','d','S','t','r','i','n','g', 0};
299     static const WCHAR emptyW[] = {0};
300     static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
301     static const WCHAR string2W[] = {'T','h','i','s', 0 ,'B','r','e','a','k','s', 0 , 0 ,'A', 0 , 0 , 0 , 'L','o','t', 0 , 0 , 0 , 0, 0};
302     static const WCHAR substring2W[] = {'T','h','i','s',0};
303
304     static const char name1A[] =   "CleanSingleString";
305     static const char name2A[] =   "SomeIntraZeroedString";
306     static const char emptyA[] = "";
307     static const char string1A[] = "ThisNeverBreaks";
308     static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
309     static const char substring2A[] = "This";
310
311     if (0)
312     {
313         /* Crashes on NT4, Windows 2000 and XP SP1 */
314         ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
315         ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
316     }
317
318     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
319     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
320     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
321     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
322
323     /* RegSetValueA ignores the size passed in */
324     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
325     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
326     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
327     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
328
329     /* stops at first null */
330     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
331     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
332     test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
333     test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
334
335     /* only REG_SZ is supported */
336     ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
337     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
338     ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
339     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
340     ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
341     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
342
343     /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
344      * Surprisingly enough we're supposed to get zero bytes out of it.
345      */
346     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
347     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
348     test_hkey_main_Value_A(name1A, NULL, 0);
349     test_hkey_main_Value_W(name1W, NULL, 0);
350
351     /* test RegSetValueExA with an empty string */
352     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
353     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
354     test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
355     test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
356
357     /* test RegSetValueExA with off-by-one size */
358     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
359     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
360     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
361     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
362
363     /* test RegSetValueExA with normal string */
364     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
365     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
366     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
367     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
368
369     /* test RegSetValueExA with intrazeroed string */
370     ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
371     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
372     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
373     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
374
375     /* 9x doesn't support W-calls, so don't test them then */
376     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return; 
377
378     if (0)
379     {
380         /* Crashes on NT4, Windows 2000 and XP SP1 */
381         ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
382         ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
383     }
384
385     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
386     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
387     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
388     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
389
390     /* RegSetValueA ignores the size passed in */
391     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
392     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
393     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
394     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
395
396     /* stops at first null */
397     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
398     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
399     test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
400     test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
401
402     /* only REG_SZ is supported */
403     ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
404     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
405     ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
406     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
407     ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
408     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
409
410     /* test RegSetValueExW with off-by-one size */
411     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
412     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
413     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
414     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
415
416     /* test RegSetValueExW with normal string */
417     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
418     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
419     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
420     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
421
422     /* test RegSetValueExW with intrazeroed string */
423     ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
424     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
425     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
426     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
427 }
428
429 static void create_test_entries(void)
430 {
431     static const DWORD qw[2] = { 0x12345678, 0x87654321 };
432
433     SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
434     SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
435
436     ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
437         "RegSetValueExA failed\n");
438     ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
439         "RegSetValueExA failed\n");
440     ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
441        "RegSetValueExA failed\n");
442     ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1), 
443         "RegSetValueExA failed\n");
444     ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
445         "RegSetValueExA failed\n");
446     ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
447         "RegSetValueExA failed\n");
448     ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
449         "RegSetValueExA failed\n");
450 }
451         
452 static void test_enum_value(void)
453 {
454     DWORD res;
455     HKEY test_key;
456     char value[20], data[20];
457     WCHAR valueW[20], dataW[20];
458     DWORD val_count, data_count, type;
459     static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
460     static const WCHAR testW[] = {'T','e','s','t',0};
461     static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
462
463     /* create the working key for new 'Test' value */
464     res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
465     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
466
467     /* check NULL data with zero length */
468     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
469     if (GetVersion() & 0x80000000)
470         ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
471     else
472         ok( !res, "RegSetValueExA returned %d\n", res );
473     res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
474     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
475     res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
476     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
477
478     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
479     ok( res == 0, "RegSetValueExA failed error %d\n", res );
480
481     /* overflow both name and data */
482     val_count = 2;
483     data_count = 2;
484     type = 1234;
485     strcpy( value, "xxxxxxxxxx" );
486     strcpy( data, "xxxxxxxxxx" );
487     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
488     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
489     ok( val_count == 2, "val_count set to %d\n", val_count );
490     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
491     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
492     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
493     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
494
495     /* overflow name */
496     val_count = 3;
497     data_count = 20;
498     type = 1234;
499     strcpy( value, "xxxxxxxxxx" );
500     strcpy( data, "xxxxxxxxxx" );
501     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
502     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
503     /* Win9x returns 2 as specified by MSDN but NT returns 3... */
504     ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
505     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
506     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
507     /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
508     ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ), 
509         "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
510     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
511         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
512
513     /* overflow empty name */
514     val_count = 0;
515     data_count = 20;
516     type = 1234;
517     strcpy( value, "xxxxxxxxxx" );
518     strcpy( data, "xxxxxxxxxx" );
519     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
520     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
521     ok( val_count == 0, "val_count set to %d\n", val_count );
522     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
523     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
524     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
525     /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
526     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
527         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
528
529     /* overflow data */
530     val_count = 20;
531     data_count = 2;
532     type = 1234;
533     strcpy( value, "xxxxxxxxxx" );
534     strcpy( data, "xxxxxxxxxx" );
535     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
536     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
537     ok( val_count == 20, "val_count set to %d\n", val_count );
538     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
539     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
540     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
541     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
542
543     /* no overflow */
544     val_count = 20;
545     data_count = 20;
546     type = 1234;
547     strcpy( value, "xxxxxxxxxx" );
548     strcpy( data, "xxxxxxxxxx" );
549     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
550     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
551     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
552     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
553     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
554     ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
555     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
556
557     /* Unicode tests */
558
559     SetLastError(0xdeadbeef);
560     res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
561     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
562     {
563         win_skip("RegSetValueExW is not implemented\n");
564         goto cleanup;
565     }
566     ok( res == 0, "RegSetValueExW failed error %d\n", res );
567
568     /* overflow both name and data */
569     val_count = 2;
570     data_count = 2;
571     type = 1234;
572     memcpy( valueW, xxxW, sizeof(xxxW) );
573     memcpy( dataW, xxxW, sizeof(xxxW) );
574     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
575     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
576     ok( val_count == 2, "val_count set to %d\n", val_count );
577     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
578     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
579     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
580     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
581
582     /* overflow name */
583     val_count = 3;
584     data_count = 20;
585     type = 1234;
586     memcpy( valueW, xxxW, sizeof(xxxW) );
587     memcpy( dataW, xxxW, sizeof(xxxW) );
588     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
589     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
590     ok( val_count == 3, "val_count set to %d\n", val_count );
591     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
592     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
593     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
594     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
595
596     /* overflow data */
597     val_count = 20;
598     data_count = 2;
599     type = 1234;
600     memcpy( valueW, xxxW, sizeof(xxxW) );
601     memcpy( dataW, xxxW, sizeof(xxxW) );
602     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
603     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
604     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
605     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
606     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
607     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
608     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
609
610     /* no overflow */
611     val_count = 20;
612     data_count = 20;
613     type = 1234;
614     memcpy( valueW, xxxW, sizeof(xxxW) );
615     memcpy( dataW, xxxW, sizeof(xxxW) );
616     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
617     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
618     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
619     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
620     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
621     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
622     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
623
624 cleanup:
625     RegDeleteKeyA(test_key, "");
626     RegCloseKey(test_key);
627 }
628
629 static void test_query_value_ex(void)
630 {
631     DWORD ret;
632     DWORD size;
633     DWORD type;
634     BYTE buffer[10];
635     
636     ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
637     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
638     ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
639     ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
640
641     type = 0xdeadbeef;
642     size = 0xdeadbeef;
643     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
644     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
645     /* the type parameter is cleared on Win9x, but is set to a random value on
646      * NT, so don't do that test there. The size parameter is left untouched on Win9x
647      * but cleared on NT+, this can be tested on all platforms.
648      */
649     if (GetVersion() & 0x80000000)
650     {
651         ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
652         ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
653     }
654     else
655     {
656         trace("test_query_value_ex: type set to: 0x%08x\n", type);
657         ok(size == 0, "size should have been set to 0 instead of %d\n", size);
658     }
659
660     size = sizeof(buffer);
661     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
662     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
663     ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
664
665     size = 4;
666     ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
667     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
668 }
669
670 static void test_get_value(void)
671 {
672     DWORD ret;
673     DWORD size;
674     DWORD type;
675     DWORD dw, qw[2];
676     CHAR buf[80];
677     CHAR expanded[] = "bar\\subdir1";
678     CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
679    
680     if(!pRegGetValueA)
681     {
682         win_skip("RegGetValue not available on this platform\n");
683         return;
684     }
685
686     /* Invalid parameter */
687     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
688     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
689
690     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
691     size = type = dw = 0xdeadbeef;
692     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
693     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
694     ok(size == 4, "size=%d\n", size);
695     ok(type == REG_DWORD, "type=%d\n", type);
696     ok(dw == 0x12345678, "dw=%d\n", dw);
697
698     /* Query by subkey-name */
699     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
700     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
701
702     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
703     size = type = dw = 0xdeadbeef;
704     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
705     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
706     /* Although the function failed all values are retrieved */
707     ok(size == 4, "size=%d\n", size);
708     ok(type == REG_DWORD, "type=%d\n", type);
709     ok(dw == 0x12345678, "dw=%d\n", dw);
710
711     /* Test RRF_ZEROONFAILURE */
712     type = dw = 0xdeadbeef; size = 4;
713     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
714     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
715     /* Again all values are retrieved ... */
716     ok(size == 4, "size=%d\n", size);
717     ok(type == REG_DWORD, "type=%d\n", type);
718     /* ... except the buffer, which is zeroed out */
719     ok(dw == 0, "dw=%d\n", dw);
720
721     /* Test RRF_ZEROONFAILURE with a NULL buffer... */
722     type = size = 0xbadbeef;
723     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
724     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
725     ok(size == 4, "size=%d\n", size);
726     ok(type == REG_DWORD, "type=%d\n", type);
727
728     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
729     size = type = dw = 0xdeadbeef;
730     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
731     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
732     ok(size == 4, "size=%d\n", size);
733     ok(type == REG_DWORD, "type=%d\n", type);
734     ok(dw == 0x12345678, "dw=%d\n", dw);
735
736     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
737     size = type = dw = 0xdeadbeef;
738     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
739     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
740     ok(size == 4, "size=%d\n", size);
741     ok(type == REG_BINARY, "type=%d\n", type);
742     ok(dw == 0x12345678, "dw=%d\n", dw);
743     
744     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
745     qw[0] = qw[1] = size = type = 0xdeadbeef;
746     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
747     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
748     ok(size == 8, "size=%d\n", size);
749     ok(type == REG_BINARY, "type=%d\n", type);
750     ok(qw[0] == 0x12345678 && 
751        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
752     
753     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
754     type = dw = 0xdeadbeef; size = 4;
755     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
756     ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
757     ok(dw == 0xdeadbeef, "dw=%d\n", dw);
758     ok(size == 8, "size=%d\n", size);
759
760     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
761     qw[0] = qw[1] = size = type = 0xdeadbeef;
762     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
763     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
764     ok(size == 8, "size=%d\n", size);
765     ok(type == REG_BINARY, "type=%d\n", type);
766     ok(qw[0] == 0x12345678 &&
767        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
768
769     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
770     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
771     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
772     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
773     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
774     ok(type == REG_SZ, "type=%d\n", type);
775     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
776
777     /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
778     type = 0xdeadbeef; size = 0;
779     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
780     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
781     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
782     ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
783        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
784     ok(type == REG_SZ, "type=%d\n", type);
785
786     /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
787     strcpy(buf, sTestpath1);
788     type = 0xdeadbeef;
789     size = sizeof(buf);
790     ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
791     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
792     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
793     ok(size == 0 ||
794        size == 1, /* win2k3 */
795        "size=%d\n", size);
796     ok(type == REG_SZ, "type=%d\n", type);
797     ok(!strcmp(sTestpath1, buf) ||
798        !strcmp(buf, ""),
799        "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
800
801     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
802     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
803     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
804     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
805     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
806     ok(type == REG_SZ, "type=%d\n", type);
807     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
808
809     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
810     size = 0;
811     ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
812     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
813     ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
814        (size == strlen(expanded2)+2) || /* win2k3 SP2 */
815        (size == strlen(sTestpath2)+1),
816         "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
817
818     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
819     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
820     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
821     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
822     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
823     ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
824         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
825     ok(type == REG_SZ, "type=%d\n", type);
826     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
827
828     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
829     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
830     ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
831     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
832     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
833     ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
834         "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
835     ok(type == REG_SZ, "type=%d\n", type);
836     ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
837
838     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
839     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
840     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
841     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
842     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
843     ok(type == REG_EXPAND_SZ, "type=%d\n", type);
844     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
845
846     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
847     size = 0xbadbeef;
848     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
849     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
850     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
851     ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
852        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
853
854     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
855     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
856     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
857
858     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
859     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
860     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
861
862     /* Query REG_EXPAND_SZ using RRF_RT_ANY */
863     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
864     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
865     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
866     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
867     ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
868         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
869     ok(type == REG_SZ, "type=%d\n", type);
870     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
871
872
873 static void test_reg_open_key(void)
874 {
875     DWORD ret = 0;
876     HKEY hkResult = NULL;
877     HKEY hkPreserve = NULL;
878
879     /* successful open */
880     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
881     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
882     ok(hkResult != NULL, "expected hkResult != NULL\n");
883     hkPreserve = hkResult;
884
885     /* these tests fail on Win9x, but we want to be compatible with NT, so
886      * run them if we can */
887     if (!(GetVersion() & 0x80000000))
888     {
889         /* open same key twice */
890         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
891         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
892         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
893         ok(hkResult != NULL, "hkResult != NULL\n");
894         RegCloseKey(hkResult);
895     
896         /* open nonexistent key
897         * check that hkResult is set to NULL
898         */
899         hkResult = hkPreserve;
900         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
901         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
902         ok(hkResult == NULL, "expected hkResult == NULL\n");
903     
904         /* open the same nonexistent key again to make sure the key wasn't created */
905         hkResult = hkPreserve;
906         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
907         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
908         ok(hkResult == NULL, "expected hkResult == NULL\n");
909     
910         /* send in NULL lpSubKey
911         * check that hkResult receives the value of hKey
912         */
913         hkResult = hkPreserve;
914         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
915         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
916         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
917     
918         /* send empty-string in lpSubKey */
919         hkResult = hkPreserve;
920         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
921         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
922         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
923     
924         /* send in NULL lpSubKey and NULL hKey
925         * hkResult is set to NULL
926         */
927         hkResult = hkPreserve;
928         ret = RegOpenKeyA(NULL, NULL, &hkResult);
929         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
930         ok(hkResult == NULL, "expected hkResult == NULL\n");
931     }
932
933     /* only send NULL hKey
934      * the value of hkResult remains unchanged
935      */
936     hkResult = hkPreserve;
937     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
938     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
939        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
940     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
941     RegCloseKey(hkResult);
942
943     /* send in NULL hkResult */
944     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
945     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
946
947     /*  beginning backslash character */
948     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
949        ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
950            ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
951            , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
952 }
953
954 static void test_reg_create_key(void)
955 {
956     LONG ret;
957     HKEY hkey1, hkey2;
958     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
959     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
960     /* should succeed: all versions of Windows ignore the access rights
961      * to the parent handle */
962     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
963     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
964
965     /* clean up */
966     RegDeleteKey(hkey2, "");
967     RegDeleteKey(hkey1, "");
968
969     /*  beginning backslash character */
970     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
971     if (!(GetVersion() & 0x80000000))
972         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
973     else {
974         ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
975         RegDeleteKey(hkey1, NULL);
976     }
977 }
978
979 static void test_reg_close_key(void)
980 {
981     DWORD ret = 0;
982     HKEY hkHandle;
983
984     /* successfully close key
985      * hkHandle remains changed after call to RegCloseKey
986      */
987     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
988     ret = RegCloseKey(hkHandle);
989     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
990
991     /* try to close the key twice */
992     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
993     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
994        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
995     
996     /* try to close a NULL handle */
997     ret = RegCloseKey(NULL);
998     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
999        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1000
1001     /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1002      * win98 doesn't give a new handle when the same key is opened.
1003      * Not re-opening will make some next tests fail.
1004      */
1005     if (hkey_main == hkHandle)
1006     {
1007         trace("The main handle is most likely closed, so re-opening\n");
1008         RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1009     }
1010 }
1011
1012 static void test_reg_delete_key(void)
1013 {
1014     DWORD ret;
1015
1016     ret = RegDeleteKey(hkey_main, NULL);
1017
1018     /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1019      * there are also no subkeys available it will delete the key pointed to by hkey_main.
1020      * Not re-creating will make some next tests fail.
1021      */
1022     if (ret == ERROR_SUCCESS)
1023     {
1024         trace("We are probably running on NT4 or W2K as the main key is deleted,"
1025             " re-creating the main key\n");
1026         setup_main_key();
1027     }
1028     else
1029         ok(ret == ERROR_INVALID_PARAMETER ||
1030            ret == ERROR_ACCESS_DENIED ||
1031            ret == ERROR_BADKEY, /* Win95 */
1032            "ret=%d\n", ret);
1033 }
1034
1035 static void test_reg_save_key(void)
1036 {
1037     DWORD ret;
1038
1039     ret = RegSaveKey(hkey_main, "saved_key", NULL);
1040     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1041 }
1042
1043 static void test_reg_load_key(void)
1044 {
1045     DWORD ret;
1046     HKEY hkHandle;
1047
1048     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1049     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1050
1051     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1052     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1053
1054     RegCloseKey(hkHandle);
1055 }
1056
1057 static void test_reg_unload_key(void)
1058 {
1059     DWORD ret;
1060
1061     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
1062     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1063
1064     DeleteFile("saved_key");
1065     DeleteFile("saved_key.LOG");
1066 }
1067
1068 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1069 {
1070     TOKEN_PRIVILEGES tp;
1071     HANDLE hToken;
1072     LUID luid;
1073
1074     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1075         return FALSE;
1076
1077     if(!LookupPrivilegeValue(NULL, privilege, &luid))
1078     {
1079         CloseHandle(hToken);
1080         return FALSE;
1081     }
1082
1083     tp.PrivilegeCount = 1;
1084     tp.Privileges[0].Luid = luid;
1085     
1086     if (set)
1087         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1088     else
1089         tp.Privileges[0].Attributes = 0;
1090
1091     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1092     if (GetLastError() != ERROR_SUCCESS)
1093     {
1094         CloseHandle(hToken);
1095         return FALSE;
1096     }
1097
1098     CloseHandle(hToken);
1099     return TRUE;
1100 }
1101
1102 /* tests that show that RegConnectRegistry and 
1103    OpenSCManager accept computer names without the
1104    \\ prefix (what MSDN says).   */
1105 static void test_regconnectregistry( void)
1106 {
1107     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1108     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1109     DWORD len = sizeof(compName) ;
1110     BOOL ret;
1111     LONG retl;
1112     HKEY hkey;
1113     SC_HANDLE schnd;
1114
1115     SetLastError(0xdeadbeef);
1116     ret = GetComputerNameA(compName, &len);
1117     ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1118     if( !ret) return;
1119
1120     lstrcpyA(netwName, "\\\\");
1121     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1122
1123     retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1124     ok( !retl ||
1125         retl == ERROR_DLL_INIT_FAILED ||
1126         retl == ERROR_BAD_NETPATH, /* some win2k */
1127         "RegConnectRegistryA failed err = %d\n", retl);
1128     if( !retl) RegCloseKey( hkey);
1129
1130     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1131     ok( !retl ||
1132         retl == ERROR_DLL_INIT_FAILED ||
1133         retl == ERROR_BAD_NETPATH, /* some win2k */
1134         "RegConnectRegistryA failed err = %d\n", retl);
1135     if( !retl) RegCloseKey( hkey);
1136
1137     SetLastError(0xdeadbeef);
1138     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
1139     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1140     {
1141         win_skip("OpenSCManagerA is not implemented\n");
1142         return;
1143     }
1144
1145     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1146     CloseServiceHandle( schnd);
1147
1148     SetLastError(0xdeadbeef);
1149     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
1150     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1151     CloseServiceHandle( schnd);
1152
1153 }
1154
1155 static void test_reg_query_value(void)
1156 {
1157     HKEY subkey;
1158     CHAR val[MAX_PATH];
1159     WCHAR valW[5];
1160     LONG size, ret;
1161
1162     static const WCHAR expected[] = {'d','a','t','a',0};
1163
1164     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1165     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1166
1167     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1168     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1169
1170     /* try an invalid hkey */
1171     SetLastError(0xdeadbeef);
1172     size = MAX_PATH;
1173     ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1174     ok(ret == ERROR_INVALID_HANDLE ||
1175        ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1176        ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1177        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1178     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1179
1180     /* try a NULL hkey */
1181     SetLastError(0xdeadbeef);
1182     size = MAX_PATH;
1183     ret = RegQueryValueA(NULL, "subkey", val, &size);
1184     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1185        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1186     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1187
1188     /* try a NULL value */
1189     size = MAX_PATH;
1190     ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1191     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1192     ok(size == 5, "Expected 5, got %d\n", size);
1193
1194     /* try a NULL size */
1195     SetLastError(0xdeadbeef);
1196     val[0] = '\0';
1197     ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1198     ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1199     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1200     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1201
1202     /* try a NULL value and size */
1203     ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1204     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1205
1206     /* try a size too small */
1207     SetLastError(0xdeadbeef);
1208     val[0] = '\0';
1209     size = 1;
1210     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1211     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1212     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1213     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1214     ok(size == 5, "Expected 5, got %d\n", size);
1215
1216     /* successfully read the value using 'subkey' */
1217     size = MAX_PATH;
1218     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1219     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1220     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1221     ok(size == 5, "Expected 5, got %d\n", size);
1222
1223     /* successfully read the value using the subkey key */
1224     size = MAX_PATH;
1225     ret = RegQueryValueA(subkey, NULL, val, &size);
1226     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1227     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1228     ok(size == 5, "Expected 5, got %d\n", size);
1229
1230     /* unicode - try size too small */
1231     SetLastError(0xdeadbeef);
1232     valW[0] = '\0';
1233     size = 0;
1234     ret = RegQueryValueW(subkey, NULL, valW, &size);
1235     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1236     {
1237         win_skip("RegQueryValueW is not implemented\n");
1238         goto cleanup;
1239     }
1240     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1241     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1242     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1243     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1244
1245     /* unicode - try size in WCHARS */
1246     SetLastError(0xdeadbeef);
1247     size = sizeof(valW) / sizeof(WCHAR);
1248     ret = RegQueryValueW(subkey, NULL, valW, &size);
1249     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1250     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1251     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1252     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1253
1254     /* unicode - successfully read the value */
1255     size = sizeof(valW);
1256     ret = RegQueryValueW(subkey, NULL, valW, &size);
1257     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1258     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1259     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1260
1261     /* unicode - set the value without a NULL terminator */
1262     ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1263     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1264
1265     /* unicode - read the unterminated value, value is terminated for us */
1266     memset(valW, 'a', sizeof(valW));
1267     size = sizeof(valW);
1268     ret = RegQueryValueW(subkey, NULL, valW, &size);
1269     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1270     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1271     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1272
1273 cleanup:
1274     RegDeleteKeyA(subkey, "");
1275     RegCloseKey(subkey);
1276 }
1277
1278 static void test_reg_delete_tree(void)
1279 {
1280     CHAR buffer[MAX_PATH];
1281     HKEY subkey, subkey2;
1282     LONG size, ret;
1283
1284     if(!pRegDeleteTreeA) {
1285         win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1286         return;
1287     }
1288
1289     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1290     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1291     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1292     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1293     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1294     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1295     ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1296     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1297     ret = RegCloseKey(subkey2);
1298     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1299
1300     ret = pRegDeleteTreeA(subkey, "subkey2");
1301     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1302     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1303         "subkey2 was not deleted\n");
1304     size = MAX_PATH;
1305     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1306         "Default value of subkey not longer present\n");
1307
1308     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1309     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1310     ret = RegCloseKey(subkey2);
1311     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1312     ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1313     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1314     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1315         "subkey2 was not deleted\n");
1316     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1317         "Default value of subkey not longer present\n");
1318
1319     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1320     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1321     ret = RegCloseKey(subkey2);
1322     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1323     ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1324     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1325     ret = RegCloseKey(subkey2);
1326     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1327     ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1328     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1329     ret = pRegDeleteTreeA(subkey, NULL);
1330     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1331     ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1332         "subkey was deleted\n");
1333     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1334         "subkey2 was not deleted\n");
1335     ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1336         "subkey3 was not deleted\n");
1337     size = MAX_PATH;
1338     ret = RegQueryValueA(subkey, NULL, buffer, &size);
1339     ok(ret == ERROR_SUCCESS,
1340         "Default value of subkey is not present\n");
1341     ok(!lstrlenA(buffer),
1342         "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1343     size = MAX_PATH;
1344     ok(RegQueryValueA(subkey, "value", buffer, &size),
1345         "Value is still present\n");
1346
1347     ret = pRegDeleteTreeA(hkey_main, "not-here");
1348     ok(ret == ERROR_FILE_NOT_FOUND,
1349         "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1350 }
1351
1352 START_TEST(registry)
1353 {
1354     /* Load pointers for functions that are not available in all Windows versions */
1355     InitFunctionPtrs();
1356
1357     setup_main_key();
1358     test_set_value();
1359     create_test_entries();
1360     test_enum_value();
1361     test_query_value_ex();
1362     test_get_value();
1363     test_reg_open_key();
1364     test_reg_create_key();
1365     test_reg_close_key();
1366     test_reg_delete_key();
1367     test_reg_query_value();
1368
1369     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1370     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1371         set_privileges(SE_RESTORE_NAME, TRUE))
1372     {
1373         test_reg_save_key();
1374         test_reg_load_key();
1375         test_reg_unload_key();
1376
1377         set_privileges(SE_BACKUP_NAME, FALSE);
1378         set_privileges(SE_RESTORE_NAME, FALSE);
1379     }
1380
1381     test_reg_delete_tree();
1382
1383     /* cleanup */
1384     delete_key( hkey_main );
1385     
1386     test_regconnectregistry();
1387 }