advapi32/tests: Account for limited users in registry test.
[wine/multimedia.git] / dlls / advapi32 / tests / registry.c
blobd3392535cac5ca818f98d597bd5838f11bf1e6ec
1 /*
2 * Unit tests for registry functions
4 * Copyright (c) 2002 Alexandre Julliard
5 * Copyright (c) 2010 André Hentschel
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.
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.
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
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"
34 #define IS_HKCR(hk) ((UINT_PTR)hk > 0 && ((UINT_PTR)hk & 3) == 2)
36 static HKEY hkey_main;
37 static DWORD GLE;
39 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
40 static const char * sTestpath2 = "%FOO%\\subdir1";
41 static const DWORD ptr_size = 8 * sizeof(void*);
43 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
44 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
45 static DWORD (WINAPI *pRegDeleteKeyExA)(HKEY,LPCSTR,REGSAM,DWORD);
46 static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
47 static NTSTATUS (WINAPI * pNtDeleteKey)(HANDLE);
48 static NTSTATUS (WINAPI * pRtlFormatCurrentUserKeyPath)(UNICODE_STRING*);
49 static NTSTATUS (WINAPI * pRtlFreeUnicodeString)(PUNICODE_STRING);
51 static BOOL limited_user;
54 /* Debugging functions from wine/libs/wine/debug.c */
56 /* allocate some tmp string space */
57 /* FIXME: this is not 100% thread-safe */
58 static char *get_temp_buffer( int size )
60 static char *list[32];
61 static UINT pos;
62 char *ret;
63 UINT idx;
65 idx = ++pos % (sizeof(list)/sizeof(list[0]));
66 if (list[idx])
67 ret = HeapReAlloc( GetProcessHeap(), 0, list[idx], size );
68 else
69 ret = HeapAlloc( GetProcessHeap(), 0, size );
70 if (ret) list[idx] = ret;
71 return ret;
74 static const char *wine_debugstr_an( const char *str, int n )
76 static const char hex[16] = "0123456789abcdef";
77 char *dst, *res;
78 size_t size;
80 if (!((ULONG_PTR)str >> 16))
82 if (!str) return "(null)";
83 res = get_temp_buffer( 6 );
84 sprintf( res, "#%04x", LOWORD(str) );
85 return res;
87 if (n == -1) n = strlen(str);
88 if (n < 0) n = 0;
89 size = 10 + min( 300, n * 4 );
90 dst = res = get_temp_buffer( size );
91 *dst++ = '"';
92 while (n-- > 0 && dst <= res + size - 9)
94 unsigned char c = *str++;
95 switch (c)
97 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
98 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
99 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
100 case '"': *dst++ = '\\'; *dst++ = '"'; break;
101 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
102 default:
103 if (c >= ' ' && c <= 126)
104 *dst++ = c;
105 else
107 *dst++ = '\\';
108 *dst++ = 'x';
109 *dst++ = hex[(c >> 4) & 0x0f];
110 *dst++ = hex[c & 0x0f];
114 *dst++ = '"';
115 if (n > 0)
117 *dst++ = '.';
118 *dst++ = '.';
119 *dst++ = '.';
121 *dst++ = 0;
122 return res;
125 #define ADVAPI32_GET_PROC(func) \
126 p ## func = (void*)GetProcAddress(hadvapi32, #func)
128 static void InitFunctionPtrs(void)
130 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
131 HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
132 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
134 /* This function was introduced with Windows 2003 SP1 */
135 ADVAPI32_GET_PROC(RegGetValueA);
136 ADVAPI32_GET_PROC(RegDeleteTreeA);
137 ADVAPI32_GET_PROC(RegDeleteKeyExA);
139 pIsWow64Process = (void *)GetProcAddress( hkernel32, "IsWow64Process" );
140 pRtlFormatCurrentUserKeyPath = (void *)GetProcAddress( hntdll, "RtlFormatCurrentUserKeyPath" );
141 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
142 pNtDeleteKey = (void *)GetProcAddress( hntdll, "NtDeleteKey" );
145 /* delete key and all its subkeys */
146 static DWORD delete_key( HKEY hkey )
148 char name[MAX_PATH];
149 DWORD ret;
151 if ((ret = RegOpenKeyExA( hkey, "", 0, KEY_ENUMERATE_SUB_KEYS, &hkey ))) return ret;
152 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
154 HKEY tmp;
155 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
157 ret = delete_key( tmp );
158 RegCloseKey( tmp );
160 if (ret) break;
162 if (ret != ERROR_NO_MORE_ITEMS) return ret;
163 RegDeleteKeyA( hkey, "" );
164 RegCloseKey(hkey);
165 return 0;
168 static void setup_main_key(void)
170 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
172 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
175 static void check_user_privs(void)
177 DWORD ret;
178 HKEY hkey = (HKEY)0xdeadbeef;
180 ret = RegOpenKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WRITE, &hkey);
181 ok(ret == ERROR_SUCCESS || ret == ERROR_ACCESS_DENIED, "expected success or access denied, got %i\n", ret);
182 if (ret == ERROR_SUCCESS)
184 ok(hkey != NULL, "RegOpenKeyExA succeeded but returned NULL hkey\n");
185 RegCloseKey(hkey);
187 else
189 ok(hkey == NULL, "RegOpenKeyExA failed but returned hkey %p\n", hkey);
190 limited_user = TRUE;
191 trace("running as limited user\n");
195 #define lok ok_(__FILE__, line)
196 #define test_hkey_main_Value_A(name, string, full_byte_len) _test_hkey_main_Value_A(__LINE__, name, string, full_byte_len)
197 static void _test_hkey_main_Value_A(int line, LPCSTR name, LPCSTR string,
198 DWORD full_byte_len)
200 DWORD ret, type, cbData;
201 DWORD str_byte_len;
202 BYTE* value;
204 type=0xdeadbeef;
205 cbData=0xdeadbeef;
206 /* When successful RegQueryValueExA() leaves GLE as is,
207 * so we must reset it to detect unimplemented functions.
209 SetLastError(0xdeadbeef);
210 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
211 GLE = GetLastError();
212 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/1 failed: %d, GLE=%d\n", ret, GLE);
213 /* It is wrong for the Ansi version to not be implemented */
214 ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
215 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
217 str_byte_len = (string ? lstrlenA(string) : 0) + 1;
218 lok(type == REG_SZ, "RegQueryValueExA/1 returned type %d\n", type);
219 lok(cbData == full_byte_len, "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
221 value = HeapAlloc(GetProcessHeap(), 0, cbData+1);
222 memset(value, 0xbd, cbData+1);
223 type=0xdeadbeef;
224 ret = RegQueryValueExA(hkey_main, name, NULL, &type, value, &cbData);
225 GLE = GetLastError();
226 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/2 failed: %d, GLE=%d\n", ret, GLE);
227 if (!string)
229 /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
230 lok(*value == 0xbd, "RegQueryValueExA overflowed: cbData=%u *value=%02x\n", cbData, *value);
232 else
234 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExA/2 failed: %s/%d != %s/%d\n",
235 wine_debugstr_an((char*)value, cbData), cbData,
236 wine_debugstr_an(string, full_byte_len), full_byte_len);
237 lok(*(value+cbData) == 0xbd, "RegQueryValueExA/2 overflowed at offset %u: %02x != bd\n", cbData, *(value+cbData));
239 HeapFree(GetProcessHeap(), 0, value);
242 #define test_hkey_main_Value_W(name, string, full_byte_len) _test_hkey_main_Value_W(__LINE__, name, string, full_byte_len)
243 static void _test_hkey_main_Value_W(int line, LPCWSTR name, LPCWSTR string,
244 DWORD full_byte_len)
246 DWORD ret, type, cbData;
247 BYTE* value;
249 type=0xdeadbeef;
250 cbData=0xdeadbeef;
251 /* When successful RegQueryValueExW() leaves GLE as is,
252 * so we must reset it to detect unimplemented functions.
254 SetLastError(0xdeadbeef);
255 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
256 GLE = GetLastError();
257 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/1 failed: %d, GLE=%d\n", ret, GLE);
258 if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
260 win_skip("RegQueryValueExW() is not implemented\n");
261 return;
264 lok(type == REG_SZ, "RegQueryValueExW/1 returned type %d\n", type);
265 lok(cbData == full_byte_len,
266 "cbData=%d instead of %d\n", cbData, full_byte_len);
268 /* Give enough space to overflow by one WCHAR */
269 value = HeapAlloc(GetProcessHeap(), 0, cbData+2);
270 memset(value, 0xbd, cbData+2);
271 type=0xdeadbeef;
272 ret = RegQueryValueExW(hkey_main, name, NULL, &type, value, &cbData);
273 GLE = GetLastError();
274 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/2 failed: %d, GLE=%d\n", ret, GLE);
275 if (string)
277 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
278 wine_dbgstr_wn((WCHAR*)value, cbData / sizeof(WCHAR)), cbData,
279 wine_dbgstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
281 /* This implies that when cbData == 0, RegQueryValueExW() should not modify the buffer */
282 lok(*(value+cbData) == 0xbd, "RegQueryValueExW/2 overflowed at %u: %02x != bd\n", cbData, *(value+cbData));
283 lok(*(value+cbData+1) == 0xbd, "RegQueryValueExW/2 overflowed at %u+1: %02x != bd\n", cbData, *(value+cbData+1));
284 HeapFree(GetProcessHeap(), 0, value);
287 static void test_set_value(void)
289 DWORD ret;
291 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
292 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};
293 static const WCHAR emptyW[] = {0};
294 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
295 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};
296 static const WCHAR substring2W[] = {'T','h','i','s',0};
298 static const char name1A[] = "CleanSingleString";
299 static const char name2A[] = "SomeIntraZeroedString";
300 static const char emptyA[] = "";
301 static const char string1A[] = "ThisNeverBreaks";
302 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
303 static const char substring2A[] = "This";
305 if (0)
307 /* Crashes on NT4, Windows 2000 and XP SP1 */
308 ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
309 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
312 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
313 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
314 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
315 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
317 /* RegSetValueA ignores the size passed in */
318 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
319 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
320 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
321 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
323 /* stops at first null */
324 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
325 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
326 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
327 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
329 /* only REG_SZ is supported on NT*/
330 ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
331 ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\n", ret);
333 ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
334 ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\n", ret);
336 ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
337 ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\n", ret);
339 /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
340 * Surprisingly enough we're supposed to get zero bytes out of it.
342 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
343 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
344 test_hkey_main_Value_A(name1A, NULL, 0);
345 test_hkey_main_Value_W(name1W, NULL, 0);
347 /* test RegSetValueExA with an empty string */
348 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
349 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
350 test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
351 test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
353 /* test RegSetValueExA with off-by-one size */
354 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
355 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
356 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
357 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
359 /* test RegSetValueExA with normal string */
360 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
361 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
362 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
363 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
365 /* test RegSetValueExA with intrazeroed string */
366 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
367 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
368 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
369 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
371 if (0)
373 /* Crashes on NT4, Windows 2000 and XP SP1 */
374 ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
375 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
377 RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)1, 1);
378 RegSetValueExA(hkey_main, name2A, 0, REG_DWORD, (const BYTE *)1, 1);
381 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
382 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
383 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
384 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
386 ret = RegSetValueW(hkey_main, name1W, REG_SZ, string1W, sizeof(string1W));
387 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
388 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
389 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
391 /* RegSetValueW ignores the size passed in */
392 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
393 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
394 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
395 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
397 /* stops at first null */
398 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
399 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
400 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
401 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
403 /* only REG_SZ is supported */
404 ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
405 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
406 ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
407 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
408 ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
409 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
411 /* test RegSetValueExW with off-by-one size */
412 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
413 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
414 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
415 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
417 /* test RegSetValueExW with normal string */
418 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
419 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
420 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
421 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
423 /* test RegSetValueExW with intrazeroed string */
424 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
425 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
426 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
427 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
429 /* test RegSetValueExW with data = 1 */
430 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)1, 1);
431 ok(ret == ERROR_NOACCESS, "RegSetValueExW should have failed with ERROR_NOACCESS: %d, GLE=%d\n", ret, GetLastError());
432 ret = RegSetValueExW(hkey_main, name2W, 0, REG_DWORD, (const BYTE *)1, 1);
433 ok(ret == ERROR_NOACCESS, "RegSetValueExW should have failed with ERROR_NOACCESS: %d, GLE=%d\n", ret, GetLastError());
436 static void create_test_entries(void)
438 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
440 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
441 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
443 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
444 "RegSetValueExA failed\n");
445 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
446 "RegSetValueExA failed\n");
447 ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
448 "RegSetValueExA failed\n");
449 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
450 "RegSetValueExA failed\n");
451 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
452 "RegSetValueExA failed\n");
453 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
454 "RegSetValueExA failed\n");
455 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
456 "RegSetValueExA failed\n");
459 static void test_enum_value(void)
461 DWORD res;
462 HKEY test_key;
463 char value[20], data[20];
464 WCHAR valueW[20], dataW[20];
465 DWORD val_count, data_count, type;
466 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
467 static const WCHAR testW[] = {'T','e','s','t',0};
468 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
470 /* create the working key for new 'Test' value */
471 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
472 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
474 /* check NULL data with zero length */
475 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
476 if (GetVersion() & 0x80000000)
477 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
478 else
479 ok( !res, "RegSetValueExA returned %d\n", res );
480 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
481 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
482 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
483 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
485 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
486 ok( res == 0, "RegSetValueExA failed error %d\n", res );
488 /* overflow both name and data */
489 val_count = 2;
490 data_count = 2;
491 type = 1234;
492 strcpy( value, "xxxxxxxxxx" );
493 strcpy( data, "xxxxxxxxxx" );
494 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
495 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
496 ok( val_count == 2, "val_count set to %d\n", val_count );
497 ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
498 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
499 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
500 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
502 /* overflow name */
503 val_count = 3;
504 data_count = 20;
505 type = 1234;
506 strcpy( value, "xxxxxxxxxx" );
507 strcpy( data, "xxxxxxxxxx" );
508 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
509 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
510 ok( val_count == 3, "val_count set to %d\n", val_count );
511 ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
512 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
513 /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
514 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
515 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
516 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ) || broken( !strcmp( data, "xxxxxxxx" ) && data_count == 8 ),
517 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
519 /* overflow empty name */
520 val_count = 0;
521 data_count = 20;
522 type = 1234;
523 strcpy( value, "xxxxxxxxxx" );
524 strcpy( data, "xxxxxxxxxx" );
525 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
526 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
527 ok( val_count == 0, "val_count set to %d\n", val_count );
528 ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
529 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
530 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
531 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
532 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ) || broken( !strcmp( data, "xxxxxxxx" ) && data_count == 8 ),
533 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
535 /* overflow data */
536 val_count = 20;
537 data_count = 2;
538 type = 1234;
539 strcpy( value, "xxxxxxxxxx" );
540 strcpy( data, "xxxxxxxxxx" );
541 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
542 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
543 ok( val_count == 20, "val_count set to %d\n", val_count );
544 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
545 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
546 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
547 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
549 /* no overflow */
550 val_count = 20;
551 data_count = 20;
552 type = 1234;
553 strcpy( value, "xxxxxxxxxx" );
554 strcpy( data, "xxxxxxxxxx" );
555 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
556 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
557 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
558 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
559 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
560 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
561 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
563 /* Unicode tests */
565 SetLastError(0xdeadbeef);
566 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
567 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
569 win_skip("RegSetValueExW is not implemented\n");
570 goto cleanup;
572 ok( res == 0, "RegSetValueExW failed error %d\n", res );
574 /* overflow both name and data */
575 val_count = 2;
576 data_count = 2;
577 type = 1234;
578 memcpy( valueW, xxxW, sizeof(xxxW) );
579 memcpy( dataW, xxxW, sizeof(xxxW) );
580 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
581 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
582 ok( val_count == 2, "val_count set to %d\n", val_count );
583 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
584 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
585 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
586 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
588 /* overflow name */
589 val_count = 3;
590 data_count = 20;
591 type = 1234;
592 memcpy( valueW, xxxW, sizeof(xxxW) );
593 memcpy( dataW, xxxW, sizeof(xxxW) );
594 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
595 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
596 ok( val_count == 3, "val_count set to %d\n", val_count );
597 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
598 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
599 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
600 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
602 /* overflow data */
603 val_count = 20;
604 data_count = 2;
605 type = 1234;
606 memcpy( valueW, xxxW, sizeof(xxxW) );
607 memcpy( dataW, xxxW, sizeof(xxxW) );
608 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
609 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
610 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
611 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
612 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
613 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
614 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
616 /* no overflow */
617 val_count = 20;
618 data_count = 20;
619 type = 1234;
620 memcpy( valueW, xxxW, sizeof(xxxW) );
621 memcpy( dataW, xxxW, sizeof(xxxW) );
622 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
623 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
624 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
625 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
626 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
627 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
628 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
630 cleanup:
631 RegDeleteKeyA(test_key, "");
632 RegCloseKey(test_key);
635 static void test_query_value_ex(void)
637 DWORD ret;
638 DWORD size;
639 DWORD type;
640 BYTE buffer[10];
642 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
643 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
644 ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
645 ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
647 type = 0xdeadbeef;
648 size = 0xdeadbeef;
649 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
650 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
651 ok(size == 0, "size should have been set to 0 instead of %d\n", size);
653 size = sizeof(buffer);
654 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
655 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
656 ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
658 size = 4;
659 ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
660 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
663 static void test_get_value(void)
665 DWORD ret;
666 DWORD size;
667 DWORD type;
668 DWORD dw, qw[2];
669 CHAR buf[80];
670 CHAR expanded[] = "bar\\subdir1";
671 CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
673 if(!pRegGetValueA)
675 win_skip("RegGetValue not available on this platform\n");
676 return;
679 /* Invalid parameter */
680 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
681 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
683 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
684 size = type = dw = 0xdeadbeef;
685 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
686 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
687 ok(size == 4, "size=%d\n", size);
688 ok(type == REG_DWORD, "type=%d\n", type);
689 ok(dw == 0x12345678, "dw=%d\n", dw);
691 /* Query by subkey-name */
692 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
693 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
695 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
696 size = type = dw = 0xdeadbeef;
697 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
698 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
699 /* Although the function failed all values are retrieved */
700 ok(size == 4, "size=%d\n", size);
701 ok(type == REG_DWORD, "type=%d\n", type);
702 ok(dw == 0x12345678, "dw=%d\n", dw);
704 /* Test RRF_ZEROONFAILURE */
705 type = dw = 0xdeadbeef; size = 4;
706 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
707 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
708 /* Again all values are retrieved ... */
709 ok(size == 4, "size=%d\n", size);
710 ok(type == REG_DWORD, "type=%d\n", type);
711 /* ... except the buffer, which is zeroed out */
712 ok(dw == 0, "dw=%d\n", dw);
714 /* Test RRF_ZEROONFAILURE with a NULL buffer... */
715 type = size = 0xbadbeef;
716 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
717 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
718 ok(size == 4, "size=%d\n", size);
719 ok(type == REG_DWORD, "type=%d\n", type);
721 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
722 size = type = dw = 0xdeadbeef;
723 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
724 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
725 ok(size == 4, "size=%d\n", size);
726 ok(type == REG_DWORD, "type=%d\n", type);
727 ok(dw == 0x12345678, "dw=%d\n", dw);
729 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
730 size = type = dw = 0xdeadbeef;
731 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
732 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
733 ok(size == 4, "size=%d\n", size);
734 ok(type == REG_BINARY, "type=%d\n", type);
735 ok(dw == 0x12345678, "dw=%d\n", dw);
737 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
738 qw[0] = qw[1] = size = type = 0xdeadbeef;
739 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
740 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
741 ok(size == 8, "size=%d\n", size);
742 ok(type == REG_BINARY, "type=%d\n", type);
743 ok(qw[0] == 0x12345678 &&
744 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
746 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
747 type = dw = 0xdeadbeef; size = 4;
748 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
749 ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
750 ok(dw == 0xdeadbeef, "dw=%d\n", dw);
751 ok(size == 8, "size=%d\n", size);
753 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
754 qw[0] = qw[1] = size = type = 0xdeadbeef;
755 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
756 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
757 ok(size == 8, "size=%d\n", size);
758 ok(type == REG_BINARY, "type=%d\n", type);
759 ok(qw[0] == 0x12345678 &&
760 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
762 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
763 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
764 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
765 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
766 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
767 ok(type == REG_SZ, "type=%d\n", type);
768 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
770 /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
771 type = 0xdeadbeef; size = 0;
772 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
773 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
774 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
775 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
776 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
777 ok(type == REG_SZ, "type=%d\n", type);
779 /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
780 strcpy(buf, sTestpath1);
781 type = 0xdeadbeef;
782 size = sizeof(buf);
783 ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
784 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
785 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
786 ok(size == 0 ||
787 size == 1, /* win2k3 */
788 "size=%d\n", size);
789 ok(type == REG_SZ, "type=%d\n", type);
790 ok(!strcmp(sTestpath1, buf) ||
791 !strcmp(buf, ""),
792 "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
794 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
795 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
796 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
797 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
798 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
799 ok(type == REG_SZ, "type=%d\n", type);
800 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
802 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
803 size = 0;
804 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
805 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
806 ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
807 (size == strlen(expanded2)+2) || /* win2k3 SP2 */
808 (size == strlen(sTestpath2)+1),
809 "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
811 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
812 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
813 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
814 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
815 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
816 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
817 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
818 ok(type == REG_SZ, "type=%d\n", type);
819 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
821 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
822 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
823 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
824 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
825 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
826 ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
827 "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
828 ok(type == REG_SZ, "type=%d\n", type);
829 ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
831 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
832 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
833 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
834 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
835 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
836 ok(type == REG_EXPAND_SZ, "type=%d\n", type);
837 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
839 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
840 size = 0xbadbeef;
841 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
842 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
843 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
844 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
845 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
847 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
848 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
849 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
851 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
852 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
853 /* before win8: ERROR_INVALID_PARAMETER, win8: ERROR_UNSUPPORTED_TYPE */
854 ok(ret == ERROR_INVALID_PARAMETER || ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
856 /* Query REG_EXPAND_SZ using RRF_RT_ANY */
857 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
858 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
859 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
860 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
861 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
862 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
863 ok(type == REG_SZ, "type=%d\n", type);
864 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
867 static void test_reg_open_key(void)
869 DWORD ret = 0;
870 HKEY hkResult = NULL;
871 HKEY hkPreserve = NULL;
872 HKEY hkRoot64 = NULL;
873 HKEY hkRoot32 = NULL;
874 BOOL bRet;
875 SID_IDENTIFIER_AUTHORITY sid_authority = {SECURITY_WORLD_SID_AUTHORITY};
876 PSID world_sid;
877 EXPLICIT_ACCESSA access;
878 PACL key_acl;
879 SECURITY_DESCRIPTOR *sd;
881 /* successful open */
882 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
883 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
884 ok(hkResult != NULL, "expected hkResult != NULL\n");
885 hkPreserve = hkResult;
887 /* open same key twice */
888 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
889 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
890 ok(hkResult != hkPreserve, "expected hkResult != hkPreserve\n");
891 ok(hkResult != NULL, "hkResult != NULL\n");
892 RegCloseKey(hkResult);
894 /* open nonexistent key
895 * check that hkResult is set to NULL
897 hkResult = hkPreserve;
898 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
899 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
900 ok(hkResult == NULL, "expected hkResult == NULL\n");
902 /* open the same nonexistent key again to make sure the key wasn't created */
903 hkResult = hkPreserve;
904 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
905 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
906 ok(hkResult == NULL, "expected hkResult == NULL\n");
908 /* send in NULL lpSubKey
909 * check that hkResult receives the value of hKey
911 hkResult = hkPreserve;
912 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
913 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
914 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
916 /* send empty-string in lpSubKey */
917 hkResult = hkPreserve;
918 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
919 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
920 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
922 /* send in NULL lpSubKey and NULL hKey
923 * hkResult is set to NULL
925 hkResult = hkPreserve;
926 ret = RegOpenKeyA(NULL, NULL, &hkResult);
927 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
928 ok(hkResult == NULL, "expected hkResult == NULL\n");
930 /* only send NULL hKey
931 * the value of hkResult remains unchanged
933 hkResult = hkPreserve;
934 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
935 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
936 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
937 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
938 RegCloseKey(hkResult);
940 /* send in NULL hkResult */
941 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
942 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
944 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, NULL);
945 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
947 ret = RegOpenKeyA(NULL, NULL, NULL);
948 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
950 /* beginning backslash character */
951 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
952 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
953 broken(ret == ERROR_SUCCESS), /* wow64 */
954 "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
955 if (!ret) RegCloseKey(hkResult);
957 hkResult = NULL;
958 ret = RegOpenKeyExA(HKEY_CLASSES_ROOT, "\\clsid", 0, KEY_QUERY_VALUE, &hkResult);
959 ok(ret == ERROR_SUCCESS || /* 2k/XP */
960 ret == ERROR_BAD_PATHNAME, /* NT */
961 "expected ERROR_SUCCESS, ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
962 RegCloseKey(hkResult);
964 /* WOW64 flags */
965 hkResult = NULL;
966 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_32KEY, &hkResult);
967 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
968 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
969 RegCloseKey(hkResult);
971 hkResult = NULL;
972 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_64KEY, &hkResult);
973 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
974 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
975 RegCloseKey(hkResult);
977 /* check special HKEYs on 64bit
978 * only the lower 4 bytes of the supplied key are used
980 if (ptr_size == 64)
982 /* HKEY_CURRENT_USER */
983 ret = RegOpenKeyA(UlongToHandle(HandleToUlong(HKEY_CURRENT_USER)), "Software", &hkResult);
984 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
985 ok(hkResult != NULL, "expected hkResult != NULL\n");
986 RegCloseKey(hkResult);
988 ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)1 << 32), "Software", &hkResult);
989 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
990 ok(hkResult != NULL, "expected hkResult != NULL\n");
991 RegCloseKey(hkResult);
993 ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)0xdeadbeef << 32), "Software", &hkResult);
994 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
995 ok(hkResult != NULL, "expected hkResult != NULL\n");
996 RegCloseKey(hkResult);
998 ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)0xffffffff << 32), "Software", &hkResult);
999 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1000 ok(hkResult != NULL, "expected hkResult != NULL\n");
1001 RegCloseKey(hkResult);
1003 /* HKEY_LOCAL_MACHINE */
1004 ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_LOCAL_MACHINE) | (ULONG64)0xdeadbeef << 32), "Software", &hkResult);
1005 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1006 ok(hkResult != NULL, "expected hkResult != NULL\n");
1007 RegCloseKey(hkResult);
1010 /* Try using WOW64 flags when opening a key with a DACL set to verify that
1011 * the registry access check is performed correctly. Redirection isn't
1012 * being tested, so the tests don't care about whether the process is
1013 * running under WOW64. */
1014 if (!pIsWow64Process)
1016 win_skip("WOW64 flags are not recognized\n");
1017 return;
1020 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1021 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &hkRoot32, NULL);
1022 if (limited_user)
1023 ok(ret == ERROR_ACCESS_DENIED && hkRoot32 == NULL,
1024 "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1025 else
1026 ok(ret == ERROR_SUCCESS && hkRoot32 != NULL,
1027 "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1029 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1030 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &hkRoot64, NULL);
1031 if (limited_user)
1032 ok(ret == ERROR_ACCESS_DENIED && hkRoot64 == NULL,
1033 "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1034 else
1035 ok(ret == ERROR_SUCCESS && hkRoot64 != NULL,
1036 "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1038 bRet = AllocateAndInitializeSid(&sid_authority, 1, SECURITY_WORLD_RID,
1039 0, 0, 0, 0, 0, 0, 0, &world_sid);
1040 ok(bRet == TRUE,
1041 "Expected AllocateAndInitializeSid to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1043 access.grfAccessPermissions = GENERIC_ALL | STANDARD_RIGHTS_ALL;
1044 access.grfAccessMode = SET_ACCESS;
1045 access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
1046 access.Trustee.pMultipleTrustee = NULL;
1047 access.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
1048 access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
1049 access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
1050 access.Trustee.ptstrName = (char *)world_sid;
1052 ret = SetEntriesInAclA(1, &access, NULL, &key_acl);
1053 ok(ret == ERROR_SUCCESS,
1054 "Expected SetEntriesInAclA to return ERROR_SUCCESS, got %u, last error %u\n", ret, GetLastError());
1056 sd = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1057 bRet = InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION);
1058 ok(bRet == TRUE,
1059 "Expected InitializeSecurityDescriptor to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1061 bRet = SetSecurityDescriptorDacl(sd, TRUE, key_acl, FALSE);
1062 ok(bRet == TRUE,
1063 "Expected SetSecurityDescriptorDacl to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1065 if (limited_user)
1067 skip("not enough privileges to modify HKLM\n");
1069 else
1071 /* The "sanctioned" methods of setting a registry ACL aren't implemented in Wine. */
1072 bRet = SetKernelObjectSecurity(hkRoot64, DACL_SECURITY_INFORMATION, sd);
1073 ok(bRet == TRUE,
1074 "Expected SetKernelObjectSecurity to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1076 bRet = SetKernelObjectSecurity(hkRoot32, DACL_SECURITY_INFORMATION, sd);
1077 ok(bRet == TRUE,
1078 "Expected SetKernelObjectSecurity to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1080 hkResult = NULL;
1081 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, KEY_WOW64_64KEY | KEY_READ, &hkResult);
1082 ok(ret == ERROR_SUCCESS && hkResult != NULL,
1083 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1084 RegCloseKey(hkResult);
1086 hkResult = NULL;
1087 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, KEY_WOW64_32KEY | KEY_READ, &hkResult);
1088 ok(ret == ERROR_SUCCESS && hkResult != NULL,
1089 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1090 RegCloseKey(hkResult);
1093 HeapFree(GetProcessHeap(), 0, sd);
1094 LocalFree(key_acl);
1095 FreeSid(world_sid);
1096 RegDeleteKeyA(hkRoot64, "");
1097 RegCloseKey(hkRoot64);
1098 RegDeleteKeyA(hkRoot32, "");
1099 RegCloseKey(hkRoot32);
1102 static void test_reg_create_key(void)
1104 LONG ret;
1105 HKEY hkey1, hkey2;
1106 HKEY hkRoot64 = NULL;
1107 HKEY hkRoot32 = NULL;
1108 DWORD dwRet;
1109 BOOL bRet;
1110 SID_IDENTIFIER_AUTHORITY sid_authority = {SECURITY_WORLD_SID_AUTHORITY};
1111 PSID world_sid;
1112 EXPLICIT_ACCESSA access;
1113 PACL key_acl;
1114 SECURITY_DESCRIPTOR *sd;
1116 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
1117 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1118 /* should succeed: all versions of Windows ignore the access rights
1119 * to the parent handle */
1120 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
1121 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1123 /* clean up */
1124 RegDeleteKeyA(hkey2, "");
1125 RegDeleteKeyA(hkey1, "");
1126 RegCloseKey(hkey2);
1127 RegCloseKey(hkey1);
1129 /* test creation of volatile keys */
1130 ret = RegCreateKeyExA(hkey_main, "Volatile", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey1, NULL);
1131 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1132 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1133 ok(ret == ERROR_CHILD_MUST_BE_VOLATILE, "RegCreateKeyExA failed with error %d\n", ret);
1134 if (!ret) RegCloseKey( hkey2 );
1135 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1136 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1137 RegCloseKey(hkey2);
1138 /* should succeed if the key already exists */
1139 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1140 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1142 /* clean up */
1143 RegDeleteKeyA(hkey2, "");
1144 RegDeleteKeyA(hkey1, "");
1145 RegCloseKey(hkey2);
1146 RegCloseKey(hkey1);
1148 /* beginning backslash character */
1149 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
1150 if (!(GetVersion() & 0x80000000))
1151 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
1152 else {
1153 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1154 RegDeleteKeyA(hkey1, NULL);
1155 RegCloseKey(hkey1);
1158 /* WOW64 flags - open an existing key */
1159 hkey1 = NULL;
1160 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &hkey1, NULL);
1161 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1162 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1163 RegCloseKey(hkey1);
1165 hkey1 = NULL;
1166 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_64KEY, NULL, &hkey1, NULL);
1167 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1168 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1169 RegCloseKey(hkey1);
1171 /* Try using WOW64 flags when opening a key with a DACL set to verify that
1172 * the registry access check is performed correctly. Redirection isn't
1173 * being tested, so the tests don't care about whether the process is
1174 * running under WOW64. */
1175 if (!pIsWow64Process)
1177 win_skip("WOW64 flags are not recognized\n");
1178 return;
1181 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1182 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &hkRoot32, NULL);
1183 if (limited_user)
1184 ok(ret == ERROR_ACCESS_DENIED && hkRoot32 == NULL,
1185 "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%d)\n", ret);
1186 else
1187 ok(ret == ERROR_SUCCESS && hkRoot32 != NULL,
1188 "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%d)\n", ret);
1190 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1191 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &hkRoot64, NULL);
1192 if (limited_user)
1193 ok(ret == ERROR_ACCESS_DENIED && hkRoot64 == NULL,
1194 "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%d)\n", ret);
1195 else
1196 ok(ret == ERROR_SUCCESS && hkRoot64 != NULL,
1197 "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%d)\n", ret);
1199 bRet = AllocateAndInitializeSid(&sid_authority, 1, SECURITY_WORLD_RID,
1200 0, 0, 0, 0, 0, 0, 0, &world_sid);
1201 ok(bRet == TRUE,
1202 "Expected AllocateAndInitializeSid to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1204 access.grfAccessPermissions = GENERIC_ALL | STANDARD_RIGHTS_ALL;
1205 access.grfAccessMode = SET_ACCESS;
1206 access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
1207 access.Trustee.pMultipleTrustee = NULL;
1208 access.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
1209 access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
1210 access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
1211 access.Trustee.ptstrName = (char *)world_sid;
1213 dwRet = SetEntriesInAclA(1, &access, NULL, &key_acl);
1214 ok(dwRet == ERROR_SUCCESS,
1215 "Expected SetEntriesInAclA to return ERROR_SUCCESS, got %u, last error %u\n", dwRet, GetLastError());
1217 sd = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1218 bRet = InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION);
1219 ok(bRet == TRUE,
1220 "Expected InitializeSecurityDescriptor to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1222 bRet = SetSecurityDescriptorDacl(sd, TRUE, key_acl, FALSE);
1223 ok(bRet == TRUE,
1224 "Expected SetSecurityDescriptorDacl to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1226 if (limited_user)
1228 skip("not enough privileges to modify HKLM\n");
1230 else
1232 /* The "sanctioned" methods of setting a registry ACL aren't implemented in Wine. */
1233 bRet = SetKernelObjectSecurity(hkRoot64, DACL_SECURITY_INFORMATION, sd);
1234 ok(bRet == TRUE,
1235 "Expected SetKernelObjectSecurity to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1237 bRet = SetKernelObjectSecurity(hkRoot32, DACL_SECURITY_INFORMATION, sd);
1238 ok(bRet == TRUE,
1239 "Expected SetKernelObjectSecurity to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1241 hkey1 = NULL;
1242 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1243 KEY_WOW64_64KEY | KEY_READ, NULL, &hkey1, NULL);
1244 ok(ret == ERROR_SUCCESS && hkey1 != NULL,
1245 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1246 RegCloseKey(hkey1);
1248 hkey1 = NULL;
1249 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1250 KEY_WOW64_32KEY | KEY_READ, NULL, &hkey1, NULL);
1251 ok(ret == ERROR_SUCCESS && hkey1 != NULL,
1252 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1253 RegCloseKey(hkey1);
1256 HeapFree(GetProcessHeap(), 0, sd);
1257 LocalFree(key_acl);
1258 FreeSid(world_sid);
1259 RegDeleteKeyA(hkRoot64, "");
1260 RegCloseKey(hkRoot64);
1261 RegDeleteKeyA(hkRoot32, "");
1262 RegCloseKey(hkRoot32);
1265 static void test_reg_close_key(void)
1267 DWORD ret = 0;
1268 HKEY hkHandle;
1270 /* successfully close key
1271 * hkHandle remains changed after call to RegCloseKey
1273 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
1274 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1275 ret = RegCloseKey(hkHandle);
1276 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1278 /* try to close the key twice */
1279 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
1280 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
1281 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
1283 /* try to close a NULL handle */
1284 ret = RegCloseKey(NULL);
1285 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1286 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1288 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1289 * win98 doesn't give a new handle when the same key is opened.
1290 * Not re-opening will make some next tests fail.
1292 if (hkey_main == hkHandle)
1294 trace("The main handle is most likely closed, so re-opening\n");
1295 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1299 static void test_reg_delete_key(void)
1301 DWORD ret;
1303 ret = RegDeleteKeyA(hkey_main, NULL);
1305 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1306 * there are also no subkeys available it will delete the key pointed to by hkey_main.
1307 * Not re-creating will make some next tests fail.
1309 if (ret == ERROR_SUCCESS)
1311 trace("We are probably running on NT4 or W2K as the main key is deleted,"
1312 " re-creating the main key\n");
1313 setup_main_key();
1315 else
1316 ok(ret == ERROR_INVALID_PARAMETER ||
1317 ret == ERROR_ACCESS_DENIED ||
1318 ret == ERROR_BADKEY, /* Win95 */
1319 "ret=%d\n", ret);
1322 static void test_reg_save_key(void)
1324 DWORD ret;
1326 ret = RegSaveKeyA(hkey_main, "saved_key", NULL);
1327 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1330 static void test_reg_load_key(void)
1332 DWORD ret;
1333 HKEY hkHandle;
1335 ret = RegLoadKeyA(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1336 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1338 ret = RegOpenKeyA(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1339 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1341 RegCloseKey(hkHandle);
1344 static void test_reg_unload_key(void)
1346 DWORD ret;
1348 ret = RegUnLoadKeyA(HKEY_LOCAL_MACHINE, "Test");
1349 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1351 DeleteFileA("saved_key");
1352 DeleteFileA("saved_key.LOG");
1355 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1357 TOKEN_PRIVILEGES tp;
1358 HANDLE hToken;
1359 LUID luid;
1361 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1362 return FALSE;
1364 if(!LookupPrivilegeValueA(NULL, privilege, &luid))
1366 CloseHandle(hToken);
1367 return FALSE;
1370 tp.PrivilegeCount = 1;
1371 tp.Privileges[0].Luid = luid;
1373 if (set)
1374 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1375 else
1376 tp.Privileges[0].Attributes = 0;
1378 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1379 if (GetLastError() != ERROR_SUCCESS)
1381 CloseHandle(hToken);
1382 return FALSE;
1385 CloseHandle(hToken);
1386 return TRUE;
1389 /* tests that show that RegConnectRegistry and
1390 OpenSCManager accept computer names without the
1391 \\ prefix (what MSDN says). */
1392 static void test_regconnectregistry( void)
1394 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1395 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1396 DWORD len = sizeof(compName) ;
1397 BOOL ret;
1398 LONG retl;
1399 HKEY hkey;
1400 SC_HANDLE schnd;
1402 SetLastError(0xdeadbeef);
1403 ret = GetComputerNameA(compName, &len);
1404 ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1405 if( !ret) return;
1407 lstrcpyA(netwName, "\\\\");
1408 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1410 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1411 ok( !retl ||
1412 retl == ERROR_DLL_INIT_FAILED ||
1413 retl == ERROR_BAD_NETPATH, /* some win2k */
1414 "RegConnectRegistryA failed err = %d\n", retl);
1415 if( !retl) RegCloseKey( hkey);
1417 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1418 ok( !retl ||
1419 retl == ERROR_DLL_INIT_FAILED ||
1420 retl == ERROR_BAD_NETPATH, /* some win2k */
1421 "RegConnectRegistryA failed err = %d\n", retl);
1422 if( !retl) RegCloseKey( hkey);
1424 SetLastError(0xdeadbeef);
1425 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
1426 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1428 win_skip("OpenSCManagerA is not implemented\n");
1429 return;
1432 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1433 CloseServiceHandle( schnd);
1435 SetLastError(0xdeadbeef);
1436 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
1437 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1438 CloseServiceHandle( schnd);
1442 static void test_reg_query_value(void)
1444 HKEY subkey;
1445 CHAR val[MAX_PATH];
1446 WCHAR valW[5];
1447 LONG size, ret;
1449 static const WCHAR expected[] = {'d','a','t','a',0};
1451 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1452 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1454 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1455 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1457 /* try an invalid hkey */
1458 SetLastError(0xdeadbeef);
1459 size = MAX_PATH;
1460 ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1461 ok(ret == ERROR_INVALID_HANDLE ||
1462 ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1463 ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1464 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1465 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1467 /* try a NULL hkey */
1468 SetLastError(0xdeadbeef);
1469 size = MAX_PATH;
1470 ret = RegQueryValueA(NULL, "subkey", val, &size);
1471 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1472 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1473 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1475 /* try a NULL value */
1476 size = MAX_PATH;
1477 ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1478 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1479 ok(size == 5, "Expected 5, got %d\n", size);
1481 /* try a NULL size */
1482 SetLastError(0xdeadbeef);
1483 val[0] = '\0';
1484 ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1485 ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1486 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1487 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1489 /* try a NULL value and size */
1490 ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1491 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1493 /* try a size too small */
1494 SetLastError(0xdeadbeef);
1495 val[0] = '\0';
1496 size = 1;
1497 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1498 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1499 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1500 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1501 ok(size == 5, "Expected 5, got %d\n", size);
1503 /* successfully read the value using 'subkey' */
1504 size = MAX_PATH;
1505 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1506 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1507 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1508 ok(size == 5, "Expected 5, got %d\n", size);
1510 /* successfully read the value using the subkey key */
1511 size = MAX_PATH;
1512 ret = RegQueryValueA(subkey, NULL, val, &size);
1513 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1514 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1515 ok(size == 5, "Expected 5, got %d\n", size);
1517 /* unicode - try size too small */
1518 SetLastError(0xdeadbeef);
1519 valW[0] = '\0';
1520 size = 0;
1521 ret = RegQueryValueW(subkey, NULL, valW, &size);
1522 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1524 win_skip("RegQueryValueW is not implemented\n");
1525 goto cleanup;
1527 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1528 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1529 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1530 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1532 /* unicode - try size in WCHARS */
1533 SetLastError(0xdeadbeef);
1534 size = sizeof(valW) / sizeof(WCHAR);
1535 ret = RegQueryValueW(subkey, NULL, valW, &size);
1536 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1537 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1538 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1539 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1541 /* unicode - successfully read the value */
1542 size = sizeof(valW);
1543 ret = RegQueryValueW(subkey, NULL, valW, &size);
1544 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1545 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1546 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1548 /* unicode - set the value without a NULL terminator */
1549 ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1550 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1552 /* unicode - read the unterminated value, value is terminated for us */
1553 memset(valW, 'a', sizeof(valW));
1554 size = sizeof(valW);
1555 ret = RegQueryValueW(subkey, NULL, valW, &size);
1556 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1557 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1558 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1560 cleanup:
1561 RegDeleteKeyA(subkey, "");
1562 RegCloseKey(subkey);
1565 static void test_string_termination(void)
1567 HKEY subkey;
1568 LSTATUS ret;
1569 static const char string[] = "FullString";
1570 char name[11];
1571 BYTE buffer[11];
1572 DWORD insize, outsize, nsize;
1574 ret = RegCreateKeyA(hkey_main, "string_termination", &subkey);
1575 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1577 /* Off-by-one RegSetValueExA -> adds a trailing '\0'! */
1578 insize=sizeof(string)-1;
1579 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1580 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1581 outsize=insize;
1582 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1583 ok(ret == ERROR_MORE_DATA, "RegQueryValueExA returned: %d\n", ret);
1585 /* Off-by-two RegSetValueExA -> no trailing '\0' */
1586 insize=sizeof(string)-2;
1587 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1588 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1589 outsize=0;
1590 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, NULL, &outsize);
1591 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1592 ok(outsize == insize, "wrong size %u != %u\n", outsize, insize);
1594 /* RegQueryValueExA may return a string with no trailing '\0' */
1595 outsize=insize;
1596 memset(buffer, 0xbd, sizeof(buffer));
1597 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1598 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1599 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1600 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1601 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1602 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1604 /* RegQueryValueExA adds a trailing '\0' if there is room */
1605 outsize=insize+1;
1606 memset(buffer, 0xbd, sizeof(buffer));
1607 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1608 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1609 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1610 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1611 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1612 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1614 /* RegEnumValueA may return a string with no trailing '\0' */
1615 outsize=insize;
1616 memset(buffer, 0xbd, sizeof(buffer));
1617 nsize=sizeof(name);
1618 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1619 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1620 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1621 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1622 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1623 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1624 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1626 /* RegEnumValueA adds a trailing '\0' if there is room */
1627 outsize=insize+1;
1628 memset(buffer, 0xbd, sizeof(buffer));
1629 nsize=sizeof(name);
1630 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1631 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1632 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1633 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1634 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1635 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1636 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1638 RegDeleteKeyA(subkey, "");
1639 RegCloseKey(subkey);
1642 static void test_reg_delete_tree(void)
1644 CHAR buffer[MAX_PATH];
1645 HKEY subkey, subkey2;
1646 LONG size, ret;
1648 if(!pRegDeleteTreeA) {
1649 win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1650 return;
1653 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1654 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1655 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1656 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1657 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1658 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1659 ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1660 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1661 ret = RegCloseKey(subkey2);
1662 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1664 ret = pRegDeleteTreeA(subkey, "subkey2");
1665 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1666 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1667 "subkey2 was not deleted\n");
1668 size = MAX_PATH;
1669 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1670 "Default value of subkey not longer present\n");
1672 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1673 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1674 ret = RegCloseKey(subkey2);
1675 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1676 ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1677 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1678 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1679 "subkey2 was not deleted\n");
1680 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1681 "Default value of subkey not longer present\n");
1683 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1684 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1685 ret = RegCloseKey(subkey2);
1686 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1687 ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1688 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1689 ret = RegCloseKey(subkey2);
1690 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1691 ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1692 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1693 ret = pRegDeleteTreeA(subkey, NULL);
1694 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1695 ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1696 "subkey was deleted\n");
1697 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1698 "subkey2 was not deleted\n");
1699 ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1700 "subkey3 was not deleted\n");
1701 size = MAX_PATH;
1702 ret = RegQueryValueA(subkey, NULL, buffer, &size);
1703 ok(ret == ERROR_SUCCESS,
1704 "Default value of subkey is not present\n");
1705 ok(!lstrlenA(buffer),
1706 "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1707 size = MAX_PATH;
1708 ok(RegQueryValueA(subkey, "value", buffer, &size),
1709 "Value is still present\n");
1711 ret = pRegDeleteTreeA(hkey_main, "not-here");
1712 ok(ret == ERROR_FILE_NOT_FOUND,
1713 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1716 static void test_rw_order(void)
1718 HKEY hKey;
1719 DWORD dw = 0;
1720 static const char keyname[] = "test_rw_order";
1721 char value_buf[2];
1722 DWORD values, value_len, value_name_max_len;
1723 LSTATUS ret;
1725 RegDeleteKeyA(HKEY_CURRENT_USER, keyname);
1726 ret = RegCreateKeyA(HKEY_CURRENT_USER, keyname, &hKey);
1727 if(ret != ERROR_SUCCESS) {
1728 skip("Couldn't create key. Skipping.\n");
1729 return;
1732 ok(!RegSetValueExA(hKey, "A", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1733 "RegSetValueExA for value \"A\" failed\n");
1734 ok(!RegSetValueExA(hKey, "C", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1735 "RegSetValueExA for value \"C\" failed\n");
1736 ok(!RegSetValueExA(hKey, "D", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1737 "RegSetValueExA for value \"D\" failed\n");
1738 ok(!RegSetValueExA(hKey, "B", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1739 "RegSetValueExA for value \"B\" failed\n");
1741 ok(!RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &values,
1742 &value_name_max_len, NULL, NULL, NULL), "RegQueryInfoKeyA failed\n");
1743 ok(values == 4, "Expected 4 values, got %u\n", values);
1745 /* Value enumeration preserves RegSetValueEx call order */
1746 value_len = 2;
1747 ok(!RegEnumValueA(hKey, 0, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1748 ok(strcmp(value_buf, "A") == 0, "Expected name \"A\", got %s\n", value_buf);
1749 value_len = 2;
1750 ok(!RegEnumValueA(hKey, 1, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1751 todo_wine ok(strcmp(value_buf, "C") == 0, "Expected name \"C\", got %s\n", value_buf);
1752 value_len = 2;
1753 ok(!RegEnumValueA(hKey, 2, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1754 todo_wine ok(strcmp(value_buf, "D") == 0, "Expected name \"D\", got %s\n", value_buf);
1755 value_len = 2;
1756 ok(!RegEnumValueA(hKey, 3, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1757 todo_wine ok(strcmp(value_buf, "B") == 0, "Expected name \"B\", got %s\n", value_buf);
1759 ok(!RegDeleteKeyA(HKEY_CURRENT_USER, keyname), "Failed to delete key\n");
1762 static void test_symlinks(void)
1764 static const WCHAR targetW[] = {'\\','S','o','f','t','w','a','r','e','\\','W','i','n','e',
1765 '\\','T','e','s','t','\\','t','a','r','g','e','t',0};
1766 BYTE buffer[1024];
1767 UNICODE_STRING target_str;
1768 WCHAR *target;
1769 HKEY key, link;
1770 NTSTATUS status;
1771 DWORD target_len, type, len, dw, err;
1773 if (!pRtlFormatCurrentUserKeyPath || !pNtDeleteKey)
1775 win_skip( "Can't perform symlink tests\n" );
1776 return;
1779 pRtlFormatCurrentUserKeyPath( &target_str );
1781 target_len = target_str.Length + sizeof(targetW);
1782 target = HeapAlloc( GetProcessHeap(), 0, target_len );
1783 memcpy( target, target_str.Buffer, target_str.Length );
1784 memcpy( target + target_str.Length/sizeof(WCHAR), targetW, sizeof(targetW) );
1786 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1787 KEY_ALL_ACCESS, NULL, &link, NULL );
1788 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed: %u\n", err );
1790 /* REG_SZ is not allowed */
1791 err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_SZ, (BYTE *)"foobar", sizeof("foobar") );
1792 ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1793 err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_LINK,
1794 (BYTE *)target, target_len - sizeof(WCHAR) );
1795 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1796 /* other values are not allowed */
1797 err = RegSetValueExA( link, "link", 0, REG_LINK, (BYTE *)target, target_len - sizeof(WCHAR) );
1798 ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1800 /* try opening the target through the link */
1802 err = RegOpenKeyA( hkey_main, "link", &key );
1803 ok( err == ERROR_FILE_NOT_FOUND, "RegOpenKey wrong error %u\n", err );
1805 err = RegCreateKeyExA( hkey_main, "target", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1806 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1808 dw = 0xbeef;
1809 err = RegSetValueExA( key, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1810 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1811 RegCloseKey( key );
1813 err = RegOpenKeyA( hkey_main, "link", &key );
1814 ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1816 len = sizeof(buffer);
1817 err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1818 ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1819 ok( len == sizeof(DWORD), "wrong len %u\n", len );
1821 len = sizeof(buffer);
1822 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1823 ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1825 /* REG_LINK can be created in non-link keys */
1826 err = RegSetValueExA( key, "SymbolicLinkValue", 0, REG_LINK,
1827 (BYTE *)target, target_len - sizeof(WCHAR) );
1828 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1829 len = sizeof(buffer);
1830 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1831 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1832 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1833 err = RegDeleteValueA( key, "SymbolicLinkValue" );
1834 ok( err == ERROR_SUCCESS, "RegDeleteValue failed error %u\n", err );
1836 RegCloseKey( key );
1838 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1839 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1841 len = sizeof(buffer);
1842 err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1843 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1844 ok( len == sizeof(DWORD), "wrong len %u\n", len );
1846 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1847 ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1848 RegCloseKey( key );
1850 /* now open the symlink itself */
1852 err = RegOpenKeyExA( hkey_main, "link", REG_OPTION_OPEN_LINK, KEY_ALL_ACCESS, &key );
1853 ok( err == ERROR_SUCCESS, "RegOpenKeyEx failed error %u\n", err );
1854 len = sizeof(buffer);
1855 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1856 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1857 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1858 RegCloseKey( key );
1860 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_OPEN_LINK,
1861 KEY_ALL_ACCESS, NULL, &key, NULL );
1862 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1863 len = sizeof(buffer);
1864 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1865 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1866 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1867 RegCloseKey( key );
1869 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1870 KEY_ALL_ACCESS, NULL, &key, NULL );
1871 ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1873 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK | REG_OPTION_OPEN_LINK,
1874 KEY_ALL_ACCESS, NULL, &key, NULL );
1875 ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1877 err = RegDeleteKeyA( hkey_main, "target" );
1878 ok( err == ERROR_SUCCESS, "RegDeleteKey failed error %u\n", err );
1880 err = RegDeleteKeyA( hkey_main, "link" );
1881 ok( err == ERROR_FILE_NOT_FOUND, "RegDeleteKey wrong error %u\n", err );
1883 status = pNtDeleteKey( link );
1884 ok( !status, "NtDeleteKey failed: 0x%08x\n", status );
1885 RegCloseKey( link );
1887 HeapFree( GetProcessHeap(), 0, target );
1888 pRtlFreeUnicodeString( &target_str );
1891 static DWORD get_key_value( HKEY root, const char *name, DWORD flags )
1893 HKEY key;
1894 DWORD err, type, dw, len = sizeof(dw);
1896 err = RegCreateKeyExA( root, name, 0, NULL, 0, flags | KEY_ALL_ACCESS, NULL, &key, NULL );
1897 if (err == ERROR_FILE_NOT_FOUND) return 0;
1898 ok( err == ERROR_SUCCESS, "%08x: RegCreateKeyEx failed: %u\n", flags, err );
1900 err = RegQueryValueExA( key, "value", NULL, &type, (BYTE *)&dw, &len );
1901 if (err == ERROR_FILE_NOT_FOUND)
1902 dw = 0;
1903 else
1904 ok( err == ERROR_SUCCESS, "%08x: RegQueryValueEx failed: %u\n", flags, err );
1905 RegCloseKey( key );
1906 return dw;
1909 static void _check_key_value( int line, HANDLE root, const char *name, DWORD flags, DWORD expect )
1911 DWORD dw = get_key_value( root, name, flags );
1912 ok_(__FILE__,line)( dw == expect, "%08x: wrong value %u/%u\n", flags, dw, expect );
1914 #define check_key_value(root,name,flags,expect) _check_key_value( __LINE__, root, name, flags, expect )
1916 static void test_redirection(void)
1918 DWORD err, type, dw, len;
1919 HKEY key, root32, root64, key32, key64, native, op_key;
1920 BOOL is_vista = FALSE;
1921 REGSAM opposite = (sizeof(void*) == 8 ? KEY_WOW64_32KEY : KEY_WOW64_64KEY);
1923 if (ptr_size != 64)
1925 BOOL is_wow64;
1926 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 ) || !is_wow64)
1928 skip( "Not on Wow64, no redirection\n" );
1929 return;
1933 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1934 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &root64, NULL );
1935 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1937 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1938 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &root32, NULL );
1939 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1941 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, NULL, 0,
1942 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key64, NULL );
1943 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1945 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, NULL, 0,
1946 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key32, NULL );
1947 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1949 dw = 64;
1950 err = RegSetValueExA( key64, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1951 ok( err == ERROR_SUCCESS, "RegSetValueExA failed: %u\n", err );
1953 dw = 32;
1954 err = RegSetValueExA( key32, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1955 ok( err == ERROR_SUCCESS, "RegSetValueExA failed: %u\n", err );
1957 dw = 0;
1958 len = sizeof(dw);
1959 err = RegQueryValueExA( key32, "value", NULL, &type, (BYTE *)&dw, &len );
1960 ok( err == ERROR_SUCCESS, "RegQueryValueExA failed: %u\n", err );
1961 ok( dw == 32, "wrong value %u\n", dw );
1963 dw = 0;
1964 len = sizeof(dw);
1965 err = RegQueryValueExA( key64, "value", NULL, &type, (BYTE *)&dw, &len );
1966 ok( err == ERROR_SUCCESS, "RegQueryValueExA failed: %u\n", err );
1967 ok( dw == 64, "wrong value %u\n", dw );
1969 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
1970 KEY_ALL_ACCESS, NULL, &key, NULL );
1971 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1973 if (ptr_size == 32)
1975 /* the Vista mechanism allows opening Wow6432Node from a 32-bit key too */
1976 /* the new (and simpler) Win7 mechanism doesn't */
1977 if (get_key_value( key, "Wow6432Node\\Wine\\Winetest", 0 ) == 32)
1979 trace( "using Vista-style Wow6432Node handling\n" );
1980 is_vista = TRUE;
1982 check_key_value( key, "Wine\\Winetest", 0, 32 );
1983 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1984 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1985 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, is_vista ? 32 : 0 );
1986 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 0 );
1987 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, is_vista ? 32 : 0 );
1989 else
1991 if (get_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY ) == 64)
1993 trace( "using Vista-style Wow6432Node handling\n" );
1994 is_vista = TRUE;
1996 check_key_value( key, "Wine\\Winetest", 0, 64 );
1997 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
1999 RegCloseKey( key );
2001 if (ptr_size == 32)
2003 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
2004 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2005 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2006 dw = get_key_value( key, "Wine\\Winetest", 0 );
2007 ok( dw == 64 || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
2008 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, 64 );
2009 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2010 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
2011 dw = get_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY );
2012 ok( dw == 32 || broken(dw == 64) /* xp64 */, "wrong value %u\n", dw );
2013 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2014 RegCloseKey( key );
2016 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
2017 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2018 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2019 check_key_value( key, "Wine\\Winetest", 0, 32 );
2020 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2021 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2022 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, is_vista ? 32 : 0 );
2023 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 0 );
2024 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, is_vista ? 32 : 0 );
2025 RegCloseKey( key );
2028 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, ptr_size );
2029 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", 0, 32 );
2030 if (ptr_size == 64)
2032 /* KEY_WOW64 flags have no effect on 64-bit */
2033 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_64KEY, 64 );
2034 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2035 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2036 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2038 else
2040 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_64KEY, 64 );
2041 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2042 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2043 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2046 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
2047 KEY_ALL_ACCESS, NULL, &key, NULL );
2048 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2049 check_key_value( key, "Wine\\Winetest", 0, 32 );
2050 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2051 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2052 RegCloseKey( key );
2054 if (ptr_size == 32)
2056 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
2057 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2058 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2059 dw = get_key_value( key, "Wine\\Winetest", 0 );
2060 ok( dw == (is_vista ? 64 : 32) || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
2061 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2062 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2063 RegCloseKey( key );
2065 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
2066 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2067 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2068 check_key_value( key, "Wine\\Winetest", 0, 32 );
2069 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2070 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2071 RegCloseKey( key );
2074 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2075 KEY_ALL_ACCESS, NULL, &key, NULL );
2076 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2077 check_key_value( key, "Winetest", 0, 32 );
2078 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2079 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2080 RegCloseKey( key );
2082 if (ptr_size == 32)
2084 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2085 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2086 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2087 dw = get_key_value( key, "Winetest", 0 );
2088 ok( dw == 32 || (is_vista && dw == 64), "wrong value %u\n", dw );
2089 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2090 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2091 RegCloseKey( key );
2093 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2094 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2095 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2096 check_key_value( key, "Winetest", 0, 32 );
2097 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2098 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2099 RegCloseKey( key );
2102 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2103 KEY_ALL_ACCESS, NULL, &key, NULL );
2104 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2105 check_key_value( key, "Winetest", 0, ptr_size );
2106 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : ptr_size );
2107 dw = get_key_value( key, "Winetest", KEY_WOW64_32KEY );
2108 if (ptr_size == 32) ok( dw == 32, "wrong value %u\n", dw );
2109 else todo_wine ok( dw == 32, "wrong value %u\n", dw );
2110 RegCloseKey( key );
2112 if (ptr_size == 32)
2114 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2115 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2116 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2117 dw = get_key_value( key, "Winetest", 0 );
2118 ok( dw == 64 || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
2119 check_key_value( key, "Winetest", KEY_WOW64_64KEY, 64 );
2120 dw = get_key_value( key, "Winetest", KEY_WOW64_32KEY );
2121 todo_wine ok( dw == 32, "wrong value %u\n", dw );
2122 RegCloseKey( key );
2124 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2125 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2126 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2127 check_key_value( key, "Winetest", 0, 32 );
2128 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2129 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2130 RegCloseKey( key );
2133 if (pRegDeleteKeyExA)
2135 err = pRegDeleteKeyExA( key32, "", KEY_WOW64_32KEY, 0 );
2136 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2137 err = pRegDeleteKeyExA( key64, "", KEY_WOW64_64KEY, 0 );
2138 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2139 pRegDeleteKeyExA( key64, "", KEY_WOW64_64KEY, 0 );
2140 pRegDeleteKeyExA( root64, "", KEY_WOW64_64KEY, 0 );
2142 else
2144 err = RegDeleteKeyA( key32, "" );
2145 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2146 err = RegDeleteKeyA( key64, "" );
2147 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2148 RegDeleteKeyA( key64, "" );
2149 RegDeleteKeyA( root64, "" );
2151 RegCloseKey( key32 );
2152 RegCloseKey( key64 );
2153 RegCloseKey( root32 );
2154 RegCloseKey( root64 );
2156 /* open key in native bit mode */
2157 err = RegOpenKeyExA(HKEY_CLASSES_ROOT, "Interface", 0, KEY_ALL_ACCESS, &native);
2158 ok(err == ERROR_SUCCESS, "got %i\n", err);
2160 pRegDeleteKeyExA(native, "AWineTest", 0, 0);
2162 /* write subkey in opposite bit mode */
2163 err = RegOpenKeyExA(HKEY_CLASSES_ROOT, "Interface", 0, KEY_ALL_ACCESS | opposite, &op_key);
2164 ok(err == ERROR_SUCCESS, "got %i\n", err);
2166 err = RegCreateKeyExA(op_key, "AWineTest", 0, NULL, 0, KEY_ALL_ACCESS | opposite,
2167 NULL, &key, NULL);
2168 ok(err == ERROR_SUCCESS || err == ERROR_ACCESS_DENIED, "got %i\n", err);
2169 if(err != ERROR_SUCCESS){
2170 win_skip("Can't write to registry\n");
2171 RegCloseKey(op_key);
2172 RegCloseKey(native);
2173 return;
2175 RegCloseKey(key);
2177 /* verify subkey is not present in native mode */
2178 err = RegOpenKeyExA(native, "AWineTest", 0, KEY_ALL_ACCESS, &key);
2179 ok(err == ERROR_FILE_NOT_FOUND ||
2180 broken(err == ERROR_SUCCESS), /* before Win7, HKCR is reflected instead of redirected */
2181 "got %i\n", err);
2183 err = pRegDeleteKeyExA(op_key, "AWineTest", opposite, 0);
2184 ok(err == ERROR_SUCCESS, "got %i\n", err);
2186 RegCloseKey(op_key);
2187 RegCloseKey(native);
2190 static void test_classesroot(void)
2192 HKEY hkey, hklm, hkcr, hkeysub1, hklmsub1, hkcrsub1, hklmsub2, hkcrsub2;
2193 DWORD size = 8;
2194 DWORD type = REG_SZ;
2195 static CHAR buffer[8];
2196 LONG res;
2198 /* create a key in the user's classes */
2199 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", &hkey ))
2201 delete_key( hkey );
2202 RegCloseKey( hkey );
2204 res = RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2205 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkey, NULL );
2206 if (res == ERROR_ACCESS_DENIED)
2208 skip("not enough privileges to add a user class\n");
2209 return;
2211 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2213 /* try to open that key in hkcr */
2214 res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2215 KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2216 todo_wine ok(res == ERROR_SUCCESS ||
2217 broken(res == ERROR_FILE_NOT_FOUND /* WinNT */),
2218 "test key not found in hkcr: %d\n", res);
2219 if (res)
2221 skip("HKCR key merging not supported\n");
2222 delete_key( hkey );
2223 RegCloseKey( hkey );
2224 return;
2227 todo_wine ok(IS_HKCR(hkcr), "hkcr mask not set in %p\n", hkcr);
2229 /* set a value in user's classes */
2230 res = RegSetValueExA(hkey, "val1", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2231 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2233 /* try to find the value in hkcr */
2234 res = RegQueryValueExA(hkcr, "val1", NULL, &type, (LPBYTE)buffer, &size);
2235 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2236 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2238 /* modify the value in hkcr */
2239 res = RegSetValueExA(hkcr, "val1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2240 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2242 /* check if the value is also modified in user's classes */
2243 res = RegQueryValueExA(hkey, "val1", NULL, &type, (LPBYTE)buffer, &size);
2244 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2245 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2247 /* set a value in hkcr */
2248 res = RegSetValueExA(hkcr, "val0", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2249 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2251 /* try to find the value in user's classes */
2252 res = RegQueryValueExA(hkey, "val0", NULL, &type, (LPBYTE)buffer, &size);
2253 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2254 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2256 /* modify the value in user's classes */
2257 res = RegSetValueExA(hkey, "val0", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2258 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2260 /* check if the value is also modified in hkcr */
2261 res = RegQueryValueExA(hkcr, "val0", NULL, &type, (LPBYTE)buffer, &size);
2262 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2263 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2265 /* cleanup */
2266 delete_key( hkey );
2267 delete_key( hkcr );
2268 RegCloseKey( hkey );
2269 RegCloseKey( hkcr );
2271 /* create a key in the hklm classes */
2272 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", &hklm ))
2274 delete_key( hklm );
2275 RegCloseKey( hklm );
2277 res = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", 0, NULL, REG_OPTION_NON_VOLATILE,
2278 KEY_ALL_ACCESS, NULL, &hklm, NULL );
2279 if (res == ERROR_ACCESS_DENIED)
2281 skip("not enough privileges to add a system class\n");
2282 return;
2284 ok(!IS_HKCR(hklm), "hkcr mask set in %p\n", hklm);
2286 /* try to open that key in hkcr */
2287 res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2288 KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2289 ok(res == ERROR_SUCCESS,
2290 "test key not found in hkcr: %d\n", res);
2291 ok(IS_HKCR(hkcr), "hkcr mask not set in %p\n", hkcr);
2292 if (res)
2294 delete_key( hklm );
2295 RegCloseKey( hklm );
2296 return;
2299 /* set a value in hklm classes */
2300 res = RegSetValueExA(hklm, "val2", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2301 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2303 /* try to find the value in hkcr */
2304 res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2305 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2306 ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2308 /* modify the value in hkcr */
2309 res = RegSetValueExA(hkcr, "val2", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2310 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2312 /* check that the value is modified in hklm classes */
2313 res = RegQueryValueExA(hklm, "val2", NULL, &type, (LPBYTE)buffer, &size);
2314 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2315 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2317 if (RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2318 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkey, NULL )) return;
2319 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2321 /* try to open that key in hkcr */
2322 res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2323 KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2324 ok(res == ERROR_SUCCESS,
2325 "test key not found in hkcr: %d\n", res);
2326 ok(IS_HKCR(hkcr), "hkcr mask not set in %p\n", hkcr);
2328 /* set a value in user's classes */
2329 res = RegSetValueExA(hkey, "val2", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2330 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2332 /* try to find the value in hkcr */
2333 res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2334 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2335 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2337 /* modify the value in hklm */
2338 res = RegSetValueExA(hklm, "val2", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2339 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2341 /* check that the value is not overwritten in hkcr or user's classes */
2342 res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2343 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2344 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2345 res = RegQueryValueExA(hkey, "val2", NULL, &type, (LPBYTE)buffer, &size);
2346 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2347 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2349 /* modify the value in hkcr */
2350 res = RegSetValueExA(hkcr, "val2", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2351 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2353 /* check that the value is overwritten in hklm and user's classes */
2354 res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2355 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2356 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2357 res = RegQueryValueExA(hkey, "val2", NULL, &type, (LPBYTE)buffer, &size);
2358 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2359 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2361 /* create a subkey in hklm */
2362 if (RegCreateKeyExA( hklm, "subkey1", 0, NULL, 0,
2363 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hklmsub1, NULL )) return;
2364 ok(!IS_HKCR(hklmsub1), "hkcr mask set in %p\n", hklmsub1);
2365 /* try to open that subkey in hkcr */
2366 res = RegOpenKeyExA( hkcr, "subkey1", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcrsub1 );
2367 ok(res == ERROR_SUCCESS, "test key not found in hkcr: %d\n", res);
2368 ok(IS_HKCR(hkcrsub1), "hkcr mask not set in %p\n", hkcrsub1);
2370 /* set a value in hklm classes */
2371 res = RegSetValueExA(hklmsub1, "subval1", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2372 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2374 /* try to find the value in hkcr */
2375 res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2376 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2377 ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2379 /* modify the value in hkcr */
2380 res = RegSetValueExA(hkcrsub1, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2381 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2383 /* check that the value is modified in hklm classes */
2384 res = RegQueryValueExA(hklmsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2385 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2386 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2388 /* create a subkey in user's classes */
2389 if (RegCreateKeyExA( hkey, "subkey1", 0, NULL, 0,
2390 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkeysub1, NULL )) return;
2391 ok(!IS_HKCR(hkeysub1), "hkcr mask set in %p\n", hkeysub1);
2393 /* set a value in user's classes */
2394 res = RegSetValueExA(hkeysub1, "subval1", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2395 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2397 /* try to find the value in hkcr */
2398 res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2399 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2400 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2402 /* modify the value in hklm */
2403 res = RegSetValueExA(hklmsub1, "subval1", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2404 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2406 /* check that the value is not overwritten in hkcr or user's classes */
2407 res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2408 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2409 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2410 res = RegQueryValueExA(hkeysub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2411 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2412 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2414 /* modify the value in hkcr */
2415 res = RegSetValueExA(hkcrsub1, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2416 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2418 /* check that the value is not overwritten in hklm, but in user's classes */
2419 res = RegQueryValueExA(hklmsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2420 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2421 ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2422 res = RegQueryValueExA(hkeysub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2423 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2424 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2426 /* new subkey in hkcr */
2427 if (RegCreateKeyExA( hkcr, "subkey2", 0, NULL, 0,
2428 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkcrsub2, NULL )) return;
2429 ok(IS_HKCR(hkcrsub2), "hkcr mask not set in %p\n", hkcrsub2);
2430 res = RegSetValueExA(hkcrsub2, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2431 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2433 /* try to open that new subkey in user's classes and hklm */
2434 res = RegOpenKeyExA( hkey, "subkey2", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hklmsub2 );
2435 ok(res != ERROR_SUCCESS, "test key found in user's classes: %d\n", res);
2436 hklmsub2 = 0;
2437 res = RegOpenKeyExA( hklm, "subkey2", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hklmsub2 );
2438 ok(res == ERROR_SUCCESS, "test key not found in hklm: %d\n", res);
2439 ok(!IS_HKCR(hklmsub2), "hkcr mask set in %p\n", hklmsub2);
2441 /* check that the value is present in hklm */
2442 res = RegQueryValueExA(hklmsub2, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2443 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2444 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2446 /* cleanup */
2447 RegCloseKey( hkeysub1 );
2448 RegCloseKey( hklmsub1 );
2450 /* delete subkey1 from hkcr (should point at user's classes) */
2451 res = RegDeleteKeyA(hkcr, "subkey1");
2452 ok(res == ERROR_SUCCESS, "RegDeleteKey failed: %d\n", res);
2454 /* confirm key was removed in hkey but not hklm */
2455 res = RegOpenKeyExA(hkey, "subkey1", 0, KEY_READ, &hkeysub1);
2456 ok(res == ERROR_FILE_NOT_FOUND, "test key found in user's classes: %d\n", res);
2457 res = RegOpenKeyExA(hklm, "subkey1", 0, KEY_READ, &hklmsub1);
2458 ok(res == ERROR_SUCCESS, "test key not found in hklm: %d\n", res);
2459 ok(!IS_HKCR(hklmsub1), "hkcr mask set in %p\n", hklmsub1);
2461 /* delete subkey1 from hkcr again (which should now point at hklm) */
2462 res = RegDeleteKeyA(hkcr, "subkey1");
2463 ok(res == ERROR_SUCCESS, "RegDeleteKey failed: %d\n", res);
2465 /* confirm hkey was removed in hklm */
2466 RegCloseKey( hklmsub1 );
2467 res = RegOpenKeyExA(hklm, "subkey1", 0, KEY_READ, &hklmsub1);
2468 ok(res == ERROR_FILE_NOT_FOUND, "test key found in hklm: %d\n", res);
2470 /* final cleanup */
2471 delete_key( hkey );
2472 delete_key( hklm );
2473 delete_key( hkcr );
2474 delete_key( hkeysub1 );
2475 delete_key( hklmsub1 );
2476 delete_key( hkcrsub1 );
2477 delete_key( hklmsub2 );
2478 delete_key( hkcrsub2 );
2479 RegCloseKey( hkey );
2480 RegCloseKey( hklm );
2481 RegCloseKey( hkcr );
2482 RegCloseKey( hkeysub1 );
2483 RegCloseKey( hklmsub1 );
2484 RegCloseKey( hkcrsub1 );
2485 RegCloseKey( hklmsub2 );
2486 RegCloseKey( hkcrsub2 );
2489 static void test_classesroot_enum(void)
2491 HKEY hkcu=0, hklm=0, hkcr=0, hkcusub[2]={0}, hklmsub[2]={0};
2492 DWORD size;
2493 static CHAR buffer[2];
2494 LONG res;
2496 /* prepare initial testing env in HKCU */
2497 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", &hkcu ))
2499 delete_key( hkcu );
2500 RegCloseKey( hkcu );
2502 res = RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2503 KEY_SET_VALUE|KEY_ENUMERATE_SUB_KEYS, NULL, &hkcu, NULL );
2505 if (res != ERROR_SUCCESS)
2507 skip("failed to add a user class\n");
2508 return;
2511 res = RegOpenKeyA( HKEY_CLASSES_ROOT, "WineTestCls", &hkcr );
2512 todo_wine ok(res == ERROR_SUCCESS ||
2513 broken(res == ERROR_FILE_NOT_FOUND /* WinNT */),
2514 "test key not found in hkcr: %d\n", res);
2515 if (res)
2517 skip("HKCR key merging not supported\n");
2518 delete_key( hkcu );
2519 RegCloseKey( hkcu );
2520 return;
2523 res = RegSetValueExA( hkcu, "X", 0, REG_SZ, (const BYTE *) "AA", 3 );
2524 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", res);
2525 res = RegSetValueExA( hkcu, "Y", 0, REG_SZ, (const BYTE *) "B", 2 );
2526 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", res);
2527 res = RegCreateKeyA( hkcu, "A", &hkcusub[0] );
2528 ok(res == ERROR_SUCCESS, "RegCreateKeyA failed: %d\n", res);
2529 res = RegCreateKeyA( hkcu, "B", &hkcusub[1] );
2530 ok(res == ERROR_SUCCESS, "RegCreateKeyA failed: %d\n", res);
2532 /* test on values in HKCU */
2533 size = sizeof(buffer);
2534 res = RegEnumValueA( hkcr, 0, buffer, &size, NULL, NULL, NULL, NULL );
2535 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2536 ok(!strcmp( buffer, "X" ), "expected 'X', got '%s'\n", buffer);
2537 size = sizeof(buffer);
2538 res = RegEnumValueA( hkcr, 1, buffer, &size, NULL, NULL, NULL, NULL );
2539 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2540 ok(!strcmp( buffer, "Y" ), "expected 'Y', got '%s'\n", buffer);
2541 size = sizeof(buffer);
2542 res = RegEnumValueA( hkcr, 2, buffer, &size, NULL, NULL, NULL, NULL );
2543 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2545 res = RegEnumKeyA( hkcr, 0, buffer, size );
2546 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2547 ok(!strcmp( buffer, "A" ), "expected 'A', got '%s'\n", buffer);
2548 res = RegEnumKeyA( hkcr, 1, buffer, size );
2549 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2550 ok(!strcmp( buffer, "B" ), "expected 'B', got '%s'\n", buffer);
2551 res = RegEnumKeyA( hkcr, 2, buffer, size );
2552 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2554 /* prepare test env in HKLM */
2555 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", &hklm ))
2557 delete_key( hklm );
2558 RegCloseKey( hklm );
2561 res = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2562 KEY_SET_VALUE|KEY_ENUMERATE_SUB_KEYS, NULL, &hklm, NULL );
2564 if (res == ERROR_ACCESS_DENIED)
2566 RegCloseKey( hkcusub[0] );
2567 RegCloseKey( hkcusub[1] );
2568 delete_key( hkcu );
2569 RegCloseKey( hkcu );
2570 RegCloseKey( hkcr );
2571 skip("not enough privileges to add a system class\n");
2572 return;
2575 res = RegSetValueExA( hklm, "X", 0, REG_SZ, (const BYTE *) "AB", 3 );
2576 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", res);
2577 res = RegSetValueExA( hklm, "Z", 0, REG_SZ, (const BYTE *) "C", 2 );
2578 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", res);
2579 res = RegCreateKeyA( hklm, "A", &hklmsub[0] );
2580 ok(res == ERROR_SUCCESS, "RegCreateKeyA failed: %d\n", res);
2581 res = RegCreateKeyA( hklm, "C", &hklmsub[1] );
2582 ok(res == ERROR_SUCCESS, "RegCreateKeyA failed: %d\n", res);
2584 /* test on values/keys in both HKCU and HKLM */
2585 size = sizeof(buffer);
2586 res = RegEnumValueA( hkcr, 0, buffer, &size, NULL, NULL, NULL, NULL );
2587 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2588 ok(!strcmp( buffer, "X" ), "expected 'X', got '%s'\n", buffer);
2589 size = sizeof(buffer);
2590 res = RegEnumValueA( hkcr, 1, buffer, &size, NULL, NULL, NULL, NULL );
2591 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2592 ok(!strcmp( buffer, "Y" ), "expected 'Y', got '%s'\n", buffer);
2593 size = sizeof(buffer);
2594 res = RegEnumValueA( hkcr, 2, buffer, &size, NULL, NULL, NULL, NULL );
2595 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2596 ok(!strcmp( buffer, "Z" ), "expected 'Z', got '%s'\n", buffer);
2597 size = sizeof(buffer);
2598 res = RegEnumValueA( hkcr, 3, buffer, &size, NULL, NULL, NULL, NULL );
2599 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2601 res = RegEnumKeyA( hkcr, 0, buffer, size );
2602 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2603 ok(!strcmp( buffer, "A" ), "expected 'A', got '%s'\n", buffer);
2604 res = RegEnumKeyA( hkcr, 1, buffer, size );
2605 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2606 ok(!strcmp( buffer, "B" ), "expected 'B', got '%s'\n", buffer);
2607 res = RegEnumKeyA( hkcr, 2, buffer, size );
2608 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2609 ok(!strcmp( buffer, "C" ), "expected 'C', got '%s'\n", buffer);
2610 res = RegEnumKeyA( hkcr, 3, buffer, size );
2611 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2613 /* delete values/keys from HKCU to test only on HKLM */
2614 RegCloseKey( hkcusub[0] );
2615 RegCloseKey( hkcusub[1] );
2616 delete_key( hkcu );
2617 RegCloseKey( hkcu );
2619 size = sizeof(buffer);
2620 res = RegEnumValueA( hkcr, 0, buffer, &size, NULL, NULL, NULL, NULL );
2621 ok(res == ERROR_KEY_DELETED ||
2622 res == ERROR_NO_SYSTEM_RESOURCES, /* Windows XP */
2623 "expected ERROR_KEY_DELETED, got %d\n", res);
2624 size = sizeof(buffer);
2625 res = RegEnumKeyA( hkcr, 0, buffer, size );
2626 ok(res == ERROR_KEY_DELETED ||
2627 res == ERROR_NO_SYSTEM_RESOURCES, /* Windows XP */
2628 "expected ERROR_KEY_DELETED, got %d\n", res);
2630 /* reopen HKCR handle */
2631 RegCloseKey( hkcr );
2632 res = RegOpenKeyA( HKEY_CLASSES_ROOT, "WineTestCls", &hkcr );
2633 ok(res == ERROR_SUCCESS, "test key not found in hkcr: %d\n", res);
2634 if (res) goto cleanup;
2636 /* test on values/keys in HKLM */
2637 size = sizeof(buffer);
2638 res = RegEnumValueA( hkcr, 0, buffer, &size, NULL, NULL, NULL, NULL );
2639 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2640 ok(!strcmp( buffer, "X" ), "expected 'X', got '%s'\n", buffer);
2641 size = sizeof(buffer);
2642 res = RegEnumValueA( hkcr, 1, buffer, &size, NULL, NULL, NULL, NULL );
2643 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2644 ok(!strcmp( buffer, "Z" ), "expected 'Z', got '%s'\n", buffer);
2645 size = sizeof(buffer);
2646 res = RegEnumValueA( hkcr, 2, buffer, &size, NULL, NULL, NULL, NULL );
2647 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2649 res = RegEnumKeyA( hkcr, 0, buffer, size );
2650 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2651 ok(!strcmp( buffer, "A" ), "expected 'A', got '%s'\n", buffer);
2652 res = RegEnumKeyA( hkcr, 1, buffer, size );
2653 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2654 ok(!strcmp( buffer, "C" ), "expected 'C', got '%s'\n", buffer);
2655 res = RegEnumKeyA( hkcr, 2, buffer, size );
2656 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2658 cleanup:
2659 RegCloseKey( hklmsub[0] );
2660 RegCloseKey( hklmsub[1] );
2661 delete_key( hklm );
2662 RegCloseKey( hklm );
2663 RegCloseKey( hkcr );
2666 static void test_classesroot_mask(void)
2668 HKEY hkey;
2669 LSTATUS res;
2671 res = RegOpenKeyA( HKEY_CLASSES_ROOT, "CLSID", &hkey );
2672 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2673 todo_wine ok(IS_HKCR(hkey) || broken(!IS_HKCR(hkey)) /* WinNT */,
2674 "hkcr mask not set in %p\n", hkey);
2675 RegCloseKey( hkey );
2677 res = RegOpenKeyA( HKEY_CURRENT_USER, "Software", &hkey );
2678 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2679 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2680 RegCloseKey( hkey );
2682 res = RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software", &hkey );
2683 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2684 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2685 RegCloseKey( hkey );
2687 res = RegOpenKeyA( HKEY_USERS, ".Default", &hkey );
2688 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2689 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2690 RegCloseKey( hkey );
2692 res = RegOpenKeyA( HKEY_CURRENT_CONFIG, "Software", &hkey );
2693 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2694 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2695 RegCloseKey( hkey );
2698 static void test_deleted_key(void)
2700 HKEY hkey, hkey2;
2701 char value[20];
2702 DWORD val_count, type;
2703 LONG res;
2705 /* Open the test key, then delete it while it's open */
2706 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey );
2708 delete_key( hkey_main );
2710 val_count = sizeof(value);
2711 type = 0;
2712 res = RegEnumValueA( hkey, 0, value, &val_count, NULL, &type, 0, 0 );
2713 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2715 res = RegEnumKeyA( hkey, 0, value, sizeof(value) );
2716 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2718 val_count = sizeof(value);
2719 type = 0;
2720 res = RegQueryValueExA( hkey, "test", NULL, &type, (BYTE *)value, &val_count );
2721 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2723 res = RegSetValueExA( hkey, "test", 0, REG_SZ, (const BYTE*)"value", 6);
2724 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2726 res = RegOpenKeyA( hkey, "test", &hkey2 );
2727 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2728 if (res == 0)
2729 RegCloseKey( hkey2 );
2731 res = RegCreateKeyA( hkey, "test", &hkey2 );
2732 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2733 if (res == 0)
2734 RegCloseKey( hkey2 );
2736 res = RegFlushKey( hkey );
2737 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2739 RegCloseKey( hkey );
2741 setup_main_key();
2744 static void test_delete_value(void)
2746 LONG res;
2747 char longname[401];
2749 res = RegSetValueExA( hkey_main, "test", 0, REG_SZ, (const BYTE*)"value", 6 );
2750 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2752 res = RegQueryValueExA( hkey_main, "test", NULL, NULL, NULL, NULL);
2753 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2755 res = RegDeleteValueA( hkey_main, "test" );
2756 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2758 res = RegQueryValueExA( hkey_main, "test", NULL, NULL, NULL, NULL);
2759 ok(res == ERROR_FILE_NOT_FOUND, "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2761 res = RegDeleteValueA( hkey_main, "test" );
2762 ok(res == ERROR_FILE_NOT_FOUND, "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2764 memset(longname, 'a', 400);
2765 longname[400] = 0;
2766 res = RegDeleteValueA( hkey_main, longname );
2767 ok(res == ERROR_FILE_NOT_FOUND || broken(res == ERROR_MORE_DATA), /* nt4, win2k */
2768 "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2771 START_TEST(registry)
2773 /* Load pointers for functions that are not available in all Windows versions */
2774 InitFunctionPtrs();
2776 setup_main_key();
2777 check_user_privs();
2778 test_set_value();
2779 create_test_entries();
2780 test_enum_value();
2781 test_query_value_ex();
2782 test_get_value();
2783 test_reg_open_key();
2784 test_reg_create_key();
2785 test_reg_close_key();
2786 test_reg_delete_key();
2787 test_reg_query_value();
2788 test_string_termination();
2789 test_symlinks();
2790 test_redirection();
2791 test_classesroot();
2792 test_classesroot_enum();
2793 test_classesroot_mask();
2795 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
2796 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
2797 set_privileges(SE_RESTORE_NAME, TRUE))
2799 test_reg_save_key();
2800 test_reg_load_key();
2801 test_reg_unload_key();
2803 set_privileges(SE_BACKUP_NAME, FALSE);
2804 set_privileges(SE_RESTORE_NAME, FALSE);
2807 test_reg_delete_tree();
2808 test_rw_order();
2809 test_deleted_key();
2810 test_delete_value();
2812 /* cleanup */
2813 delete_key( hkey_main );
2815 test_regconnectregistry();