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