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