wininet: Keep handles invalid but reserved in InternetCloseHandle.
[wine] / dlls / setupapi / tests / devinst.c
1 /*
2  * Devinst tests
3  *
4  * Copyright 2006 Christian Gmeiner
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "guiddef.h"
30 #include "setupapi.h"
31
32 #include "wine/test.h"
33
34 static BOOL is_wow64;
35
36 /* function pointers */
37 static HDEVINFO (WINAPI *pSetupDiCreateDeviceInfoList)(GUID*,HWND);
38 static HDEVINFO (WINAPI *pSetupDiCreateDeviceInfoListExW)(GUID*,HWND,PCWSTR,PVOID);
39 static BOOL     (WINAPI *pSetupDiCreateDeviceInterfaceA)(HDEVINFO, PSP_DEVINFO_DATA, const GUID *, PCSTR, DWORD, PSP_DEVICE_INTERFACE_DATA);
40 static BOOL     (WINAPI *pSetupDiCallClassInstaller)(DI_FUNCTION, HDEVINFO, PSP_DEVINFO_DATA);
41 static BOOL     (WINAPI *pSetupDiDestroyDeviceInfoList)(HDEVINFO);
42 static BOOL     (WINAPI *pSetupDiEnumDeviceInfo)(HDEVINFO, DWORD, PSP_DEVINFO_DATA);
43 static BOOL     (WINAPI *pSetupDiEnumDeviceInterfaces)(HDEVINFO, PSP_DEVINFO_DATA, const GUID *, DWORD, PSP_DEVICE_INTERFACE_DATA);
44 static BOOL     (WINAPI *pSetupDiGetINFClassA)(PCSTR, LPGUID, PSTR, DWORD, PDWORD);
45 static BOOL     (WINAPI *pSetupDiInstallClassA)(HWND, PCSTR, DWORD, HSPFILEQ);
46 static HKEY     (WINAPI *pSetupDiOpenClassRegKeyExA)(GUID*,REGSAM,DWORD,PCSTR,PVOID);
47 static HKEY     (WINAPI *pSetupDiOpenDevRegKey)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, DWORD, DWORD, REGSAM);
48 static HKEY     (WINAPI *pSetupDiCreateDevRegKeyW)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, DWORD, DWORD, HINF, PCWSTR);
49 static BOOL     (WINAPI *pSetupDiCreateDeviceInfoA)(HDEVINFO, PCSTR, GUID *, PCSTR, HWND, DWORD, PSP_DEVINFO_DATA);
50 static BOOL     (WINAPI *pSetupDiCreateDeviceInfoW)(HDEVINFO, PCWSTR, GUID *, PCWSTR, HWND, DWORD, PSP_DEVINFO_DATA);
51 static BOOL     (WINAPI *pSetupDiGetDeviceInstanceIdA)(HDEVINFO, PSP_DEVINFO_DATA, PSTR, DWORD, PDWORD);
52 static BOOL     (WINAPI *pSetupDiGetDeviceInterfaceDetailA)(HDEVINFO, PSP_DEVICE_INTERFACE_DATA, PSP_DEVICE_INTERFACE_DETAIL_DATA_A, DWORD, PDWORD, PSP_DEVINFO_DATA);
53 static BOOL     (WINAPI *pSetupDiGetDeviceInterfaceDetailW)(HDEVINFO, PSP_DEVICE_INTERFACE_DATA, PSP_DEVICE_INTERFACE_DETAIL_DATA_W, DWORD, PDWORD, PSP_DEVINFO_DATA);
54 static BOOL     (WINAPI *pSetupDiRegisterDeviceInfo)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, PSP_DETSIG_CMPPROC, PVOID, PSP_DEVINFO_DATA);
55 static HDEVINFO (WINAPI *pSetupDiGetClassDevsA)(CONST GUID *, LPCSTR, HWND, DWORD);
56 static HDEVINFO (WINAPI *pSetupDiGetClassDevsW)(CONST GUID *, LPCWSTR, HWND, DWORD);
57 static BOOL     (WINAPI *pSetupDiSetDeviceRegistryPropertyA)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, const BYTE *, DWORD);
58 static BOOL     (WINAPI *pSetupDiSetDeviceRegistryPropertyW)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, const BYTE *, DWORD);
59 static BOOL     (WINAPI *pSetupDiGetDeviceRegistryPropertyA)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD);
60 static BOOL     (WINAPI *pSetupDiGetDeviceRegistryPropertyW)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD);
61 static BOOL     (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
62
63 /* This is a unique guid for testing purposes */
64 static GUID guid = {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,0x00,0x11,0x95,0x5c,0x2b,0xdb}};
65
66 static void init_function_pointers(void)
67 {
68     HMODULE hSetupAPI = GetModuleHandleA("setupapi.dll");
69     HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
70
71     pSetupDiCreateDeviceInfoA = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoA");
72     pSetupDiCreateDeviceInfoW = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoW");
73     pSetupDiCreateDeviceInfoList = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoList");
74     pSetupDiCreateDeviceInfoListExW = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoListExW");
75     pSetupDiCreateDeviceInterfaceA = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInterfaceA");
76     pSetupDiDestroyDeviceInfoList = (void *)GetProcAddress(hSetupAPI, "SetupDiDestroyDeviceInfoList");
77     pSetupDiCallClassInstaller = (void *)GetProcAddress(hSetupAPI, "SetupDiCallClassInstaller");
78     pSetupDiEnumDeviceInfo = (void *)GetProcAddress(hSetupAPI, "SetupDiEnumDeviceInfo");
79     pSetupDiEnumDeviceInterfaces = (void *)GetProcAddress(hSetupAPI, "SetupDiEnumDeviceInterfaces");
80     pSetupDiGetDeviceInstanceIdA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceInstanceIdA");
81     pSetupDiGetDeviceInterfaceDetailA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceInterfaceDetailA");
82     pSetupDiGetDeviceInterfaceDetailW = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceInterfaceDetailW");
83     pSetupDiInstallClassA = (void *)GetProcAddress(hSetupAPI, "SetupDiInstallClassA");
84     pSetupDiOpenClassRegKeyExA = (void *)GetProcAddress(hSetupAPI, "SetupDiOpenClassRegKeyExA");
85     pSetupDiOpenDevRegKey = (void *)GetProcAddress(hSetupAPI, "SetupDiOpenDevRegKey");
86     pSetupDiCreateDevRegKeyW = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDevRegKeyW");
87     pSetupDiRegisterDeviceInfo = (void *)GetProcAddress(hSetupAPI, "SetupDiRegisterDeviceInfo");
88     pSetupDiGetClassDevsA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetClassDevsA");
89     pSetupDiGetClassDevsW = (void *)GetProcAddress(hSetupAPI, "SetupDiGetClassDevsW");
90     pSetupDiGetINFClassA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetINFClassA");
91     pSetupDiSetDeviceRegistryPropertyA = (void *)GetProcAddress(hSetupAPI, "SetupDiSetDeviceRegistryPropertyA");
92     pSetupDiSetDeviceRegistryPropertyW = (void *)GetProcAddress(hSetupAPI, "SetupDiSetDeviceRegistryPropertyW");
93     pSetupDiGetDeviceRegistryPropertyA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceRegistryPropertyA");
94     pSetupDiGetDeviceRegistryPropertyW = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceRegistryPropertyW");
95     pIsWow64Process = (void *)GetProcAddress(hKernel32, "IsWow64Process");
96 }
97
98 static void change_reg_permissions(const WCHAR *regkey)
99 {
100     HKEY hkey;
101     SID_IDENTIFIER_AUTHORITY ident = { SECURITY_WORLD_SID_AUTHORITY };
102     SECURITY_DESCRIPTOR sd;
103     PSID EveryoneSid;
104     PACL pacl = NULL;
105
106     RegOpenKeyExW(HKEY_LOCAL_MACHINE, regkey, 0, WRITE_DAC, &hkey);
107
108     /* Initialize the 'Everyone' sid */
109     AllocateAndInitializeSid(&ident, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
110
111     pacl = HeapAlloc(GetProcessHeap(), 0, 256);
112     InitializeAcl(pacl, 256, ACL_REVISION);
113
114     /* Add 'Full Control' for 'Everyone' */
115     AddAccessAllowedAce(pacl, ACL_REVISION, KEY_ALL_ACCESS, EveryoneSid);
116
117     InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
118
119     SetSecurityDescriptorDacl(&sd, TRUE, pacl, FALSE);
120
121     /* Set the new security on the registry key */
122     RegSetKeySecurity(hkey, DACL_SECURITY_INFORMATION, &sd);
123
124     RegCloseKey(hkey);
125
126     HeapFree(GetProcessHeap(), 0, pacl);
127     if (EveryoneSid)
128         FreeSid(EveryoneSid);
129 }
130
131 static BOOL remove_device(void)
132 {
133     HDEVINFO set;
134     SP_DEVINFO_DATA devInfo = { sizeof(devInfo), { 0 } };
135     BOOL ret, retval;
136
137     SetLastError(0xdeadbeef);
138     set = pSetupDiGetClassDevsA(&guid, NULL, 0, 0);
139     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsA failed: %08x\n",
140      GetLastError());
141
142     SetLastError(0xdeadbeef);
143     ret = pSetupDiEnumDeviceInfo(set, 0, &devInfo);
144     ok(ret, "SetupDiEnumDeviceInfo failed: %08x\n", GetLastError());
145
146     SetLastError(0xdeadbeef);
147     retval = pSetupDiCallClassInstaller(DIF_REMOVE, set, &devInfo);
148     if(is_wow64)
149         todo_wine ok(!retval && GetLastError() == ERROR_IN_WOW64,
150                      "SetupDiCallClassInstaller(DIF_REMOVE...) succeeded: %08x\n", GetLastError());
151     else
152         todo_wine ok(retval,
153                      "SetupDiCallClassInstaller(DIF_REMOVE...) failed: %08x\n", GetLastError());
154
155     SetLastError(0xdeadbeef);
156     ret = pSetupDiDestroyDeviceInfoList(set);
157     ok(ret, "SetupDiDestroyDeviceInfoList failed: %08x\n", GetLastError());
158
159     return retval;
160 }
161
162 /* RegDeleteTreeW from dlls/advapi32/registry.c */
163 static LSTATUS devinst_RegDeleteTreeW(HKEY hKey, LPCWSTR lpszSubKey)
164 {
165     LONG ret;
166     DWORD dwMaxSubkeyLen, dwMaxValueLen;
167     DWORD dwMaxLen, dwSize;
168     WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
169     HKEY hSubKey = hKey;
170
171     if(lpszSubKey)
172     {
173         ret = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
174         if (ret) return ret;
175     }
176
177     /* Get highest length for keys, values */
178     ret = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, NULL,
179             &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
180     if (ret) goto cleanup;
181
182     dwMaxSubkeyLen++;
183     dwMaxValueLen++;
184     dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
185     if (dwMaxLen > sizeof(szNameBuf)/sizeof(WCHAR))
186     {
187         /* Name too big: alloc a buffer for it */
188         if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(WCHAR))))
189         {
190             ret = ERROR_NOT_ENOUGH_MEMORY;
191             goto cleanup;
192         }
193     }
194
195
196     /* Recursively delete all the subkeys */
197     while (TRUE)
198     {
199         dwSize = dwMaxLen;
200         if (RegEnumKeyExW(hSubKey, 0, lpszName, &dwSize, NULL,
201                           NULL, NULL, NULL)) break;
202
203         ret = devinst_RegDeleteTreeW(hSubKey, lpszName);
204         if (ret) goto cleanup;
205     }
206
207     if (lpszSubKey)
208         ret = RegDeleteKeyW(hKey, lpszSubKey);
209     else
210         while (TRUE)
211         {
212             dwSize = dwMaxLen;
213             if (RegEnumValueW(hKey, 0, lpszName, &dwSize,
214                   NULL, NULL, NULL, NULL)) break;
215
216             ret = RegDeleteValueW(hKey, lpszName);
217             if (ret) goto cleanup;
218         }
219
220 cleanup:
221     /* Free buffer if allocated */
222     if (lpszName != szNameBuf)
223         HeapFree( GetProcessHeap(), 0, lpszName);
224     if(lpszSubKey)
225         RegCloseKey(hSubKey);
226     return ret;
227 }
228
229 static void clean_devclass_key(void)
230 {
231     static const WCHAR devclass[] = {'S','y','s','t','e','m','\\',
232      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
233      'C','o','n','t','r','o','l','\\','D','e','v','i','c','e','C','l','a','s','s','e','s','\\',
234      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
235      '1','1','d','b','-','b','7','0','4','-',
236      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
237     HKEY key;
238     DWORD subkeys;
239
240     /* Check if we have subkeys as Windows 2000 doesn't delete
241      * the keys under the DeviceClasses key after a SetupDiDestroyDeviceInfoList.
242      */
243     RegOpenKeyW(HKEY_LOCAL_MACHINE, devclass, &key);
244     RegQueryInfoKey(key, NULL, NULL, NULL, &subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
245     if (subkeys > 0)
246     {
247         trace("We are most likely on Windows 2000\n");
248         devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, devclass);
249     }
250     else
251     {
252         ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, devclass),
253          "Couldn't delete deviceclass key\n");
254     }
255 }
256
257 static void test_SetupDiCreateDeviceInfoListEx(void) 
258 {
259     HDEVINFO devlist;
260     BOOL ret;
261     DWORD error;
262     static CHAR notnull[] = "NotNull";
263     static const WCHAR machine[] = { 'd','u','m','m','y',0 };
264
265     SetLastError(0xdeadbeef);
266     /* create empty DeviceInfoList, but set Reserved to a value, which is not NULL */
267     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, notnull);
268
269     error = GetLastError();
270     if (error == ERROR_CALL_NOT_IMPLEMENTED)
271     {
272         win_skip("SetupDiCreateDeviceInfoListExW is not implemented\n");
273         return;
274     }
275     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
276     ok(error == ERROR_INVALID_PARAMETER, "GetLastError returned wrong value : %d, (expected %d)\n", error, ERROR_INVALID_PARAMETER);
277
278     SetLastError(0xdeadbeef);
279     /* create empty DeviceInfoList, but set MachineName to something */
280     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, machine, NULL);
281
282     error = GetLastError();
283     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
284     ok(error == ERROR_INVALID_MACHINENAME || error == ERROR_MACHINE_UNAVAILABLE, "GetLastError returned wrong value : %d, (expected %d or %d)\n", error, ERROR_INVALID_MACHINENAME, ERROR_MACHINE_UNAVAILABLE);
285
286     /* create empty DeviceInfoList */
287     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, NULL);
288     ok(devlist && devlist != INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected != %p)\n", devlist, error, INVALID_HANDLE_VALUE);
289
290     /* destroy DeviceInfoList */
291     ret = pSetupDiDestroyDeviceInfoList(devlist);
292     ok(ret, "SetupDiDestroyDeviceInfoList failed : %d\n", error);
293 }
294
295 static void test_SetupDiOpenClassRegKeyExA(void)
296 {
297     static const CHAR guidString[] = "{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
298     HKEY hkey;
299
300     /* Check return value for nonexistent key */
301     hkey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
302         DIOCR_INSTALLER, NULL, NULL);
303     ok(hkey == INVALID_HANDLE_VALUE,
304         "returned %p (expected INVALID_HANDLE_VALUE)\n", hkey);
305
306     /* Test it for a key that exists */
307     hkey = SetupDiOpenClassRegKey(NULL, KEY_ALL_ACCESS);
308     if (hkey != INVALID_HANDLE_VALUE)
309     {
310         HKEY classKey;
311         if (RegCreateKeyA(hkey, guidString, &classKey) == ERROR_SUCCESS)
312         {
313             RegCloseKey(classKey);
314             SetLastError(0xdeadbeef);
315             classKey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
316                 DIOCR_INSTALLER, NULL, NULL);
317             ok(classKey != INVALID_HANDLE_VALUE,
318                 "opening class registry key failed with error %d\n",
319                 GetLastError());
320             if (classKey != INVALID_HANDLE_VALUE)
321                 RegCloseKey(classKey);
322             RegDeleteKeyA(hkey, guidString);
323         }
324         else
325             trace("failed to create registry key for test\n");
326
327         RegCloseKey(hkey);
328     }
329     else
330         trace("failed to open classes key\n");
331 }
332
333 static void create_inf_file(LPCSTR filename)
334 {
335     DWORD dwNumberOfBytesWritten;
336     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
337                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
338
339     static const char data[] =
340         "[Version]\n"
341         "Signature=\"$Chicago$\"\n"
342         "Class=Bogus\n"
343         "ClassGUID={6a55b5a4-3f65-11db-b704-0011955c2bdb}\n"
344         "[ClassInstall32]\n"
345         "AddReg=BogusClass.NT.AddReg\n"
346         "[BogusClass.NT.AddReg]\n"
347         "HKR,,,,\"Wine test devices\"\n";
348
349     WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
350     CloseHandle(hf);
351 }
352
353 static void get_temp_filename(LPSTR path)
354 {
355     static char curr[MAX_PATH] = { 0 };
356     char temp[MAX_PATH];
357     LPSTR ptr;
358
359     if (!*curr)
360         GetCurrentDirectoryA(MAX_PATH, curr);
361     GetTempFileNameA(curr, "set", 0, temp);
362     ptr = strrchr(temp, '\\');
363
364     lstrcpyA(path, ptr + 1);
365 }
366
367 static void testInstallClass(void)
368 {
369     static const WCHAR classKey[] = {'S','y','s','t','e','m','\\',
370      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
371      'C','o','n','t','r','o','l','\\','C','l','a','s','s','\\',
372      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
373      '1','1','d','b','-','b','7','0','4','-',
374      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
375     char tmpfile[MAX_PATH];
376     BOOL ret;
377
378     tmpfile[0] = '.';
379     tmpfile[1] = '\\';
380     get_temp_filename(tmpfile + 2);
381     create_inf_file(tmpfile + 2);
382
383     ret = pSetupDiInstallClassA(NULL, NULL, 0, NULL);
384     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
385      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
386     ret = pSetupDiInstallClassA(NULL, NULL, DI_NOVCP, NULL);
387     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
388      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
389     ret = pSetupDiInstallClassA(NULL, tmpfile + 2, DI_NOVCP, NULL);
390     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
391      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
392     ret = pSetupDiInstallClassA(NULL, tmpfile + 2, 0, NULL);
393     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
394      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
395     /* The next call will succeed. Information is put into the registry but the
396      * location(s) is/are depending on the Windows version.
397      */
398     ret = pSetupDiInstallClassA(NULL, tmpfile, 0, NULL);
399     ok(ret, "SetupDiInstallClassA failed: %08x\n", GetLastError());
400
401     ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, classKey),
402      "Couldn't delete classkey\n");
403
404     DeleteFile(tmpfile);
405 }
406
407 static void testCreateDeviceInfo(void)
408 {
409     BOOL ret;
410     HDEVINFO set;
411     HKEY key;
412     LONG res;
413     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
414      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
415      'E','n','u','m','\\','R','o','o','t','\\',
416      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
417
418     SetLastError(0xdeadbeef);
419     ret = pSetupDiCreateDeviceInfoA(NULL, NULL, NULL, NULL, NULL, 0, NULL);
420     ok(!ret, "Expected failure\n");
421     ok(GetLastError() == ERROR_INVALID_DEVINST_NAME ||
422       GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
423      "Unexpected last error, got %08x\n", GetLastError());
424
425     SetLastError(0xdeadbeef);
426     ret = pSetupDiCreateDeviceInfoA(NULL, "Root\\LEGACY_BOGUS\\0000", NULL,
427      NULL, NULL, 0, NULL);
428     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
429      "Expected ERROR_INVALID_HANDLEHANDLE, got %08x\n", GetLastError());
430     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
431     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %08x\n",
432      GetLastError());
433     if (set)
434     {
435         SP_DEVINFO_DATA devInfo = { 0 };
436         DWORD i;
437         static GUID deadbeef =
438          {0xdeadbeef, 0xdead, 0xbeef, {0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
439         LONG res;
440         HKEY key;
441         static const WCHAR bogus0000[] = {'S','y','s','t','e','m','\\',
442          'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
443          'E','n','u','m','\\','R','o','o','t','\\',
444          'L','E','G','A','C','Y','_','B','O','G','U','S','\\','0','0','0','0',0};
445
446         /* So we know we have a clean start */
447         res = RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus0000, &key);
448         ok(res != ERROR_SUCCESS, "Expected key to not exist\n");
449         /* No GUID given */
450         SetLastError(0xdeadbeef);
451         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", NULL,
452          NULL, NULL, 0, NULL);
453         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
454             "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
455         /* Even though NT4 fails it still adds some stuff to the registry that
456          * can't be deleted via normal setupapi functions. As the registry is written
457          * by a different user (SYSTEM) we have to do some magic to get rid of the key
458          */
459         if (!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus0000, &key))
460         {
461             trace("NT4 created a bogus key on failure, will be removed now\n");
462             change_reg_permissions(bogus0000);
463             ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, bogus0000),
464              "Could not delete LEGACY_BOGUS\\0000 key\n");
465         }
466         /* We can't add device information to the set with a different GUID */
467         SetLastError(0xdeadbeef);
468         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000",
469          &deadbeef, NULL, NULL, 0, NULL);
470         ok(!ret && GetLastError() == ERROR_CLASS_MISMATCH,
471          "Expected ERROR_CLASS_MISMATCH, got %08x\n", GetLastError());
472         if (!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus0000, &key))
473         {
474             trace("NT4 created a bogus key on failure, will be removed now\n");
475             change_reg_permissions(bogus0000);
476             ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, bogus0000),
477              "Could not delete LEGACY_BOGUS\\0000 key\n");
478         }
479         /* Finally, with all three required parameters, this succeeds: */
480         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
481          NULL, NULL, 0, NULL);
482         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
483         /* This fails because the device ID already exists.. */
484         SetLastError(0xdeadbeef);
485         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
486          NULL, NULL, 0, &devInfo);
487         ok(!ret && GetLastError() == ERROR_DEVINST_ALREADY_EXISTS,
488          "Expected ERROR_DEVINST_ALREADY_EXISTS, got %08x\n", GetLastError());
489         /* whereas this "fails" because cbSize is wrong.. */
490         SetLastError(0xdeadbeef);
491         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid, NULL, NULL,
492          DICD_GENERATE_ID, &devInfo);
493         ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
494          "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
495         /* and this finally succeeds. */
496         devInfo.cbSize = sizeof(devInfo);
497         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid, NULL, NULL,
498          DICD_GENERATE_ID, &devInfo);
499         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
500         /* There were three devices added, however - the second failure just
501          * resulted in the SP_DEVINFO_DATA not getting copied.
502          */
503         SetLastError(0xdeadbeef);
504         i = 0;
505         while (pSetupDiEnumDeviceInfo(set, i, &devInfo))
506             i++;
507         ok(i == 3, "Expected 3 devices, got %d\n", i);
508         ok(GetLastError() == ERROR_NO_MORE_ITEMS,
509          "SetupDiEnumDeviceInfo failed: %08x\n", GetLastError());
510         pSetupDiDestroyDeviceInfoList(set);
511     }
512
513     /* The bogus registry key shouldn't be there after this test. The only
514      * reasons this key would still be present:
515      *
516      * - We are running on Wine which has to be fixed
517      * - We have leftovers from old tests
518      */
519     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key);
520     todo_wine
521     ok(res == ERROR_FILE_NOT_FOUND, "Expected key to not exist\n");
522     if (res == ERROR_SUCCESS)
523     {
524         DWORD subkeys;
525
526         /* Check if we have subkeys */
527         RegQueryInfoKey(key, NULL, NULL, NULL, &subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
528         if (subkeys > 0)
529         {
530             int i;
531
532             /* Leftovers from old tests */
533             trace("Going to remove %d devices\n", subkeys);
534             for (i = 0; i < subkeys; i++)
535             {
536                 BOOL ret;
537
538                 ret = remove_device();
539                 ok(ret, "Expected a device to be removed\n");
540             }
541         }
542         else
543         {
544             /* Wine doesn't delete the bogus key itself currently */
545             trace("We are most likely on Wine\n");
546             RegDeleteKeyW(HKEY_LOCAL_MACHINE, bogus);
547         }
548     }
549 }
550
551 static void testGetDeviceInstanceId(void)
552 {
553     BOOL ret;
554     HDEVINFO set;
555     SP_DEVINFO_DATA devInfo = { 0 };
556
557     SetLastError(0xdeadbeef);
558     ret = pSetupDiGetDeviceInstanceIdA(NULL, NULL, NULL, 0, NULL);
559     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
560      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
561     SetLastError(0xdeadbeef);
562     ret = pSetupDiGetDeviceInstanceIdA(NULL, &devInfo, NULL, 0, NULL);
563     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
564      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
565     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
566     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %08x\n",
567      GetLastError());
568     if (set)
569     {
570         char instanceID[MAX_PATH];
571         DWORD size;
572
573         SetLastError(0xdeadbeef);
574         ret = pSetupDiGetDeviceInstanceIdA(set, NULL, NULL, 0, NULL);
575         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
576          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
577         SetLastError(0xdeadbeef);
578         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, NULL);
579         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
580          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
581         SetLastError(0xdeadbeef);
582         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
583         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
584          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
585         devInfo.cbSize = sizeof(devInfo);
586         SetLastError(0xdeadbeef);
587         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
588         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
589          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
590         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
591          NULL, NULL, 0, &devInfo);
592         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
593         SetLastError(0xdeadbeef);
594         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
595         ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
596          "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
597         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, instanceID,
598          sizeof(instanceID), NULL);
599         ok(ret, "SetupDiGetDeviceInstanceIdA failed: %08x\n", GetLastError());
600         ok(!lstrcmpA(instanceID, "ROOT\\LEGACY_BOGUS\\0000"),
601          "Unexpected instance ID %s\n", instanceID);
602         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid,
603          NULL, NULL, DICD_GENERATE_ID, &devInfo);
604         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
605         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, instanceID,
606          sizeof(instanceID), NULL);
607         ok(ret, "SetupDiGetDeviceInstanceIdA failed: %08x\n", GetLastError());
608         /* NT4 returns 'Root' and W2K and above 'ROOT' */
609         ok(!lstrcmpiA(instanceID, "ROOT\\LEGACY_BOGUS\\0001"),
610          "Unexpected instance ID %s\n", instanceID);
611         pSetupDiDestroyDeviceInfoList(set);
612     }
613 }
614
615 static void testRegisterDeviceInfo(void)
616 {
617     BOOL ret;
618     HDEVINFO set;
619
620     SetLastError(0xdeadbeef);
621     ret = pSetupDiRegisterDeviceInfo(NULL, NULL, 0, NULL, NULL, NULL);
622     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
623      "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
624     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
625     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %d\n", GetLastError());
626     if (set)
627     {
628         SP_DEVINFO_DATA devInfo = { 0 };
629
630         SetLastError(0xdeadbeef);
631         ret = pSetupDiRegisterDeviceInfo(set, NULL, 0, NULL, NULL, NULL);
632         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
633          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
634         SetLastError(0xdeadbeef);
635         ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL, NULL);
636         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
637          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
638         devInfo.cbSize = sizeof(devInfo);
639         SetLastError(0xdeadbeef);
640         ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL, NULL);
641         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
642          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
643         ret = pSetupDiCreateDeviceInfoA(set, "USB\\BOGUS\\0000", &guid,
644          NULL, NULL, 0, &devInfo);
645         ok(ret || GetLastError() == ERROR_DEVINST_ALREADY_EXISTS,
646                 "SetupDiCreateDeviceInfoA failed: %d\n", GetLastError());
647         if (ret)
648         {
649             /* If it already existed, registering it again will fail */
650             ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL,
651              NULL);
652             ok(ret, "SetupDiCreateDeviceInfoA failed: %d\n", GetLastError());
653         }
654         /* FIXME: On Win2K+ systems, this is now persisted to registry in
655          * HKLM\System\CCS\Enum\USB\Bogus\0000.
656          * FIXME: the key also becomes undeletable.  How to get rid of it?
657          */
658         pSetupDiDestroyDeviceInfoList(set);
659     }
660 }
661
662 static void testCreateDeviceInterface(void)
663 {
664     BOOL ret;
665     HDEVINFO set;
666     HKEY key;
667     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
668      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
669      'E','n','u','m','\\','R','o','o','t','\\',
670      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
671     static const WCHAR devclass[] = {'S','y','s','t','e','m','\\',
672      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
673      'C','o','n','t','r','o','l','\\','D','e','v','i','c','e','C','l','a','s','s','e','s','\\',
674      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
675      '1','1','d','b','-','b','7','0','4','-',
676      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
677
678     if (!pSetupDiCreateDeviceInterfaceA || !pSetupDiEnumDeviceInterfaces)
679     {
680         win_skip("SetupDiCreateDeviceInterfaceA and/or SetupDiEnumDeviceInterfaces are not available\n");
681         return;
682     }
683     SetLastError(0xdeadbeef);
684     ret = pSetupDiCreateDeviceInterfaceA(NULL, NULL, NULL, NULL, 0, NULL);
685     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
686      "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
687     SetLastError(0xdeadbeef);
688     ret = pSetupDiCreateDeviceInterfaceA(NULL, NULL, &guid, NULL, 0, NULL);
689     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
690      "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
691     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
692     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %d\n", GetLastError());
693     if (set)
694     {
695         SP_DEVINFO_DATA devInfo = { 0 };
696         SP_DEVICE_INTERFACE_DATA interfaceData = { sizeof(interfaceData),
697             { 0 } };
698         DWORD i;
699
700         SetLastError(0xdeadbeef);
701         ret = pSetupDiCreateDeviceInterfaceA(set, NULL, NULL, NULL, 0, NULL);
702         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
703          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
704         SetLastError(0xdeadbeef);
705         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, NULL, NULL, 0,
706                 NULL);
707         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
708          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
709         devInfo.cbSize = sizeof(devInfo);
710         ret = pSetupDiCreateDeviceInfoA(set, "ROOT\\LEGACY_BOGUS\\0000", &guid,
711                 NULL, NULL, 0, &devInfo);
712         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
713         SetLastError(0xdeadbeef);
714         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, NULL, NULL, 0,
715                 NULL);
716         ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
717          "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
718         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, NULL, 0,
719                 NULL);
720         ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
721         /* Creating the same interface a second time succeeds */
722         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, NULL, 0,
723                 NULL);
724         ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
725         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, "Oogah", 0,
726                 NULL);
727         ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
728         ret = pSetupDiEnumDeviceInterfaces(set, &devInfo, &guid, 0,
729                 &interfaceData);
730         ok(ret, "SetupDiEnumDeviceInterfaces failed: %d\n", GetLastError());
731         i = 0;
732         while (pSetupDiEnumDeviceInterfaces(set, &devInfo, &guid, i,
733                     &interfaceData))
734             i++;
735         ok(i == 2, "expected 2 interfaces, got %d\n", i);
736         ok(GetLastError() == ERROR_NO_MORE_ITEMS,
737          "SetupDiEnumDeviceInterfaces failed: %08x\n", GetLastError());
738         ret = pSetupDiDestroyDeviceInfoList(set);
739         ok(ret, "SetupDiDestroyDeviceInfoList failed: %08x\n", GetLastError());
740
741         /* Cleanup */
742         /* FIXME: On Wine we still have the bogus entry in Enum\Root and
743          * subkeys, as well as the deviceclass key with subkeys.
744          * Only clean the deviceclass key once Wine if fixed.
745          */
746         if (!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key))
747         {
748             /* Wine doesn't delete the information currently */
749             trace("We are most likely on Wine\n");
750             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, bogus);
751             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, devclass);
752         }
753         else
754         {
755             clean_devclass_key();
756         }
757     }
758 }
759
760 static void testGetDeviceInterfaceDetail(void)
761 {
762     BOOL ret;
763     HDEVINFO set;
764     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
765      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
766      'E','n','u','m','\\','R','o','o','t','\\',
767      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
768     static const WCHAR devclass[] = {'S','y','s','t','e','m','\\',
769      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
770      'C','o','n','t','r','o','l','\\','D','e','v','i','c','e','C','l','a','s','s','e','s','\\',
771      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
772      '1','1','d','b','-','b','7','0','4','-',
773      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
774
775     if (!pSetupDiCreateDeviceInterfaceA || !pSetupDiGetDeviceInterfaceDetailA)
776     {
777         win_skip("SetupDiCreateDeviceInterfaceA and/or SetupDiGetDeviceInterfaceDetailA are not available\n");
778         return;
779     }
780     SetLastError(0xdeadbeef);
781     ret = pSetupDiGetDeviceInterfaceDetailA(NULL, NULL, NULL, 0, NULL, NULL);
782     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
783      "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
784     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
785     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %d\n", GetLastError());
786     if (set)
787     {
788         SP_DEVINFO_DATA devInfo = { sizeof(devInfo), { 0 } };
789         SP_DEVICE_INTERFACE_DATA interfaceData = { sizeof(interfaceData),
790             { 0 } };
791         DWORD size = 0;
792         HKEY key;
793
794         SetLastError(0xdeadbeef);
795         ret = pSetupDiGetDeviceInterfaceDetailA(set, NULL, NULL, 0, NULL,
796                 NULL);
797         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
798          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
799         ret = pSetupDiCreateDeviceInfoA(set, "ROOT\\LEGACY_BOGUS\\0000", &guid,
800                 NULL, NULL, 0, &devInfo);
801         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
802         SetLastError(0xdeadbeef);
803         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, NULL, 0,
804                 &interfaceData);
805         ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
806         SetLastError(0xdeadbeef);
807         ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, NULL,
808                 0, NULL, NULL);
809         ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
810          "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
811         SetLastError(0xdeadbeef);
812         ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, NULL,
813                 100, NULL, NULL);
814         ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
815          "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
816         SetLastError(0xdeadbeef);
817         ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, NULL,
818                 0, &size, NULL);
819         ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
820          "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
821         if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
822         {
823             static const char path[] =
824              "\\\\?\\root#legacy_bogus#0000#{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
825             static const char path_w2k[] =
826              "\\\\?\\root#legacy_bogus#0000#{6a55b5a4-3f65-11db-b704-0011955c2bdb}\\";
827             LPBYTE buf = HeapAlloc(GetProcessHeap(), 0, size);
828             SP_DEVICE_INTERFACE_DETAIL_DATA_A *detail =
829                 (SP_DEVICE_INTERFACE_DETAIL_DATA_A *)buf;
830             DWORD expectedsize = FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR)*(1 + strlen(path));
831
832             detail->cbSize = 0;
833             SetLastError(0xdeadbeef);
834             ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, detail,
835                     size, &size, NULL);
836             ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
837              "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
838             detail->cbSize = size;
839             SetLastError(0xdeadbeef);
840             ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, detail,
841                     size, &size, NULL);
842             ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
843              "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
844             detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
845             SetLastError(0xdeadbeef);
846             ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, detail,
847                     size, &size, NULL);
848             ok(ret, "SetupDiGetDeviceInterfaceDetailA failed: %d\n",
849                     GetLastError());
850             ok(!lstrcmpiA(path, detail->DevicePath) ||
851              !lstrcmpiA(path_w2k, detail->DevicePath), "Unexpected path %s\n",
852              detail->DevicePath);
853             /* Check SetupDiGetDeviceInterfaceDetailW */
854             ret = pSetupDiGetDeviceInterfaceDetailW(set, &interfaceData, NULL, 0, &size, NULL);
855             ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
856              "Expected ERROR_INSUFFICIENT_BUFFER, got error code: %d\n", GetLastError());
857             ok(expectedsize == size ||
858              (expectedsize + sizeof(WCHAR)) == size /* W2K adds a backslash */,
859              "SetupDiGetDeviceInterfaceDetailW returned wrong reqsize, got %d\n",
860              size);
861
862             HeapFree(GetProcessHeap(), 0, buf);
863         }
864         pSetupDiDestroyDeviceInfoList(set);
865
866         /* Cleanup */
867         /* FIXME: On Wine we still have the bogus entry in Enum\Root and
868          * subkeys, as well as the deviceclass key with subkeys.
869          * Only do the RegDeleteKey, once Wine is fixed.
870          */
871         if (!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key))
872         {
873             /* Wine doesn't delete the information currently */
874             trace("We are most likely on Wine\n");
875             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, bogus);
876             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, devclass);
877         }
878         else
879         {
880             clean_devclass_key();
881         }
882     }
883 }
884
885 static void testDevRegKey(void)
886 {
887     static const WCHAR classKey[] = {'S','y','s','t','e','m','\\',
888      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
889      'C','o','n','t','r','o','l','\\','C','l','a','s','s','\\',
890      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
891      '1','1','d','b','-','b','7','0','4','-',
892      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
893     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
894      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
895      'E','n','u','m','\\','R','o','o','t','\\',
896      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
897     BOOL ret;
898     HDEVINFO set;
899     HKEY key = NULL;
900     BOOL classKeyCreated;
901
902     SetLastError(0xdeadbeef);
903     key = pSetupDiCreateDevRegKeyW(NULL, NULL, 0, 0, 0, NULL, NULL);
904     ok(key == INVALID_HANDLE_VALUE,
905      "Expected INVALID_HANDLE_VALUE, got %p\n", key);
906     ok(GetLastError() == ERROR_INVALID_HANDLE,
907      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
908
909     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
910     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %d\n", GetLastError());
911     if (set)
912     {
913         SP_DEVINFO_DATA devInfo = { sizeof(devInfo), { 0 } };
914         LONG res;
915
916         /* The device info key shouldn't be there */
917         res = RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key);
918         ok(res != ERROR_SUCCESS, "Expected key to not exist\n");
919         RegCloseKey(key);
920         /* Create the device information */
921         ret = pSetupDiCreateDeviceInfoA(set, "ROOT\\LEGACY_BOGUS\\0000", &guid,
922                 NULL, NULL, 0, &devInfo);
923         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
924         /* The device info key should have been created */
925         ok(!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key),
926          "Expected registry key to exist\n");
927         RegCloseKey(key);
928         SetLastError(0xdeadbeef);
929         key = pSetupDiOpenDevRegKey(NULL, NULL, 0, 0, 0, 0);
930         ok(!key || key == INVALID_HANDLE_VALUE,
931          "Expected INVALID_HANDLE_VALUE or a NULL key (NT4)\n");
932         ok(GetLastError() == ERROR_INVALID_HANDLE,
933          "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
934         SetLastError(0xdeadbeef);
935         key = pSetupDiOpenDevRegKey(set, NULL, 0, 0, 0, 0);
936         ok(key == INVALID_HANDLE_VALUE &&
937          GetLastError() == ERROR_INVALID_PARAMETER,
938          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
939         SetLastError(0xdeadbeef);
940         key = pSetupDiOpenDevRegKey(set, &devInfo, 0, 0, 0, 0);
941         ok(key == INVALID_HANDLE_VALUE &&
942          GetLastError() == ERROR_INVALID_FLAGS,
943          "Expected ERROR_INVALID_FLAGS, got %d\n", GetLastError());
944         SetLastError(0xdeadbeef);
945         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0, 0, 0);
946         ok(key == INVALID_HANDLE_VALUE &&
947          GetLastError() == ERROR_INVALID_FLAGS,
948          "Expected ERROR_INVALID_FLAGS, got %d\n", GetLastError());
949         SetLastError(0xdeadbeef);
950         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
951          DIREG_BOTH, 0);
952         ok(key == INVALID_HANDLE_VALUE &&
953          GetLastError() == ERROR_INVALID_FLAGS,
954          "Expected ERROR_INVALID_FLAGS, got %d\n", GetLastError());
955         SetLastError(0xdeadbeef);
956         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
957          DIREG_DRV, 0);
958         ok(key == INVALID_HANDLE_VALUE &&
959          GetLastError() == ERROR_DEVINFO_NOT_REGISTERED,
960          "Expected ERROR_DEVINFO_NOT_REGISTERED, got %08x\n", GetLastError());
961         SetLastError(0xdeadbeef);
962         ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL, NULL);
963         ok(ret, "SetupDiRegisterDeviceInfo failed: %08x\n", GetLastError());
964         SetLastError(0xdeadbeef);
965         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
966          DIREG_DRV, 0);
967         /* The software key isn't created by default */
968         todo_wine
969         ok(key == INVALID_HANDLE_VALUE &&
970          GetLastError() == ERROR_KEY_DOES_NOT_EXIST,
971          "Expected ERROR_KEY_DOES_NOT_EXIST, got %08x\n", GetLastError());
972         SetLastError(0xdeadbeef);
973         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
974          DIREG_DEV, 0);
975         todo_wine
976         ok(key == INVALID_HANDLE_VALUE &&
977          GetLastError() == ERROR_KEY_DOES_NOT_EXIST,
978          "Expected ERROR_KEY_DOES_NOT_EXIST, got %08x\n", GetLastError());
979         SetLastError(0xdeadbeef);
980         /* The class key shouldn't be there */
981         res = RegOpenKeyW(HKEY_LOCAL_MACHINE, classKey, &key);
982         todo_wine
983         ok(res != ERROR_SUCCESS, "Expected key to not exist\n");
984         RegCloseKey(key);
985         /* Create the device reg key */
986         key = pSetupDiCreateDevRegKeyW(set, &devInfo, DICS_FLAG_GLOBAL, 0,
987          DIREG_DRV, NULL, NULL);
988         /* Vista and higher don't actually create the key */
989         ok(key != INVALID_HANDLE_VALUE || GetLastError() == ERROR_KEY_DOES_NOT_EXIST,
990          "SetupDiCreateDevRegKey failed: %08x\n", GetLastError());
991         if (key != INVALID_HANDLE_VALUE)
992         {
993             classKeyCreated = TRUE;
994             RegCloseKey(key);
995             /* The class key should have been created */
996             ok(!RegOpenKeyW(HKEY_LOCAL_MACHINE, classKey, &key),
997              "Expected registry key to exist\n");
998             RegCloseKey(key);
999             SetLastError(0xdeadbeef);
1000             key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
1001              DIREG_DRV, 0);
1002             todo_wine
1003             ok(key == INVALID_HANDLE_VALUE &&
1004              (GetLastError() == ERROR_INVALID_DATA ||
1005              GetLastError() == ERROR_ACCESS_DENIED), /* win2k3 */
1006              "Expected ERROR_INVALID_DATA or ERROR_ACCESS_DENIED, got %08x\n", GetLastError());
1007             key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
1008              DIREG_DRV, KEY_READ);
1009             ok(key != INVALID_HANDLE_VALUE, "SetupDiOpenDevRegKey failed: %08x\n",
1010              GetLastError());
1011             pSetupDiDestroyDeviceInfoList(set);
1012         }
1013         else
1014             classKeyCreated = FALSE;
1015
1016         /* Cleanup */
1017         ret = remove_device();
1018         if(!is_wow64)
1019             todo_wine ok(ret, "Expected the device to be removed: %08x\n", GetLastError());
1020
1021         /* FIXME: Only do the RegDeleteKey, once Wine is fixed */
1022         if (!ret)
1023         {
1024             /* Wine doesn't delete the information currently */
1025             trace("We are most likely on Wine\n");
1026             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, bogus);
1027             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, classKey);
1028         }
1029         else if (classKeyCreated)
1030         {
1031             /* There should only be a class key entry, so a simple
1032              * RegDeleteKey should work
1033              *
1034              * This could fail if it's the first time for this new test
1035              * after running the old tests.
1036              */
1037             ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, classKey),
1038              "Couldn't delete classkey\n");
1039         }
1040     }
1041 }
1042
1043 static void testRegisterAndGetDetail(void)
1044 {
1045     HDEVINFO set;
1046     BOOL ret;
1047     SP_DEVINFO_DATA devInfo = { sizeof(SP_DEVINFO_DATA), { 0 } };
1048     SP_DEVICE_INTERFACE_DATA interfaceData = { sizeof(interfaceData), { 0 } };
1049     DWORD dwSize = 0;
1050     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
1051      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1052      'E','n','u','m','\\','R','o','o','t','\\',
1053      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
1054     static const WCHAR devclass[] = {'S','y','s','t','e','m','\\',
1055      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1056      'C','o','n','t','r','o','l','\\','D','e','v','i','c','e','C','l','a','s','s','e','s','\\',
1057      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
1058      '1','1','d','b','-','b','7','0','4','-',
1059      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
1060
1061     if (!pSetupDiCreateDeviceInterfaceA || !pSetupDiEnumDeviceInterfaces ||
1062         !pSetupDiGetDeviceInterfaceDetailA)
1063     {
1064         win_skip("Needed functions are not available\n");
1065         return;
1066     }
1067
1068     SetLastError(0xdeadbeef);
1069     set = pSetupDiGetClassDevsA(&guid, NULL, 0, DIGCF_ALLCLASSES);
1070     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsA failed: %08x\n",
1071      GetLastError());
1072
1073     SetLastError(0xdeadbeef);
1074     ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid, NULL, 0,
1075      DICD_GENERATE_ID, &devInfo);
1076     ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
1077     SetLastError(0xdeadbeef);
1078     ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, NULL, 0, &interfaceData);
1079     ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
1080     SetLastError(0xdeadbeef);
1081     ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL, NULL);
1082     ok(ret, "SetupDiRegisterDeviceInfo failed: %08x\n", GetLastError());
1083
1084     pSetupDiDestroyDeviceInfoList(set);
1085
1086     SetLastError(0xdeadbeef);
1087     set = pSetupDiGetClassDevsA(&guid, NULL, 0, DIGCF_DEVICEINTERFACE);
1088     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsA failed: %08x\n",
1089      GetLastError());
1090
1091     SetLastError(0xdeadbeef);
1092     ret = pSetupDiEnumDeviceInterfaces(set, NULL, &guid, 0, &interfaceData);
1093     ok(ret, "SetupDiEnumDeviceInterfaces failed: %08x\n", GetLastError());
1094     SetLastError(0xdeadbeef);
1095     ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, NULL, 0, &dwSize, NULL);
1096     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1097      "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
1098     if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1099     {
1100         static const char path[] =
1101             "\\\\?\\root#legacy_bogus#0000#{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
1102         static const char path_wow64[] =
1103             "\\\\?\\root#legacy_bogus#0001#{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
1104         static const char path_w2k[] =
1105             "\\\\?\\root#legacy_bogus#0000#{6a55b5a4-3f65-11db-b704-0011955c2bdb}\\";
1106         PSP_DEVICE_INTERFACE_DETAIL_DATA_A detail = NULL;
1107
1108         detail = HeapAlloc(GetProcessHeap(), 0, dwSize);
1109         if (detail)
1110         {
1111             detail->cbSize = sizeof(*detail);
1112             SetLastError(0xdeadbeef);
1113             ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData,
1114              detail, dwSize, &dwSize, NULL);
1115             ok(ret, "SetupDiGetDeviceInterfaceDetailA failed: %08x\n", GetLastError());
1116             /* FIXME: This one only worked because old data wasn't removed properly. As soon
1117              * as all the tests are cleaned up correctly this has to be (or should be) fixed
1118              */
1119             if(is_wow64)
1120                 ok(!lstrcmpiA(path_wow64, detail->DevicePath),
1121                    "Unexpected path %s\n", detail->DevicePath);
1122             else
1123                 todo_wine ok(!lstrcmpiA(path, detail->DevicePath) ||
1124                              !lstrcmpiA(path_w2k, detail->DevicePath),
1125                              "Unexpected path %s\n", detail->DevicePath);
1126             HeapFree(GetProcessHeap(), 0, detail);
1127         }
1128     }
1129
1130     pSetupDiDestroyDeviceInfoList(set);
1131
1132     /* Cleanup */
1133     ret = remove_device();
1134     if(!is_wow64)
1135         todo_wine ok(ret, "Expected the device to be removed: %08x\n", GetLastError());
1136
1137     /* FIXME: Only do the RegDeleteKey, once Wine is fixed */
1138     if (!ret)
1139     {
1140         /* Wine doesn't delete the information currently */
1141         trace("We are most likely on Wine\n");
1142         devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, bogus);
1143         devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, devclass);
1144     }
1145     else
1146     {
1147         clean_devclass_key();
1148     }
1149 }
1150
1151 static void testDeviceRegistryPropertyA(void)
1152 {
1153     HDEVINFO set;
1154     SP_DEVINFO_DATA devInfo = { sizeof(SP_DEVINFO_DATA), { 0 } };
1155     CHAR devName[] = "LEGACY_BOGUS";
1156     CHAR friendlyName[] = "Bogus";
1157     CHAR buf[6] = "";
1158     DWORD buflen = 6;
1159     DWORD size;
1160     DWORD regType;
1161     BOOL ret;
1162     LONG res;
1163     HKEY key;
1164     static const CHAR bogus[] =
1165      "System\\CurrentControlSet\\Enum\\Root\\LEGACY_BOGUS";
1166
1167     SetLastError(0xdeadbeef);
1168     set = pSetupDiGetClassDevsA(&guid, NULL, 0, DIGCF_DEVICEINTERFACE);
1169     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsA failed: %08x\n",
1170      GetLastError());
1171     SetLastError(0xdeadbeef);
1172     ret = pSetupDiCreateDeviceInfoA(set, devName, &guid, NULL, NULL,
1173      DICD_GENERATE_ID, &devInfo);
1174     ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
1175     SetLastError(0xdeadbeef);
1176     ret = pSetupDiSetDeviceRegistryPropertyA(NULL, NULL, -1, NULL, 0);
1177     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
1178      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1179     SetLastError(0xdeadbeef);
1180     ret = pSetupDiSetDeviceRegistryPropertyA(set, NULL, -1, NULL, 0);
1181     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1182      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
1183     SetLastError(0xdeadbeef);
1184     ret = pSetupDiSetDeviceRegistryPropertyA(set, &devInfo, -1, NULL, 0);
1185     todo_wine
1186     ok(!ret && GetLastError() == ERROR_INVALID_REG_PROPERTY,
1187      "Expected ERROR_INVALID_REG_PROPERTY, got %08x\n", GetLastError());
1188     /* GetLastError() returns nonsense in win2k3 */
1189     ret = pSetupDiSetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1190      NULL, 0);
1191     todo_wine
1192     ok(!ret, "Expected failure, got %d\n", ret);
1193     SetLastError(0xdeadbeef);
1194     ret = pSetupDiSetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1195      (PBYTE)friendlyName, buflen);
1196     ok(ret, "SetupDiSetDeviceRegistryPropertyA failed: %08x\n", GetLastError());
1197     SetLastError(0xdeadbeef);
1198     ret = pSetupDiGetDeviceRegistryPropertyA(NULL, NULL, -1, NULL, NULL, 0, NULL);
1199     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
1200      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1201     SetLastError(0xdeadbeef);
1202     ret = pSetupDiGetDeviceRegistryPropertyA(set, NULL, -1, NULL, NULL, 0, NULL);
1203     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1204      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
1205     SetLastError(0xdeadbeef);
1206     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, -1, NULL, NULL, 0, NULL);
1207     todo_wine
1208     ok(!ret && GetLastError() == ERROR_INVALID_REG_PROPERTY,
1209      "Expected ERROR_INVALID_REG_PROPERTY, got %08x\n", GetLastError());
1210     /* GetLastError() returns nonsense in win2k3 */
1211     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1212      NULL, NULL, buflen, NULL);
1213     ok(!ret, "Expected failure, got %d\n", ret);
1214     SetLastError(0xdeadbeef);
1215     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1216      NULL, NULL, 0, &size);
1217     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1218      "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
1219     ok(buflen == size, "Unexpected size: %d\n", size);
1220     SetLastError(0xdeadbeef);
1221     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1222      NULL, (PBYTE)buf, buflen, NULL);
1223     ok(ret, "SetupDiGetDeviceRegistryPropertyA failed: %08x\n", GetLastError());
1224     ok(!lstrcmpiA(friendlyName, buf), "Unexpected property\n");
1225     SetLastError(0xdeadbeef);
1226     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1227      &regType, (PBYTE)buf, buflen, NULL);
1228     ok(ret, "SetupDiGetDeviceRegistryPropertyA failed: %08x\n", GetLastError());
1229     ok(!lstrcmpiA(friendlyName, buf), "Unexpected value of property\n");
1230     ok(regType == REG_SZ, "Unexpected type of property: %d\n", regType);
1231     SetLastError(0xdeadbeef);
1232     ret = pSetupDiSetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1233      NULL, 0);
1234     ok(ret, "SetupDiSetDeviceRegistryPropertyA failed: %08x\n", GetLastError());
1235     SetLastError(0xdeadbeef);
1236     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1237      NULL, (PBYTE)buf, buflen, &size);
1238     todo_wine
1239     ok(!ret && GetLastError() == ERROR_INVALID_DATA,
1240      "Expected ERROR_INVALID_DATA, got %08x\n", GetLastError());
1241     pSetupDiDestroyDeviceInfoList(set);
1242
1243     res = RegOpenKeyA(HKEY_LOCAL_MACHINE, bogus, &key);
1244     if(!is_wow64)
1245         todo_wine ok(res == ERROR_FILE_NOT_FOUND, "Expected key to not exist\n");
1246     /* FIXME: Remove when Wine is fixed */
1247     if (res == ERROR_SUCCESS)
1248     {
1249         /* Wine doesn't delete the information currently */
1250         trace("We are most likely on Wine\n");
1251         RegDeleteKeyA(HKEY_LOCAL_MACHINE, bogus);
1252     }
1253 }
1254
1255 static void testDeviceRegistryPropertyW(void)
1256 {
1257     HDEVINFO set;
1258     SP_DEVINFO_DATA devInfo = { sizeof(SP_DEVINFO_DATA), { 0 } };
1259     WCHAR devName[] = {'L','E','G','A','C','Y','_','B','O','G','U','S',0};
1260     WCHAR friendlyName[] = {'B','o','g','u','s',0};
1261     WCHAR buf[6] = {0};
1262     DWORD buflen = 6 * sizeof(WCHAR);
1263     DWORD size;
1264     DWORD regType;
1265     BOOL ret;
1266     LONG res;
1267     HKEY key;
1268     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
1269      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1270      'E','n','u','m','\\','R','o','o','t','\\',
1271      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
1272
1273     SetLastError(0xdeadbeef);
1274     set = pSetupDiGetClassDevsW(&guid, NULL, 0, DIGCF_DEVICEINTERFACE);
1275     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsW failed: %08x\n",
1276      GetLastError());
1277     SetLastError(0xdeadbeef);
1278     ret = pSetupDiCreateDeviceInfoW(set, devName, &guid, NULL, NULL,
1279      DICD_GENERATE_ID, &devInfo);
1280     ok(ret, "SetupDiCreateDeviceInfoW failed: %08x\n", GetLastError());
1281     SetLastError(0xdeadbeef);
1282     ret = pSetupDiSetDeviceRegistryPropertyW(NULL, NULL, -1, NULL, 0);
1283     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
1284      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1285     SetLastError(0xdeadbeef);
1286     ret = pSetupDiSetDeviceRegistryPropertyW(set, NULL, -1, NULL, 0);
1287     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1288      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
1289     SetLastError(0xdeadbeef);
1290     ret = pSetupDiSetDeviceRegistryPropertyW(set, &devInfo, -1, NULL, 0);
1291     todo_wine
1292     ok(!ret && GetLastError() == ERROR_INVALID_REG_PROPERTY,
1293      "Expected ERROR_INVALID_REG_PROPERTY, got %08x\n", GetLastError());
1294     /* GetLastError() returns nonsense in win2k3 */
1295     ret = pSetupDiSetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1296      NULL, 0);
1297     todo_wine
1298     ok(!ret, "Expected failure, got %d\n", ret);
1299     SetLastError(0xdeadbeef);
1300     ret = pSetupDiSetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1301      (PBYTE)friendlyName, buflen);
1302     ok(ret, "SetupDiSetDeviceRegistryPropertyW failed: %08x\n", GetLastError());
1303     SetLastError(0xdeadbeef);
1304     ret = pSetupDiGetDeviceRegistryPropertyW(NULL, NULL, -1, NULL, NULL, 0, NULL);
1305     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
1306      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1307     SetLastError(0xdeadbeef);
1308     ret = pSetupDiGetDeviceRegistryPropertyW(set, NULL, -1, NULL, NULL, 0, NULL);
1309     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1310      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
1311     SetLastError(0xdeadbeef);
1312     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, -1, NULL, NULL, 0, NULL);
1313     todo_wine
1314     ok(!ret && GetLastError() == ERROR_INVALID_REG_PROPERTY,
1315      "Expected ERROR_INVALID_REG_PROPERTY, got %08x\n", GetLastError());
1316     /* GetLastError() returns nonsense in win2k3 */
1317     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1318      NULL, NULL, buflen, NULL);
1319     ok(!ret, "Expected failure, got %d\n", ret);
1320     SetLastError(0xdeadbeef);
1321     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1322      NULL, NULL, 0, &size);
1323     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1324      "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
1325     ok(buflen == size, "Unexpected size: %d\n", size);
1326     SetLastError(0xdeadbeef);
1327     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1328      NULL, (PBYTE)buf, buflen, NULL);
1329     ok(ret, "SetupDiGetDeviceRegistryPropertyW failed: %08x\n", GetLastError());
1330     ok(!lstrcmpiW(friendlyName, buf), "Unexpected property\n");
1331     SetLastError(0xdeadbeef);
1332     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1333      &regType, (PBYTE)buf, buflen, NULL);
1334     ok(ret, "SetupDiGetDeviceRegistryPropertyW failed: %08x\n", GetLastError());
1335     ok(!lstrcmpiW(friendlyName, buf), "Unexpected value of property\n");
1336     ok(regType == REG_SZ, "Unexpected type of property: %d\n", regType);
1337     SetLastError(0xdeadbeef);
1338     ret = pSetupDiSetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1339      NULL, 0);
1340     ok(ret, "SetupDiSetDeviceRegistryPropertyW failed: %08x\n", GetLastError());
1341     SetLastError(0xdeadbeef);
1342     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1343      NULL, (PBYTE)buf, buflen, &size);
1344     todo_wine
1345     ok(!ret && GetLastError() == ERROR_INVALID_DATA,
1346      "Expected ERROR_INVALID_DATA, got %08x\n", GetLastError());
1347     pSetupDiDestroyDeviceInfoList(set);
1348
1349     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key);
1350     if(!is_wow64)
1351         todo_wine ok(res == ERROR_FILE_NOT_FOUND, "Expected key to not exist\n");
1352     /* FIXME: Remove when Wine is fixed */
1353     if (res == ERROR_SUCCESS)
1354     {
1355         /* Wine doesn't delete the information currently */
1356         trace("We are most likely on Wine\n");
1357         RegDeleteKeyW(HKEY_LOCAL_MACHINE, bogus);
1358     }
1359 }
1360
1361 static void testSetupDiGetINFClassA(void)
1362 {
1363     static const char inffile[] = "winetest.inf";
1364     static const char content[] = "[Version]\r\n\r\n";
1365
1366     char cn[MAX_PATH];
1367     char filename[MAX_PATH];
1368     DWORD count;
1369     BOOL retval;
1370     GUID guid;
1371     HANDLE h;
1372
1373     if(!pSetupDiGetINFClassA)
1374     {
1375         win_skip("SetupDiGetINFClassA not present\n");
1376         return;
1377     }
1378
1379     count = GetTempPathA(MAX_PATH, filename);
1380     if(!count)
1381     {
1382         win_skip("GetTempPathA failed\n");
1383         return;
1384     }
1385
1386     strcat(filename, inffile);
1387     DeleteFileA(filename);
1388
1389     /* not existing file */
1390     SetLastError(0xdeadbeef);
1391     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1392     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1393     if (ERROR_CALL_NOT_IMPLEMENTED == GetLastError())
1394     {
1395         skip("SetupDiGetINFClassA is not implemented\n");
1396         return;
1397     }
1398     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1399         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1400
1401     /* missing file wins against other invalid parameter */
1402     SetLastError(0xdeadbeef);
1403     retval = SetupDiGetINFClassA(filename, NULL, cn, MAX_PATH, &count);
1404     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1405     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1406         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1407
1408     SetLastError(0xdeadbeef);
1409     retval = SetupDiGetINFClassA(filename, &guid, NULL, MAX_PATH, &count);
1410     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1411     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1412         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1413
1414     SetLastError(0xdeadbeef);
1415     retval = SetupDiGetINFClassA(filename, &guid, cn, 0, &count);
1416     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1417     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1418         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1419
1420     SetLastError(0xdeadbeef);
1421     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, NULL);
1422     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1423     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1424         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1425
1426     /* test file content */
1427     h = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1428                     FILE_ATTRIBUTE_NORMAL, NULL);
1429     if(h == INVALID_HANDLE_VALUE)
1430     {
1431         win_skip("failed to create file %s (error %u)\n", filename, GetLastError());
1432         return;
1433     }
1434     CloseHandle( h);
1435
1436     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1437     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1438
1439     h = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1440                     FILE_ATTRIBUTE_NORMAL, NULL);
1441     if(h == INVALID_HANDLE_VALUE)
1442     {
1443         win_skip("failed to create file %s (error %u)\n", filename, GetLastError());
1444         return;
1445     }
1446     WriteFile( h, content, sizeof(content), &count, NULL);
1447     CloseHandle( h);
1448
1449     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1450     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1451
1452     WritePrivateProfileStringA("Version", "Signature", "\"$CHICAGO$\"", filename);
1453
1454     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1455     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1456
1457     WritePrivateProfileStringA("Version", "Class", "WINE", filename);
1458
1459     count = 0xdeadbeef;
1460     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1461     ok(retval, "expected SetupDiGetINFClassA to succeed! error %u\n", GetLastError());
1462     ok(count == 5, "expected count==5, got %u\n", count);
1463
1464     count = 0xdeadbeef;
1465     retval = SetupDiGetINFClassA(filename, &guid, cn, 5, &count);
1466     ok(retval, "expected SetupDiGetINFClassA to succeed! error %u\n", GetLastError());
1467     ok(count == 5, "expected count==5, got %u\n", count);
1468
1469     count = 0xdeadbeef;
1470     SetLastError(0xdeadbeef);
1471     retval = SetupDiGetINFClassA(filename, &guid, cn, 4, &count);
1472     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1473     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(),
1474         "expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
1475     ok(count == 5, "expected count==5, got %u\n", count);
1476
1477     /* invalid parameter */
1478     SetLastError(0xdeadbeef);
1479     retval = SetupDiGetINFClassA(NULL, &guid, cn, MAX_PATH, &count);
1480     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1481     ok(ERROR_INVALID_PARAMETER == GetLastError(),
1482         "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1483
1484     SetLastError(0xdeadbeef);
1485     retval = SetupDiGetINFClassA(filename, NULL, cn, MAX_PATH, &count);
1486     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1487     ok(ERROR_INVALID_PARAMETER == GetLastError(),
1488         "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1489
1490     SetLastError(0xdeadbeef);
1491     retval = SetupDiGetINFClassA(filename, &guid, NULL, MAX_PATH, &count);
1492     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1493     ok(ERROR_INVALID_PARAMETER == GetLastError(),
1494         "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1495
1496     SetLastError(0xdeadbeef);
1497     retval = SetupDiGetINFClassA(filename, &guid, cn, 0, &count);
1498     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1499     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError() ||
1500        ERROR_INVALID_PARAMETER == GetLastError(),
1501         "expected error ERROR_INSUFFICIENT_BUFFER or ERROR_INVALID_PARAMETER, "
1502         "got %u\n", GetLastError());
1503
1504     DeleteFileA(filename);
1505
1506     WritePrivateProfileStringA("Version", "Signature", "\"$CHICAGO$\"", filename);
1507     WritePrivateProfileStringA("Version", "ClassGUID", "WINE", filename);
1508
1509     SetLastError(0xdeadbeef);
1510     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1511     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1512     ok(RPC_S_INVALID_STRING_UUID == GetLastError() ||
1513        ERROR_INVALID_PARAMETER == GetLastError(),
1514         "expected error RPC_S_INVALID_STRING_UUID or ERROR_INVALID_PARAMETER, "
1515         "got %u\n", GetLastError());
1516
1517     /* network adapter guid */
1518     WritePrivateProfileStringA("Version", "ClassGUID",
1519                                "{4d36e972-e325-11ce-bfc1-08002be10318}", filename);
1520
1521     /* this test succeeds only if the guid is known to the system */
1522     count = 0xdeadbeef;
1523     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1524     ok(retval, "expected SetupDiGetINFClassA to succeed! error %u\n", GetLastError());
1525     todo_wine
1526     ok(count == 4, "expected count==4, got %u(%s)\n", count, cn);
1527
1528     DeleteFileA(filename);
1529 }
1530
1531 START_TEST(devinst)
1532 {
1533     HDEVINFO set;
1534
1535      init_function_pointers();
1536
1537     /* Win9x/WinMe does things totally different so we skip all the tests
1538      *
1539      * We don't want to exclude NT4 so hence this check.
1540      */
1541     SetLastError(0xdeadbeef);
1542     set = pSetupDiGetClassDevsW(NULL, NULL, 0, 0);
1543     if (set == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1544     {
1545         win_skip("Win9x/WinMe has totally different behavior\n");
1546         return;
1547     }
1548
1549     if (pIsWow64Process)
1550         pIsWow64Process(GetCurrentProcess(), &is_wow64);
1551
1552     if (pSetupDiCreateDeviceInfoListExW)
1553         test_SetupDiCreateDeviceInfoListEx();
1554     else
1555         win_skip("SetupDiCreateDeviceInfoListExW is not available\n");
1556
1557     if (pSetupDiOpenClassRegKeyExA)
1558         test_SetupDiOpenClassRegKeyExA();
1559     else
1560         win_skip("SetupDiOpenClassRegKeyExA is not available\n");
1561
1562     testInstallClass();
1563     testCreateDeviceInfo();
1564     testGetDeviceInstanceId();
1565     testRegisterDeviceInfo();
1566     testCreateDeviceInterface();
1567     testGetDeviceInterfaceDetail();
1568     testDevRegKey();
1569     testRegisterAndGetDetail();
1570     testDeviceRegistryPropertyA();
1571     testDeviceRegistryPropertyW();
1572     testSetupDiGetINFClassA();
1573 }