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 /* default implementation of wine_dbgstr_an */
60 static const char *wine_debugstr_an( const char *str, int n )
62 static const char hex[16] = "0123456789abcdef";
66 if (!((ULONG_PTR)str >> 16))
68 if (!str) return "(null)";
69 res = get_temp_buffer( 6 );
70 sprintf( res, "#%04x", LOWORD(str) );
73 if (n == -1) n = strlen(str);
75 size = 10 + min( 300, n * 4 );
76 dst = res = get_temp_buffer( size );
78 while (n-- > 0 && dst <= res + size - 9)
80 unsigned char c = *str++;
83 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
84 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
85 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
86 case '"': *dst++ = '\\'; *dst++ = '"'; break;
87 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
89 if (c >= ' ' && c <= 126)
95 *dst++ = hex[(c >> 4) & 0x0f];
96 *dst++ = hex[c & 0x0f];
111 /* default implementation of wine_dbgstr_wn */
112 static const char *wine_debugstr_wn( const WCHAR *str, int n )
118 if (!str) return "(null)";
119 res = get_temp_buffer( 6 );
120 sprintf( res, "#%04x", LOWORD(str) );
123 if (n == -1) n = lstrlenW(str);
125 else if (n > 200) n = 200;
126 dst = res = get_temp_buffer( n * 5 + 7 );
134 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
135 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
136 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
137 case '"': *dst++ = '\\'; *dst++ = '"'; break;
138 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
140 if (c >= ' ' && c <= 126)
145 sprintf(dst,"%04x",c);
162 #define ADVAPI32_GET_PROC(func) \
163 p ## func = (void*)GetProcAddress(hadvapi32, #func); \
165 trace("GetProcAddress(%s) failed\n", #func);
167 static void InitFunctionPtrs(void)
169 hadvapi32 = GetModuleHandleA("advapi32.dll");
171 /* This function was introduced with Windows 2003 SP1 */
172 ADVAPI32_GET_PROC(RegGetValueA)
173 ADVAPI32_GET_PROC(RegDeleteTreeA)
176 /* delete key and all its subkeys */
177 static DWORD delete_key( HKEY hkey )
182 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
185 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
187 ret = delete_key( tmp );
192 if (ret != ERROR_NO_MORE_ITEMS) return ret;
193 RegDeleteKeyA( hkey, "" );
197 static void setup_main_key(void)
199 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
201 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
204 static void test_hkey_main_Value_A(LPCSTR name, LPCSTR string,
207 DWORD ret, type, cbData;
210 static const char nA[]={'N', 0};
214 /* When successful RegQueryValueExA() leaves GLE as is,
215 * so we must reset it to detect unimplemented functions.
217 SetLastError(0xdeadbeef);
218 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
219 GLE = GetLastError();
220 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
221 /* It is wrong for the Ansi version to not be implemented */
222 ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
223 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
225 str_byte_len = (string ? lstrlenA(string) : 0) + 1;
226 ok(type == REG_SZ, "RegQueryValueExA returned type %d\n", type);
227 ok(cbData == full_byte_len || cbData == str_byte_len /* Win9x */,
228 "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
230 value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
233 ret = RegQueryValueExA(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
234 GLE = GetLastError();
235 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
238 /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
239 ok(strcmp(value, nA) == 0 || (cbData == 1 && *value == '\0') /* Win9x */,
240 "RegQueryValueExA failed: '%s' != '%s'\n", value, string);
244 ok(memcmp(value, string, cbData) == 0, "RegQueryValueExA failed: %s/%d != %s/%d\n",
245 wine_debugstr_an(value, cbData), cbData,
246 wine_debugstr_an(string, full_byte_len), full_byte_len);
248 HeapFree(GetProcessHeap(), 0, value);
251 static void test_hkey_main_Value_W(LPCWSTR name, LPCWSTR string,
254 DWORD ret, type, cbData;
256 static const WCHAR nW[]={'N', 0};
260 /* When successful RegQueryValueExW() leaves GLE as is,
261 * so we must reset it to detect unimplemented functions.
263 SetLastError(0xdeadbeef);
264 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
265 GLE = GetLastError();
266 ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
267 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
269 ok(type == REG_SZ, "RegQueryValueExW returned type %d\n", type);
270 ok(cbData == full_byte_len,
271 "cbData=%d instead of %d\n", cbData, full_byte_len);
273 value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
276 ret = RegQueryValueExW(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
277 GLE = GetLastError();
278 ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
281 /* When cbData == 0, RegQueryValueExW() should not modify the buffer */
284 ok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
285 wine_debugstr_wn(value, cbData / sizeof(WCHAR)), cbData,
286 wine_debugstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len / sizeof(WCHAR));
287 HeapFree(GetProcessHeap(), 0, value);
290 static void test_set_value(void)
294 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
295 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};
296 static const WCHAR emptyW[] = {0};
297 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
298 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};
300 static const char name1A[] = "CleanSingleString";
301 static const char name2A[] = "SomeIntraZeroedString";
302 static const char emptyA[] = "";
303 static const char string1A[] = "ThisNeverBreaks";
304 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
306 /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
307 * Surprisingly enough we're supposed to get zero bytes out of it.
308 * FIXME: Wine's on-disk file format does not differentiate this with
309 * regular empty strings but there's no way to test as it requires
310 * stopping the wineserver.
312 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
313 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
314 test_hkey_main_Value_A(name1A, NULL, 0);
315 test_hkey_main_Value_W(name1W, NULL, 0);
317 /* test RegSetValueExA with an empty string */
318 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
319 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
320 test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
321 test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
323 /* test RegSetValueExA with normal string */
324 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
325 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
326 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
327 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
329 /* test RegSetValueExA with intrazeroed string */
330 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
331 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
332 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
333 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
335 /* 9x doesn't support W-calls, so don't test them then */
336 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
338 /* test RegSetValueExW with normal string */
339 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
340 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
341 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
342 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
344 /* test RegSetValueExW with intrazeroed string */
345 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
346 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
347 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
348 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
351 static void create_test_entries(void)
353 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
355 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
356 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
358 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
359 "RegSetValueExA failed\n");
360 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
361 "RegSetValueExA failed\n");
362 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
363 "RegSetValueExA failed\n");
364 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
365 "RegSetValueExA failed\n");
366 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
367 "RegSetValueExA failed\n");
368 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
369 "RegSetValueExA failed\n");
372 static void test_enum_value(void)
376 char value[20], data[20];
377 WCHAR valueW[20], dataW[20];
378 DWORD val_count, data_count, type;
379 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
380 static const WCHAR testW[] = {'T','e','s','t',0};
381 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
383 /* create the working key for new 'Test' value */
384 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
385 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
387 /* check NULL data with zero length */
388 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
389 if (GetVersion() & 0x80000000)
390 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
392 ok( !res, "RegSetValueExA returned %d\n", res );
393 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
394 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
395 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
396 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
398 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
399 ok( res == 0, "RegSetValueExA failed error %d\n", res );
401 /* overflow both name and data */
405 strcpy( value, "xxxxxxxxxx" );
406 strcpy( data, "xxxxxxxxxx" );
407 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
408 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
409 ok( val_count == 2, "val_count set to %d\n", val_count );
410 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
411 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
412 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
413 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
419 strcpy( value, "xxxxxxxxxx" );
420 strcpy( data, "xxxxxxxxxx" );
421 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
422 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
423 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
424 ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
425 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
426 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
427 /* v5.1.2600.0 (XP Home and Proffesional) does not touch value or data in this case */
428 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
429 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
430 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
431 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
433 /* overflow empty name */
437 strcpy( value, "xxxxxxxxxx" );
438 strcpy( data, "xxxxxxxxxx" );
439 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
440 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
441 ok( val_count == 0, "val_count set to %d\n", val_count );
442 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
443 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
444 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
445 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
446 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
447 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
453 strcpy( value, "xxxxxxxxxx" );
454 strcpy( data, "xxxxxxxxxx" );
455 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
456 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
457 ok( val_count == 20, "val_count set to %d\n", val_count );
458 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
459 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
460 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
461 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
467 strcpy( value, "xxxxxxxxxx" );
468 strcpy( data, "xxxxxxxxxx" );
469 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
470 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
471 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
472 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
473 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
474 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
475 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
479 SetLastError(0xdeadbeef);
480 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
481 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
483 skip("RegSetValueExW is not implemented\n");
486 ok( res == 0, "RegSetValueExW failed error %d\n", res );
488 /* overflow both name and data */
492 memcpy( valueW, xxxW, sizeof(xxxW) );
493 memcpy( dataW, xxxW, sizeof(xxxW) );
494 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &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*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
498 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
499 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
500 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
506 memcpy( valueW, xxxW, sizeof(xxxW) );
507 memcpy( dataW, xxxW, sizeof(xxxW) );
508 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
509 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
510 ok( val_count == 3, "val_count set to %d\n", val_count );
511 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
512 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
513 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
514 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
520 memcpy( valueW, xxxW, sizeof(xxxW) );
521 memcpy( dataW, xxxW, sizeof(xxxW) );
522 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
523 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
524 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
525 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
526 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
527 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
528 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
534 memcpy( valueW, xxxW, sizeof(xxxW) );
535 memcpy( dataW, xxxW, sizeof(xxxW) );
536 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
537 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
538 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
539 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
540 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
541 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
542 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
545 RegDeleteKeyA(test_key, "");
546 RegCloseKey(test_key);
549 static void test_query_value_ex(void)
556 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
557 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
558 ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
559 ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
563 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
564 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
565 /* the type parameter is cleared on Win9x, but is set to a random value on
566 * NT, so don't do that test there. The size parameter is left untouched on Win9x
567 * but cleared on NT+, this can be tested on all platforms.
569 if (GetVersion() & 0x80000000)
571 ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
572 ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
576 trace("test_query_value_ex: type set to: 0x%08x\n", type);
577 ok(size == 0, "size should have been set to 0 instead of %d\n", size);
580 size = sizeof(buffer);
581 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
582 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
583 ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
586 static void test_get_value(void)
593 CHAR expanded[] = "bar\\subdir1";
597 skip("RegGetValue not available on this platform\n");
601 /* Invalid parameter */
602 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
603 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
605 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
606 size = type = dw = 0xdeadbeef;
607 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
608 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
609 ok(size == 4, "size=%d\n", size);
610 ok(type == REG_DWORD, "type=%d\n", type);
611 ok(dw == 0x12345678, "dw=%d\n", dw);
613 /* Query by subkey-name */
614 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
615 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
617 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
618 size = type = dw = 0xdeadbeef;
619 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
620 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
621 /* Although the function failed all values are retrieved */
622 ok(size == 4, "size=%d\n", size);
623 ok(type == REG_DWORD, "type=%d\n", type);
624 ok(dw == 0x12345678, "dw=%d\n", dw);
626 /* Test RRF_ZEROONFAILURE */
627 type = dw = 0xdeadbeef; size = 4;
628 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
629 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
630 /* Again all values are retrieved ... */
631 ok(size == 4, "size=%d\n", size);
632 ok(type == REG_DWORD, "type=%d\n", type);
633 /* ... except the buffer, which is zeroed out */
634 ok(dw == 0, "dw=%d\n", dw);
636 /* Test RRF_ZEROONFAILURE with a NULL buffer... */
637 type = size = 0xbadbeef;
638 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
639 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
640 ok(size == 4, "size=%d\n", size);
641 ok(type == REG_DWORD, "type=%d\n", type);
643 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
644 size = type = dw = 0xdeadbeef;
645 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
646 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
647 ok(size == 4, "size=%d\n", size);
648 ok(type == REG_DWORD, "type=%d\n", type);
649 ok(dw == 0x12345678, "dw=%d\n", dw);
651 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
652 size = type = dw = 0xdeadbeef;
653 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
654 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
655 ok(size == 4, "size=%d\n", size);
656 ok(type == REG_BINARY, "type=%d\n", type);
657 ok(dw == 0x12345678, "dw=%d\n", dw);
659 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
660 qw[0] = qw[1] = size = type = 0xdeadbeef;
661 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
662 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
663 ok(size == 8, "size=%d\n", size);
664 ok(type == REG_BINARY, "type=%d\n", type);
665 ok(qw[0] == 0x12345678 &&
666 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
668 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
669 type = dw = 0xdeadbeef; size = 4;
670 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
671 ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
672 ok(dw == 0xdeadbeef, "dw=%d\n", dw);
673 ok(size == 8, "size=%d\n", size);
675 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
676 qw[0] = qw[1] = size = type = 0xdeadbeef;
677 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
678 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
679 ok(size == 8, "size=%d\n", size);
680 ok(type == REG_BINARY, "type=%d\n", type);
681 ok(qw[0] == 0x12345678 &&
682 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
684 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
685 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
686 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
687 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
688 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
689 ok(type == REG_SZ, "type=%d\n", type);
690 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
692 /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
693 type = 0xdeadbeef; size = 0;
694 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
695 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
696 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
697 ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
698 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
699 ok(type == REG_SZ, "type=%d\n", type);
701 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
702 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
703 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
704 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
705 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
706 ok(type == REG_SZ, "type=%d\n", type);
707 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
709 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
711 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
712 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
713 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
714 ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1),
715 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
717 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
718 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
719 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
720 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
721 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
722 ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1),
723 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
724 ok(type == REG_SZ, "type=%d\n", type);
725 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
727 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
728 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
729 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
730 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
731 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
732 ok(type == REG_EXPAND_SZ, "type=%d\n", type);
733 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
735 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
737 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
738 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
739 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
740 ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
741 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
743 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
744 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
745 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
747 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
748 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
749 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
752 static void test_reg_open_key(void)
755 HKEY hkResult = NULL;
756 HKEY hkPreserve = NULL;
758 /* successful open */
759 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
760 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
761 ok(hkResult != NULL, "expected hkResult != NULL\n");
762 hkPreserve = hkResult;
764 /* these tests fail on Win9x, but we want to be compatible with NT, so
765 * run them if we can */
766 if (!(GetVersion() & 0x80000000))
768 /* open same key twice */
769 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
770 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
771 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
772 ok(hkResult != NULL, "hkResult != NULL\n");
773 RegCloseKey(hkResult);
775 /* open nonexistent key
776 * check that hkResult is set to NULL
778 hkResult = hkPreserve;
779 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
780 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
781 ok(hkResult == NULL, "expected hkResult == NULL\n");
783 /* open the same nonexistent key again to make sure the key wasn't created */
784 hkResult = hkPreserve;
785 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
786 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
787 ok(hkResult == NULL, "expected hkResult == NULL\n");
789 /* send in NULL lpSubKey
790 * check that hkResult receives the value of hKey
792 hkResult = hkPreserve;
793 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
794 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
795 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
797 /* send empty-string in lpSubKey */
798 hkResult = hkPreserve;
799 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
800 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
801 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
803 /* send in NULL lpSubKey and NULL hKey
804 * hkResult is set to NULL
806 hkResult = hkPreserve;
807 ret = RegOpenKeyA(NULL, NULL, &hkResult);
808 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
809 ok(hkResult == NULL, "expected hkResult == NULL\n");
812 /* only send NULL hKey
813 * the value of hkResult remains unchanged
815 hkResult = hkPreserve;
816 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
817 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
818 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
819 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
820 RegCloseKey(hkResult);
822 /* send in NULL hkResult */
823 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
824 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
826 /* beginning backslash character */
827 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
828 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
829 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
830 , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
833 static void test_reg_create_key(void)
837 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
838 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
839 /* should succeed: all versions of Windows ignore the access rights
840 * to the parent handle */
841 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
842 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
845 RegDeleteKey(hkey2, "");
846 RegDeleteKey(hkey1, "");
848 /* beginning backslash character */
849 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
850 if (!(GetVersion() & 0x80000000))
851 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
853 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
854 RegDeleteKey(hkey1, NULL);
858 static void test_reg_close_key(void)
863 /* successfully close key
864 * hkHandle remains changed after call to RegCloseKey
866 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
867 ret = RegCloseKey(hkHandle);
868 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
870 /* try to close the key twice */
871 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
872 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
873 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
875 /* try to close a NULL handle */
876 ret = RegCloseKey(NULL);
877 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
878 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
880 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
881 * win98 doesn't give a new handle when the same key is opened.
882 * Not re-opening will make some next tests fail.
884 if (hkey_main == hkHandle)
886 trace("The main handle is most likely closed, so re-opening\n");
887 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
891 static void test_reg_delete_key(void)
895 ret = RegDeleteKey(hkey_main, NULL);
897 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
898 * there are also no subkeys available it will delete the key pointed to by hkey_main.
899 * Not re-creating will make some next tests fail.
901 if (ret == ERROR_SUCCESS)
903 trace("We are probably running on NT4 or W2K as the main key is deleted,"
904 " re-creating the main key\n");
908 ok(ret == ERROR_INVALID_PARAMETER ||
909 ret == ERROR_ACCESS_DENIED ||
910 ret == ERROR_BADKEY, /* Win95 */
914 static void test_reg_save_key(void)
918 ret = RegSaveKey(hkey_main, "saved_key", NULL);
919 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
922 static void test_reg_load_key(void)
927 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
928 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
930 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
931 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
933 RegCloseKey(hkHandle);
936 static void test_reg_unload_key(void)
940 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
941 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
943 DeleteFile("saved_key");
944 DeleteFile("saved_key.LOG");
947 static BOOL set_privileges(LPCSTR privilege, BOOL set)
953 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
956 if(!LookupPrivilegeValue(NULL, privilege, &luid))
962 tp.PrivilegeCount = 1;
963 tp.Privileges[0].Luid = luid;
966 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
968 tp.Privileges[0].Attributes = 0;
970 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
971 if (GetLastError() != ERROR_SUCCESS)
981 /* tests that show that RegConnectRegistry and
982 OpenSCManager accept computer names without the
983 \\ prefix (what MSDN says). */
984 static void test_regconnectregistry( void)
986 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
987 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
988 DWORD len = sizeof(compName) ;
994 SetLastError(0xdeadbeef);
995 ret = GetComputerNameA(compName, &len);
996 ok( ret, "GetComputerName failed err = %d\n", GetLastError());
999 lstrcpyA(netwName, "\\\\");
1000 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1002 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1003 ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1004 if( !retl) RegCloseKey( hkey);
1006 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1007 ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1008 if( !retl) RegCloseKey( hkey);
1010 SetLastError(0xdeadbeef);
1011 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
1012 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1014 skip("OpenSCManagerA is not implemented\n");
1018 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1019 CloseServiceHandle( schnd);
1021 SetLastError(0xdeadbeef);
1022 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
1023 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1024 CloseServiceHandle( schnd);
1028 static void test_reg_query_value(void)
1035 static const WCHAR expected[] = {'d','a','t','a',0};
1037 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1038 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1040 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1041 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1043 /* try an invalid hkey */
1044 SetLastError(0xdeadbeef);
1046 ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1047 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1048 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1049 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1051 /* try a NULL hkey */
1052 SetLastError(0xdeadbeef);
1054 ret = RegQueryValueA(NULL, "subkey", val, &size);
1055 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1056 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1057 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1059 /* try a NULL value */
1061 ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1062 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1063 ok(size == 5, "Expected 5, got %d\n", size);
1065 /* try a NULL size */
1066 SetLastError(0xdeadbeef);
1068 ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1069 ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1070 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1071 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1073 /* try a NULL value and size */
1074 ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1075 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1077 /* try a size too small */
1078 SetLastError(0xdeadbeef);
1081 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1082 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1083 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1084 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1085 ok(size == 5, "Expected 5, got %d\n", size);
1087 /* successfully read the value using 'subkey' */
1089 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1090 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1091 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1092 ok(size == 5, "Expected 5, got %d\n", size);
1094 /* successfully read the value using the subkey key */
1096 ret = RegQueryValueA(subkey, NULL, val, &size);
1097 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1098 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1099 ok(size == 5, "Expected 5, got %d\n", size);
1101 /* unicode - try size too small */
1102 SetLastError(0xdeadbeef);
1105 ret = RegQueryValueW(subkey, NULL, valW, &size);
1106 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1108 skip("RegQueryValueW is not implemented\n");
1111 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1112 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1113 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1114 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1116 /* unicode - try size in WCHARS */
1117 SetLastError(0xdeadbeef);
1118 size = sizeof(valW) / sizeof(WCHAR);
1119 ret = RegQueryValueW(subkey, NULL, valW, &size);
1120 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1121 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1122 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1123 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1125 /* unicode - successfully read the value */
1126 size = sizeof(valW);
1127 ret = RegQueryValueW(subkey, NULL, valW, &size);
1128 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1129 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1130 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1132 /* unicode - set the value without a NULL terminator */
1133 ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1134 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1136 /* unicode - read the unterminated value, value is terminated for us */
1137 memset(valW, 'a', sizeof(valW));
1138 size = sizeof(valW);
1139 ret = RegQueryValueW(subkey, NULL, valW, &size);
1140 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1141 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1142 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1145 RegDeleteKeyA(subkey, "");
1146 RegCloseKey(subkey);
1149 static void test_reg_delete_tree(void)
1151 CHAR buffer[MAX_PATH];
1152 HKEY subkey, subkey2;
1155 if(!pRegDeleteTreeA) {
1156 skip("Skipping RegDeleteTreeA tests, function not present\n");
1160 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1161 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1162 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1163 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1164 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1165 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1166 ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1167 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1168 ret = RegCloseKey(subkey2);
1169 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1171 ret = pRegDeleteTreeA(subkey, "subkey2");
1172 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1173 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1174 "subkey2 was not deleted\n");
1176 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1177 "Default value of subkey not longer present\n");
1179 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1180 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1181 ret = RegCloseKey(subkey2);
1182 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1183 ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1184 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1185 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1186 "subkey2 was not deleted\n");
1187 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1188 "Default value of subkey not longer present\n");
1190 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1191 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1192 ret = RegCloseKey(subkey2);
1193 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1194 ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1195 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1196 ret = RegCloseKey(subkey2);
1197 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1198 ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1199 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1200 ret = pRegDeleteTreeA(subkey, NULL);
1201 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1202 ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1203 "subkey was deleted\n");
1204 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1205 "subkey2 was not deleted\n");
1206 ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1207 "subkey3 was not deleted\n");
1209 ret = RegQueryValueA(subkey, NULL, buffer, &size);
1210 ok(ret == ERROR_SUCCESS,
1211 "Default value of subkey is not present\n");
1212 ok(!lstrlenA(buffer),
1213 "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1215 ok(RegQueryValueA(subkey, "value", buffer, &size),
1216 "Value is still present\n");
1218 ret = pRegDeleteTreeA(hkey_main, "not-here");
1219 ok(ret == ERROR_FILE_NOT_FOUND,
1220 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1223 START_TEST(registry)
1225 /* Load pointers for functions that are not available in all Windows versions */
1230 create_test_entries();
1232 test_query_value_ex();
1234 test_reg_open_key();
1235 test_reg_create_key();
1236 test_reg_close_key();
1237 test_reg_delete_key();
1238 test_reg_query_value();
1240 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1241 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1242 set_privileges(SE_RESTORE_NAME, TRUE))
1244 test_reg_save_key();
1245 test_reg_load_key();
1246 test_reg_unload_key();
1248 set_privileges(SE_BACKUP_NAME, FALSE);
1249 set_privileges(SE_RESTORE_NAME, FALSE);
1252 test_reg_delete_tree();
1255 delete_key( hkey_main );
1257 test_regconnectregistry();