[ADVAPI32_WINETEST] Sync with Wine Staging 1.7.37. CORE-9246
[reactos.git] / rostests / winetests / advapi32 / registry.c
1 /*
2 * Unit tests for registry functions
3 *
4 * Copyright (c) 2002 Alexandre Julliard
5 * Copyright (c) 2010 André Hentschel
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #define WIN32_NO_STATUS
23 #define WIN32_LEAN_AND_MEAN
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winternl.h"
32 #include "winreg.h"
33 #include "winsvc.h"
34 #include "winerror.h"
35 #include "aclapi.h"
36
37 #define IS_HKCR(hk) ((UINT_PTR)hk > 0 && ((UINT_PTR)hk & 3) == 2)
38
39 static HKEY hkey_main;
40 static DWORD GLE;
41
42 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
43 static const char * sTestpath2 = "%FOO%\\subdir1";
44 static const DWORD ptr_size = 8 * sizeof(void*);
45
46 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
47 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
48 static DWORD (WINAPI *pRegDeleteKeyExA)(HKEY,LPCSTR,REGSAM,DWORD);
49 static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
50 static NTSTATUS (WINAPI * pNtDeleteKey)(HANDLE);
51 static NTSTATUS (WINAPI * pRtlFormatCurrentUserKeyPath)(UNICODE_STRING*);
52 static NTSTATUS (WINAPI * pRtlFreeUnicodeString)(PUNICODE_STRING);
53 static LONG (WINAPI *pRegDeleteKeyValueA)(HKEY,LPCSTR,LPCSTR);
54 static LONG (WINAPI *pRegSetKeyValueW)(HKEY,LPCWSTR,LPCWSTR,DWORD,const void*,DWORD);
55
56 static BOOL limited_user;
57
58
59 /* Debugging functions from wine/libs/wine/debug.c */
60
61 /* allocate some tmp string space */
62 /* FIXME: this is not 100% thread-safe */
63 static char *get_temp_buffer( int size )
64 {
65 static char *list[32];
66 static UINT pos;
67 char *ret;
68 UINT idx;
69
70 idx = ++pos % (sizeof(list)/sizeof(list[0]));
71 if (list[idx])
72 ret = HeapReAlloc( GetProcessHeap(), 0, list[idx], size );
73 else
74 ret = HeapAlloc( GetProcessHeap(), 0, size );
75 if (ret) list[idx] = ret;
76 return ret;
77 }
78
79 static const char *wine_debugstr_an( const char *str, int n )
80 {
81 static const char hex[16] = "0123456789abcdef";
82 char *dst, *res;
83 size_t size;
84
85 if (!((ULONG_PTR)str >> 16))
86 {
87 if (!str) return "(null)";
88 res = get_temp_buffer( 6 );
89 sprintf( res, "#%04x", LOWORD(str) );
90 return res;
91 }
92 if (n == -1) n = strlen(str);
93 if (n < 0) n = 0;
94 size = 10 + min( 300, n * 4 );
95 dst = res = get_temp_buffer( size );
96 *dst++ = '"';
97 while (n-- > 0 && dst <= res + size - 9)
98 {
99 unsigned char c = *str++;
100 switch (c)
101 {
102 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
103 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
104 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
105 case '"': *dst++ = '\\'; *dst++ = '"'; break;
106 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
107 default:
108 if (c >= ' ' && c <= 126)
109 *dst++ = c;
110 else
111 {
112 *dst++ = '\\';
113 *dst++ = 'x';
114 *dst++ = hex[(c >> 4) & 0x0f];
115 *dst++ = hex[c & 0x0f];
116 }
117 }
118 }
119 *dst++ = '"';
120 if (n > 0)
121 {
122 *dst++ = '.';
123 *dst++ = '.';
124 *dst++ = '.';
125 }
126 *dst++ = 0;
127 return res;
128 }
129
130 #define ADVAPI32_GET_PROC(func) \
131 p ## func = (void*)GetProcAddress(hadvapi32, #func)
132
133 static void InitFunctionPtrs(void)
134 {
135 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
136 HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
137 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
138
139 /* This function was introduced with Windows 2003 SP1 */
140 ADVAPI32_GET_PROC(RegGetValueA);
141 ADVAPI32_GET_PROC(RegDeleteTreeA);
142 ADVAPI32_GET_PROC(RegDeleteKeyExA);
143 ADVAPI32_GET_PROC(RegDeleteKeyValueA);
144 ADVAPI32_GET_PROC(RegSetKeyValueW);
145
146 pIsWow64Process = (void *)GetProcAddress( hkernel32, "IsWow64Process" );
147 pRtlFormatCurrentUserKeyPath = (void *)GetProcAddress( hntdll, "RtlFormatCurrentUserKeyPath" );
148 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
149 pNtDeleteKey = (void *)GetProcAddress( hntdll, "NtDeleteKey" );
150 }
151
152 /* delete key and all its subkeys */
153 static DWORD delete_key( HKEY hkey )
154 {
155 char name[MAX_PATH];
156 DWORD ret;
157
158 if ((ret = RegOpenKeyExA( hkey, "", 0, KEY_ENUMERATE_SUB_KEYS, &hkey ))) return ret;
159 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
160 {
161 HKEY tmp;
162 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
163 {
164 ret = delete_key( tmp );
165 RegCloseKey( tmp );
166 }
167 if (ret) break;
168 }
169 if (ret != ERROR_NO_MORE_ITEMS) return ret;
170 RegDeleteKeyA( hkey, "" );
171 RegCloseKey(hkey);
172 return 0;
173 }
174
175 static void setup_main_key(void)
176 {
177 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
178
179 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
180 }
181
182 static void check_user_privs(void)
183 {
184 DWORD ret;
185 HKEY hkey = (HKEY)0xdeadbeef;
186
187 ret = RegOpenKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WRITE, &hkey);
188 ok(ret == ERROR_SUCCESS || ret == ERROR_ACCESS_DENIED, "expected success or access denied, got %i\n", ret);
189 if (ret == ERROR_SUCCESS)
190 {
191 ok(hkey != NULL, "RegOpenKeyExA succeeded but returned NULL hkey\n");
192 RegCloseKey(hkey);
193 }
194 else
195 {
196 ok(hkey == NULL, "RegOpenKeyExA failed but returned hkey %p\n", hkey);
197 limited_user = TRUE;
198 trace("running as limited user\n");
199 }
200 }
201
202 #define lok ok_(__FILE__, line)
203 #define test_hkey_main_Value_A(name, string, full_byte_len) _test_hkey_main_Value_A(__LINE__, name, string, full_byte_len)
204 static void _test_hkey_main_Value_A(int line, LPCSTR name, LPCSTR string,
205 DWORD full_byte_len)
206 {
207 DWORD ret, type, cbData;
208 DWORD str_byte_len;
209 BYTE* value;
210
211 type=0xdeadbeef;
212 cbData=0xdeadbeef;
213 /* When successful RegQueryValueExA() leaves GLE as is,
214 * so we must reset it to detect unimplemented functions.
215 */
216 SetLastError(0xdeadbeef);
217 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
218 GLE = GetLastError();
219 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/1 failed: %d, GLE=%d\n", ret, GLE);
220 /* It is wrong for the Ansi version to not be implemented */
221 ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
222 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
223
224 str_byte_len = (string ? lstrlenA(string) : 0) + 1;
225 lok(type == REG_SZ, "RegQueryValueExA/1 returned type %d\n", type);
226 lok(cbData == full_byte_len, "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
227
228 value = HeapAlloc(GetProcessHeap(), 0, cbData+1);
229 memset(value, 0xbd, cbData+1);
230 type=0xdeadbeef;
231 ret = RegQueryValueExA(hkey_main, name, NULL, &type, value, &cbData);
232 GLE = GetLastError();
233 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/2 failed: %d, GLE=%d\n", ret, GLE);
234 if (!string)
235 {
236 /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
237 lok(*value == 0xbd, "RegQueryValueExA overflowed: cbData=%u *value=%02x\n", cbData, *value);
238 }
239 else
240 {
241 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExA/2 failed: %s/%d != %s/%d\n",
242 wine_debugstr_an((char*)value, cbData), cbData,
243 wine_debugstr_an(string, full_byte_len), full_byte_len);
244 lok(*(value+cbData) == 0xbd, "RegQueryValueExA/2 overflowed at offset %u: %02x != bd\n", cbData, *(value+cbData));
245 }
246 HeapFree(GetProcessHeap(), 0, value);
247 }
248
249 #define test_hkey_main_Value_W(name, string, full_byte_len) _test_hkey_main_Value_W(__LINE__, name, string, full_byte_len)
250 static void _test_hkey_main_Value_W(int line, LPCWSTR name, LPCWSTR string,
251 DWORD full_byte_len)
252 {
253 DWORD ret, type, cbData;
254 BYTE* value;
255
256 type=0xdeadbeef;
257 cbData=0xdeadbeef;
258 /* When successful RegQueryValueExW() leaves GLE as is,
259 * so we must reset it to detect unimplemented functions.
260 */
261 SetLastError(0xdeadbeef);
262 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
263 GLE = GetLastError();
264 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/1 failed: %d, GLE=%d\n", ret, GLE);
265 if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
266 {
267 win_skip("RegQueryValueExW() is not implemented\n");
268 return;
269 }
270
271 lok(type == REG_SZ, "RegQueryValueExW/1 returned type %d\n", type);
272 lok(cbData == full_byte_len,
273 "cbData=%d instead of %d\n", cbData, full_byte_len);
274
275 /* Give enough space to overflow by one WCHAR */
276 value = HeapAlloc(GetProcessHeap(), 0, cbData+2);
277 memset(value, 0xbd, cbData+2);
278 type=0xdeadbeef;
279 ret = RegQueryValueExW(hkey_main, name, NULL, &type, value, &cbData);
280 GLE = GetLastError();
281 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/2 failed: %d, GLE=%d\n", ret, GLE);
282 if (string)
283 {
284 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
285 wine_dbgstr_wn((WCHAR*)value, cbData / sizeof(WCHAR)), cbData,
286 wine_dbgstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
287 }
288 /* This implies that when cbData == 0, RegQueryValueExW() should not modify the buffer */
289 lok(*(value+cbData) == 0xbd, "RegQueryValueExW/2 overflowed at %u: %02x != bd\n", cbData, *(value+cbData));
290 lok(*(value+cbData+1) == 0xbd, "RegQueryValueExW/2 overflowed at %u+1: %02x != bd\n", cbData, *(value+cbData+1));
291 HeapFree(GetProcessHeap(), 0, value);
292 }
293
294 static void test_set_value(void)
295 {
296 DWORD ret;
297
298 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
299 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};
300 static const WCHAR emptyW[] = {0};
301 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
302 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};
303 static const WCHAR substring2W[] = {'T','h','i','s',0};
304
305 static const char name1A[] = "CleanSingleString";
306 static const char name2A[] = "SomeIntraZeroedString";
307 static const char emptyA[] = "";
308 static const char string1A[] = "ThisNeverBreaks";
309 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
310 static const char substring2A[] = "This";
311
312 if (0)
313 {
314 /* Crashes on NT4, Windows 2000 and XP SP1 */
315 ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
316 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
317 }
318
319 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
320 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
321 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
322 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
323
324 /* RegSetValueA ignores the size passed in */
325 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
326 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
327 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
328 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
329
330 /* stops at first null */
331 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
332 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
333 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
334 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
335
336 /* only REG_SZ is supported on NT*/
337 ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
338 ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\n", ret);
339
340 ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
341 ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\n", ret);
342
343 ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
344 ok(ret == ERROR_INVALID_PARAMETER, "got %d (expected ERROR_INVALID_PARAMETER)\n", ret);
345
346 /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
347 * Surprisingly enough we're supposed to get zero bytes out of it.
348 */
349 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
350 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
351 test_hkey_main_Value_A(name1A, NULL, 0);
352 test_hkey_main_Value_W(name1W, NULL, 0);
353
354 /* test RegSetValueExA with an empty string */
355 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
356 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
357 test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
358 test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
359
360 /* test RegSetValueExA with off-by-one size */
361 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
362 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
363 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
364 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
365
366 /* test RegSetValueExA with normal string */
367 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
368 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
369 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
370 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
371
372 /* test RegSetValueExA with intrazeroed string */
373 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
374 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
375 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
376 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
377
378 if (0)
379 {
380 /* Crashes on NT4, Windows 2000 and XP SP1 */
381 ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
382 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
383
384 RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)1, 1);
385 RegSetValueExA(hkey_main, name2A, 0, REG_DWORD, (const BYTE *)1, 1);
386 }
387
388 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
389 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
390 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
391 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
392
393 ret = RegSetValueW(hkey_main, name1W, REG_SZ, string1W, sizeof(string1W));
394 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
395 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
396 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
397
398 /* RegSetValueW ignores the size passed in */
399 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
400 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
401 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
402 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
403
404 /* stops at first null */
405 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
406 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
407 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
408 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
409
410 /* only REG_SZ is supported */
411 ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
412 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
413 ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
414 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
415 ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
416 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
417
418 /* test RegSetValueExW with off-by-one size */
419 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
420 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
421 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
422 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
423
424 /* test RegSetValueExW with normal string */
425 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
426 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
427 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
428 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
429
430 /* test RegSetValueExW with intrazeroed string */
431 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
432 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
433 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
434 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
435
436 /* test RegSetValueExW with data = 1 */
437 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)1, 1);
438 ok(ret == ERROR_NOACCESS, "RegSetValueExW should have failed with ERROR_NOACCESS: %d, GLE=%d\n", ret, GetLastError());
439 ret = RegSetValueExW(hkey_main, name2W, 0, REG_DWORD, (const BYTE *)1, 1);
440 ok(ret == ERROR_NOACCESS, "RegSetValueExW should have failed with ERROR_NOACCESS: %d, GLE=%d\n", ret, GetLastError());
441
442 if (pRegGetValueA) /* avoid a crash on Windows 2000 */
443 {
444 ret = RegSetValueExW(hkey_main, NULL, 0, REG_SZ, NULL, 4);
445 ok(ret == ERROR_NOACCESS, "RegSetValueExW should have failed with ERROR_NOACCESS: %d, GLE=%d\n", ret, GetLastError());
446
447 ret = RegSetValueExW(hkey_main, NULL, 0, REG_SZ, NULL, 0);
448 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
449
450 ret = RegSetValueExW(hkey_main, NULL, 0, REG_DWORD, NULL, 4);
451 ok(ret == ERROR_NOACCESS, "RegSetValueExW should have failed with ERROR_NOACCESS: %d, GLE=%d\n", ret, GetLastError());
452
453 ret = RegSetValueExW(hkey_main, NULL, 0, REG_DWORD, NULL, 0);
454 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
455 }
456
457 /* RegSetKeyValue */
458 if (!pRegSetKeyValueW)
459 win_skip("RegSetKeyValue() is not supported.\n");
460 else
461 {
462 static const WCHAR subkeyW[] = {'s','u','b','k','e','y',0};
463 DWORD len, type;
464 HKEY subkey;
465
466 ret = pRegSetKeyValueW(hkey_main, NULL, name1W, REG_SZ, (const BYTE*)string2W, sizeof(string2W));
467 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
468 test_hkey_main_Value_A(name1A, string2A, sizeof(string2A));
469 test_hkey_main_Value_W(name1W, string2W, sizeof(string2W));
470
471 ret = pRegSetKeyValueW(hkey_main, subkeyW, name1W, REG_SZ, string1W, sizeof(string1W));
472 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
473
474 ret = RegOpenKeyExW(hkey_main, subkeyW, 0, KEY_QUERY_VALUE, &subkey);
475 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
476 type = len = 0;
477 ret = RegQueryValueExW(subkey, name1W, 0, &type, NULL, &len);
478 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
479 ok(len == sizeof(string1W), "got %d\n", len);
480 ok(type == REG_SZ, "got type %d\n", type);
481
482 ret = pRegSetKeyValueW(hkey_main, subkeyW, name1W, REG_SZ, NULL, 0);
483 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
484
485 ret = pRegSetKeyValueW(hkey_main, subkeyW, name1W, REG_SZ, NULL, 4);
486 ok(ret == ERROR_NOACCESS, "got %d\n", ret);
487
488 ret = pRegSetKeyValueW(hkey_main, subkeyW, name1W, REG_DWORD, NULL, 4);
489 ok(ret == ERROR_NOACCESS, "got %d\n", ret);
490
491 RegCloseKey(subkey);
492 }
493 }
494
495 static void create_test_entries(void)
496 {
497 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
498
499 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
500 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
501
502 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
503 "RegSetValueExA failed\n");
504 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
505 "RegSetValueExA failed\n");
506 ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
507 "RegSetValueExA failed\n");
508 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
509 "RegSetValueExA failed\n");
510 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
511 "RegSetValueExA failed\n");
512 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
513 "RegSetValueExA failed\n");
514 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
515 "RegSetValueExA failed\n");
516 }
517
518 static void test_enum_value(void)
519 {
520 DWORD res;
521 HKEY test_key;
522 char value[20], data[20];
523 WCHAR valueW[20], dataW[20];
524 DWORD val_count, data_count, type;
525 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
526 static const WCHAR testW[] = {'T','e','s','t',0};
527 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
528
529 /* create the working key for new 'Test' value */
530 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
531 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
532
533 /* check NULL data with zero length */
534 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
535 if (GetVersion() & 0x80000000)
536 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
537 else
538 ok( !res, "RegSetValueExA returned %d\n", res );
539 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
540 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
541 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
542 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
543
544 /* test reading the value and data without setting them */
545 val_count = 20;
546 data_count = 20;
547 type = 1234;
548 strcpy( value, "xxxxxxxxxx" );
549 strcpy( data, "xxxxxxxxxx" );
550 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
551 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
552 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
553 ok( data_count == 0, "data_count set to %d instead of 0\n", data_count );
554 ok( type == REG_BINARY, "type %d is not REG_BINARY\n", type );
555 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
556 ok( !strcmp( data, "xxxxxxxxxx" ), "data is '%s' instead of xxxxxxxxxx\n", data );
557
558 val_count = 20;
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_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
565 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
566 ok( data_count == 0, "data_count set to %d instead of 0\n", data_count );
567 ok( type == REG_BINARY, "type %d is not REG_BINARY\n", type );
568 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
569 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data is not 'xxxxxxxxxx'\n" );
570
571 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
572 ok( res == 0, "RegSetValueExA failed error %d\n", res );
573
574 /* overflow both name and data */
575 val_count = 2;
576 data_count = 2;
577 type = 1234;
578 strcpy( value, "xxxxxxxxxx" );
579 strcpy( data, "xxxxxxxxxx" );
580 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
581 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
582 ok( val_count == 2, "val_count set to %d\n", val_count );
583 ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
584 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
585 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
586 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
587
588 /* overflow name */
589 val_count = 3;
590 data_count = 20;
591 type = 1234;
592 strcpy( value, "xxxxxxxxxx" );
593 strcpy( data, "xxxxxxxxxx" );
594 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
595 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
596 ok( val_count == 3, "val_count set to %d\n", val_count );
597 ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
598 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
599 /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
600 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
601 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
602 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ) || broken( !strcmp( data, "xxxxxxxx" ) && data_count == 8 ),
603 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
604
605 /* overflow empty name */
606 val_count = 0;
607 data_count = 20;
608 type = 1234;
609 strcpy( value, "xxxxxxxxxx" );
610 strcpy( data, "xxxxxxxxxx" );
611 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
612 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
613 ok( val_count == 0, "val_count set to %d\n", val_count );
614 ok( data_count == 7 || broken( data_count == 8 ), "data_count set to %d instead of 7\n", data_count );
615 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
616 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
617 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
618 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ) || broken( !strcmp( data, "xxxxxxxx" ) && data_count == 8 ),
619 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
620
621 /* overflow data */
622 val_count = 20;
623 data_count = 2;
624 type = 1234;
625 strcpy( value, "xxxxxxxxxx" );
626 strcpy( data, "xxxxxxxxxx" );
627 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
628 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
629 ok( val_count == 20, "val_count set to %d\n", val_count );
630 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
631 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
632 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
633 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
634
635 /* no overflow */
636 val_count = 20;
637 data_count = 20;
638 type = 1234;
639 strcpy( value, "xxxxxxxxxx" );
640 strcpy( data, "xxxxxxxxxx" );
641 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
642 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
643 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
644 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
645 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
646 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
647 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
648
649 /* Unicode tests */
650
651 SetLastError(0xdeadbeef);
652 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
653 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
654 {
655 win_skip("RegSetValueExW is not implemented\n");
656 goto cleanup;
657 }
658 ok( res == 0, "RegSetValueExW failed error %d\n", res );
659
660 /* overflow both name and data */
661 val_count = 2;
662 data_count = 2;
663 type = 1234;
664 memcpy( valueW, xxxW, sizeof(xxxW) );
665 memcpy( dataW, xxxW, sizeof(xxxW) );
666 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
667 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
668 ok( val_count == 2, "val_count set to %d\n", val_count );
669 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
670 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
671 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
672 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
673
674 /* overflow name */
675 val_count = 3;
676 data_count = 20;
677 type = 1234;
678 memcpy( valueW, xxxW, sizeof(xxxW) );
679 memcpy( dataW, xxxW, sizeof(xxxW) );
680 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
681 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
682 ok( val_count == 3, "val_count set to %d\n", val_count );
683 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
684 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
685 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
686 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
687
688 /* overflow data */
689 val_count = 20;
690 data_count = 2;
691 type = 1234;
692 memcpy( valueW, xxxW, sizeof(xxxW) );
693 memcpy( dataW, xxxW, sizeof(xxxW) );
694 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
695 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
696 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
697 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
698 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
699 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
700 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
701
702 /* no overflow */
703 val_count = 20;
704 data_count = 20;
705 type = 1234;
706 memcpy( valueW, xxxW, sizeof(xxxW) );
707 memcpy( dataW, xxxW, sizeof(xxxW) );
708 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
709 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
710 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
711 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
712 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
713 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
714 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
715
716 cleanup:
717 RegDeleteKeyA(test_key, "");
718 RegCloseKey(test_key);
719 }
720
721 static void test_query_value_ex(void)
722 {
723 DWORD ret;
724 DWORD size;
725 DWORD type;
726 BYTE buffer[10];
727
728 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
729 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
730 ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
731 ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
732
733 type = 0xdeadbeef;
734 size = 0xdeadbeef;
735 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
736 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
737 ok(size == 0, "size should have been set to 0 instead of %d\n", size);
738
739 size = sizeof(buffer);
740 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
741 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
742 ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
743
744 size = 4;
745 ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
746 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
747 }
748
749 static void test_get_value(void)
750 {
751 DWORD ret;
752 DWORD size;
753 DWORD type;
754 DWORD dw, qw[2];
755 CHAR buf[80];
756 CHAR expanded[] = "bar\\subdir1";
757 CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
758
759 if(!pRegGetValueA)
760 {
761 win_skip("RegGetValue not available on this platform\n");
762 return;
763 }
764
765 /* Invalid parameter */
766 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
767 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
768
769 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
770 size = type = dw = 0xdeadbeef;
771 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
772 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
773 ok(size == 4, "size=%d\n", size);
774 ok(type == REG_DWORD, "type=%d\n", type);
775 ok(dw == 0x12345678, "dw=%d\n", dw);
776
777 /* Query by subkey-name */
778 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
779 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
780
781 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
782 size = type = dw = 0xdeadbeef;
783 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
784 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
785 /* Although the function failed all values are retrieved */
786 ok(size == 4, "size=%d\n", size);
787 ok(type == REG_DWORD, "type=%d\n", type);
788 ok(dw == 0x12345678, "dw=%d\n", dw);
789
790 /* Test RRF_ZEROONFAILURE */
791 type = dw = 0xdeadbeef; size = 4;
792 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
793 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
794 /* Again all values are retrieved ... */
795 ok(size == 4, "size=%d\n", size);
796 ok(type == REG_DWORD, "type=%d\n", type);
797 /* ... except the buffer, which is zeroed out */
798 ok(dw == 0, "dw=%d\n", dw);
799
800 /* Test RRF_ZEROONFAILURE with a NULL buffer... */
801 type = size = 0xbadbeef;
802 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
803 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
804 ok(size == 4, "size=%d\n", size);
805 ok(type == REG_DWORD, "type=%d\n", type);
806
807 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
808 size = type = dw = 0xdeadbeef;
809 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
810 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
811 ok(size == 4, "size=%d\n", size);
812 ok(type == REG_DWORD, "type=%d\n", type);
813 ok(dw == 0x12345678, "dw=%d\n", dw);
814
815 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
816 size = type = dw = 0xdeadbeef;
817 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
818 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
819 ok(size == 4, "size=%d\n", size);
820 ok(type == REG_BINARY, "type=%d\n", type);
821 ok(dw == 0x12345678, "dw=%d\n", dw);
822
823 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
824 qw[0] = qw[1] = size = type = 0xdeadbeef;
825 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
826 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
827 ok(size == 8, "size=%d\n", size);
828 ok(type == REG_BINARY, "type=%d\n", type);
829 ok(qw[0] == 0x12345678 &&
830 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
831
832 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
833 type = dw = 0xdeadbeef; size = 4;
834 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
835 ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
836 ok(dw == 0xdeadbeef, "dw=%d\n", dw);
837 ok(size == 8, "size=%d\n", size);
838
839 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
840 qw[0] = qw[1] = size = type = 0xdeadbeef;
841 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
842 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
843 ok(size == 8, "size=%d\n", size);
844 ok(type == REG_BINARY, "type=%d\n", type);
845 ok(qw[0] == 0x12345678 &&
846 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
847
848 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
849 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
850 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
851 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
852 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
853 ok(type == REG_SZ, "type=%d\n", type);
854 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
855
856 /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
857 type = 0xdeadbeef; size = 0;
858 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
859 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
860 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
861 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
862 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
863 ok(type == REG_SZ, "type=%d\n", type);
864
865 /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
866 strcpy(buf, sTestpath1);
867 type = 0xdeadbeef;
868 size = sizeof(buf);
869 ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
870 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
871 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
872 ok(size == 0 ||
873 size == 1, /* win2k3 */
874 "size=%d\n", size);
875 ok(type == REG_SZ, "type=%d\n", type);
876 ok(!strcmp(sTestpath1, buf) ||
877 !strcmp(buf, ""),
878 "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
879
880 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
881 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
882 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
883 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
884 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
885 ok(type == REG_SZ, "type=%d\n", type);
886 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
887
888 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
889 size = 0;
890 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
891 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
892 ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
893 (size == strlen(expanded2)+2) || /* win2k3 SP2 */
894 (size == strlen(sTestpath2)+1),
895 "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
896
897 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
898 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
899 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
900 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
901 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
902 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
903 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
904 ok(type == REG_SZ, "type=%d\n", type);
905 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
906
907 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
908 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
909 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
910 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
911 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
912 ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
913 "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
914 ok(type == REG_SZ, "type=%d\n", type);
915 ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
916
917 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
918 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
919 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
920 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
921 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
922 ok(type == REG_EXPAND_SZ, "type=%d\n", type);
923 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
924
925 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
926 size = 0xbadbeef;
927 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
928 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
929 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
930 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
931 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
932
933 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
934 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
935 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
936
937 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
938 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
939 /* before win8: ERROR_INVALID_PARAMETER, win8: ERROR_UNSUPPORTED_TYPE */
940 ok(ret == ERROR_INVALID_PARAMETER || ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
941
942 /* Query REG_EXPAND_SZ using RRF_RT_ANY */
943 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
944 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
945 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
946 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
947 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
948 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
949 ok(type == REG_SZ, "type=%d\n", type);
950 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
951 }
952
953 static void test_reg_open_key(void)
954 {
955 DWORD ret = 0;
956 HKEY hkResult = NULL;
957 HKEY hkPreserve = NULL;
958 HKEY hkRoot64 = NULL;
959 HKEY hkRoot32 = NULL;
960 BOOL bRet;
961 SID_IDENTIFIER_AUTHORITY sid_authority = {SECURITY_WORLD_SID_AUTHORITY};
962 PSID world_sid;
963 EXPLICIT_ACCESSA access;
964 PACL key_acl;
965 SECURITY_DESCRIPTOR *sd;
966
967 /* successful open */
968 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
969 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
970 ok(hkResult != NULL, "expected hkResult != NULL\n");
971 hkPreserve = hkResult;
972
973 /* open same key twice */
974 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
975 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
976 ok(hkResult != hkPreserve, "expected hkResult != hkPreserve\n");
977 ok(hkResult != NULL, "hkResult != NULL\n");
978 RegCloseKey(hkResult);
979
980 /* trailing slashes */
981 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test\\\\", &hkResult);
982 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
983 RegCloseKey(hkResult);
984
985 /* open nonexistent key
986 * check that hkResult is set to NULL
987 */
988 hkResult = hkPreserve;
989 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
990 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
991 ok(hkResult == NULL, "expected hkResult == NULL\n");
992
993 /* open the same nonexistent key again to make sure the key wasn't created */
994 hkResult = hkPreserve;
995 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
996 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
997 ok(hkResult == NULL, "expected hkResult == NULL\n");
998
999 /* send in NULL lpSubKey
1000 * check that hkResult receives the value of hKey
1001 */
1002 hkResult = hkPreserve;
1003 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
1004 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1005 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
1006
1007 /* send empty-string in lpSubKey */
1008 hkResult = hkPreserve;
1009 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
1010 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1011 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
1012
1013 /* send in NULL lpSubKey and NULL hKey
1014 * hkResult is set to NULL
1015 */
1016 hkResult = hkPreserve;
1017 ret = RegOpenKeyA(NULL, NULL, &hkResult);
1018 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1019 ok(hkResult == NULL, "expected hkResult == NULL\n");
1020
1021 /* only send NULL hKey
1022 * the value of hkResult remains unchanged
1023 */
1024 hkResult = hkPreserve;
1025 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
1026 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1027 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1028 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
1029 RegCloseKey(hkResult);
1030
1031 /* send in NULL hkResult */
1032 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
1033 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1034
1035 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, NULL);
1036 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1037
1038 ret = RegOpenKeyA(NULL, NULL, NULL);
1039 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1040
1041 /* beginning backslash character */
1042 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
1043 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
1044 broken(ret == ERROR_SUCCESS), /* wow64 */
1045 "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
1046 if (!ret) RegCloseKey(hkResult);
1047
1048 hkResult = NULL;
1049 ret = RegOpenKeyExA(HKEY_CLASSES_ROOT, "\\clsid", 0, KEY_QUERY_VALUE, &hkResult);
1050 ok(ret == ERROR_SUCCESS || /* 2k/XP */
1051 ret == ERROR_BAD_PATHNAME, /* NT */
1052 "expected ERROR_SUCCESS, ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
1053 RegCloseKey(hkResult);
1054
1055 /* WOW64 flags */
1056 hkResult = NULL;
1057 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_32KEY, &hkResult);
1058 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1059 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1060 RegCloseKey(hkResult);
1061
1062 hkResult = NULL;
1063 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_64KEY, &hkResult);
1064 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1065 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1066 RegCloseKey(hkResult);
1067
1068 /* check special HKEYs on 64bit
1069 * only the lower 4 bytes of the supplied key are used
1070 */
1071 if (ptr_size == 64)
1072 {
1073 /* HKEY_CURRENT_USER */
1074 ret = RegOpenKeyA(UlongToHandle(HandleToUlong(HKEY_CURRENT_USER)), "Software", &hkResult);
1075 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1076 ok(hkResult != NULL, "expected hkResult != NULL\n");
1077 RegCloseKey(hkResult);
1078
1079 ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)1 << 32), "Software", &hkResult);
1080 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1081 ok(hkResult != NULL, "expected hkResult != NULL\n");
1082 RegCloseKey(hkResult);
1083
1084 ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)0xdeadbeef << 32), "Software", &hkResult);
1085 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1086 ok(hkResult != NULL, "expected hkResult != NULL\n");
1087 RegCloseKey(hkResult);
1088
1089 ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_CURRENT_USER) | (ULONG64)0xffffffff << 32), "Software", &hkResult);
1090 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1091 ok(hkResult != NULL, "expected hkResult != NULL\n");
1092 RegCloseKey(hkResult);
1093
1094 /* HKEY_LOCAL_MACHINE */
1095 ret = RegOpenKeyA((HKEY)(HandleToUlong(HKEY_LOCAL_MACHINE) | (ULONG64)0xdeadbeef << 32), "Software", &hkResult);
1096 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1097 ok(hkResult != NULL, "expected hkResult != NULL\n");
1098 RegCloseKey(hkResult);
1099 }
1100
1101 /* Try using WOW64 flags when opening a key with a DACL set to verify that
1102 * the registry access check is performed correctly. Redirection isn't
1103 * being tested, so the tests don't care about whether the process is
1104 * running under WOW64. */
1105 if (!pIsWow64Process)
1106 {
1107 win_skip("WOW64 flags are not recognized\n");
1108 return;
1109 }
1110
1111 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1112 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &hkRoot32, NULL);
1113 if (limited_user)
1114 ok(ret == ERROR_ACCESS_DENIED && hkRoot32 == NULL,
1115 "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1116 else
1117 ok(ret == ERROR_SUCCESS && hkRoot32 != NULL,
1118 "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1119
1120 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1121 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &hkRoot64, NULL);
1122 if (limited_user)
1123 ok(ret == ERROR_ACCESS_DENIED && hkRoot64 == NULL,
1124 "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1125 else
1126 ok(ret == ERROR_SUCCESS && hkRoot64 != NULL,
1127 "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1128
1129 bRet = AllocateAndInitializeSid(&sid_authority, 1, SECURITY_WORLD_RID,
1130 0, 0, 0, 0, 0, 0, 0, &world_sid);
1131 ok(bRet == TRUE,
1132 "Expected AllocateAndInitializeSid to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1133
1134 access.grfAccessPermissions = GENERIC_ALL | STANDARD_RIGHTS_ALL;
1135 access.grfAccessMode = SET_ACCESS;
1136 access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
1137 access.Trustee.pMultipleTrustee = NULL;
1138 access.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
1139 access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
1140 access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
1141 access.Trustee.ptstrName = (char *)world_sid;
1142
1143 ret = SetEntriesInAclA(1, &access, NULL, &key_acl);
1144 ok(ret == ERROR_SUCCESS,
1145 "Expected SetEntriesInAclA to return ERROR_SUCCESS, got %u, last error %u\n", ret, GetLastError());
1146
1147 sd = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1148 bRet = InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION);
1149 ok(bRet == TRUE,
1150 "Expected InitializeSecurityDescriptor to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1151
1152 bRet = SetSecurityDescriptorDacl(sd, TRUE, key_acl, FALSE);
1153 ok(bRet == TRUE,
1154 "Expected SetSecurityDescriptorDacl to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1155
1156 if (limited_user)
1157 {
1158 skip("not enough privileges to modify HKLM\n");
1159 }
1160 else
1161 {
1162 LONG error;
1163
1164 error = RegSetKeySecurity(hkRoot64, DACL_SECURITY_INFORMATION, sd);
1165 ok(error == ERROR_SUCCESS,
1166 "Expected RegSetKeySecurity to return success, got error %u\n", error);
1167
1168 error = RegSetKeySecurity(hkRoot32, DACL_SECURITY_INFORMATION, sd);
1169 ok(error == ERROR_SUCCESS,
1170 "Expected RegSetKeySecurity to return success, got error %u\n", error);
1171
1172 hkResult = NULL;
1173 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, KEY_WOW64_64KEY | KEY_READ, &hkResult);
1174 ok(ret == ERROR_SUCCESS && hkResult != NULL,
1175 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1176 RegCloseKey(hkResult);
1177
1178 hkResult = NULL;
1179 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, KEY_WOW64_32KEY | KEY_READ, &hkResult);
1180 ok(ret == ERROR_SUCCESS && hkResult != NULL,
1181 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1182 RegCloseKey(hkResult);
1183 }
1184
1185 HeapFree(GetProcessHeap(), 0, sd);
1186 LocalFree(key_acl);
1187 FreeSid(world_sid);
1188 RegDeleteKeyA(hkRoot64, "");
1189 RegCloseKey(hkRoot64);
1190 RegDeleteKeyA(hkRoot32, "");
1191 RegCloseKey(hkRoot32);
1192 }
1193
1194 static void test_reg_create_key(void)
1195 {
1196 LONG ret;
1197 HKEY hkey1, hkey2;
1198 HKEY hkRoot64 = NULL;
1199 HKEY hkRoot32 = NULL;
1200 DWORD dwRet;
1201 BOOL bRet;
1202 SID_IDENTIFIER_AUTHORITY sid_authority = {SECURITY_WORLD_SID_AUTHORITY};
1203 PSID world_sid;
1204 EXPLICIT_ACCESSA access;
1205 PACL key_acl;
1206 SECURITY_DESCRIPTOR *sd;
1207
1208 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
1209 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1210 /* should succeed: all versions of Windows ignore the access rights
1211 * to the parent handle */
1212 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
1213 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1214
1215 /* clean up */
1216 RegDeleteKeyA(hkey2, "");
1217 RegDeleteKeyA(hkey1, "");
1218 RegCloseKey(hkey2);
1219 RegCloseKey(hkey1);
1220
1221 /* test creation of volatile keys */
1222 ret = RegCreateKeyExA(hkey_main, "Volatile", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey1, NULL);
1223 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1224 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1225 ok(ret == ERROR_CHILD_MUST_BE_VOLATILE, "RegCreateKeyExA failed with error %d\n", ret);
1226 if (!ret) RegCloseKey( hkey2 );
1227 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1228 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1229 RegCloseKey(hkey2);
1230 /* should succeed if the key already exists */
1231 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
1232 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1233
1234 /* clean up */
1235 RegDeleteKeyA(hkey2, "");
1236 RegDeleteKeyA(hkey1, "");
1237 RegCloseKey(hkey2);
1238 RegCloseKey(hkey1);
1239
1240 /* beginning backslash character */
1241 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
1242 if (!(GetVersion() & 0x80000000))
1243 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
1244 else {
1245 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1246 RegDeleteKeyA(hkey1, "");
1247 RegCloseKey(hkey1);
1248 }
1249
1250 /* trailing backslash characters */
1251 ret = RegCreateKeyExA(hkey_main, "Subkey4\\\\", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
1252 ok(ret == ERROR_SUCCESS, "RegCreateKeyExA failed with error %d\n", ret);
1253 RegDeleteKeyA(hkey1, "");
1254 RegCloseKey(hkey1);
1255
1256 /* WOW64 flags - open an existing key */
1257 hkey1 = NULL;
1258 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &hkey1, NULL);
1259 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1260 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1261 RegCloseKey(hkey1);
1262
1263 hkey1 = NULL;
1264 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_64KEY, NULL, &hkey1, NULL);
1265 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1266 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1267 RegCloseKey(hkey1);
1268
1269 /* Try using WOW64 flags when opening a key with a DACL set to verify that
1270 * the registry access check is performed correctly. Redirection isn't
1271 * being tested, so the tests don't care about whether the process is
1272 * running under WOW64. */
1273 if (!pIsWow64Process)
1274 {
1275 win_skip("WOW64 flags are not recognized\n");
1276 return;
1277 }
1278
1279 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1280 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &hkRoot32, NULL);
1281 if (limited_user)
1282 ok(ret == ERROR_ACCESS_DENIED && hkRoot32 == NULL,
1283 "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%d)\n", ret);
1284 else
1285 ok(ret == ERROR_SUCCESS && hkRoot32 != NULL,
1286 "RegCreateKeyEx with KEY_WOW64_32KEY failed (err=%d)\n", ret);
1287
1288 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1289 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &hkRoot64, NULL);
1290 if (limited_user)
1291 ok(ret == ERROR_ACCESS_DENIED && hkRoot64 == NULL,
1292 "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%d)\n", ret);
1293 else
1294 ok(ret == ERROR_SUCCESS && hkRoot64 != NULL,
1295 "RegCreateKeyEx with KEY_WOW64_64KEY failed (err=%d)\n", ret);
1296
1297 bRet = AllocateAndInitializeSid(&sid_authority, 1, SECURITY_WORLD_RID,
1298 0, 0, 0, 0, 0, 0, 0, &world_sid);
1299 ok(bRet == TRUE,
1300 "Expected AllocateAndInitializeSid to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1301
1302 access.grfAccessPermissions = GENERIC_ALL | STANDARD_RIGHTS_ALL;
1303 access.grfAccessMode = SET_ACCESS;
1304 access.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
1305 access.Trustee.pMultipleTrustee = NULL;
1306 access.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
1307 access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
1308 access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
1309 access.Trustee.ptstrName = (char *)world_sid;
1310
1311 dwRet = SetEntriesInAclA(1, &access, NULL, &key_acl);
1312 ok(dwRet == ERROR_SUCCESS,
1313 "Expected SetEntriesInAclA to return ERROR_SUCCESS, got %u, last error %u\n", dwRet, GetLastError());
1314
1315 sd = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1316 bRet = InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION);
1317 ok(bRet == TRUE,
1318 "Expected InitializeSecurityDescriptor to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1319
1320 bRet = SetSecurityDescriptorDacl(sd, TRUE, key_acl, FALSE);
1321 ok(bRet == TRUE,
1322 "Expected SetSecurityDescriptorDacl to return TRUE, got %d, last error %u\n", bRet, GetLastError());
1323
1324 if (limited_user)
1325 {
1326 skip("not enough privileges to modify HKLM\n");
1327 }
1328 else
1329 {
1330 ret = RegSetKeySecurity(hkRoot64, DACL_SECURITY_INFORMATION, sd);
1331 ok(ret == ERROR_SUCCESS,
1332 "Expected RegSetKeySecurity to return success, got error %u\n", ret);
1333
1334 ret = RegSetKeySecurity(hkRoot32, DACL_SECURITY_INFORMATION, sd);
1335 ok(ret == ERROR_SUCCESS,
1336 "Expected RegSetKeySecurity to return success, got error %u\n", ret);
1337
1338 hkey1 = NULL;
1339 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1340 KEY_WOW64_64KEY | KEY_READ, NULL, &hkey1, NULL);
1341 ok(ret == ERROR_SUCCESS && hkey1 != NULL,
1342 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1343 RegCloseKey(hkey1);
1344
1345 hkey1 = NULL;
1346 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
1347 KEY_WOW64_32KEY | KEY_READ, NULL, &hkey1, NULL);
1348 ok(ret == ERROR_SUCCESS && hkey1 != NULL,
1349 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1350 RegCloseKey(hkey1);
1351 }
1352
1353 HeapFree(GetProcessHeap(), 0, sd);
1354 LocalFree(key_acl);
1355 FreeSid(world_sid);
1356 RegDeleteKeyA(hkRoot64, "");
1357 RegCloseKey(hkRoot64);
1358 RegDeleteKeyA(hkRoot32, "");
1359 RegCloseKey(hkRoot32);
1360 }
1361
1362 static void test_reg_close_key(void)
1363 {
1364 DWORD ret = 0;
1365 HKEY hkHandle;
1366
1367 /* successfully close key
1368 * hkHandle remains changed after call to RegCloseKey
1369 */
1370 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
1371 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1372 ret = RegCloseKey(hkHandle);
1373 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1374
1375 /* try to close the key twice */
1376 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
1377 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
1378 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
1379
1380 /* try to close a NULL handle */
1381 ret = RegCloseKey(NULL);
1382 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1383 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1384
1385 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1386 * win98 doesn't give a new handle when the same key is opened.
1387 * Not re-opening will make some next tests fail.
1388 */
1389 if (hkey_main == hkHandle)
1390 {
1391 trace("The main handle is most likely closed, so re-opening\n");
1392 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1393 }
1394 }
1395
1396 static void test_reg_delete_key(void)
1397 {
1398 DWORD ret;
1399 HKEY key;
1400
1401 ret = RegDeleteKeyA(hkey_main, NULL);
1402
1403 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1404 * there are also no subkeys available it will delete the key pointed to by hkey_main.
1405 * Not re-creating will make some next tests fail.
1406 */
1407 if (ret == ERROR_SUCCESS)
1408 {
1409 trace("We are probably running on NT4 or W2K as the main key is deleted,"
1410 " re-creating the main key\n");
1411 setup_main_key();
1412 }
1413 else
1414 ok(ret == ERROR_INVALID_PARAMETER ||
1415 ret == ERROR_ACCESS_DENIED ||
1416 ret == ERROR_BADKEY, /* Win95 */
1417 "ret=%d\n", ret);
1418
1419 ret = RegCreateKeyA(hkey_main, "deleteme", &key);
1420 ok(ret == ERROR_SUCCESS, "Could not create key, got %d\n", ret);
1421 ret = RegDeleteKeyA(key, "");
1422 ok(ret == ERROR_SUCCESS, "RegDeleteKeyA failed, got %d\n", ret);
1423 RegCloseKey(key);
1424 ret = RegOpenKeyA(hkey_main, "deleteme", &key);
1425 ok(ret == ERROR_FILE_NOT_FOUND, "Key was not deleted, got %d\n", ret);
1426 RegCloseKey(key);
1427 }
1428
1429 static void test_reg_save_key(void)
1430 {
1431 DWORD ret;
1432
1433 ret = RegSaveKeyA(hkey_main, "saved_key", NULL);
1434 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1435 }
1436
1437 static void test_reg_load_key(void)
1438 {
1439 DWORD ret;
1440 HKEY hkHandle;
1441
1442 ret = RegLoadKeyA(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1443 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1444
1445 ret = RegOpenKeyA(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1446 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1447
1448 RegCloseKey(hkHandle);
1449 }
1450
1451 static void test_reg_unload_key(void)
1452 {
1453 DWORD ret;
1454
1455 ret = RegUnLoadKeyA(HKEY_LOCAL_MACHINE, "Test");
1456 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1457
1458 DeleteFileA("saved_key");
1459 DeleteFileA("saved_key.LOG");
1460 }
1461
1462 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1463 {
1464 TOKEN_PRIVILEGES tp;
1465 HANDLE hToken;
1466 LUID luid;
1467
1468 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1469 return FALSE;
1470
1471 if(!LookupPrivilegeValueA(NULL, privilege, &luid))
1472 {
1473 CloseHandle(hToken);
1474 return FALSE;
1475 }
1476
1477 tp.PrivilegeCount = 1;
1478 tp.Privileges[0].Luid = luid;
1479
1480 if (set)
1481 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1482 else
1483 tp.Privileges[0].Attributes = 0;
1484
1485 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1486 if (GetLastError() != ERROR_SUCCESS)
1487 {
1488 CloseHandle(hToken);
1489 return FALSE;
1490 }
1491
1492 CloseHandle(hToken);
1493 return TRUE;
1494 }
1495
1496 /* tests that show that RegConnectRegistry and
1497 OpenSCManager accept computer names without the
1498 \\ prefix (what MSDN says). */
1499 static void test_regconnectregistry( void)
1500 {
1501 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1502 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1503 DWORD len = sizeof(compName) ;
1504 BOOL ret;
1505 LONG retl;
1506 HKEY hkey;
1507 SC_HANDLE schnd;
1508
1509 SetLastError(0xdeadbeef);
1510 ret = GetComputerNameA(compName, &len);
1511 ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1512 if( !ret) return;
1513
1514 lstrcpyA(netwName, "\\\\");
1515 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1516
1517 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1518 ok( !retl ||
1519 retl == ERROR_DLL_INIT_FAILED ||
1520 retl == ERROR_BAD_NETPATH, /* some win2k */
1521 "RegConnectRegistryA failed err = %d\n", retl);
1522 if( !retl) RegCloseKey( hkey);
1523
1524 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1525 ok( !retl ||
1526 retl == ERROR_DLL_INIT_FAILED ||
1527 retl == ERROR_BAD_NETPATH, /* some win2k */
1528 "RegConnectRegistryA failed err = %d\n", retl);
1529 if( !retl) RegCloseKey( hkey);
1530
1531 SetLastError(0xdeadbeef);
1532 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
1533 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1534 {
1535 win_skip("OpenSCManagerA is not implemented\n");
1536 return;
1537 }
1538
1539 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1540 CloseServiceHandle( schnd);
1541
1542 SetLastError(0xdeadbeef);
1543 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
1544 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1545 CloseServiceHandle( schnd);
1546
1547 }
1548
1549 static void test_reg_query_value(void)
1550 {
1551 HKEY subkey;
1552 CHAR val[MAX_PATH];
1553 WCHAR valW[5];
1554 LONG size, ret;
1555
1556 static const WCHAR expected[] = {'d','a','t','a',0};
1557
1558 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1559 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1560
1561 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1562 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1563
1564 /* try an invalid hkey */
1565 SetLastError(0xdeadbeef);
1566 size = MAX_PATH;
1567 ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1568 ok(ret == ERROR_INVALID_HANDLE ||
1569 ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1570 ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1571 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1572 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1573
1574 /* try a NULL hkey */
1575 SetLastError(0xdeadbeef);
1576 size = MAX_PATH;
1577 ret = RegQueryValueA(NULL, "subkey", val, &size);
1578 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1579 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1580 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1581
1582 /* try a NULL value */
1583 size = MAX_PATH;
1584 ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1585 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1586 ok(size == 5, "Expected 5, got %d\n", size);
1587
1588 /* try a NULL size */
1589 SetLastError(0xdeadbeef);
1590 val[0] = '\0';
1591 ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1592 ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1593 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1594 ok(!val[0], "Expected val to be untouched, got %s\n", val);
1595
1596 /* try a NULL value and size */
1597 ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1598 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1599
1600 /* try a size too small */
1601 SetLastError(0xdeadbeef);
1602 val[0] = '\0';
1603 size = 1;
1604 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1605 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1606 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1607 ok(!val[0], "Expected val to be untouched, got %s\n", val);
1608 ok(size == 5, "Expected 5, got %d\n", size);
1609
1610 /* successfully read the value using 'subkey' */
1611 size = MAX_PATH;
1612 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1613 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1614 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1615 ok(size == 5, "Expected 5, got %d\n", size);
1616
1617 /* successfully read the value using the subkey key */
1618 size = MAX_PATH;
1619 ret = RegQueryValueA(subkey, NULL, val, &size);
1620 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1621 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1622 ok(size == 5, "Expected 5, got %d\n", size);
1623
1624 /* unicode - try size too small */
1625 SetLastError(0xdeadbeef);
1626 valW[0] = '\0';
1627 size = 0;
1628 ret = RegQueryValueW(subkey, NULL, valW, &size);
1629 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1630 {
1631 win_skip("RegQueryValueW is not implemented\n");
1632 goto cleanup;
1633 }
1634 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1635 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1636 ok(!valW[0], "Expected valW to be untouched\n");
1637 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1638
1639 /* unicode - try size in WCHARS */
1640 SetLastError(0xdeadbeef);
1641 size = sizeof(valW) / sizeof(WCHAR);
1642 ret = RegQueryValueW(subkey, NULL, valW, &size);
1643 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1644 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1645 ok(!valW[0], "Expected valW to be untouched\n");
1646 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1647
1648 /* unicode - successfully read the value */
1649 size = sizeof(valW);
1650 ret = RegQueryValueW(subkey, NULL, valW, &size);
1651 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1652 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1653 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1654
1655 /* unicode - set the value without a NULL terminator */
1656 ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1657 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1658
1659 /* unicode - read the unterminated value, value is terminated for us */
1660 memset(valW, 'a', sizeof(valW));
1661 size = sizeof(valW);
1662 ret = RegQueryValueW(subkey, NULL, valW, &size);
1663 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1664 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1665 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1666
1667 cleanup:
1668 RegDeleteKeyA(subkey, "");
1669 RegCloseKey(subkey);
1670 }
1671
1672 static void test_string_termination(void)
1673 {
1674 HKEY subkey;
1675 LSTATUS ret;
1676 static const char string[] = "FullString";
1677 char name[11];
1678 BYTE buffer[11];
1679 DWORD insize, outsize, nsize;
1680
1681 ret = RegCreateKeyA(hkey_main, "string_termination", &subkey);
1682 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1683
1684 /* Off-by-one RegSetValueExA -> adds a trailing '\0'! */
1685 insize=sizeof(string)-1;
1686 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1687 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1688 outsize=insize;
1689 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1690 ok(ret == ERROR_MORE_DATA, "RegQueryValueExA returned: %d\n", ret);
1691
1692 /* Off-by-two RegSetValueExA -> no trailing '\0' */
1693 insize=sizeof(string)-2;
1694 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1695 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1696 outsize=0;
1697 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, NULL, &outsize);
1698 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1699 ok(outsize == insize, "wrong size %u != %u\n", outsize, insize);
1700
1701 /* RegQueryValueExA may return a string with no trailing '\0' */
1702 outsize=insize;
1703 memset(buffer, 0xbd, sizeof(buffer));
1704 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1705 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1706 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1707 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1708 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1709 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1710
1711 /* RegQueryValueExA adds a trailing '\0' if there is room */
1712 outsize=insize+1;
1713 memset(buffer, 0xbd, sizeof(buffer));
1714 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1715 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1716 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1717 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1718 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1719 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1720
1721 /* RegEnumValueA may return a string with no trailing '\0' */
1722 outsize=insize;
1723 memset(buffer, 0xbd, sizeof(buffer));
1724 nsize=sizeof(name);
1725 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1726 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1727 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1728 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1729 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1730 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1731 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1732
1733 /* RegEnumValueA adds a trailing '\0' if there is room */
1734 outsize=insize+1;
1735 memset(buffer, 0xbd, sizeof(buffer));
1736 nsize=sizeof(name);
1737 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1738 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1739 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1740 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1741 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1742 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1743 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1744
1745 RegDeleteKeyA(subkey, "");
1746 RegCloseKey(subkey);
1747 }
1748
1749 static void test_reg_delete_tree(void)
1750 {
1751 CHAR buffer[MAX_PATH];
1752 HKEY subkey, subkey2;
1753 LONG size, ret;
1754
1755 if(!pRegDeleteTreeA) {
1756 win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1757 return;
1758 }
1759
1760 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1761 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1762 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1763 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1764 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1765 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1766 ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1767 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1768 ret = RegCloseKey(subkey2);
1769 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1770
1771 ret = pRegDeleteTreeA(subkey, "subkey2");
1772 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1773 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1774 "subkey2 was not deleted\n");
1775 size = MAX_PATH;
1776 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1777 "Default value of subkey not longer present\n");
1778
1779 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1780 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1781 ret = RegCloseKey(subkey2);
1782 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1783 ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1784 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1785 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1786 "subkey2 was not deleted\n");
1787 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1788 "Default value of subkey not longer present\n");
1789
1790 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1791 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1792 ret = RegCloseKey(subkey2);
1793 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1794 ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1795 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1796 ret = RegCloseKey(subkey2);
1797 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1798 ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1799 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1800 ret = pRegDeleteTreeA(subkey, NULL);
1801 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1802 ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1803 "subkey was deleted\n");
1804 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1805 "subkey2 was not deleted\n");
1806 ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1807 "subkey3 was not deleted\n");
1808 size = MAX_PATH;
1809 ret = RegQueryValueA(subkey, NULL, buffer, &size);
1810 ok(ret == ERROR_SUCCESS,
1811 "Default value of subkey is not present\n");
1812 ok(!buffer[0], "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1813 size = MAX_PATH;
1814 ok(RegQueryValueA(subkey, "value", buffer, &size),
1815 "Value is still present\n");
1816
1817 ret = pRegDeleteTreeA(hkey_main, "not-here");
1818 ok(ret == ERROR_FILE_NOT_FOUND,
1819 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1820 }
1821
1822 static void test_rw_order(void)
1823 {
1824 HKEY hKey;
1825 DWORD dw = 0;
1826 static const char keyname[] = "test_rw_order";
1827 char value_buf[2];
1828 DWORD values, value_len, value_name_max_len;
1829 LSTATUS ret;
1830
1831 RegDeleteKeyA(HKEY_CURRENT_USER, keyname);
1832 ret = RegCreateKeyA(HKEY_CURRENT_USER, keyname, &hKey);
1833 if(ret != ERROR_SUCCESS) {
1834 skip("Couldn't create key. Skipping.\n");
1835 return;
1836 }
1837
1838 ok(!RegSetValueExA(hKey, "A", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1839 "RegSetValueExA for value \"A\" failed\n");
1840 ok(!RegSetValueExA(hKey, "C", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1841 "RegSetValueExA for value \"C\" failed\n");
1842 ok(!RegSetValueExA(hKey, "D", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1843 "RegSetValueExA for value \"D\" failed\n");
1844 ok(!RegSetValueExA(hKey, "B", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1845 "RegSetValueExA for value \"B\" failed\n");
1846
1847 ok(!RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &values,
1848 &value_name_max_len, NULL, NULL, NULL), "RegQueryInfoKeyA failed\n");
1849 ok(values == 4, "Expected 4 values, got %u\n", values);
1850
1851 /* Value enumeration preserves RegSetValueEx call order */
1852 value_len = 2;
1853 ok(!RegEnumValueA(hKey, 0, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1854 ok(strcmp(value_buf, "A") == 0, "Expected name \"A\", got %s\n", value_buf);
1855 value_len = 2;
1856 ok(!RegEnumValueA(hKey, 1, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1857 todo_wine ok(strcmp(value_buf, "C") == 0, "Expected name \"C\", got %s\n", value_buf);
1858 value_len = 2;
1859 ok(!RegEnumValueA(hKey, 2, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1860 todo_wine ok(strcmp(value_buf, "D") == 0, "Expected name \"D\", got %s\n", value_buf);
1861 value_len = 2;
1862 ok(!RegEnumValueA(hKey, 3, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1863 todo_wine ok(strcmp(value_buf, "B") == 0, "Expected name \"B\", got %s\n", value_buf);
1864
1865 ok(!RegDeleteKeyA(HKEY_CURRENT_USER, keyname), "Failed to delete key\n");
1866 }
1867
1868 static void test_symlinks(void)
1869 {
1870 static const WCHAR targetW[] = {'\\','S','o','f','t','w','a','r','e','\\','W','i','n','e',
1871 '\\','T','e','s','t','\\','t','a','r','g','e','t',0};
1872 BYTE buffer[1024];
1873 UNICODE_STRING target_str;
1874 WCHAR *target;
1875 HKEY key, link;
1876 NTSTATUS status;
1877 DWORD target_len, type, len, dw, err;
1878
1879 if (!pRtlFormatCurrentUserKeyPath || !pNtDeleteKey)
1880 {
1881 win_skip( "Can't perform symlink tests\n" );
1882 return;
1883 }
1884
1885 pRtlFormatCurrentUserKeyPath( &target_str );
1886
1887 target_len = target_str.Length + sizeof(targetW);
1888 target = HeapAlloc( GetProcessHeap(), 0, target_len );
1889 memcpy( target, target_str.Buffer, target_str.Length );
1890 memcpy( target + target_str.Length/sizeof(WCHAR), targetW, sizeof(targetW) );
1891
1892 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1893 KEY_ALL_ACCESS, NULL, &link, NULL );
1894 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed: %u\n", err );
1895
1896 /* REG_SZ is not allowed */
1897 err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_SZ, (BYTE *)"foobar", sizeof("foobar") );
1898 ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1899 err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_LINK,
1900 (BYTE *)target, target_len - sizeof(WCHAR) );
1901 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1902 /* other values are not allowed */
1903 err = RegSetValueExA( link, "link", 0, REG_LINK, (BYTE *)target, target_len - sizeof(WCHAR) );
1904 ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1905
1906 /* try opening the target through the link */
1907
1908 err = RegOpenKeyA( hkey_main, "link", &key );
1909 ok( err == ERROR_FILE_NOT_FOUND, "RegOpenKey wrong error %u\n", err );
1910
1911 err = RegCreateKeyExA( hkey_main, "target", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1912 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1913
1914 dw = 0xbeef;
1915 err = RegSetValueExA( key, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1916 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1917 RegCloseKey( key );
1918
1919 err = RegOpenKeyA( hkey_main, "link", &key );
1920 ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1921
1922 len = sizeof(buffer);
1923 err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1924 ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1925 ok( len == sizeof(DWORD), "wrong len %u\n", len );
1926
1927 len = sizeof(buffer);
1928 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1929 ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1930
1931 /* REG_LINK can be created in non-link keys */
1932 err = RegSetValueExA( key, "SymbolicLinkValue", 0, REG_LINK,
1933 (BYTE *)target, target_len - sizeof(WCHAR) );
1934 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1935 len = sizeof(buffer);
1936 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1937 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1938 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1939 err = RegDeleteValueA( key, "SymbolicLinkValue" );
1940 ok( err == ERROR_SUCCESS, "RegDeleteValue failed error %u\n", err );
1941
1942 RegCloseKey( key );
1943
1944 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1945 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1946
1947 len = sizeof(buffer);
1948 err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1949 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1950 ok( len == sizeof(DWORD), "wrong len %u\n", len );
1951
1952 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1953 ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1954 RegCloseKey( key );
1955
1956 /* now open the symlink itself */
1957
1958 err = RegOpenKeyExA( hkey_main, "link", REG_OPTION_OPEN_LINK, KEY_ALL_ACCESS, &key );
1959 ok( err == ERROR_SUCCESS, "RegOpenKeyEx failed error %u\n", err );
1960 len = sizeof(buffer);
1961 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1962 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1963 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1964 RegCloseKey( key );
1965
1966 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_OPEN_LINK,
1967 KEY_ALL_ACCESS, NULL, &key, NULL );
1968 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1969 len = sizeof(buffer);
1970 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1971 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1972 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1973 RegCloseKey( key );
1974
1975 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1976 KEY_ALL_ACCESS, NULL, &key, NULL );
1977 ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1978
1979 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK | REG_OPTION_OPEN_LINK,
1980 KEY_ALL_ACCESS, NULL, &key, NULL );
1981 ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1982
1983 err = RegDeleteKeyA( hkey_main, "target" );
1984 ok( err == ERROR_SUCCESS, "RegDeleteKey failed error %u\n", err );
1985
1986 err = RegDeleteKeyA( hkey_main, "link" );
1987 ok( err == ERROR_FILE_NOT_FOUND, "RegDeleteKey wrong error %u\n", err );
1988
1989 status = pNtDeleteKey( link );
1990 ok( !status, "NtDeleteKey failed: 0x%08x\n", status );
1991 RegCloseKey( link );
1992
1993 HeapFree( GetProcessHeap(), 0, target );
1994 pRtlFreeUnicodeString( &target_str );
1995 }
1996
1997 static DWORD get_key_value( HKEY root, const char *name, DWORD flags )
1998 {
1999 HKEY key;
2000 DWORD err, type, dw, len = sizeof(dw);
2001
2002 err = RegCreateKeyExA( root, name, 0, NULL, 0, flags | KEY_ALL_ACCESS, NULL, &key, NULL );
2003 if (err == ERROR_FILE_NOT_FOUND) return 0;
2004 ok( err == ERROR_SUCCESS, "%08x: RegCreateKeyEx failed: %u\n", flags, err );
2005
2006 err = RegQueryValueExA( key, "value", NULL, &type, (BYTE *)&dw, &len );
2007 if (err == ERROR_FILE_NOT_FOUND)
2008 dw = 0;
2009 else
2010 ok( err == ERROR_SUCCESS, "%08x: RegQueryValueEx failed: %u\n", flags, err );
2011 RegCloseKey( key );
2012 return dw;
2013 }
2014
2015 static void _check_key_value( int line, HANDLE root, const char *name, DWORD flags, DWORD expect )
2016 {
2017 DWORD dw = get_key_value( root, name, flags );
2018 ok_(__FILE__,line)( dw == expect, "%08x: wrong value %u/%u\n", flags, dw, expect );
2019 }
2020 #define check_key_value(root,name,flags,expect) _check_key_value( __LINE__, root, name, flags, expect )
2021
2022 static void test_redirection(void)
2023 {
2024 DWORD err, type, dw, len;
2025 HKEY key, root32, root64, key32, key64, native, op_key;
2026 BOOL is_vista = FALSE;
2027 REGSAM opposite = (sizeof(void*) == 8 ? KEY_WOW64_32KEY : KEY_WOW64_64KEY);
2028
2029 if (ptr_size != 64)
2030 {
2031 BOOL is_wow64;
2032 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 ) || !is_wow64)
2033 {
2034 skip( "Not on Wow64, no redirection\n" );
2035 return;
2036 }
2037 }
2038
2039 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2040 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &root64, NULL );
2041 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2042
2043 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2044 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &root32, NULL );
2045 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2046
2047 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, NULL, 0,
2048 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key64, NULL );
2049 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2050
2051 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, NULL, 0,
2052 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key32, NULL );
2053 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2054
2055 dw = 64;
2056 err = RegSetValueExA( key64, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
2057 ok( err == ERROR_SUCCESS, "RegSetValueExA failed: %u\n", err );
2058
2059 dw = 32;
2060 err = RegSetValueExA( key32, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
2061 ok( err == ERROR_SUCCESS, "RegSetValueExA failed: %u\n", err );
2062
2063 dw = 0;
2064 len = sizeof(dw);
2065 err = RegQueryValueExA( key32, "value", NULL, &type, (BYTE *)&dw, &len );
2066 ok( err == ERROR_SUCCESS, "RegQueryValueExA failed: %u\n", err );
2067 ok( dw == 32, "wrong value %u\n", dw );
2068
2069 dw = 0;
2070 len = sizeof(dw);
2071 err = RegQueryValueExA( key64, "value", NULL, &type, (BYTE *)&dw, &len );
2072 ok( err == ERROR_SUCCESS, "RegQueryValueExA failed: %u\n", err );
2073 ok( dw == 64, "wrong value %u\n", dw );
2074
2075 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
2076 KEY_ALL_ACCESS, NULL, &key, NULL );
2077 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2078
2079 if (ptr_size == 32)
2080 {
2081 /* the Vista mechanism allows opening Wow6432Node from a 32-bit key too */
2082 /* the new (and simpler) Win7 mechanism doesn't */
2083 if (get_key_value( key, "Wow6432Node\\Wine\\Winetest", 0 ) == 32)
2084 {
2085 trace( "using Vista-style Wow6432Node handling\n" );
2086 is_vista = TRUE;
2087 }
2088 check_key_value( key, "Wine\\Winetest", 0, 32 );
2089 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2090 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2091 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, is_vista ? 32 : 0 );
2092 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 0 );
2093 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, is_vista ? 32 : 0 );
2094 }
2095 else
2096 {
2097 if (get_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY ) == 64)
2098 {
2099 trace( "using Vista-style Wow6432Node handling\n" );
2100 is_vista = TRUE;
2101 }
2102 check_key_value( key, "Wine\\Winetest", 0, 64 );
2103 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
2104 }
2105 RegCloseKey( key );
2106
2107 if (ptr_size == 32)
2108 {
2109 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
2110 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2111 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2112 dw = get_key_value( key, "Wine\\Winetest", 0 );
2113 ok( dw == 64 || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
2114 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, 64 );
2115 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2116 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
2117 dw = get_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY );
2118 ok( dw == 32 || broken(dw == 64) /* xp64 */, "wrong value %u\n", dw );
2119 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2120 RegCloseKey( key );
2121
2122 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
2123 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2124 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2125 check_key_value( key, "Wine\\Winetest", 0, 32 );
2126 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2127 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2128 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, is_vista ? 32 : 0 );
2129 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 0 );
2130 check_key_value( key, "Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, is_vista ? 32 : 0 );
2131 RegCloseKey( key );
2132 }
2133 else
2134 {
2135 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
2136 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2137 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2138 check_key_value( key, "Wine\\Winetest", 0, 64 );
2139 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, 64 );
2140 dw = get_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY );
2141 todo_wine ok( dw == 32, "wrong value %u\n", dw );
2142 check_key_value( key, "Wow6432Node\\Wine\\Winetest", 0, 32 );
2143 RegCloseKey( key );
2144
2145 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0,
2146 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2147 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2148 check_key_value( key, "Wine\\Winetest", 0, 32 );
2149 dw = get_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY );
2150 ok( dw == 32 || broken(dw == 64) /* vista */, "wrong value %u\n", dw );
2151 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2152 RegCloseKey( key );
2153 }
2154
2155 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", 0, ptr_size );
2156 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", 0, 32 );
2157 if (ptr_size == 64)
2158 {
2159 /* KEY_WOW64 flags have no effect on 64-bit */
2160 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_64KEY, 64 );
2161 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2162 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2163 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2164 }
2165 else
2166 {
2167 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_64KEY, 64 );
2168 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2169 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2170 check_key_value( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2171 }
2172
2173 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
2174 KEY_ALL_ACCESS, NULL, &key, NULL );
2175 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2176 check_key_value( key, "Wine\\Winetest", 0, 32 );
2177 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2178 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2179 RegCloseKey( key );
2180
2181 if (ptr_size == 32)
2182 {
2183 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
2184 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2185 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2186 dw = get_key_value( key, "Wine\\Winetest", 0 );
2187 ok( dw == (is_vista ? 64 : 32) || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
2188 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2189 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2190 RegCloseKey( key );
2191
2192 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node", 0, NULL, 0,
2193 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2194 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2195 check_key_value( key, "Wine\\Winetest", 0, 32 );
2196 check_key_value( key, "Wine\\Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2197 check_key_value( key, "Wine\\Winetest", KEY_WOW64_32KEY, 32 );
2198 RegCloseKey( key );
2199 }
2200
2201 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2202 KEY_ALL_ACCESS, NULL, &key, NULL );
2203 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2204 check_key_value( key, "Winetest", 0, 32 );
2205 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2206 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2207 RegCloseKey( key );
2208
2209 if (ptr_size == 32)
2210 {
2211 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2212 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2213 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2214 dw = get_key_value( key, "Winetest", 0 );
2215 ok( dw == 32 || (is_vista && dw == 64), "wrong value %u\n", dw );
2216 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2217 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2218 RegCloseKey( key );
2219
2220 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Wine", 0, NULL, 0,
2221 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2222 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2223 check_key_value( key, "Winetest", 0, 32 );
2224 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2225 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2226 RegCloseKey( key );
2227 }
2228
2229 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2230 KEY_ALL_ACCESS, NULL, &key, NULL );
2231 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2232 check_key_value( key, "Winetest", 0, ptr_size );
2233 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : ptr_size );
2234 dw = get_key_value( key, "Winetest", KEY_WOW64_32KEY );
2235 if (ptr_size == 32) ok( dw == 32, "wrong value %u\n", dw );
2236 else todo_wine ok( dw == 32, "wrong value %u\n", dw );
2237 RegCloseKey( key );
2238
2239 if (ptr_size == 32)
2240 {
2241 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2242 KEY_WOW64_64KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2243 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2244 dw = get_key_value( key, "Winetest", 0 );
2245 ok( dw == 64 || broken(dw == 32) /* xp64 */, "wrong value %u\n", dw );
2246 check_key_value( key, "Winetest", KEY_WOW64_64KEY, 64 );
2247 dw = get_key_value( key, "Winetest", KEY_WOW64_32KEY );
2248 todo_wine ok( dw == 32, "wrong value %u\n", dw );
2249 RegCloseKey( key );
2250
2251 err = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine", 0, NULL, 0,
2252 KEY_WOW64_32KEY | KEY_ALL_ACCESS, NULL, &key, NULL );
2253 ok( err == ERROR_SUCCESS, "RegCreateKeyExA failed: %u\n", err );
2254 check_key_value( key, "Winetest", 0, 32 );
2255 check_key_value( key, "Winetest", KEY_WOW64_64KEY, is_vista ? 64 : 32 );
2256 check_key_value( key, "Winetest", KEY_WOW64_32KEY, 32 );
2257 RegCloseKey( key );
2258 }
2259
2260 if (pRegDeleteKeyExA)
2261 {
2262 err = pRegDeleteKeyExA( key32, "", KEY_WOW64_32KEY, 0 );
2263 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2264 err = pRegDeleteKeyExA( key64, "", KEY_WOW64_64KEY, 0 );
2265 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2266 pRegDeleteKeyExA( key64, "", KEY_WOW64_64KEY, 0 );
2267 pRegDeleteKeyExA( root64, "", KEY_WOW64_64KEY, 0 );
2268 }
2269 else
2270 {
2271 err = RegDeleteKeyA( key32, "" );
2272 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2273 err = RegDeleteKeyA( key64, "" );
2274 ok( err == ERROR_SUCCESS, "RegDeleteKey failed: %u\n", err );
2275 RegDeleteKeyA( key64, "" );
2276 RegDeleteKeyA( root64, "" );
2277 }
2278 RegCloseKey( key32 );
2279 RegCloseKey( key64 );
2280 RegCloseKey( root32 );
2281 RegCloseKey( root64 );
2282
2283 /* open key in native bit mode */
2284 err = RegOpenKeyExA(HKEY_CLASSES_ROOT, "Interface", 0, KEY_ALL_ACCESS, &native);
2285 ok(err == ERROR_SUCCESS, "got %i\n", err);
2286
2287 pRegDeleteKeyExA(native, "AWineTest", 0, 0);
2288
2289 /* write subkey in opposite bit mode */
2290 err = RegOpenKeyExA(HKEY_CLASSES_ROOT, "Interface", 0, KEY_ALL_ACCESS | opposite, &op_key);
2291 ok(err == ERROR_SUCCESS, "got %i\n", err);
2292
2293 err = RegCreateKeyExA(op_key, "AWineTest", 0, NULL, 0, KEY_ALL_ACCESS | opposite,
2294 NULL, &key, NULL);
2295 ok(err == ERROR_SUCCESS || err == ERROR_ACCESS_DENIED, "got %i\n", err);
2296 if(err != ERROR_SUCCESS){
2297 win_skip("Can't write to registry\n");
2298 RegCloseKey(op_key);
2299 RegCloseKey(native);
2300 return;
2301 }
2302 RegCloseKey(key);
2303
2304 /* verify subkey is not present in native mode */
2305 err = RegOpenKeyExA(native, "AWineTest", 0, KEY_ALL_ACCESS, &key);
2306 ok(err == ERROR_FILE_NOT_FOUND ||
2307 broken(err == ERROR_SUCCESS), /* before Win7, HKCR is reflected instead of redirected */
2308 "got %i\n", err);
2309
2310 err = pRegDeleteKeyExA(op_key, "AWineTest", opposite, 0);
2311 ok(err == ERROR_SUCCESS, "got %i\n", err);
2312
2313 RegCloseKey(op_key);
2314 RegCloseKey(native);
2315 }
2316
2317 static void test_classesroot(void)
2318 {
2319 HKEY hkey, hklm, hkcr, hkeysub1, hklmsub1, hkcrsub1, hklmsub2, hkcrsub2;
2320 DWORD size = 8;
2321 DWORD type = REG_SZ;
2322 static CHAR buffer[8];
2323 LONG res;
2324
2325 /* create a key in the user's classes */
2326 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", &hkey ))
2327 {
2328 delete_key( hkey );
2329 RegCloseKey( hkey );
2330 }
2331 res = RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2332 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkey, NULL );
2333 if (res == ERROR_ACCESS_DENIED)
2334 {
2335 skip("not enough privileges to add a user class\n");
2336 return;
2337 }
2338 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2339
2340 /* try to open that key in hkcr */
2341 res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2342 KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2343 todo_wine ok(res == ERROR_SUCCESS ||
2344 broken(res == ERROR_FILE_NOT_FOUND /* WinNT */),
2345 "test key not found in hkcr: %d\n", res);
2346 if (res)
2347 {
2348 skip("HKCR key merging not supported\n");
2349 delete_key( hkey );
2350 RegCloseKey( hkey );
2351 return;
2352 }
2353
2354 todo_wine ok(IS_HKCR(hkcr), "hkcr mask not set in %p\n", hkcr);
2355
2356 /* set a value in user's classes */
2357 res = RegSetValueExA(hkey, "val1", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2358 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2359
2360 /* try to find the value in hkcr */
2361 res = RegQueryValueExA(hkcr, "val1", NULL, &type, (LPBYTE)buffer, &size);
2362 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2363 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2364
2365 /* modify the value in hkcr */
2366 res = RegSetValueExA(hkcr, "val1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2367 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2368
2369 /* check if the value is also modified in user's classes */
2370 res = RegQueryValueExA(hkey, "val1", NULL, &type, (LPBYTE)buffer, &size);
2371 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2372 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2373
2374 /* set a value in hkcr */
2375 res = RegSetValueExA(hkcr, "val0", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2376 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2377
2378 /* try to find the value in user's classes */
2379 res = RegQueryValueExA(hkey, "val0", NULL, &type, (LPBYTE)buffer, &size);
2380 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2381 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2382
2383 /* modify the value in user's classes */
2384 res = RegSetValueExA(hkey, "val0", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2385 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2386
2387 /* check if the value is also modified in hkcr */
2388 res = RegQueryValueExA(hkcr, "val0", NULL, &type, (LPBYTE)buffer, &size);
2389 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2390 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2391
2392 /* cleanup */
2393 delete_key( hkey );
2394 delete_key( hkcr );
2395 RegCloseKey( hkey );
2396 RegCloseKey( hkcr );
2397
2398 /* create a key in the hklm classes */
2399 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", &hklm ))
2400 {
2401 delete_key( hklm );
2402 RegCloseKey( hklm );
2403 }
2404 res = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", 0, NULL, REG_OPTION_NON_VOLATILE,
2405 KEY_ALL_ACCESS, NULL, &hklm, NULL );
2406 if (res == ERROR_ACCESS_DENIED)
2407 {
2408 skip("not enough privileges to add a system class\n");
2409 return;
2410 }
2411 ok(!IS_HKCR(hklm), "hkcr mask set in %p\n", hklm);
2412
2413 /* try to open that key in hkcr */
2414 res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2415 KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2416 ok(res == ERROR_SUCCESS,
2417 "test key not found in hkcr: %d\n", res);
2418 ok(IS_HKCR(hkcr), "hkcr mask not set in %p\n", hkcr);
2419 if (res)
2420 {
2421 delete_key( hklm );
2422 RegCloseKey( hklm );
2423 return;
2424 }
2425
2426 /* set a value in hklm classes */
2427 res = RegSetValueExA(hklm, "val2", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2428 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2429
2430 /* try to find the value in hkcr */
2431 res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2432 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2433 ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2434
2435 /* modify the value in hkcr */
2436 res = RegSetValueExA(hkcr, "val2", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2437 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2438
2439 /* check that the value is modified in hklm classes */
2440 res = RegQueryValueExA(hklm, "val2", NULL, &type, (LPBYTE)buffer, &size);
2441 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2442 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2443
2444 if (RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2445 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkey, NULL )) return;
2446 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2447
2448 /* try to open that key in hkcr */
2449 res = RegOpenKeyExA( HKEY_CLASSES_ROOT, "WineTestCls", 0,
2450 KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcr );
2451 ok(res == ERROR_SUCCESS,
2452 "test key not found in hkcr: %d\n", res);
2453 ok(IS_HKCR(hkcr), "hkcr mask not set in %p\n", hkcr);
2454
2455 /* set a value in user's classes */
2456 res = RegSetValueExA(hkey, "val2", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2457 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2458
2459 /* try to find the value in hkcr */
2460 res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2461 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2462 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2463
2464 /* modify the value in hklm */
2465 res = RegSetValueExA(hklm, "val2", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2466 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2467
2468 /* check that the value is not overwritten in hkcr or user's classes */
2469 res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2470 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2471 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2472 res = RegQueryValueExA(hkey, "val2", NULL, &type, (LPBYTE)buffer, &size);
2473 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2474 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2475
2476 /* modify the value in hkcr */
2477 res = RegSetValueExA(hkcr, "val2", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2478 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2479
2480 /* check that the value is overwritten in hklm and user's classes */
2481 res = RegQueryValueExA(hkcr, "val2", NULL, &type, (LPBYTE)buffer, &size);
2482 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2483 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2484 res = RegQueryValueExA(hkey, "val2", NULL, &type, (LPBYTE)buffer, &size);
2485 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2486 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2487
2488 /* create a subkey in hklm */
2489 if (RegCreateKeyExA( hklm, "subkey1", 0, NULL, 0,
2490 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hklmsub1, NULL )) return;
2491 ok(!IS_HKCR(hklmsub1), "hkcr mask set in %p\n", hklmsub1);
2492 /* try to open that subkey in hkcr */
2493 res = RegOpenKeyExA( hkcr, "subkey1", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hkcrsub1 );
2494 ok(res == ERROR_SUCCESS, "test key not found in hkcr: %d\n", res);
2495 ok(IS_HKCR(hkcrsub1), "hkcr mask not set in %p\n", hkcrsub1);
2496
2497 /* set a value in hklm classes */
2498 res = RegSetValueExA(hklmsub1, "subval1", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2499 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2500
2501 /* try to find the value in hkcr */
2502 res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2503 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2504 ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2505
2506 /* modify the value in hkcr */
2507 res = RegSetValueExA(hkcrsub1, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2508 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2509
2510 /* check that the value is modified in hklm classes */
2511 res = RegQueryValueExA(hklmsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2512 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2513 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2514
2515 /* create a subkey in user's classes */
2516 if (RegCreateKeyExA( hkey, "subkey1", 0, NULL, 0,
2517 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkeysub1, NULL )) return;
2518 ok(!IS_HKCR(hkeysub1), "hkcr mask set in %p\n", hkeysub1);
2519
2520 /* set a value in user's classes */
2521 res = RegSetValueExA(hkeysub1, "subval1", 0, REG_SZ, (const BYTE *)"user", sizeof("user"));
2522 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2523
2524 /* try to find the value in hkcr */
2525 res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2526 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2527 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2528
2529 /* modify the value in hklm */
2530 res = RegSetValueExA(hklmsub1, "subval1", 0, REG_SZ, (const BYTE *)"hklm", sizeof("hklm"));
2531 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2532
2533 /* check that the value is not overwritten in hkcr or user's classes */
2534 res = RegQueryValueExA(hkcrsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2535 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2536 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2537 res = RegQueryValueExA(hkeysub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2538 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2539 ok(!strcmp( buffer, "user" ), "value set to '%s'\n", buffer );
2540
2541 /* modify the value in hkcr */
2542 res = RegSetValueExA(hkcrsub1, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2543 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2544
2545 /* check that the value is not overwritten in hklm, but in user's classes */
2546 res = RegQueryValueExA(hklmsub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2547 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2548 ok(!strcmp( buffer, "hklm" ), "value set to '%s'\n", buffer );
2549 res = RegQueryValueExA(hkeysub1, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2550 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%x\n", res, GetLastError());
2551 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2552
2553 /* new subkey in hkcr */
2554 if (RegCreateKeyExA( hkcr, "subkey2", 0, NULL, 0,
2555 KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &hkcrsub2, NULL )) return;
2556 ok(IS_HKCR(hkcrsub2), "hkcr mask not set in %p\n", hkcrsub2);
2557 res = RegSetValueExA(hkcrsub2, "subval1", 0, REG_SZ, (const BYTE *)"hkcr", sizeof("hkcr"));
2558 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%x\n", res, GetLastError());
2559
2560 /* try to open that new subkey in user's classes and hklm */
2561 res = RegOpenKeyExA( hkey, "subkey2", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hklmsub2 );
2562 ok(res != ERROR_SUCCESS, "test key found in user's classes: %d\n", res);
2563 hklmsub2 = 0;
2564 res = RegOpenKeyExA( hklm, "subkey2", 0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hklmsub2 );
2565 ok(res == ERROR_SUCCESS, "test key not found in hklm: %d\n", res);
2566 ok(!IS_HKCR(hklmsub2), "hkcr mask set in %p\n", hklmsub2);
2567
2568 /* check that the value is present in hklm */
2569 res = RegQueryValueExA(hklmsub2, "subval1", NULL, &type, (LPBYTE)buffer, &size);
2570 ok(res == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", res);
2571 ok(!strcmp( buffer, "hkcr" ), "value set to '%s'\n", buffer );
2572
2573 /* cleanup */
2574 RegCloseKey( hkeysub1 );
2575 RegCloseKey( hklmsub1 );
2576
2577 /* delete subkey1 from hkcr (should point at user's classes) */
2578 res = RegDeleteKeyA(hkcr, "subkey1");
2579 ok(res == ERROR_SUCCESS, "RegDeleteKey failed: %d\n", res);
2580
2581 /* confirm key was removed in hkey but not hklm */
2582 res = RegOpenKeyExA(hkey, "subkey1", 0, KEY_READ, &hkeysub1);
2583 ok(res == ERROR_FILE_NOT_FOUND, "test key found in user's classes: %d\n", res);
2584 res = RegOpenKeyExA(hklm, "subkey1", 0, KEY_READ, &hklmsub1);
2585 ok(res == ERROR_SUCCESS, "test key not found in hklm: %d\n", res);
2586 ok(!IS_HKCR(hklmsub1), "hkcr mask set in %p\n", hklmsub1);
2587
2588 /* delete subkey1 from hkcr again (which should now point at hklm) */
2589 res = RegDeleteKeyA(hkcr, "subkey1");
2590 ok(res == ERROR_SUCCESS, "RegDeleteKey failed: %d\n", res);
2591
2592 /* confirm hkey was removed in hklm */
2593 RegCloseKey( hklmsub1 );
2594 res = RegOpenKeyExA(hklm, "subkey1", 0, KEY_READ, &hklmsub1);
2595 ok(res == ERROR_FILE_NOT_FOUND, "test key found in hklm: %d\n", res);
2596
2597 /* final cleanup */
2598 delete_key( hkey );
2599 delete_key( hklm );
2600 delete_key( hkcr );
2601 delete_key( hkeysub1 );
2602 delete_key( hklmsub1 );
2603 delete_key( hkcrsub1 );
2604 delete_key( hklmsub2 );
2605 delete_key( hkcrsub2 );
2606 RegCloseKey( hkey );
2607 RegCloseKey( hklm );
2608 RegCloseKey( hkcr );
2609 RegCloseKey( hkeysub1 );
2610 RegCloseKey( hklmsub1 );
2611 RegCloseKey( hkcrsub1 );
2612 RegCloseKey( hklmsub2 );
2613 RegCloseKey( hkcrsub2 );
2614 }
2615
2616 static void test_classesroot_enum(void)
2617 {
2618 HKEY hkcu=0, hklm=0, hkcr=0, hkcusub[2]={0}, hklmsub[2]={0};
2619 DWORD size;
2620 static CHAR buffer[2];
2621 LONG res;
2622
2623 /* prepare initial testing env in HKCU */
2624 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", &hkcu ))
2625 {
2626 delete_key( hkcu );
2627 RegCloseKey( hkcu );
2628 }
2629 res = RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2630 KEY_SET_VALUE|KEY_ENUMERATE_SUB_KEYS, NULL, &hkcu, NULL );
2631
2632 if (res != ERROR_SUCCESS)
2633 {
2634 skip("failed to add a user class\n");
2635 return;
2636 }
2637
2638 res = RegOpenKeyA( HKEY_CLASSES_ROOT, "WineTestCls", &hkcr );
2639 todo_wine ok(res == ERROR_SUCCESS ||
2640 broken(res == ERROR_FILE_NOT_FOUND /* WinNT */),
2641 "test key not found in hkcr: %d\n", res);
2642 if (res)
2643 {
2644 skip("HKCR key merging not supported\n");
2645 delete_key( hkcu );
2646 RegCloseKey( hkcu );
2647 return;
2648 }
2649
2650 res = RegSetValueExA( hkcu, "X", 0, REG_SZ, (const BYTE *) "AA", 3 );
2651 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", res);
2652 res = RegSetValueExA( hkcu, "Y", 0, REG_SZ, (const BYTE *) "B", 2 );
2653 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", res);
2654 res = RegCreateKeyA( hkcu, "A", &hkcusub[0] );
2655 ok(res == ERROR_SUCCESS, "RegCreateKeyA failed: %d\n", res);
2656 res = RegCreateKeyA( hkcu, "B", &hkcusub[1] );
2657 ok(res == ERROR_SUCCESS, "RegCreateKeyA failed: %d\n", res);
2658
2659 /* test on values in HKCU */
2660 size = sizeof(buffer);
2661 res = RegEnumValueA( hkcr, 0, buffer, &size, NULL, NULL, NULL, NULL );
2662 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2663 ok(!strcmp( buffer, "X" ), "expected 'X', got '%s'\n", buffer);
2664 size = sizeof(buffer);
2665 res = RegEnumValueA( hkcr, 1, buffer, &size, NULL, NULL, NULL, NULL );
2666 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2667 ok(!strcmp( buffer, "Y" ), "expected 'Y', got '%s'\n", buffer);
2668 size = sizeof(buffer);
2669 res = RegEnumValueA( hkcr, 2, buffer, &size, NULL, NULL, NULL, NULL );
2670 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2671
2672 res = RegEnumKeyA( hkcr, 0, buffer, size );
2673 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2674 ok(!strcmp( buffer, "A" ), "expected 'A', got '%s'\n", buffer);
2675 res = RegEnumKeyA( hkcr, 1, buffer, size );
2676 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2677 ok(!strcmp( buffer, "B" ), "expected 'B', got '%s'\n", buffer);
2678 res = RegEnumKeyA( hkcr, 2, buffer, size );
2679 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2680
2681 /* prepare test env in HKLM */
2682 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", &hklm ))
2683 {
2684 delete_key( hklm );
2685 RegCloseKey( hklm );
2686 }
2687
2688 res = RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Classes\\WineTestCls", 0, NULL, 0,
2689 KEY_SET_VALUE|KEY_ENUMERATE_SUB_KEYS, NULL, &hklm, NULL );
2690
2691 if (res == ERROR_ACCESS_DENIED)
2692 {
2693 RegCloseKey( hkcusub[0] );
2694 RegCloseKey( hkcusub[1] );
2695 delete_key( hkcu );
2696 RegCloseKey( hkcu );
2697 RegCloseKey( hkcr );
2698 skip("not enough privileges to add a system class\n");
2699 return;
2700 }
2701
2702 res = RegSetValueExA( hklm, "X", 0, REG_SZ, (const BYTE *) "AB", 3 );
2703 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", res);
2704 res = RegSetValueExA( hklm, "Z", 0, REG_SZ, (const BYTE *) "C", 2 );
2705 ok(res == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", res);
2706 res = RegCreateKeyA( hklm, "A", &hklmsub[0] );
2707 ok(res == ERROR_SUCCESS, "RegCreateKeyA failed: %d\n", res);
2708 res = RegCreateKeyA( hklm, "C", &hklmsub[1] );
2709 ok(res == ERROR_SUCCESS, "RegCreateKeyA failed: %d\n", res);
2710
2711 /* test on values/keys in both HKCU and HKLM */
2712 size = sizeof(buffer);
2713 res = RegEnumValueA( hkcr, 0, buffer, &size, NULL, NULL, NULL, NULL );
2714 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2715 ok(!strcmp( buffer, "X" ), "expected 'X', got '%s'\n", buffer);
2716 size = sizeof(buffer);
2717 res = RegEnumValueA( hkcr, 1, buffer, &size, NULL, NULL, NULL, NULL );
2718 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2719 ok(!strcmp( buffer, "Y" ), "expected 'Y', got '%s'\n", buffer);
2720 size = sizeof(buffer);
2721 res = RegEnumValueA( hkcr, 2, buffer, &size, NULL, NULL, NULL, NULL );
2722 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2723 ok(!strcmp( buffer, "Z" ), "expected 'Z', got '%s'\n", buffer);
2724 size = sizeof(buffer);
2725 res = RegEnumValueA( hkcr, 3, buffer, &size, NULL, NULL, NULL, NULL );
2726 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2727
2728 res = RegEnumKeyA( hkcr, 0, buffer, size );
2729 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2730 ok(!strcmp( buffer, "A" ), "expected 'A', got '%s'\n", buffer);
2731 res = RegEnumKeyA( hkcr, 1, buffer, size );
2732 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2733 ok(!strcmp( buffer, "B" ), "expected 'B', got '%s'\n", buffer);
2734 res = RegEnumKeyA( hkcr, 2, buffer, size );
2735 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2736 ok(!strcmp( buffer, "C" ), "expected 'C', got '%s'\n", buffer);
2737 res = RegEnumKeyA( hkcr, 3, buffer, size );
2738 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2739
2740 /* delete values/keys from HKCU to test only on HKLM */
2741 RegCloseKey( hkcusub[0] );
2742 RegCloseKey( hkcusub[1] );
2743 delete_key( hkcu );
2744 RegCloseKey( hkcu );
2745
2746 size = sizeof(buffer);
2747 res = RegEnumValueA( hkcr, 0, buffer, &size, NULL, NULL, NULL, NULL );
2748 ok(res == ERROR_KEY_DELETED ||
2749 res == ERROR_NO_SYSTEM_RESOURCES, /* Windows XP */
2750 "expected ERROR_KEY_DELETED, got %d\n", res);
2751 size = sizeof(buffer);
2752 res = RegEnumKeyA( hkcr, 0, buffer, size );
2753 ok(res == ERROR_KEY_DELETED ||
2754 res == ERROR_NO_SYSTEM_RESOURCES, /* Windows XP */
2755 "expected ERROR_KEY_DELETED, got %d\n", res);
2756
2757 /* reopen HKCR handle */
2758 RegCloseKey( hkcr );
2759 res = RegOpenKeyA( HKEY_CLASSES_ROOT, "WineTestCls", &hkcr );
2760 ok(res == ERROR_SUCCESS, "test key not found in hkcr: %d\n", res);
2761 if (res) goto cleanup;
2762
2763 /* test on values/keys in HKLM */
2764 size = sizeof(buffer);
2765 res = RegEnumValueA( hkcr, 0, buffer, &size, NULL, NULL, NULL, NULL );
2766 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2767 ok(!strcmp( buffer, "X" ), "expected 'X', got '%s'\n", buffer);
2768 size = sizeof(buffer);
2769 res = RegEnumValueA( hkcr, 1, buffer, &size, NULL, NULL, NULL, NULL );
2770 ok(res == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", res );
2771 ok(!strcmp( buffer, "Z" ), "expected 'Z', got '%s'\n", buffer);
2772 size = sizeof(buffer);
2773 res = RegEnumValueA( hkcr, 2, buffer, &size, NULL, NULL, NULL, NULL );
2774 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2775
2776 res = RegEnumKeyA( hkcr, 0, buffer, size );
2777 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2778 ok(!strcmp( buffer, "A" ), "expected 'A', got '%s'\n", buffer);
2779 res = RegEnumKeyA( hkcr, 1, buffer, size );
2780 ok(res == ERROR_SUCCESS, "RegEnumKey failed: %d\n", res );
2781 ok(!strcmp( buffer, "C" ), "expected 'C', got '%s'\n", buffer);
2782 res = RegEnumKeyA( hkcr, 2, buffer, size );
2783 ok(res == ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n", res );
2784
2785 cleanup:
2786 RegCloseKey( hklmsub[0] );
2787 RegCloseKey( hklmsub[1] );
2788 delete_key( hklm );
2789 RegCloseKey( hklm );
2790 RegCloseKey( hkcr );
2791 }
2792
2793 static void test_classesroot_mask(void)
2794 {
2795 HKEY hkey;
2796 LSTATUS res;
2797
2798 res = RegOpenKeyA( HKEY_CLASSES_ROOT, "CLSID", &hkey );
2799 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2800 todo_wine ok(IS_HKCR(hkey) || broken(!IS_HKCR(hkey)) /* WinNT */,
2801 "hkcr mask not set in %p\n", hkey);
2802 RegCloseKey( hkey );
2803
2804 res = RegOpenKeyA( HKEY_CURRENT_USER, "Software", &hkey );
2805 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2806 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2807 RegCloseKey( hkey );
2808
2809 res = RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software", &hkey );
2810 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2811 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2812 RegCloseKey( hkey );
2813
2814 res = RegOpenKeyA( HKEY_USERS, ".Default", &hkey );
2815 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2816 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2817 RegCloseKey( hkey );
2818
2819 res = RegOpenKeyA( HKEY_CURRENT_CONFIG, "Software", &hkey );
2820 ok(res == ERROR_SUCCESS, "RegOpenKeyA failed: %d\n", res);
2821 ok(!IS_HKCR(hkey), "hkcr mask set in %p\n", hkey);
2822 RegCloseKey( hkey );
2823 }
2824
2825 static void test_deleted_key(void)
2826 {
2827 HKEY hkey, hkey2;
2828 char value[20];
2829 DWORD val_count, type;
2830 LONG res;
2831
2832 /* Open the test key, then delete it while it's open */
2833 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey );
2834
2835 delete_key( hkey_main );
2836
2837 val_count = sizeof(value);
2838 type = 0;
2839 res = RegEnumValueA( hkey, 0, value, &val_count, NULL, &type, 0, 0 );
2840 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2841
2842 res = RegEnumKeyA( hkey, 0, value, sizeof(value) );
2843 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2844
2845 val_count = sizeof(value);
2846 type = 0;
2847 res = RegQueryValueExA( hkey, "test", NULL, &type, (BYTE *)value, &val_count );
2848 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2849
2850 res = RegSetValueExA( hkey, "test", 0, REG_SZ, (const BYTE*)"value", 6);
2851 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2852
2853 res = RegOpenKeyA( hkey, "test", &hkey2 );
2854 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2855 if (res == 0)
2856 RegCloseKey( hkey2 );
2857
2858 res = RegCreateKeyA( hkey, "test", &hkey2 );
2859 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2860 if (res == 0)
2861 RegCloseKey( hkey2 );
2862
2863 res = RegFlushKey( hkey );
2864 ok(res == ERROR_KEY_DELETED, "expect ERROR_KEY_DELETED, got %i\n", res);
2865
2866 RegCloseKey( hkey );
2867
2868 setup_main_key();
2869 }
2870
2871 static void test_delete_value(void)
2872 {
2873 LONG res;
2874 char longname[401];
2875
2876 res = RegSetValueExA( hkey_main, "test", 0, REG_SZ, (const BYTE*)"value", 6 );
2877 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2878
2879 res = RegQueryValueExA( hkey_main, "test", NULL, NULL, NULL, NULL);
2880 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2881
2882 res = RegDeleteValueA( hkey_main, "test" );
2883 ok(res == ERROR_SUCCESS, "expect ERROR_SUCCESS, got %i\n", res);
2884
2885 res = RegQueryValueExA( hkey_main, "test", NULL, NULL, NULL, NULL);
2886 ok(res == ERROR_FILE_NOT_FOUND, "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2887
2888 res = RegDeleteValueA( hkey_main, "test" );
2889 ok(res == ERROR_FILE_NOT_FOUND, "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2890
2891 memset(longname, 'a', 400);
2892 longname[400] = 0;
2893 res = RegDeleteValueA( hkey_main, longname );
2894 ok(res == ERROR_FILE_NOT_FOUND || broken(res == ERROR_MORE_DATA), /* nt4, win2k */
2895 "expect ERROR_FILE_NOT_FOUND, got %i\n", res);
2896 }
2897
2898 static void test_delete_key_value(void)
2899 {
2900 HKEY subkey;
2901 LONG ret;
2902
2903 if (!pRegDeleteKeyValueA)
2904 {
2905 win_skip("RegDeleteKeyValue is not available.\n");
2906 return;
2907 }
2908
2909 ret = pRegDeleteKeyValueA(NULL, NULL, NULL);
2910 ok(ret == ERROR_INVALID_HANDLE, "got %d\n", ret);
2911
2912 ret = pRegDeleteKeyValueA(hkey_main, NULL, NULL);
2913 ok(ret == ERROR_FILE_NOT_FOUND, "got %d\n", ret);
2914
2915 ret = RegSetValueExA(hkey_main, "test", 0, REG_SZ, (const BYTE*)"value", 6);
2916 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
2917
2918 ret = RegQueryValueExA(hkey_main, "test", NULL, NULL, NULL, NULL);
2919 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
2920
2921 /* NULL subkey name means delete from open key */
2922 ret = pRegDeleteKeyValueA(hkey_main, NULL, "test");
2923 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
2924
2925 ret = RegQueryValueExA(hkey_main, "test", NULL, NULL, NULL, NULL);
2926 ok(ret == ERROR_FILE_NOT_FOUND, "got %d\n", ret);
2927
2928 /* now with real subkey */
2929 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &subkey, NULL);
2930 ok(!ret, "failed with error %d\n", ret);
2931
2932 ret = RegSetValueExA(subkey, "test", 0, REG_SZ, (const BYTE*)"value", 6);
2933 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
2934
2935 ret = RegQueryValueExA(subkey, "test", NULL, NULL, NULL, NULL);
2936 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
2937
2938 ret = pRegDeleteKeyValueA(hkey_main, "Subkey1", "test");
2939 ok(ret == ERROR_SUCCESS, "got %d\n", ret);
2940
2941 ret = RegQueryValueExA(subkey, "test", NULL, NULL, NULL, NULL);
2942 ok(ret == ERROR_FILE_NOT_FOUND, "got %d\n", ret);
2943
2944 RegDeleteKeyA(subkey, "");
2945 RegCloseKey(subkey);
2946 }
2947
2948 START_TEST(registry)
2949 {
2950 /* Load pointers for functions that are not available in all Windows versions */
2951 InitFunctionPtrs();
2952
2953 setup_main_key();
2954 check_user_privs();
2955 test_set_value();
2956 create_test_entries();
2957 test_enum_value();
2958 test_query_value_ex();
2959 test_get_value();
2960 test_reg_open_key();
2961 test_reg_create_key();
2962 test_reg_close_key();
2963 test_reg_delete_key();
2964 test_reg_query_value();
2965 test_string_termination();
2966 test_symlinks();
2967 test_redirection();
2968 test_classesroot();
2969 test_classesroot_enum();
2970 test_classesroot_mask();
2971
2972 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
2973 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
2974 set_privileges(SE_RESTORE_NAME, TRUE))
2975 {
2976 test_reg_save_key();
2977 test_reg_load_key();
2978 test_reg_unload_key();
2979
2980 set_privileges(SE_BACKUP_NAME, FALSE);
2981 set_privileges(SE_RESTORE_NAME, FALSE);
2982 }
2983
2984 test_reg_delete_tree();
2985 test_rw_order();
2986 test_deleted_key();
2987 test_delete_value();
2988 test_delete_key_value();
2989
2990 /* cleanup */
2991 delete_key( hkey_main );
2992
2993 test_regconnectregistry();
2994 }