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