Release 1.5.29.
[wine] / dlls / advapi32 / tests / registry.c
1 /*
2  * Unit tests for registry functions
3  *
4  * Copyright (c) 2002 Alexandre Julliard
5  * Copyright (c) 2010 AndrĂ© Hentschel
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "winreg.h"
30 #include "winsvc.h"
31 #include "winerror.h"
32 #include "aclapi.h"
33
34 static HKEY hkey_main;
35 static DWORD GLE;
36
37 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
38 static const char * sTestpath2 = "%FOO%\\subdir1";
39 static const DWORD ptr_size = 8 * sizeof(void*);
40
41 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
42 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
43 static DWORD (WINAPI *pRegDeleteKeyExA)(HKEY,LPCSTR,REGSAM,DWORD);
44 static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
45 static NTSTATUS (WINAPI * pNtDeleteKey)(HANDLE);
46 static NTSTATUS (WINAPI * pRtlFormatCurrentUserKeyPath)(UNICODE_STRING*);
47 static NTSTATUS (WINAPI * pRtlFreeUnicodeString)(PUNICODE_STRING);
48
49
50 /* Debugging functions from wine/libs/wine/debug.c */
51
52 /* allocate some tmp string space */
53 /* FIXME: this is not 100% thread-safe */
54 static char *get_temp_buffer( int size )
55 {
56     static char *list[32];
57     static UINT pos;
58     char *ret;
59     UINT idx;
60
61     idx = ++pos % (sizeof(list)/sizeof(list[0]));
62     if (list[idx])
63         ret = HeapReAlloc( GetProcessHeap(), 0, list[idx], size );
64     else
65         ret = HeapAlloc( GetProcessHeap(), 0, size );
66     if (ret) list[idx] = ret;
67     return ret;
68 }
69
70 static const char *wine_debugstr_an( const char *str, int n )
71 {
72     static const char hex[16] = "0123456789abcdef";
73     char *dst, *res;
74     size_t size;
75
76     if (!((ULONG_PTR)str >> 16))
77     {
78         if (!str) return "(null)";
79         res = get_temp_buffer( 6 );
80         sprintf( res, "#%04x", LOWORD(str) );
81         return res;
82     }
83     if (n == -1) n = strlen(str);
84     if (n < 0) n = 0;
85     size = 10 + min( 300, n * 4 );
86     dst = res = get_temp_buffer( size );
87     *dst++ = '"';
88     while (n-- > 0 && dst <= res + size - 9)
89     {
90         unsigned char c = *str++;
91         switch (c)
92         {
93         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
94         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
95         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
96         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
97         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
98         default:
99             if (c >= ' ' && c <= 126)
100                 *dst++ = c;
101             else
102             {
103                 *dst++ = '\\';
104                 *dst++ = 'x';
105                 *dst++ = hex[(c >> 4) & 0x0f];
106                 *dst++ = hex[c & 0x0f];
107             }
108         }
109     }
110     *dst++ = '"';
111     if (n > 0)
112     {
113         *dst++ = '.';
114         *dst++ = '.';
115         *dst++ = '.';
116     }
117     *dst++ = 0;
118     return res;
119 }
120
121 #define ADVAPI32_GET_PROC(func) \
122     p ## func = (void*)GetProcAddress(hadvapi32, #func)
123
124 static void InitFunctionPtrs(void)
125 {
126     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
127     HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
128     HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
129
130     /* This function was introduced with Windows 2003 SP1 */
131     ADVAPI32_GET_PROC(RegGetValueA);
132     ADVAPI32_GET_PROC(RegDeleteTreeA);
133     ADVAPI32_GET_PROC(RegDeleteKeyExA);
134
135     pIsWow64Process = (void *)GetProcAddress( hkernel32, "IsWow64Process" );
136     pRtlFormatCurrentUserKeyPath = (void *)GetProcAddress( hntdll, "RtlFormatCurrentUserKeyPath" );
137     pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
138     pNtDeleteKey = (void *)GetProcAddress( hntdll, "NtDeleteKey" );
139 }
140
141 /* delete key and all its subkeys */
142 static DWORD delete_key( HKEY hkey )
143 {
144     char name[MAX_PATH];
145     DWORD ret;
146
147     while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
148     {
149         HKEY tmp;
150         if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
151         {
152             ret = delete_key( tmp );
153             RegCloseKey( tmp );
154         }
155         if (ret) break;
156     }
157     if (ret != ERROR_NO_MORE_ITEMS) return ret;
158     RegDeleteKeyA( hkey, "" );
159     return 0;
160 }
161
162 static void setup_main_key(void)
163 {
164     if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
165
166     assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
167 }
168
169 #define lok ok_(__FILE__, line)
170 #define test_hkey_main_Value_A(name, string, full_byte_len) _test_hkey_main_Value_A(__LINE__, name, string, full_byte_len)
171 static void _test_hkey_main_Value_A(int line, LPCSTR name, LPCSTR string,
172                                    DWORD full_byte_len)
173 {
174     DWORD ret, type, cbData;
175     DWORD str_byte_len;
176     BYTE* value;
177
178     type=0xdeadbeef;
179     cbData=0xdeadbeef;
180     /* When successful RegQueryValueExA() leaves GLE as is,
181      * so we must reset it to detect unimplemented functions.
182      */
183     SetLastError(0xdeadbeef);
184     ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
185     GLE = GetLastError();
186     lok(ret == ERROR_SUCCESS, "RegQueryValueExA/1 failed: %d, GLE=%d\n", ret, GLE);
187     /* It is wrong for the Ansi version to not be implemented */
188     ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
189     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
190
191     str_byte_len = (string ? lstrlenA(string) : 0) + 1;
192     lok(type == REG_SZ, "RegQueryValueExA/1 returned type %d\n", type);
193     lok(cbData == full_byte_len, "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
194
195     value = HeapAlloc(GetProcessHeap(), 0, cbData+1);
196     memset(value, 0xbd, cbData+1);
197     type=0xdeadbeef;
198     ret = RegQueryValueExA(hkey_main, name, NULL, &type, value, &cbData);
199     GLE = GetLastError();
200     lok(ret == ERROR_SUCCESS, "RegQueryValueExA/2 failed: %d, GLE=%d\n", ret, GLE);
201     if (!string)
202     {
203         /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
204         lok(*value == 0xbd, "RegQueryValueExA overflowed: cbData=%u *value=%02x\n", cbData, *value);
205     }
206     else
207     {
208         lok(memcmp(value, string, cbData) == 0, "RegQueryValueExA/2 failed: %s/%d != %s/%d\n",
209            wine_debugstr_an((char*)value, cbData), cbData,
210            wine_debugstr_an(string, full_byte_len), full_byte_len);
211         lok(*(value+cbData) == 0xbd, "RegQueryValueExA/2 overflowed at offset %u: %02x != bd\n", cbData, *(value+cbData));
212     }
213     HeapFree(GetProcessHeap(), 0, value);
214 }
215
216 #define test_hkey_main_Value_W(name, string, full_byte_len) _test_hkey_main_Value_W(__LINE__, name, string, full_byte_len)
217 static void _test_hkey_main_Value_W(int line, LPCWSTR name, LPCWSTR string,
218                                    DWORD full_byte_len)
219 {
220     DWORD ret, type, cbData;
221     BYTE* value;
222
223     type=0xdeadbeef;
224     cbData=0xdeadbeef;
225     /* When successful RegQueryValueExW() leaves GLE as is,
226      * so we must reset it to detect unimplemented functions.
227      */
228     SetLastError(0xdeadbeef);
229     ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
230     GLE = GetLastError();
231     lok(ret == ERROR_SUCCESS, "RegQueryValueExW/1 failed: %d, GLE=%d\n", ret, GLE);
232     if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
233     {
234         win_skip("RegQueryValueExW() is not implemented\n");
235         return;
236     }
237
238     lok(type == REG_SZ, "RegQueryValueExW/1 returned type %d\n", type);
239     lok(cbData == full_byte_len,
240         "cbData=%d instead of %d\n", cbData, full_byte_len);
241
242     /* Give enough space to overflow by one WCHAR */
243     value = HeapAlloc(GetProcessHeap(), 0, cbData+2);
244     memset(value, 0xbd, cbData+2);
245     type=0xdeadbeef;
246     ret = RegQueryValueExW(hkey_main, name, NULL, &type, value, &cbData);
247     GLE = GetLastError();
248     lok(ret == ERROR_SUCCESS, "RegQueryValueExW/2 failed: %d, GLE=%d\n", ret, GLE);
249     if (string)
250     {
251         lok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
252            wine_dbgstr_wn((WCHAR*)value, cbData / sizeof(WCHAR)), cbData,
253            wine_dbgstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
254     }
255     /* This implies that when cbData == 0, RegQueryValueExW() should not modify the buffer */
256     lok(*(value+cbData) == 0xbd, "RegQueryValueExW/2 overflowed at %u: %02x != bd\n", cbData, *(value+cbData));
257     lok(*(value+cbData+1) == 0xbd, "RegQueryValueExW/2 overflowed at %u+1: %02x != bd\n", cbData, *(value+cbData+1));
258     HeapFree(GetProcessHeap(), 0, value);
259 }
260
261 static void test_set_value(void)
262 {
263     DWORD ret;
264
265     static const WCHAR name1W[] =   {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
266     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};
267     static const WCHAR emptyW[] = {0};
268     static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
269     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};
270     static const WCHAR substring2W[] = {'T','h','i','s',0};
271
272     static const char name1A[] =   "CleanSingleString";
273     static const char name2A[] =   "SomeIntraZeroedString";
274     static const char emptyA[] = "";
275     static const char string1A[] = "ThisNeverBreaks";
276     static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
277     static const char substring2A[] = "This";
278
279     if (0)
280     {
281         /* Crashes on NT4, Windows 2000 and XP SP1 */
282         ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
283         ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
284     }
285
286     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
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     /* RegSetValueA ignores the size passed in */
292     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
293     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
294     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
295     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
296
297     /* stops at first null */
298     ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
299     ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
300     test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
301     test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
302
303     /* only REG_SZ is supported on NT*/
304     ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
305     ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\n", ret);
306
307     ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
308     ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\n", ret);
309
310     ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
311     ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\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     if (0)
346     {
347         /* Crashes on NT4, Windows 2000 and XP SP1 */
348         ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
349         ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
350
351         RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)1, 1);
352         RegSetValueExA(hkey_main, name2A, 0, REG_DWORD, (const BYTE *)1, 1);
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     ret = RegSetValueW(hkey_main, name1W, REG_SZ, string1W, sizeof(string1W));
361     ok(ret == ERROR_SUCCESS, "RegSetValueW 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     /* RegSetValueW ignores the size passed in */
366     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
367     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
368     test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
369     test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
370
371     /* stops at first null */
372     ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
373     ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
374     test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
375     test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
376
377     /* only REG_SZ is supported */
378     ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
379     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
380     ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
381     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
382     ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
383     ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
384
385     /* test RegSetValueExW with off-by-one size */
386     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
387     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
388     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
389     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
390
391     /* test RegSetValueExW with normal string */
392     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
393     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
394     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
395     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
396
397     /* test RegSetValueExW with intrazeroed string */
398     ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
399     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
400     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
401     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
402
403     /* test RegSetValueExW with data = 1 */
404     ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)1, 1);
405     ok(ret == ERROR_NOACCESS, "RegSetValueExW should have failed with ERROR_NOACCESS: %d, GLE=%d\n", ret, GetLastError());
406     ret = RegSetValueExW(hkey_main, name2W, 0, REG_DWORD, (const BYTE *)1, 1);
407     ok(ret == ERROR_NOACCESS, "RegSetValueExW should have failed with ERROR_NOACCESS: %d, GLE=%d\n", ret, GetLastError());
408 }
409
410 static void create_test_entries(void)
411 {
412     static const DWORD qw[2] = { 0x12345678, 0x87654321 };
413
414     SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
415     SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
416
417     ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
418         "RegSetValueExA failed\n");
419     ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
420         "RegSetValueExA failed\n");
421     ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
422        "RegSetValueExA failed\n");
423     ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1), 
424         "RegSetValueExA failed\n");
425     ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
426         "RegSetValueExA failed\n");
427     ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
428         "RegSetValueExA failed\n");
429     ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
430         "RegSetValueExA failed\n");
431 }
432         
433 static void test_enum_value(void)
434 {
435     DWORD res;
436     HKEY test_key;
437     char value[20], data[20];
438     WCHAR valueW[20], dataW[20];
439     DWORD val_count, data_count, type;
440     static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
441     static const WCHAR testW[] = {'T','e','s','t',0};
442     static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
443
444     /* create the working key for new 'Test' value */
445     res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
446     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
447
448     /* check NULL data with zero length */
449     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
450     if (GetVersion() & 0x80000000)
451         ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
452     else
453         ok( !res, "RegSetValueExA returned %d\n", res );
454     res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
455     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
456     res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
457     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
458
459     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
460     ok( res == 0, "RegSetValueExA failed error %d\n", res );
461
462     /* overflow both name and data */
463     val_count = 2;
464     data_count = 2;
465     type = 1234;
466     strcpy( value, "xxxxxxxxxx" );
467     strcpy( data, "xxxxxxxxxx" );
468     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
469     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
470     ok( val_count == 2, "val_count set to %d\n", val_count );
471     ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
472     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
473     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
474     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
475
476     /* overflow name */
477     val_count = 3;
478     data_count = 20;
479     type = 1234;
480     strcpy( value, "xxxxxxxxxx" );
481     strcpy( data, "xxxxxxxxxx" );
482     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
483     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
484     ok( val_count == 3, "val_count set to %d\n", val_count );
485     ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
486     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
487     /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
488     ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ), 
489         "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
490     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ) || broken( !strcmp( data, "xxxxxxxx" ) && data_count == 8 ),
491         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
492
493     /* overflow empty name */
494     val_count = 0;
495     data_count = 20;
496     type = 1234;
497     strcpy( value, "xxxxxxxxxx" );
498     strcpy( data, "xxxxxxxxxx" );
499     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
500     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
501     ok( val_count == 0, "val_count set to %d\n", val_count );
502     ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
503     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
504     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
505     /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
506     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ) || broken( !strcmp( data, "xxxxxxxx" ) && data_count == 8 ),
507         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
508
509     /* overflow data */
510     val_count = 20;
511     data_count = 2;
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 == 20, "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     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
522
523     /* no overflow */
524     val_count = 20;
525     data_count = 20;
526     type = 1234;
527     strcpy( value, "xxxxxxxxxx" );
528     strcpy( data, "xxxxxxxxxx" );
529     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
530     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
531     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
532     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
533     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
534     ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
535     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
536
537     /* Unicode tests */
538
539     SetLastError(0xdeadbeef);
540     res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
541     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
542     {
543         win_skip("RegSetValueExW is not implemented\n");
544         goto cleanup;
545     }
546     ok( res == 0, "RegSetValueExW failed error %d\n", res );
547
548     /* overflow both name and data */
549     val_count = 2;
550     data_count = 2;
551     type = 1234;
552     memcpy( valueW, xxxW, sizeof(xxxW) );
553     memcpy( dataW, xxxW, sizeof(xxxW) );
554     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
555     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
556     ok( val_count == 2, "val_count set to %d\n", val_count );
557     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
558     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
559     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
560     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
561
562     /* overflow name */
563     val_count = 3;
564     data_count = 20;
565     type = 1234;
566     memcpy( valueW, xxxW, sizeof(xxxW) );
567     memcpy( dataW, xxxW, sizeof(xxxW) );
568     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
569     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
570     ok( val_count == 3, "val_count set to %d\n", val_count );
571     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
572     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
573     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
574     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
575
576     /* overflow data */
577     val_count = 20;
578     data_count = 2;
579     type = 1234;
580     memcpy( valueW, xxxW, sizeof(xxxW) );
581     memcpy( dataW, xxxW, sizeof(xxxW) );
582     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
583     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
584     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
585     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
586     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
587     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
588     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
589
590     /* no overflow */
591     val_count = 20;
592     data_count = 20;
593     type = 1234;
594     memcpy( valueW, xxxW, sizeof(xxxW) );
595     memcpy( dataW, xxxW, sizeof(xxxW) );
596     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
597     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
598     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
599     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
600     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
601     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
602     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
603
604 cleanup:
605     RegDeleteKeyA(test_key, "");
606     RegCloseKey(test_key);
607 }
608
609 static void test_query_value_ex(void)
610 {
611     DWORD ret;
612     DWORD size;
613     DWORD type;
614     BYTE buffer[10];
615     
616     ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
617     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
618     ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
619     ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
620
621     type = 0xdeadbeef;
622     size = 0xdeadbeef;
623     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
624     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
625     ok(size == 0, "size should have been set to 0 instead of %d\n", size);
626
627     size = sizeof(buffer);
628     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
629     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
630     ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
631
632     size = 4;
633     ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
634     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
635 }
636
637 static void test_get_value(void)
638 {
639     DWORD ret;
640     DWORD size;
641     DWORD type;
642     DWORD dw, qw[2];
643     CHAR buf[80];
644     CHAR expanded[] = "bar\\subdir1";
645     CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
646    
647     if(!pRegGetValueA)
648     {
649         win_skip("RegGetValue not available on this platform\n");
650         return;
651     }
652
653     /* Invalid parameter */
654     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
655     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
656
657     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
658     size = type = dw = 0xdeadbeef;
659     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
660     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
661     ok(size == 4, "size=%d\n", size);
662     ok(type == REG_DWORD, "type=%d\n", type);
663     ok(dw == 0x12345678, "dw=%d\n", dw);
664
665     /* Query by subkey-name */
666     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
667     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
668
669     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
670     size = type = dw = 0xdeadbeef;
671     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
672     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
673     /* Although the function failed all values are retrieved */
674     ok(size == 4, "size=%d\n", size);
675     ok(type == REG_DWORD, "type=%d\n", type);
676     ok(dw == 0x12345678, "dw=%d\n", dw);
677
678     /* Test RRF_ZEROONFAILURE */
679     type = dw = 0xdeadbeef; size = 4;
680     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
681     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
682     /* Again all values are retrieved ... */
683     ok(size == 4, "size=%d\n", size);
684     ok(type == REG_DWORD, "type=%d\n", type);
685     /* ... except the buffer, which is zeroed out */
686     ok(dw == 0, "dw=%d\n", dw);
687
688     /* Test RRF_ZEROONFAILURE with a NULL buffer... */
689     type = size = 0xbadbeef;
690     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
691     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
692     ok(size == 4, "size=%d\n", size);
693     ok(type == REG_DWORD, "type=%d\n", type);
694
695     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
696     size = type = dw = 0xdeadbeef;
697     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
698     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
699     ok(size == 4, "size=%d\n", size);
700     ok(type == REG_DWORD, "type=%d\n", type);
701     ok(dw == 0x12345678, "dw=%d\n", dw);
702
703     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
704     size = type = dw = 0xdeadbeef;
705     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
706     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
707     ok(size == 4, "size=%d\n", size);
708     ok(type == REG_BINARY, "type=%d\n", type);
709     ok(dw == 0x12345678, "dw=%d\n", dw);
710     
711     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
712     qw[0] = qw[1] = size = type = 0xdeadbeef;
713     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
714     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
715     ok(size == 8, "size=%d\n", size);
716     ok(type == REG_BINARY, "type=%d\n", type);
717     ok(qw[0] == 0x12345678 && 
718        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
719     
720     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
721     type = dw = 0xdeadbeef; size = 4;
722     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
723     ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
724     ok(dw == 0xdeadbeef, "dw=%d\n", dw);
725     ok(size == 8, "size=%d\n", size);
726
727     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
728     qw[0] = qw[1] = size = type = 0xdeadbeef;
729     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
730     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
731     ok(size == 8, "size=%d\n", size);
732     ok(type == REG_BINARY, "type=%d\n", type);
733     ok(qw[0] == 0x12345678 &&
734        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
735
736     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
737     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
738     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
739     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
740     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
741     ok(type == REG_SZ, "type=%d\n", type);
742     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
743
744     /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
745     type = 0xdeadbeef; size = 0;
746     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
747     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
748     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
749     ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
750        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
751     ok(type == REG_SZ, "type=%d\n", type);
752
753     /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
754     strcpy(buf, sTestpath1);
755     type = 0xdeadbeef;
756     size = sizeof(buf);
757     ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
758     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
759     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
760     ok(size == 0 ||
761        size == 1, /* win2k3 */
762        "size=%d\n", size);
763     ok(type == REG_SZ, "type=%d\n", type);
764     ok(!strcmp(sTestpath1, buf) ||
765        !strcmp(buf, ""),
766        "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
767
768     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
769     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
770     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
771     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
772     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
773     ok(type == REG_SZ, "type=%d\n", type);
774     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
775
776     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
777     size = 0;
778     ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
779     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
780     ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
781        (size == strlen(expanded2)+2) || /* win2k3 SP2 */
782        (size == strlen(sTestpath2)+1),
783         "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
784
785     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
786     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
787     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
788     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
789     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
790     ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
791         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
792     ok(type == REG_SZ, "type=%d\n", type);
793     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
794
795     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
796     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
797     ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
798     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
799     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
800     ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
801         "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
802     ok(type == REG_SZ, "type=%d\n", type);
803     ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
804
805     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
806     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
807     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
808     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
809     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
810     ok(type == REG_EXPAND_SZ, "type=%d\n", type);
811     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
812
813     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
814     size = 0xbadbeef;
815     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
816     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
817     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
818     ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
819        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
820
821     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
822     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
823     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
824
825     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
826     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
827     /* before win8: ERROR_INVALID_PARAMETER, win8: ERROR_UNSUPPORTED_TYPE */
828     ok(ret == ERROR_INVALID_PARAMETER || ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
829
830     /* Query REG_EXPAND_SZ using RRF_RT_ANY */
831     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
832     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
833     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
834     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
835     ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
836         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
837     ok(type == REG_SZ, "type=%d\n", type);
838     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
839
840
841 static void test_reg_open_key(void)
842 {
843     DWORD ret = 0;
844     HKEY hkResult = NULL;
845     HKEY hkPreserve = NULL;
846     HKEY hkRoot64 = NULL;
847     HKEY hkRoot32 = NULL;
848     BOOL bRet;
849     SID_IDENTIFIER_AUTHORITY sid_authority = {SECURITY_WORLD_SID_AUTHORITY};
850     PSID world_sid;
851     EXPLICIT_ACCESSA access;
852     PACL key_acl;
853     SECURITY_DESCRIPTOR *sd;
854
855     /* successful open */
856     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
857     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
858     ok(hkResult != NULL, "expected hkResult != NULL\n");
859     hkPreserve = hkResult;
860
861     /* open same key twice */
862     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
863     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
864     ok(hkResult != hkPreserve, "expected hkResult != hkPreserve\n");
865     ok(hkResult != NULL, "hkResult != NULL\n");
866     RegCloseKey(hkResult);
867
868     /* open nonexistent key
869     * check that hkResult is set to NULL
870     */
871     hkResult = hkPreserve;
872     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
873     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
874     ok(hkResult == NULL, "expected hkResult == NULL\n");
875
876     /* open the same nonexistent key again to make sure the key wasn't created */
877     hkResult = hkPreserve;
878     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
879     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
880     ok(hkResult == NULL, "expected hkResult == NULL\n");
881
882     /* send in NULL lpSubKey
883     * check that hkResult receives the value of hKey
884     */
885     hkResult = hkPreserve;
886     ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
887     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
888     ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
889
890     /* send empty-string in lpSubKey */
891     hkResult = hkPreserve;
892     ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
893     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
894     ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
895
896     /* send in NULL lpSubKey and NULL hKey
897     * hkResult is set to NULL
898     */
899     hkResult = hkPreserve;
900     ret = RegOpenKeyA(NULL, NULL, &hkResult);
901     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
902     ok(hkResult == NULL, "expected hkResult == NULL\n");
903
904     /* only send NULL hKey
905      * the value of hkResult remains unchanged
906      */
907     hkResult = hkPreserve;
908     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
909     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
910        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
911     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
912     RegCloseKey(hkResult);
913
914     /* send in NULL hkResult */
915     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
916     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
917
918     ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, NULL);
919     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
920
921     ret = RegOpenKeyA(NULL, NULL, NULL);
922     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
923
924     /*  beginning backslash character */
925     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
926     ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
927        broken(ret == ERROR_SUCCESS),  /* wow64 */
928        "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
929     if (!ret) RegCloseKey(hkResult);
930
931     hkResult = NULL;
932     ret = RegOpenKeyExA(HKEY_CLASSES_ROOT, "\\clsid", 0, KEY_QUERY_VALUE, &hkResult);
933     ok(ret == ERROR_SUCCESS || /* 2k/XP */
934        ret == ERROR_BAD_PATHNAME, /* NT */
935        "expected ERROR_SUCCESS, ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
936     RegCloseKey(hkResult);
937
938     /* WOW64 flags */
939     hkResult = NULL;
940     ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_32KEY, &hkResult);
941     ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
942         "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
943     RegCloseKey(hkResult);
944
945     hkResult = NULL;
946     ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_64KEY, &hkResult);
947     ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
948         "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
949     RegCloseKey(hkResult);
950
951     /* check special HKEYs on 64bit
952      * only the lower 4 bytes of the supplied key are used
953      */
954     if (ptr_size == 64)
955     {
956         /* HKEY_CURRENT_USER */
957         ret = RegOpenKeyA(UlongToHandle(HandleToUlong(HKEY_CURRENT_USER)), "Software", &hkResult);
958         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
959         ok(hkResult != NULL, "expected hkResult != NULL\n");
960         RegCloseKey(hkResult);
961
962         ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)1 << 32), "Software", &hkResult);
963         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
964         ok(hkResult != NULL, "expected hkResult != NULL\n");
965         RegCloseKey(hkResult);
966
967         ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)0xdeadbeef << 32), "Software", &hkResult);
968         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
969         ok(hkResult != NULL, "expected hkResult != NULL\n");
970         RegCloseKey(hkResult);
971
972         ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)0xffffffff << 32), "Software", &hkResult);
973         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
974         ok(hkResult != NULL, "expected hkResult != NULL\n");
975         RegCloseKey(hkResult);
976
977         /* HKEY_LOCAL_MACHINE */
978         ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_LOCAL_MACHINE) | (ULONG64)0xdeadbeef << 32), "Software", &hkResult);
979         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
980         ok(hkResult != NULL, "expected hkResult != NULL\n");
981         RegCloseKey(hkResult);
982     }
983
984     /* Try using WOW64 flags when opening a key with a DACL set to verify that
985      * the registry access check is performed correctly. Redirection isn't
986      * being tested, so the tests don't care about whether the process is
987      * running under WOW64. */
988     if (!pIsWow64Process)
989     {
990         win_skip("WOW64 flags are not recognized\n");
991         return;
992     }
993
994     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
995                           KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &hkRoot32, NULL);
996     ok(ret == ERROR_SUCCESS && hkRoot32 != NULL,
997        "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
998
999     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1000                           KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &hkRoot64, NULL);
1001     ok(ret == ERROR_SUCCESS && hkRoot64 != NULL,
1002        "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1003
1004     bRet = AllocateAndInitializeSid(&sid_authority, 1, SECURITY_WORLD_RID,
1005                                     0, 0, 0, 0, 0, 0, 0, &world_sid);
1006     ok(bRet == TRUE,
1007        "Expected AllocateAndInitializeSid to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1008
1009     access.grfAccessPermissions = GENERIC_ALL | STANDARD_RIGHTS_ALL;
1010     access.grfAccessMode = SET_ACCESS;
1011     access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
1012     access.Trustee.pMultipleTrustee = NULL;
1013     access.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
1014     access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
1015     access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
1016     access.Trustee.ptstrName = (char *)world_sid;
1017
1018     ret = SetEntriesInAclA(1, &access, NULL, &key_acl);
1019     ok(ret == ERROR_SUCCESS,
1020        "Expected SetEntriesInAclA to return ERROR_SUCCESS, got %u, last error %u\n", ret, GetLastError());
1021
1022     sd = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1023     bRet = InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION);
1024     ok(bRet == TRUE,
1025        "Expected InitializeSecurityDescriptor to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1026
1027     bRet = SetSecurityDescriptorDacl(sd, TRUE, key_acl, FALSE);
1028     ok(bRet == TRUE,
1029        "Expected SetSecurityDescriptorDacl to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1030
1031     /* The "sanctioned" methods of setting a registry ACL aren't implemented in Wine. */
1032     bRet = SetKernelObjectSecurity(hkRoot64, DACL_SECURITY_INFORMATION, sd);
1033     ok(bRet == TRUE,
1034        "Expected SetKernelObjectSecurity to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1035
1036     bRet = SetKernelObjectSecurity(hkRoot32, DACL_SECURITY_INFORMATION, sd);
1037     ok(bRet == TRUE,
1038        "Expected SetKernelObjectSecurity to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1039
1040     hkResult = NULL;
1041     ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, KEY_WOW64_64KEY | KEY_READ, &hkResult);
1042     ok(ret == ERROR_SUCCESS && hkResult != NULL,
1043        "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1044     RegCloseKey(hkResult);
1045
1046     hkResult = NULL;
1047     ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, KEY_WOW64_32KEY | KEY_READ, &hkResult);
1048     ok(ret == ERROR_SUCCESS && hkResult != NULL,
1049        "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1050     RegCloseKey(hkResult);
1051
1052     HeapFree(GetProcessHeap(), 0, sd);
1053     LocalFree(key_acl);
1054     FreeSid(world_sid);
1055     RegDeleteKeyA(hkRoot64, "");
1056     RegCloseKey(hkRoot64);
1057     RegDeleteKeyA(hkRoot32, "");
1058     RegCloseKey(hkRoot32);
1059 }
1060
1061 static void test_reg_create_key(void)
1062 {
1063     LONG ret;
1064     HKEY hkey1, hkey2;
1065     HKEY hkRoot64 = NULL;
1066     HKEY hkRoot32 = NULL;
1067     DWORD dwRet;
1068     BOOL bRet;
1069     SID_IDENTIFIER_AUTHORITY sid_authority = {SECURITY_WORLD_SID_AUTHORITY};
1070     PSID world_sid;
1071     EXPLICIT_ACCESSA access;
1072     PACL key_acl;
1073     SECURITY_DESCRIPTOR *sd;
1074
1075     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
1076     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1077     /* should succeed: all versions of Windows ignore the access rights
1078      * to the parent handle */
1079     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
1080     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1081
1082     /* clean up */
1083     RegDeleteKey(hkey2, "");
1084     RegDeleteKey(hkey1, "");
1085     RegCloseKey(hkey2);
1086     RegCloseKey(hkey1);
1087
1088     /* test creation of volatile keys */
1089     ret = RegCreateKeyExA(hkey_main, "Volatile", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey1, NULL);
1090     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1091     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1092     ok(ret == ERROR_CHILD_MUST_BE_VOLATILE, "RegCreateKeyExA failed with error %d\n", ret);
1093     if (!ret) RegCloseKey( hkey2 );
1094     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1095     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1096     RegCloseKey(hkey2);
1097     /* should succeed if the key already exists */
1098     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1099     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1100
1101     /* clean up */
1102     RegDeleteKey(hkey2, "");
1103     RegDeleteKey(hkey1, "");
1104     RegCloseKey(hkey2);
1105     RegCloseKey(hkey1);
1106
1107     /*  beginning backslash character */
1108     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
1109     if (!(GetVersion() & 0x80000000))
1110         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
1111     else {
1112         ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1113         RegDeleteKey(hkey1, NULL);
1114         RegCloseKey(hkey1);
1115     }
1116
1117     /* WOW64 flags - open an existing key */
1118     hkey1 = NULL;
1119     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &hkey1, NULL);
1120     ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1121         "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1122     RegCloseKey(hkey1);
1123
1124     hkey1 = NULL;
1125     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_64KEY, NULL, &hkey1, NULL);
1126     ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1127         "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1128     RegCloseKey(hkey1);
1129
1130     /* Try using WOW64 flags when opening a key with a DACL set to verify that
1131      * the registry access check is performed correctly. Redirection isn't
1132      * being tested, so the tests don't care about whether the process is
1133      * running under WOW64. */
1134     if (!pIsWow64Process)
1135     {
1136         win_skip("WOW64 flags are not recognized\n");
1137         return;
1138     }
1139
1140     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1141                           KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &hkRoot32, NULL);
1142     ok(ret == ERROR_SUCCESS && hkRoot32 != NULL,
1143        "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%d)\n", ret);
1144
1145     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1146                           KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &hkRoot64, NULL);
1147     ok(ret == ERROR_SUCCESS && hkRoot64 != NULL,
1148        "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%d)\n", ret);
1149
1150     bRet = AllocateAndInitializeSid(&sid_authority, 1, SECURITY_WORLD_RID,
1151                                     0, 0, 0, 0, 0, 0, 0, &world_sid);
1152     ok(bRet == TRUE,
1153        "Expected AllocateAndInitializeSid to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1154
1155     access.grfAccessPermissions = GENERIC_ALL | STANDARD_RIGHTS_ALL;
1156     access.grfAccessMode = SET_ACCESS;
1157     access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
1158     access.Trustee.pMultipleTrustee = NULL;
1159     access.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
1160     access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
1161     access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
1162     access.Trustee.ptstrName = (char *)world_sid;
1163
1164     dwRet = SetEntriesInAclA(1, &access, NULL, &key_acl);
1165     ok(ret == ERROR_SUCCESS,
1166        "Expected SetEntriesInAclA to return ERROR_SUCCESS, got %u, last error %u\n", dwRet, GetLastError());
1167
1168     sd = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1169     bRet = InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION);
1170     ok(bRet == TRUE,
1171        "Expected InitializeSecurityDescriptor to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1172
1173     bRet = SetSecurityDescriptorDacl(sd, TRUE, key_acl, FALSE);
1174     ok(bRet == TRUE,
1175        "Expected SetSecurityDescriptorDacl to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1176
1177     /* The "sanctioned" methods of setting a registry ACL aren't implemented in Wine. */
1178     bRet = SetKernelObjectSecurity(hkRoot64, DACL_SECURITY_INFORMATION, sd);
1179     ok(bRet == TRUE,
1180        "Expected SetKernelObjectSecurity to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1181
1182     bRet = SetKernelObjectSecurity(hkRoot32, DACL_SECURITY_INFORMATION, sd);
1183     ok(bRet == TRUE,
1184        "Expected SetKernelObjectSecurity to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1185
1186     hkey1 = NULL;
1187     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1188                           KEY_WOW64_64KEY | KEY_READ, NULL, &hkey1, NULL);
1189     ok(ret == ERROR_SUCCESS && hkey1 != NULL,
1190        "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1191     RegCloseKey(hkey1);
1192
1193     hkey1 = NULL;
1194     ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1195                           KEY_WOW64_32KEY | KEY_READ, NULL, &hkey1, NULL);
1196     ok(ret == ERROR_SUCCESS && hkey1 != NULL,
1197        "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1198     RegCloseKey(hkey1);
1199
1200     HeapFree(GetProcessHeap(), 0, sd);
1201     LocalFree(key_acl);
1202     FreeSid(world_sid);
1203     RegDeleteKeyA(hkRoot64, "");
1204     RegCloseKey(hkRoot64);
1205     RegDeleteKeyA(hkRoot32, "");
1206     RegCloseKey(hkRoot32);
1207 }
1208
1209 static void test_reg_close_key(void)
1210 {
1211     DWORD ret = 0;
1212     HKEY hkHandle;
1213
1214     /* successfully close key
1215      * hkHandle remains changed after call to RegCloseKey
1216      */
1217     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
1218     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1219     ret = RegCloseKey(hkHandle);
1220     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1221
1222     /* try to close the key twice */
1223     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
1224     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
1225        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
1226     
1227     /* try to close a NULL handle */
1228     ret = RegCloseKey(NULL);
1229     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1230        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1231
1232     /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1233      * win98 doesn't give a new handle when the same key is opened.
1234      * Not re-opening will make some next tests fail.
1235      */
1236     if (hkey_main == hkHandle)
1237     {
1238         trace("The main handle is most likely closed, so re-opening\n");
1239         RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1240     }
1241 }
1242
1243 static void test_reg_delete_key(void)
1244 {
1245     DWORD ret;
1246
1247     ret = RegDeleteKey(hkey_main, NULL);
1248
1249     /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1250      * there are also no subkeys available it will delete the key pointed to by hkey_main.
1251      * Not re-creating will make some next tests fail.
1252      */
1253     if (ret == ERROR_SUCCESS)
1254     {
1255         trace("We are probably running on NT4 or W2K as the main key is deleted,"
1256             " re-creating the main key\n");
1257         setup_main_key();
1258     }
1259     else
1260         ok(ret == ERROR_INVALID_PARAMETER ||
1261            ret == ERROR_ACCESS_DENIED ||
1262            ret == ERROR_BADKEY, /* Win95 */
1263            "ret=%d\n", ret);
1264 }
1265
1266 static void test_reg_save_key(void)
1267 {
1268     DWORD ret;
1269
1270     ret = RegSaveKey(hkey_main, "saved_key", NULL);
1271     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1272 }
1273
1274 static void test_reg_load_key(void)
1275 {
1276     DWORD ret;
1277     HKEY hkHandle;
1278
1279     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1280     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1281
1282     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1283     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1284
1285     RegCloseKey(hkHandle);
1286 }
1287
1288 static void test_reg_unload_key(void)
1289 {
1290     DWORD ret;
1291
1292     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
1293     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1294
1295     DeleteFile("saved_key");
1296     DeleteFile("saved_key.LOG");
1297 }
1298
1299 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1300 {
1301     TOKEN_PRIVILEGES tp;
1302     HANDLE hToken;
1303     LUID luid;
1304
1305     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1306         return FALSE;
1307
1308     if(!LookupPrivilegeValue(NULL, privilege, &luid))
1309     {
1310         CloseHandle(hToken);
1311         return FALSE;
1312     }
1313
1314     tp.PrivilegeCount = 1;
1315     tp.Privileges[0].Luid = luid;
1316     
1317     if (set)
1318         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1319     else
1320         tp.Privileges[0].Attributes = 0;
1321
1322     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1323     if (GetLastError() != ERROR_SUCCESS)
1324     {
1325         CloseHandle(hToken);
1326         return FALSE;
1327     }
1328
1329     CloseHandle(hToken);
1330     return TRUE;
1331 }
1332
1333 /* tests that show that RegConnectRegistry and 
1334    OpenSCManager accept computer names without the
1335    \\ prefix (what MSDN says).   */
1336 static void test_regconnectregistry( void)
1337 {
1338     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1339     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1340     DWORD len = sizeof(compName) ;
1341     BOOL ret;
1342     LONG retl;
1343     HKEY hkey;
1344     SC_HANDLE schnd;
1345
1346     SetLastError(0xdeadbeef);
1347     ret = GetComputerNameA(compName, &len);
1348     ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1349     if( !ret) return;
1350
1351     lstrcpyA(netwName, "\\\\");
1352     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1353
1354     retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1355     ok( !retl ||
1356         retl == ERROR_DLL_INIT_FAILED ||
1357         retl == ERROR_BAD_NETPATH, /* some win2k */
1358         "RegConnectRegistryA failed err = %d\n", retl);
1359     if( !retl) RegCloseKey( hkey);
1360
1361     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1362     ok( !retl ||
1363         retl == ERROR_DLL_INIT_FAILED ||
1364         retl == ERROR_BAD_NETPATH, /* some win2k */
1365         "RegConnectRegistryA failed err = %d\n", retl);
1366     if( !retl) RegCloseKey( hkey);
1367
1368     SetLastError(0xdeadbeef);
1369     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
1370     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1371     {
1372         win_skip("OpenSCManagerA is not implemented\n");
1373         return;
1374     }
1375
1376     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1377     CloseServiceHandle( schnd);
1378
1379     SetLastError(0xdeadbeef);
1380     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
1381     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1382     CloseServiceHandle( schnd);
1383
1384 }
1385
1386 static void test_reg_query_value(void)
1387 {
1388     HKEY subkey;
1389     CHAR val[MAX_PATH];
1390     WCHAR valW[5];
1391     LONG size, ret;
1392
1393     static const WCHAR expected[] = {'d','a','t','a',0};
1394
1395     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1396     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1397
1398     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1399     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1400
1401     /* try an invalid hkey */
1402     SetLastError(0xdeadbeef);
1403     size = MAX_PATH;
1404     ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1405     ok(ret == ERROR_INVALID_HANDLE ||
1406        ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1407        ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1408        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1409     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1410
1411     /* try a NULL hkey */
1412     SetLastError(0xdeadbeef);
1413     size = MAX_PATH;
1414     ret = RegQueryValueA(NULL, "subkey", val, &size);
1415     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1416        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1417     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1418
1419     /* try a NULL value */
1420     size = MAX_PATH;
1421     ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1422     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1423     ok(size == 5, "Expected 5, got %d\n", size);
1424
1425     /* try a NULL size */
1426     SetLastError(0xdeadbeef);
1427     val[0] = '\0';
1428     ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1429     ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1430     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1431     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1432
1433     /* try a NULL value and size */
1434     ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1435     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1436
1437     /* try a size too small */
1438     SetLastError(0xdeadbeef);
1439     val[0] = '\0';
1440     size = 1;
1441     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1442     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1443     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1444     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1445     ok(size == 5, "Expected 5, got %d\n", size);
1446
1447     /* successfully read the value using 'subkey' */
1448     size = MAX_PATH;
1449     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1450     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1451     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1452     ok(size == 5, "Expected 5, got %d\n", size);
1453
1454     /* successfully read the value using the subkey key */
1455     size = MAX_PATH;
1456     ret = RegQueryValueA(subkey, NULL, val, &size);
1457     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1458     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1459     ok(size == 5, "Expected 5, got %d\n", size);
1460
1461     /* unicode - try size too small */
1462     SetLastError(0xdeadbeef);
1463     valW[0] = '\0';
1464     size = 0;
1465     ret = RegQueryValueW(subkey, NULL, valW, &size);
1466     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1467     {
1468         win_skip("RegQueryValueW is not implemented\n");
1469         goto cleanup;
1470     }
1471     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1472     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1473     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1474     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1475
1476     /* unicode - try size in WCHARS */
1477     SetLastError(0xdeadbeef);
1478     size = sizeof(valW) / sizeof(WCHAR);
1479     ret = RegQueryValueW(subkey, NULL, valW, &size);
1480     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1481     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1482     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1483     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1484
1485     /* unicode - successfully read the value */
1486     size = sizeof(valW);
1487     ret = RegQueryValueW(subkey, NULL, valW, &size);
1488     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1489     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1490     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1491
1492     /* unicode - set the value without a NULL terminator */
1493     ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1494     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1495
1496     /* unicode - read the unterminated value, value is terminated for us */
1497     memset(valW, 'a', sizeof(valW));
1498     size = sizeof(valW);
1499     ret = RegQueryValueW(subkey, NULL, valW, &size);
1500     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1501     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1502     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1503
1504 cleanup:
1505     RegDeleteKeyA(subkey, "");
1506     RegCloseKey(subkey);
1507 }
1508
1509 static void test_string_termination(void)
1510 {
1511     HKEY subkey;
1512     LSTATUS ret;
1513     static const char string[] = "FullString";
1514     char name[11];
1515     BYTE buffer[11];
1516     DWORD insize, outsize, nsize;
1517
1518     ret = RegCreateKeyA(hkey_main, "string_termination", &subkey);
1519     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1520
1521     /* Off-by-one RegSetValueExA -> adds a trailing '\0'! */
1522     insize=sizeof(string)-1;
1523     ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1524     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1525     outsize=insize;
1526     ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1527     ok(ret == ERROR_MORE_DATA, "RegQueryValueExA returned: %d\n", ret);
1528
1529     /* Off-by-two RegSetValueExA -> no trailing '\0' */
1530     insize=sizeof(string)-2;
1531     ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1532     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1533     outsize=0;
1534     ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, NULL, &outsize);
1535     ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1536     ok(outsize == insize, "wrong size %u != %u\n", outsize, insize);
1537
1538     /* RegQueryValueExA may return a string with no trailing '\0' */
1539     outsize=insize;
1540     memset(buffer, 0xbd, sizeof(buffer));
1541     ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1542     ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1543     ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1544     ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1545        wine_debugstr_an((char*)buffer, outsize), outsize, string);
1546     ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1547
1548     /* RegQueryValueExA adds a trailing '\0' if there is room */
1549     outsize=insize+1;
1550     memset(buffer, 0xbd, sizeof(buffer));
1551     ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1552     ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1553     ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1554     ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1555        wine_debugstr_an((char*)buffer, outsize), outsize, string);
1556     ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1557
1558     /* RegEnumValueA may return a string with no trailing '\0' */
1559     outsize=insize;
1560     memset(buffer, 0xbd, sizeof(buffer));
1561     nsize=sizeof(name);
1562     ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1563     ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1564     ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1565     ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1566     ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1567        wine_debugstr_an((char*)buffer, outsize), outsize, string);
1568     ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1569
1570     /* RegEnumValueA adds a trailing '\0' if there is room */
1571     outsize=insize+1;
1572     memset(buffer, 0xbd, sizeof(buffer));
1573     nsize=sizeof(name);
1574     ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1575     ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1576     ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1577     ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1578     ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1579        wine_debugstr_an((char*)buffer, outsize), outsize, string);
1580     ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1581
1582     RegDeleteKeyA(subkey, "");
1583     RegCloseKey(subkey);
1584 }
1585
1586 static void test_reg_delete_tree(void)
1587 {
1588     CHAR buffer[MAX_PATH];
1589     HKEY subkey, subkey2;
1590     LONG size, ret;
1591
1592     if(!pRegDeleteTreeA) {
1593         win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1594         return;
1595     }
1596
1597     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1598     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1599     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1600     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1601     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1602     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1603     ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1604     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1605     ret = RegCloseKey(subkey2);
1606     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1607
1608     ret = pRegDeleteTreeA(subkey, "subkey2");
1609     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1610     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1611         "subkey2 was not deleted\n");
1612     size = MAX_PATH;
1613     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1614         "Default value of subkey not longer present\n");
1615
1616     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1617     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1618     ret = RegCloseKey(subkey2);
1619     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1620     ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1621     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1622     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1623         "subkey2 was not deleted\n");
1624     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1625         "Default value of subkey not longer present\n");
1626
1627     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1628     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1629     ret = RegCloseKey(subkey2);
1630     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1631     ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1632     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1633     ret = RegCloseKey(subkey2);
1634     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1635     ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1636     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1637     ret = pRegDeleteTreeA(subkey, NULL);
1638     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1639     ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1640         "subkey was deleted\n");
1641     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1642         "subkey2 was not deleted\n");
1643     ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1644         "subkey3 was not deleted\n");
1645     size = MAX_PATH;
1646     ret = RegQueryValueA(subkey, NULL, buffer, &size);
1647     ok(ret == ERROR_SUCCESS,
1648         "Default value of subkey is not present\n");
1649     ok(!lstrlenA(buffer),
1650         "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1651     size = MAX_PATH;
1652     ok(RegQueryValueA(subkey, "value", buffer, &size),
1653         "Value is still present\n");
1654
1655     ret = pRegDeleteTreeA(hkey_main, "not-here");
1656     ok(ret == ERROR_FILE_NOT_FOUND,
1657         "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1658 }
1659
1660 static void test_rw_order(void)
1661 {
1662     HKEY hKey;
1663     DWORD dw = 0;
1664     static char keyname[] = "test_rw_order";
1665     char value_buf[2];
1666     DWORD values, value_len, value_name_max_len;
1667     LSTATUS ret;
1668
1669     RegDeleteKeyA(HKEY_CURRENT_USER, keyname);
1670     ret = RegCreateKeyA(HKEY_CURRENT_USER, keyname, &hKey);
1671     if(ret != ERROR_SUCCESS) {
1672         skip("Couldn't create key. Skipping.\n");
1673         return;
1674     }
1675
1676     ok(!RegSetValueExA(hKey, "A", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1677        "RegSetValueExA for value \"A\" failed\n");
1678     ok(!RegSetValueExA(hKey, "C", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1679        "RegSetValueExA for value \"C\" failed\n");
1680     ok(!RegSetValueExA(hKey, "D", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1681        "RegSetValueExA for value \"D\" failed\n");
1682     ok(!RegSetValueExA(hKey, "B", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1683        "RegSetValueExA for value \"B\" failed\n");
1684
1685     ok(!RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &values,
1686        &value_name_max_len, NULL, NULL, NULL), "RegQueryInfoKeyA failed\n");
1687     ok(values == 4, "Expected 4 values, got %u\n", values);
1688
1689     /* Value enumeration preserves RegSetValueEx call order */
1690     value_len = 2;
1691     ok(!RegEnumValueA(hKey, 0, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1692     ok(strcmp(value_buf, "A") == 0, "Expected name \"A\", got %s\n", value_buf);
1693     value_len = 2;
1694     ok(!RegEnumValueA(hKey, 1, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1695     todo_wine ok(strcmp(value_buf, "C") == 0, "Expected name \"C\", got %s\n", value_buf);
1696     value_len = 2;
1697     ok(!RegEnumValueA(hKey, 2, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1698     todo_wine ok(strcmp(value_buf, "D") == 0, "Expected name \"D\", got %s\n", value_buf);
1699     value_len = 2;
1700     ok(!RegEnumValueA(hKey, 3, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1701     todo_wine ok(strcmp(value_buf, "B") == 0, "Expected name \"B\", got %s\n", value_buf);
1702
1703     ok(!RegDeleteKey(HKEY_CURRENT_USER, keyname), "Failed to delete key\n");
1704 }
1705
1706 static void test_symlinks(void)
1707 {
1708     static const WCHAR targetW[] = {'\\','S','o','f','t','w','a','r','e','\\','W','i','n','e',
1709                                     '\\','T','e','s','t','\\','t','a','r','g','e','t',0};
1710     BYTE buffer[1024];
1711     UNICODE_STRING target_str;
1712     WCHAR *target;
1713     HKEY key, link;
1714     NTSTATUS status;
1715     DWORD target_len, type, len, dw, err;
1716
1717     if (!pRtlFormatCurrentUserKeyPath || !pNtDeleteKey)
1718     {
1719         win_skip( "Can't perform symlink tests\n" );
1720         return;
1721     }
1722
1723     pRtlFormatCurrentUserKeyPath( &target_str );
1724
1725     target_len = target_str.Length + sizeof(targetW);
1726     target = HeapAlloc( GetProcessHeap(), 0, target_len );
1727     memcpy( target, target_str.Buffer, target_str.Length );
1728     memcpy( target + target_str.Length/sizeof(WCHAR), targetW, sizeof(targetW) );
1729
1730     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1731                            KEY_ALL_ACCESS, NULL, &link, NULL );
1732     ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed: %u\n", err );
1733
1734     /* REG_SZ is not allowed */
1735     err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_SZ, (BYTE *)"foobar", sizeof("foobar") );
1736     ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1737     err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_LINK,
1738                           (BYTE *)target, target_len - sizeof(WCHAR) );
1739     ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1740     /* other values are not allowed */
1741     err = RegSetValueExA( link, "link", 0, REG_LINK, (BYTE *)target, target_len - sizeof(WCHAR) );
1742     ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1743
1744     /* try opening the target through the link */
1745
1746     err = RegOpenKeyA( hkey_main, "link", &key );
1747     ok( err == ERROR_FILE_NOT_FOUND, "RegOpenKey wrong error %u\n", err );
1748
1749     err = RegCreateKeyExA( hkey_main, "target", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1750     ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1751
1752     dw = 0xbeef;
1753     err = RegSetValueExA( key, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1754     ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1755     RegCloseKey( key );
1756
1757     err = RegOpenKeyA( hkey_main, "link", &key );
1758     ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1759
1760     len = sizeof(buffer);
1761     err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1762     ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1763     ok( len == sizeof(DWORD), "wrong len %u\n", len );
1764
1765     len = sizeof(buffer);
1766     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1767     ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1768
1769     /* REG_LINK can be created in non-link keys */
1770     err = RegSetValueExA( key, "SymbolicLinkValue", 0, REG_LINK,
1771                           (BYTE *)target, target_len - sizeof(WCHAR) );
1772     ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1773     len = sizeof(buffer);
1774     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1775     ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1776     ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1777     err = RegDeleteValueA( key, "SymbolicLinkValue" );
1778     ok( err == ERROR_SUCCESS, "RegDeleteValue failed error %u\n", err );
1779
1780     RegCloseKey( key );
1781
1782     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1783     ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1784
1785     len = sizeof(buffer);
1786     err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1787     ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1788     ok( len == sizeof(DWORD), "wrong len %u\n", len );
1789
1790     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1791     ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1792     RegCloseKey( key );
1793
1794     /* now open the symlink itself */
1795
1796     err = RegOpenKeyExA( hkey_main, "link", REG_OPTION_OPEN_LINK, KEY_ALL_ACCESS, &key );
1797     ok( err == ERROR_SUCCESS, "RegOpenKeyEx failed error %u\n", err );
1798     len = sizeof(buffer);
1799     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1800     ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1801     ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1802     RegCloseKey( key );
1803
1804     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_OPEN_LINK,
1805                            KEY_ALL_ACCESS, NULL, &key, NULL );
1806     ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1807     len = sizeof(buffer);
1808     err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1809     ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1810     ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1811     RegCloseKey( key );
1812
1813     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1814                            KEY_ALL_ACCESS, NULL, &key, NULL );
1815     ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1816
1817     err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK | REG_OPTION_OPEN_LINK,
1818                            KEY_ALL_ACCESS, NULL, &key, NULL );
1819     ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1820
1821     err = RegDeleteKey( hkey_main, "target" );
1822     ok( err == ERROR_SUCCESS, "RegDeleteKey failed error %u\n", err );
1823
1824     err = RegDeleteKey( hkey_main, "link" );
1825     ok( err == ERROR_FILE_NOT_FOUND, "RegDeleteKey wrong error %u\n", err );
1826
1827     status = pNtDeleteKey( link );
1828     ok( !status, "NtDeleteKey failed: 0x%08x\n", status );
1829     RegCloseKey( link );
1830
1831     HeapFree( GetProcessHeap(), 0, target );
1832     pRtlFreeUnicodeString( &target_str );
1833 }
1834
1835 static DWORD get_key_value( HKEY root, const char *name, DWORD flags )
1836 {
1837     HKEY key;
1838     DWORD err, type, dw, len = sizeof(dw);
1839
1840     err = RegCreateKeyExA( root, name, 0, NULL, 0, flags | KEY_ALL_ACCESS, NULL, &key, NULL );
1841     if (err == ERROR_FILE_NOT_FOUND) return 0;
1842     ok( err == ERROR_SUCCESS, "%08x: RegCreateKeyEx failed: %u\n", flags, err );
1843
1844     err = RegQueryValueExA( key, "value", NULL, &type, (BYTE *)&dw, &len );
1845     if (err == ERROR_FILE_NOT_FOUND)
1846         dw = 0;
1847     else
1848         ok( err == ERROR_SUCCESS, "%08x: RegQueryValueEx failed: %u\n", flags, err );
1849     RegCloseKey( key );
1850     return dw;
1851 }
1852
1853 static void _check_key_value( int line, HANDLE root, const char *name, DWORD flags, DWORD expect )
1854 {
1855     DWORD dw = get_key_value( root, name, flags );
1856     ok_(__FILE__,line)( dw == expect, "%08x: wrong value %u/%u\n", flags, dw, expect );
1857 }
1858 #define check_key_value(root,name,flags,expect) _check_key_value( __LINE__, root, name, flags, expect )
1859
1860 static void test_redirection(void)
1861 {
1862     DWORD err, type, dw, len;
1863     HKEY key, root32, root64, key32, key64;
1864     BOOL is_vista = FALSE;
1865
1866     if (ptr_size != 64)
1867     {
1868         BOOL is_wow64;
1869         if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 ) || !is_wow64)
1870         {
1871             skip( "Not on Wow64, no redirection\n" );
1872             return;
1873         }
1874     }
1875
1876     err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1877                            KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &root64, NULL );
1878     ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1879
1880     err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1881                            KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &root32, NULL );
1882     ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1883
1884     err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, NULL, 0,
1885                            KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key64, NULL );
1886     ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1887
1888     err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, NULL, 0,
1889                            KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key32, NULL );
1890     ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1891
1892     dw = 64;
1893     err = RegSetValueExA( key64, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1894     ok( err == ERROR_SUCCESS, "RegSetValueExA failed: %u\n", err );
1895
1896     dw = 32;
1897     err = RegSetValueExA( key32, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1898     ok( err == ERROR_SUCCESS, "RegSetValueExA failed: %u\n", err );
1899
1900     dw = 0;
1901     len = sizeof(dw);
1902     err = RegQueryValueExA( key32, "value", NULL, &type, (BYTE *)&dw, &len );
1903     ok( err == ERROR_SUCCESS, "RegQueryValueExA failed: %u\n", err );
1904     ok( dw == 32, "wrong value %u\n", dw );
1905
1906     dw = 0;
1907     len = sizeof(dw);
1908     err = RegQueryValueExA( key64, "value", NULL, &type, (BYTE *)&dw, &len );
1909     ok( err == ERROR_SUCCESS, "RegQueryValueExA failed: %u\n", err );
1910     ok( dw == 64, "wrong value %u\n", dw );
1911
1912     err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
1913                            KEY_ALL_ACCESS, NULL, &key, NULL );
1914     ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1915
1916     if (ptr_size == 32)
1917     {
1918         /* the Vista mechanism allows opening Wow6432Node from a 32-bit key too */
1919         /* the new (and simpler) Win7 mechanism doesn't */
1920         if (get_key_value( key, "Wow6432Node\\Wine\\Winetest", 0 ) == 32)
1921         {
1922             trace( "using Vista-style Wow6432Node handling\n" );
1923             is_vista = TRUE;
1924         }
1925         check_key_value( key, "Wine\\Winetest", 0, 32 );
1926         check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1927         check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1928         check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, is_vista ? 32 : 0 );
1929         check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 0 );
1930         check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, is_vista ? 32 : 0 );
1931     }
1932     else
1933     {
1934         if (get_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY ) == 64)
1935         {
1936             trace( "using Vista-style Wow6432Node handling\n" );
1937             is_vista = TRUE;
1938         }
1939         check_key_value( key, "Wine\\Winetest", 0, 64 );
1940         check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
1941     }
1942     RegCloseKey( key );
1943
1944     if (ptr_size == 32)
1945     {
1946         err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
1947                                KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1948         ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1949         dw = get_key_value( key, "Wine\\Winetest", 0 );
1950         ok( dw == 64 || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
1951         check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, 64 );
1952         check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1953         check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
1954         dw = get_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY );
1955         ok( dw == 32 || broken(dw == 64) /* xp64 */, "wrong value %u\n", dw );
1956         check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1957         RegCloseKey( key );
1958
1959         err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
1960                                KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1961         ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1962         check_key_value( key, "Wine\\Winetest", 0, 32 );
1963         check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1964         check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1965         check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, is_vista ? 32 : 0 );
1966         check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 0 );
1967         check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, is_vista ? 32 : 0 );
1968         RegCloseKey( key );
1969     }
1970
1971     check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, ptr_size );
1972     check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", 0, 32 );
1973     if (ptr_size == 64)
1974     {
1975         /* KEY_WOW64 flags have no effect on 64-bit */
1976         check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_64KEY, 64 );
1977         check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1978         check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1979         check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1980     }
1981     else
1982     {
1983         check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_64KEY, 64 );
1984         check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1985         check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1986         check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1987     }
1988
1989     err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
1990                            KEY_ALL_ACCESS, NULL, &key, NULL );
1991     ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1992     check_key_value( key, "Wine\\Winetest", 0, 32 );
1993     check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1994     check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1995     RegCloseKey( key );
1996
1997     if (ptr_size == 32)
1998     {
1999         err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
2000                                KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2001         ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2002         dw = get_key_value( key, "Wine\\Winetest", 0 );
2003         ok( dw == (is_vista ? 64 : 32) || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
2004         check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2005         check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2006         RegCloseKey( key );
2007
2008         err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
2009                                KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2010         ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2011         check_key_value( key, "Wine\\Winetest", 0, 32 );
2012         check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2013         check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2014         RegCloseKey( key );
2015     }
2016
2017     err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2018                            KEY_ALL_ACCESS, NULL, &key, NULL );
2019     ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2020     check_key_value( key, "Winetest", 0, 32 );
2021     check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2022     check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2023     RegCloseKey( key );
2024
2025     if (ptr_size == 32)
2026     {
2027         err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2028                                KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2029         ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2030         dw = get_key_value( key, "Winetest", 0 );
2031         ok( dw == 32 || (is_vista && dw == 64), "wrong value %u\n", dw );
2032         check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2033         check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2034         RegCloseKey( key );
2035
2036         err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2037                                KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2038         ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2039         check_key_value( key, "Winetest", 0, 32 );
2040         check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2041         check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2042         RegCloseKey( key );
2043     }
2044
2045     err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2046                            KEY_ALL_ACCESS, NULL, &key, NULL );
2047     ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2048     check_key_value( key, "Winetest", 0, ptr_size );
2049     check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : ptr_size );
2050     dw = get_key_value( key, "Winetest", KEY_WOW64_32KEY );
2051     if (ptr_size == 32) ok( dw == 32, "wrong value %u\n", dw );
2052     else todo_wine ok( dw == 32, "wrong value %u\n", dw );
2053     RegCloseKey( key );
2054
2055     if (ptr_size == 32)
2056     {
2057         err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2058                                KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2059         ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2060         dw = get_key_value( key, "Winetest", 0 );
2061         ok( dw == 64 || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
2062         check_key_value( key, "Winetest", KEY_WOW64_64KEY, 64 );
2063         dw = get_key_value( key, "Winetest", KEY_WOW64_32KEY );
2064         todo_wine ok( dw == 32, "wrong value %u\n", dw );
2065         RegCloseKey( key );
2066
2067         err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2068                                KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2069         ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2070         check_key_value( key, "Winetest", 0, 32 );
2071         check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2072         check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2073         RegCloseKey( key );
2074     }
2075
2076     if (pRegDeleteKeyExA)
2077     {
2078         err = pRegDeleteKeyExA( key32, "", KEY_WOW64_32KEY, 0 );
2079         ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2080         err = pRegDeleteKeyExA( key64, "", KEY_WOW64_64KEY, 0 );
2081         ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2082         pRegDeleteKeyExA( key64, "", KEY_WOW64_64KEY, 0 );
2083         pRegDeleteKeyExA( root64, "", KEY_WOW64_64KEY, 0 );
2084     }
2085     else
2086     {
2087         err = RegDeleteKeyA( key32, "" );
2088         ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2089         err = RegDeleteKeyA( key64, "" );
2090         ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2091         RegDeleteKeyA( key64, "" );
2092         RegDeleteKeyA( root64, "" );
2093     }
2094     RegCloseKey( key32 );
2095     RegCloseKey( key64 );
2096     RegCloseKey( root32 );
2097     RegCloseKey( root64 );
2098 }
2099
2100 static void test_classesroot(void)
2101 {
2102     HKEY hkey, hklm, hkcr, hkeysub1, hklmsub1, hkcrsub1, hklmsub2, hkcrsub2;
2103     DWORD size = 8;
2104     DWORD type = REG_SZ;
2105     static CHAR buffer[8];
2106     LONG res;
2107
2108     /* create a key in the user's classes */
2109     if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", &hkey ))
2110     {
2111         delete_key( hkey );
2112         RegCloseKey( hkey );
2113     }
2114     res = RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2115                            KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkey, NULL );
2116     if (res == ERROR_ACCESS_DENIED)
2117     {
2118         skip("not enough privileges to add a user class\n");
2119         return;
2120     }
2121
2122     /* try to open that key in hkcr */
2123     res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2124                          KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2125     todo_wine ok(res == ERROR_SUCCESS ||
2126                  broken(res == ERROR_FILE_NOT_FOUND /* WinNT */),
2127                  "test key not found in hkcr: %d\n", res);
2128     if (res)
2129     {
2130         skip("HKCR key merging not supported\n");
2131         delete_key( hkey );
2132         RegCloseKey( hkey );
2133         return;
2134     }
2135
2136     /* set a value in user's classes */
2137     res = RegSetValueExA(hkey, "val1", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2138     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2139
2140     /* try to find the value in hkcr */
2141     res = RegQueryValueExA(hkcr, "val1", NULL, &type, (LPBYTE)buffer, &size);
2142     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2143     ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2144
2145     /* modify the value in hkcr */
2146     res = RegSetValueExA(hkcr, "val1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2147     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2148
2149     /* check if the value is also modified in user's classes */
2150     res = RegQueryValueExA(hkey, "val1", NULL, &type, (LPBYTE)buffer, &size);
2151     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2152     ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2153
2154     /* set a value in hkcr */
2155     res = RegSetValueExA(hkcr, "val0", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2156     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2157
2158     /* try to find the value in user's classes */
2159     res = RegQueryValueExA(hkcr, "val0", NULL, &type, (LPBYTE)buffer, &size);
2160     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2161     ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2162
2163     /* modify the value in user's classes */
2164     res = RegSetValueExA(hkcr, "val0", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2165     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2166
2167     /* check if the value is also modified in hkcr */
2168     res = RegQueryValueExA(hkey, "val0", NULL, &type, (LPBYTE)buffer, &size);
2169     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2170     ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2171
2172     /* cleanup */
2173     delete_key( hkey );
2174     delete_key( hkcr );
2175     RegCloseKey( hkey );
2176     RegCloseKey( hkcr );
2177
2178     /* create a key in the hklm classes */
2179     if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", &hklm ))
2180     {
2181         delete_key( hklm );
2182         RegCloseKey( hklm );
2183     }
2184     res = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", 0, NULL, REG_OPTION_NON_VOLATILE,
2185                            KEY_ALL_ACCESS, NULL, &hklm, NULL );
2186     if (res == ERROR_ACCESS_DENIED)
2187     {
2188         skip("not enough privileges to add a system class\n");
2189         return;
2190     }
2191
2192     /* try to open that key in hkcr */
2193     res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2194                          KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2195     ok(res == ERROR_SUCCESS,
2196        "test key not found in hkcr: %d\n", res);
2197     if (res)
2198     {
2199         delete_key( hklm );
2200         RegCloseKey( hklm );
2201         return;
2202     }
2203
2204     /* set a value in hklm classes */
2205     res = RegSetValueExA(hklm, "val2", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2206     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2207
2208     /* try to find the value in hkcr */
2209     res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2210     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2211     ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2212
2213     /* modify the value in hkcr */
2214     res = RegSetValueExA(hkcr, "val2", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2215     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2216
2217     /* check that the value is not modified in hklm classes */
2218     res = RegQueryValueExA(hklm, "val2", NULL, &type, (LPBYTE)buffer, &size);
2219     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2220     ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2221
2222     if (RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2223                          KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkey, NULL )) return;
2224
2225     /* try to open that key in hkcr */
2226     res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2227                          KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2228     ok(res == ERROR_SUCCESS,
2229        "test key not found in hkcr: %d\n", res);
2230
2231     /* set a value in user's classes */
2232     res = RegSetValueExA(hkey, "val2", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2233     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2234
2235     /* try to find the value in hkcr */
2236     res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2237     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2238     ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2239
2240     /* modify the value in hklm */
2241     res = RegSetValueExA(hklm, "val2", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2242     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2243
2244     /* check that the value is not overwritten in hkcr or user's classes */
2245     res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2246     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2247     ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2248     res = RegQueryValueExA(hkey, "val2", NULL, &type, (LPBYTE)buffer, &size);
2249     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2250     ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2251
2252     /* modify the value in hkcr */
2253     res = RegSetValueExA(hkcr, "val2", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2254     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2255
2256     /* check that the value is overwritten in hklm and user's classes */
2257     res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2258     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2259     ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2260     res = RegQueryValueExA(hkey, "val2", NULL, &type, (LPBYTE)buffer, &size);
2261     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2262     ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2263
2264     /* create a subkey in hklm */
2265     if (RegCreateKeyExA( hklm, "subkey1", 0, NULL, 0,
2266                          KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hklmsub1, NULL )) return;
2267     /* try to open that subkey in hkcr */
2268     res = RegOpenKeyExA( hkcr, "subkey1", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcrsub1 );
2269     ok(res == ERROR_SUCCESS, "test key not found in hkcr: %d\n", res);
2270
2271     /* set a value in hklm classes */
2272     res = RegSetValueExA(hklmsub1, "subval1", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2273     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2274
2275     /* try to find the value in hkcr */
2276     res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2277     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2278     ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2279
2280     /* modify the value in hkcr */
2281     res = RegSetValueExA(hkcrsub1, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2282     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2283
2284     /* check that the value is modified in hklm classes */
2285     res = RegQueryValueExA(hklmsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2286     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2287     ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2288
2289     /* create a subkey in user's classes */
2290     if (RegCreateKeyExA( hkey, "subkey1", 0, NULL, 0,
2291                          KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkeysub1, NULL )) return;
2292
2293     /* set a value in user's classes */
2294     res = RegSetValueExA(hkeysub1, "subval1", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2295     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2296
2297     /* try to find the value in hkcr */
2298     res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2299     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2300     ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2301
2302     /* modify the value in hklm */
2303     res = RegSetValueExA(hklmsub1, "subval1", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2304     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2305
2306     /* check that the value is not overwritten in hkcr or user's classes */
2307     res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2308     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2309     ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2310     res = RegQueryValueExA(hkeysub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2311     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2312     ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2313
2314     /* modify the value in hkcr */
2315     res = RegSetValueExA(hkcrsub1, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2316     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2317
2318     /* check that the value is not overwritten in hklm, but in user's classes */
2319     res = RegQueryValueExA(hklmsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2320     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2321     ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2322     res = RegQueryValueExA(hkeysub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2323     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2324     ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2325
2326     /* new subkey in hkcr */
2327     if (RegCreateKeyExA( hkcr, "subkey2", 0, NULL, 0,
2328                          KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkcrsub2, NULL )) return;
2329     res = RegSetValueExA(hkcrsub2, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2330     ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2331
2332     /* try to open that new subkey in user's classes and hklm */
2333     res = RegOpenKeyExA( hkey, "subkey2", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hklmsub2 );
2334     ok(res != ERROR_SUCCESS, "test key found in user's classes: %d\n", res);
2335     hklmsub2 = 0;
2336     res = RegOpenKeyExA( hklm, "subkey2", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hklmsub2 );
2337     ok(res == ERROR_SUCCESS, "test key not found in hklm: %d\n", res);
2338
2339     /* check that the value is present in hklm */
2340     res = RegQueryValueExA(hklmsub2, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2341     ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2342     ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2343
2344     /* final cleanup */
2345     delete_key( hkey );
2346     delete_key( hklm );
2347     delete_key( hkcr );
2348     delete_key( hkeysub1 );
2349     delete_key( hklmsub1 );
2350     delete_key( hkcrsub1 );
2351     delete_key( hklmsub2 );
2352     delete_key( hkcrsub2 );
2353     RegCloseKey( hkey );
2354     RegCloseKey( hklm );
2355     RegCloseKey( hkcr );
2356     RegCloseKey( hkeysub1 );
2357     RegCloseKey( hklmsub1 );
2358     RegCloseKey( hkcrsub1 );
2359     RegCloseKey( hklmsub2 );
2360     RegCloseKey( hkcrsub2 );
2361 }
2362
2363 static void test_deleted_key(void)
2364 {
2365     HKEY hkey, hkey2;
2366     char value[20];
2367     DWORD val_count, type;
2368     LONG res;
2369
2370     /* Open the test key, then delete it while it's open */
2371     RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey );
2372
2373     delete_key( hkey_main );
2374
2375     val_count = sizeof(value);
2376     type = 0;
2377     res = RegEnumValueA( hkey, 0, value, &val_count, NULL, &type, 0, 0 );
2378     ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2379
2380     res = RegEnumKeyA( hkey, 0, value, sizeof(value) );
2381     ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2382
2383     val_count = sizeof(value);
2384     type = 0;
2385     res = RegQueryValueExA( hkey, "test", NULL, &type, (BYTE *)value, &val_count );
2386     ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2387
2388     res = RegSetValueExA( hkey, "test", 0, REG_SZ, (const BYTE*)"value", 6);
2389     ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2390
2391     res = RegOpenKeyA( hkey, "test", &hkey2 );
2392     ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2393     if (res == 0)
2394         RegCloseKey( hkey2 );
2395
2396     res = RegCreateKeyA( hkey, "test", &hkey2 );
2397     ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2398     if (res == 0)
2399         RegCloseKey( hkey2 );
2400
2401     res = RegFlushKey( hkey );
2402     ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2403
2404     RegCloseKey( hkey );
2405
2406     setup_main_key();
2407 }
2408
2409 static void test_delete_value(void)
2410 {
2411     LONG res;
2412     char longname[401];
2413
2414     res = RegSetValueExA( hkey_main, "test", 0, REG_SZ, (const BYTE*)"value", 6 );
2415     ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2416
2417     res = RegQueryValueExA( hkey_main, "test", NULL, NULL, NULL, NULL);
2418     ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2419
2420     res = RegDeleteValueA( hkey_main, "test" );
2421     ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2422
2423     res = RegQueryValueExA( hkey_main, "test", NULL, NULL, NULL, NULL);
2424     ok(res == ERROR_FILE_NOT_FOUND, "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2425
2426     res = RegDeleteValueA( hkey_main, "test" );
2427     ok(res == ERROR_FILE_NOT_FOUND, "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2428
2429     memset(longname, 'a', 400);
2430     longname[400] = 0;
2431     res = RegDeleteValueA( hkey_main, longname );
2432     ok(res == ERROR_FILE_NOT_FOUND || broken(res == ERROR_MORE_DATA), /* nt4, win2k */
2433        "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2434 }
2435
2436 START_TEST(registry)
2437 {
2438     /* Load pointers for functions that are not available in all Windows versions */
2439     InitFunctionPtrs();
2440
2441     setup_main_key();
2442     test_set_value();
2443     create_test_entries();
2444     test_enum_value();
2445     test_query_value_ex();
2446     test_get_value();
2447     test_reg_open_key();
2448     test_reg_create_key();
2449     test_reg_close_key();
2450     test_reg_delete_key();
2451     test_reg_query_value();
2452     test_string_termination();
2453     test_symlinks();
2454     test_redirection();
2455     test_classesroot();
2456
2457     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
2458     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
2459         set_privileges(SE_RESTORE_NAME, TRUE))
2460     {
2461         test_reg_save_key();
2462         test_reg_load_key();
2463         test_reg_unload_key();
2464
2465         set_privileges(SE_BACKUP_NAME, FALSE);
2466         set_privileges(SE_RESTORE_NAME, FALSE);
2467     }
2468
2469     test_reg_delete_tree();
2470     test_rw_order();
2471     test_deleted_key();
2472     test_delete_value();
2473
2474     /* cleanup */
2475     delete_key( hkey_main );
2476     
2477     test_regconnectregistry();
2478 }