msi/tests: Free exception information.
[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 "winternl.h"
28 #include "winreg.h"
29 #include "winsvc.h"
30 #include "winerror.h"
31
32 static HKEY hkey_main;
33 static DWORD GLE;
34
35 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
36 static const char * sTestpath2 = "%FOO%\\subdir1";
37
38 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
39 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
40 static NTSTATUS (WINAPI * pNtDeleteKey)(HANDLE);
41 static NTSTATUS (WINAPI * pRtlFormatCurrentUserKeyPath)(UNICODE_STRING*);
42 static NTSTATUS (WINAPI * pRtlFreeUnicodeString)(PUNICODE_STRING);
43
44
45 /* Debugging functions from wine/libs/wine/debug.c */
46
47 /* allocate some tmp string space */
48 /* FIXME: this is not 100% thread-safe */
49 static char *get_temp_buffer( int size )
50 {
51     static char *list[32];
52     static UINT pos;
53     char *ret;
54     UINT idx;
55
56     idx = ++pos % (sizeof(list)/sizeof(list[0]));
57     if (list[idx])
58         ret = HeapReAlloc( GetProcessHeap(), 0, list[idx], size );
59     else
60         ret = HeapAlloc( GetProcessHeap(), 0, size );
61     if (ret) list[idx] = ret;
62     return ret;
63 }
64
65 static const char *wine_debugstr_an( const char *str, int n )
66 {
67     static const char hex[16] = "0123456789abcdef";
68     char *dst, *res;
69     size_t size;
70
71     if (!((ULONG_PTR)str >> 16))
72     {
73         if (!str) return "(null)";
74         res = get_temp_buffer( 6 );
75         sprintf( res, "#%04x", LOWORD(str) );
76         return res;
77     }
78     if (n == -1) n = strlen(str);
79     if (n < 0) n = 0;
80     size = 10 + min( 300, n * 4 );
81     dst = res = get_temp_buffer( size );
82     *dst++ = '"';
83     while (n-- > 0 && dst <= res + size - 9)
84     {
85         unsigned char c = *str++;
86         switch (c)
87         {
88         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
89         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
90         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
91         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
92         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
93         default:
94             if (c >= ' ' && c <= 126)
95                 *dst++ = c;
96             else
97             {
98                 *dst++ = '\\';
99                 *dst++ = 'x';
100                 *dst++ = hex[(c >> 4) & 0x0f];
101                 *dst++ = hex[c & 0x0f];
102             }
103         }
104     }
105     *dst++ = '"';
106     if (n > 0)
107     {
108         *dst++ = '.';
109         *dst++ = '.';
110         *dst++ = '.';
111     }
112     *dst++ = 0;
113     return res;
114 }
115
116 #define ADVAPI32_GET_PROC(func) \
117     p ## func = (void*)GetProcAddress(hadvapi32, #func)
118
119 static void InitFunctionPtrs(void)
120 {
121     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
122     HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
123
124     /* This function was introduced with Windows 2003 SP1 */
125     ADVAPI32_GET_PROC(RegGetValueA);
126     ADVAPI32_GET_PROC(RegDeleteTreeA);
127
128     pRtlFormatCurrentUserKeyPath = (void *)GetProcAddress( hntdll, "RtlFormatCurrentUserKeyPath" );
129     pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
130     pNtDeleteKey = (void *)GetProcAddress( hntdll, "NtDeleteKey" );
131 }
132
133 /* delete key and all its subkeys */
134 static DWORD delete_key( HKEY hkey )
135 {
136     char name[MAX_PATH];
137     DWORD ret;
138
139     while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
140     {
141         HKEY tmp;
142         if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
143         {
144             ret = delete_key( tmp );
145             RegCloseKey( tmp );
146         }
147         if (ret) break;
148     }
149     if (ret != ERROR_NO_MORE_ITEMS) return ret;
150     RegDeleteKeyA( hkey, "" );
151     return 0;
152 }
153
154 static void setup_main_key(void)
155 {
156     if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
157
158     assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
159 }
160
161 #define lok ok_(__FILE__, line)
162 #define test_hkey_main_Value_A(name, string, full_byte_len) _test_hkey_main_Value_A(__LINE__, name, string, full_byte_len)
163 static void _test_hkey_main_Value_A(int line, LPCSTR name, LPCSTR string,
164                                    DWORD full_byte_len)
165 {
166     DWORD ret, type, cbData;
167     DWORD str_byte_len;
168     BYTE* value;
169
170     type=0xdeadbeef;
171     cbData=0xdeadbeef;
172     /* When successful RegQueryValueExA() leaves GLE as is,
173      * so we must reset it to detect unimplemented functions.
174      */
175     SetLastError(0xdeadbeef);
176     ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
177     GLE = GetLastError();
178     lok(ret == ERROR_SUCCESS, "RegQueryValueExA/1 failed: %d, GLE=%d\n", ret, GLE);
179     /* It is wrong for the Ansi version to not be implemented */
180     ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
181     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
182
183     str_byte_len = (string ? lstrlenA(string) : 0) + 1;
184     lok(type == REG_SZ, "RegQueryValueExA/1 returned type %d\n", type);
185     lok(cbData == full_byte_len || cbData == str_byte_len /* Win9x */,
186         "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
187
188     value = HeapAlloc(GetProcessHeap(), 0, cbData+1);
189     memset(value, 0xbd, cbData+1);
190     type=0xdeadbeef;
191     ret = RegQueryValueExA(hkey_main, name, NULL, &type, value, &cbData);
192     GLE = GetLastError();
193     lok(ret == ERROR_SUCCESS, "RegQueryValueExA/2 failed: %d, GLE=%d\n", ret, GLE);
194     if (!string)
195     {
196         /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
197         lok(*value == 0xbd || (cbData == 1 && *value == '\0') /* Win9x */,
198            "RegQueryValueExA overflowed: cbData=%u *value=%02x\n", cbData, *value);
199     }
200     else
201     {
202         lok(memcmp(value, string, cbData) == 0, "RegQueryValueExA/2 failed: %s/%d != %s/%d\n",
203            wine_debugstr_an((char*)value, cbData), cbData,
204            wine_debugstr_an(string, full_byte_len), full_byte_len);
205         lok(*(value+cbData) == 0xbd, "RegQueryValueExA/2 overflowed at offset %u: %02x != bd\n", cbData, *(value+cbData));
206     }
207     HeapFree(GetProcessHeap(), 0, value);
208 }
209
210 #define test_hkey_main_Value_W(name, string, full_byte_len) _test_hkey_main_Value_W(__LINE__, name, string, full_byte_len)
211 static void _test_hkey_main_Value_W(int line, LPCWSTR name, LPCWSTR string,
212                                    DWORD full_byte_len)
213 {
214     DWORD ret, type, cbData;
215     BYTE* value;
216
217     type=0xdeadbeef;
218     cbData=0xdeadbeef;
219     /* When successful RegQueryValueExW() leaves GLE as is,
220      * so we must reset it to detect unimplemented functions.
221      */
222     SetLastError(0xdeadbeef);
223     ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
224     GLE = GetLastError();
225     lok(ret == ERROR_SUCCESS, "RegQueryValueExW/1 failed: %d, GLE=%d\n", ret, GLE);
226     if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
227     {
228         win_skip("RegQueryValueExW() is not implemented\n");
229         return;
230     }
231
232     lok(type == REG_SZ, "RegQueryValueExW/1 returned type %d\n", type);
233     lok(cbData == full_byte_len,
234         "cbData=%d instead of %d\n", cbData, full_byte_len);
235
236     /* Give enough space to overflow by one WCHAR */
237     value = HeapAlloc(GetProcessHeap(), 0, cbData+2);
238     memset(value, 0xbd, cbData+2);
239     type=0xdeadbeef;
240     ret = RegQueryValueExW(hkey_main, name, NULL, &type, value, &cbData);
241     GLE = GetLastError();
242     lok(ret == ERROR_SUCCESS, "RegQueryValueExW/2 failed: %d, GLE=%d\n", ret, GLE);
243     if (string)
244     {
245         lok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
246            wine_dbgstr_wn((WCHAR*)value, cbData / sizeof(WCHAR)), cbData,
247            wine_dbgstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
248     }
249     /* This implies that when cbData == 0, RegQueryValueExW() should not modify the buffer */
250     lok(*(value+cbData) == 0xbd, "RegQueryValueExW/2 overflowed at %u: %02x != bd\n", cbData, *(value+cbData));
251     lok(*(value+cbData+1) == 0xbd, "RegQueryValueExW/2 overflowed at %u+1: %02x != bd\n", cbData, *(value+cbData+1));
252     HeapFree(GetProcessHeap(), 0, value);
253 }
254
255 static void test_set_value(void)
256 {
257     DWORD ret;
258
259     static const WCHAR name1W[] =   {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
260     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};
261     static const WCHAR emptyW[] = {0};
262     static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
263     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};
264     static const WCHAR substring2W[] = {'T','h','i','s',0};
265
266     static const char name1A[] =   "CleanSingleString";
267     static const char name2A[] =   "SomeIntraZeroedString";
268     static const char emptyA[] = "";
269     static const char string1A[] = "ThisNeverBreaks";
270     static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
271     static const char substring2A[] = "This";
272
273     if (0)
274     {
275         /* Crashes on NT4, Windows 2000 and XP SP1 */
276         ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
277         ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
278     }
279
280     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
281     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
282     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
283     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
284
285     /* RegSetValueA ignores the size passed in */
286     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
287     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
288     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
289     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
290
291     /* stops at first null */
292     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
293     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
294     test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
295     test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
296
297     /* only REG_SZ is supported on NT*/
298     ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
299     /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
300     ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
301         "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
302
303     ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
304     /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
305     ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
306         "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
307
308     ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
309     /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
310     ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
311         "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
312
313     /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
314      * Surprisingly enough we're supposed to get zero bytes out of it.
315      */
316     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
317     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
318     test_hkey_main_Value_A(name1A, NULL, 0);
319     test_hkey_main_Value_W(name1W, NULL, 0);
320
321     /* test RegSetValueExA with an empty string */
322     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
323     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
324     test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
325     test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
326
327     /* test RegSetValueExA with off-by-one size */
328     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
329     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
330     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
331     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
332
333     /* test RegSetValueExA with normal string */
334     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
335     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
336     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
337     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
338
339     /* test RegSetValueExA with intrazeroed string */
340     ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
341     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
342     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
343     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
344
345     /* 9x doesn't support W-calls, so don't test them then */
346     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return; 
347
348     if (0)
349     {
350         /* Crashes on NT4, Windows 2000 and XP SP1 */
351         ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
352         ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
353     }
354
355     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
356     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
357     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
358     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
359
360     /* RegSetValueA ignores the size passed in */
361     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
362     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
363     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
364     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
365
366     /* stops at first null */
367     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
368     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
369     test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
370     test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
371
372     /* only REG_SZ is supported */
373     ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
374     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
375     ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
376     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
377     ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
378     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
379
380     /* test RegSetValueExW with off-by-one size */
381     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
382     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
383     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
384     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
385
386     /* test RegSetValueExW with normal string */
387     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
388     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
389     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
390     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
391
392     /* test RegSetValueExW with intrazeroed string */
393     ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
394     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
395     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
396     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
397 }
398
399 static void create_test_entries(void)
400 {
401     static const DWORD qw[2] = { 0x12345678, 0x87654321 };
402
403     SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
404     SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
405
406     ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
407         "RegSetValueExA failed\n");
408     ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
409         "RegSetValueExA failed\n");
410     ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
411        "RegSetValueExA failed\n");
412     ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1), 
413         "RegSetValueExA failed\n");
414     ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
415         "RegSetValueExA failed\n");
416     ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
417         "RegSetValueExA failed\n");
418     ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
419         "RegSetValueExA failed\n");
420 }
421         
422 static void test_enum_value(void)
423 {
424     DWORD res;
425     HKEY test_key;
426     char value[20], data[20];
427     WCHAR valueW[20], dataW[20];
428     DWORD val_count, data_count, type;
429     static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
430     static const WCHAR testW[] = {'T','e','s','t',0};
431     static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
432
433     /* create the working key for new 'Test' value */
434     res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
435     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
436
437     /* check NULL data with zero length */
438     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
439     if (GetVersion() & 0x80000000)
440         ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
441     else
442         ok( !res, "RegSetValueExA returned %d\n", res );
443     res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
444     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
445     res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
446     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
447
448     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
449     ok( res == 0, "RegSetValueExA failed error %d\n", res );
450
451     /* overflow both name and data */
452     val_count = 2;
453     data_count = 2;
454     type = 1234;
455     strcpy( value, "xxxxxxxxxx" );
456     strcpy( data, "xxxxxxxxxx" );
457     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
458     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
459     ok( val_count == 2, "val_count set to %d\n", val_count );
460     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
461     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
462     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
463     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
464
465     /* overflow name */
466     val_count = 3;
467     data_count = 20;
468     type = 1234;
469     strcpy( value, "xxxxxxxxxx" );
470     strcpy( data, "xxxxxxxxxx" );
471     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
472     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
473     /* Win9x returns 2 as specified by MSDN but NT returns 3... */
474     ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
475     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
476     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
477     /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
478     ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ), 
479         "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
480     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
481         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
482
483     /* overflow empty name */
484     val_count = 0;
485     data_count = 20;
486     type = 1234;
487     strcpy( value, "xxxxxxxxxx" );
488     strcpy( data, "xxxxxxxxxx" );
489     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
490     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
491     ok( val_count == 0, "val_count set to %d\n", val_count );
492     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
493     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
494     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
495     /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
496     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
497         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
498
499     /* overflow data */
500     val_count = 20;
501     data_count = 2;
502     type = 1234;
503     strcpy( value, "xxxxxxxxxx" );
504     strcpy( data, "xxxxxxxxxx" );
505     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
506     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
507     ok( val_count == 20, "val_count set to %d\n", val_count );
508     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
509     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
510     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
511     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
512
513     /* no overflow */
514     val_count = 20;
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_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
521     ok( val_count == 4, "val_count set to %d instead of 4\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, "Test" ), "value is '%s' instead of Test\n", value );
525     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
526
527     /* Unicode tests */
528
529     SetLastError(0xdeadbeef);
530     res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
531     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
532     {
533         win_skip("RegSetValueExW is not implemented\n");
534         goto cleanup;
535     }
536     ok( res == 0, "RegSetValueExW failed error %d\n", res );
537
538     /* overflow both name and data */
539     val_count = 2;
540     data_count = 2;
541     type = 1234;
542     memcpy( valueW, xxxW, sizeof(xxxW) );
543     memcpy( dataW, xxxW, sizeof(xxxW) );
544     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
545     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
546     ok( val_count == 2, "val_count set to %d\n", val_count );
547     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
548     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
549     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
550     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
551
552     /* overflow name */
553     val_count = 3;
554     data_count = 20;
555     type = 1234;
556     memcpy( valueW, xxxW, sizeof(xxxW) );
557     memcpy( dataW, xxxW, sizeof(xxxW) );
558     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
559     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
560     ok( val_count == 3, "val_count set to %d\n", val_count );
561     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
562     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
563     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
564     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
565
566     /* overflow data */
567     val_count = 20;
568     data_count = 2;
569     type = 1234;
570     memcpy( valueW, xxxW, sizeof(xxxW) );
571     memcpy( dataW, xxxW, sizeof(xxxW) );
572     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
573     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
574     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
575     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
576     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
577     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
578     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
579
580     /* no overflow */
581     val_count = 20;
582     data_count = 20;
583     type = 1234;
584     memcpy( valueW, xxxW, sizeof(xxxW) );
585     memcpy( dataW, xxxW, sizeof(xxxW) );
586     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
587     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
588     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
589     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
590     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
591     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
592     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
593
594 cleanup:
595     RegDeleteKeyA(test_key, "");
596     RegCloseKey(test_key);
597 }
598
599 static void test_query_value_ex(void)
600 {
601     DWORD ret;
602     DWORD size;
603     DWORD type;
604     BYTE buffer[10];
605     
606     ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
607     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
608     ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
609     ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
610
611     type = 0xdeadbeef;
612     size = 0xdeadbeef;
613     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
614     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
615     /* the type parameter is cleared on Win9x, but is set to a random value on
616      * NT, so don't do that test there. The size parameter is left untouched on Win9x
617      * but cleared on NT+, this can be tested on all platforms.
618      */
619     if (GetVersion() & 0x80000000)
620     {
621         ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
622         ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
623     }
624     else
625     {
626         trace("test_query_value_ex: type set to: 0x%08x\n", type);
627         ok(size == 0, "size should have been set to 0 instead of %d\n", size);
628     }
629
630     size = sizeof(buffer);
631     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
632     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
633     ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
634
635     size = 4;
636     ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
637     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
638 }
639
640 static void test_get_value(void)
641 {
642     DWORD ret;
643     DWORD size;
644     DWORD type;
645     DWORD dw, qw[2];
646     CHAR buf[80];
647     CHAR expanded[] = "bar\\subdir1";
648     CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
649    
650     if(!pRegGetValueA)
651     {
652         win_skip("RegGetValue not available on this platform\n");
653         return;
654     }
655
656     /* Invalid parameter */
657     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
658     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
659
660     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
661     size = type = dw = 0xdeadbeef;
662     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
663     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
664     ok(size == 4, "size=%d\n", size);
665     ok(type == REG_DWORD, "type=%d\n", type);
666     ok(dw == 0x12345678, "dw=%d\n", dw);
667
668     /* Query by subkey-name */
669     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
670     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
671
672     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
673     size = type = dw = 0xdeadbeef;
674     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
675     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
676     /* Although the function failed all values are retrieved */
677     ok(size == 4, "size=%d\n", size);
678     ok(type == REG_DWORD, "type=%d\n", type);
679     ok(dw == 0x12345678, "dw=%d\n", dw);
680
681     /* Test RRF_ZEROONFAILURE */
682     type = dw = 0xdeadbeef; size = 4;
683     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
684     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
685     /* Again all values are retrieved ... */
686     ok(size == 4, "size=%d\n", size);
687     ok(type == REG_DWORD, "type=%d\n", type);
688     /* ... except the buffer, which is zeroed out */
689     ok(dw == 0, "dw=%d\n", dw);
690
691     /* Test RRF_ZEROONFAILURE with a NULL buffer... */
692     type = size = 0xbadbeef;
693     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
694     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
695     ok(size == 4, "size=%d\n", size);
696     ok(type == REG_DWORD, "type=%d\n", type);
697
698     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
699     size = type = dw = 0xdeadbeef;
700     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
701     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
702     ok(size == 4, "size=%d\n", size);
703     ok(type == REG_DWORD, "type=%d\n", type);
704     ok(dw == 0x12345678, "dw=%d\n", dw);
705
706     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
707     size = type = dw = 0xdeadbeef;
708     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
709     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
710     ok(size == 4, "size=%d\n", size);
711     ok(type == REG_BINARY, "type=%d\n", type);
712     ok(dw == 0x12345678, "dw=%d\n", dw);
713     
714     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
715     qw[0] = qw[1] = size = type = 0xdeadbeef;
716     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
717     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
718     ok(size == 8, "size=%d\n", size);
719     ok(type == REG_BINARY, "type=%d\n", type);
720     ok(qw[0] == 0x12345678 && 
721        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
722     
723     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
724     type = dw = 0xdeadbeef; size = 4;
725     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
726     ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
727     ok(dw == 0xdeadbeef, "dw=%d\n", dw);
728     ok(size == 8, "size=%d\n", size);
729
730     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
731     qw[0] = qw[1] = size = type = 0xdeadbeef;
732     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
733     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
734     ok(size == 8, "size=%d\n", size);
735     ok(type == REG_BINARY, "type=%d\n", type);
736     ok(qw[0] == 0x12345678 &&
737        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
738
739     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
740     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
741     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
742     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
743     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
744     ok(type == REG_SZ, "type=%d\n", type);
745     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
746
747     /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
748     type = 0xdeadbeef; size = 0;
749     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
750     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
751     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
752     ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
753        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
754     ok(type == REG_SZ, "type=%d\n", type);
755
756     /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
757     strcpy(buf, sTestpath1);
758     type = 0xdeadbeef;
759     size = sizeof(buf);
760     ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
761     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
762     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
763     ok(size == 0 ||
764        size == 1, /* win2k3 */
765        "size=%d\n", size);
766     ok(type == REG_SZ, "type=%d\n", type);
767     ok(!strcmp(sTestpath1, buf) ||
768        !strcmp(buf, ""),
769        "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
770
771     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
772     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
773     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
774     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
775     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
776     ok(type == REG_SZ, "type=%d\n", type);
777     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
778
779     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
780     size = 0;
781     ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
782     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
783     ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
784        (size == strlen(expanded2)+2) || /* win2k3 SP2 */
785        (size == strlen(sTestpath2)+1),
786         "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
787
788     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
789     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
790     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
791     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
792     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
793     ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
794         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
795     ok(type == REG_SZ, "type=%d\n", type);
796     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
797
798     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
799     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
800     ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
801     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
802     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
803     ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
804         "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
805     ok(type == REG_SZ, "type=%d\n", type);
806     ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
807
808     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
809     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
810     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
811     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
812     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
813     ok(type == REG_EXPAND_SZ, "type=%d\n", type);
814     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
815
816     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
817     size = 0xbadbeef;
818     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
819     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
820     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
821     ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
822        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
823
824     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
825     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
826     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
827
828     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
829     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
830     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
831
832     /* Query REG_EXPAND_SZ using RRF_RT_ANY */
833     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
834     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
835     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
836     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
837     ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
838         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
839     ok(type == REG_SZ, "type=%d\n", type);
840     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
841
842
843 static void test_reg_open_key(void)
844 {
845     DWORD ret = 0;
846     HKEY hkResult = NULL;
847     HKEY hkPreserve = NULL;
848
849     /* successful open */
850     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
851     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
852     ok(hkResult != NULL, "expected hkResult != NULL\n");
853     hkPreserve = hkResult;
854
855     /* these tests fail on Win9x, but we want to be compatible with NT, so
856      * run them if we can */
857     if (!(GetVersion() & 0x80000000))
858     {
859         /* open same key twice */
860         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
861         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
862         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
863         ok(hkResult != NULL, "hkResult != NULL\n");
864         RegCloseKey(hkResult);
865     
866         /* open nonexistent key
867         * check that hkResult is set to NULL
868         */
869         hkResult = hkPreserve;
870         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
871         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
872         ok(hkResult == NULL, "expected hkResult == NULL\n");
873     
874         /* open the same nonexistent key again to make sure the key wasn't created */
875         hkResult = hkPreserve;
876         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
877         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
878         ok(hkResult == NULL, "expected hkResult == NULL\n");
879     
880         /* send in NULL lpSubKey
881         * check that hkResult receives the value of hKey
882         */
883         hkResult = hkPreserve;
884         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
885         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
886         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
887     
888         /* send empty-string in lpSubKey */
889         hkResult = hkPreserve;
890         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
891         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
892         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
893     
894         /* send in NULL lpSubKey and NULL hKey
895         * hkResult is set to NULL
896         */
897         hkResult = hkPreserve;
898         ret = RegOpenKeyA(NULL, NULL, &hkResult);
899         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
900         ok(hkResult == NULL, "expected hkResult == NULL\n");
901     }
902
903     /* only send NULL hKey
904      * the value of hkResult remains unchanged
905      */
906     hkResult = hkPreserve;
907     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
908     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
909        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
910     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
911     RegCloseKey(hkResult);
912
913     /* send in NULL hkResult */
914     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
915     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
916
917     ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, NULL);
918     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
919
920     ret = RegOpenKeyA(NULL, NULL, NULL);
921     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
922
923     /*  beginning backslash character */
924     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
925        ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
926            ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
927            , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
928
929     hkResult = NULL;
930     ret = RegOpenKeyExA(HKEY_CLASSES_ROOT, "\\clsid", 0, KEY_QUERY_VALUE, &hkResult);
931     ok(ret == ERROR_SUCCESS || /* 2k/XP */
932        ret == ERROR_BAD_PATHNAME || /* NT */
933        ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
934        , "expected ERROR_SUCCESS, ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
935     RegCloseKey(hkResult);
936
937     /* WOW64 flags */
938     hkResult = NULL;
939     ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_32KEY, &hkResult);
940     ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
941         "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
942     RegCloseKey(hkResult);
943
944     hkResult = NULL;
945     ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_64KEY, &hkResult);
946     ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
947         "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
948     RegCloseKey(hkResult);
949 }
950
951 static void test_reg_create_key(void)
952 {
953     LONG ret;
954     HKEY hkey1, hkey2;
955     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
956     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
957     /* should succeed: all versions of Windows ignore the access rights
958      * to the parent handle */
959     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
960     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
961
962     /* clean up */
963     RegDeleteKey(hkey2, "");
964     RegDeleteKey(hkey1, "");
965     RegCloseKey(hkey2);
966     RegCloseKey(hkey1);
967
968     /* test creation of volatile keys */
969     ret = RegCreateKeyExA(hkey_main, "Volatile", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey1, NULL);
970     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
971     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
972     ok(ret == ERROR_CHILD_MUST_BE_VOLATILE || broken(!ret), /* win9x */
973        "RegCreateKeyExA failed with error %d\n", ret);
974     if (!ret) RegCloseKey( hkey2 );
975     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
976     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
977     RegCloseKey(hkey2);
978     /* should succeed if the key already exists */
979     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
980     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
981
982     /* clean up */
983     RegDeleteKey(hkey2, "");
984     RegDeleteKey(hkey1, "");
985     RegCloseKey(hkey2);
986     RegCloseKey(hkey1);
987
988     /*  beginning backslash character */
989     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
990     if (!(GetVersion() & 0x80000000))
991         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
992     else {
993         ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
994         RegDeleteKey(hkey1, NULL);
995         RegCloseKey(hkey1);
996     }
997
998     /* WOW64 flags - open an existing key */
999     hkey1 = NULL;
1000     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &hkey1, NULL);
1001     ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1002         "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1003     RegCloseKey(hkey1);
1004
1005     hkey1 = NULL;
1006     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_64KEY, NULL, &hkey1, NULL);
1007     ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1008         "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1009     RegCloseKey(hkey1);
1010 }
1011
1012 static void test_reg_close_key(void)
1013 {
1014     DWORD ret = 0;
1015     HKEY hkHandle;
1016
1017     /* successfully close key
1018      * hkHandle remains changed after call to RegCloseKey
1019      */
1020     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
1021     ret = RegCloseKey(hkHandle);
1022     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1023
1024     /* try to close the key twice */
1025     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
1026     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
1027        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
1028     
1029     /* try to close a NULL handle */
1030     ret = RegCloseKey(NULL);
1031     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1032        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1033
1034     /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1035      * win98 doesn't give a new handle when the same key is opened.
1036      * Not re-opening will make some next tests fail.
1037      */
1038     if (hkey_main == hkHandle)
1039     {
1040         trace("The main handle is most likely closed, so re-opening\n");
1041         RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1042     }
1043 }
1044
1045 static void test_reg_delete_key(void)
1046 {
1047     DWORD ret;
1048
1049     ret = RegDeleteKey(hkey_main, NULL);
1050
1051     /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1052      * there are also no subkeys available it will delete the key pointed to by hkey_main.
1053      * Not re-creating will make some next tests fail.
1054      */
1055     if (ret == ERROR_SUCCESS)
1056     {
1057         trace("We are probably running on NT4 or W2K as the main key is deleted,"
1058             " re-creating the main key\n");
1059         setup_main_key();
1060     }
1061     else
1062         ok(ret == ERROR_INVALID_PARAMETER ||
1063            ret == ERROR_ACCESS_DENIED ||
1064            ret == ERROR_BADKEY, /* Win95 */
1065            "ret=%d\n", ret);
1066 }
1067
1068 static void test_reg_save_key(void)
1069 {
1070     DWORD ret;
1071
1072     ret = RegSaveKey(hkey_main, "saved_key", NULL);
1073     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1074 }
1075
1076 static void test_reg_load_key(void)
1077 {
1078     DWORD ret;
1079     HKEY hkHandle;
1080
1081     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1082     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1083
1084     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1085     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1086
1087     RegCloseKey(hkHandle);
1088 }
1089
1090 static void test_reg_unload_key(void)
1091 {
1092     DWORD ret;
1093
1094     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
1095     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1096
1097     DeleteFile("saved_key");
1098     DeleteFile("saved_key.LOG");
1099 }
1100
1101 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1102 {
1103     TOKEN_PRIVILEGES tp;
1104     HANDLE hToken;
1105     LUID luid;
1106
1107     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1108         return FALSE;
1109
1110     if(!LookupPrivilegeValue(NULL, privilege, &luid))
1111     {
1112         CloseHandle(hToken);
1113         return FALSE;
1114     }
1115
1116     tp.PrivilegeCount = 1;
1117     tp.Privileges[0].Luid = luid;
1118     
1119     if (set)
1120         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1121     else
1122         tp.Privileges[0].Attributes = 0;
1123
1124     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1125     if (GetLastError() != ERROR_SUCCESS)
1126     {
1127         CloseHandle(hToken);
1128         return FALSE;
1129     }
1130
1131     CloseHandle(hToken);
1132     return TRUE;
1133 }
1134
1135 /* tests that show that RegConnectRegistry and 
1136    OpenSCManager accept computer names without the
1137    \\ prefix (what MSDN says).   */
1138 static void test_regconnectregistry( void)
1139 {
1140     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1141     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1142     DWORD len = sizeof(compName) ;
1143     BOOL ret;
1144     LONG retl;
1145     HKEY hkey;
1146     SC_HANDLE schnd;
1147
1148     SetLastError(0xdeadbeef);
1149     ret = GetComputerNameA(compName, &len);
1150     ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1151     if( !ret) return;
1152
1153     lstrcpyA(netwName, "\\\\");
1154     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1155
1156     retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1157     ok( !retl ||
1158         retl == ERROR_DLL_INIT_FAILED ||
1159         retl == ERROR_BAD_NETPATH, /* some win2k */
1160         "RegConnectRegistryA failed err = %d\n", retl);
1161     if( !retl) RegCloseKey( hkey);
1162
1163     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1164     ok( !retl ||
1165         retl == ERROR_DLL_INIT_FAILED ||
1166         retl == ERROR_BAD_NETPATH, /* some win2k */
1167         "RegConnectRegistryA failed err = %d\n", retl);
1168     if( !retl) RegCloseKey( hkey);
1169
1170     SetLastError(0xdeadbeef);
1171     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
1172     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1173     {
1174         win_skip("OpenSCManagerA is not implemented\n");
1175         return;
1176     }
1177
1178     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1179     CloseServiceHandle( schnd);
1180
1181     SetLastError(0xdeadbeef);
1182     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
1183     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1184     CloseServiceHandle( schnd);
1185
1186 }
1187
1188 static void test_reg_query_value(void)
1189 {
1190     HKEY subkey;
1191     CHAR val[MAX_PATH];
1192     WCHAR valW[5];
1193     LONG size, ret;
1194
1195     static const WCHAR expected[] = {'d','a','t','a',0};
1196
1197     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1198     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1199
1200     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1201     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1202
1203     /* try an invalid hkey */
1204     SetLastError(0xdeadbeef);
1205     size = MAX_PATH;
1206     ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1207     ok(ret == ERROR_INVALID_HANDLE ||
1208        ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1209        ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1210        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1211     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1212
1213     /* try a NULL hkey */
1214     SetLastError(0xdeadbeef);
1215     size = MAX_PATH;
1216     ret = RegQueryValueA(NULL, "subkey", val, &size);
1217     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1218        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1219     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1220
1221     /* try a NULL value */
1222     size = MAX_PATH;
1223     ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1224     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1225     ok(size == 5, "Expected 5, got %d\n", size);
1226
1227     /* try a NULL size */
1228     SetLastError(0xdeadbeef);
1229     val[0] = '\0';
1230     ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1231     ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1232     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1233     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1234
1235     /* try a NULL value and size */
1236     ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1237     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1238
1239     /* try a size too small */
1240     SetLastError(0xdeadbeef);
1241     val[0] = '\0';
1242     size = 1;
1243     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1244     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1245     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1246     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1247     ok(size == 5, "Expected 5, got %d\n", size);
1248
1249     /* successfully read the value using 'subkey' */
1250     size = MAX_PATH;
1251     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1252     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1253     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1254     ok(size == 5, "Expected 5, got %d\n", size);
1255
1256     /* successfully read the value using the subkey key */
1257     size = MAX_PATH;
1258     ret = RegQueryValueA(subkey, NULL, val, &size);
1259     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1260     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1261     ok(size == 5, "Expected 5, got %d\n", size);
1262
1263     /* unicode - try size too small */
1264     SetLastError(0xdeadbeef);
1265     valW[0] = '\0';
1266     size = 0;
1267     ret = RegQueryValueW(subkey, NULL, valW, &size);
1268     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1269     {
1270         win_skip("RegQueryValueW is not implemented\n");
1271         goto cleanup;
1272     }
1273     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1274     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1275     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1276     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1277
1278     /* unicode - try size in WCHARS */
1279     SetLastError(0xdeadbeef);
1280     size = sizeof(valW) / sizeof(WCHAR);
1281     ret = RegQueryValueW(subkey, NULL, valW, &size);
1282     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1283     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1284     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1285     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1286
1287     /* unicode - successfully read the value */
1288     size = sizeof(valW);
1289     ret = RegQueryValueW(subkey, NULL, valW, &size);
1290     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1291     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1292     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1293
1294     /* unicode - set the value without a NULL terminator */
1295     ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1296     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1297
1298     /* unicode - read the unterminated value, value is terminated for us */
1299     memset(valW, 'a', sizeof(valW));
1300     size = sizeof(valW);
1301     ret = RegQueryValueW(subkey, NULL, valW, &size);
1302     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1303     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1304     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1305
1306 cleanup:
1307     RegDeleteKeyA(subkey, "");
1308     RegCloseKey(subkey);
1309 }
1310
1311 static void test_string_termination(void)
1312 {
1313     HKEY subkey;
1314     LSTATUS ret;
1315     static const char string[] = "FullString";
1316     char name[11];
1317     BYTE buffer[11];
1318     DWORD insize, outsize, nsize;
1319
1320     ret = RegCreateKeyA(hkey_main, "string_termination", &subkey);
1321     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1322
1323     /* Off-by-one RegSetValueExA -> adds a trailing '\0'! */
1324     insize=sizeof(string)-1;
1325     ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1326     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1327     outsize=insize;
1328     ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1329     ok(ret == ERROR_MORE_DATA, "RegQueryValueExA returned: %d\n", ret);
1330
1331     /* Off-by-two RegSetValueExA -> no trailing '\0', except on Win9x */
1332     insize=sizeof(string)-2;
1333     ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1334     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1335     outsize=0;
1336     ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, NULL, &outsize);
1337     ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1338     ok(outsize == insize || broken(outsize == sizeof(string)) /* Win9x */,
1339        "wrong size %u != %u\n", outsize, insize);
1340
1341     if (outsize == insize)
1342     {
1343         /* RegQueryValueExA may return a string with no trailing '\0' */
1344         outsize=insize;
1345         memset(buffer, 0xbd, sizeof(buffer));
1346         ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1347         ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1348         ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1349         ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1350            wine_debugstr_an((char*)buffer, outsize), outsize, string);
1351         ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1352
1353         /* RegQueryValueExA adds a trailing '\0' if there is room */
1354         outsize=insize+1;
1355         memset(buffer, 0xbd, sizeof(buffer));
1356         ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1357         ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1358         ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1359         ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1360            wine_debugstr_an((char*)buffer, outsize), outsize, string);
1361         ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1362
1363         /* RegEnumValueA may return a string with no trailing '\0' */
1364         outsize=insize;
1365         memset(buffer, 0xbd, sizeof(buffer));
1366         nsize=sizeof(name);
1367         ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1368         ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1369         ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1370         ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1371         ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1372            wine_debugstr_an((char*)buffer, outsize), outsize, string);
1373         ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1374
1375         /* RegEnumValueA adds a trailing '\0' if there is room */
1376         outsize=insize+1;
1377         memset(buffer, 0xbd, sizeof(buffer));
1378         nsize=sizeof(name);
1379         ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1380         ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1381         ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1382         ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1383         ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1384            wine_debugstr_an((char*)buffer, outsize), outsize, string);
1385         ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1386     }
1387
1388     RegDeleteKeyA(subkey, "");
1389     RegCloseKey(subkey);
1390 }
1391
1392 static void test_reg_delete_tree(void)
1393 {
1394     CHAR buffer[MAX_PATH];
1395     HKEY subkey, subkey2;
1396     LONG size, ret;
1397
1398     if(!pRegDeleteTreeA) {
1399         win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1400         return;
1401     }
1402
1403     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1404     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1405     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1406     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1407     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1408     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1409     ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1410     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1411     ret = RegCloseKey(subkey2);
1412     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1413
1414     ret = pRegDeleteTreeA(subkey, "subkey2");
1415     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1416     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1417         "subkey2 was not deleted\n");
1418     size = MAX_PATH;
1419     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1420         "Default value of subkey not longer present\n");
1421
1422     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1423     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1424     ret = RegCloseKey(subkey2);
1425     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1426     ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1427     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1428     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1429         "subkey2 was not deleted\n");
1430     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1431         "Default value of subkey not longer present\n");
1432
1433     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1434     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1435     ret = RegCloseKey(subkey2);
1436     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1437     ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1438     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1439     ret = RegCloseKey(subkey2);
1440     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1441     ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1442     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1443     ret = pRegDeleteTreeA(subkey, NULL);
1444     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1445     ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1446         "subkey was deleted\n");
1447     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1448         "subkey2 was not deleted\n");
1449     ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1450         "subkey3 was not deleted\n");
1451     size = MAX_PATH;
1452     ret = RegQueryValueA(subkey, NULL, buffer, &size);
1453     ok(ret == ERROR_SUCCESS,
1454         "Default value of subkey is not present\n");
1455     ok(!lstrlenA(buffer),
1456         "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1457     size = MAX_PATH;
1458     ok(RegQueryValueA(subkey, "value", buffer, &size),
1459         "Value is still present\n");
1460
1461     ret = pRegDeleteTreeA(hkey_main, "not-here");
1462     ok(ret == ERROR_FILE_NOT_FOUND,
1463         "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1464 }
1465
1466 static void test_rw_order(void)
1467 {
1468     HKEY hKey;
1469     DWORD dw = 0;
1470     static char keyname[] = "test_rw_order";
1471     char value_buf[2];
1472     DWORD values, value_len, value_name_max_len;
1473     LSTATUS ret;
1474
1475     RegDeleteKeyA(HKEY_CURRENT_USER, keyname);
1476     ret = RegCreateKeyA(HKEY_CURRENT_USER, keyname, &hKey);
1477     if(ret != ERROR_SUCCESS) {
1478         skip("Couldn't create key. Skipping.\n");
1479         return;
1480     }
1481
1482     ok(!RegSetValueExA(hKey, "A", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1483        "RegSetValueExA for value \"A\" failed\n");
1484     ok(!RegSetValueExA(hKey, "C", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1485        "RegSetValueExA for value \"C\" failed\n");
1486     ok(!RegSetValueExA(hKey, "D", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1487        "RegSetValueExA for value \"D\" failed\n");
1488     ok(!RegSetValueExA(hKey, "B", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1489        "RegSetValueExA for value \"B\" failed\n");
1490
1491     ok(!RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &values,
1492        &value_name_max_len, NULL, NULL, NULL), "RegQueryInfoKeyA failed\n");
1493     ok(values == 4, "Expected 4 values, got %u\n", values);
1494
1495     /* Value enumeration preserves RegSetValueEx call order */
1496     value_len = 2;
1497     ok(!RegEnumValueA(hKey, 0, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1498     ok(strcmp(value_buf, "A") == 0, "Expected name \"A\", got %s\n", value_buf);
1499     value_len = 2;
1500     ok(!RegEnumValueA(hKey, 1, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1501     todo_wine ok(strcmp(value_buf, "C") == 0, "Expected name \"C\", got %s\n", value_buf);
1502     value_len = 2;
1503     ok(!RegEnumValueA(hKey, 2, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1504     todo_wine ok(strcmp(value_buf, "D") == 0, "Expected name \"D\", got %s\n", value_buf);
1505     value_len = 2;
1506     ok(!RegEnumValueA(hKey, 3, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1507     todo_wine ok(strcmp(value_buf, "B") == 0, "Expected name \"B\", got %s\n", value_buf);
1508
1509     ok(!RegDeleteKey(HKEY_CURRENT_USER, keyname), "Failed to delete key\n");
1510 }
1511
1512 static void test_symlinks(void)
1513 {
1514     static const WCHAR targetW[] = {'\\','S','o','f','t','w','a','r','e','\\','W','i','n','e',
1515                                     '\\','T','e','s','t','\\','t','a','r','g','e','t',0};
1516     BYTE buffer[1024];
1517     UNICODE_STRING target_str;
1518     WCHAR *target;
1519     HKEY key, link;
1520     NTSTATUS status;
1521     DWORD target_len, type, len, dw, err;
1522
1523     if (!pRtlFormatCurrentUserKeyPath || !pNtDeleteKey)
1524     {
1525         win_skip( "Can't perform symlink tests\n" );
1526         return;
1527     }
1528
1529     pRtlFormatCurrentUserKeyPath( &target_str );
1530
1531     target_len = target_str.Length + sizeof(targetW);
1532     target = HeapAlloc( GetProcessHeap(), 0, target_len );
1533     memcpy( target, target_str.Buffer, target_str.Length );
1534     memcpy( target + target_str.Length/sizeof(WCHAR), targetW, sizeof(targetW) );
1535
1536     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1537                            KEY_ALL_ACCESS, NULL, &link, NULL );
1538     ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed: %u\n", err );
1539
1540     /* REG_SZ is not allowed */
1541     err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_SZ, (BYTE *)"foobar", sizeof("foobar") );
1542     ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1543     err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_LINK,
1544                           (BYTE *)target, target_len - sizeof(WCHAR) );
1545     ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1546     /* other values are not allowed */
1547     err = RegSetValueExA( link, "link", 0, REG_LINK, (BYTE *)target, target_len - sizeof(WCHAR) );
1548     ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1549
1550     /* try opening the target through the link */
1551
1552     err = RegOpenKeyA( hkey_main, "link", &key );
1553     ok( err == ERROR_FILE_NOT_FOUND, "RegOpenKey wrong error %u\n", err );
1554
1555     err = RegCreateKeyExA( hkey_main, "target", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1556     ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1557
1558     dw = 0xbeef;
1559     err = RegSetValueExA( key, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1560     ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1561     RegCloseKey( key );
1562
1563     err = RegOpenKeyA( hkey_main, "link", &key );
1564     ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1565
1566     len = sizeof(buffer);
1567     err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1568     ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1569     ok( len == sizeof(DWORD), "wrong len %u\n", len );
1570
1571     len = sizeof(buffer);
1572     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1573     ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1574
1575     /* REG_LINK can be created in non-link keys */
1576     err = RegSetValueExA( key, "SymbolicLinkValue", 0, REG_LINK,
1577                           (BYTE *)target, target_len - sizeof(WCHAR) );
1578     ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1579     len = sizeof(buffer);
1580     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1581     ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1582     ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1583     err = RegDeleteValueA( key, "SymbolicLinkValue" );
1584     ok( err == ERROR_SUCCESS, "RegDeleteValue failed error %u\n", err );
1585
1586     RegCloseKey( key );
1587
1588     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1589     ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1590
1591     len = sizeof(buffer);
1592     err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1593     ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1594     ok( len == sizeof(DWORD), "wrong len %u\n", len );
1595
1596     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1597     ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1598     RegCloseKey( key );
1599
1600     /* now open the symlink itself */
1601
1602     err = RegOpenKeyExA( hkey_main, "link", REG_OPTION_OPEN_LINK, KEY_ALL_ACCESS, &key );
1603     ok( err == ERROR_SUCCESS, "RegOpenKeyEx failed error %u\n", err );
1604     len = sizeof(buffer);
1605     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1606     ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1607     ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1608     RegCloseKey( key );
1609
1610     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_OPEN_LINK,
1611                            KEY_ALL_ACCESS, NULL, &key, NULL );
1612     ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1613     len = sizeof(buffer);
1614     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1615     ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1616     ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1617     RegCloseKey( key );
1618
1619     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1620                            KEY_ALL_ACCESS, NULL, &key, NULL );
1621     ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1622
1623     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK | REG_OPTION_OPEN_LINK,
1624                            KEY_ALL_ACCESS, NULL, &key, NULL );
1625     ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1626
1627     err = RegDeleteKey( hkey_main, "target" );
1628     ok( err == ERROR_SUCCESS, "RegDeleteKey failed error %u\n", err );
1629
1630     err = RegDeleteKey( hkey_main, "link" );
1631     ok( err == ERROR_FILE_NOT_FOUND, "RegDeleteKey wrong error %u\n", err );
1632
1633     status = pNtDeleteKey( link );
1634     ok( !status, "NtDeleteKey failed: 0x%08x\n", status );
1635     RegCloseKey( link );
1636
1637     HeapFree( GetProcessHeap(), 0, target );
1638     pRtlFreeUnicodeString( &target_str );
1639 }
1640
1641 START_TEST(registry)
1642 {
1643     /* Load pointers for functions that are not available in all Windows versions */
1644     InitFunctionPtrs();
1645
1646     setup_main_key();
1647     test_set_value();
1648     create_test_entries();
1649     test_enum_value();
1650     test_query_value_ex();
1651     test_get_value();
1652     test_reg_open_key();
1653     test_reg_create_key();
1654     test_reg_close_key();
1655     test_reg_delete_key();
1656     test_reg_query_value();
1657     test_string_termination();
1658     test_symlinks();
1659
1660     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1661     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1662         set_privileges(SE_RESTORE_NAME, TRUE))
1663     {
1664         test_reg_save_key();
1665         test_reg_load_key();
1666         test_reg_unload_key();
1667
1668         set_privileges(SE_BACKUP_NAME, FALSE);
1669         set_privileges(SE_RESTORE_NAME, FALSE);
1670     }
1671
1672     test_reg_delete_tree();
1673     test_rw_order();
1674
1675     /* cleanup */
1676     delete_key( hkey_main );
1677     
1678     test_regconnectregistry();
1679 }