2 * Unit tests for registry functions
4 * Copyright (c) 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/test.h"
30 static HKEY hkey_main;
32 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
33 static const char * sTestpath2 = "%FOO%\\subdir1";
35 /* delete key and all its subkeys */
36 static DWORD delete_key( HKEY hkey )
41 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
44 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
46 ret = delete_key( tmp );
51 if (ret != ERROR_NO_MORE_ITEMS) return ret;
52 RegDeleteKeyA( hkey, "" );
56 static void setup_main_key(void)
58 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
60 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
63 static void create_test_entries(void)
65 DWORD qw[2] = { 0x12345678, 0x87654321 };
67 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
68 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
70 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (LPBYTE)sTestpath1, strlen(sTestpath1)+1),
71 "RegSetValueExA failed\n");
72 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (LPBYTE)sTestpath1, strlen(sTestpath1)+1),
73 "RegSetValueExA failed\n");
74 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (LPBYTE)sTestpath2, strlen(sTestpath2)+1),
75 "RegSetValueExA failed\n");
76 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (LPBYTE)qw, 4),
77 "RegSetValueExA failed\n");
78 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (LPBYTE)qw, 4),
79 "RegSetValueExA failed\n");
80 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (LPBYTE)qw, 8),
81 "RegSetValueExA failed\n");
84 static void test_enum_value(void)
88 char value[20], data[20];
89 WCHAR valueW[20], dataW[20];
90 DWORD val_count, data_count, type;
91 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
92 static const WCHAR testW[] = {'T','e','s','t',0};
93 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
95 /* create the working key for new 'Test' value */
96 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
97 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res);
99 /* check NULL data with zero length */
100 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
101 if (GetVersion() & 0x80000000)
102 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %ld\n", res );
104 ok( !res, "RegSetValueExA returned %ld\n", res );
105 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
106 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
107 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
108 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
110 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
111 ok( res == 0, "RegSetValueExA failed error %ld\n", res );
113 /* overflow both name and data */
117 strcpy( value, "xxxxxxxxxx" );
118 strcpy( data, "xxxxxxxxxx" );
119 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
120 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
121 ok( val_count == 2, "val_count set to %ld\n", val_count );
122 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
123 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
124 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
125 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
131 strcpy( value, "xxxxxxxxxx" );
132 strcpy( data, "xxxxxxxxxx" );
133 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
134 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
135 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
136 ok( val_count == 2 || val_count == 3, "val_count set to %ld\n", val_count );
137 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
138 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
139 /* v5.1.2600.0 (XP Home and Proffesional) does not touch value or data in this case */
140 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
141 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
142 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
143 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
145 /* overflow empty name */
149 strcpy( value, "xxxxxxxxxx" );
150 strcpy( data, "xxxxxxxxxx" );
151 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
152 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
153 ok( val_count == 0, "val_count set to %ld\n", val_count );
154 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
155 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
156 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
157 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
158 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
159 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
165 strcpy( value, "xxxxxxxxxx" );
166 strcpy( data, "xxxxxxxxxx" );
167 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
168 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
169 ok( val_count == 20, "val_count set to %ld\n", val_count );
170 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
171 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
172 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
173 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
179 strcpy( value, "xxxxxxxxxx" );
180 strcpy( data, "xxxxxxxxxx" );
181 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
182 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
183 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
184 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
185 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
186 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
187 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
192 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
193 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
195 ok( res == 0, "RegSetValueExW failed error %ld\n", res );
197 /* overflow both name and data */
201 memcpy( valueW, xxxW, sizeof(xxxW) );
202 memcpy( dataW, xxxW, sizeof(xxxW) );
203 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
204 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
205 ok( val_count == 2, "val_count set to %ld\n", val_count );
206 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
207 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
208 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
209 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
215 memcpy( valueW, xxxW, sizeof(xxxW) );
216 memcpy( dataW, xxxW, sizeof(xxxW) );
217 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
218 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
219 ok( val_count == 3, "val_count set to %ld\n", val_count );
220 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
221 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
222 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
223 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
229 memcpy( valueW, xxxW, sizeof(xxxW) );
230 memcpy( dataW, xxxW, sizeof(xxxW) );
231 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
232 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
233 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
234 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
235 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
236 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
237 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
243 memcpy( valueW, xxxW, sizeof(xxxW) );
244 memcpy( dataW, xxxW, sizeof(xxxW) );
245 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
246 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
247 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
248 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
249 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
250 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
251 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
254 static void test_query_value_ex(void)
260 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
261 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
262 ok(size == strlen(sTestpath1) + 1, "(%ld,%ld)\n", (DWORD)strlen(sTestpath1) + 1, size);
263 ok(type == REG_SZ, "type %ld is not REG_SZ\n", type);
266 static void test_get_value(void)
269 DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
276 CHAR expanded[] = "bar\\subdir1";
278 /* This function was introduced with Windows 2003 SP1 */
279 hadvapi32 = LoadLibraryA("advapi32.dll");
282 ok(0, "error=%ld\n", GetLastError());
285 pRegGetValueA = (PVOID)GetProcAddress(hadvapi32, "RegGetValueA");
289 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
290 size = type = dw = 0xdeadbeef;
291 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
292 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
293 ok(size == 4, "size=%ld\n", size);
294 ok(type == REG_DWORD, "type=%ld\n", type);
295 ok(dw == 0x12345678, "dw=%ld\n", dw);
297 /* Query by subkey-name */
298 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
299 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
301 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
302 size = type = dw = 0xdeadbeef;
303 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
304 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
305 /* Although the function failed all values are retrieved */
306 ok(size == 4, "size=%ld\n", size);
307 ok(type == REG_DWORD, "type=%ld\n", type);
308 ok(dw == 0x12345678, "dw=%ld\n", dw);
310 /* Test RRF_ZEROONFAILURE */
311 type = dw = 0xdeadbeef; size = 4;
312 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
313 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
314 /* Again all values are retrieved ... */
315 ok(size == 4, "size=%ld\n", size);
316 ok(type == REG_DWORD, "type=%ld\n", type);
317 /* ... except the buffer, which is zeroed out */
318 ok(dw == 0, "dw=%ld\n", dw);
320 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
321 size = type = dw = 0xdeadbeef;
322 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
323 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
324 ok(size == 4, "size=%ld\n", size);
325 ok(type == REG_DWORD, "type=%ld\n", type);
326 ok(dw == 0x12345678, "dw=%ld\n", dw);
328 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
329 size = type = dw = 0xdeadbeef;
330 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
331 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
332 ok(size == 4, "size=%ld\n", size);
333 ok(type == REG_BINARY, "type=%ld\n", type);
334 ok(dw == 0x12345678, "dw=%ld\n", dw);
336 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
337 qw[0] = qw[1] = size = type = 0xdeadbeef;
338 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
339 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%ld\n", ret);
340 ok(size == 8, "size=%ld\n", size);
341 ok(type == REG_BINARY, "type=%ld\n", type);
342 ok(qw[0] == 0x12345678 &&
343 qw[1] == 0x87654321, "qw={%ld,%ld}\n", qw[0], qw[1]);
345 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
346 type = dw = 0xdeadbeef; size = 4;
347 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
348 ok(ret == ERROR_MORE_DATA, "ret=%ld\n", ret);
349 ok(dw == 0xdeadbeef, "dw=%ld\n", dw);
350 ok(size == 8, "size=%ld\n", size);
352 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
353 qw[0] = qw[1] = size = type = 0xdeadbeef;
354 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
355 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
356 ok(size == 8, "size=%ld\n", size);
357 ok(type == REG_BINARY, "type=%ld\n", type);
358 ok(qw[0] == 0x12345678 &&
359 qw[1] == 0x87654321, "qw={%ld,%ld}\n", qw[0], qw[1]);
361 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
362 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
363 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
364 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
365 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
366 ok(type == REG_SZ, "type=%ld\n", type);
367 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
369 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
370 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
371 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
372 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
373 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
374 ok(type == REG_SZ, "type=%ld\n", type);
375 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
377 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
378 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
379 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
380 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
381 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
382 ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1),
383 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%ld\n", strlen(expanded), strlen(sTestpath1), size);
384 ok(type == REG_SZ, "type=%ld\n", type);
385 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
387 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
388 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
389 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
390 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
391 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
392 ok(type == REG_EXPAND_SZ, "type=%ld\n", type);
393 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
395 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
396 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
397 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
399 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
400 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
401 ok(ret == ERROR_INVALID_PARAMETER, "ret=%ld\n", ret);
404 static void test_reg_open_key(void)
407 HKEY hkResult = NULL;
408 HKEY hkPreserve = NULL;
410 /* successful open */
411 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
412 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
413 ok(hkResult != NULL, "expected hkResult != NULL\n");
414 hkPreserve = hkResult;
416 /* these tests fail on Win9x, but we want to be compatible with NT, so
417 * run them if we can */
418 if (!(GetVersion() & 0x80000000))
420 /* open same key twice */
421 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
422 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
423 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
424 ok(hkResult != NULL, "hkResult != NULL\n");
425 RegCloseKey(hkResult);
427 /* open nonexistent key
428 * check that hkResult is set to NULL
430 hkResult = hkPreserve;
431 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
432 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
433 ok(hkResult == NULL, "expected hkResult == NULL\n");
435 /* open the same nonexistent key again to make sure the key wasn't created */
436 hkResult = hkPreserve;
437 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
438 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
439 ok(hkResult == NULL, "expected hkResult == NULL\n");
441 /* send in NULL lpSubKey
442 * check that hkResult receives the value of hKey
444 hkResult = hkPreserve;
445 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
446 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
447 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
449 /* send empty-string in lpSubKey */
450 hkResult = hkPreserve;
451 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
452 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
453 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
455 /* send in NULL lpSubKey and NULL hKey
456 * hkResult is set to NULL
458 hkResult = hkPreserve;
459 ret = RegOpenKeyA(NULL, NULL, &hkResult);
460 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
461 ok(hkResult == NULL, "expected hkResult == NULL\n");
464 /* only send NULL hKey
465 * the value of hkResult remains unchanged
467 hkResult = hkPreserve;
468 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
469 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
470 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
471 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
472 RegCloseKey(hkResult);
474 /* send in NULL hkResult */
475 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
476 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
478 /* beginning backslash character */
479 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
480 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
481 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
482 , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %ld\n", ret);
485 static void test_reg_create_key(void)
489 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
490 ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
491 /* should succeed: all versions of Windows ignore the access rights
492 * to the parent handle */
493 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
494 ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
497 RegDeleteKey(hkey2, NULL);
498 RegDeleteKey(hkey1, NULL);
500 /* beginning backslash character */
501 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
502 if (!(GetVersion() & 0x80000000))
503 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %ld\n", ret);
505 ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
506 RegDeleteKey(hkey1, NULL);
510 static void test_reg_close_key(void)
515 /* successfully close key
516 * hkHandle remains changed after call to RegCloseKey
518 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
519 ret = RegCloseKey(hkHandle);
520 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
522 /* try to close the key twice */
523 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
524 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
525 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %ld\n", ret);
527 /* try to close a NULL handle */
528 ret = RegCloseKey(NULL);
529 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
530 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
533 static void test_reg_delete_key(void)
537 ret = RegDeleteKey(hkey_main, NULL);
538 ok(ret == ERROR_INVALID_PARAMETER ||
539 ret == ERROR_ACCESS_DENIED ||
540 ret == ERROR_BADKEY, /* Win95 */
544 static void test_reg_save_key(void)
548 ret = RegSaveKey(hkey_main, "saved_key", NULL);
549 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
552 static void test_reg_load_key(void)
557 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
558 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
560 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
561 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
563 RegCloseKey(hkHandle);
566 static void test_reg_unload_key(void)
570 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
571 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
573 DeleteFile("saved_key");
574 DeleteFile("saved_key.LOG");
577 static BOOL set_privileges(LPCSTR privilege, BOOL set)
583 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
586 if(!LookupPrivilegeValue(NULL, privilege, &luid))
592 tp.PrivilegeCount = 1;
593 tp.Privileges[0].Luid = luid;
596 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
598 tp.Privileges[0].Attributes = 0;
600 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
601 if (GetLastError() != ERROR_SUCCESS)
611 /* tests that show that RegConnectRegistry and
612 OpenSCManager accept computer names without the
613 \\ prefix (what MSDN says). */
614 static void test_regconnectregistry( void)
616 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
617 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
618 DWORD len = sizeof(compName) ;
625 ret = GetComputerNameA(compName, &len);
626 ok( ret, "GetComputerName failed err = %ld\n", GetLastError());
629 lstrcpyA(netwName, "\\\\");
630 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
632 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
633 ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %ld\n", retl);
634 if( !retl) RegCloseKey( hkey);
636 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
637 ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %ld\n", retl);
638 if( !retl) RegCloseKey( hkey);
640 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
641 GLE = GetLastError();
642 ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED,
643 "OpenSCManagerA failed err = %ld\n", GLE);
644 CloseServiceHandle( schnd);
646 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
647 GLE = GetLastError();
648 ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED,
649 "OpenSCManagerA failed err = %ld\n", GLE);
650 CloseServiceHandle( schnd);
657 create_test_entries();
659 test_query_value_ex();
662 test_reg_create_key();
663 test_reg_close_key();
664 test_reg_delete_key();
666 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
667 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
668 set_privileges(SE_RESTORE_NAME, TRUE))
672 test_reg_unload_key();
674 set_privileges(SE_BACKUP_NAME, FALSE);
675 set_privileges(SE_RESTORE_NAME, FALSE);
679 delete_key( hkey_main );
681 test_regconnectregistry();