winex11: add owned windows to taskbar if owner is not mapped
[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) return;
267
268     ok(type == REG_SZ, "RegQueryValueExW returned type %d\n", type);
269     ok(cbData == full_byte_len,
270         "cbData=%d instead of %d\n", cbData, full_byte_len);
271
272     value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
273     lstrcpyW(value, nW);
274     type=0xdeadbeef;
275     ret = RegQueryValueExW(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
276     GLE = GetLastError();
277     ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
278     if (!string)
279     {
280         /* When cbData == 0, RegQueryValueExW() should not modify the buffer */
281         string=nW;
282     }
283     ok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
284        wine_debugstr_wn(value, cbData / sizeof(WCHAR)), cbData,
285        wine_debugstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
286     HeapFree(GetProcessHeap(), 0, value);
287 }
288
289 static void test_set_value(void)
290 {
291     DWORD ret;
292
293     static const WCHAR name1W[] =   {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
294     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};
295     static const WCHAR emptyW[] = {0};
296     static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
297     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};
298     static const WCHAR substring2W[] = {'T','h','i','s',0};
299
300     static const char name1A[] =   "CleanSingleString";
301     static const char name2A[] =   "SomeIntraZeroedString";
302     static const char emptyA[] = "";
303     static const char string1A[] = "ThisNeverBreaks";
304     static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
305     static const char substring2A[] = "This";
306
307     if (0)
308     {
309         /* Crashes on NT4, Windows 2000 and XP SP1 */
310         ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
311         ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
312     }
313
314     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
315     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
316     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
317     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
318
319     /* RegSetValueA ignores the size passed in */
320     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
321     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
322     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
323     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
324
325     /* stops at first null */
326     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
327     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
328     test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
329     test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
330
331     /* only REG_SZ is supported */
332     ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
333     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
334     ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
335     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
336     ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
337     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
338
339     /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
340      * Surprisingly enough we're supposed to get zero bytes out of it.
341      */
342     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
343     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
344     test_hkey_main_Value_A(name1A, NULL, 0);
345     test_hkey_main_Value_W(name1W, NULL, 0);
346
347     /* test RegSetValueExA with an empty string */
348     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
349     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
350     test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
351     test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
352
353     /* test RegSetValueExA with off-by-one size */
354     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
355     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
356     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
357     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
358
359     /* test RegSetValueExA with normal string */
360     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
361     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
362     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
363     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
364
365     /* test RegSetValueExA with intrazeroed string */
366     ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
367     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
368     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
369     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
370
371     /* 9x doesn't support W-calls, so don't test them then */
372     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return; 
373
374     if (0)
375     {
376         /* Crashes on NT4, Windows 2000 and XP SP1 */
377         ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
378         ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
379     }
380
381     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
382     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
383     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
384     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
385
386     /* RegSetValueA ignores the size passed in */
387     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
388     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
389     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
390     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
391
392     /* stops at first null */
393     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
394     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
395     test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
396     test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
397
398     /* only REG_SZ is supported */
399     ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
400     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
401     ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
402     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
403     ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
404     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
405
406     /* test RegSetValueExW with off-by-one size */
407     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
408     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
409     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
410     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
411
412     /* test RegSetValueExW with normal string */
413     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
414     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
415     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
416     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
417
418     /* test RegSetValueExW with intrazeroed string */
419     ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
420     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
421     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
422     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
423 }
424
425 static void create_test_entries(void)
426 {
427     static const DWORD qw[2] = { 0x12345678, 0x87654321 };
428
429     SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
430     SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
431
432     ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
433         "RegSetValueExA failed\n");
434     ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
435         "RegSetValueExA failed\n");
436     ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, NULL, 0),
437        "RegSetValueExA failed\n");
438     ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1), 
439         "RegSetValueExA failed\n");
440     ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
441         "RegSetValueExA failed\n");
442     ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
443         "RegSetValueExA failed\n");
444     ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
445         "RegSetValueExA failed\n");
446 }
447         
448 static void test_enum_value(void)
449 {
450     DWORD res;
451     HKEY test_key;
452     char value[20], data[20];
453     WCHAR valueW[20], dataW[20];
454     DWORD val_count, data_count, type;
455     static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
456     static const WCHAR testW[] = {'T','e','s','t',0};
457     static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
458
459     /* create the working key for new 'Test' value */
460     res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
461     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
462
463     /* check NULL data with zero length */
464     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
465     if (GetVersion() & 0x80000000)
466         ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
467     else
468         ok( !res, "RegSetValueExA returned %d\n", res );
469     res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
470     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
471     res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
472     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
473
474     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
475     ok( res == 0, "RegSetValueExA failed error %d\n", res );
476
477     /* overflow both name and data */
478     val_count = 2;
479     data_count = 2;
480     type = 1234;
481     strcpy( value, "xxxxxxxxxx" );
482     strcpy( data, "xxxxxxxxxx" );
483     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
484     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
485     ok( val_count == 2, "val_count set to %d\n", val_count );
486     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
487     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
488     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
489     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
490
491     /* overflow name */
492     val_count = 3;
493     data_count = 20;
494     type = 1234;
495     strcpy( value, "xxxxxxxxxx" );
496     strcpy( data, "xxxxxxxxxx" );
497     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
498     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
499     /* Win9x returns 2 as specified by MSDN but NT returns 3... */
500     ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
501     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
502     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
503     /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
504     ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ), 
505         "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
506     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
507         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
508
509     /* overflow empty name */
510     val_count = 0;
511     data_count = 20;
512     type = 1234;
513     strcpy( value, "xxxxxxxxxx" );
514     strcpy( data, "xxxxxxxxxx" );
515     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
516     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
517     ok( val_count == 0, "val_count set to %d\n", val_count );
518     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
519     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
520     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
521     /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
522     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
523         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
524
525     /* overflow data */
526     val_count = 20;
527     data_count = 2;
528     type = 1234;
529     strcpy( value, "xxxxxxxxxx" );
530     strcpy( data, "xxxxxxxxxx" );
531     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
532     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
533     ok( val_count == 20, "val_count set to %d\n", val_count );
534     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
535     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
536     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
537     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
538
539     /* no overflow */
540     val_count = 20;
541     data_count = 20;
542     type = 1234;
543     strcpy( value, "xxxxxxxxxx" );
544     strcpy( data, "xxxxxxxxxx" );
545     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
546     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
547     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
548     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
549     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
550     ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
551     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
552
553     /* Unicode tests */
554
555     SetLastError(0xdeadbeef);
556     res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
557     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
558     {
559         skip("RegSetValueExW is not implemented\n");
560         goto cleanup;
561     }
562     ok( res == 0, "RegSetValueExW failed error %d\n", res );
563
564     /* overflow both name and data */
565     val_count = 2;
566     data_count = 2;
567     type = 1234;
568     memcpy( valueW, xxxW, sizeof(xxxW) );
569     memcpy( dataW, xxxW, sizeof(xxxW) );
570     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
571     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
572     ok( val_count == 2, "val_count set to %d\n", val_count );
573     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
574     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
575     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
576     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
577
578     /* overflow name */
579     val_count = 3;
580     data_count = 20;
581     type = 1234;
582     memcpy( valueW, xxxW, sizeof(xxxW) );
583     memcpy( dataW, xxxW, sizeof(xxxW) );
584     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
585     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
586     ok( val_count == 3, "val_count set to %d\n", val_count );
587     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
588     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
589     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
590     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
591
592     /* overflow data */
593     val_count = 20;
594     data_count = 2;
595     type = 1234;
596     memcpy( valueW, xxxW, sizeof(xxxW) );
597     memcpy( dataW, xxxW, sizeof(xxxW) );
598     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
599     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
600     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
601     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
602     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
603     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
604     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
605
606     /* no overflow */
607     val_count = 20;
608     data_count = 20;
609     type = 1234;
610     memcpy( valueW, xxxW, sizeof(xxxW) );
611     memcpy( dataW, xxxW, sizeof(xxxW) );
612     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
613     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
614     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
615     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
616     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
617     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
618     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
619
620 cleanup:
621     RegDeleteKeyA(test_key, "");
622     RegCloseKey(test_key);
623 }
624
625 static void test_query_value_ex(void)
626 {
627     DWORD ret;
628     DWORD size;
629     DWORD type;
630     BYTE buffer[10];
631     
632     ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
633     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
634     ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
635     ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
636
637     type = 0xdeadbeef;
638     size = 0xdeadbeef;
639     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
640     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
641     /* the type parameter is cleared on Win9x, but is set to a random value on
642      * NT, so don't do that test there. The size parameter is left untouched on Win9x
643      * but cleared on NT+, this can be tested on all platforms.
644      */
645     if (GetVersion() & 0x80000000)
646     {
647         ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
648         ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
649     }
650     else
651     {
652         trace("test_query_value_ex: type set to: 0x%08x\n", type);
653         ok(size == 0, "size should have been set to 0 instead of %d\n", size);
654     }
655
656     size = sizeof(buffer);
657     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
658     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
659     ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
660
661     size = 4;
662     ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
663     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
664 }
665
666 static void test_get_value(void)
667 {
668     DWORD ret;
669     DWORD size;
670     DWORD type;
671     DWORD dw, qw[2];
672     CHAR buf[80];
673     CHAR expanded[] = "bar\\subdir1";
674     CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
675    
676     if(!pRegGetValueA)
677     {
678         skip("RegGetValue not available on this platform\n");
679         return;
680     }
681
682     /* Invalid parameter */
683     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
684     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
685
686     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
687     size = type = dw = 0xdeadbeef;
688     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
689     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
690     ok(size == 4, "size=%d\n", size);
691     ok(type == REG_DWORD, "type=%d\n", type);
692     ok(dw == 0x12345678, "dw=%d\n", dw);
693
694     /* Query by subkey-name */
695     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
696     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
697
698     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
699     size = type = dw = 0xdeadbeef;
700     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
701     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
702     /* Although the function failed all values are retrieved */
703     ok(size == 4, "size=%d\n", size);
704     ok(type == REG_DWORD, "type=%d\n", type);
705     ok(dw == 0x12345678, "dw=%d\n", dw);
706
707     /* Test RRF_ZEROONFAILURE */
708     type = dw = 0xdeadbeef; size = 4;
709     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
710     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
711     /* Again all values are retrieved ... */
712     ok(size == 4, "size=%d\n", size);
713     ok(type == REG_DWORD, "type=%d\n", type);
714     /* ... except the buffer, which is zeroed out */
715     ok(dw == 0, "dw=%d\n", dw);
716
717     /* Test RRF_ZEROONFAILURE with a NULL buffer... */
718     type = size = 0xbadbeef;
719     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
720     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
721     ok(size == 4, "size=%d\n", size);
722     ok(type == REG_DWORD, "type=%d\n", type);
723
724     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
725     size = type = dw = 0xdeadbeef;
726     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
727     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
728     ok(size == 4, "size=%d\n", size);
729     ok(type == REG_DWORD, "type=%d\n", type);
730     ok(dw == 0x12345678, "dw=%d\n", dw);
731
732     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
733     size = type = dw = 0xdeadbeef;
734     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
735     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
736     ok(size == 4, "size=%d\n", size);
737     ok(type == REG_BINARY, "type=%d\n", type);
738     ok(dw == 0x12345678, "dw=%d\n", dw);
739     
740     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
741     qw[0] = qw[1] = size = type = 0xdeadbeef;
742     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
743     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
744     ok(size == 8, "size=%d\n", size);
745     ok(type == REG_BINARY, "type=%d\n", type);
746     ok(qw[0] == 0x12345678 && 
747        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
748     
749     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
750     type = dw = 0xdeadbeef; size = 4;
751     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
752     ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
753     ok(dw == 0xdeadbeef, "dw=%d\n", dw);
754     ok(size == 8, "size=%d\n", size);
755
756     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
757     qw[0] = qw[1] = size = type = 0xdeadbeef;
758     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
759     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
760     ok(size == 8, "size=%d\n", size);
761     ok(type == REG_BINARY, "type=%d\n", type);
762     ok(qw[0] == 0x12345678 &&
763        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
764
765     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
766     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
767     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
768     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
769     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
770     ok(type == REG_SZ, "type=%d\n", type);
771     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
772
773     /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
774     type = 0xdeadbeef; size = 0;
775     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
776     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
777     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
778     ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
779        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
780     ok(type == REG_SZ, "type=%d\n", type);
781
782     /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
783     strcpy(buf, sTestpath1);
784     type = 0xdeadbeef;
785     size = sizeof(buf);
786     ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
787     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
788     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
789     ok(size == 0, "size=%d\n", size);
790     ok(type == REG_SZ, "type=%d\n", type);
791     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
792
793     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
794     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
795     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
796     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
797     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
798     ok(type == REG_SZ, "type=%d\n", type);
799     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
800
801     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
802     size = 0;
803     ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
804     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
805     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
806     ok((size == strlen(expanded2)+1) || (size == strlen(sTestpath2)+1),
807         "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
808
809     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
810     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
811     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
812     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
813     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
814     ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1), 
815         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
816     ok(type == REG_SZ, "type=%d\n", type);
817     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
818
819     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
820     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
821     ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
822     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
823     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
824     ok((size == strlen(expanded2)+1) || (size == strlen(sTestpath2)+1),
825         "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
826     ok(type == REG_SZ, "type=%d\n", type);
827     ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
828
829     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
830     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
831     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
832     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
833     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
834     ok(type == REG_EXPAND_SZ, "type=%d\n", type);
835     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
836
837     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
838     size = 0xbadbeef;
839     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
840     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
841     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
842     ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
843        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
844
845     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
846     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
847     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
848
849     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
850     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
851     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
852
853
854 static void test_reg_open_key(void)
855 {
856     DWORD ret = 0;
857     HKEY hkResult = NULL;
858     HKEY hkPreserve = NULL;
859
860     /* successful open */
861     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
862     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
863     ok(hkResult != NULL, "expected hkResult != NULL\n");
864     hkPreserve = hkResult;
865
866     /* these tests fail on Win9x, but we want to be compatible with NT, so
867      * run them if we can */
868     if (!(GetVersion() & 0x80000000))
869     {
870         /* open same key twice */
871         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
872         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
873         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
874         ok(hkResult != NULL, "hkResult != NULL\n");
875         RegCloseKey(hkResult);
876     
877         /* open nonexistent key
878         * check that hkResult is set to NULL
879         */
880         hkResult = hkPreserve;
881         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
882         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
883         ok(hkResult == NULL, "expected hkResult == NULL\n");
884     
885         /* open the same nonexistent key again to make sure the key wasn't created */
886         hkResult = hkPreserve;
887         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
888         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
889         ok(hkResult == NULL, "expected hkResult == NULL\n");
890     
891         /* send in NULL lpSubKey
892         * check that hkResult receives the value of hKey
893         */
894         hkResult = hkPreserve;
895         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
896         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
897         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
898     
899         /* send empty-string in lpSubKey */
900         hkResult = hkPreserve;
901         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
902         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
903         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
904     
905         /* send in NULL lpSubKey and NULL hKey
906         * hkResult is set to NULL
907         */
908         hkResult = hkPreserve;
909         ret = RegOpenKeyA(NULL, NULL, &hkResult);
910         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
911         ok(hkResult == NULL, "expected hkResult == NULL\n");
912     }
913
914     /* only send NULL hKey
915      * the value of hkResult remains unchanged
916      */
917     hkResult = hkPreserve;
918     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
919     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
920        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
921     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
922     RegCloseKey(hkResult);
923
924     /* send in NULL hkResult */
925     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
926     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
927
928     /*  beginning backslash character */
929     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
930        ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
931            ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
932            , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
933 }
934
935 static void test_reg_create_key(void)
936 {
937     LONG ret;
938     HKEY hkey1, hkey2;
939     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
940     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
941     /* should succeed: all versions of Windows ignore the access rights
942      * to the parent handle */
943     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
944     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
945
946     /* clean up */
947     RegDeleteKey(hkey2, "");
948     RegDeleteKey(hkey1, "");
949
950     /*  beginning backslash character */
951     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
952     if (!(GetVersion() & 0x80000000))
953         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
954     else {
955         ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
956         RegDeleteKey(hkey1, NULL);
957     }
958 }
959
960 static void test_reg_close_key(void)
961 {
962     DWORD ret = 0;
963     HKEY hkHandle;
964
965     /* successfully close key
966      * hkHandle remains changed after call to RegCloseKey
967      */
968     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
969     ret = RegCloseKey(hkHandle);
970     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
971
972     /* try to close the key twice */
973     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
974     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
975        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
976     
977     /* try to close a NULL handle */
978     ret = RegCloseKey(NULL);
979     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
980        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
981
982     /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
983      * win98 doesn't give a new handle when the same key is opened.
984      * Not re-opening will make some next tests fail.
985      */
986     if (hkey_main == hkHandle)
987     {
988         trace("The main handle is most likely closed, so re-opening\n");
989         RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
990     }
991 }
992
993 static void test_reg_delete_key(void)
994 {
995     DWORD ret;
996
997     ret = RegDeleteKey(hkey_main, NULL);
998
999     /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1000      * there are also no subkeys available it will delete the key pointed to by hkey_main.
1001      * Not re-creating will make some next tests fail.
1002      */
1003     if (ret == ERROR_SUCCESS)
1004     {
1005         trace("We are probably running on NT4 or W2K as the main key is deleted,"
1006             " re-creating the main key\n");
1007         setup_main_key();
1008     }
1009     else
1010         ok(ret == ERROR_INVALID_PARAMETER ||
1011            ret == ERROR_ACCESS_DENIED ||
1012            ret == ERROR_BADKEY, /* Win95 */
1013            "ret=%d\n", ret);
1014 }
1015
1016 static void test_reg_save_key(void)
1017 {
1018     DWORD ret;
1019
1020     ret = RegSaveKey(hkey_main, "saved_key", NULL);
1021     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1022 }
1023
1024 static void test_reg_load_key(void)
1025 {
1026     DWORD ret;
1027     HKEY hkHandle;
1028
1029     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1030     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1031
1032     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1033     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1034
1035     RegCloseKey(hkHandle);
1036 }
1037
1038 static void test_reg_unload_key(void)
1039 {
1040     DWORD ret;
1041
1042     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
1043     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1044
1045     DeleteFile("saved_key");
1046     DeleteFile("saved_key.LOG");
1047 }
1048
1049 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1050 {
1051     TOKEN_PRIVILEGES tp;
1052     HANDLE hToken;
1053     LUID luid;
1054
1055     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1056         return FALSE;
1057
1058     if(!LookupPrivilegeValue(NULL, privilege, &luid))
1059     {
1060         CloseHandle(hToken);
1061         return FALSE;
1062     }
1063
1064     tp.PrivilegeCount = 1;
1065     tp.Privileges[0].Luid = luid;
1066     
1067     if (set)
1068         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1069     else
1070         tp.Privileges[0].Attributes = 0;
1071
1072     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1073     if (GetLastError() != ERROR_SUCCESS)
1074     {
1075         CloseHandle(hToken);
1076         return FALSE;
1077     }
1078
1079     CloseHandle(hToken);
1080     return TRUE;
1081 }
1082
1083 /* tests that show that RegConnectRegistry and 
1084    OpenSCManager accept computer names without the
1085    \\ prefix (what MSDN says).   */
1086 static void test_regconnectregistry( void)
1087 {
1088     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1089     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1090     DWORD len = sizeof(compName) ;
1091     BOOL ret;
1092     LONG retl;
1093     HKEY hkey;
1094     SC_HANDLE schnd;
1095
1096     SetLastError(0xdeadbeef);
1097     ret = GetComputerNameA(compName, &len);
1098     ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1099     if( !ret) return;
1100
1101     lstrcpyA(netwName, "\\\\");
1102     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1103
1104     retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1105     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1106     if( !retl) RegCloseKey( hkey);
1107
1108     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1109     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1110     if( !retl) RegCloseKey( hkey);
1111
1112     SetLastError(0xdeadbeef);
1113     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
1114     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1115     {
1116         skip("OpenSCManagerA is not implemented\n");
1117         return;
1118     }
1119
1120     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1121     CloseServiceHandle( schnd);
1122
1123     SetLastError(0xdeadbeef);
1124     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
1125     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1126     CloseServiceHandle( schnd);
1127
1128 }
1129
1130 static void test_reg_query_value(void)
1131 {
1132     HKEY subkey;
1133     CHAR val[MAX_PATH];
1134     WCHAR valW[5];
1135     LONG size, ret;
1136
1137     static const WCHAR expected[] = {'d','a','t','a',0};
1138
1139     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1140     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1141
1142     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1143     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1144
1145     /* try an invalid hkey */
1146     SetLastError(0xdeadbeef);
1147     size = MAX_PATH;
1148     ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1149     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1150        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1151     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1152
1153     /* try a NULL hkey */
1154     SetLastError(0xdeadbeef);
1155     size = MAX_PATH;
1156     ret = RegQueryValueA(NULL, "subkey", val, &size);
1157     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1158        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1159     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1160
1161     /* try a NULL value */
1162     size = MAX_PATH;
1163     ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1164     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1165     ok(size == 5, "Expected 5, got %d\n", size);
1166
1167     /* try a NULL size */
1168     SetLastError(0xdeadbeef);
1169     val[0] = '\0';
1170     ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1171     ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1172     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1173     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1174
1175     /* try a NULL value and size */
1176     ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1177     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1178
1179     /* try a size too small */
1180     SetLastError(0xdeadbeef);
1181     val[0] = '\0';
1182     size = 1;
1183     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1184     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1185     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1186     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1187     ok(size == 5, "Expected 5, got %d\n", size);
1188
1189     /* successfully read the value using 'subkey' */
1190     size = MAX_PATH;
1191     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1192     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1193     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1194     ok(size == 5, "Expected 5, got %d\n", size);
1195
1196     /* successfully read the value using the subkey key */
1197     size = MAX_PATH;
1198     ret = RegQueryValueA(subkey, NULL, val, &size);
1199     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1200     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1201     ok(size == 5, "Expected 5, got %d\n", size);
1202
1203     /* unicode - try size too small */
1204     SetLastError(0xdeadbeef);
1205     valW[0] = '\0';
1206     size = 0;
1207     ret = RegQueryValueW(subkey, NULL, valW, &size);
1208     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1209     {
1210         skip("RegQueryValueW is not implemented\n");
1211         goto cleanup;
1212     }
1213     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1214     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1215     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1216     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1217
1218     /* unicode - try size in WCHARS */
1219     SetLastError(0xdeadbeef);
1220     size = sizeof(valW) / sizeof(WCHAR);
1221     ret = RegQueryValueW(subkey, NULL, valW, &size);
1222     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1223     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1224     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1225     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1226
1227     /* unicode - successfully read the value */
1228     size = sizeof(valW);
1229     ret = RegQueryValueW(subkey, NULL, valW, &size);
1230     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1231     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1232     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1233
1234     /* unicode - set the value without a NULL terminator */
1235     ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1236     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1237
1238     /* unicode - read the unterminated value, value is terminated for us */
1239     memset(valW, 'a', sizeof(valW));
1240     size = sizeof(valW);
1241     ret = RegQueryValueW(subkey, NULL, valW, &size);
1242     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1243     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1244     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1245
1246 cleanup:
1247     RegDeleteKeyA(subkey, "");
1248     RegCloseKey(subkey);
1249 }
1250
1251 static void test_reg_delete_tree(void)
1252 {
1253     CHAR buffer[MAX_PATH];
1254     HKEY subkey, subkey2;
1255     LONG size, ret;
1256
1257     if(!pRegDeleteTreeA) {
1258         skip("Skipping RegDeleteTreeA tests, function not present\n");
1259         return;
1260     }
1261
1262     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1263     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1264     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1265     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1266     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1267     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1268     ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1269     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1270     ret = RegCloseKey(subkey2);
1271     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1272
1273     ret = pRegDeleteTreeA(subkey, "subkey2");
1274     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1275     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1276         "subkey2 was not deleted\n");
1277     size = MAX_PATH;
1278     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1279         "Default value of subkey not longer present\n");
1280
1281     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1282     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1283     ret = RegCloseKey(subkey2);
1284     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1285     ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1286     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1287     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1288         "subkey2 was not deleted\n");
1289     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1290         "Default value of subkey not longer present\n");
1291
1292     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1293     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1294     ret = RegCloseKey(subkey2);
1295     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1296     ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1297     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1298     ret = RegCloseKey(subkey2);
1299     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1300     ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1301     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1302     ret = pRegDeleteTreeA(subkey, NULL);
1303     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1304     ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1305         "subkey was deleted\n");
1306     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1307         "subkey2 was not deleted\n");
1308     ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1309         "subkey3 was not deleted\n");
1310     size = MAX_PATH;
1311     ret = RegQueryValueA(subkey, NULL, buffer, &size);
1312     ok(ret == ERROR_SUCCESS,
1313         "Default value of subkey is not present\n");
1314     ok(!lstrlenA(buffer),
1315         "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1316     size = MAX_PATH;
1317     ok(RegQueryValueA(subkey, "value", buffer, &size),
1318         "Value is still present\n");
1319
1320     ret = pRegDeleteTreeA(hkey_main, "not-here");
1321     ok(ret == ERROR_FILE_NOT_FOUND,
1322         "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1323 }
1324
1325 START_TEST(registry)
1326 {
1327     /* Load pointers for functions that are not available in all Windows versions */
1328     InitFunctionPtrs();
1329
1330     setup_main_key();
1331     test_set_value();
1332     create_test_entries();
1333     test_enum_value();
1334     test_query_value_ex();
1335     test_get_value();
1336     test_reg_open_key();
1337     test_reg_create_key();
1338     test_reg_close_key();
1339     test_reg_delete_key();
1340     test_reg_query_value();
1341
1342     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1343     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1344         set_privileges(SE_RESTORE_NAME, TRUE))
1345     {
1346         test_reg_save_key();
1347         test_reg_load_key();
1348         test_reg_unload_key();
1349
1350         set_privileges(SE_BACKUP_NAME, FALSE);
1351         set_privileges(SE_RESTORE_NAME, FALSE);
1352     }
1353
1354     test_reg_delete_tree();
1355
1356     /* cleanup */
1357     delete_key( hkey_main );
1358     
1359     test_regconnectregistry();
1360 }