2 * Unit tests for registry functions
4 * Copyright (c) 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/test.h"
31 static HKEY hkey_main;
34 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
35 static const char * sTestpath2 = "%FOO%\\subdir1";
37 static HMODULE hadvapi32;
38 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
39 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
43 /* Debugging functions from wine/libs/wine/debug.c */
45 /* allocate some tmp string space */
46 /* FIXME: this is not 100% thread-safe */
47 static char *get_temp_buffer( int size )
49 static char *list[32];
54 idx = ++pos % (sizeof(list)/sizeof(list[0]));
55 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
59 static const char *wine_debugstr_an( const char *str, int n )
61 static const char hex[16] = "0123456789abcdef";
65 if (!((ULONG_PTR)str >> 16))
67 if (!str) return "(null)";
68 res = get_temp_buffer( 6 );
69 sprintf( res, "#%04x", LOWORD(str) );
72 if (n == -1) n = strlen(str);
74 size = 10 + min( 300, n * 4 );
75 dst = res = get_temp_buffer( size );
77 while (n-- > 0 && dst <= res + size - 9)
79 unsigned char c = *str++;
82 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
83 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
84 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
85 case '"': *dst++ = '\\'; *dst++ = '"'; break;
86 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
88 if (c >= ' ' && c <= 126)
94 *dst++ = hex[(c >> 4) & 0x0f];
95 *dst++ = hex[c & 0x0f];
110 static const char *wine_debugstr_wn( const WCHAR *str, int n )
117 if (!str) return "(null)";
118 res = get_temp_buffer( 6 );
119 sprintf( res, "#%04x", LOWORD(str) );
122 if (n == -1) n = lstrlenW(str);
124 size = 12 + min( 300, n * 5);
125 dst = res = get_temp_buffer( n * 5 + 7 );
128 while (n-- > 0 && dst <= res + size - 10)
133 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
134 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
135 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
136 case '"': *dst++ = '\\'; *dst++ = '"'; break;
137 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
139 if (c >= ' ' && c <= 126)
144 sprintf(dst,"%04x",c);
161 #define ADVAPI32_GET_PROC(func) \
162 p ## func = (void*)GetProcAddress(hadvapi32, #func);
165 static void InitFunctionPtrs(void)
167 hadvapi32 = GetModuleHandleA("advapi32.dll");
169 /* This function was introduced with Windows 2003 SP1 */
170 ADVAPI32_GET_PROC(RegGetValueA)
171 ADVAPI32_GET_PROC(RegDeleteTreeA)
174 /* delete key and all its subkeys */
175 static DWORD delete_key( HKEY hkey )
180 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
183 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
185 ret = delete_key( tmp );
190 if (ret != ERROR_NO_MORE_ITEMS) return ret;
191 RegDeleteKeyA( hkey, "" );
195 static void setup_main_key(void)
197 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
199 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
202 static void test_hkey_main_Value_A(LPCSTR name, LPCSTR string,
205 DWORD ret, type, cbData;
208 static const char nA[]={'N', 0};
212 /* When successful RegQueryValueExA() leaves GLE as is,
213 * so we must reset it to detect unimplemented functions.
215 SetLastError(0xdeadbeef);
216 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
217 GLE = GetLastError();
218 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
219 /* It is wrong for the Ansi version to not be implemented */
220 ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
221 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
223 str_byte_len = (string ? lstrlenA(string) : 0) + 1;
224 ok(type == REG_SZ, "RegQueryValueExA returned type %d\n", type);
225 ok(cbData == full_byte_len || cbData == str_byte_len /* Win9x */,
226 "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
228 value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
231 ret = RegQueryValueExA(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
232 GLE = GetLastError();
233 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
236 /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
237 ok(strcmp(value, nA) == 0 || (cbData == 1 && *value == '\0') /* Win9x */,
238 "RegQueryValueExA failed: '%s' != '%s'\n", value, string);
242 ok(memcmp(value, string, cbData) == 0, "RegQueryValueExA failed: %s/%d != %s/%d\n",
243 wine_debugstr_an(value, cbData), cbData,
244 wine_debugstr_an(string, full_byte_len), full_byte_len);
246 HeapFree(GetProcessHeap(), 0, value);
249 static void test_hkey_main_Value_W(LPCWSTR name, LPCWSTR string,
252 DWORD ret, type, cbData;
254 static const WCHAR nW[]={'N', 0};
258 /* When successful RegQueryValueExW() leaves GLE as is,
259 * so we must reset it to detect unimplemented functions.
261 SetLastError(0xdeadbeef);
262 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
263 GLE = GetLastError();
264 ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
265 if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
267 win_skip("RegQueryValueExW() is not implemented\n");
271 ok(type == REG_SZ, "RegQueryValueExW returned type %d\n", type);
272 ok(cbData == full_byte_len,
273 "cbData=%d instead of %d\n", cbData, full_byte_len);
275 value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
278 ret = RegQueryValueExW(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
279 GLE = GetLastError();
280 ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
283 /* When cbData == 0, RegQueryValueExW() should not modify the buffer */
286 ok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
287 wine_debugstr_wn(value, cbData / sizeof(WCHAR)), cbData,
288 wine_debugstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
289 HeapFree(GetProcessHeap(), 0, value);
292 static void test_set_value(void)
296 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
297 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};
298 static const WCHAR emptyW[] = {0};
299 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
300 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};
301 static const WCHAR substring2W[] = {'T','h','i','s',0};
303 static const char name1A[] = "CleanSingleString";
304 static const char name2A[] = "SomeIntraZeroedString";
305 static const char emptyA[] = "";
306 static const char string1A[] = "ThisNeverBreaks";
307 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
308 static const char substring2A[] = "This";
312 /* Crashes on NT4, Windows 2000 and XP SP1 */
313 ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
314 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
317 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
318 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
319 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
320 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
322 /* RegSetValueA ignores the size passed in */
323 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
324 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
325 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
326 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
328 /* stops at first null */
329 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
330 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
331 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
332 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
334 /* only REG_SZ is supported on NT*/
335 ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
336 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
337 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
338 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
340 ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
341 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
342 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
343 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
345 ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
346 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
347 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
348 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
350 /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
351 * Surprisingly enough we're supposed to get zero bytes out of it.
353 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
354 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
355 test_hkey_main_Value_A(name1A, NULL, 0);
356 test_hkey_main_Value_W(name1W, NULL, 0);
358 /* test RegSetValueExA with an empty string */
359 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
360 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
361 test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
362 test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
364 /* test RegSetValueExA with off-by-one size */
365 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
366 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
367 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
368 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
370 /* test RegSetValueExA with normal string */
371 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
372 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
373 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
374 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
376 /* test RegSetValueExA with intrazeroed string */
377 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
378 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
379 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
380 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
382 /* 9x doesn't support W-calls, so don't test them then */
383 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
387 /* Crashes on NT4, Windows 2000 and XP SP1 */
388 ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
389 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
392 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
393 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
394 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
395 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
397 /* RegSetValueA ignores the size passed in */
398 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
399 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
400 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
401 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
403 /* stops at first null */
404 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
405 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
406 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
407 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
409 /* only REG_SZ is supported */
410 ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
411 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
412 ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
413 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
414 ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
415 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
417 /* test RegSetValueExW with off-by-one size */
418 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
419 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
420 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
421 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
423 /* test RegSetValueExW with normal string */
424 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
425 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
426 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
427 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
429 /* test RegSetValueExW with intrazeroed string */
430 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
431 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
432 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
433 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
436 static void create_test_entries(void)
438 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
440 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
441 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
443 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
444 "RegSetValueExA failed\n");
445 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
446 "RegSetValueExA failed\n");
447 ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
448 "RegSetValueExA failed\n");
449 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
450 "RegSetValueExA failed\n");
451 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
452 "RegSetValueExA failed\n");
453 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
454 "RegSetValueExA failed\n");
455 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
456 "RegSetValueExA failed\n");
459 static void test_enum_value(void)
463 char value[20], data[20];
464 WCHAR valueW[20], dataW[20];
465 DWORD val_count, data_count, type;
466 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
467 static const WCHAR testW[] = {'T','e','s','t',0};
468 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
470 /* create the working key for new 'Test' value */
471 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
472 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
474 /* check NULL data with zero length */
475 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
476 if (GetVersion() & 0x80000000)
477 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
479 ok( !res, "RegSetValueExA returned %d\n", res );
480 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
481 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
482 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
483 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
485 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
486 ok( res == 0, "RegSetValueExA failed error %d\n", res );
488 /* overflow both name and data */
492 strcpy( value, "xxxxxxxxxx" );
493 strcpy( data, "xxxxxxxxxx" );
494 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
495 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
496 ok( val_count == 2, "val_count set to %d\n", val_count );
497 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
498 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
499 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
500 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
506 strcpy( value, "xxxxxxxxxx" );
507 strcpy( data, "xxxxxxxxxx" );
508 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
509 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
510 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
511 ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
512 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
513 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
514 /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
515 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
516 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
517 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
518 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
520 /* overflow empty name */
524 strcpy( value, "xxxxxxxxxx" );
525 strcpy( data, "xxxxxxxxxx" );
526 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
527 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
528 ok( val_count == 0, "val_count set to %d\n", val_count );
529 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
530 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
531 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
532 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
533 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
534 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
540 strcpy( value, "xxxxxxxxxx" );
541 strcpy( data, "xxxxxxxxxx" );
542 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
543 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
544 ok( val_count == 20, "val_count set to %d\n", val_count );
545 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
546 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
547 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
548 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
554 strcpy( value, "xxxxxxxxxx" );
555 strcpy( data, "xxxxxxxxxx" );
556 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
557 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
558 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
559 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
560 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
561 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
562 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
566 SetLastError(0xdeadbeef);
567 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
568 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
570 win_skip("RegSetValueExW is not implemented\n");
573 ok( res == 0, "RegSetValueExW failed error %d\n", res );
575 /* overflow both name and data */
579 memcpy( valueW, xxxW, sizeof(xxxW) );
580 memcpy( dataW, xxxW, sizeof(xxxW) );
581 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
582 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
583 ok( val_count == 2, "val_count set to %d\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, xxxW, sizeof(xxxW) ), "value modified\n" );
587 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
593 memcpy( valueW, xxxW, sizeof(xxxW) );
594 memcpy( dataW, xxxW, sizeof(xxxW) );
595 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
596 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
597 ok( val_count == 3, "val_count set to %d\n", val_count );
598 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
599 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
600 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
601 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
607 memcpy( valueW, xxxW, sizeof(xxxW) );
608 memcpy( dataW, xxxW, sizeof(xxxW) );
609 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
610 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
611 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
612 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
613 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
614 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
615 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
621 memcpy( valueW, xxxW, sizeof(xxxW) );
622 memcpy( dataW, xxxW, sizeof(xxxW) );
623 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
624 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
625 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
626 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
627 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
628 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
629 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
632 RegDeleteKeyA(test_key, "");
633 RegCloseKey(test_key);
636 static void test_query_value_ex(void)
643 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
644 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
645 ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
646 ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
650 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
651 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
652 /* the type parameter is cleared on Win9x, but is set to a random value on
653 * NT, so don't do that test there. The size parameter is left untouched on Win9x
654 * but cleared on NT+, this can be tested on all platforms.
656 if (GetVersion() & 0x80000000)
658 ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
659 ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
663 trace("test_query_value_ex: type set to: 0x%08x\n", type);
664 ok(size == 0, "size should have been set to 0 instead of %d\n", size);
667 size = sizeof(buffer);
668 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
669 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
670 ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
673 ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
674 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
677 static void test_get_value(void)
684 CHAR expanded[] = "bar\\subdir1";
685 CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
689 win_skip("RegGetValue not available on this platform\n");
693 /* Invalid parameter */
694 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
695 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
697 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
698 size = type = dw = 0xdeadbeef;
699 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
700 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
701 ok(size == 4, "size=%d\n", size);
702 ok(type == REG_DWORD, "type=%d\n", type);
703 ok(dw == 0x12345678, "dw=%d\n", dw);
705 /* Query by subkey-name */
706 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
707 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
709 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
710 size = type = dw = 0xdeadbeef;
711 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
712 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
713 /* Although the function failed all values are retrieved */
714 ok(size == 4, "size=%d\n", size);
715 ok(type == REG_DWORD, "type=%d\n", type);
716 ok(dw == 0x12345678, "dw=%d\n", dw);
718 /* Test RRF_ZEROONFAILURE */
719 type = dw = 0xdeadbeef; size = 4;
720 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
721 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
722 /* Again all values are retrieved ... */
723 ok(size == 4, "size=%d\n", size);
724 ok(type == REG_DWORD, "type=%d\n", type);
725 /* ... except the buffer, which is zeroed out */
726 ok(dw == 0, "dw=%d\n", dw);
728 /* Test RRF_ZEROONFAILURE with a NULL buffer... */
729 type = size = 0xbadbeef;
730 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
731 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
732 ok(size == 4, "size=%d\n", size);
733 ok(type == REG_DWORD, "type=%d\n", type);
735 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
736 size = type = dw = 0xdeadbeef;
737 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
738 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
739 ok(size == 4, "size=%d\n", size);
740 ok(type == REG_DWORD, "type=%d\n", type);
741 ok(dw == 0x12345678, "dw=%d\n", dw);
743 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
744 size = type = dw = 0xdeadbeef;
745 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
746 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
747 ok(size == 4, "size=%d\n", size);
748 ok(type == REG_BINARY, "type=%d\n", type);
749 ok(dw == 0x12345678, "dw=%d\n", dw);
751 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
752 qw[0] = qw[1] = size = type = 0xdeadbeef;
753 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
754 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
755 ok(size == 8, "size=%d\n", size);
756 ok(type == REG_BINARY, "type=%d\n", type);
757 ok(qw[0] == 0x12345678 &&
758 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
760 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
761 type = dw = 0xdeadbeef; size = 4;
762 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
763 ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
764 ok(dw == 0xdeadbeef, "dw=%d\n", dw);
765 ok(size == 8, "size=%d\n", size);
767 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
768 qw[0] = qw[1] = size = type = 0xdeadbeef;
769 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
770 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
771 ok(size == 8, "size=%d\n", size);
772 ok(type == REG_BINARY, "type=%d\n", type);
773 ok(qw[0] == 0x12345678 &&
774 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
776 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
777 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
778 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
779 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
780 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
781 ok(type == REG_SZ, "type=%d\n", type);
782 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
784 /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
785 type = 0xdeadbeef; size = 0;
786 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
787 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
788 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
789 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
790 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
791 ok(type == REG_SZ, "type=%d\n", type);
793 /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
794 strcpy(buf, sTestpath1);
797 ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
798 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
799 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
801 size == 1, /* win2k3 */
803 ok(type == REG_SZ, "type=%d\n", type);
804 ok(!strcmp(sTestpath1, buf) ||
806 "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
808 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
809 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
810 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
811 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
812 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
813 ok(type == REG_SZ, "type=%d\n", type);
814 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
816 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
818 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
819 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
820 ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
821 (size == strlen(expanded2)+2) || /* win2k3 SP2 */
822 (size == strlen(sTestpath2)+1),
823 "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
825 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
826 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
827 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
828 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
829 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
830 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
831 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
832 ok(type == REG_SZ, "type=%d\n", type);
833 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
835 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
836 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
837 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
838 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
839 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
840 ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
841 "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
842 ok(type == REG_SZ, "type=%d\n", type);
843 ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
845 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
846 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
847 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
848 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
849 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
850 ok(type == REG_EXPAND_SZ, "type=%d\n", type);
851 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
853 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
855 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
856 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
857 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
858 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
859 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
861 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
862 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
863 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
865 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
866 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
867 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
869 /* Query REG_EXPAND_SZ using RRF_RT_ANY */
870 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
871 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
872 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
873 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
874 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
875 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
876 ok(type == REG_SZ, "type=%d\n", type);
877 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
880 static void test_reg_open_key(void)
883 HKEY hkResult = NULL;
884 HKEY hkPreserve = NULL;
886 /* successful open */
887 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
888 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
889 ok(hkResult != NULL, "expected hkResult != NULL\n");
890 hkPreserve = hkResult;
892 /* these tests fail on Win9x, but we want to be compatible with NT, so
893 * run them if we can */
894 if (!(GetVersion() & 0x80000000))
896 /* open same key twice */
897 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
898 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
899 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
900 ok(hkResult != NULL, "hkResult != NULL\n");
901 RegCloseKey(hkResult);
903 /* open nonexistent key
904 * check that hkResult is set to NULL
906 hkResult = hkPreserve;
907 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
908 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
909 ok(hkResult == NULL, "expected hkResult == NULL\n");
911 /* open the same nonexistent key again to make sure the key wasn't created */
912 hkResult = hkPreserve;
913 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
914 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
915 ok(hkResult == NULL, "expected hkResult == NULL\n");
917 /* send in NULL lpSubKey
918 * check that hkResult receives the value of hKey
920 hkResult = hkPreserve;
921 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
922 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
923 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
925 /* send empty-string in lpSubKey */
926 hkResult = hkPreserve;
927 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
928 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
929 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
931 /* send in NULL lpSubKey and NULL hKey
932 * hkResult is set to NULL
934 hkResult = hkPreserve;
935 ret = RegOpenKeyA(NULL, NULL, &hkResult);
936 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
937 ok(hkResult == NULL, "expected hkResult == NULL\n");
940 /* only send NULL hKey
941 * the value of hkResult remains unchanged
943 hkResult = hkPreserve;
944 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
945 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
946 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
947 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
948 RegCloseKey(hkResult);
950 /* send in NULL hkResult */
951 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
952 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
954 /* beginning backslash character */
955 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
956 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
957 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
958 , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
962 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_32KEY, &hkResult);
963 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
964 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
965 RegCloseKey(hkResult);
968 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_64KEY, &hkResult);
969 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
970 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
971 RegCloseKey(hkResult);
974 static void test_reg_create_key(void)
978 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
979 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
980 /* should succeed: all versions of Windows ignore the access rights
981 * to the parent handle */
982 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
983 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
986 RegDeleteKey(hkey2, "");
987 RegDeleteKey(hkey1, "");
989 /* beginning backslash character */
990 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
991 if (!(GetVersion() & 0x80000000))
992 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
994 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
995 RegDeleteKey(hkey1, NULL);
998 /* WOW64 flags - open an existing key */
1000 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &hkey1, NULL);
1001 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1002 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1006 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_64KEY, NULL, &hkey1, NULL);
1007 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1008 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1012 static void test_reg_close_key(void)
1017 /* successfully close key
1018 * hkHandle remains changed after call to RegCloseKey
1020 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
1021 ret = RegCloseKey(hkHandle);
1022 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1024 /* try to close the key twice */
1025 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
1026 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
1027 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
1029 /* try to close a NULL handle */
1030 ret = RegCloseKey(NULL);
1031 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1032 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1034 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1035 * win98 doesn't give a new handle when the same key is opened.
1036 * Not re-opening will make some next tests fail.
1038 if (hkey_main == hkHandle)
1040 trace("The main handle is most likely closed, so re-opening\n");
1041 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1045 static void test_reg_delete_key(void)
1049 ret = RegDeleteKey(hkey_main, NULL);
1051 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1052 * there are also no subkeys available it will delete the key pointed to by hkey_main.
1053 * Not re-creating will make some next tests fail.
1055 if (ret == ERROR_SUCCESS)
1057 trace("We are probably running on NT4 or W2K as the main key is deleted,"
1058 " re-creating the main key\n");
1062 ok(ret == ERROR_INVALID_PARAMETER ||
1063 ret == ERROR_ACCESS_DENIED ||
1064 ret == ERROR_BADKEY, /* Win95 */
1068 static void test_reg_save_key(void)
1072 ret = RegSaveKey(hkey_main, "saved_key", NULL);
1073 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1076 static void test_reg_load_key(void)
1081 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1082 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1084 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1085 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1087 RegCloseKey(hkHandle);
1090 static void test_reg_unload_key(void)
1094 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
1095 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1097 DeleteFile("saved_key");
1098 DeleteFile("saved_key.LOG");
1101 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1103 TOKEN_PRIVILEGES tp;
1107 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1110 if(!LookupPrivilegeValue(NULL, privilege, &luid))
1112 CloseHandle(hToken);
1116 tp.PrivilegeCount = 1;
1117 tp.Privileges[0].Luid = luid;
1120 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1122 tp.Privileges[0].Attributes = 0;
1124 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1125 if (GetLastError() != ERROR_SUCCESS)
1127 CloseHandle(hToken);
1131 CloseHandle(hToken);
1135 /* tests that show that RegConnectRegistry and
1136 OpenSCManager accept computer names without the
1137 \\ prefix (what MSDN says). */
1138 static void test_regconnectregistry( void)
1140 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1141 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1142 DWORD len = sizeof(compName) ;
1148 SetLastError(0xdeadbeef);
1149 ret = GetComputerNameA(compName, &len);
1150 ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1153 lstrcpyA(netwName, "\\\\");
1154 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1156 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1158 retl == ERROR_DLL_INIT_FAILED ||
1159 retl == ERROR_BAD_NETPATH, /* some win2k */
1160 "RegConnectRegistryA failed err = %d\n", retl);
1161 if( !retl) RegCloseKey( hkey);
1163 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1165 retl == ERROR_DLL_INIT_FAILED ||
1166 retl == ERROR_BAD_NETPATH, /* some win2k */
1167 "RegConnectRegistryA failed err = %d\n", retl);
1168 if( !retl) RegCloseKey( hkey);
1170 SetLastError(0xdeadbeef);
1171 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
1172 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1174 win_skip("OpenSCManagerA is not implemented\n");
1178 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1179 CloseServiceHandle( schnd);
1181 SetLastError(0xdeadbeef);
1182 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
1183 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1184 CloseServiceHandle( schnd);
1188 static void test_reg_query_value(void)
1195 static const WCHAR expected[] = {'d','a','t','a',0};
1197 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1198 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1200 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1201 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1203 /* try an invalid hkey */
1204 SetLastError(0xdeadbeef);
1206 ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1207 ok(ret == ERROR_INVALID_HANDLE ||
1208 ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1209 ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1210 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1211 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1213 /* try a NULL hkey */
1214 SetLastError(0xdeadbeef);
1216 ret = RegQueryValueA(NULL, "subkey", val, &size);
1217 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1218 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1219 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1221 /* try a NULL value */
1223 ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1224 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1225 ok(size == 5, "Expected 5, got %d\n", size);
1227 /* try a NULL size */
1228 SetLastError(0xdeadbeef);
1230 ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1231 ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1232 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1233 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1235 /* try a NULL value and size */
1236 ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1237 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1239 /* try a size too small */
1240 SetLastError(0xdeadbeef);
1243 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1244 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1245 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1246 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1247 ok(size == 5, "Expected 5, got %d\n", size);
1249 /* successfully read the value using 'subkey' */
1251 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1252 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1253 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1254 ok(size == 5, "Expected 5, got %d\n", size);
1256 /* successfully read the value using the subkey key */
1258 ret = RegQueryValueA(subkey, NULL, val, &size);
1259 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1260 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1261 ok(size == 5, "Expected 5, got %d\n", size);
1263 /* unicode - try size too small */
1264 SetLastError(0xdeadbeef);
1267 ret = RegQueryValueW(subkey, NULL, valW, &size);
1268 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1270 win_skip("RegQueryValueW is not implemented\n");
1273 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1274 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1275 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1276 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1278 /* unicode - try size in WCHARS */
1279 SetLastError(0xdeadbeef);
1280 size = sizeof(valW) / sizeof(WCHAR);
1281 ret = RegQueryValueW(subkey, NULL, valW, &size);
1282 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1283 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1284 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1285 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1287 /* unicode - successfully read the value */
1288 size = sizeof(valW);
1289 ret = RegQueryValueW(subkey, NULL, valW, &size);
1290 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1291 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1292 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1294 /* unicode - set the value without a NULL terminator */
1295 ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1296 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1298 /* unicode - read the unterminated value, value is terminated for us */
1299 memset(valW, 'a', sizeof(valW));
1300 size = sizeof(valW);
1301 ret = RegQueryValueW(subkey, NULL, valW, &size);
1302 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1303 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1304 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1307 RegDeleteKeyA(subkey, "");
1308 RegCloseKey(subkey);
1311 static void test_reg_delete_tree(void)
1313 CHAR buffer[MAX_PATH];
1314 HKEY subkey, subkey2;
1317 if(!pRegDeleteTreeA) {
1318 win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1322 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1323 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1324 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1325 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1326 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1327 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1328 ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1329 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1330 ret = RegCloseKey(subkey2);
1331 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1333 ret = pRegDeleteTreeA(subkey, "subkey2");
1334 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1335 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1336 "subkey2 was not deleted\n");
1338 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1339 "Default value of subkey not longer present\n");
1341 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1342 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1343 ret = RegCloseKey(subkey2);
1344 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1345 ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1346 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1347 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1348 "subkey2 was not deleted\n");
1349 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1350 "Default value of subkey not longer present\n");
1352 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1353 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1354 ret = RegCloseKey(subkey2);
1355 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1356 ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1357 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1358 ret = RegCloseKey(subkey2);
1359 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1360 ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1361 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1362 ret = pRegDeleteTreeA(subkey, NULL);
1363 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1364 ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1365 "subkey was deleted\n");
1366 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1367 "subkey2 was not deleted\n");
1368 ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1369 "subkey3 was not deleted\n");
1371 ret = RegQueryValueA(subkey, NULL, buffer, &size);
1372 ok(ret == ERROR_SUCCESS,
1373 "Default value of subkey is not present\n");
1374 ok(!lstrlenA(buffer),
1375 "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1377 ok(RegQueryValueA(subkey, "value", buffer, &size),
1378 "Value is still present\n");
1380 ret = pRegDeleteTreeA(hkey_main, "not-here");
1381 ok(ret == ERROR_FILE_NOT_FOUND,
1382 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1385 START_TEST(registry)
1387 /* Load pointers for functions that are not available in all Windows versions */
1392 create_test_entries();
1394 test_query_value_ex();
1396 test_reg_open_key();
1397 test_reg_create_key();
1398 test_reg_close_key();
1399 test_reg_delete_key();
1400 test_reg_query_value();
1402 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1403 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1404 set_privileges(SE_RESTORE_NAME, TRUE))
1406 test_reg_save_key();
1407 test_reg_load_key();
1408 test_reg_unload_key();
1410 set_privileges(SE_BACKUP_NAME, FALSE);
1411 set_privileges(SE_RESTORE_NAME, FALSE);
1414 test_reg_delete_tree();
1417 delete_key( hkey_main );
1419 test_regconnectregistry();