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;
33 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
34 static const char * sTestpath2 = "%FOO%\\subdir1";
36 /* delete key and all its subkeys */
37 static DWORD delete_key( HKEY hkey )
42 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
45 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
47 ret = delete_key( tmp );
52 if (ret != ERROR_NO_MORE_ITEMS) return ret;
53 RegDeleteKeyA( hkey, "" );
57 static void setup_main_key(void)
59 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
61 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
64 static void test_hkey_main_Value_A(LPCSTR name, LPCSTR string)
66 DWORD ret, type, cbData;
67 DWORD str_byte_len, full_byte_len;
69 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
71 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %ld, GLE=%ld\n", ret, GLE);
72 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
74 str_byte_len = lstrlenA(string) + 1;
75 full_byte_len = sizeof(string);
76 ok(type == REG_SZ, "RegQueryValueExA returned type %ld\n", type);
77 ok(cbData == full_byte_len || cbData == str_byte_len,
78 "cbData=%ld instead of %ld or %ld\n", cbData, full_byte_len, str_byte_len);
81 static void test_hkey_main_Value_W(LPCWSTR name, LPCWSTR string)
83 DWORD ret, type, cbData;
84 DWORD str_byte_len, full_byte_len;
86 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
88 ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %ld, GLE=%ld\n", ret, GLE);
89 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
91 str_byte_len = (lstrlenW(string) + 1) * sizeof(WCHAR);
92 full_byte_len = sizeof(string);
93 ok(type == REG_SZ, "RegQueryValueExW returned type %ld\n", type);
94 ok(cbData == full_byte_len || cbData == str_byte_len,
95 "cbData=%ld instead of %ld or %ld\n", cbData, full_byte_len, str_byte_len);
98 static void test_set_value(void)
102 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
103 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};
104 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
105 static const WCHAR string2W[] = {'T','h','i','s', 0 ,'B','r','e','a','k','s', 0 , 0 ,'A', 0 , 0 , 0 , 0 ,'L','o','t', 0 , 0 , 0 , 0};
107 static const char name1A[] = "CleanSingleString";
108 static const char name2A[] = "SomeIntraZeroedString";
109 static const char string1A[] = "ThisNeverBreaks";
110 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
112 /* test RegSetValueExA with normal string */
113 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
114 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %ld, GLE=%ld\n", ret, GetLastError());
115 test_hkey_main_Value_A(name1A, string1A);
116 test_hkey_main_Value_W(name1W, string1W);
118 /* test RegSetValueExA with intrazeroed string */
119 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
120 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %ld, GLE=%ld\n", ret, GetLastError());
121 test_hkey_main_Value_A(name1A, string1A);
122 test_hkey_main_Value_W(name1W, string1W);
124 /* 9x doesn't support W-calls, so don't test them then */
125 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
127 /* test RegSetValueExW with normal string */
128 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
129 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %ld, GLE=%ld\n", ret, GetLastError());
130 test_hkey_main_Value_A(name1A, string1A);
131 test_hkey_main_Value_W(name1W, string1W);
133 /* test RegSetValueExW with intrazeroed string */
134 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
135 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %ld, GLE=%ld\n", ret, GetLastError());
136 test_hkey_main_Value_A(name1A, string1A);
137 test_hkey_main_Value_W(name1W, string1W);
140 static void create_test_entries(void)
142 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
144 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
145 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
147 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
148 "RegSetValueExA failed\n");
149 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
150 "RegSetValueExA failed\n");
151 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
152 "RegSetValueExA failed\n");
153 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
154 "RegSetValueExA failed\n");
155 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
156 "RegSetValueExA failed\n");
157 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
158 "RegSetValueExA failed\n");
161 static void test_enum_value(void)
165 char value[20], data[20];
166 WCHAR valueW[20], dataW[20];
167 DWORD val_count, data_count, type;
168 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
169 static const WCHAR testW[] = {'T','e','s','t',0};
170 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
172 /* create the working key for new 'Test' value */
173 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
174 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res);
176 /* check NULL data with zero length */
177 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
178 if (GetVersion() & 0x80000000)
179 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %ld\n", res );
181 ok( !res, "RegSetValueExA returned %ld\n", res );
182 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
183 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
184 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
185 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
187 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
188 ok( res == 0, "RegSetValueExA failed error %ld\n", res );
190 /* overflow both name and data */
194 strcpy( value, "xxxxxxxxxx" );
195 strcpy( data, "xxxxxxxxxx" );
196 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
197 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
198 ok( val_count == 2, "val_count set to %ld\n", val_count );
199 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
200 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
201 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
202 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
208 strcpy( value, "xxxxxxxxxx" );
209 strcpy( data, "xxxxxxxxxx" );
210 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
211 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
212 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
213 ok( val_count == 2 || val_count == 3, "val_count set to %ld\n", val_count );
214 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
215 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
216 /* v5.1.2600.0 (XP Home and Proffesional) does not touch value or data in this case */
217 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
218 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
219 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
220 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
222 /* overflow empty name */
226 strcpy( value, "xxxxxxxxxx" );
227 strcpy( data, "xxxxxxxxxx" );
228 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
229 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
230 ok( val_count == 0, "val_count set to %ld\n", val_count );
231 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
232 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
233 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
234 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
235 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
236 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
242 strcpy( value, "xxxxxxxxxx" );
243 strcpy( data, "xxxxxxxxxx" );
244 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
245 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
246 ok( val_count == 20, "val_count set to %ld\n", val_count );
247 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
248 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
249 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
250 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
256 strcpy( value, "xxxxxxxxxx" );
257 strcpy( data, "xxxxxxxxxx" );
258 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
259 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
260 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
261 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
262 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
263 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
264 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
269 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
270 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
272 ok( res == 0, "RegSetValueExW failed error %ld\n", res );
274 /* overflow both name and data */
278 memcpy( valueW, xxxW, sizeof(xxxW) );
279 memcpy( dataW, xxxW, sizeof(xxxW) );
280 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
281 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
282 ok( val_count == 2, "val_count set to %ld\n", val_count );
283 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
284 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
285 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
286 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
292 memcpy( valueW, xxxW, sizeof(xxxW) );
293 memcpy( dataW, xxxW, sizeof(xxxW) );
294 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
295 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
296 ok( val_count == 3, "val_count set to %ld\n", val_count );
297 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
298 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
299 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
300 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
306 memcpy( valueW, xxxW, sizeof(xxxW) );
307 memcpy( dataW, xxxW, sizeof(xxxW) );
308 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
309 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
310 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
311 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
312 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
313 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
314 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
320 memcpy( valueW, xxxW, sizeof(xxxW) );
321 memcpy( dataW, xxxW, sizeof(xxxW) );
322 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
323 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
324 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
325 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
326 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
327 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
328 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
331 static void test_query_value_ex(void)
338 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
339 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
340 ok(size == strlen(sTestpath1) + 1, "(%ld,%ld)\n", (DWORD)strlen(sTestpath1) + 1, size);
341 ok(type == REG_SZ, "type %ld is not REG_SZ\n", type);
345 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Non Existent Value", NULL, &type, NULL, &size);
346 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
347 ok(size == 0, "size should have been set to 0 instead of %ld\n", size);
348 ok(type == (DWORD)HKEY_CLASSES_ROOT /* NT */ || type == 0 /* Win9x */,
349 "type should have been set to 0x80000000 or 0 instead of 0x%lx\n", type);
351 size = sizeof(buffer);
352 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Non Existent Value", NULL, &type, buffer, &size);
353 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
354 ok(size == sizeof(buffer), "size shouldn't have been changed to %ld\n", size);
357 static void test_get_value(void)
360 DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
367 CHAR expanded[] = "bar\\subdir1";
369 /* This function was introduced with Windows 2003 SP1 */
370 hadvapi32 = LoadLibraryA("advapi32.dll");
373 ok(0, "error=%ld\n", GetLastError());
376 pRegGetValueA = (PVOID)GetProcAddress(hadvapi32, "RegGetValueA");
380 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
381 size = type = dw = 0xdeadbeef;
382 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
383 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
384 ok(size == 4, "size=%ld\n", size);
385 ok(type == REG_DWORD, "type=%ld\n", type);
386 ok(dw == 0x12345678, "dw=%ld\n", dw);
388 /* Query by subkey-name */
389 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
390 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
392 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
393 size = type = dw = 0xdeadbeef;
394 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
395 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
396 /* Although the function failed all values are retrieved */
397 ok(size == 4, "size=%ld\n", size);
398 ok(type == REG_DWORD, "type=%ld\n", type);
399 ok(dw == 0x12345678, "dw=%ld\n", dw);
401 /* Test RRF_ZEROONFAILURE */
402 type = dw = 0xdeadbeef; size = 4;
403 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
404 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
405 /* Again all values are retrieved ... */
406 ok(size == 4, "size=%ld\n", size);
407 ok(type == REG_DWORD, "type=%ld\n", type);
408 /* ... except the buffer, which is zeroed out */
409 ok(dw == 0, "dw=%ld\n", dw);
411 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
412 size = type = dw = 0xdeadbeef;
413 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
414 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
415 ok(size == 4, "size=%ld\n", size);
416 ok(type == REG_DWORD, "type=%ld\n", type);
417 ok(dw == 0x12345678, "dw=%ld\n", dw);
419 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
420 size = type = dw = 0xdeadbeef;
421 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
422 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
423 ok(size == 4, "size=%ld\n", size);
424 ok(type == REG_BINARY, "type=%ld\n", type);
425 ok(dw == 0x12345678, "dw=%ld\n", dw);
427 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
428 qw[0] = qw[1] = size = type = 0xdeadbeef;
429 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
430 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%ld\n", ret);
431 ok(size == 8, "size=%ld\n", size);
432 ok(type == REG_BINARY, "type=%ld\n", type);
433 ok(qw[0] == 0x12345678 &&
434 qw[1] == 0x87654321, "qw={%ld,%ld}\n", qw[0], qw[1]);
436 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
437 type = dw = 0xdeadbeef; size = 4;
438 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
439 ok(ret == ERROR_MORE_DATA, "ret=%ld\n", ret);
440 ok(dw == 0xdeadbeef, "dw=%ld\n", dw);
441 ok(size == 8, "size=%ld\n", size);
443 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
444 qw[0] = qw[1] = size = type = 0xdeadbeef;
445 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
446 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
447 ok(size == 8, "size=%ld\n", size);
448 ok(type == REG_BINARY, "type=%ld\n", type);
449 ok(qw[0] == 0x12345678 &&
450 qw[1] == 0x87654321, "qw={%ld,%ld}\n", qw[0], qw[1]);
452 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
453 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
454 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
455 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
456 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", lstrlenA(sTestpath1), size);
457 ok(type == REG_SZ, "type=%ld\n", type);
458 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
460 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
461 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
462 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
463 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
464 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", lstrlenA(sTestpath1), size);
465 ok(type == REG_SZ, "type=%ld\n", type);
466 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
468 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
469 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
470 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
471 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
472 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
473 ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1),
474 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%ld\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
475 ok(type == REG_SZ, "type=%ld\n", type);
476 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
478 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
479 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
480 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
481 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
482 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", lstrlenA(sTestpath1), size);
483 ok(type == REG_EXPAND_SZ, "type=%ld\n", type);
484 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
486 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
487 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
488 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
490 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
491 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
492 ok(ret == ERROR_INVALID_PARAMETER, "ret=%ld\n", ret);
495 static void test_reg_open_key(void)
498 HKEY hkResult = NULL;
499 HKEY hkPreserve = NULL;
501 /* successful open */
502 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
503 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
504 ok(hkResult != NULL, "expected hkResult != NULL\n");
505 hkPreserve = hkResult;
507 /* these tests fail on Win9x, but we want to be compatible with NT, so
508 * run them if we can */
509 if (!(GetVersion() & 0x80000000))
511 /* open same key twice */
512 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
513 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
514 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
515 ok(hkResult != NULL, "hkResult != NULL\n");
516 RegCloseKey(hkResult);
518 /* open nonexistent key
519 * check that hkResult is set to NULL
521 hkResult = hkPreserve;
522 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
523 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
524 ok(hkResult == NULL, "expected hkResult == NULL\n");
526 /* open the same nonexistent key again to make sure the key wasn't created */
527 hkResult = hkPreserve;
528 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
529 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
530 ok(hkResult == NULL, "expected hkResult == NULL\n");
532 /* send in NULL lpSubKey
533 * check that hkResult receives the value of hKey
535 hkResult = hkPreserve;
536 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
537 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
538 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
540 /* send empty-string in lpSubKey */
541 hkResult = hkPreserve;
542 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
543 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
544 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
546 /* send in NULL lpSubKey and NULL hKey
547 * hkResult is set to NULL
549 hkResult = hkPreserve;
550 ret = RegOpenKeyA(NULL, NULL, &hkResult);
551 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
552 ok(hkResult == NULL, "expected hkResult == NULL\n");
555 /* only send NULL hKey
556 * the value of hkResult remains unchanged
558 hkResult = hkPreserve;
559 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
560 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
561 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
562 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
563 RegCloseKey(hkResult);
565 /* send in NULL hkResult */
566 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
567 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
569 /* beginning backslash character */
570 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
571 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
572 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
573 , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %ld\n", ret);
576 static void test_reg_create_key(void)
580 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
581 ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
582 /* should succeed: all versions of Windows ignore the access rights
583 * to the parent handle */
584 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
585 ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
588 RegDeleteKey(hkey2, "");
589 RegDeleteKey(hkey1, "");
591 /* beginning backslash character */
592 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
593 if (!(GetVersion() & 0x80000000))
594 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %ld\n", ret);
596 ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
597 RegDeleteKey(hkey1, NULL);
601 static void test_reg_close_key(void)
606 /* successfully close key
607 * hkHandle remains changed after call to RegCloseKey
609 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
610 ret = RegCloseKey(hkHandle);
611 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
613 /* try to close the key twice */
614 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
615 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
616 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %ld\n", ret);
618 /* try to close a NULL handle */
619 ret = RegCloseKey(NULL);
620 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
621 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
624 static void test_reg_delete_key(void)
628 ret = RegDeleteKey(hkey_main, NULL);
629 ok(ret == ERROR_INVALID_PARAMETER ||
630 ret == ERROR_ACCESS_DENIED ||
631 ret == ERROR_BADKEY, /* Win95 */
635 static void test_reg_save_key(void)
639 ret = RegSaveKey(hkey_main, "saved_key", NULL);
640 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
643 static void test_reg_load_key(void)
648 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
649 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
651 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
652 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
654 RegCloseKey(hkHandle);
657 static void test_reg_unload_key(void)
661 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
662 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
664 DeleteFile("saved_key");
665 DeleteFile("saved_key.LOG");
668 static BOOL set_privileges(LPCSTR privilege, BOOL set)
674 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
677 if(!LookupPrivilegeValue(NULL, privilege, &luid))
683 tp.PrivilegeCount = 1;
684 tp.Privileges[0].Luid = luid;
687 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
689 tp.Privileges[0].Attributes = 0;
691 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
692 if (GetLastError() != ERROR_SUCCESS)
702 /* tests that show that RegConnectRegistry and
703 OpenSCManager accept computer names without the
704 \\ prefix (what MSDN says). */
705 static void test_regconnectregistry( void)
707 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
708 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
709 DWORD len = sizeof(compName) ;
716 ret = GetComputerNameA(compName, &len);
717 ok( ret, "GetComputerName failed err = %ld\n", GetLastError());
720 lstrcpyA(netwName, "\\\\");
721 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
723 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
724 ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %ld\n", retl);
725 if( !retl) RegCloseKey( hkey);
727 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
728 ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %ld\n", retl);
729 if( !retl) RegCloseKey( hkey);
731 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
732 GLE = GetLastError();
733 ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED,
734 "OpenSCManagerA failed err = %ld\n", GLE);
735 CloseServiceHandle( schnd);
737 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
738 GLE = GetLastError();
739 ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED,
740 "OpenSCManagerA failed err = %ld\n", GLE);
741 CloseServiceHandle( schnd);
749 create_test_entries();
751 test_query_value_ex();
754 test_reg_create_key();
755 test_reg_close_key();
756 test_reg_delete_key();
758 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
759 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
760 set_privileges(SE_RESTORE_NAME, TRUE))
764 test_reg_unload_key();
766 set_privileges(SE_BACKUP_NAME, FALSE);
767 set_privileges(SE_RESTORE_NAME, FALSE);
771 delete_key( hkey_main );
773 test_regconnectregistry();