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); \
164 trace("GetProcAddress(%s) failed\n", #func);
166 static void InitFunctionPtrs(void)
168 hadvapi32 = GetModuleHandleA("advapi32.dll");
170 /* This function was introduced with Windows 2003 SP1 */
171 ADVAPI32_GET_PROC(RegGetValueA)
172 ADVAPI32_GET_PROC(RegDeleteTreeA)
175 /* delete key and all its subkeys */
176 static DWORD delete_key( HKEY hkey )
181 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
184 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
186 ret = delete_key( tmp );
191 if (ret != ERROR_NO_MORE_ITEMS) return ret;
192 RegDeleteKeyA( hkey, "" );
196 static void setup_main_key(void)
198 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
200 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
203 static void test_hkey_main_Value_A(LPCSTR name, LPCSTR string,
206 DWORD ret, type, cbData;
209 static const char nA[]={'N', 0};
213 /* When successful RegQueryValueExA() leaves GLE as is,
214 * so we must reset it to detect unimplemented functions.
216 SetLastError(0xdeadbeef);
217 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
218 GLE = GetLastError();
219 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
220 /* It is wrong for the Ansi version to not be implemented */
221 ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
222 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
224 str_byte_len = (string ? lstrlenA(string) : 0) + 1;
225 ok(type == REG_SZ, "RegQueryValueExA returned type %d\n", type);
226 ok(cbData == full_byte_len || cbData == str_byte_len /* Win9x */,
227 "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
229 value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
232 ret = RegQueryValueExA(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
233 GLE = GetLastError();
234 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
237 /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
238 ok(strcmp(value, nA) == 0 || (cbData == 1 && *value == '\0') /* Win9x */,
239 "RegQueryValueExA failed: '%s' != '%s'\n", value, string);
243 ok(memcmp(value, string, cbData) == 0, "RegQueryValueExA failed: %s/%d != %s/%d\n",
244 wine_debugstr_an(value, cbData), cbData,
245 wine_debugstr_an(string, full_byte_len), full_byte_len);
247 HeapFree(GetProcessHeap(), 0, value);
250 static void test_hkey_main_Value_W(LPCWSTR name, LPCWSTR string,
253 DWORD ret, type, cbData;
255 static const WCHAR nW[]={'N', 0};
259 /* When successful RegQueryValueExW() leaves GLE as is,
260 * so we must reset it to detect unimplemented functions.
262 SetLastError(0xdeadbeef);
263 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
264 GLE = GetLastError();
265 ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
266 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
268 ok(type == REG_SZ, "RegQueryValueExW returned type %d\n", type);
269 ok(cbData == full_byte_len,
270 "cbData=%d instead of %d\n", cbData, full_byte_len);
272 value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
275 ret = RegQueryValueExW(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
276 GLE = GetLastError();
277 ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
280 /* When cbData == 0, RegQueryValueExW() should not modify the buffer */
283 ok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
284 wine_debugstr_wn(value, cbData / sizeof(WCHAR)), cbData,
285 wine_debugstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
286 HeapFree(GetProcessHeap(), 0, value);
289 static void test_set_value(void)
293 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
294 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};
295 static const WCHAR emptyW[] = {0};
296 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
297 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};
299 static const char name1A[] = "CleanSingleString";
300 static const char name2A[] = "SomeIntraZeroedString";
301 static const char emptyA[] = "";
302 static const char string1A[] = "ThisNeverBreaks";
303 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
305 /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
306 * Surprisingly enough we're supposed to get zero bytes out of it.
307 * FIXME: Wine's on-disk file format does not differentiate this with
308 * regular empty strings but there's no way to test as it requires
309 * stopping the wineserver.
311 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
312 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
313 test_hkey_main_Value_A(name1A, NULL, 0);
314 test_hkey_main_Value_W(name1W, NULL, 0);
316 /* test RegSetValueExA with an empty string */
317 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
318 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
319 test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
320 test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
322 /* test RegSetValueExA with normal string */
323 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
324 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
325 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
326 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
328 /* test RegSetValueExA with intrazeroed string */
329 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
330 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
331 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
332 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
334 /* 9x doesn't support W-calls, so don't test them then */
335 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
337 /* test RegSetValueExW with normal string */
338 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
339 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
340 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
341 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
343 /* test RegSetValueExW with intrazeroed string */
344 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
345 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
346 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
347 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
350 static void create_test_entries(void)
352 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
354 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
355 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
357 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
358 "RegSetValueExA failed\n");
359 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
360 "RegSetValueExA failed\n");
361 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
362 "RegSetValueExA failed\n");
363 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
364 "RegSetValueExA failed\n");
365 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
366 "RegSetValueExA failed\n");
367 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
368 "RegSetValueExA failed\n");
371 static void test_enum_value(void)
375 char value[20], data[20];
376 WCHAR valueW[20], dataW[20];
377 DWORD val_count, data_count, type;
378 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
379 static const WCHAR testW[] = {'T','e','s','t',0};
380 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
382 /* create the working key for new 'Test' value */
383 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
384 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
386 /* check NULL data with zero length */
387 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
388 if (GetVersion() & 0x80000000)
389 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
391 ok( !res, "RegSetValueExA returned %d\n", res );
392 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
393 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
394 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
395 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
397 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
398 ok( res == 0, "RegSetValueExA failed error %d\n", res );
400 /* overflow both name and data */
404 strcpy( value, "xxxxxxxxxx" );
405 strcpy( data, "xxxxxxxxxx" );
406 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
407 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
408 ok( val_count == 2, "val_count set to %d\n", val_count );
409 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
410 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
411 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
412 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
418 strcpy( value, "xxxxxxxxxx" );
419 strcpy( data, "xxxxxxxxxx" );
420 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
421 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
422 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
423 ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
424 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
425 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
426 /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
427 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
428 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
429 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
430 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
432 /* overflow empty name */
436 strcpy( value, "xxxxxxxxxx" );
437 strcpy( data, "xxxxxxxxxx" );
438 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
439 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
440 ok( val_count == 0, "val_count set to %d\n", val_count );
441 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
442 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
443 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
444 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
445 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
446 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
452 strcpy( value, "xxxxxxxxxx" );
453 strcpy( data, "xxxxxxxxxx" );
454 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
455 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
456 ok( val_count == 20, "val_count set to %d\n", val_count );
457 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
458 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
459 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
460 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
466 strcpy( value, "xxxxxxxxxx" );
467 strcpy( data, "xxxxxxxxxx" );
468 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
469 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
470 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
471 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
472 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
473 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
474 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
478 SetLastError(0xdeadbeef);
479 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
480 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
482 skip("RegSetValueExW is not implemented\n");
485 ok( res == 0, "RegSetValueExW failed error %d\n", res );
487 /* overflow both name and data */
491 memcpy( valueW, xxxW, sizeof(xxxW) );
492 memcpy( dataW, xxxW, sizeof(xxxW) );
493 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
494 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
495 ok( val_count == 2, "val_count set to %d\n", val_count );
496 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
497 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
498 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
499 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
505 memcpy( valueW, xxxW, sizeof(xxxW) );
506 memcpy( dataW, xxxW, sizeof(xxxW) );
507 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
508 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
509 ok( val_count == 3, "val_count set to %d\n", val_count );
510 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
511 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
512 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
513 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
519 memcpy( valueW, xxxW, sizeof(xxxW) );
520 memcpy( dataW, xxxW, sizeof(xxxW) );
521 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
522 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
523 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
524 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
525 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
526 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
527 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
533 memcpy( valueW, xxxW, sizeof(xxxW) );
534 memcpy( dataW, xxxW, sizeof(xxxW) );
535 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
536 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
537 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
538 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
539 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
540 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
541 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
544 RegDeleteKeyA(test_key, "");
545 RegCloseKey(test_key);
548 static void test_query_value_ex(void)
555 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
556 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
557 ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
558 ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
562 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
563 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
564 /* the type parameter is cleared on Win9x, but is set to a random value on
565 * NT, so don't do that test there. The size parameter is left untouched on Win9x
566 * but cleared on NT+, this can be tested on all platforms.
568 if (GetVersion() & 0x80000000)
570 ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
571 ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
575 trace("test_query_value_ex: type set to: 0x%08x\n", type);
576 ok(size == 0, "size should have been set to 0 instead of %d\n", size);
579 size = sizeof(buffer);
580 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
581 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
582 ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
585 ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
586 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
589 static void test_get_value(void)
596 CHAR expanded[] = "bar\\subdir1";
597 CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
601 skip("RegGetValue not available on this platform\n");
605 /* Invalid parameter */
606 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
607 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
609 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
610 size = type = dw = 0xdeadbeef;
611 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
612 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
613 ok(size == 4, "size=%d\n", size);
614 ok(type == REG_DWORD, "type=%d\n", type);
615 ok(dw == 0x12345678, "dw=%d\n", dw);
617 /* Query by subkey-name */
618 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
619 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
621 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
622 size = type = dw = 0xdeadbeef;
623 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
624 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
625 /* Although the function failed all values are retrieved */
626 ok(size == 4, "size=%d\n", size);
627 ok(type == REG_DWORD, "type=%d\n", type);
628 ok(dw == 0x12345678, "dw=%d\n", dw);
630 /* Test RRF_ZEROONFAILURE */
631 type = dw = 0xdeadbeef; size = 4;
632 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
633 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
634 /* Again all values are retrieved ... */
635 ok(size == 4, "size=%d\n", size);
636 ok(type == REG_DWORD, "type=%d\n", type);
637 /* ... except the buffer, which is zeroed out */
638 ok(dw == 0, "dw=%d\n", dw);
640 /* Test RRF_ZEROONFAILURE with a NULL buffer... */
641 type = size = 0xbadbeef;
642 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
643 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
644 ok(size == 4, "size=%d\n", size);
645 ok(type == REG_DWORD, "type=%d\n", type);
647 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
648 size = type = dw = 0xdeadbeef;
649 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
650 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
651 ok(size == 4, "size=%d\n", size);
652 ok(type == REG_DWORD, "type=%d\n", type);
653 ok(dw == 0x12345678, "dw=%d\n", dw);
655 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
656 size = type = dw = 0xdeadbeef;
657 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
658 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
659 ok(size == 4, "size=%d\n", size);
660 ok(type == REG_BINARY, "type=%d\n", type);
661 ok(dw == 0x12345678, "dw=%d\n", dw);
663 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
664 qw[0] = qw[1] = size = type = 0xdeadbeef;
665 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
666 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
667 ok(size == 8, "size=%d\n", size);
668 ok(type == REG_BINARY, "type=%d\n", type);
669 ok(qw[0] == 0x12345678 &&
670 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
672 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
673 type = dw = 0xdeadbeef; size = 4;
674 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
675 ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
676 ok(dw == 0xdeadbeef, "dw=%d\n", dw);
677 ok(size == 8, "size=%d\n", size);
679 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
680 qw[0] = qw[1] = size = type = 0xdeadbeef;
681 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
682 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
683 ok(size == 8, "size=%d\n", size);
684 ok(type == REG_BINARY, "type=%d\n", type);
685 ok(qw[0] == 0x12345678 &&
686 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
688 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
689 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
690 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
691 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
692 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
693 ok(type == REG_SZ, "type=%d\n", type);
694 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
696 /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
697 type = 0xdeadbeef; size = 0;
698 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
699 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
700 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
701 ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
702 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
703 ok(type == REG_SZ, "type=%d\n", type);
705 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
706 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
707 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
708 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
709 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
710 ok(type == REG_SZ, "type=%d\n", type);
711 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
713 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
715 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
716 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
717 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
718 ok((size == strlen(expanded2)+1) || (size == strlen(sTestpath2)+1),
719 "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
721 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
722 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
723 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
724 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
725 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
726 ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1),
727 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
728 ok(type == REG_SZ, "type=%d\n", type);
729 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
731 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
732 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
733 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
734 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
735 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
736 ok((size == strlen(expanded2)+1) || (size == strlen(sTestpath2)+1),
737 "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
738 ok(type == REG_SZ, "type=%d\n", type);
739 ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
741 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
742 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
743 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
744 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
745 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
746 ok(type == REG_EXPAND_SZ, "type=%d\n", type);
747 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
749 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
751 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
752 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
753 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
754 ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
755 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
757 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
758 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
759 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
761 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
762 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
763 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
766 static void test_reg_open_key(void)
769 HKEY hkResult = NULL;
770 HKEY hkPreserve = NULL;
772 /* successful open */
773 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
774 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
775 ok(hkResult != NULL, "expected hkResult != NULL\n");
776 hkPreserve = hkResult;
778 /* these tests fail on Win9x, but we want to be compatible with NT, so
779 * run them if we can */
780 if (!(GetVersion() & 0x80000000))
782 /* open same key twice */
783 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
784 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
785 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
786 ok(hkResult != NULL, "hkResult != NULL\n");
787 RegCloseKey(hkResult);
789 /* open nonexistent key
790 * check that hkResult is set to NULL
792 hkResult = hkPreserve;
793 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
794 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
795 ok(hkResult == NULL, "expected hkResult == NULL\n");
797 /* open the same nonexistent key again to make sure the key wasn't created */
798 hkResult = hkPreserve;
799 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
800 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
801 ok(hkResult == NULL, "expected hkResult == NULL\n");
803 /* send in NULL lpSubKey
804 * check that hkResult receives the value of hKey
806 hkResult = hkPreserve;
807 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
808 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
809 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
811 /* send empty-string in lpSubKey */
812 hkResult = hkPreserve;
813 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
814 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
815 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
817 /* send in NULL lpSubKey and NULL hKey
818 * hkResult is set to NULL
820 hkResult = hkPreserve;
821 ret = RegOpenKeyA(NULL, NULL, &hkResult);
822 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
823 ok(hkResult == NULL, "expected hkResult == NULL\n");
826 /* only send NULL hKey
827 * the value of hkResult remains unchanged
829 hkResult = hkPreserve;
830 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
831 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
832 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
833 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
834 RegCloseKey(hkResult);
836 /* send in NULL hkResult */
837 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
838 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
840 /* beginning backslash character */
841 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
842 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
843 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
844 , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
847 static void test_reg_create_key(void)
851 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
852 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
853 /* should succeed: all versions of Windows ignore the access rights
854 * to the parent handle */
855 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
856 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
859 RegDeleteKey(hkey2, "");
860 RegDeleteKey(hkey1, "");
862 /* beginning backslash character */
863 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
864 if (!(GetVersion() & 0x80000000))
865 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
867 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
868 RegDeleteKey(hkey1, NULL);
872 static void test_reg_close_key(void)
877 /* successfully close key
878 * hkHandle remains changed after call to RegCloseKey
880 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
881 ret = RegCloseKey(hkHandle);
882 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
884 /* try to close the key twice */
885 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
886 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
887 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
889 /* try to close a NULL handle */
890 ret = RegCloseKey(NULL);
891 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
892 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
894 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
895 * win98 doesn't give a new handle when the same key is opened.
896 * Not re-opening will make some next tests fail.
898 if (hkey_main == hkHandle)
900 trace("The main handle is most likely closed, so re-opening\n");
901 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
905 static void test_reg_delete_key(void)
909 ret = RegDeleteKey(hkey_main, NULL);
911 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
912 * there are also no subkeys available it will delete the key pointed to by hkey_main.
913 * Not re-creating will make some next tests fail.
915 if (ret == ERROR_SUCCESS)
917 trace("We are probably running on NT4 or W2K as the main key is deleted,"
918 " re-creating the main key\n");
922 ok(ret == ERROR_INVALID_PARAMETER ||
923 ret == ERROR_ACCESS_DENIED ||
924 ret == ERROR_BADKEY, /* Win95 */
928 static void test_reg_save_key(void)
932 ret = RegSaveKey(hkey_main, "saved_key", NULL);
933 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
936 static void test_reg_load_key(void)
941 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
942 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
944 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
945 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
947 RegCloseKey(hkHandle);
950 static void test_reg_unload_key(void)
954 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
955 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
957 DeleteFile("saved_key");
958 DeleteFile("saved_key.LOG");
961 static BOOL set_privileges(LPCSTR privilege, BOOL set)
967 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
970 if(!LookupPrivilegeValue(NULL, privilege, &luid))
976 tp.PrivilegeCount = 1;
977 tp.Privileges[0].Luid = luid;
980 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
982 tp.Privileges[0].Attributes = 0;
984 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
985 if (GetLastError() != ERROR_SUCCESS)
995 /* tests that show that RegConnectRegistry and
996 OpenSCManager accept computer names without the
997 \\ prefix (what MSDN says). */
998 static void test_regconnectregistry( void)
1000 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1001 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1002 DWORD len = sizeof(compName) ;
1008 SetLastError(0xdeadbeef);
1009 ret = GetComputerNameA(compName, &len);
1010 ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1013 lstrcpyA(netwName, "\\\\");
1014 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1016 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1017 ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1018 if( !retl) RegCloseKey( hkey);
1020 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1021 ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1022 if( !retl) RegCloseKey( hkey);
1024 SetLastError(0xdeadbeef);
1025 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
1026 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1028 skip("OpenSCManagerA is not implemented\n");
1032 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1033 CloseServiceHandle( schnd);
1035 SetLastError(0xdeadbeef);
1036 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
1037 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1038 CloseServiceHandle( schnd);
1042 static void test_reg_query_value(void)
1049 static const WCHAR expected[] = {'d','a','t','a',0};
1051 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1052 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1054 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1055 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1057 /* try an invalid hkey */
1058 SetLastError(0xdeadbeef);
1060 ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1061 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1062 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1063 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1065 /* try a NULL hkey */
1066 SetLastError(0xdeadbeef);
1068 ret = RegQueryValueA(NULL, "subkey", val, &size);
1069 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1070 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1071 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1073 /* try a NULL value */
1075 ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1076 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1077 ok(size == 5, "Expected 5, got %d\n", size);
1079 /* try a NULL size */
1080 SetLastError(0xdeadbeef);
1082 ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1083 ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1084 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1085 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1087 /* try a NULL value and size */
1088 ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1089 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1091 /* try a size too small */
1092 SetLastError(0xdeadbeef);
1095 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1096 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1097 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1098 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1099 ok(size == 5, "Expected 5, got %d\n", size);
1101 /* successfully read the value using 'subkey' */
1103 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1104 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1105 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1106 ok(size == 5, "Expected 5, got %d\n", size);
1108 /* successfully read the value using the subkey key */
1110 ret = RegQueryValueA(subkey, NULL, val, &size);
1111 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1112 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1113 ok(size == 5, "Expected 5, got %d\n", size);
1115 /* unicode - try size too small */
1116 SetLastError(0xdeadbeef);
1119 ret = RegQueryValueW(subkey, NULL, valW, &size);
1120 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1122 skip("RegQueryValueW is not implemented\n");
1125 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1126 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1127 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1128 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1130 /* unicode - try size in WCHARS */
1131 SetLastError(0xdeadbeef);
1132 size = sizeof(valW) / sizeof(WCHAR);
1133 ret = RegQueryValueW(subkey, NULL, valW, &size);
1134 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1135 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1136 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1137 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1139 /* unicode - successfully read the value */
1140 size = sizeof(valW);
1141 ret = RegQueryValueW(subkey, NULL, valW, &size);
1142 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1143 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1144 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1146 /* unicode - set the value without a NULL terminator */
1147 ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1148 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1150 /* unicode - read the unterminated value, value is terminated for us */
1151 memset(valW, 'a', sizeof(valW));
1152 size = sizeof(valW);
1153 ret = RegQueryValueW(subkey, NULL, valW, &size);
1154 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1155 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1156 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1159 RegDeleteKeyA(subkey, "");
1160 RegCloseKey(subkey);
1163 static void test_reg_delete_tree(void)
1165 CHAR buffer[MAX_PATH];
1166 HKEY subkey, subkey2;
1169 if(!pRegDeleteTreeA) {
1170 skip("Skipping RegDeleteTreeA tests, function not present\n");
1174 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1175 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1176 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1177 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1178 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1179 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1180 ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1181 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1182 ret = RegCloseKey(subkey2);
1183 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1185 ret = pRegDeleteTreeA(subkey, "subkey2");
1186 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1187 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1188 "subkey2 was not deleted\n");
1190 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1191 "Default value of subkey not longer present\n");
1193 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1194 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1195 ret = RegCloseKey(subkey2);
1196 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1197 ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1198 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1199 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1200 "subkey2 was not deleted\n");
1201 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1202 "Default value of subkey not longer present\n");
1204 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1205 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1206 ret = RegCloseKey(subkey2);
1207 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1208 ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1209 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1210 ret = RegCloseKey(subkey2);
1211 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1212 ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1213 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1214 ret = pRegDeleteTreeA(subkey, NULL);
1215 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1216 ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1217 "subkey was deleted\n");
1218 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1219 "subkey2 was not deleted\n");
1220 ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1221 "subkey3 was not deleted\n");
1223 ret = RegQueryValueA(subkey, NULL, buffer, &size);
1224 ok(ret == ERROR_SUCCESS,
1225 "Default value of subkey is not present\n");
1226 ok(!lstrlenA(buffer),
1227 "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1229 ok(RegQueryValueA(subkey, "value", buffer, &size),
1230 "Value is still present\n");
1232 ret = pRegDeleteTreeA(hkey_main, "not-here");
1233 ok(ret == ERROR_FILE_NOT_FOUND,
1234 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1237 START_TEST(registry)
1239 /* Load pointers for functions that are not available in all Windows versions */
1244 create_test_entries();
1246 test_query_value_ex();
1248 test_reg_open_key();
1249 test_reg_create_key();
1250 test_reg_close_key();
1251 test_reg_delete_key();
1252 test_reg_query_value();
1254 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1255 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1256 set_privileges(SE_RESTORE_NAME, TRUE))
1258 test_reg_save_key();
1259 test_reg_load_key();
1260 test_reg_unload_key();
1262 set_privileges(SE_BACKUP_NAME, FALSE);
1263 set_privileges(SE_RESTORE_NAME, FALSE);
1266 test_reg_delete_tree();
1269 delete_key( hkey_main );
1271 test_regconnectregistry();