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
), cbData
,
286 wine_debugstr_wn(string
, full_byte_len
), full_byte_len
);
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 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
602 size
= type
= dw
= 0xdeadbeef;
603 ret
= pRegGetValueA(hkey_main
, NULL
, "DWORD", RRF_RT_REG_DWORD
, &type
, &dw
, &size
);
604 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
605 ok(size
== 4, "size=%d\n", size
);
606 ok(type
== REG_DWORD
, "type=%d\n", type
);
607 ok(dw
== 0x12345678, "dw=%d\n", dw
);
609 /* Query by subkey-name */
610 ret
= pRegGetValueA(HKEY_CURRENT_USER
, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD
, NULL
, NULL
, NULL
);
611 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
613 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
614 size
= type
= dw
= 0xdeadbeef;
615 ret
= pRegGetValueA(hkey_main
, NULL
, "DWORD", RRF_RT_REG_BINARY
, &type
, &dw
, &size
);
616 ok(ret
== ERROR_UNSUPPORTED_TYPE
, "ret=%d\n", ret
);
617 /* Although the function failed all values are retrieved */
618 ok(size
== 4, "size=%d\n", size
);
619 ok(type
== REG_DWORD
, "type=%d\n", type
);
620 ok(dw
== 0x12345678, "dw=%d\n", dw
);
622 /* Test RRF_ZEROONFAILURE */
623 type
= dw
= 0xdeadbeef; size
= 4;
624 ret
= pRegGetValueA(hkey_main
, NULL
, "DWORD", RRF_RT_REG_SZ
|RRF_ZEROONFAILURE
, &type
, &dw
, &size
);
625 ok(ret
== ERROR_UNSUPPORTED_TYPE
, "ret=%d\n", ret
);
626 /* Again all values are retrieved ... */
627 ok(size
== 4, "size=%d\n", size
);
628 ok(type
== REG_DWORD
, "type=%d\n", type
);
629 /* ... except the buffer, which is zeroed out */
630 ok(dw
== 0, "dw=%d\n", dw
);
632 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
633 size
= type
= dw
= 0xdeadbeef;
634 ret
= pRegGetValueA(hkey_main
, NULL
, "DWORD", RRF_RT_DWORD
, &type
, &dw
, &size
);
635 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
636 ok(size
== 4, "size=%d\n", size
);
637 ok(type
== REG_DWORD
, "type=%d\n", type
);
638 ok(dw
== 0x12345678, "dw=%d\n", dw
);
640 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
641 size
= type
= dw
= 0xdeadbeef;
642 ret
= pRegGetValueA(hkey_main
, NULL
, "BIN32", RRF_RT_DWORD
, &type
, &dw
, &size
);
643 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
644 ok(size
== 4, "size=%d\n", size
);
645 ok(type
== REG_BINARY
, "type=%d\n", type
);
646 ok(dw
== 0x12345678, "dw=%d\n", dw
);
648 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
649 qw
[0] = qw
[1] = size
= type
= 0xdeadbeef;
650 ret
= pRegGetValueA(hkey_main
, NULL
, "BIN64", RRF_RT_DWORD
, &type
, qw
, &size
);
651 ok(ret
== ERROR_DATATYPE_MISMATCH
, "ret=%d\n", ret
);
652 ok(size
== 8, "size=%d\n", size
);
653 ok(type
== REG_BINARY
, "type=%d\n", type
);
654 ok(qw
[0] == 0x12345678 &&
655 qw
[1] == 0x87654321, "qw={%d,%d}\n", qw
[0], qw
[1]);
657 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
658 type
= dw
= 0xdeadbeef; size
= 4;
659 ret
= pRegGetValueA(hkey_main
, NULL
, "BIN64", RRF_RT_REG_BINARY
, &type
, &dw
, &size
);
660 ok(ret
== ERROR_MORE_DATA
, "ret=%d\n", ret
);
661 ok(dw
== 0xdeadbeef, "dw=%d\n", dw
);
662 ok(size
== 8, "size=%d\n", size
);
664 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
665 qw
[0] = qw
[1] = size
= type
= 0xdeadbeef;
666 ret
= pRegGetValueA(hkey_main
, NULL
, "BIN64", RRF_RT_QWORD
, &type
, qw
, &size
);
667 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
668 ok(size
== 8, "size=%d\n", size
);
669 ok(type
== REG_BINARY
, "type=%d\n", type
);
670 ok(qw
[0] == 0x12345678 &&
671 qw
[1] == 0x87654321, "qw={%d,%d}\n", qw
[0], qw
[1]);
673 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
674 buf
[0] = 0; type
= 0xdeadbeef; size
= sizeof(buf
);
675 ret
= pRegGetValueA(hkey_main
, NULL
, "TP1_SZ", RRF_RT_REG_SZ
, &type
, buf
, &size
);
676 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
677 ok(size
== strlen(sTestpath1
)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1
), size
);
678 ok(type
== REG_SZ
, "type=%d\n", type
);
679 ok(!strcmp(sTestpath1
, buf
), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1
, buf
);
681 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
682 buf
[0] = 0; type
= 0xdeadbeef; size
= sizeof(buf
);
683 ret
= pRegGetValueA(hkey_main
, NULL
, "TP1_SZ", RRF_RT_REG_SZ
|RRF_NOEXPAND
, &type
, buf
, &size
);
684 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
685 ok(size
== strlen(sTestpath1
)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1
), size
);
686 ok(type
== REG_SZ
, "type=%d\n", type
);
687 ok(!strcmp(sTestpath1
, buf
), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1
, buf
);
689 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
690 buf
[0] = 0; type
= 0xdeadbeef; size
= sizeof(buf
);
691 ret
= pRegGetValueA(hkey_main
, NULL
, "TP1_EXP_SZ", RRF_RT_REG_SZ
, &type
, buf
, &size
);
692 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
693 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
694 ok((size
== strlen(expanded
)+1) || (size
== strlen(sTestpath1
)+1),
695 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded
), lstrlenA(sTestpath1
), size
);
696 ok(type
== REG_SZ
, "type=%d\n", type
);
697 ok(!strcmp(expanded
, buf
), "expanded=\"%s\" buf=\"%s\"\n", expanded
, buf
);
699 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
700 buf
[0] = 0; type
= 0xdeadbeef; size
= sizeof(buf
);
701 ret
= pRegGetValueA(hkey_main
, NULL
, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ
|RRF_NOEXPAND
, &type
, buf
, &size
);
702 ok(ret
== ERROR_SUCCESS
, "ret=%d\n", ret
);
703 ok(size
== strlen(sTestpath1
)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1
), size
);
704 ok(type
== REG_EXPAND_SZ
, "type=%d\n", type
);
705 ok(!strcmp(sTestpath1
, buf
), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1
, buf
);
707 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
708 ret
= pRegGetValueA(hkey_main
, NULL
, "TP1_EXP_SZ", RRF_RT_REG_SZ
|RRF_NOEXPAND
, NULL
, NULL
, NULL
);
709 ok(ret
== ERROR_UNSUPPORTED_TYPE
, "ret=%d\n", ret
);
711 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
712 ret
= pRegGetValueA(hkey_main
, NULL
, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ
, NULL
, NULL
, NULL
);
713 ok(ret
== ERROR_INVALID_PARAMETER
, "ret=%d\n", ret
);
716 static void test_reg_open_key(void)
719 HKEY hkResult
= NULL
;
720 HKEY hkPreserve
= NULL
;
722 /* successful open */
723 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Test", &hkResult
);
724 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
725 ok(hkResult
!= NULL
, "expected hkResult != NULL\n");
726 hkPreserve
= hkResult
;
728 /* these tests fail on Win9x, but we want to be compatible with NT, so
729 * run them if we can */
730 if (!(GetVersion() & 0x80000000))
732 /* open same key twice */
733 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Test", &hkResult
);
734 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
735 ok(hkResult
!= hkPreserve
, "epxected hkResult != hkPreserve\n");
736 ok(hkResult
!= NULL
, "hkResult != NULL\n");
737 RegCloseKey(hkResult
);
739 /* open nonexistent key
740 * check that hkResult is set to NULL
742 hkResult
= hkPreserve
;
743 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Nonexistent", &hkResult
);
744 ok(ret
== ERROR_FILE_NOT_FOUND
, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret
);
745 ok(hkResult
== NULL
, "expected hkResult == NULL\n");
747 /* open the same nonexistent key again to make sure the key wasn't created */
748 hkResult
= hkPreserve
;
749 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Nonexistent", &hkResult
);
750 ok(ret
== ERROR_FILE_NOT_FOUND
, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret
);
751 ok(hkResult
== NULL
, "expected hkResult == NULL\n");
753 /* send in NULL lpSubKey
754 * check that hkResult receives the value of hKey
756 hkResult
= hkPreserve
;
757 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, NULL
, &hkResult
);
758 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
759 ok(hkResult
== HKEY_CURRENT_USER
, "expected hkResult == HKEY_CURRENT_USER\n");
761 /* send empty-string in lpSubKey */
762 hkResult
= hkPreserve
;
763 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, "", &hkResult
);
764 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
765 ok(hkResult
== HKEY_CURRENT_USER
, "expected hkResult == HKEY_CURRENT_USER\n");
767 /* send in NULL lpSubKey and NULL hKey
768 * hkResult is set to NULL
770 hkResult
= hkPreserve
;
771 ret
= RegOpenKeyA(NULL
, NULL
, &hkResult
);
772 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
773 ok(hkResult
== NULL
, "expected hkResult == NULL\n");
776 /* only send NULL hKey
777 * the value of hkResult remains unchanged
779 hkResult
= hkPreserve
;
780 ret
= RegOpenKeyA(NULL
, "Software\\Wine\\Test", &hkResult
);
781 ok(ret
== ERROR_INVALID_HANDLE
|| ret
== ERROR_BADKEY
, /* Windows 95 returns BADKEY */
782 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret
);
783 ok(hkResult
== hkPreserve
, "expected hkResult == hkPreserve\n");
784 RegCloseKey(hkResult
);
786 /* send in NULL hkResult */
787 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Test", NULL
);
788 ok(ret
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", ret
);
790 /* beginning backslash character */
791 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, "\\Software\\Wine\\Test", &hkResult
);
792 ok(ret
== ERROR_BAD_PATHNAME
|| /* NT/2k/XP */
793 ret
== ERROR_FILE_NOT_FOUND
/* Win9x,ME */
794 , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret
);
797 static void test_reg_create_key(void)
801 ret
= RegCreateKeyExA(hkey_main
, "Subkey1", 0, NULL
, 0, KEY_NOTIFY
, NULL
, &hkey1
, NULL
);
802 ok(!ret
, "RegCreateKeyExA failed with error %d\n", ret
);
803 /* should succeed: all versions of Windows ignore the access rights
804 * to the parent handle */
805 ret
= RegCreateKeyExA(hkey1
, "Subkey2", 0, NULL
, 0, KEY_SET_VALUE
, NULL
, &hkey2
, NULL
);
806 ok(!ret
, "RegCreateKeyExA failed with error %d\n", ret
);
809 RegDeleteKey(hkey2
, "");
810 RegDeleteKey(hkey1
, "");
812 /* beginning backslash character */
813 ret
= RegCreateKeyExA(hkey_main
, "\\Subkey3", 0, NULL
, 0, KEY_NOTIFY
, NULL
, &hkey1
, NULL
);
814 if (!(GetVersion() & 0x80000000))
815 ok(ret
== ERROR_BAD_PATHNAME
, "expected ERROR_BAD_PATHNAME, got %d\n", ret
);
817 ok(!ret
, "RegCreateKeyExA failed with error %d\n", ret
);
818 RegDeleteKey(hkey1
, NULL
);
822 static void test_reg_close_key(void)
827 /* successfully close key
828 * hkHandle remains changed after call to RegCloseKey
830 ret
= RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Test", &hkHandle
);
831 ret
= RegCloseKey(hkHandle
);
832 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
834 /* try to close the key twice */
835 ret
= RegCloseKey(hkHandle
); /* Windows 95 doesn't mind. */
836 ok(ret
== ERROR_INVALID_HANDLE
|| ret
== ERROR_SUCCESS
,
837 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret
);
839 /* try to close a NULL handle */
840 ret
= RegCloseKey(NULL
);
841 ok(ret
== ERROR_INVALID_HANDLE
|| ret
== ERROR_BADKEY
, /* Windows 95 returns BADKEY */
842 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret
);
844 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
845 * win98 doesn't give a new handle when the same key is opened.
846 * Not re-opening will make some next tests fail.
848 if (hkey_main
== hkHandle
)
850 trace("The main handle is most likely closed, so re-opening\n");
851 RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Test", &hkey_main
);
855 static void test_reg_delete_key(void)
859 ret
= RegDeleteKey(hkey_main
, NULL
);
861 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
862 * there are also no subkeys available it will delete the key pointed to by hkey_main.
863 * Not re-creating will make some next tests fail.
865 if (ret
== ERROR_SUCCESS
)
867 trace("We are probably running on NT4 or W2K as the main key is deleted,"
868 " re-creating the main key\n");
872 ok(ret
== ERROR_INVALID_PARAMETER
||
873 ret
== ERROR_ACCESS_DENIED
||
874 ret
== ERROR_BADKEY
, /* Win95 */
878 static void test_reg_save_key(void)
882 ret
= RegSaveKey(hkey_main
, "saved_key", NULL
);
883 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
886 static void test_reg_load_key(void)
891 ret
= RegLoadKey(HKEY_LOCAL_MACHINE
, "Test", "saved_key");
892 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
894 ret
= RegOpenKey(HKEY_LOCAL_MACHINE
, "Test", &hkHandle
);
895 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
897 RegCloseKey(hkHandle
);
900 static void test_reg_unload_key(void)
904 ret
= RegUnLoadKey(HKEY_LOCAL_MACHINE
, "Test");
905 ok(ret
== ERROR_SUCCESS
, "expected ERROR_SUCCESS, got %d\n", ret
);
907 DeleteFile("saved_key");
908 DeleteFile("saved_key.LOG");
911 static BOOL
set_privileges(LPCSTR privilege
, BOOL set
)
917 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &hToken
))
920 if(!LookupPrivilegeValue(NULL
, privilege
, &luid
))
926 tp
.PrivilegeCount
= 1;
927 tp
.Privileges
[0].Luid
= luid
;
930 tp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
932 tp
.Privileges
[0].Attributes
= 0;
934 AdjustTokenPrivileges(hToken
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), NULL
, NULL
);
935 if (GetLastError() != ERROR_SUCCESS
)
945 /* tests that show that RegConnectRegistry and
946 OpenSCManager accept computer names without the
947 \\ prefix (what MSDN says). */
948 static void test_regconnectregistry( void)
950 CHAR compName
[MAX_COMPUTERNAME_LENGTH
+ 1];
951 CHAR netwName
[MAX_COMPUTERNAME_LENGTH
+ 3]; /* 2 chars for double backslash */
952 DWORD len
= sizeof(compName
) ;
958 SetLastError(0xdeadbeef);
959 ret
= GetComputerNameA(compName
, &len
);
960 ok( ret
, "GetComputerName failed err = %d\n", GetLastError());
963 lstrcpyA(netwName
, "\\\\");
964 lstrcpynA(netwName
+2, compName
, MAX_COMPUTERNAME_LENGTH
+ 1);
966 retl
= RegConnectRegistryA( compName
, HKEY_LOCAL_MACHINE
, &hkey
);
967 ok( !retl
|| retl
== ERROR_DLL_INIT_FAILED
, "RegConnectRegistryA failed err = %d\n", retl
);
968 if( !retl
) RegCloseKey( hkey
);
970 retl
= RegConnectRegistryA( netwName
, HKEY_LOCAL_MACHINE
, &hkey
);
971 ok( !retl
|| retl
== ERROR_DLL_INIT_FAILED
, "RegConnectRegistryA failed err = %d\n", retl
);
972 if( !retl
) RegCloseKey( hkey
);
974 SetLastError(0xdeadbeef);
975 schnd
= OpenSCManagerA( compName
, NULL
, GENERIC_READ
);
976 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
978 skip("OpenSCManagerA is not implemented\n");
982 ok( schnd
!= NULL
, "OpenSCManagerA failed err = %d\n", GetLastError());
983 CloseServiceHandle( schnd
);
985 SetLastError(0xdeadbeef);
986 schnd
= OpenSCManagerA( netwName
, NULL
, GENERIC_READ
);
987 ok( schnd
!= NULL
, "OpenSCManagerA failed err = %d\n", GetLastError());
988 CloseServiceHandle( schnd
);
992 static void test_reg_query_value(void)
999 static const WCHAR expected
[] = {'d','a','t','a',0};
1001 ret
= RegCreateKeyA(hkey_main
, "subkey", &subkey
);
1002 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1004 ret
= RegSetValueA(subkey
, NULL
, REG_SZ
, "data", 4);
1005 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1007 /* try an invalid hkey */
1008 SetLastError(0xdeadbeef);
1010 ret
= RegQueryValueA((HKEY
)0xcafebabe, "subkey", val
, &size
);
1011 ok(ret
== ERROR_INVALID_HANDLE
|| ret
== ERROR_BADKEY
, /* Windows 98 returns BADKEY */
1012 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret
);
1013 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1015 /* try a NULL hkey */
1016 SetLastError(0xdeadbeef);
1018 ret
= RegQueryValueA(NULL
, "subkey", val
, &size
);
1019 ok(ret
== ERROR_INVALID_HANDLE
|| ret
== ERROR_BADKEY
, /* Windows 98 returns BADKEY */
1020 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret
);
1021 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1023 /* try a NULL value */
1025 ret
= RegQueryValueA(hkey_main
, "subkey", NULL
, &size
);
1026 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1027 ok(size
== 5, "Expected 5, got %d\n", size
);
1029 /* try a NULL size */
1030 SetLastError(0xdeadbeef);
1032 ret
= RegQueryValueA(hkey_main
, "subkey", val
, NULL
);
1033 ok(ret
== ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret
);
1034 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1035 ok(lstrlenA(val
) == 0, "Expected val to be untouched, got %s\n", val
);
1037 /* try a NULL value and size */
1038 ret
= RegQueryValueA(hkey_main
, "subkey", NULL
, NULL
);
1039 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1041 /* try a size too small */
1042 SetLastError(0xdeadbeef);
1045 ret
= RegQueryValueA(hkey_main
, "subkey", val
, &size
);
1046 ok(ret
== ERROR_MORE_DATA
, "Expected ERROR_MORE_DATA, got %d\n", ret
);
1047 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1048 ok(lstrlenA(val
) == 0, "Expected val to be untouched, got %s\n", val
);
1049 ok(size
== 5, "Expected 5, got %d\n", size
);
1051 /* successfully read the value using 'subkey' */
1053 ret
= RegQueryValueA(hkey_main
, "subkey", val
, &size
);
1054 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1055 ok(!lstrcmpA(val
, "data"), "Expected 'data', got '%s'\n", val
);
1056 ok(size
== 5, "Expected 5, got %d\n", size
);
1058 /* successfully read the value using the subkey key */
1060 ret
= RegQueryValueA(subkey
, NULL
, val
, &size
);
1061 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1062 ok(!lstrcmpA(val
, "data"), "Expected 'data', got '%s'\n", val
);
1063 ok(size
== 5, "Expected 5, got %d\n", size
);
1065 /* unicode - try size too small */
1066 SetLastError(0xdeadbeef);
1069 ret
= RegQueryValueW(subkey
, NULL
, valW
, &size
);
1070 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1072 skip("RegQueryValueW is not implemented\n");
1075 ok(ret
== ERROR_MORE_DATA
, "Expected ERROR_MORE_DATA, got %d\n", ret
);
1076 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1077 ok(lstrlenW(valW
) == 0, "Expected valW to be untouched\n");
1078 ok(size
== sizeof(expected
), "Got wrong size: %d\n", size
);
1080 /* unicode - try size in WCHARS */
1081 SetLastError(0xdeadbeef);
1082 size
= sizeof(valW
) / sizeof(WCHAR
);
1083 ret
= RegQueryValueW(subkey
, NULL
, valW
, &size
);
1084 ok(ret
== ERROR_MORE_DATA
, "Expected ERROR_MORE_DATA, got %d\n", ret
);
1085 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1086 ok(lstrlenW(valW
) == 0, "Expected valW to be untouched\n");
1087 ok(size
== sizeof(expected
), "Got wrong size: %d\n", size
);
1089 /* unicode - successfully read the value */
1090 size
= sizeof(valW
);
1091 ret
= RegQueryValueW(subkey
, NULL
, valW
, &size
);
1092 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1093 ok(!lstrcmpW(valW
, expected
), "Got wrong value\n");
1094 ok(size
== sizeof(expected
), "Got wrong size: %d\n", size
);
1096 /* unicode - set the value without a NULL terminator */
1097 ret
= RegSetValueW(subkey
, NULL
, REG_SZ
, expected
, sizeof(expected
)-sizeof(WCHAR
));
1098 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1100 /* unicode - read the unterminated value, value is terminated for us */
1101 memset(valW
, 'a', sizeof(valW
));
1102 size
= sizeof(valW
);
1103 ret
= RegQueryValueW(subkey
, NULL
, valW
, &size
);
1104 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1105 ok(!lstrcmpW(valW
, expected
), "Got wrong value\n");
1106 ok(size
== sizeof(expected
), "Got wrong size: %d\n", size
);
1109 RegDeleteKeyA(subkey
, "");
1110 RegCloseKey(subkey
);
1113 static void test_reg_delete_tree(void)
1115 CHAR buffer
[MAX_PATH
];
1116 HKEY subkey
, subkey2
;
1119 if(!pRegDeleteTreeA
) {
1120 skip("Skipping RegDeleteTreeA tests, function not present\n");
1124 ret
= RegCreateKeyA(hkey_main
, "subkey", &subkey
);
1125 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1126 ret
= RegCreateKeyA(subkey
, "subkey2", &subkey2
);
1127 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1128 ret
= RegSetValueA(subkey
, NULL
, REG_SZ
, "data", 4);
1129 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1130 ret
= RegSetValueA(subkey2
, NULL
, REG_SZ
, "data2", 5);
1131 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1132 ret
= RegCloseKey(subkey2
);
1133 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1135 ret
= pRegDeleteTreeA(subkey
, "subkey2");
1136 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1137 ok(RegOpenKeyA(subkey
, "subkey2", &subkey2
),
1138 "subkey2 was not deleted\n");
1140 ok(!RegQueryValueA(subkey
, NULL
, buffer
, &size
),
1141 "Default value of subkey not longer present\n");
1143 ret
= RegCreateKeyA(subkey
, "subkey2", &subkey2
);
1144 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1145 ret
= RegCloseKey(subkey2
);
1146 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1147 ret
= pRegDeleteTreeA(hkey_main
, "subkey\\subkey2");
1148 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1149 ok(RegOpenKeyA(subkey
, "subkey2", &subkey2
),
1150 "subkey2 was not deleted\n");
1151 ok(!RegQueryValueA(subkey
, NULL
, buffer
, &size
),
1152 "Default value of subkey not longer present\n");
1154 ret
= RegCreateKeyA(subkey
, "subkey2", &subkey2
);
1155 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1156 ret
= RegCloseKey(subkey2
);
1157 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1158 ret
= RegCreateKeyA(subkey
, "subkey3", &subkey2
);
1159 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1160 ret
= RegCloseKey(subkey2
);
1161 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1162 ret
= RegSetValueA(subkey
, "value", REG_SZ
, "data2", 5);
1163 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1164 ret
= pRegDeleteTreeA(subkey
, NULL
);
1165 ok(ret
== ERROR_SUCCESS
, "Expected ERROR_SUCCESS, got %d\n", ret
);
1166 ok(!RegOpenKeyA(hkey_main
, "subkey", &subkey
),
1167 "subkey was deleted\n");
1168 ok(RegOpenKeyA(subkey
, "subkey2", &subkey2
),
1169 "subkey2 was not deleted\n");
1170 ok(RegOpenKeyA(subkey
, "subkey3", &subkey2
),
1171 "subkey3 was not deleted\n");
1173 ret
= RegQueryValueA(subkey
, NULL
, buffer
, &size
);
1174 ok(ret
== ERROR_SUCCESS
,
1175 "Default value of subkey is not present\n");
1176 ok(!lstrlenA(buffer
),
1177 "Expected length 0 got length %u(%s)\n", lstrlenA(buffer
), buffer
);
1179 ok(RegQueryValueA(subkey
, "value", buffer
, &size
),
1180 "Value is still present\n");
1182 ret
= pRegDeleteTreeA(hkey_main
, "not-here");
1183 ok(ret
== ERROR_FILE_NOT_FOUND
,
1184 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret
);
1187 START_TEST(registry
)
1189 /* Load pointers for functions that are not available in all Windows versions */
1194 create_test_entries();
1196 test_query_value_ex();
1198 test_reg_open_key();
1199 test_reg_create_key();
1200 test_reg_close_key();
1201 test_reg_delete_key();
1202 test_reg_query_value();
1204 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1205 if (set_privileges(SE_BACKUP_NAME
, TRUE
) &&
1206 set_privileges(SE_RESTORE_NAME
, TRUE
))
1208 test_reg_save_key();
1209 test_reg_load_key();
1210 test_reg_unload_key();
1212 set_privileges(SE_BACKUP_NAME
, FALSE
);
1213 set_privileges(SE_RESTORE_NAME
, FALSE
);
1216 test_reg_delete_tree();
1219 delete_key( hkey_main
);
1221 test_regconnectregistry();