advapi32: Add support for the KEY_WOW64_32KEY flag in RegCreateKey on 64-bit.
[wine/multimedia.git] / dlls / advapi32 / tests / registry.c
blobea036e8bd1698b197f4abf1358035dd2f0156b0f
1 /*
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
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "wine/test.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winternl.h"
28 #include "winreg.h"
29 #include "winsvc.h"
30 #include "winerror.h"
32 static HKEY hkey_main;
33 static DWORD GLE;
35 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
36 static const char * sTestpath2 = "%FOO%\\subdir1";
38 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
39 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
40 static DWORD (WINAPI *pRegDeleteKeyExA)(HKEY,LPCSTR,REGSAM,DWORD);
41 static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
42 static NTSTATUS (WINAPI * pNtDeleteKey)(HANDLE);
43 static NTSTATUS (WINAPI * pRtlFormatCurrentUserKeyPath)(UNICODE_STRING*);
44 static NTSTATUS (WINAPI * pRtlFreeUnicodeString)(PUNICODE_STRING);
47 /* Debugging functions from wine/libs/wine/debug.c */
49 /* allocate some tmp string space */
50 /* FIXME: this is not 100% thread-safe */
51 static char *get_temp_buffer( int size )
53 static char *list[32];
54 static UINT pos;
55 char *ret;
56 UINT idx;
58 idx = ++pos % (sizeof(list)/sizeof(list[0]));
59 if (list[idx])
60 ret = HeapReAlloc( GetProcessHeap(), 0, list[idx], size );
61 else
62 ret = HeapAlloc( GetProcessHeap(), 0, size );
63 if (ret) list[idx] = ret;
64 return ret;
67 static const char *wine_debugstr_an( const char *str, int n )
69 static const char hex[16] = "0123456789abcdef";
70 char *dst, *res;
71 size_t size;
73 if (!((ULONG_PTR)str >> 16))
75 if (!str) return "(null)";
76 res = get_temp_buffer( 6 );
77 sprintf( res, "#%04x", LOWORD(str) );
78 return res;
80 if (n == -1) n = strlen(str);
81 if (n < 0) n = 0;
82 size = 10 + min( 300, n * 4 );
83 dst = res = get_temp_buffer( size );
84 *dst++ = '"';
85 while (n-- > 0 && dst <= res + size - 9)
87 unsigned char c = *str++;
88 switch (c)
90 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
91 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
92 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
93 case '"': *dst++ = '\\'; *dst++ = '"'; break;
94 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
95 default:
96 if (c >= ' ' && c <= 126)
97 *dst++ = c;
98 else
100 *dst++ = '\\';
101 *dst++ = 'x';
102 *dst++ = hex[(c >> 4) & 0x0f];
103 *dst++ = hex[c & 0x0f];
107 *dst++ = '"';
108 if (n > 0)
110 *dst++ = '.';
111 *dst++ = '.';
112 *dst++ = '.';
114 *dst++ = 0;
115 return res;
118 #define ADVAPI32_GET_PROC(func) \
119 p ## func = (void*)GetProcAddress(hadvapi32, #func)
121 static void InitFunctionPtrs(void)
123 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
124 HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
125 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
127 /* This function was introduced with Windows 2003 SP1 */
128 ADVAPI32_GET_PROC(RegGetValueA);
129 ADVAPI32_GET_PROC(RegDeleteTreeA);
130 ADVAPI32_GET_PROC(RegDeleteKeyExA);
132 pIsWow64Process = (void *)GetProcAddress( hkernel32, "IsWow64Process" );
133 pRtlFormatCurrentUserKeyPath = (void *)GetProcAddress( hntdll, "RtlFormatCurrentUserKeyPath" );
134 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
135 pNtDeleteKey = (void *)GetProcAddress( hntdll, "NtDeleteKey" );
138 /* delete key and all its subkeys */
139 static DWORD delete_key( HKEY hkey )
141 char name[MAX_PATH];
142 DWORD ret;
144 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
146 HKEY tmp;
147 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
149 ret = delete_key( tmp );
150 RegCloseKey( tmp );
152 if (ret) break;
154 if (ret != ERROR_NO_MORE_ITEMS) return ret;
155 RegDeleteKeyA( hkey, "" );
156 return 0;
159 static void setup_main_key(void)
161 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
163 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
166 #define lok ok_(__FILE__, line)
167 #define test_hkey_main_Value_A(name, string, full_byte_len) _test_hkey_main_Value_A(__LINE__, name, string, full_byte_len)
168 static void _test_hkey_main_Value_A(int line, LPCSTR name, LPCSTR string,
169 DWORD full_byte_len)
171 DWORD ret, type, cbData;
172 DWORD str_byte_len;
173 BYTE* value;
175 type=0xdeadbeef;
176 cbData=0xdeadbeef;
177 /* When successful RegQueryValueExA() leaves GLE as is,
178 * so we must reset it to detect unimplemented functions.
180 SetLastError(0xdeadbeef);
181 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
182 GLE = GetLastError();
183 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/1 failed: %d, GLE=%d\n", ret, GLE);
184 /* It is wrong for the Ansi version to not be implemented */
185 ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
186 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
188 str_byte_len = (string ? lstrlenA(string) : 0) + 1;
189 lok(type == REG_SZ, "RegQueryValueExA/1 returned type %d\n", type);
190 lok(cbData == full_byte_len || cbData == str_byte_len /* Win9x */,
191 "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
193 value = HeapAlloc(GetProcessHeap(), 0, cbData+1);
194 memset(value, 0xbd, cbData+1);
195 type=0xdeadbeef;
196 ret = RegQueryValueExA(hkey_main, name, NULL, &type, value, &cbData);
197 GLE = GetLastError();
198 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/2 failed: %d, GLE=%d\n", ret, GLE);
199 if (!string)
201 /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
202 lok(*value == 0xbd || (cbData == 1 && *value == '\0') /* Win9x */,
203 "RegQueryValueExA overflowed: cbData=%u *value=%02x\n", cbData, *value);
205 else
207 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExA/2 failed: %s/%d != %s/%d\n",
208 wine_debugstr_an((char*)value, cbData), cbData,
209 wine_debugstr_an(string, full_byte_len), full_byte_len);
210 lok(*(value+cbData) == 0xbd, "RegQueryValueExA/2 overflowed at offset %u: %02x != bd\n", cbData, *(value+cbData));
212 HeapFree(GetProcessHeap(), 0, value);
215 #define test_hkey_main_Value_W(name, string, full_byte_len) _test_hkey_main_Value_W(__LINE__, name, string, full_byte_len)
216 static void _test_hkey_main_Value_W(int line, LPCWSTR name, LPCWSTR string,
217 DWORD full_byte_len)
219 DWORD ret, type, cbData;
220 BYTE* value;
222 type=0xdeadbeef;
223 cbData=0xdeadbeef;
224 /* When successful RegQueryValueExW() leaves GLE as is,
225 * so we must reset it to detect unimplemented functions.
227 SetLastError(0xdeadbeef);
228 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
229 GLE = GetLastError();
230 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/1 failed: %d, GLE=%d\n", ret, GLE);
231 if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
233 win_skip("RegQueryValueExW() is not implemented\n");
234 return;
237 lok(type == REG_SZ, "RegQueryValueExW/1 returned type %d\n", type);
238 lok(cbData == full_byte_len,
239 "cbData=%d instead of %d\n", cbData, full_byte_len);
241 /* Give enough space to overflow by one WCHAR */
242 value = HeapAlloc(GetProcessHeap(), 0, cbData+2);
243 memset(value, 0xbd, cbData+2);
244 type=0xdeadbeef;
245 ret = RegQueryValueExW(hkey_main, name, NULL, &type, value, &cbData);
246 GLE = GetLastError();
247 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/2 failed: %d, GLE=%d\n", ret, GLE);
248 if (string)
250 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
251 wine_dbgstr_wn((WCHAR*)value, cbData / sizeof(WCHAR)), cbData,
252 wine_dbgstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
254 /* This implies that when cbData == 0, RegQueryValueExW() should not modify the buffer */
255 lok(*(value+cbData) == 0xbd, "RegQueryValueExW/2 overflowed at %u: %02x != bd\n", cbData, *(value+cbData));
256 lok(*(value+cbData+1) == 0xbd, "RegQueryValueExW/2 overflowed at %u+1: %02x != bd\n", cbData, *(value+cbData+1));
257 HeapFree(GetProcessHeap(), 0, value);
260 static void test_set_value(void)
262 DWORD ret;
264 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
265 static const WCHAR name2W[] = {'S','o','m','e','I','n','t','r','a','Z','e','r','o','e','d','S','t','r','i','n','g', 0};
266 static const WCHAR emptyW[] = {0};
267 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
268 static const WCHAR string2W[] = {'T','h','i','s', 0 ,'B','r','e','a','k','s', 0 , 0 ,'A', 0 , 0 , 0 , 'L','o','t', 0 , 0 , 0 , 0, 0};
269 static const WCHAR substring2W[] = {'T','h','i','s',0};
271 static const char name1A[] = "CleanSingleString";
272 static const char name2A[] = "SomeIntraZeroedString";
273 static const char emptyA[] = "";
274 static const char string1A[] = "ThisNeverBreaks";
275 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
276 static const char substring2A[] = "This";
278 if (0)
280 /* Crashes on NT4, Windows 2000 and XP SP1 */
281 ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
282 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
285 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
286 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
287 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
288 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
290 /* RegSetValueA ignores the size passed in */
291 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
292 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
293 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
294 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
296 /* stops at first null */
297 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
298 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
299 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
300 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
302 /* only REG_SZ is supported on NT*/
303 ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
304 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
305 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
306 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
308 ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
309 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
310 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
311 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
313 ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
314 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
315 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
316 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
318 /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
319 * Surprisingly enough we're supposed to get zero bytes out of it.
321 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
322 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
323 test_hkey_main_Value_A(name1A, NULL, 0);
324 test_hkey_main_Value_W(name1W, NULL, 0);
326 /* test RegSetValueExA with an empty string */
327 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
328 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
329 test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
330 test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
332 /* test RegSetValueExA with off-by-one size */
333 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
334 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
335 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
336 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
338 /* test RegSetValueExA with normal string */
339 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
340 ok(ret == ERROR_SUCCESS, "RegSetValueExA 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 RegSetValueExA with intrazeroed string */
345 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
346 ok(ret == ERROR_SUCCESS, "RegSetValueExA 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));
350 /* 9x doesn't support W-calls, so don't test them then */
351 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
353 if (0)
355 /* Crashes on NT4, Windows 2000 and XP SP1 */
356 ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
357 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
360 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
361 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
362 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
363 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
365 /* RegSetValueA ignores the size passed in */
366 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
367 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
368 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
369 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
371 /* stops at first null */
372 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
373 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
374 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
375 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
377 /* only REG_SZ is supported */
378 ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
379 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
380 ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
381 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
382 ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
383 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
385 /* test RegSetValueExW with off-by-one size */
386 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
387 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
388 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
389 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
391 /* test RegSetValueExW with normal string */
392 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
393 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
394 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
395 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
397 /* test RegSetValueExW with intrazeroed string */
398 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
399 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
400 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
401 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
404 static void create_test_entries(void)
406 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
408 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
409 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
411 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
412 "RegSetValueExA failed\n");
413 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
414 "RegSetValueExA failed\n");
415 ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
416 "RegSetValueExA failed\n");
417 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
418 "RegSetValueExA failed\n");
419 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
420 "RegSetValueExA failed\n");
421 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
422 "RegSetValueExA failed\n");
423 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
424 "RegSetValueExA failed\n");
427 static void test_enum_value(void)
429 DWORD res;
430 HKEY test_key;
431 char value[20], data[20];
432 WCHAR valueW[20], dataW[20];
433 DWORD val_count, data_count, type;
434 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
435 static const WCHAR testW[] = {'T','e','s','t',0};
436 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
438 /* create the working key for new 'Test' value */
439 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
440 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
442 /* check NULL data with zero length */
443 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
444 if (GetVersion() & 0x80000000)
445 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
446 else
447 ok( !res, "RegSetValueExA returned %d\n", res );
448 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
449 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
450 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
451 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
453 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
454 ok( res == 0, "RegSetValueExA failed error %d\n", res );
456 /* overflow both name and data */
457 val_count = 2;
458 data_count = 2;
459 type = 1234;
460 strcpy( value, "xxxxxxxxxx" );
461 strcpy( data, "xxxxxxxxxx" );
462 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
463 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
464 ok( val_count == 2, "val_count set to %d\n", val_count );
465 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
466 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
467 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
468 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
470 /* overflow name */
471 val_count = 3;
472 data_count = 20;
473 type = 1234;
474 strcpy( value, "xxxxxxxxxx" );
475 strcpy( data, "xxxxxxxxxx" );
476 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
477 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
478 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
479 ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
480 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
481 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
482 /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
483 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
484 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
485 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
486 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
488 /* overflow empty name */
489 val_count = 0;
490 data_count = 20;
491 type = 1234;
492 strcpy( value, "xxxxxxxxxx" );
493 strcpy( data, "xxxxxxxxxx" );
494 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
495 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
496 ok( val_count == 0, "val_count set to %d\n", val_count );
497 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
498 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
499 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
500 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
501 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
502 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
504 /* overflow data */
505 val_count = 20;
506 data_count = 2;
507 type = 1234;
508 strcpy( value, "xxxxxxxxxx" );
509 strcpy( data, "xxxxxxxxxx" );
510 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
511 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
512 ok( val_count == 20, "val_count set to %d\n", val_count );
513 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
514 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
515 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
516 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
518 /* no overflow */
519 val_count = 20;
520 data_count = 20;
521 type = 1234;
522 strcpy( value, "xxxxxxxxxx" );
523 strcpy( data, "xxxxxxxxxx" );
524 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
525 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
526 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
527 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
528 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
529 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
530 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
532 /* Unicode tests */
534 SetLastError(0xdeadbeef);
535 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
536 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
538 win_skip("RegSetValueExW is not implemented\n");
539 goto cleanup;
541 ok( res == 0, "RegSetValueExW failed error %d\n", res );
543 /* overflow both name and data */
544 val_count = 2;
545 data_count = 2;
546 type = 1234;
547 memcpy( valueW, xxxW, sizeof(xxxW) );
548 memcpy( dataW, xxxW, sizeof(xxxW) );
549 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
550 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
551 ok( val_count == 2, "val_count set to %d\n", val_count );
552 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
553 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
554 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
555 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
557 /* overflow name */
558 val_count = 3;
559 data_count = 20;
560 type = 1234;
561 memcpy( valueW, xxxW, sizeof(xxxW) );
562 memcpy( dataW, xxxW, sizeof(xxxW) );
563 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
564 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
565 ok( val_count == 3, "val_count set to %d\n", val_count );
566 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
567 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
568 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
569 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
571 /* overflow data */
572 val_count = 20;
573 data_count = 2;
574 type = 1234;
575 memcpy( valueW, xxxW, sizeof(xxxW) );
576 memcpy( dataW, xxxW, sizeof(xxxW) );
577 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
578 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
579 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
580 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
581 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
582 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
583 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
585 /* no overflow */
586 val_count = 20;
587 data_count = 20;
588 type = 1234;
589 memcpy( valueW, xxxW, sizeof(xxxW) );
590 memcpy( dataW, xxxW, sizeof(xxxW) );
591 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
592 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
593 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
594 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
595 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
596 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
597 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
599 cleanup:
600 RegDeleteKeyA(test_key, "");
601 RegCloseKey(test_key);
604 static void test_query_value_ex(void)
606 DWORD ret;
607 DWORD size;
608 DWORD type;
609 BYTE buffer[10];
611 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
612 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
613 ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
614 ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
616 type = 0xdeadbeef;
617 size = 0xdeadbeef;
618 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
619 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
620 /* the type parameter is cleared on Win9x, but is set to a random value on
621 * NT, so don't do that test there. The size parameter is left untouched on Win9x
622 * but cleared on NT+, this can be tested on all platforms.
624 if (GetVersion() & 0x80000000)
626 ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
627 ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
629 else
631 trace("test_query_value_ex: type set to: 0x%08x\n", type);
632 ok(size == 0, "size should have been set to 0 instead of %d\n", size);
635 size = sizeof(buffer);
636 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
637 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
638 ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
640 size = 4;
641 ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
642 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
645 static void test_get_value(void)
647 DWORD ret;
648 DWORD size;
649 DWORD type;
650 DWORD dw, qw[2];
651 CHAR buf[80];
652 CHAR expanded[] = "bar\\subdir1";
653 CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
655 if(!pRegGetValueA)
657 win_skip("RegGetValue not available on this platform\n");
658 return;
661 /* Invalid parameter */
662 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
663 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
665 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
666 size = type = dw = 0xdeadbeef;
667 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
668 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
669 ok(size == 4, "size=%d\n", size);
670 ok(type == REG_DWORD, "type=%d\n", type);
671 ok(dw == 0x12345678, "dw=%d\n", dw);
673 /* Query by subkey-name */
674 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
675 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
677 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
678 size = type = dw = 0xdeadbeef;
679 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
680 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
681 /* Although the function failed all values are retrieved */
682 ok(size == 4, "size=%d\n", size);
683 ok(type == REG_DWORD, "type=%d\n", type);
684 ok(dw == 0x12345678, "dw=%d\n", dw);
686 /* Test RRF_ZEROONFAILURE */
687 type = dw = 0xdeadbeef; size = 4;
688 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
689 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
690 /* Again all values are retrieved ... */
691 ok(size == 4, "size=%d\n", size);
692 ok(type == REG_DWORD, "type=%d\n", type);
693 /* ... except the buffer, which is zeroed out */
694 ok(dw == 0, "dw=%d\n", dw);
696 /* Test RRF_ZEROONFAILURE with a NULL buffer... */
697 type = size = 0xbadbeef;
698 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
699 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
700 ok(size == 4, "size=%d\n", size);
701 ok(type == REG_DWORD, "type=%d\n", type);
703 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
704 size = type = dw = 0xdeadbeef;
705 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
706 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
707 ok(size == 4, "size=%d\n", size);
708 ok(type == REG_DWORD, "type=%d\n", type);
709 ok(dw == 0x12345678, "dw=%d\n", dw);
711 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
712 size = type = dw = 0xdeadbeef;
713 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
714 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
715 ok(size == 4, "size=%d\n", size);
716 ok(type == REG_BINARY, "type=%d\n", type);
717 ok(dw == 0x12345678, "dw=%d\n", dw);
719 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
720 qw[0] = qw[1] = size = type = 0xdeadbeef;
721 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
722 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
723 ok(size == 8, "size=%d\n", size);
724 ok(type == REG_BINARY, "type=%d\n", type);
725 ok(qw[0] == 0x12345678 &&
726 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
728 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
729 type = dw = 0xdeadbeef; size = 4;
730 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
731 ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
732 ok(dw == 0xdeadbeef, "dw=%d\n", dw);
733 ok(size == 8, "size=%d\n", size);
735 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
736 qw[0] = qw[1] = size = type = 0xdeadbeef;
737 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
738 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
739 ok(size == 8, "size=%d\n", size);
740 ok(type == REG_BINARY, "type=%d\n", type);
741 ok(qw[0] == 0x12345678 &&
742 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
744 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
745 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
746 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
747 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
748 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
749 ok(type == REG_SZ, "type=%d\n", type);
750 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
752 /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
753 type = 0xdeadbeef; size = 0;
754 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
755 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
756 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
757 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
758 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
759 ok(type == REG_SZ, "type=%d\n", type);
761 /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
762 strcpy(buf, sTestpath1);
763 type = 0xdeadbeef;
764 size = sizeof(buf);
765 ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
766 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
767 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
768 ok(size == 0 ||
769 size == 1, /* win2k3 */
770 "size=%d\n", size);
771 ok(type == REG_SZ, "type=%d\n", type);
772 ok(!strcmp(sTestpath1, buf) ||
773 !strcmp(buf, ""),
774 "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
776 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
777 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
778 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
779 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
780 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
781 ok(type == REG_SZ, "type=%d\n", type);
782 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
784 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
785 size = 0;
786 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
787 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
788 ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
789 (size == strlen(expanded2)+2) || /* win2k3 SP2 */
790 (size == strlen(sTestpath2)+1),
791 "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
793 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
794 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
795 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
796 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
797 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
798 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
799 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
800 ok(type == REG_SZ, "type=%d\n", type);
801 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
803 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
804 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
805 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
806 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
807 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
808 ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
809 "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
810 ok(type == REG_SZ, "type=%d\n", type);
811 ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
813 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
814 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
815 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
816 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
817 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
818 ok(type == REG_EXPAND_SZ, "type=%d\n", type);
819 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
821 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
822 size = 0xbadbeef;
823 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
824 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
825 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
826 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
827 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
829 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
830 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
831 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
833 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
834 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
835 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
837 /* Query REG_EXPAND_SZ using RRF_RT_ANY */
838 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
839 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
840 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
841 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
842 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
843 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
844 ok(type == REG_SZ, "type=%d\n", type);
845 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
848 static void test_reg_open_key(void)
850 DWORD ret = 0;
851 HKEY hkResult = NULL;
852 HKEY hkPreserve = NULL;
854 /* successful open */
855 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
856 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
857 ok(hkResult != NULL, "expected hkResult != NULL\n");
858 hkPreserve = hkResult;
860 /* these tests fail on Win9x, but we want to be compatible with NT, so
861 * run them if we can */
862 if (!(GetVersion() & 0x80000000))
864 /* open same key twice */
865 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
866 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
867 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
868 ok(hkResult != NULL, "hkResult != NULL\n");
869 RegCloseKey(hkResult);
871 /* open nonexistent key
872 * check that hkResult is set to NULL
874 hkResult = hkPreserve;
875 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
876 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
877 ok(hkResult == NULL, "expected hkResult == NULL\n");
879 /* open the same nonexistent key again to make sure the key wasn't created */
880 hkResult = hkPreserve;
881 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
882 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
883 ok(hkResult == NULL, "expected hkResult == NULL\n");
885 /* send in NULL lpSubKey
886 * check that hkResult receives the value of hKey
888 hkResult = hkPreserve;
889 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
890 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
891 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
893 /* send empty-string in lpSubKey */
894 hkResult = hkPreserve;
895 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
896 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
897 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
899 /* send in NULL lpSubKey and NULL hKey
900 * hkResult is set to NULL
902 hkResult = hkPreserve;
903 ret = RegOpenKeyA(NULL, NULL, &hkResult);
904 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
905 ok(hkResult == NULL, "expected hkResult == NULL\n");
908 /* only send NULL hKey
909 * the value of hkResult remains unchanged
911 hkResult = hkPreserve;
912 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
913 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
914 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
915 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
916 RegCloseKey(hkResult);
918 /* send in NULL hkResult */
919 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
920 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
922 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, NULL);
923 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
925 ret = RegOpenKeyA(NULL, NULL, NULL);
926 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
928 /* beginning backslash character */
929 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
930 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
931 ret == ERROR_FILE_NOT_FOUND || /* Win9x,ME */
932 broken(ret == ERROR_SUCCESS), /* wow64 */
933 "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
934 if (!ret) RegCloseKey(hkResult);
936 hkResult = NULL;
937 ret = RegOpenKeyExA(HKEY_CLASSES_ROOT, "\\clsid", 0, KEY_QUERY_VALUE, &hkResult);
938 ok(ret == ERROR_SUCCESS || /* 2k/XP */
939 ret == ERROR_BAD_PATHNAME || /* NT */
940 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
941 , "expected ERROR_SUCCESS, ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
942 RegCloseKey(hkResult);
944 /* WOW64 flags */
945 hkResult = NULL;
946 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_32KEY, &hkResult);
947 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
948 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
949 RegCloseKey(hkResult);
951 hkResult = NULL;
952 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_64KEY, &hkResult);
953 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
954 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
955 RegCloseKey(hkResult);
958 static void test_reg_create_key(void)
960 LONG ret;
961 HKEY hkey1, hkey2;
962 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
963 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
964 /* should succeed: all versions of Windows ignore the access rights
965 * to the parent handle */
966 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
967 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
969 /* clean up */
970 RegDeleteKey(hkey2, "");
971 RegDeleteKey(hkey1, "");
972 RegCloseKey(hkey2);
973 RegCloseKey(hkey1);
975 /* test creation of volatile keys */
976 ret = RegCreateKeyExA(hkey_main, "Volatile", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey1, NULL);
977 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
978 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
979 ok(ret == ERROR_CHILD_MUST_BE_VOLATILE || broken(!ret), /* win9x */
980 "RegCreateKeyExA failed with error %d\n", ret);
981 if (!ret) RegCloseKey( hkey2 );
982 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
983 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
984 RegCloseKey(hkey2);
985 /* should succeed if the key already exists */
986 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
987 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
989 /* clean up */
990 RegDeleteKey(hkey2, "");
991 RegDeleteKey(hkey1, "");
992 RegCloseKey(hkey2);
993 RegCloseKey(hkey1);
995 /* beginning backslash character */
996 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
997 if (!(GetVersion() & 0x80000000))
998 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
999 else {
1000 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1001 RegDeleteKey(hkey1, NULL);
1002 RegCloseKey(hkey1);
1005 /* WOW64 flags - open an existing key */
1006 hkey1 = NULL;
1007 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &hkey1, NULL);
1008 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1009 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1010 RegCloseKey(hkey1);
1012 hkey1 = NULL;
1013 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_64KEY, NULL, &hkey1, NULL);
1014 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1015 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1016 RegCloseKey(hkey1);
1019 static void test_reg_close_key(void)
1021 DWORD ret = 0;
1022 HKEY hkHandle;
1024 /* successfully close key
1025 * hkHandle remains changed after call to RegCloseKey
1027 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
1028 ret = RegCloseKey(hkHandle);
1029 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1031 /* try to close the key twice */
1032 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
1033 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
1034 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
1036 /* try to close a NULL handle */
1037 ret = RegCloseKey(NULL);
1038 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1039 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1041 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1042 * win98 doesn't give a new handle when the same key is opened.
1043 * Not re-opening will make some next tests fail.
1045 if (hkey_main == hkHandle)
1047 trace("The main handle is most likely closed, so re-opening\n");
1048 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1052 static void test_reg_delete_key(void)
1054 DWORD ret;
1056 ret = RegDeleteKey(hkey_main, NULL);
1058 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1059 * there are also no subkeys available it will delete the key pointed to by hkey_main.
1060 * Not re-creating will make some next tests fail.
1062 if (ret == ERROR_SUCCESS)
1064 trace("We are probably running on NT4 or W2K as the main key is deleted,"
1065 " re-creating the main key\n");
1066 setup_main_key();
1068 else
1069 ok(ret == ERROR_INVALID_PARAMETER ||
1070 ret == ERROR_ACCESS_DENIED ||
1071 ret == ERROR_BADKEY, /* Win95 */
1072 "ret=%d\n", ret);
1075 static void test_reg_save_key(void)
1077 DWORD ret;
1079 ret = RegSaveKey(hkey_main, "saved_key", NULL);
1080 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1083 static void test_reg_load_key(void)
1085 DWORD ret;
1086 HKEY hkHandle;
1088 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1089 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1091 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1092 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1094 RegCloseKey(hkHandle);
1097 static void test_reg_unload_key(void)
1099 DWORD ret;
1101 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
1102 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1104 DeleteFile("saved_key");
1105 DeleteFile("saved_key.LOG");
1108 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1110 TOKEN_PRIVILEGES tp;
1111 HANDLE hToken;
1112 LUID luid;
1114 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1115 return FALSE;
1117 if(!LookupPrivilegeValue(NULL, privilege, &luid))
1119 CloseHandle(hToken);
1120 return FALSE;
1123 tp.PrivilegeCount = 1;
1124 tp.Privileges[0].Luid = luid;
1126 if (set)
1127 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1128 else
1129 tp.Privileges[0].Attributes = 0;
1131 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1132 if (GetLastError() != ERROR_SUCCESS)
1134 CloseHandle(hToken);
1135 return FALSE;
1138 CloseHandle(hToken);
1139 return TRUE;
1142 /* tests that show that RegConnectRegistry and
1143 OpenSCManager accept computer names without the
1144 \\ prefix (what MSDN says). */
1145 static void test_regconnectregistry( void)
1147 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1148 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1149 DWORD len = sizeof(compName) ;
1150 BOOL ret;
1151 LONG retl;
1152 HKEY hkey;
1153 SC_HANDLE schnd;
1155 SetLastError(0xdeadbeef);
1156 ret = GetComputerNameA(compName, &len);
1157 ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1158 if( !ret) return;
1160 lstrcpyA(netwName, "\\\\");
1161 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1163 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1164 ok( !retl ||
1165 retl == ERROR_DLL_INIT_FAILED ||
1166 retl == ERROR_BAD_NETPATH, /* some win2k */
1167 "RegConnectRegistryA failed err = %d\n", retl);
1168 if( !retl) RegCloseKey( hkey);
1170 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1171 ok( !retl ||
1172 retl == ERROR_DLL_INIT_FAILED ||
1173 retl == ERROR_BAD_NETPATH, /* some win2k */
1174 "RegConnectRegistryA failed err = %d\n", retl);
1175 if( !retl) RegCloseKey( hkey);
1177 SetLastError(0xdeadbeef);
1178 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
1179 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1181 win_skip("OpenSCManagerA is not implemented\n");
1182 return;
1185 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1186 CloseServiceHandle( schnd);
1188 SetLastError(0xdeadbeef);
1189 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
1190 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1191 CloseServiceHandle( schnd);
1195 static void test_reg_query_value(void)
1197 HKEY subkey;
1198 CHAR val[MAX_PATH];
1199 WCHAR valW[5];
1200 LONG size, ret;
1202 static const WCHAR expected[] = {'d','a','t','a',0};
1204 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1205 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1207 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1208 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1210 /* try an invalid hkey */
1211 SetLastError(0xdeadbeef);
1212 size = MAX_PATH;
1213 ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1214 ok(ret == ERROR_INVALID_HANDLE ||
1215 ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1216 ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1217 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1218 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1220 /* try a NULL hkey */
1221 SetLastError(0xdeadbeef);
1222 size = MAX_PATH;
1223 ret = RegQueryValueA(NULL, "subkey", val, &size);
1224 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1225 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1226 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1228 /* try a NULL value */
1229 size = MAX_PATH;
1230 ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1231 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1232 ok(size == 5, "Expected 5, got %d\n", size);
1234 /* try a NULL size */
1235 SetLastError(0xdeadbeef);
1236 val[0] = '\0';
1237 ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1238 ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1239 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1240 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1242 /* try a NULL value and size */
1243 ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1244 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1246 /* try a size too small */
1247 SetLastError(0xdeadbeef);
1248 val[0] = '\0';
1249 size = 1;
1250 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1251 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1252 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1253 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1254 ok(size == 5, "Expected 5, got %d\n", size);
1256 /* successfully read the value using 'subkey' */
1257 size = MAX_PATH;
1258 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1259 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1260 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1261 ok(size == 5, "Expected 5, got %d\n", size);
1263 /* successfully read the value using the subkey key */
1264 size = MAX_PATH;
1265 ret = RegQueryValueA(subkey, NULL, val, &size);
1266 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1267 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1268 ok(size == 5, "Expected 5, got %d\n", size);
1270 /* unicode - try size too small */
1271 SetLastError(0xdeadbeef);
1272 valW[0] = '\0';
1273 size = 0;
1274 ret = RegQueryValueW(subkey, NULL, valW, &size);
1275 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1277 win_skip("RegQueryValueW is not implemented\n");
1278 goto cleanup;
1280 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1281 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1282 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1283 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1285 /* unicode - try size in WCHARS */
1286 SetLastError(0xdeadbeef);
1287 size = sizeof(valW) / sizeof(WCHAR);
1288 ret = RegQueryValueW(subkey, NULL, valW, &size);
1289 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1290 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1291 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1292 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1294 /* unicode - successfully read the value */
1295 size = sizeof(valW);
1296 ret = RegQueryValueW(subkey, NULL, valW, &size);
1297 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1298 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1299 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1301 /* unicode - set the value without a NULL terminator */
1302 ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1303 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1305 /* unicode - read the unterminated value, value is terminated for us */
1306 memset(valW, 'a', sizeof(valW));
1307 size = sizeof(valW);
1308 ret = RegQueryValueW(subkey, NULL, valW, &size);
1309 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1310 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1311 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1313 cleanup:
1314 RegDeleteKeyA(subkey, "");
1315 RegCloseKey(subkey);
1318 static void test_string_termination(void)
1320 HKEY subkey;
1321 LSTATUS ret;
1322 static const char string[] = "FullString";
1323 char name[11];
1324 BYTE buffer[11];
1325 DWORD insize, outsize, nsize;
1327 ret = RegCreateKeyA(hkey_main, "string_termination", &subkey);
1328 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1330 /* Off-by-one RegSetValueExA -> adds a trailing '\0'! */
1331 insize=sizeof(string)-1;
1332 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1333 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1334 outsize=insize;
1335 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1336 ok(ret == ERROR_MORE_DATA, "RegQueryValueExA returned: %d\n", ret);
1338 /* Off-by-two RegSetValueExA -> no trailing '\0', except on Win9x */
1339 insize=sizeof(string)-2;
1340 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1341 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1342 outsize=0;
1343 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, NULL, &outsize);
1344 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1345 ok(outsize == insize || broken(outsize == sizeof(string)) /* Win9x */,
1346 "wrong size %u != %u\n", outsize, insize);
1348 if (outsize == insize)
1350 /* RegQueryValueExA may return a string with no trailing '\0' */
1351 outsize=insize;
1352 memset(buffer, 0xbd, sizeof(buffer));
1353 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1354 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1355 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1356 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1357 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1358 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1360 /* RegQueryValueExA adds a trailing '\0' if there is room */
1361 outsize=insize+1;
1362 memset(buffer, 0xbd, sizeof(buffer));
1363 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1364 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1365 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1366 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1367 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1368 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1370 /* RegEnumValueA may return a string with no trailing '\0' */
1371 outsize=insize;
1372 memset(buffer, 0xbd, sizeof(buffer));
1373 nsize=sizeof(name);
1374 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1375 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1376 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1377 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1378 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1379 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1380 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1382 /* RegEnumValueA adds a trailing '\0' if there is room */
1383 outsize=insize+1;
1384 memset(buffer, 0xbd, sizeof(buffer));
1385 nsize=sizeof(name);
1386 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1387 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1388 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1389 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1390 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1391 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1392 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1395 RegDeleteKeyA(subkey, "");
1396 RegCloseKey(subkey);
1399 static void test_reg_delete_tree(void)
1401 CHAR buffer[MAX_PATH];
1402 HKEY subkey, subkey2;
1403 LONG size, ret;
1405 if(!pRegDeleteTreeA) {
1406 win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1407 return;
1410 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1411 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1412 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1413 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1414 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1415 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1416 ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1417 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1418 ret = RegCloseKey(subkey2);
1419 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1421 ret = pRegDeleteTreeA(subkey, "subkey2");
1422 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1423 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1424 "subkey2 was not deleted\n");
1425 size = MAX_PATH;
1426 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1427 "Default value of subkey not longer present\n");
1429 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1430 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1431 ret = RegCloseKey(subkey2);
1432 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1433 ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1434 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1435 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1436 "subkey2 was not deleted\n");
1437 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1438 "Default value of subkey not longer present\n");
1440 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1441 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1442 ret = RegCloseKey(subkey2);
1443 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1444 ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1445 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1446 ret = RegCloseKey(subkey2);
1447 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1448 ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1449 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1450 ret = pRegDeleteTreeA(subkey, NULL);
1451 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1452 ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1453 "subkey was deleted\n");
1454 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1455 "subkey2 was not deleted\n");
1456 ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1457 "subkey3 was not deleted\n");
1458 size = MAX_PATH;
1459 ret = RegQueryValueA(subkey, NULL, buffer, &size);
1460 ok(ret == ERROR_SUCCESS,
1461 "Default value of subkey is not present\n");
1462 ok(!lstrlenA(buffer),
1463 "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1464 size = MAX_PATH;
1465 ok(RegQueryValueA(subkey, "value", buffer, &size),
1466 "Value is still present\n");
1468 ret = pRegDeleteTreeA(hkey_main, "not-here");
1469 ok(ret == ERROR_FILE_NOT_FOUND,
1470 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1473 static void test_rw_order(void)
1475 HKEY hKey;
1476 DWORD dw = 0;
1477 static char keyname[] = "test_rw_order";
1478 char value_buf[2];
1479 DWORD values, value_len, value_name_max_len;
1480 LSTATUS ret;
1482 RegDeleteKeyA(HKEY_CURRENT_USER, keyname);
1483 ret = RegCreateKeyA(HKEY_CURRENT_USER, keyname, &hKey);
1484 if(ret != ERROR_SUCCESS) {
1485 skip("Couldn't create key. Skipping.\n");
1486 return;
1489 ok(!RegSetValueExA(hKey, "A", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1490 "RegSetValueExA for value \"A\" failed\n");
1491 ok(!RegSetValueExA(hKey, "C", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1492 "RegSetValueExA for value \"C\" failed\n");
1493 ok(!RegSetValueExA(hKey, "D", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1494 "RegSetValueExA for value \"D\" failed\n");
1495 ok(!RegSetValueExA(hKey, "B", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1496 "RegSetValueExA for value \"B\" failed\n");
1498 ok(!RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &values,
1499 &value_name_max_len, NULL, NULL, NULL), "RegQueryInfoKeyA failed\n");
1500 ok(values == 4, "Expected 4 values, got %u\n", values);
1502 /* Value enumeration preserves RegSetValueEx call order */
1503 value_len = 2;
1504 ok(!RegEnumValueA(hKey, 0, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1505 ok(strcmp(value_buf, "A") == 0, "Expected name \"A\", got %s\n", value_buf);
1506 value_len = 2;
1507 ok(!RegEnumValueA(hKey, 1, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1508 todo_wine ok(strcmp(value_buf, "C") == 0, "Expected name \"C\", got %s\n", value_buf);
1509 value_len = 2;
1510 ok(!RegEnumValueA(hKey, 2, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1511 todo_wine ok(strcmp(value_buf, "D") == 0, "Expected name \"D\", got %s\n", value_buf);
1512 value_len = 2;
1513 ok(!RegEnumValueA(hKey, 3, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1514 todo_wine ok(strcmp(value_buf, "B") == 0, "Expected name \"B\", got %s\n", value_buf);
1516 ok(!RegDeleteKey(HKEY_CURRENT_USER, keyname), "Failed to delete key\n");
1519 static void test_symlinks(void)
1521 static const WCHAR targetW[] = {'\\','S','o','f','t','w','a','r','e','\\','W','i','n','e',
1522 '\\','T','e','s','t','\\','t','a','r','g','e','t',0};
1523 BYTE buffer[1024];
1524 UNICODE_STRING target_str;
1525 WCHAR *target;
1526 HKEY key, link;
1527 NTSTATUS status;
1528 DWORD target_len, type, len, dw, err;
1530 if (!pRtlFormatCurrentUserKeyPath || !pNtDeleteKey)
1532 win_skip( "Can't perform symlink tests\n" );
1533 return;
1536 pRtlFormatCurrentUserKeyPath( &target_str );
1538 target_len = target_str.Length + sizeof(targetW);
1539 target = HeapAlloc( GetProcessHeap(), 0, target_len );
1540 memcpy( target, target_str.Buffer, target_str.Length );
1541 memcpy( target + target_str.Length/sizeof(WCHAR), targetW, sizeof(targetW) );
1543 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1544 KEY_ALL_ACCESS, NULL, &link, NULL );
1545 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed: %u\n", err );
1547 /* REG_SZ is not allowed */
1548 err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_SZ, (BYTE *)"foobar", sizeof("foobar") );
1549 ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1550 err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_LINK,
1551 (BYTE *)target, target_len - sizeof(WCHAR) );
1552 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1553 /* other values are not allowed */
1554 err = RegSetValueExA( link, "link", 0, REG_LINK, (BYTE *)target, target_len - sizeof(WCHAR) );
1555 ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1557 /* try opening the target through the link */
1559 err = RegOpenKeyA( hkey_main, "link", &key );
1560 ok( err == ERROR_FILE_NOT_FOUND, "RegOpenKey wrong error %u\n", err );
1562 err = RegCreateKeyExA( hkey_main, "target", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1563 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1565 dw = 0xbeef;
1566 err = RegSetValueExA( key, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1567 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1568 RegCloseKey( key );
1570 err = RegOpenKeyA( hkey_main, "link", &key );
1571 ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1573 len = sizeof(buffer);
1574 err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1575 ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1576 ok( len == sizeof(DWORD), "wrong len %u\n", len );
1578 len = sizeof(buffer);
1579 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1580 ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1582 /* REG_LINK can be created in non-link keys */
1583 err = RegSetValueExA( key, "SymbolicLinkValue", 0, REG_LINK,
1584 (BYTE *)target, target_len - sizeof(WCHAR) );
1585 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1586 len = sizeof(buffer);
1587 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1588 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1589 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1590 err = RegDeleteValueA( key, "SymbolicLinkValue" );
1591 ok( err == ERROR_SUCCESS, "RegDeleteValue failed error %u\n", err );
1593 RegCloseKey( key );
1595 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1596 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1598 len = sizeof(buffer);
1599 err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1600 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1601 ok( len == sizeof(DWORD), "wrong len %u\n", len );
1603 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1604 ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1605 RegCloseKey( key );
1607 /* now open the symlink itself */
1609 err = RegOpenKeyExA( hkey_main, "link", REG_OPTION_OPEN_LINK, KEY_ALL_ACCESS, &key );
1610 ok( err == ERROR_SUCCESS, "RegOpenKeyEx failed error %u\n", err );
1611 len = sizeof(buffer);
1612 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1613 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1614 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1615 RegCloseKey( key );
1617 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_OPEN_LINK,
1618 KEY_ALL_ACCESS, NULL, &key, NULL );
1619 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1620 len = sizeof(buffer);
1621 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1622 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1623 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1624 RegCloseKey( key );
1626 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1627 KEY_ALL_ACCESS, NULL, &key, NULL );
1628 ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1630 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK | REG_OPTION_OPEN_LINK,
1631 KEY_ALL_ACCESS, NULL, &key, NULL );
1632 ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1634 err = RegDeleteKey( hkey_main, "target" );
1635 ok( err == ERROR_SUCCESS, "RegDeleteKey failed error %u\n", err );
1637 err = RegDeleteKey( hkey_main, "link" );
1638 ok( err == ERROR_FILE_NOT_FOUND, "RegDeleteKey wrong error %u\n", err );
1640 status = pNtDeleteKey( link );
1641 ok( !status, "NtDeleteKey failed: 0x%08x\n", status );
1642 RegCloseKey( link );
1644 HeapFree( GetProcessHeap(), 0, target );
1645 pRtlFreeUnicodeString( &target_str );
1648 static const DWORD ptr_size = 8 * sizeof(void*);
1650 static DWORD get_key_value( HKEY root, const char *name, DWORD flags )
1652 HKEY key;
1653 DWORD err, type, dw, len = sizeof(dw);
1655 err = RegCreateKeyExA( root, name, 0, NULL, 0, flags | KEY_ALL_ACCESS, NULL, &key, NULL );
1656 if (err == ERROR_FILE_NOT_FOUND) return 0;
1657 ok( err == ERROR_SUCCESS, "%08x: RegCreateKeyEx failed: %u\n", flags, err );
1659 err = RegQueryValueExA( key, "value", NULL, &type, (BYTE *)&dw, &len );
1660 if (err == ERROR_FILE_NOT_FOUND)
1661 dw = 0;
1662 else
1663 ok( err == ERROR_SUCCESS, "%08x: RegQueryValueEx failed: %u\n", flags, err );
1664 RegCloseKey( key );
1665 return dw;
1668 static void _check_key_value( int line, HANDLE root, const char *name, DWORD flags, DWORD expect )
1670 DWORD dw = get_key_value( root, name, flags );
1671 ok_(__FILE__,line)( dw == expect, "%08x: wrong value %u/%u\n", flags, dw, expect );
1673 #define check_key_value(root,name,flags,expect) _check_key_value( __LINE__, root, name, flags, expect )
1675 static void test_redirection(void)
1677 DWORD err, type, dw, len;
1678 HKEY key, root32, root64, key32, key64;
1679 BOOL is_vista = FALSE;
1681 if (ptr_size != 64)
1683 BOOL is_wow64;
1684 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 ) || !is_wow64)
1686 skip( "Not on Wow64, no redirection\n" );
1687 return;
1691 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1692 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &root64, NULL );
1693 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1695 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1696 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &root32, NULL );
1697 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1699 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, NULL, 0,
1700 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key64, NULL );
1701 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1703 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, NULL, 0,
1704 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key32, NULL );
1705 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1707 dw = 64;
1708 err = RegSetValueExA( key64, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1709 ok( err == ERROR_SUCCESS, "RegSetValueExA failed: %u\n", err );
1711 dw = 32;
1712 err = RegSetValueExA( key32, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1713 ok( err == ERROR_SUCCESS, "RegSetValueExA failed: %u\n", err );
1715 dw = 0;
1716 len = sizeof(dw);
1717 err = RegQueryValueExA( key32, "value", NULL, &type, (BYTE *)&dw, &len );
1718 ok( err == ERROR_SUCCESS, "RegQueryValueExA failed: %u\n", err );
1719 ok( dw == 32, "wrong value %u\n", dw );
1721 dw = 0;
1722 len = sizeof(dw);
1723 err = RegQueryValueExA( key64, "value", NULL, &type, (BYTE *)&dw, &len );
1724 ok( err == ERROR_SUCCESS, "RegQueryValueExA failed: %u\n", err );
1725 ok( dw == 64, "wrong value %u\n", dw );
1727 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
1728 KEY_ALL_ACCESS, NULL, &key, NULL );
1729 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1731 if (ptr_size == 32)
1733 /* the Vista mechanism allows opening Wow6432Node from a 32-bit key too */
1734 /* the new (and simpler) Win7 mechanism doesn't */
1735 if (get_key_value( key, "Wow6432Node\\Wine\\Winetest", 0 ) == 32)
1737 trace( "using Vista-style Wow6432Node handling\n" );
1738 is_vista = TRUE;
1740 check_key_value( key, "Wine\\Winetest", 0, 32 );
1741 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1742 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1743 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, is_vista ? 32 : 0 );
1744 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 0 );
1745 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, is_vista ? 32 : 0 );
1747 else
1749 if (get_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY ) == 64)
1751 trace( "using Vista-style Wow6432Node handling\n" );
1752 is_vista = TRUE;
1754 check_key_value( key, "Wine\\Winetest", 0, 64 );
1755 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
1757 RegCloseKey( key );
1759 if (ptr_size == 32)
1761 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
1762 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1763 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1764 dw = get_key_value( key, "Wine\\Winetest", 0 );
1765 ok( dw == 64 || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
1766 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, 64 );
1767 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1768 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
1769 dw = get_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY );
1770 ok( dw == 32 || broken(dw == 64) /* xp64 */, "wrong value %u\n", dw );
1771 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1772 RegCloseKey( key );
1774 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
1775 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1776 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1777 check_key_value( key, "Wine\\Winetest", 0, 32 );
1778 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1779 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1780 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, is_vista ? 32 : 0 );
1781 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 0 );
1782 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, is_vista ? 32 : 0 );
1783 RegCloseKey( key );
1786 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, ptr_size );
1787 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", 0, 32 );
1788 if (ptr_size == 64)
1790 /* KEY_WOW64 flags have no effect on 64-bit */
1791 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_64KEY, 64 );
1792 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1793 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1794 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1796 else
1798 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_64KEY, 64 );
1799 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1800 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1801 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1804 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
1805 KEY_ALL_ACCESS, NULL, &key, NULL );
1806 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1807 check_key_value( key, "Wine\\Winetest", 0, 32 );
1808 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1809 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1810 RegCloseKey( key );
1812 if (ptr_size == 32)
1814 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
1815 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1816 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1817 dw = get_key_value( key, "Wine\\Winetest", 0 );
1818 ok( dw == (is_vista ? 64 : 32) || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
1819 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1820 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1821 RegCloseKey( key );
1823 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
1824 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1825 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1826 check_key_value( key, "Wine\\Winetest", 0, 32 );
1827 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1828 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
1829 RegCloseKey( key );
1832 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
1833 KEY_ALL_ACCESS, NULL, &key, NULL );
1834 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1835 check_key_value( key, "Winetest", 0, 32 );
1836 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1837 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
1838 RegCloseKey( key );
1840 if (ptr_size == 32)
1842 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
1843 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1844 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1845 dw = get_key_value( key, "Winetest", 0 );
1846 ok( dw == 32 || (is_vista && dw == 64), "wrong value %u\n", dw );
1847 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1848 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
1849 RegCloseKey( key );
1851 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
1852 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1853 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1854 check_key_value( key, "Winetest", 0, 32 );
1855 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1856 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
1857 RegCloseKey( key );
1860 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1861 KEY_ALL_ACCESS, NULL, &key, NULL );
1862 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1863 check_key_value( key, "Winetest", 0, ptr_size );
1864 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : ptr_size );
1865 dw = get_key_value( key, "Winetest", KEY_WOW64_32KEY );
1866 if (ptr_size == 32) ok( dw == 32, "wrong value %u\n", dw );
1867 else todo_wine ok( dw == 32, "wrong value %u\n", dw );
1868 RegCloseKey( key );
1870 if (ptr_size == 32)
1872 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1873 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1874 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1875 dw = get_key_value( key, "Winetest", 0 );
1876 ok( dw == 64 || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
1877 check_key_value( key, "Winetest", KEY_WOW64_64KEY, 64 );
1878 dw = get_key_value( key, "Winetest", KEY_WOW64_32KEY );
1879 todo_wine ok( dw == 32, "wrong value %u\n", dw );
1880 RegCloseKey( key );
1882 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1883 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
1884 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
1885 check_key_value( key, "Winetest", 0, 32 );
1886 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
1887 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
1888 RegCloseKey( key );
1891 if (pRegDeleteKeyExA)
1893 err = pRegDeleteKeyExA( key32, "", KEY_WOW64_32KEY, 0 );
1894 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
1895 err = pRegDeleteKeyExA( key64, "", KEY_WOW64_64KEY, 0 );
1896 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
1897 pRegDeleteKeyExA( key64, "", KEY_WOW64_64KEY, 0 );
1898 pRegDeleteKeyExA( root64, "", KEY_WOW64_64KEY, 0 );
1900 else
1902 err = RegDeleteKeyA( key32, "" );
1903 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
1904 err = RegDeleteKeyA( key64, "" );
1905 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
1906 RegDeleteKeyA( key64, "" );
1907 RegDeleteKeyA( root64, "" );
1909 RegCloseKey( key32 );
1910 RegCloseKey( key64 );
1911 RegCloseKey( root32 );
1912 RegCloseKey( root64 );
1915 static void test_deleted_key(void)
1917 HKEY hkey, hkey2;
1918 char value[20];
1919 DWORD val_count, type;
1920 LONG res;
1922 /* Open the test key, then delete it while it's open */
1923 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey );
1925 delete_key( hkey_main );
1927 val_count = 20;
1928 type = 0;
1929 res = RegEnumValueA( hkey, 0, value, &val_count, NULL, &type, 0, 0 );
1930 todo_wine ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
1932 res = RegSetValueExA( hkey, "test", 0, REG_SZ, (const BYTE*)"value", 6);
1933 todo_wine ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
1935 res = RegOpenKeyA( hkey, "test", &hkey2 );
1936 todo_wine ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
1937 if (res == 0)
1938 RegCloseKey( hkey2 );
1940 res = RegCreateKeyA( hkey, "test", &hkey2 );
1941 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
1942 if (res == 0)
1943 RegCloseKey( hkey2 );
1945 RegCloseKey( hkey );
1947 setup_main_key();
1950 static void test_delete_value(void)
1952 LONG res;
1953 char longname[401];
1955 res = RegSetValueExA( hkey_main, "test", 0, REG_SZ, (const BYTE*)"value", 6 );
1956 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
1958 res = RegQueryValueExA( hkey_main, "test", NULL, NULL, NULL, NULL);
1959 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
1961 res = RegDeleteValueA( hkey_main, "test" );
1962 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
1964 res = RegQueryValueExA( hkey_main, "test", NULL, NULL, NULL, NULL);
1965 ok(res == ERROR_FILE_NOT_FOUND, "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
1967 res = RegDeleteValueA( hkey_main, "test" );
1968 ok(res == ERROR_FILE_NOT_FOUND, "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
1970 memset(longname, 'a', 400);
1971 longname[400] = 0;
1972 res = RegDeleteValueA( hkey_main, longname );
1973 ok(res == ERROR_FILE_NOT_FOUND || broken(res == ERROR_MORE_DATA), /* nt4, win2k */
1974 "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
1977 START_TEST(registry)
1979 /* Load pointers for functions that are not available in all Windows versions */
1980 InitFunctionPtrs();
1982 setup_main_key();
1983 test_set_value();
1984 create_test_entries();
1985 test_enum_value();
1986 test_query_value_ex();
1987 test_get_value();
1988 test_reg_open_key();
1989 test_reg_create_key();
1990 test_reg_close_key();
1991 test_reg_delete_key();
1992 test_reg_query_value();
1993 test_string_termination();
1994 test_symlinks();
1995 test_redirection();
1997 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1998 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1999 set_privileges(SE_RESTORE_NAME, TRUE))
2001 test_reg_save_key();
2002 test_reg_load_key();
2003 test_reg_unload_key();
2005 set_privileges(SE_BACKUP_NAME, FALSE);
2006 set_privileges(SE_RESTORE_NAME, FALSE);
2009 test_reg_delete_tree();
2010 test_rw_order();
2011 test_deleted_key();
2012 test_delete_value();
2014 /* cleanup */
2015 delete_key( hkey_main );
2017 test_regconnectregistry();