setupapi/tests: Add a test for installing a driver service via an INF file.
[wine] / dlls / setupapi / devinst.c
1 /*
2  * SetupAPI device installer
3  *
4  * Copyright 2000 Andreas Mohr for CodeWeavers
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 "config.h"
22 #include "wine/port.h"
23  
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnt.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "setupapi.h"
35 #include "wine/debug.h"
36 #include "wine/list.h"
37 #include "wine/unicode.h"
38 #include "cfgmgr32.h"
39 #include "initguid.h"
40 #include "winioctl.h"
41 #include "rpc.h"
42 #include "rpcdce.h"
43
44 #include "setupapi_private.h"
45
46
47 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
48
49 /* Unicode constants */
50 static const WCHAR ClassGUID[]  = {'C','l','a','s','s','G','U','I','D',0};
51 static const WCHAR Class[]  = {'C','l','a','s','s',0};
52 static const WCHAR ClassInstall32[]  = {'C','l','a','s','s','I','n','s','t','a','l','l','3','2',0};
53 static const WCHAR NoDisplayClass[]  = {'N','o','D','i','s','p','l','a','y','C','l','a','s','s',0};
54 static const WCHAR NoInstallClass[]  = {'N','o','I','n','s','t','a','l','l','C','l','a','s','s',0};
55 static const WCHAR NoUseClass[]  = {'N','o','U','s','e','C','l','a','s','s',0};
56 static const WCHAR NtExtension[]  = {'.','N','T',0};
57 static const WCHAR NtPlatformExtension[]  = {'.','N','T','x','8','6',0};
58 static const WCHAR Version[]  = {'V','e','r','s','i','o','n',0};
59 static const WCHAR WinExtension[]  = {'.','W','i','n',0};
60
61 /* Registry key and value names */
62 static const WCHAR ControlClass[] = {'S','y','s','t','e','m','\\',
63                                   'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
64                                   'C','o','n','t','r','o','l','\\',
65                                   'C','l','a','s','s',0};
66
67 static const WCHAR DeviceClasses[] = {'S','y','s','t','e','m','\\',
68                                   'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
69                                   'C','o','n','t','r','o','l','\\',
70                                   'D','e','v','i','c','e','C','l','a','s','s','e','s',0};
71 static const WCHAR Enum[] = {'S','y','s','t','e','m','\\',
72                                   'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
73                                   'E','n','u','m',0};
74 static const WCHAR DeviceDesc[] = {'D','e','v','i','c','e','D','e','s','c',0};
75 static const WCHAR DeviceInstance[] = {'D','e','v','i','c','e','I','n','s','t','a','n','c','e',0};
76 static const WCHAR HardwareId[] = {'H','a','r','d','w','a','r','e','I','D',0};
77 static const WCHAR CompatibleIDs[] = {'C','o','m','p','a','t','i','b','l','e','I','d','s',0};
78 static const WCHAR Service[] = {'S','e','r','v','i','c','e',0};
79 static const WCHAR Driver[] = {'D','r','i','v','e','r',0};
80 static const WCHAR ConfigFlags[] = {'C','o','n','f','i','g','F','l','a','g','s',0};
81 static const WCHAR Mfg[] = {'M','f','g',0};
82 static const WCHAR FriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
83 static const WCHAR LocationInformation[] = {'L','o','c','a','t','i','o','n','I','n','f','o','r','m','a','t','i','o','n',0};
84 static const WCHAR Capabilities[] = {'C','a','p','a','b','i','l','i','t','i','e','s',0};
85 static const WCHAR UINumber[] = {'U','I','N','u','m','b','e','r',0};
86 static const WCHAR UpperFilters[] = {'U','p','p','e','r','F','i','l','t','e','r','s',0};
87 static const WCHAR LowerFilters[] = {'L','o','w','e','r','F','i','l','t','e','r','s',0};
88 static const WCHAR Phantom[] = {'P','h','a','n','t','o','m',0};
89 static const WCHAR SymbolicLink[] = {'S','y','m','b','o','l','i','c','L','i','n','k',0};
90
91 /* is used to identify if a DeviceInfoSet pointer is
92 valid or not */
93 #define SETUP_DEVICE_INFO_SET_MAGIC 0xd00ff056
94
95 struct DeviceInfoSet
96 {
97     DWORD magic;        /* if is equal to SETUP_DEVICE_INFO_SET_MAGIC struct is okay */
98     GUID ClassGuid;
99     HWND hwndParent;
100     DWORD cDevices;
101     SP_DEVINFO_DATA *devices;
102 };
103
104 /* Pointed to by SP_DEVICE_INTERFACE_DATA's Reserved member */
105 struct InterfaceInfo
106 {
107     LPWSTR           referenceString;
108     LPWSTR           symbolicLink;
109     PSP_DEVINFO_DATA device;
110 };
111
112 /* A device may have multiple instances of the same interface, so this holds
113  * each instance belonging to a particular interface.
114  */
115 struct InterfaceInstances
116 {
117     GUID                      guid;
118     DWORD                     cInstances;
119     DWORD                     cInstancesAllocated;
120     SP_DEVICE_INTERFACE_DATA *instances;
121     struct list               entry;
122 };
123
124 /* Pointed to by SP_DEVINFO_DATA's Reserved member */
125 struct DeviceInfo
126 {
127     struct DeviceInfoSet *set;
128     HKEY                  key;
129     BOOL                  phantom;
130     DWORD                 devId;
131     LPWSTR                instanceId;
132     struct list           interfaces;
133 };
134
135 static void SETUPDI_GuidToString(const GUID *guid, LPWSTR guidStr)
136 {
137     static const WCHAR fmt[] = {'{','%','0','8','X','-','%','0','4','X','-',
138         '%','0','4','X','-','%','0','2','X','%','0','2','X','-','%','0','2',
139         'X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%',
140         '0','2','X','}',0};
141
142     sprintfW(guidStr, fmt, guid->Data1, guid->Data2, guid->Data3,
143         guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
144         guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
145 }
146
147 static void SETUPDI_FreeInterfaceInstances(struct InterfaceInstances *instances)
148 {
149     DWORD i;
150
151     for (i = 0; i < instances->cInstances; i++)
152     {
153         struct InterfaceInfo *ifaceInfo =
154             (struct InterfaceInfo *)instances->instances[i].Reserved;
155
156         if (ifaceInfo->device && ifaceInfo->device->Reserved)
157         {
158             struct DeviceInfo *devInfo =
159                 (struct DeviceInfo *)ifaceInfo->device->Reserved;
160
161             if (devInfo->phantom)
162                 SetupDiDeleteDeviceInterfaceRegKey(devInfo->set,
163                         &instances->instances[i], 0);
164         }
165         HeapFree(GetProcessHeap(), 0, ifaceInfo->referenceString);
166         HeapFree(GetProcessHeap(), 0, ifaceInfo->symbolicLink);
167         HeapFree(GetProcessHeap(), 0, ifaceInfo);
168     }
169     HeapFree(GetProcessHeap(), 0, instances->instances);
170 }
171
172 /* Finds the interface with interface class InterfaceClassGuid in the device.
173  * Returns TRUE if found, and updates *interface to point to device's
174  * interfaces member where the given interface was found.
175  * Returns FALSE if not found.
176  */
177 static BOOL SETUPDI_FindInterface(const struct DeviceInfo *devInfo,
178         const GUID *InterfaceClassGuid, struct InterfaceInstances **interface)
179 {
180     BOOL found = FALSE;
181     struct InterfaceInstances *iface;
182
183     TRACE("%s\n", debugstr_guid(InterfaceClassGuid));
184
185     LIST_FOR_EACH_ENTRY(iface, &devInfo->interfaces, struct InterfaceInstances,
186             entry)
187     {
188         if (IsEqualGUID(&iface->guid, InterfaceClassGuid))
189         {
190             *interface = iface;
191             found = TRUE;
192             break;
193         }
194     }
195     TRACE("returning %d (%p)\n", found, found ? *interface : NULL);
196     return found;
197 }
198
199 /* Finds the interface instance with reference string ReferenceString in the
200  * interface instance map.  Returns TRUE if found, and updates instanceIndex to
201  * the index of the interface instance's instances member
202  * where the given instance was found.  Returns FALSE if not found.
203  */
204 static BOOL SETUPDI_FindInterfaceInstance(
205         const struct InterfaceInstances *instances,
206         LPCWSTR ReferenceString, DWORD *instanceIndex)
207 {
208     BOOL found = FALSE;
209     DWORD i;
210
211     TRACE("%s\n", debugstr_w(ReferenceString));
212
213     for (i = 0; !found && i < instances->cInstances; i++)
214     {
215         SP_DEVICE_INTERFACE_DATA *ifaceData = &instances->instances[i];
216         struct InterfaceInfo *ifaceInfo =
217             (struct InterfaceInfo *)ifaceData->Reserved;
218
219         if (!ReferenceString && !ifaceInfo->referenceString)
220         {
221             *instanceIndex = i;
222             found = TRUE;
223         }
224         else if (ReferenceString && ifaceInfo->referenceString &&
225                 !lstrcmpiW(ifaceInfo->referenceString, ReferenceString))
226         {
227             *instanceIndex = i;
228             found = TRUE;
229         }
230     }
231     TRACE("returning %d (%d)\n", found, found ? *instanceIndex : 0);
232     return found;
233 }
234
235 static LPWSTR SETUPDI_CreateSymbolicLinkPath(LPCWSTR instanceId,
236         const GUID *InterfaceClassGuid, LPCWSTR ReferenceString)
237 {
238     static const WCHAR fmt[] = {'\\','\\','?','\\','%','s','#','%','s',0};
239     WCHAR guidStr[39];
240     DWORD len;
241     LPWSTR ret;
242
243     SETUPDI_GuidToString(InterfaceClassGuid, guidStr);
244     /* omit length of format specifiers, but include NULL terminator: */
245     len = lstrlenW(fmt) - 4 + 1;
246     len += lstrlenW(instanceId) + lstrlenW(guidStr);
247     if (ReferenceString)
248     {
249         /* space for a hash between string and reference string: */
250         len += lstrlenW(ReferenceString) + 1;
251     }
252     ret = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
253     if (ret)
254     {
255         int printed = sprintfW(ret, fmt, instanceId, guidStr);
256         LPWSTR ptr;
257
258         /* replace '\\' with '#' after the "\\\\?\\" beginning */
259         for (ptr = strchrW(ret + 4, '\\'); ptr; ptr = strchrW(ptr + 1, '\\'))
260             *ptr = '#';
261         if (ReferenceString)
262         {
263             ret[printed - 1] = '\\';
264             lstrcpyW(ret + printed, ReferenceString);
265         }
266     }
267     return ret;
268 }
269
270 /* Adds an interface with the given interface class and reference string to
271  * the device, if it doesn't already exist in the device.  If iface is not
272  * NULL, returns a pointer to the newly added (or already existing) interface.
273  */
274 static BOOL SETUPDI_AddInterfaceInstance(PSP_DEVINFO_DATA DeviceInfoData,
275         const GUID *InterfaceClassGuid, LPCWSTR ReferenceString,
276         SP_DEVICE_INTERFACE_DATA **ifaceData)
277 {
278     struct DeviceInfo *devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
279     BOOL newInterface = FALSE, ret;
280     struct InterfaceInstances *iface = NULL;
281
282     TRACE("%p %s %s %p\n", devInfo, debugstr_guid(InterfaceClassGuid),
283             debugstr_w(ReferenceString), iface);
284
285     if (!(ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
286     {
287         iface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
288                 sizeof(struct InterfaceInstances));
289         if (iface)
290         {
291             list_add_tail(&devInfo->interfaces, &iface->entry);
292             newInterface = TRUE;
293         }
294     }
295     if (iface)
296     {
297         DWORD instanceIndex = 0;
298
299         if (!(ret = SETUPDI_FindInterfaceInstance(iface, ReferenceString,
300                         &instanceIndex)))
301         {
302             SP_DEVICE_INTERFACE_DATA *instance = NULL;
303
304             if (!iface->cInstancesAllocated)
305             {
306                 iface->instances = HeapAlloc(GetProcessHeap(), 0,
307                         sizeof(SP_DEVICE_INTERFACE_DATA));
308                 if (iface->instances)
309                     instance = &iface->instances[iface->cInstancesAllocated++];
310             }
311             else if (iface->cInstances == iface->cInstancesAllocated)
312             {
313                 iface->instances = HeapReAlloc(GetProcessHeap(), 0,
314                         iface->instances,
315                         (iface->cInstancesAllocated + 1) *
316                         sizeof(SP_DEVICE_INTERFACE_DATA));
317                 if (iface->instances)
318                     instance = &iface->instances[iface->cInstancesAllocated++];
319             }
320             else
321                 instance = &iface->instances[iface->cInstances];
322             if (instance)
323             {
324                 struct InterfaceInfo *ifaceInfo = HeapAlloc(GetProcessHeap(),
325                         0, sizeof(struct InterfaceInfo));
326
327                 if (ifaceInfo)
328                 {
329                     ret = TRUE;
330                     ifaceInfo->device = DeviceInfoData;
331                     ifaceInfo->symbolicLink = SETUPDI_CreateSymbolicLinkPath(
332                             devInfo->instanceId, InterfaceClassGuid,
333                             ReferenceString);
334                     if (ReferenceString)
335                     {
336                         ifaceInfo->referenceString =
337                             HeapAlloc(GetProcessHeap(), 0,
338                                 (lstrlenW(ReferenceString) + 1) *
339                                 sizeof(WCHAR));
340                         if (ifaceInfo->referenceString)
341                             lstrcpyW(ifaceInfo->referenceString,
342                                     ReferenceString);
343                         else
344                             ret = FALSE;
345                     }
346                     else
347                         ifaceInfo->referenceString = NULL;
348                     if (ret)
349                     {
350                         HKEY key;
351
352                         iface->cInstances++;
353                         instance->cbSize =
354                             sizeof(SP_DEVICE_INTERFACE_DATA);
355                         instance->InterfaceClassGuid = *InterfaceClassGuid;
356                         instance->Flags = SPINT_ACTIVE; /* FIXME */
357                         instance->Reserved = (ULONG_PTR)ifaceInfo;
358                         if (newInterface)
359                             iface->guid = *InterfaceClassGuid;
360                         key = SetupDiCreateDeviceInterfaceRegKeyW(devInfo->set,
361                                 instance, 0, KEY_WRITE, NULL, NULL);
362                         if (key != INVALID_HANDLE_VALUE)
363                         {
364                             RegSetValueExW(key, SymbolicLink, 0, REG_SZ,
365                                     (BYTE *)ifaceInfo->symbolicLink,
366                                     lstrlenW(ifaceInfo->symbolicLink) *
367                                     sizeof(WCHAR));
368                             RegCloseKey(key);
369                         }
370                         if (ifaceData)
371                             *ifaceData = instance;
372                     }
373                     else
374                         HeapFree(GetProcessHeap(), 0, ifaceInfo);
375                 }
376             }
377         }
378         else
379         {
380             if (ifaceData)
381                 *ifaceData = &iface->instances[instanceIndex];
382         }
383     }
384     else
385         ret = FALSE;
386     TRACE("returning %d\n", ret);
387     return ret;
388 }
389
390 static BOOL SETUPDI_SetInterfaceSymbolicLink(SP_DEVICE_INTERFACE_DATA *iface,
391         LPCWSTR symbolicLink)
392 {
393     struct InterfaceInfo *info = (struct InterfaceInfo *)iface->Reserved;
394     BOOL ret = FALSE;
395
396     if (info)
397     {
398         HeapFree(GetProcessHeap(), 0, info->symbolicLink);
399         info->symbolicLink = HeapAlloc(GetProcessHeap(), 0,
400                 (lstrlenW(symbolicLink) + 1) * sizeof(WCHAR));
401         if (info->symbolicLink)
402         {
403             lstrcpyW(info->symbolicLink, symbolicLink);
404             ret = TRUE;
405         }
406     }
407     return ret;
408 }
409
410 static HKEY SETUPDI_CreateDevKey(struct DeviceInfo *devInfo)
411 {
412     HKEY enumKey, key = INVALID_HANDLE_VALUE;
413     LONG l;
414
415     l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
416             NULL, &enumKey, NULL);
417     if (!l)
418     {
419         RegCreateKeyExW(enumKey, devInfo->instanceId, 0, NULL, 0,
420                 KEY_READ | KEY_WRITE, NULL, &key, NULL);
421         RegCloseKey(enumKey);
422     }
423     return key;
424 }
425
426 static HKEY SETUPDI_CreateDrvKey(struct DeviceInfo *devInfo)
427 {
428     static const WCHAR slash[] = { '\\',0 };
429     WCHAR classKeyPath[MAX_PATH];
430     HKEY classKey, key = INVALID_HANDLE_VALUE;
431     LONG l;
432
433     lstrcpyW(classKeyPath, ControlClass);
434     lstrcatW(classKeyPath, slash);
435     SETUPDI_GuidToString(&devInfo->set->ClassGuid,
436             classKeyPath + lstrlenW(classKeyPath));
437     l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
438             KEY_ALL_ACCESS, NULL, &classKey, NULL);
439     if (!l)
440     {
441         static const WCHAR fmt[] = { '%','0','4','u',0 };
442         WCHAR devId[10];
443
444         sprintfW(devId, fmt, devInfo->devId);
445         RegCreateKeyExW(classKey, devId, 0, NULL, 0, KEY_READ | KEY_WRITE,
446                 NULL, &key, NULL);
447         RegCloseKey(classKey);
448     }
449     return key;
450 }
451
452 static struct DeviceInfo *SETUPDI_AllocateDeviceInfo(struct DeviceInfoSet *set,
453         DWORD devId, LPCWSTR instanceId, BOOL phantom)
454 {
455     struct DeviceInfo *devInfo = NULL;
456     HANDLE devInst = GlobalAlloc(GMEM_FIXED, sizeof(struct DeviceInfo));
457     if (devInst)
458         devInfo = GlobalLock(devInst);
459
460     if (devInfo)
461     {
462         devInfo->set = set;
463         devInfo->devId = (DWORD)devInst;
464
465         devInfo->instanceId = HeapAlloc(GetProcessHeap(), 0,
466                 (lstrlenW(instanceId) + 1) * sizeof(WCHAR));
467         if (devInfo->instanceId)
468         {
469             devInfo->key = INVALID_HANDLE_VALUE;
470             devInfo->phantom = phantom;
471             lstrcpyW(devInfo->instanceId, instanceId);
472             struprW(devInfo->instanceId);
473             devInfo->key = SETUPDI_CreateDevKey(devInfo);
474             if (devInfo->key != INVALID_HANDLE_VALUE)
475             {
476                 if (phantom)
477                     RegSetValueExW(devInfo->key, Phantom, 0, REG_DWORD,
478                             (LPBYTE)&phantom, sizeof(phantom));
479             }
480             list_init(&devInfo->interfaces);
481             GlobalUnlock(devInst);
482         }
483         else
484         {
485             GlobalUnlock(devInst);
486             GlobalFree(devInst);
487             devInfo = NULL;
488         }
489     }
490     return devInfo;
491 }
492
493 static void SETUPDI_FreeDeviceInfo(struct DeviceInfo *devInfo)
494 {
495     struct InterfaceInstances *iface, *next;
496
497     if (devInfo->key != INVALID_HANDLE_VALUE)
498         RegCloseKey(devInfo->key);
499     if (devInfo->phantom)
500     {
501         HKEY enumKey;
502         LONG l;
503
504         l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0,
505                 KEY_ALL_ACCESS, NULL, &enumKey, NULL);
506         if (!l)
507         {
508             RegDeleteTreeW(enumKey, devInfo->instanceId);
509             RegCloseKey(enumKey);
510         }
511     }
512     HeapFree(GetProcessHeap(), 0, devInfo->instanceId);
513     LIST_FOR_EACH_ENTRY_SAFE(iface, next, &devInfo->interfaces,
514             struct InterfaceInstances, entry)
515     {
516         list_remove(&iface->entry);
517         SETUPDI_FreeInterfaceInstances(iface);
518         HeapFree(GetProcessHeap(), 0, iface);
519     }
520     GlobalFree((HANDLE)devInfo->devId);
521 }
522
523 /* Adds a device with GUID guid and identifer devInst to set.  Allocates a
524  * struct DeviceInfo, and points the returned device info's Reserved member
525  * to it.  "Phantom" devices are deleted from the registry when closed.
526  * Returns a pointer to the newly allocated device info.
527  */
528 static BOOL SETUPDI_AddDeviceToSet(struct DeviceInfoSet *set,
529         const GUID *guid,
530         DWORD devInst,
531         LPCWSTR instanceId,
532         BOOL phantom,
533         SP_DEVINFO_DATA **dev)
534 {
535     BOOL ret = FALSE;
536     struct DeviceInfo *devInfo = SETUPDI_AllocateDeviceInfo(set, set->cDevices,
537             instanceId, phantom);
538
539     TRACE("%p, %s, %d, %s, %d\n", set, debugstr_guid(guid), devInst,
540             debugstr_w(instanceId), phantom);
541
542     if (devInfo)
543     {
544         if (set->devices)
545             set->devices = HeapReAlloc(GetProcessHeap(), 0, set->devices,
546                     (set->cDevices + 1) * sizeof(SP_DEVINFO_DATA));
547         else
548             set->devices = HeapAlloc(GetProcessHeap(), 0,
549                     sizeof(SP_DEVINFO_DATA));
550         if (set->devices)
551         {
552             WCHAR classGuidStr[39];
553             SP_DEVINFO_DATA *DeviceInfoData = &set->devices[set->cDevices++];
554
555             DeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA);
556             DeviceInfoData->ClassGuid = *guid;
557             DeviceInfoData->DevInst = devInfo->devId;
558             DeviceInfoData->Reserved = (ULONG_PTR)devInfo;
559             SETUPDI_GuidToString(guid, classGuidStr);
560             SetupDiSetDeviceRegistryPropertyW((HDEVINFO)set,
561                 DeviceInfoData, SPDRP_CLASSGUID, (const BYTE *)classGuidStr,
562                 lstrlenW(classGuidStr) * sizeof(WCHAR));
563             if (dev)
564                 *dev = DeviceInfoData;
565             ret = TRUE;
566         }
567         else
568         {
569             HeapFree(GetProcessHeap(), 0, devInfo);
570             SetLastError(ERROR_OUTOFMEMORY);
571         }
572     }
573     return ret;
574 }
575
576 /***********************************************************************
577  *              SetupDiBuildClassInfoList  (SETUPAPI.@)
578  *
579  * Returns a list of setup class GUIDs that identify the classes
580  * that are installed on a local machine.
581  *
582  * PARAMS
583  *   Flags [I] control exclusion of classes from the list.
584  *   ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
585  *   ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
586  *   RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
587  *
588  * RETURNS
589  *   Success: TRUE.
590  *   Failure: FALSE.
591  */
592 BOOL WINAPI SetupDiBuildClassInfoList(
593         DWORD Flags,
594         LPGUID ClassGuidList,
595         DWORD ClassGuidListSize,
596         PDWORD RequiredSize)
597 {
598     TRACE("\n");
599     return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
600                                         ClassGuidListSize, RequiredSize,
601                                         NULL, NULL);
602 }
603
604 /***********************************************************************
605  *              SetupDiBuildClassInfoListExA  (SETUPAPI.@)
606  *
607  * Returns a list of setup class GUIDs that identify the classes
608  * that are installed on a local or remote macine.
609  *
610  * PARAMS
611  *   Flags [I] control exclusion of classes from the list.
612  *   ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
613  *   ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
614  *   RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
615  *   MachineName [I] name of a remote machine.
616  *   Reserved [I] must be NULL.
617  *
618  * RETURNS
619  *   Success: TRUE.
620  *   Failure: FALSE.
621  */
622 BOOL WINAPI SetupDiBuildClassInfoListExA(
623         DWORD Flags,
624         LPGUID ClassGuidList,
625         DWORD ClassGuidListSize,
626         PDWORD RequiredSize,
627         LPCSTR MachineName,
628         PVOID Reserved)
629 {
630     LPWSTR MachineNameW = NULL;
631     BOOL bResult;
632
633     TRACE("\n");
634
635     if (MachineName)
636     {
637         MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
638         if (MachineNameW == NULL) return FALSE;
639     }
640
641     bResult = SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
642                                            ClassGuidListSize, RequiredSize,
643                                            MachineNameW, Reserved);
644
645     MyFree(MachineNameW);
646
647     return bResult;
648 }
649
650 /***********************************************************************
651  *              SetupDiBuildClassInfoListExW  (SETUPAPI.@)
652  *
653  * Returns a list of setup class GUIDs that identify the classes
654  * that are installed on a local or remote macine.
655  *
656  * PARAMS
657  *   Flags [I] control exclusion of classes from the list.
658  *   ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
659  *   ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
660  *   RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
661  *   MachineName [I] name of a remote machine.
662  *   Reserved [I] must be NULL.
663  *
664  * RETURNS
665  *   Success: TRUE.
666  *   Failure: FALSE.
667  */
668 BOOL WINAPI SetupDiBuildClassInfoListExW(
669         DWORD Flags,
670         LPGUID ClassGuidList,
671         DWORD ClassGuidListSize,
672         PDWORD RequiredSize,
673         LPCWSTR MachineName,
674         PVOID Reserved)
675 {
676     WCHAR szKeyName[40];
677     HKEY hClassesKey;
678     HKEY hClassKey;
679     DWORD dwLength;
680     DWORD dwIndex;
681     LONG lError;
682     DWORD dwGuidListIndex = 0;
683
684     TRACE("\n");
685
686     if (RequiredSize != NULL)
687         *RequiredSize = 0;
688
689     hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
690                                             KEY_ALL_ACCESS,
691                                             DIOCR_INSTALLER,
692                                             MachineName,
693                                             Reserved);
694     if (hClassesKey == INVALID_HANDLE_VALUE)
695     {
696         return FALSE;
697     }
698
699     for (dwIndex = 0; ; dwIndex++)
700     {
701         dwLength = 40;
702         lError = RegEnumKeyExW(hClassesKey,
703                                dwIndex,
704                                szKeyName,
705                                &dwLength,
706                                NULL,
707                                NULL,
708                                NULL,
709                                NULL);
710         TRACE("RegEnumKeyExW() returns %d\n", lError);
711         if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
712         {
713             TRACE("Key name: %p\n", szKeyName);
714
715             if (RegOpenKeyExW(hClassesKey,
716                               szKeyName,
717                               0,
718                               KEY_ALL_ACCESS,
719                               &hClassKey))
720             {
721                 RegCloseKey(hClassesKey);
722                 return FALSE;
723             }
724
725             if (!RegQueryValueExW(hClassKey,
726                                   NoUseClass,
727                                   NULL,
728                                   NULL,
729                                   NULL,
730                                   NULL))
731             {
732                 TRACE("'NoUseClass' value found!\n");
733                 RegCloseKey(hClassKey);
734                 continue;
735             }
736
737             if ((Flags & DIBCI_NOINSTALLCLASS) &&
738                 (!RegQueryValueExW(hClassKey,
739                                    NoInstallClass,
740                                    NULL,
741                                    NULL,
742                                    NULL,
743                                    NULL)))
744             {
745                 TRACE("'NoInstallClass' value found!\n");
746                 RegCloseKey(hClassKey);
747                 continue;
748             }
749
750             if ((Flags & DIBCI_NODISPLAYCLASS) &&
751                 (!RegQueryValueExW(hClassKey,
752                                    NoDisplayClass,
753                                    NULL,
754                                    NULL,
755                                    NULL,
756                                    NULL)))
757             {
758                 TRACE("'NoDisplayClass' value found!\n");
759                 RegCloseKey(hClassKey);
760                 continue;
761             }
762
763             RegCloseKey(hClassKey);
764
765             TRACE("Guid: %p\n", szKeyName);
766             if (dwGuidListIndex < ClassGuidListSize)
767             {
768                 if (szKeyName[0] == '{' && szKeyName[37] == '}')
769                 {
770                     szKeyName[37] = 0;
771                 }
772                 TRACE("Guid: %p\n", &szKeyName[1]);
773
774                 UuidFromStringW(&szKeyName[1],
775                                 &ClassGuidList[dwGuidListIndex]);
776             }
777
778             dwGuidListIndex++;
779         }
780
781         if (lError != ERROR_SUCCESS)
782             break;
783     }
784
785     RegCloseKey(hClassesKey);
786
787     if (RequiredSize != NULL)
788         *RequiredSize = dwGuidListIndex;
789
790     if (ClassGuidListSize < dwGuidListIndex)
791     {
792         SetLastError(ERROR_INSUFFICIENT_BUFFER);
793         return FALSE;
794     }
795
796     return TRUE;
797 }
798
799 /***********************************************************************
800  *              SetupDiClassGuidsFromNameA  (SETUPAPI.@)
801  */
802 BOOL WINAPI SetupDiClassGuidsFromNameA(
803         LPCSTR ClassName,
804         LPGUID ClassGuidList,
805         DWORD ClassGuidListSize,
806         PDWORD RequiredSize)
807 {
808   return SetupDiClassGuidsFromNameExA(ClassName, ClassGuidList,
809                                       ClassGuidListSize, RequiredSize,
810                                       NULL, NULL);
811 }
812
813 /***********************************************************************
814  *              SetupDiClassGuidsFromNameW  (SETUPAPI.@)
815  */
816 BOOL WINAPI SetupDiClassGuidsFromNameW(
817         LPCWSTR ClassName,
818         LPGUID ClassGuidList,
819         DWORD ClassGuidListSize,
820         PDWORD RequiredSize)
821 {
822   return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
823                                       ClassGuidListSize, RequiredSize,
824                                       NULL, NULL);
825 }
826
827 /***********************************************************************
828  *              SetupDiClassGuidsFromNameExA  (SETUPAPI.@)
829  */
830 BOOL WINAPI SetupDiClassGuidsFromNameExA(
831         LPCSTR ClassName,
832         LPGUID ClassGuidList,
833         DWORD ClassGuidListSize,
834         PDWORD RequiredSize,
835         LPCSTR MachineName,
836         PVOID Reserved)
837 {
838     LPWSTR ClassNameW = NULL;
839     LPWSTR MachineNameW = NULL;
840     BOOL bResult;
841
842     ClassNameW = MultiByteToUnicode(ClassName, CP_ACP);
843     if (ClassNameW == NULL)
844         return FALSE;
845
846     if (MachineName)
847     {
848         MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
849         if (MachineNameW == NULL)
850         {
851             MyFree(ClassNameW);
852             return FALSE;
853         }
854     }
855
856     bResult = SetupDiClassGuidsFromNameExW(ClassNameW, ClassGuidList,
857                                            ClassGuidListSize, RequiredSize,
858                                            MachineNameW, Reserved);
859
860     MyFree(MachineNameW);
861     MyFree(ClassNameW);
862
863     return bResult;
864 }
865
866 /***********************************************************************
867  *              SetupDiClassGuidsFromNameExW  (SETUPAPI.@)
868  */
869 BOOL WINAPI SetupDiClassGuidsFromNameExW(
870         LPCWSTR ClassName,
871         LPGUID ClassGuidList,
872         DWORD ClassGuidListSize,
873         PDWORD RequiredSize,
874         LPCWSTR MachineName,
875         PVOID Reserved)
876 {
877     WCHAR szKeyName[40];
878     WCHAR szClassName[256];
879     HKEY hClassesKey;
880     HKEY hClassKey;
881     DWORD dwLength;
882     DWORD dwIndex;
883     LONG lError;
884     DWORD dwGuidListIndex = 0;
885
886     if (RequiredSize != NULL)
887         *RequiredSize = 0;
888
889     hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
890                                             KEY_ALL_ACCESS,
891                                             DIOCR_INSTALLER,
892                                             MachineName,
893                                             Reserved);
894     if (hClassesKey == INVALID_HANDLE_VALUE)
895     {
896         return FALSE;
897     }
898
899     for (dwIndex = 0; ; dwIndex++)
900     {
901         dwLength = 40;
902         lError = RegEnumKeyExW(hClassesKey,
903                                dwIndex,
904                                szKeyName,
905                                &dwLength,
906                                NULL,
907                                NULL,
908                                NULL,
909                                NULL);
910         TRACE("RegEnumKeyExW() returns %d\n", lError);
911         if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
912         {
913             TRACE("Key name: %p\n", szKeyName);
914
915             if (RegOpenKeyExW(hClassesKey,
916                               szKeyName,
917                               0,
918                               KEY_ALL_ACCESS,
919                               &hClassKey))
920             {
921                 RegCloseKey(hClassesKey);
922                 return FALSE;
923             }
924
925             dwLength = 256 * sizeof(WCHAR);
926             if (!RegQueryValueExW(hClassKey,
927                                   Class,
928                                   NULL,
929                                   NULL,
930                                   (LPBYTE)szClassName,
931                                   &dwLength))
932             {
933                 TRACE("Class name: %p\n", szClassName);
934
935                 if (strcmpiW(szClassName, ClassName) == 0)
936                 {
937                     TRACE("Found matching class name\n");
938
939                     TRACE("Guid: %p\n", szKeyName);
940                     if (dwGuidListIndex < ClassGuidListSize)
941                     {
942                         if (szKeyName[0] == '{' && szKeyName[37] == '}')
943                         {
944                             szKeyName[37] = 0;
945                         }
946                         TRACE("Guid: %p\n", &szKeyName[1]);
947
948                         UuidFromStringW(&szKeyName[1],
949                                         &ClassGuidList[dwGuidListIndex]);
950                     }
951
952                     dwGuidListIndex++;
953                 }
954             }
955
956             RegCloseKey(hClassKey);
957         }
958
959         if (lError != ERROR_SUCCESS)
960             break;
961     }
962
963     RegCloseKey(hClassesKey);
964
965     if (RequiredSize != NULL)
966         *RequiredSize = dwGuidListIndex;
967
968     if (ClassGuidListSize < dwGuidListIndex)
969     {
970         SetLastError(ERROR_INSUFFICIENT_BUFFER);
971         return FALSE;
972     }
973
974     return TRUE;
975 }
976
977 /***********************************************************************
978  *              SetupDiClassNameFromGuidA  (SETUPAPI.@)
979  */
980 BOOL WINAPI SetupDiClassNameFromGuidA(
981         const GUID* ClassGuid,
982         PSTR ClassName,
983         DWORD ClassNameSize,
984         PDWORD RequiredSize)
985 {
986   return SetupDiClassNameFromGuidExA(ClassGuid, ClassName,
987                                      ClassNameSize, RequiredSize,
988                                      NULL, NULL);
989 }
990
991 /***********************************************************************
992  *              SetupDiClassNameFromGuidW  (SETUPAPI.@)
993  */
994 BOOL WINAPI SetupDiClassNameFromGuidW(
995         const GUID* ClassGuid,
996         PWSTR ClassName,
997         DWORD ClassNameSize,
998         PDWORD RequiredSize)
999 {
1000   return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
1001                                      ClassNameSize, RequiredSize,
1002                                      NULL, NULL);
1003 }
1004
1005 /***********************************************************************
1006  *              SetupDiClassNameFromGuidExA  (SETUPAPI.@)
1007  */
1008 BOOL WINAPI SetupDiClassNameFromGuidExA(
1009         const GUID* ClassGuid,
1010         PSTR ClassName,
1011         DWORD ClassNameSize,
1012         PDWORD RequiredSize,
1013         PCSTR MachineName,
1014         PVOID Reserved)
1015 {
1016     WCHAR ClassNameW[MAX_CLASS_NAME_LEN];
1017     LPWSTR MachineNameW = NULL;
1018     BOOL ret;
1019
1020     if (MachineName)
1021         MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1022     ret = SetupDiClassNameFromGuidExW(ClassGuid, ClassNameW, MAX_CLASS_NAME_LEN,
1023      NULL, MachineNameW, Reserved);
1024     if (ret)
1025     {
1026         int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
1027          ClassNameSize, NULL, NULL);
1028
1029         if (!ClassNameSize && RequiredSize)
1030             *RequiredSize = len;
1031     }
1032     MyFree(MachineNameW);
1033     return ret;
1034 }
1035
1036 /***********************************************************************
1037  *              SetupDiClassNameFromGuidExW  (SETUPAPI.@)
1038  */
1039 BOOL WINAPI SetupDiClassNameFromGuidExW(
1040         const GUID* ClassGuid,
1041         PWSTR ClassName,
1042         DWORD ClassNameSize,
1043         PDWORD RequiredSize,
1044         PCWSTR MachineName,
1045         PVOID Reserved)
1046 {
1047     HKEY hKey;
1048     DWORD dwLength;
1049
1050     hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1051                                      KEY_ALL_ACCESS,
1052                                      DIOCR_INSTALLER,
1053                                      MachineName,
1054                                      Reserved);
1055     if (hKey == INVALID_HANDLE_VALUE)
1056     {
1057         return FALSE;
1058     }
1059
1060     if (RequiredSize != NULL)
1061     {
1062         dwLength = 0;
1063         if (RegQueryValueExW(hKey,
1064                              Class,
1065                              NULL,
1066                              NULL,
1067                              NULL,
1068                              &dwLength))
1069         {
1070             RegCloseKey(hKey);
1071             return FALSE;
1072         }
1073
1074         *RequiredSize = dwLength / sizeof(WCHAR);
1075     }
1076
1077     dwLength = ClassNameSize * sizeof(WCHAR);
1078     if (RegQueryValueExW(hKey,
1079                          Class,
1080                          NULL,
1081                          NULL,
1082                          (LPBYTE)ClassName,
1083                          &dwLength))
1084     {
1085         RegCloseKey(hKey);
1086         return FALSE;
1087     }
1088
1089     RegCloseKey(hKey);
1090
1091     return TRUE;
1092 }
1093
1094 /***********************************************************************
1095  *              SetupDiCreateDeviceInfoList (SETUPAPI.@)
1096  */
1097 HDEVINFO WINAPI
1098 SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
1099                             HWND hwndParent)
1100 {
1101   return SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent, NULL, NULL);
1102 }
1103
1104 /***********************************************************************
1105  *              SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
1106  */
1107 HDEVINFO WINAPI
1108 SetupDiCreateDeviceInfoListExA(const GUID *ClassGuid,
1109                                HWND hwndParent,
1110                                PCSTR MachineName,
1111                                PVOID Reserved)
1112 {
1113     LPWSTR MachineNameW = NULL;
1114     HDEVINFO hDevInfo;
1115
1116     TRACE("\n");
1117
1118     if (MachineName)
1119     {
1120         MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1121         if (MachineNameW == NULL)
1122             return INVALID_HANDLE_VALUE;
1123     }
1124
1125     hDevInfo = SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent,
1126                                               MachineNameW, Reserved);
1127
1128     MyFree(MachineNameW);
1129
1130     return hDevInfo;
1131 }
1132
1133 /***********************************************************************
1134  *              SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
1135  *
1136  * Create an empty DeviceInfoSet list.
1137  *
1138  * PARAMS
1139  *   ClassGuid [I] if not NULL only devices with GUID ClcassGuid are associated
1140  *                 with this list.
1141  *   hwndParent [I] hwnd needed for interface related actions.
1142  *   MachineName [I] name of machine to create emtpy DeviceInfoSet list, if NULL
1143  *                   local registry will be used.
1144  *   Reserved [I] must be NULL
1145  *
1146  * RETURNS
1147  *   Success: empty list.
1148  *   Failure: INVALID_HANDLE_VALUE.
1149  */
1150 HDEVINFO WINAPI
1151 SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
1152                                HWND hwndParent,
1153                                PCWSTR MachineName,
1154                                PVOID Reserved)
1155 {
1156     struct DeviceInfoSet *list = NULL;
1157     DWORD size = sizeof(struct DeviceInfoSet);
1158
1159     TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid), hwndParent,
1160       debugstr_w(MachineName), Reserved);
1161
1162     if (MachineName != NULL)
1163     {
1164         FIXME("remote support is not implemented\n");
1165         SetLastError(ERROR_INVALID_MACHINENAME);
1166         return INVALID_HANDLE_VALUE;
1167     }
1168
1169     if (Reserved != NULL)
1170     {
1171         SetLastError(ERROR_INVALID_PARAMETER);
1172         return INVALID_HANDLE_VALUE;
1173     }
1174
1175     list = HeapAlloc(GetProcessHeap(), 0, size);
1176     if (!list)
1177     {
1178         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1179         return INVALID_HANDLE_VALUE;
1180     }
1181
1182     list->magic = SETUP_DEVICE_INFO_SET_MAGIC;
1183     list->hwndParent = hwndParent;
1184     memcpy(&list->ClassGuid,
1185             ClassGuid ? ClassGuid : &GUID_NULL,
1186             sizeof(list->ClassGuid));
1187     list->cDevices = 0;
1188     list->devices = NULL;
1189
1190     return (HDEVINFO)list;
1191 }
1192
1193 /***********************************************************************
1194  *              SetupDiCreateDevRegKeyA (SETUPAPI.@)
1195  */
1196 HKEY WINAPI SetupDiCreateDevRegKeyA(
1197         HDEVINFO DeviceInfoSet,
1198         PSP_DEVINFO_DATA DeviceInfoData,
1199         DWORD Scope,
1200         DWORD HwProfile,
1201         DWORD KeyType,
1202         HINF InfHandle,
1203         PCSTR InfSectionName)
1204 {
1205     PWSTR InfSectionNameW = NULL;
1206     HKEY key;
1207
1208     TRACE("%p %p %d %d %d %p %s\n", DeviceInfoSet, DeviceInfoData, Scope,
1209             HwProfile, KeyType, InfHandle, debugstr_a(InfSectionName));
1210
1211     if (InfHandle)
1212     {
1213         if (!InfSectionName)
1214         {
1215             SetLastError(ERROR_INVALID_PARAMETER);
1216             return INVALID_HANDLE_VALUE;
1217         }
1218         else
1219         {
1220             InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
1221             if (InfSectionNameW == NULL) return INVALID_HANDLE_VALUE;
1222         }
1223     }
1224     key = SetupDiCreateDevRegKeyW(DeviceInfoSet, DeviceInfoData, Scope,
1225             HwProfile, KeyType, InfHandle, InfSectionNameW);
1226     MyFree(InfSectionNameW);
1227     return key;
1228 }
1229
1230 /***********************************************************************
1231  *              SetupDiCreateDevRegKeyW (SETUPAPI.@)
1232  */
1233 HKEY WINAPI SetupDiCreateDevRegKeyW(
1234         HDEVINFO DeviceInfoSet,
1235         PSP_DEVINFO_DATA DeviceInfoData,
1236         DWORD Scope,
1237         DWORD HwProfile,
1238         DWORD KeyType,
1239         HINF InfHandle,
1240         PCWSTR InfSectionName)
1241 {
1242     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1243     struct DeviceInfo *devInfo;
1244     HKEY key = INVALID_HANDLE_VALUE;
1245
1246     TRACE("%p %p %d %d %d %p %s\n", DeviceInfoSet, DeviceInfoData, Scope,
1247             HwProfile, KeyType, InfHandle, debugstr_w(InfSectionName));
1248
1249     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1250     {
1251         SetLastError(ERROR_INVALID_HANDLE);
1252         return INVALID_HANDLE_VALUE;
1253     }
1254     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1255     {
1256         SetLastError(ERROR_INVALID_HANDLE);
1257         return INVALID_HANDLE_VALUE;
1258     }
1259     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1260             || !DeviceInfoData->Reserved)
1261     {
1262         SetLastError(ERROR_INVALID_PARAMETER);
1263         return INVALID_HANDLE_VALUE;
1264     }
1265     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1266     if (devInfo->set != set)
1267     {
1268         SetLastError(ERROR_INVALID_PARAMETER);
1269         return INVALID_HANDLE_VALUE;
1270     }
1271     if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
1272     {
1273         SetLastError(ERROR_INVALID_FLAGS);
1274         return INVALID_HANDLE_VALUE;
1275     }
1276     if (KeyType != DIREG_DEV && KeyType != DIREG_DRV)
1277     {
1278         SetLastError(ERROR_INVALID_FLAGS);
1279         return INVALID_HANDLE_VALUE;
1280     }
1281     if (devInfo->phantom)
1282     {
1283         SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
1284         return INVALID_HANDLE_VALUE;
1285     }
1286     if (Scope != DICS_FLAG_GLOBAL)
1287         FIXME("unimplemented for scope %d\n", Scope);
1288     switch (KeyType)
1289     {
1290         case DIREG_DEV:
1291             key = SETUPDI_CreateDevKey(devInfo);
1292             break;
1293         case DIREG_DRV:
1294             key = SETUPDI_CreateDrvKey(devInfo);
1295             break;
1296         default:
1297             WARN("unknown KeyType %d\n", KeyType);
1298     }
1299     if (InfHandle)
1300         SetupInstallFromInfSectionW(NULL, InfHandle, InfSectionName, SPINST_ALL,
1301                 NULL, NULL, SP_COPY_NEWER_ONLY, NULL, NULL, DeviceInfoSet,
1302                 DeviceInfoData);
1303     return key;
1304 }
1305
1306 /***********************************************************************
1307  *              SetupDiCreateDeviceInfoA (SETUPAPI.@)
1308  */
1309 BOOL WINAPI SetupDiCreateDeviceInfoA(
1310        HDEVINFO DeviceInfoSet,
1311        PCSTR DeviceName,
1312        CONST GUID *ClassGuid,
1313        PCSTR DeviceDescription,
1314        HWND hwndParent,
1315        DWORD CreationFlags,
1316        PSP_DEVINFO_DATA DeviceInfoData)
1317 {
1318     BOOL ret = FALSE;
1319     LPWSTR DeviceNameW = NULL;
1320     LPWSTR DeviceDescriptionW = NULL;
1321
1322     if (DeviceName)
1323     {
1324         DeviceNameW = MultiByteToUnicode(DeviceName, CP_ACP);
1325         if (DeviceNameW == NULL) return FALSE;
1326     }
1327     if (DeviceDescription)
1328     {
1329         DeviceDescriptionW = MultiByteToUnicode(DeviceDescription, CP_ACP);
1330         if (DeviceDescriptionW == NULL)
1331         {
1332             MyFree(DeviceNameW);
1333             return FALSE;
1334         }
1335     }
1336
1337     ret = SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceNameW, ClassGuid, DeviceDescriptionW,
1338             hwndParent, CreationFlags, DeviceInfoData);
1339
1340     MyFree(DeviceNameW);
1341     MyFree(DeviceDescriptionW);
1342
1343     return ret;
1344 }
1345
1346 static DWORD SETUPDI_DevNameToDevID(LPCWSTR devName)
1347 {
1348     LPCWSTR ptr;
1349     DWORD devNameLen = lstrlenW(devName), devInst = 0;
1350     BOOL valid = TRUE;
1351
1352     TRACE("%s\n", debugstr_w(devName));
1353     for (ptr = devName; valid && *ptr && ptr - devName < devNameLen; )
1354     {
1355         if (isdigitW(*ptr))
1356         {
1357             devInst *= 10;
1358             devInst |= *ptr - '0';
1359             ptr++;
1360         }
1361         else
1362             valid = FALSE;
1363     }
1364     TRACE("%d\n", valid ? devInst : 0xffffffff);
1365     return valid ? devInst : 0xffffffff;
1366 }
1367
1368 /***********************************************************************
1369  *              SetupDiCreateDeviceInfoW (SETUPAPI.@)
1370  */
1371 BOOL WINAPI SetupDiCreateDeviceInfoW(
1372        HDEVINFO DeviceInfoSet,
1373        PCWSTR DeviceName,
1374        CONST GUID *ClassGuid,
1375        PCWSTR DeviceDescription,
1376        HWND hwndParent,
1377        DWORD CreationFlags,
1378        PSP_DEVINFO_DATA DeviceInfoData)
1379 {
1380     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1381     BOOL ret = FALSE, allocatedInstanceId = FALSE;
1382     LPCWSTR instanceId = NULL;
1383
1384     TRACE("%p %s %s %s %p %x %p\n", DeviceInfoSet, debugstr_w(DeviceName),
1385         debugstr_guid(ClassGuid), debugstr_w(DeviceDescription),
1386         hwndParent, CreationFlags, DeviceInfoData);
1387
1388     if (!DeviceName)
1389     {
1390         SetLastError(ERROR_INVALID_DEVINST_NAME);
1391         return FALSE;
1392     }
1393     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1394     {
1395         SetLastError(ERROR_INVALID_HANDLE);
1396         return FALSE;
1397     }
1398     if (!ClassGuid)
1399     {
1400         SetLastError(ERROR_INVALID_PARAMETER);
1401         return FALSE;
1402     }
1403     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1404     {
1405         SetLastError(ERROR_INVALID_HANDLE);
1406         return FALSE;
1407     }
1408     if (!IsEqualGUID(&set->ClassGuid, &GUID_NULL) &&
1409         !IsEqualGUID(ClassGuid, &set->ClassGuid))
1410     {
1411         SetLastError(ERROR_CLASS_MISMATCH);
1412         return FALSE;
1413     }
1414     if ((CreationFlags & DICD_GENERATE_ID))
1415     {
1416         if (strchrW(DeviceName, '\\'))
1417             SetLastError(ERROR_INVALID_DEVINST_NAME);
1418         else
1419         {
1420             static const WCHAR newDeviceFmt[] = {'R','O','O','T','\\','%','s',
1421                 '\\','%','0','4','d',0};
1422             DWORD devId;
1423
1424             if (set->cDevices)
1425             {
1426                 DWORD i, highestDevID = 0;
1427
1428                 for (i = 0; i < set->cDevices; i++)
1429                 {
1430                     struct DeviceInfo *devInfo =
1431                         (struct DeviceInfo *)set->devices[i].Reserved;
1432                     LPCWSTR devName = strrchrW(devInfo->instanceId, '\\');
1433                     DWORD id;
1434
1435                     if (devName)
1436                         devName++;
1437                     else
1438                         devName = devInfo->instanceId;
1439                     id = SETUPDI_DevNameToDevID(devName);
1440                     if (id != 0xffffffff && id > highestDevID)
1441                         highestDevID = id;
1442                 }
1443                 devId = highestDevID + 1;
1444             }
1445             else
1446                 devId = 0;
1447             /* 17 == lstrlenW(L"Root\\") + lstrlenW("\\") + 1 + %d max size */
1448             instanceId = HeapAlloc(GetProcessHeap(), 0,
1449                     (17 + lstrlenW(DeviceName)) * sizeof(WCHAR));
1450             if (instanceId)
1451             {
1452                 sprintfW((LPWSTR)instanceId, newDeviceFmt, DeviceName,
1453                         devId);
1454                 allocatedInstanceId = TRUE;
1455                 ret = TRUE;
1456             }
1457             else
1458                 ret = FALSE;
1459         }
1460     }
1461     else
1462     {
1463         DWORD i;
1464
1465         ret = TRUE;
1466         instanceId = DeviceName;
1467         for (i = 0; ret && i < set->cDevices; i++)
1468         {
1469             struct DeviceInfo *devInfo =
1470                 (struct DeviceInfo *)set->devices[i].Reserved;
1471
1472             if (!lstrcmpiW(DeviceName, devInfo->instanceId))
1473             {
1474                 SetLastError(ERROR_DEVINST_ALREADY_EXISTS);
1475                 ret = FALSE;
1476             }
1477         }
1478     }
1479     if (ret)
1480     {
1481         SP_DEVINFO_DATA *dev = NULL;
1482
1483         ret = SETUPDI_AddDeviceToSet(set, ClassGuid, 0 /* FIXME: DevInst */,
1484                 instanceId, TRUE, &dev);
1485         if (ret)
1486         {
1487             if (DeviceDescription)
1488                 SetupDiSetDeviceRegistryPropertyW(DeviceInfoSet,
1489                     dev, SPDRP_DEVICEDESC, (const BYTE *)DeviceDescription,
1490                     lstrlenW(DeviceDescription) * sizeof(WCHAR));
1491             if (DeviceInfoData)
1492             {
1493                 if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
1494                 {
1495                     SetLastError(ERROR_INVALID_USER_BUFFER);
1496                     ret = FALSE;
1497                 }
1498                 else
1499                     *DeviceInfoData = *dev;
1500             }
1501         }
1502     }
1503     if (allocatedInstanceId)
1504         HeapFree(GetProcessHeap(), 0, (LPWSTR)instanceId);
1505
1506     return ret;
1507 }
1508
1509 /***********************************************************************
1510  *              SetupDiRegisterDeviceInfo (SETUPAPI.@)
1511  */
1512 BOOL WINAPI SetupDiRegisterDeviceInfo(
1513         HDEVINFO DeviceInfoSet,
1514         PSP_DEVINFO_DATA DeviceInfoData,
1515         DWORD Flags,
1516         PSP_DETSIG_CMPPROC CompareProc,
1517         PVOID CompareContext,
1518         PSP_DEVINFO_DATA DupDeviceInfoData)
1519 {
1520     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1521     struct DeviceInfo *devInfo;
1522
1523     TRACE("%p %p %08x %p %p %p\n", DeviceInfoSet, DeviceInfoData, Flags,
1524             CompareProc, CompareContext, DupDeviceInfoData);
1525
1526     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1527     {
1528         SetLastError(ERROR_INVALID_HANDLE);
1529         return FALSE;
1530     }
1531     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1532     {
1533         SetLastError(ERROR_INVALID_HANDLE);
1534         return FALSE;
1535     }
1536     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1537             || !DeviceInfoData->Reserved)
1538     {
1539         SetLastError(ERROR_INVALID_PARAMETER);
1540         return FALSE;
1541     }
1542     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1543     if (devInfo->set != set)
1544     {
1545         SetLastError(ERROR_INVALID_PARAMETER);
1546         return FALSE;
1547     }
1548     if (devInfo->phantom)
1549     {
1550         devInfo->phantom = FALSE;
1551         RegDeleteValueW(devInfo->key, Phantom);
1552     }
1553     return TRUE;
1554 }
1555
1556 /***********************************************************************
1557  *              SetupDiEnumDeviceInfo (SETUPAPI.@)
1558  */
1559 BOOL WINAPI SetupDiEnumDeviceInfo(
1560         HDEVINFO  devinfo,
1561         DWORD  index,
1562         PSP_DEVINFO_DATA info)
1563 {
1564     BOOL ret = FALSE;
1565
1566     TRACE("%p %d %p\n", devinfo, index, info);
1567
1568     if(info==NULL)
1569     {
1570         SetLastError(ERROR_INVALID_PARAMETER);
1571         return FALSE;
1572     }
1573     if (devinfo && devinfo != INVALID_HANDLE_VALUE)
1574     {
1575         struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
1576         if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
1577         {
1578             if (index < list->cDevices)
1579             {
1580                 if (info->cbSize == sizeof(SP_DEVINFO_DATA))
1581                 {
1582                     memcpy(info, &list->devices[index], info->cbSize);
1583                     ret = TRUE;
1584                 }
1585                 else
1586                     SetLastError(ERROR_INVALID_USER_BUFFER);
1587             }
1588             else
1589                 SetLastError(ERROR_NO_MORE_ITEMS);
1590         }
1591         else
1592             SetLastError(ERROR_INVALID_HANDLE);
1593     }
1594     else
1595         SetLastError(ERROR_INVALID_HANDLE);
1596     return ret;
1597 }
1598
1599 /***********************************************************************
1600  *              SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
1601  */
1602 BOOL WINAPI SetupDiGetDeviceInstanceIdA(
1603         HDEVINFO DeviceInfoSet,
1604         PSP_DEVINFO_DATA DeviceInfoData,
1605         PSTR DeviceInstanceId,
1606         DWORD DeviceInstanceIdSize,
1607         PDWORD RequiredSize)
1608 {
1609     BOOL ret = FALSE;
1610     DWORD size;
1611     PWSTR instanceId;
1612
1613     TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
1614             DeviceInstanceIdSize, RequiredSize);
1615
1616     SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
1617                                 DeviceInfoData,
1618                                 NULL,
1619                                 0,
1620                                 &size);
1621     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1622         return FALSE;
1623     instanceId = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1624     if (instanceId)
1625     {
1626         ret = SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
1627                                           DeviceInfoData,
1628                                           instanceId,
1629                                           size,
1630                                           &size);
1631         if (ret)
1632         {
1633             int len = WideCharToMultiByte(CP_ACP, 0, instanceId, -1,
1634                                           DeviceInstanceId,
1635                                           DeviceInstanceIdSize, NULL, NULL);
1636
1637             if (!len)
1638                 ret = FALSE;
1639             else
1640             {
1641                 if (len > DeviceInstanceIdSize)
1642                 {
1643                     SetLastError(ERROR_INSUFFICIENT_BUFFER);
1644                     ret = FALSE;
1645                 }
1646                 if (RequiredSize)
1647                     *RequiredSize = len;
1648             }
1649         }
1650         HeapFree(GetProcessHeap(), 0, instanceId);
1651     }
1652     return ret;
1653 }
1654
1655 /***********************************************************************
1656  *              SetupDiGetDeviceInstanceIdW (SETUPAPI.@)
1657  */
1658 BOOL WINAPI SetupDiGetDeviceInstanceIdW(
1659         HDEVINFO DeviceInfoSet,
1660         PSP_DEVINFO_DATA DeviceInfoData,
1661         PWSTR DeviceInstanceId,
1662         DWORD DeviceInstanceIdSize,
1663         PDWORD RequiredSize)
1664 {
1665     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1666     struct DeviceInfo *devInfo;
1667
1668     TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
1669             DeviceInstanceIdSize, RequiredSize);
1670
1671     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1672     {
1673         SetLastError(ERROR_INVALID_HANDLE);
1674         return FALSE;
1675     }
1676     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1677     {
1678         SetLastError(ERROR_INVALID_HANDLE);
1679         return FALSE;
1680     }
1681     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1682             || !DeviceInfoData->Reserved)
1683     {
1684         SetLastError(ERROR_INVALID_PARAMETER);
1685         return FALSE;
1686     }
1687     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1688     if (devInfo->set != set)
1689     {
1690         SetLastError(ERROR_INVALID_PARAMETER);
1691         return FALSE;
1692     }
1693     TRACE("instance ID: %s\n", debugstr_w(devInfo->instanceId));
1694     if (DeviceInstanceIdSize < lstrlenW(devInfo->instanceId) + 1)
1695     {
1696         SetLastError(ERROR_INSUFFICIENT_BUFFER);
1697         if (RequiredSize)
1698             *RequiredSize = lstrlenW(devInfo->instanceId) + 1;
1699         return FALSE;
1700     }
1701     lstrcpyW(DeviceInstanceId, devInfo->instanceId);
1702     if (RequiredSize)
1703         *RequiredSize = lstrlenW(devInfo->instanceId) + 1;
1704     return TRUE;
1705 }
1706
1707 /***********************************************************************
1708  *              SetupDiGetActualSectionToInstallA (SETUPAPI.@)
1709  */
1710 BOOL WINAPI SetupDiGetActualSectionToInstallA(
1711         HINF InfHandle,
1712         PCSTR InfSectionName,
1713         PSTR InfSectionWithExt,
1714         DWORD InfSectionWithExtSize,
1715         PDWORD RequiredSize,
1716         PSTR *Extension)
1717 {
1718     FIXME("\n");
1719     return FALSE;
1720 }
1721
1722 /***********************************************************************
1723  *              SetupDiGetActualSectionToInstallW (SETUPAPI.@)
1724  */
1725 BOOL WINAPI SetupDiGetActualSectionToInstallW(
1726         HINF InfHandle,
1727         PCWSTR InfSectionName,
1728         PWSTR InfSectionWithExt,
1729         DWORD InfSectionWithExtSize,
1730         PDWORD RequiredSize,
1731         PWSTR *Extension)
1732 {
1733     WCHAR szBuffer[MAX_PATH];
1734     DWORD dwLength;
1735     DWORD dwFullLength;
1736     LONG lLineCount = -1;
1737
1738     lstrcpyW(szBuffer, InfSectionName);
1739     dwLength = lstrlenW(szBuffer);
1740
1741     if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
1742     {
1743         /* Test section name with '.NTx86' extension */
1744         lstrcpyW(&szBuffer[dwLength], NtPlatformExtension);
1745         lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1746
1747         if (lLineCount == -1)
1748         {
1749             /* Test section name with '.NT' extension */
1750             lstrcpyW(&szBuffer[dwLength], NtExtension);
1751             lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1752         }
1753     }
1754     else
1755     {
1756         /* Test section name with '.Win' extension */
1757         lstrcpyW(&szBuffer[dwLength], WinExtension);
1758         lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1759     }
1760
1761     if (lLineCount == -1)
1762     {
1763         /* Test section name without extension */
1764         szBuffer[dwLength] = 0;
1765         lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1766     }
1767
1768     if (lLineCount == -1)
1769     {
1770         SetLastError(ERROR_INVALID_PARAMETER);
1771         return FALSE;
1772     }
1773
1774     dwFullLength = lstrlenW(szBuffer);
1775
1776     if (InfSectionWithExt != NULL && InfSectionWithExtSize != 0)
1777     {
1778         if (InfSectionWithExtSize < (dwFullLength + 1))
1779         {
1780             SetLastError(ERROR_INSUFFICIENT_BUFFER);
1781             return FALSE;
1782         }
1783
1784         lstrcpyW(InfSectionWithExt, szBuffer);
1785         if (Extension != NULL)
1786         {
1787             *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
1788         }
1789     }
1790
1791     if (RequiredSize != NULL)
1792     {
1793         *RequiredSize = dwFullLength + 1;
1794     }
1795
1796     return TRUE;
1797 }
1798
1799 /***********************************************************************
1800  *              SetupDiGetClassDescriptionA  (SETUPAPI.@)
1801  */
1802 BOOL WINAPI SetupDiGetClassDescriptionA(
1803         const GUID* ClassGuid,
1804         PSTR ClassDescription,
1805         DWORD ClassDescriptionSize,
1806         PDWORD RequiredSize)
1807 {
1808   return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
1809                                        ClassDescriptionSize,
1810                                        RequiredSize, NULL, NULL);
1811 }
1812
1813 /***********************************************************************
1814  *              SetupDiGetClassDescriptionW  (SETUPAPI.@)
1815  */
1816 BOOL WINAPI SetupDiGetClassDescriptionW(
1817         const GUID* ClassGuid,
1818         PWSTR ClassDescription,
1819         DWORD ClassDescriptionSize,
1820         PDWORD RequiredSize)
1821 {
1822   return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
1823                                        ClassDescriptionSize,
1824                                        RequiredSize, NULL, NULL);
1825 }
1826
1827 /***********************************************************************
1828  *              SetupDiGetClassDescriptionExA  (SETUPAPI.@)
1829  */
1830 BOOL WINAPI SetupDiGetClassDescriptionExA(
1831         const GUID* ClassGuid,
1832         PSTR ClassDescription,
1833         DWORD ClassDescriptionSize,
1834         PDWORD RequiredSize,
1835         PCSTR MachineName,
1836         PVOID Reserved)
1837 {
1838     HKEY hKey;
1839     DWORD dwLength;
1840     BOOL ret;
1841
1842     hKey = SetupDiOpenClassRegKeyExA(ClassGuid,
1843                                      KEY_ALL_ACCESS,
1844                                      DIOCR_INSTALLER,
1845                                      MachineName,
1846                                      Reserved);
1847     if (hKey == INVALID_HANDLE_VALUE)
1848     {
1849         WARN("SetupDiOpenClassRegKeyExA() failed (Error %u)\n", GetLastError());
1850         return FALSE;
1851     }
1852
1853     dwLength = ClassDescriptionSize;
1854     ret = !RegQueryValueExA( hKey, NULL, NULL, NULL,
1855                              (LPBYTE)ClassDescription, &dwLength );
1856     if (RequiredSize) *RequiredSize = dwLength;
1857     RegCloseKey(hKey);
1858     return ret;
1859 }
1860
1861 /***********************************************************************
1862  *              SetupDiGetClassDescriptionExW  (SETUPAPI.@)
1863  */
1864 BOOL WINAPI SetupDiGetClassDescriptionExW(
1865         const GUID* ClassGuid,
1866         PWSTR ClassDescription,
1867         DWORD ClassDescriptionSize,
1868         PDWORD RequiredSize,
1869         PCWSTR MachineName,
1870         PVOID Reserved)
1871 {
1872     HKEY hKey;
1873     DWORD dwLength;
1874     BOOL ret;
1875
1876     hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1877                                      KEY_ALL_ACCESS,
1878                                      DIOCR_INSTALLER,
1879                                      MachineName,
1880                                      Reserved);
1881     if (hKey == INVALID_HANDLE_VALUE)
1882     {
1883         WARN("SetupDiOpenClassRegKeyExW() failed (Error %u)\n", GetLastError());
1884         return FALSE;
1885     }
1886
1887     dwLength = ClassDescriptionSize * sizeof(WCHAR);
1888     ret = !RegQueryValueExW( hKey, NULL, NULL, NULL,
1889                              (LPBYTE)ClassDescription, &dwLength );
1890     if (RequiredSize) *RequiredSize = dwLength / sizeof(WCHAR);
1891     RegCloseKey(hKey);
1892     return ret;
1893 }
1894
1895 /***********************************************************************
1896  *              SetupDiGetClassDevsA (SETUPAPI.@)
1897  */
1898 HDEVINFO WINAPI SetupDiGetClassDevsA(
1899        CONST GUID *class,
1900        LPCSTR enumstr,
1901        HWND parent,
1902        DWORD flags)
1903 {
1904     HDEVINFO ret;
1905     LPWSTR enumstrW = NULL;
1906
1907     if (enumstr)
1908     {
1909         int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1910         enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1911         if (!enumstrW)
1912         {
1913             ret = INVALID_HANDLE_VALUE;
1914             goto end;
1915         }
1916         MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1917     }
1918     ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, NULL, NULL,
1919             NULL);
1920     HeapFree(GetProcessHeap(), 0, enumstrW);
1921
1922 end:
1923     return ret;
1924 }
1925
1926 /***********************************************************************
1927  *                SetupDiGetClassDevsExA (SETUPAPI.@)
1928  */
1929 HDEVINFO WINAPI SetupDiGetClassDevsExA(
1930         const GUID *class,
1931         PCSTR enumstr,
1932         HWND parent,
1933         DWORD flags,
1934         HDEVINFO deviceset,
1935         PCSTR machine,
1936         PVOID reserved)
1937 {
1938     HDEVINFO ret;
1939     LPWSTR enumstrW = NULL, machineW = NULL;
1940
1941     if (enumstr)
1942     {
1943         int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1944         enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1945         if (!enumstrW)
1946         {
1947             ret = INVALID_HANDLE_VALUE;
1948             goto end;
1949         }
1950         MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1951     }
1952     if (machine)
1953     {
1954         int len = MultiByteToWideChar(CP_ACP, 0, machine, -1, NULL, 0);
1955         machineW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1956         if (!machineW)
1957         {
1958             HeapFree(GetProcessHeap(), 0, enumstrW);
1959             ret = INVALID_HANDLE_VALUE;
1960             goto end;
1961         }
1962         MultiByteToWideChar(CP_ACP, 0, machine, -1, machineW, len);
1963     }
1964     ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, deviceset,
1965             machineW, reserved);
1966     HeapFree(GetProcessHeap(), 0, enumstrW);
1967     HeapFree(GetProcessHeap(), 0, machineW);
1968
1969 end:
1970     return ret;
1971 }
1972
1973 static void SETUPDI_AddDeviceInterfaces(SP_DEVINFO_DATA *dev, HKEY key,
1974         const GUID *interface)
1975 {
1976     DWORD i, len;
1977     WCHAR subKeyName[MAX_PATH];
1978     LONG l = ERROR_SUCCESS;
1979
1980     for (i = 0; !l; i++)
1981     {
1982         len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1983         l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
1984         if (!l)
1985         {
1986             HKEY subKey;
1987             SP_DEVICE_INTERFACE_DATA *iface = NULL;
1988
1989             /* The subkey name is the reference string, with a '#' prepended */
1990             SETUPDI_AddInterfaceInstance(dev, interface, subKeyName + 1,
1991                     &iface);
1992             l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
1993             if (!l)
1994             {
1995                 WCHAR symbolicLink[MAX_PATH];
1996                 DWORD dataType;
1997
1998                 len = sizeof(symbolicLink);
1999                 l = RegQueryValueExW(subKey, SymbolicLink, NULL, &dataType,
2000                         (BYTE *)symbolicLink, &len);
2001                 if (!l && dataType == REG_SZ)
2002                     SETUPDI_SetInterfaceSymbolicLink(iface, symbolicLink);
2003                 RegCloseKey(subKey);
2004             }
2005             /* Allow enumeration to continue */
2006             l = ERROR_SUCCESS;
2007         }
2008     }
2009     /* FIXME: find and add all the device's interfaces to the device */
2010 }
2011
2012 static void SETUPDI_EnumerateMatchingInterfaces(HDEVINFO DeviceInfoSet,
2013         HKEY key, const GUID *interface, LPCWSTR enumstr)
2014 {
2015     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2016     DWORD i, len;
2017     WCHAR subKeyName[MAX_PATH];
2018     LONG l;
2019     HKEY enumKey = INVALID_HANDLE_VALUE;
2020
2021     TRACE("%s\n", debugstr_w(enumstr));
2022
2023     l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_READ, NULL,
2024             &enumKey, NULL);
2025     for (i = 0; !l; i++)
2026     {
2027         len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2028         l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
2029         if (!l)
2030         {
2031             HKEY subKey;
2032
2033             l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
2034             if (!l)
2035             {
2036                 WCHAR deviceInst[MAX_PATH * 3];
2037                 DWORD dataType;
2038
2039                 len = sizeof(deviceInst);
2040                 l = RegQueryValueExW(subKey, DeviceInstance, NULL, &dataType,
2041                         (BYTE *)deviceInst, &len);
2042                 if (!l && dataType == REG_SZ)
2043                 {
2044                     TRACE("found instance ID %s\n", debugstr_w(deviceInst));
2045                     if (!enumstr || !lstrcmpiW(enumstr, deviceInst))
2046                     {
2047                         HKEY deviceKey;
2048
2049                         l = RegOpenKeyExW(enumKey, deviceInst, 0, KEY_READ,
2050                                 &deviceKey);
2051                         if (!l)
2052                         {
2053                             WCHAR deviceClassStr[40];
2054
2055                             len = sizeof(deviceClassStr);
2056                             l = RegQueryValueExW(deviceKey, ClassGUID, NULL,
2057                                     &dataType, (BYTE *)deviceClassStr, &len);
2058                             if (!l && dataType == REG_SZ &&
2059                                     deviceClassStr[0] == '{' &&
2060                                     deviceClassStr[37] == '}')
2061                             {
2062                                 GUID deviceClass;
2063                                 SP_DEVINFO_DATA *dev;
2064
2065                                 deviceClassStr[37] = 0;
2066                                 UuidFromStringW(&deviceClassStr[1],
2067                                         &deviceClass);
2068                                 if (SETUPDI_AddDeviceToSet(set, &deviceClass,
2069                                         0 /* FIXME: DevInst */, deviceInst,
2070                                         FALSE, &dev))
2071                                     SETUPDI_AddDeviceInterfaces(dev, subKey,
2072                                             interface);
2073                             }
2074                             RegCloseKey(deviceKey);
2075                         }
2076                     }
2077                 }
2078                 RegCloseKey(subKey);
2079             }
2080             /* Allow enumeration to continue */
2081             l = ERROR_SUCCESS;
2082         }
2083     }
2084     if (enumKey != INVALID_HANDLE_VALUE)
2085         RegCloseKey(enumKey);
2086 }
2087
2088 static void SETUPDI_EnumerateInterfaces(HDEVINFO DeviceInfoSet,
2089         const GUID *interface, LPCWSTR enumstr, DWORD flags)
2090 {
2091     HKEY interfacesKey = SetupDiOpenClassRegKeyExW(interface, KEY_READ,
2092             DIOCR_INTERFACE, NULL, NULL);
2093
2094     TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(interface),
2095             debugstr_w(enumstr), flags);
2096
2097     if (interfacesKey != INVALID_HANDLE_VALUE)
2098     {
2099         if (flags & DIGCF_ALLCLASSES)
2100         {
2101             DWORD i, len;
2102             WCHAR interfaceGuidStr[40];
2103             LONG l = ERROR_SUCCESS;
2104
2105             for (i = 0; !l; i++)
2106             {
2107                 len = sizeof(interfaceGuidStr) / sizeof(interfaceGuidStr[0]);
2108                 l = RegEnumKeyExW(interfacesKey, i, interfaceGuidStr, &len,
2109                         NULL, NULL, NULL, NULL);
2110                 if (!l)
2111                 {
2112                     if (interfaceGuidStr[0] == '{' &&
2113                             interfaceGuidStr[37] == '}')
2114                     {
2115                         HKEY interfaceKey;
2116                         GUID interfaceGuid;
2117
2118                         interfaceGuidStr[37] = 0;
2119                         UuidFromStringW(&interfaceGuidStr[1], &interfaceGuid);
2120                         l = RegOpenKeyExW(interfacesKey, interfaceGuidStr, 0,
2121                                 KEY_READ, &interfaceKey);
2122                         if (!l)
2123                         {
2124                             SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
2125                                     interfaceKey, &interfaceGuid, enumstr);
2126                             RegCloseKey(interfaceKey);
2127                         }
2128                     }
2129                 }
2130             }
2131         }
2132         else
2133         {
2134             /* In this case, SetupDiOpenClassRegKeyExW opened the specific
2135              * interface's key, so just pass that long
2136              */
2137             SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
2138                     interfacesKey, interface, enumstr);
2139         }
2140         RegCloseKey(interfacesKey);
2141     }
2142 }
2143
2144 static void SETUPDI_EnumerateMatchingDeviceInstances(struct DeviceInfoSet *set,
2145         LPCWSTR enumerator, LPCWSTR deviceName, HKEY deviceKey,
2146         const GUID *class, DWORD flags)
2147 {
2148     DWORD i, len;
2149     WCHAR deviceInstance[MAX_PATH];
2150     LONG l = ERROR_SUCCESS;
2151
2152     TRACE("%s %s\n", debugstr_w(enumerator), debugstr_w(deviceName));
2153
2154     for (i = 0; !l; i++)
2155     {
2156         len = sizeof(deviceInstance) / sizeof(deviceInstance[0]);
2157         l = RegEnumKeyExW(deviceKey, i, deviceInstance, &len, NULL, NULL, NULL,
2158                 NULL);
2159         if (!l)
2160         {
2161             HKEY subKey;
2162
2163             l = RegOpenKeyExW(deviceKey, deviceInstance, 0, KEY_READ, &subKey);
2164             if (!l)
2165             {
2166                 WCHAR classGuid[40];
2167                 DWORD dataType;
2168
2169                 len = sizeof(classGuid);
2170                 l = RegQueryValueExW(subKey, ClassGUID, NULL, &dataType,
2171                         (BYTE *)classGuid, &len);
2172                 if (!l && dataType == REG_SZ)
2173                 {
2174                     if (classGuid[0] == '{' && classGuid[37] == '}')
2175                     {
2176                         GUID deviceClass;
2177
2178                         classGuid[37] = 0;
2179                         UuidFromStringW(&classGuid[1], &deviceClass);
2180                         if ((flags & DIGCF_ALLCLASSES) ||
2181                                 IsEqualGUID(class, &deviceClass))
2182                         {
2183                             static const WCHAR fmt[] =
2184                              {'%','s','\\','%','s','\\','%','s',0};
2185                             LPWSTR instanceId;
2186
2187                             instanceId = HeapAlloc(GetProcessHeap(), 0,
2188                                 (lstrlenW(enumerator) + lstrlenW(deviceName) +
2189                                 lstrlenW(deviceInstance) + 3) * sizeof(WCHAR));
2190                             if (instanceId)
2191                             {
2192                                 sprintfW(instanceId, fmt, enumerator,
2193                                         deviceName, deviceInstance);
2194                                 SETUPDI_AddDeviceToSet(set, &deviceClass,
2195                                         0 /* FIXME: DevInst */, instanceId,
2196                                         FALSE, NULL);
2197                                 HeapFree(GetProcessHeap(), 0, instanceId);
2198                             }
2199                         }
2200                     }
2201                 }
2202                 RegCloseKey(subKey);
2203             }
2204             /* Allow enumeration to continue */
2205             l = ERROR_SUCCESS;
2206         }
2207     }
2208 }
2209
2210 static void SETUPDI_EnumerateMatchingDevices(HDEVINFO DeviceInfoSet,
2211         LPCWSTR parent, HKEY key, const GUID *class, DWORD flags)
2212 {
2213     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2214     DWORD i, len;
2215     WCHAR subKeyName[MAX_PATH];
2216     LONG l = ERROR_SUCCESS;
2217
2218     TRACE("%s\n", debugstr_w(parent));
2219
2220     for (i = 0; !l; i++)
2221     {
2222         len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2223         l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
2224         if (!l)
2225         {
2226             HKEY subKey;
2227
2228             l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
2229             if (!l)
2230             {
2231                 TRACE("%s\n", debugstr_w(subKeyName));
2232                 SETUPDI_EnumerateMatchingDeviceInstances(set, parent,
2233                         subKeyName, subKey, class, flags);
2234                 RegCloseKey(subKey);
2235             }
2236             /* Allow enumeration to continue */
2237             l = ERROR_SUCCESS;
2238         }
2239     }
2240 }
2241
2242 static void SETUPDI_EnumerateDevices(HDEVINFO DeviceInfoSet, const GUID *class,
2243         LPCWSTR enumstr, DWORD flags)
2244 {
2245     HKEY enumKey;
2246     LONG l;
2247
2248     TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(class),
2249             debugstr_w(enumstr), flags);
2250
2251     l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_READ, NULL,
2252             &enumKey, NULL);
2253     if (enumKey != INVALID_HANDLE_VALUE)
2254     {
2255         if (enumstr)
2256         {
2257             HKEY enumStrKey;
2258
2259             l = RegOpenKeyExW(enumKey, enumstr, 0, KEY_READ,
2260                     &enumStrKey);
2261             if (!l)
2262             {
2263                 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet, enumstr,
2264                         enumStrKey, class, flags);
2265                 RegCloseKey(enumStrKey);
2266             }
2267         }
2268         else
2269         {
2270             DWORD i, len;
2271             WCHAR subKeyName[MAX_PATH];
2272
2273             l = ERROR_SUCCESS;
2274             for (i = 0; !l; i++)
2275             {
2276                 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2277                 l = RegEnumKeyExW(enumKey, i, subKeyName, &len, NULL,
2278                         NULL, NULL, NULL);
2279                 if (!l)
2280                 {
2281                     HKEY subKey;
2282
2283                     l = RegOpenKeyExW(enumKey, subKeyName, 0, KEY_READ,
2284                             &subKey);
2285                     if (!l)
2286                     {
2287                         SETUPDI_EnumerateMatchingDevices(DeviceInfoSet,
2288                                 subKeyName, subKey, class, flags);
2289                         RegCloseKey(subKey);
2290                     }
2291                     /* Allow enumeration to continue */
2292                     l = ERROR_SUCCESS;
2293                 }
2294             }
2295         }
2296         RegCloseKey(enumKey);
2297     }
2298 }
2299
2300 /***********************************************************************
2301  *              SetupDiGetClassDevsW (SETUPAPI.@)
2302  */
2303 HDEVINFO WINAPI SetupDiGetClassDevsW(
2304        CONST GUID *class,
2305        LPCWSTR enumstr,
2306        HWND parent,
2307        DWORD flags)
2308 {
2309     return SetupDiGetClassDevsExW(class, enumstr, parent, flags, NULL, NULL,
2310             NULL);
2311 }
2312
2313 /***********************************************************************
2314  *              SetupDiGetClassDevsExW (SETUPAPI.@)
2315  */
2316 HDEVINFO WINAPI SetupDiGetClassDevsExW(
2317         CONST GUID *class,
2318         PCWSTR enumstr,
2319         HWND parent,
2320         DWORD flags,
2321         HDEVINFO deviceset,
2322         PCWSTR machine,
2323         PVOID reserved)
2324 {
2325     static const DWORD unsupportedFlags = DIGCF_DEFAULT | DIGCF_PRESENT |
2326         DIGCF_PROFILE;
2327     HDEVINFO set;
2328
2329     TRACE("%s %s %p 0x%08x %p %s %p\n", debugstr_guid(class),
2330             debugstr_w(enumstr), parent, flags, deviceset, debugstr_w(machine),
2331             reserved);
2332
2333     if (!(flags & DIGCF_ALLCLASSES) && !class)
2334     {
2335         SetLastError(ERROR_INVALID_PARAMETER);
2336         return NULL;
2337     }
2338     if (flags & unsupportedFlags)
2339         WARN("unsupported flags %08x\n", flags & unsupportedFlags);
2340     if (deviceset)
2341         set = deviceset;
2342     else
2343         set = SetupDiCreateDeviceInfoListExW(class, parent, machine, reserved);
2344     if (set)
2345     {
2346         if (machine)
2347             FIXME("%s: unimplemented for remote machines\n",
2348                     debugstr_w(machine));
2349         else if (flags & DIGCF_DEVICEINTERFACE)
2350             SETUPDI_EnumerateInterfaces(set, class, enumstr, flags);
2351         else
2352             SETUPDI_EnumerateDevices(set, class, enumstr, flags);
2353     }
2354     return set;
2355 }
2356
2357 /***********************************************************************
2358  *              SetupDiGetDeviceInfoListDetailA  (SETUPAPI.@)
2359  */
2360 BOOL WINAPI SetupDiGetDeviceInfoListDetailA(
2361         HDEVINFO DeviceInfoSet,
2362         PSP_DEVINFO_LIST_DETAIL_DATA_A DevInfoData )
2363 {
2364     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2365
2366     TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2367
2368     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2369     {
2370         SetLastError(ERROR_INVALID_HANDLE);
2371         return FALSE;
2372     }
2373     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2374     {
2375         SetLastError(ERROR_INVALID_HANDLE);
2376         return FALSE;
2377     }
2378     if (!DevInfoData ||
2379             DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_A))
2380     {
2381         SetLastError(ERROR_INVALID_PARAMETER);
2382         return FALSE;
2383     }
2384     DevInfoData->ClassGuid = set->ClassGuid;
2385     DevInfoData->RemoteMachineHandle = NULL;
2386     DevInfoData->RemoteMachineName[0] = '\0';
2387     return TRUE;
2388 }
2389
2390 /***********************************************************************
2391  *              SetupDiGetDeviceInfoListDetailW  (SETUPAPI.@)
2392  */
2393 BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
2394         HDEVINFO DeviceInfoSet,
2395         PSP_DEVINFO_LIST_DETAIL_DATA_W DevInfoData )
2396 {
2397     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2398
2399     TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2400
2401     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2402     {
2403         SetLastError(ERROR_INVALID_HANDLE);
2404         return FALSE;
2405     }
2406     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2407     {
2408         SetLastError(ERROR_INVALID_HANDLE);
2409         return FALSE;
2410     }
2411     if (!DevInfoData ||
2412             DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W))
2413     {
2414         SetLastError(ERROR_INVALID_PARAMETER);
2415         return FALSE;
2416     }
2417     DevInfoData->ClassGuid = set->ClassGuid;
2418     DevInfoData->RemoteMachineHandle = NULL;
2419     DevInfoData->RemoteMachineName[0] = '\0';
2420     return TRUE;
2421 }
2422
2423 /***********************************************************************
2424  *              SetupDiCreateDeviceInterfaceA (SETUPAPI.@)
2425  */
2426 BOOL WINAPI SetupDiCreateDeviceInterfaceA(
2427         HDEVINFO DeviceInfoSet,
2428         PSP_DEVINFO_DATA DeviceInfoData,
2429         const GUID *InterfaceClassGuid,
2430         PCSTR ReferenceString,
2431         DWORD CreationFlags,
2432         PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2433 {
2434     BOOL ret;
2435     LPWSTR ReferenceStringW = NULL;
2436
2437     TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2438             debugstr_guid(InterfaceClassGuid), debugstr_a(ReferenceString),
2439             CreationFlags, DeviceInterfaceData);
2440
2441     if (ReferenceString)
2442     {
2443         ReferenceStringW = MultiByteToUnicode(ReferenceString, CP_ACP);
2444         if (ReferenceStringW == NULL) return FALSE;
2445     }
2446
2447     ret = SetupDiCreateDeviceInterfaceW(DeviceInfoSet, DeviceInfoData,
2448             InterfaceClassGuid, ReferenceStringW, CreationFlags,
2449             DeviceInterfaceData);
2450
2451     MyFree(ReferenceStringW);
2452
2453     return ret;
2454 }
2455
2456 /***********************************************************************
2457  *              SetupDiCreateDeviceInterfaceW (SETUPAPI.@)
2458  */
2459 BOOL WINAPI SetupDiCreateDeviceInterfaceW(
2460         HDEVINFO DeviceInfoSet,
2461         PSP_DEVINFO_DATA DeviceInfoData,
2462         const GUID *InterfaceClassGuid,
2463         PCWSTR ReferenceString,
2464         DWORD CreationFlags,
2465         PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2466 {
2467     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2468     struct DeviceInfo *devInfo;
2469     SP_DEVICE_INTERFACE_DATA *iface = NULL;
2470     BOOL ret;
2471
2472     TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2473             debugstr_guid(InterfaceClassGuid), debugstr_w(ReferenceString),
2474             CreationFlags, DeviceInterfaceData);
2475
2476     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2477     {
2478         SetLastError(ERROR_INVALID_HANDLE);
2479         return FALSE;
2480     }
2481     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2482     {
2483         SetLastError(ERROR_INVALID_HANDLE);
2484         return FALSE;
2485     }
2486     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2487             || !DeviceInfoData->Reserved)
2488     {
2489         SetLastError(ERROR_INVALID_PARAMETER);
2490         return FALSE;
2491     }
2492     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2493     if (devInfo->set != set)
2494     {
2495         SetLastError(ERROR_INVALID_PARAMETER);
2496         return FALSE;
2497     }
2498     if (!InterfaceClassGuid)
2499     {
2500         SetLastError(ERROR_INVALID_USER_BUFFER);
2501         return FALSE;
2502     }
2503     if ((ret = SETUPDI_AddInterfaceInstance(DeviceInfoData, InterfaceClassGuid,
2504                     ReferenceString, &iface)))
2505     {
2506         if (DeviceInterfaceData)
2507         {
2508             if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2509             {
2510                 SetLastError(ERROR_INVALID_USER_BUFFER);
2511                 ret = FALSE;
2512             }
2513             else
2514                 *DeviceInterfaceData = *iface;
2515         }
2516     }
2517     return ret;
2518 }
2519
2520 /***********************************************************************
2521  *              SetupDiCreateDeviceInterfaceRegKeyA (SETUPAPI.@)
2522  */
2523 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyA(
2524         HDEVINFO DeviceInfoSet,
2525         PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2526         DWORD Reserved,
2527         REGSAM samDesired,
2528         HINF InfHandle,
2529         PCSTR InfSectionName)
2530 {
2531     HKEY key;
2532     PWSTR InfSectionNameW = NULL;
2533
2534     TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2535             samDesired, InfHandle, InfSectionName);
2536     if (InfHandle)
2537     {
2538         if (!InfSectionName)
2539         {
2540             SetLastError(ERROR_INVALID_PARAMETER);
2541             return INVALID_HANDLE_VALUE;
2542         }
2543         InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
2544         if (!InfSectionNameW)
2545             return INVALID_HANDLE_VALUE;
2546     }
2547     key = SetupDiCreateDeviceInterfaceRegKeyW(DeviceInfoSet,
2548             DeviceInterfaceData, Reserved, samDesired, InfHandle,
2549             InfSectionNameW);
2550     MyFree(InfSectionNameW);
2551     return key;
2552 }
2553
2554 static PWSTR SETUPDI_GetInstancePath(struct InterfaceInfo *ifaceInfo)
2555 {
2556     static const WCHAR hash[] = {'#',0};
2557     PWSTR instancePath = NULL;
2558
2559     if (ifaceInfo->referenceString)
2560     {
2561         instancePath = HeapAlloc(GetProcessHeap(), 0,
2562                 (lstrlenW(ifaceInfo->referenceString) + 2) * sizeof(WCHAR));
2563         if (instancePath)
2564         {
2565             lstrcpyW(instancePath, hash);
2566             lstrcatW(instancePath, ifaceInfo->referenceString);
2567         }
2568         else
2569             SetLastError(ERROR_OUTOFMEMORY);
2570     }
2571     else
2572     {
2573         instancePath = HeapAlloc(GetProcessHeap(), 0,
2574                 (lstrlenW(hash) + 1) * sizeof(WCHAR));
2575         if (instancePath)
2576             lstrcpyW(instancePath, hash);
2577     }
2578     return instancePath;
2579 }
2580
2581 /***********************************************************************
2582  *              SetupDiCreateDeviceInterfaceRegKeyW (SETUPAPI.@)
2583  */
2584 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyW(
2585         HDEVINFO DeviceInfoSet,
2586         PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2587         DWORD Reserved,
2588         REGSAM samDesired,
2589         HINF InfHandle,
2590         PCWSTR InfSectionName)
2591 {
2592     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2593     HKEY key = INVALID_HANDLE_VALUE, interfacesKey;
2594     LONG l;
2595
2596     TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2597             samDesired, InfHandle, InfSectionName);
2598
2599     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2600             set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2601     {
2602         SetLastError(ERROR_INVALID_HANDLE);
2603         return INVALID_HANDLE_VALUE;
2604     }
2605     if (!DeviceInterfaceData ||
2606             DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2607             !DeviceInterfaceData->Reserved)
2608     {
2609         SetLastError(ERROR_INVALID_PARAMETER);
2610         return INVALID_HANDLE_VALUE;
2611     }
2612     if (InfHandle && !InfSectionName)
2613     {
2614         SetLastError(ERROR_INVALID_PARAMETER);
2615         return INVALID_HANDLE_VALUE;
2616     }
2617     if (!(l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, DeviceClasses, 0, NULL, 0,
2618                     samDesired, NULL, &interfacesKey, NULL)))
2619     {
2620         HKEY parent;
2621         WCHAR bracedGuidString[39];
2622
2623         SETUPDI_GuidToString(&DeviceInterfaceData->InterfaceClassGuid,
2624                 bracedGuidString);
2625         if (!(l = RegCreateKeyExW(interfacesKey, bracedGuidString, 0, NULL, 0,
2626                         samDesired, NULL, &parent, NULL)))
2627         {
2628             struct InterfaceInfo *ifaceInfo =
2629                 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2630             PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2631
2632             if (instancePath)
2633             {
2634                 LONG l;
2635
2636                 l = RegCreateKeyExW(parent, instancePath, 0, NULL, 0,
2637                         samDesired, NULL, &key, NULL);
2638                 if (l)
2639                 {
2640                     SetLastError(l);
2641                     key = INVALID_HANDLE_VALUE;
2642                 }
2643                 else if (InfHandle)
2644                     FIXME("INF section installation unsupported\n");
2645             }
2646             HeapFree(GetProcessHeap(), 0, instancePath);
2647             RegCloseKey(parent);
2648         }
2649         else
2650             SetLastError(l);
2651         RegCloseKey(interfacesKey);
2652     }
2653     else
2654         SetLastError(l);
2655     return key;
2656 }
2657
2658 /***********************************************************************
2659  *              SetupDiDeleteDeviceInterfaceRegKey (SETUPAPI.@)
2660  */
2661 BOOL WINAPI SetupDiDeleteDeviceInterfaceRegKey(
2662         HDEVINFO DeviceInfoSet,
2663         PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2664         DWORD Reserved)
2665 {
2666     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2667     HKEY parent;
2668     BOOL ret = FALSE;
2669
2670     TRACE("%p %p %d\n", DeviceInfoSet, DeviceInterfaceData, Reserved);
2671
2672     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2673             set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2674     {
2675         SetLastError(ERROR_INVALID_HANDLE);
2676         return FALSE;
2677     }
2678     if (!DeviceInterfaceData ||
2679             DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2680             !DeviceInterfaceData->Reserved)
2681     {
2682         SetLastError(ERROR_INVALID_PARAMETER);
2683         return FALSE;
2684     }
2685     parent = SetupDiOpenClassRegKeyExW(&DeviceInterfaceData->InterfaceClassGuid,
2686             KEY_ALL_ACCESS, DIOCR_INTERFACE, NULL, NULL);
2687     if (parent != INVALID_HANDLE_VALUE)
2688     {
2689         struct InterfaceInfo *ifaceInfo =
2690             (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2691         PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2692
2693         if (instancePath)
2694         {
2695             LONG l = RegDeleteKeyW(parent, instancePath);
2696
2697             if (l)
2698                 SetLastError(l);
2699             else
2700                 ret = TRUE;
2701             HeapFree(GetProcessHeap(), 0, instancePath);
2702         }
2703         RegCloseKey(parent);
2704     }
2705     return ret;
2706 }
2707
2708 /***********************************************************************
2709  *              SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2710  *
2711  * PARAMS
2712  *   DeviceInfoSet      [I]    Set of devices from which to enumerate
2713  *                             interfaces
2714  *   DeviceInfoData     [I]    (Optional) If specified, a specific device
2715  *                             instance from which to enumerate interfaces.
2716  *                             If it isn't specified, all interfaces for all
2717  *                             devices in the set are enumerated.
2718  *   InterfaceClassGuid [I]    The interface class to enumerate.
2719  *   MemberIndex        [I]    An index of the interface instance to enumerate.
2720  *                             A caller should start with MemberIndex set to 0,
2721  *                             and continue until the function fails with
2722  *                             ERROR_NO_MORE_ITEMS.
2723  *   DeviceInterfaceData [I/O] Returns an enumerated interface.  Its cbSize
2724  *                             member must be set to
2725  *                             sizeof(SP_DEVICE_INTERFACE_DATA).
2726  *
2727  * RETURNS
2728  *   Success: non-zero value.
2729  *   Failure: FALSE.  Call GetLastError() for more info.
2730  */
2731 BOOL WINAPI SetupDiEnumDeviceInterfaces(
2732        HDEVINFO DeviceInfoSet,
2733        PSP_DEVINFO_DATA DeviceInfoData,
2734        CONST GUID * InterfaceClassGuid,
2735        DWORD MemberIndex,
2736        PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2737 {
2738     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2739     BOOL ret = FALSE;
2740
2741     TRACE("%p, %p, %s, %d, %p\n", DeviceInfoSet, DeviceInfoData,
2742      debugstr_guid(InterfaceClassGuid), MemberIndex, DeviceInterfaceData);
2743
2744     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2745             set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2746     {
2747         SetLastError(ERROR_INVALID_HANDLE);
2748         return FALSE;
2749     }
2750     if (DeviceInfoData && (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA) ||
2751                 !DeviceInfoData->Reserved))
2752     {
2753         SetLastError(ERROR_INVALID_PARAMETER);
2754         return FALSE;
2755     }
2756     if (!DeviceInterfaceData ||
2757             DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2758     {
2759         SetLastError(ERROR_INVALID_PARAMETER);
2760         return FALSE;
2761     }
2762     if (DeviceInfoData)
2763     {
2764         struct DeviceInfo *devInfo =
2765             (struct DeviceInfo *)DeviceInfoData->Reserved;
2766         struct InterfaceInstances *iface;
2767
2768         if ((ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
2769         {
2770             if (MemberIndex < iface->cInstances)
2771                 *DeviceInterfaceData = iface->instances[MemberIndex];
2772             else
2773             {
2774                 SetLastError(ERROR_NO_MORE_ITEMS);
2775                 ret = FALSE;
2776             }
2777         }
2778         else
2779             SetLastError(ERROR_NO_MORE_ITEMS);
2780     }
2781     else
2782     {
2783         DWORD i, cEnumerated = 0;
2784         BOOL found = FALSE;
2785
2786         for (i = 0; !found && cEnumerated < MemberIndex + 1 &&
2787                 i < set->cDevices; i++)
2788         {
2789             struct DeviceInfo *devInfo =
2790                 (struct DeviceInfo *)set->devices[i].Reserved;
2791             struct InterfaceInstances *iface;
2792
2793             if (SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface))
2794             {
2795                 if (cEnumerated + iface->cInstances < MemberIndex + 1)
2796                     cEnumerated += iface->cInstances;
2797                 else
2798                 {
2799                     DWORD instanceIndex = MemberIndex - cEnumerated;
2800
2801                     *DeviceInterfaceData = iface->instances[instanceIndex];
2802                     cEnumerated += instanceIndex + 1;
2803                     found = TRUE;
2804                     ret = TRUE;
2805                 }
2806             }
2807         }
2808         if (!found)
2809             SetLastError(ERROR_NO_MORE_ITEMS);
2810     }
2811     return ret;
2812 }
2813
2814 /***********************************************************************
2815  *              SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2816   *
2817  * Destroy a DeviceInfoList and free all used memory of the list.
2818  *
2819  * PARAMS
2820  *   devinfo [I] DeviceInfoList pointer to list to destroy
2821  *
2822  * RETURNS
2823  *   Success: non zero value.
2824  *   Failure: zero value.
2825  */
2826 BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
2827 {
2828     BOOL ret = FALSE;
2829
2830     TRACE("%p\n", devinfo);
2831     if (devinfo && devinfo != INVALID_HANDLE_VALUE)
2832     {
2833         struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
2834
2835         if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
2836         {
2837             DWORD i;
2838
2839             for (i = 0; i < list->cDevices; i++)
2840                 SETUPDI_FreeDeviceInfo(
2841                         (struct DeviceInfo *)list->devices[i].Reserved);
2842             HeapFree(GetProcessHeap(), 0, list->devices);
2843             HeapFree(GetProcessHeap(), 0, list);
2844             ret = TRUE;
2845         }
2846     }
2847
2848     if (ret == FALSE)
2849         SetLastError(ERROR_INVALID_HANDLE);
2850
2851     return ret;
2852 }
2853
2854 /***********************************************************************
2855  *              SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2856  */
2857 BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
2858       HDEVINFO DeviceInfoSet,
2859       PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2860       PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
2861       DWORD DeviceInterfaceDetailDataSize,
2862       PDWORD RequiredSize,
2863       PSP_DEVINFO_DATA DeviceInfoData)
2864 {
2865     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2866     struct InterfaceInfo *info;
2867     DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath)
2868         + 1;
2869     BOOL ret = FALSE;
2870
2871     TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2872      DeviceInterfaceData, DeviceInterfaceDetailData,
2873      DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2874
2875     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2876             set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2877     {
2878         SetLastError(ERROR_INVALID_HANDLE);
2879         return FALSE;
2880     }
2881     if (!DeviceInterfaceData ||
2882             DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2883             !DeviceInterfaceData->Reserved)
2884     {
2885         SetLastError(ERROR_INVALID_PARAMETER);
2886         return FALSE;
2887     }
2888     if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize !=
2889             offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath) + sizeof(char)))
2890     {
2891         SetLastError(ERROR_INVALID_USER_BUFFER);
2892         return FALSE;
2893     }
2894     if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2895     {
2896         SetLastError(ERROR_INVALID_USER_BUFFER);
2897         return FALSE;
2898     }
2899     info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2900     if (info->symbolicLink)
2901         bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2902                 NULL, 0, NULL, NULL);
2903     if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2904     {
2905         if (info->symbolicLink)
2906             WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2907                     DeviceInterfaceDetailData->DevicePath,
2908                     DeviceInterfaceDetailDataSize -
2909                     offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath),
2910                     NULL, NULL);
2911         else
2912             DeviceInterfaceDetailData->DevicePath[0] = '\0';
2913         if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2914             *DeviceInfoData = *info->device;
2915         ret = TRUE;
2916     }
2917     else
2918     {
2919         if (RequiredSize)
2920             *RequiredSize = bytesNeeded;
2921         SetLastError(ERROR_INSUFFICIENT_BUFFER);
2922     }
2923     return ret;
2924 }
2925
2926 /***********************************************************************
2927  *              SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
2928  */
2929 BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
2930       HDEVINFO DeviceInfoSet,
2931       PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2932       PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
2933       DWORD DeviceInterfaceDetailDataSize,
2934       PDWORD RequiredSize,
2935       PSP_DEVINFO_DATA DeviceInfoData)
2936 {
2937     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2938     struct InterfaceInfo *info;
2939     DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)
2940         + sizeof(WCHAR); /* include NULL terminator */
2941     BOOL ret = FALSE;
2942
2943     TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2944      DeviceInterfaceData, DeviceInterfaceDetailData,
2945      DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2946
2947     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2948             set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2949     {
2950         SetLastError(ERROR_INVALID_HANDLE);
2951         return FALSE;
2952     }
2953     if (!DeviceInterfaceData ||
2954             DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2955             !DeviceInterfaceData->Reserved)
2956     {
2957         SetLastError(ERROR_INVALID_PARAMETER);
2958         return FALSE;
2959     }
2960     if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
2961             offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR) ||
2962             DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W)))
2963     {
2964         SetLastError(ERROR_INVALID_USER_BUFFER);
2965         return FALSE;
2966     }
2967     if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2968     {
2969         SetLastError(ERROR_INVALID_USER_BUFFER);
2970         return FALSE;
2971     }
2972     info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2973     if (info->symbolicLink)
2974         bytesNeeded += lstrlenW(info->symbolicLink);
2975     if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2976     {
2977         if (info->symbolicLink)
2978             lstrcpyW(DeviceInterfaceDetailData->DevicePath, info->symbolicLink);
2979         else
2980             DeviceInterfaceDetailData->DevicePath[0] = '\0';
2981         if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2982             *DeviceInfoData = *info->device;
2983         ret = TRUE;
2984     }
2985     else
2986     {
2987         if (RequiredSize)
2988             *RequiredSize = bytesNeeded;
2989         SetLastError(ERROR_INSUFFICIENT_BUFFER);
2990     }
2991     return ret;
2992 }
2993
2994 struct PropertyMapEntry
2995 {
2996     DWORD   regType;
2997     LPCSTR  nameA;
2998     LPCWSTR nameW;
2999 };
3000
3001 static struct PropertyMapEntry PropertyMap[] = {
3002     { REG_SZ, "DeviceDesc", DeviceDesc },
3003     { REG_MULTI_SZ, "HardwareId", HardwareId },
3004     { REG_MULTI_SZ, "CompatibleIDs", CompatibleIDs },
3005     { 0, NULL, NULL }, /* SPDRP_UNUSED0 */
3006     { REG_SZ, "Service", Service },
3007     { 0, NULL, NULL }, /* SPDRP_UNUSED1 */
3008     { 0, NULL, NULL }, /* SPDRP_UNUSED2 */
3009     { REG_SZ, "Class", Class },
3010     { REG_SZ, "ClassGUID", ClassGUID },
3011     { REG_SZ, "Driver", Driver },
3012     { REG_DWORD, "ConfigFlags", ConfigFlags },
3013     { REG_SZ, "Mfg", Mfg },
3014     { REG_SZ, "FriendlyName", FriendlyName },
3015     { REG_SZ, "LocationInformation", LocationInformation },
3016     { 0, NULL, NULL }, /* SPDRP_PHYSICAL_DEVICE_OBJECT_NAME */
3017     { REG_DWORD, "Capabilities", Capabilities },
3018     { REG_DWORD, "UINumber", UINumber },
3019     { REG_MULTI_SZ, "UpperFilters", UpperFilters },
3020     { REG_MULTI_SZ, "LowerFilters", LowerFilters },
3021 };
3022
3023 /***********************************************************************
3024  *              SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
3025  */
3026 BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
3027         HDEVINFO  DeviceInfoSet,
3028         PSP_DEVINFO_DATA  DeviceInfoData,
3029         DWORD   Property,
3030         PDWORD  PropertyRegDataType,
3031         PBYTE   PropertyBuffer,
3032         DWORD   PropertyBufferSize,
3033         PDWORD  RequiredSize)
3034 {
3035     BOOL ret = FALSE;
3036     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3037     struct DeviceInfo *devInfo;
3038
3039     TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
3040         Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
3041         RequiredSize);
3042
3043     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3044     {
3045         SetLastError(ERROR_INVALID_HANDLE);
3046         return FALSE;
3047     }
3048     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3049     {
3050         SetLastError(ERROR_INVALID_HANDLE);
3051         return FALSE;
3052     }
3053     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3054             || !DeviceInfoData->Reserved)
3055     {
3056         SetLastError(ERROR_INVALID_PARAMETER);
3057         return FALSE;
3058     }
3059     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3060     if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3061         && PropertyMap[Property].nameA)
3062     {
3063         DWORD size = PropertyBufferSize;
3064         LONG l = RegQueryValueExA(devInfo->key, PropertyMap[Property].nameA,
3065                 NULL, PropertyRegDataType, PropertyBuffer, &size);
3066
3067         if (RequiredSize)
3068             *RequiredSize = size;
3069         if (!PropertyBuffer)
3070             ; /* do nothing, ret is already FALSE, last error is already set */
3071         else if (!l)
3072             ret = TRUE;
3073         else
3074             SetLastError(l);
3075     }
3076     return ret;
3077 }
3078
3079 /***********************************************************************
3080  *              SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
3081  */
3082 BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
3083         HDEVINFO  DeviceInfoSet,
3084         PSP_DEVINFO_DATA  DeviceInfoData,
3085         DWORD   Property,
3086         PDWORD  PropertyRegDataType,
3087         PBYTE   PropertyBuffer,
3088         DWORD   PropertyBufferSize,
3089         PDWORD  RequiredSize)
3090 {
3091     BOOL ret = FALSE;
3092     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3093     struct DeviceInfo *devInfo;
3094
3095     TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
3096         Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
3097         RequiredSize);
3098
3099     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3100     {
3101         SetLastError(ERROR_INVALID_HANDLE);
3102         return FALSE;
3103     }
3104     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3105     {
3106         SetLastError(ERROR_INVALID_HANDLE);
3107         return FALSE;
3108     }
3109     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3110             || !DeviceInfoData->Reserved)
3111     {
3112         SetLastError(ERROR_INVALID_PARAMETER);
3113         return FALSE;
3114     }
3115     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3116     if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3117         && PropertyMap[Property].nameW)
3118     {
3119         DWORD size = PropertyBufferSize;
3120         LONG l = RegQueryValueExW(devInfo->key, PropertyMap[Property].nameW,
3121                 NULL, PropertyRegDataType, PropertyBuffer, &size);
3122
3123         if (RequiredSize)
3124             *RequiredSize = size;
3125         if (!PropertyBuffer)
3126             ; /* do nothing, ret is already FALSE, last error is already set */
3127         else if (!l)
3128             ret = TRUE;
3129         else
3130             SetLastError(l);
3131     }
3132     return ret;
3133 }
3134
3135 /***********************************************************************
3136  *              SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
3137  */
3138 BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
3139         HDEVINFO DeviceInfoSet,
3140         PSP_DEVINFO_DATA DeviceInfoData,
3141         DWORD Property,
3142         const BYTE *PropertyBuffer,
3143         DWORD PropertyBufferSize)
3144 {
3145     BOOL ret = FALSE;
3146     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3147     struct DeviceInfo *devInfo;
3148
3149     TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
3150         PropertyBuffer, PropertyBufferSize);
3151
3152     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3153     {
3154         SetLastError(ERROR_INVALID_HANDLE);
3155         return FALSE;
3156     }
3157     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3158     {
3159         SetLastError(ERROR_INVALID_HANDLE);
3160         return FALSE;
3161     }
3162     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3163             || !DeviceInfoData->Reserved)
3164     {
3165         SetLastError(ERROR_INVALID_PARAMETER);
3166         return FALSE;
3167     }
3168     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3169     if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3170         && PropertyMap[Property].nameA)
3171     {
3172         LONG l = RegSetValueExA(devInfo->key, PropertyMap[Property].nameA, 0,
3173                 PropertyMap[Property].regType, PropertyBuffer,
3174                 PropertyBufferSize);
3175         if (!l)
3176             ret = TRUE;
3177         else
3178             SetLastError(l);
3179     }
3180     return ret;
3181 }
3182
3183 /***********************************************************************
3184  *              SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
3185  */
3186 BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
3187         HDEVINFO DeviceInfoSet,
3188         PSP_DEVINFO_DATA DeviceInfoData,
3189         DWORD Property,
3190         const BYTE *PropertyBuffer,
3191         DWORD PropertyBufferSize)
3192 {
3193     BOOL ret = FALSE;
3194     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3195     struct DeviceInfo *devInfo;
3196
3197     TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
3198         PropertyBuffer, PropertyBufferSize);
3199
3200     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3201     {
3202         SetLastError(ERROR_INVALID_HANDLE);
3203         return FALSE;
3204     }
3205     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3206     {
3207         SetLastError(ERROR_INVALID_HANDLE);
3208         return FALSE;
3209     }
3210     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3211             || !DeviceInfoData->Reserved)
3212     {
3213         SetLastError(ERROR_INVALID_PARAMETER);
3214         return FALSE;
3215     }
3216     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3217     if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3218         && PropertyMap[Property].nameW)
3219     {
3220         LONG l = RegSetValueExW(devInfo->key, PropertyMap[Property].nameW, 0,
3221                 PropertyMap[Property].regType, PropertyBuffer,
3222                 PropertyBufferSize);
3223         if (!l)
3224             ret = TRUE;
3225         else
3226             SetLastError(l);
3227     }
3228     return ret;
3229 }
3230
3231 /***********************************************************************
3232  *              SetupDiInstallClassA (SETUPAPI.@)
3233  */
3234 BOOL WINAPI SetupDiInstallClassA(
3235         HWND hwndParent,
3236         PCSTR InfFileName,
3237         DWORD Flags,
3238         HSPFILEQ FileQueue)
3239 {
3240     UNICODE_STRING FileNameW;
3241     BOOL Result;
3242
3243     if (!InfFileName)
3244     {
3245         SetLastError(ERROR_INVALID_PARAMETER);
3246         return FALSE;
3247     }
3248     if (!RtlCreateUnicodeStringFromAsciiz(&FileNameW, InfFileName))
3249     {
3250         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3251         return FALSE;
3252     }
3253
3254     Result = SetupDiInstallClassW(hwndParent, FileNameW.Buffer, Flags, FileQueue);
3255
3256     RtlFreeUnicodeString(&FileNameW);
3257
3258     return Result;
3259 }
3260
3261 static HKEY CreateClassKey(HINF hInf)
3262 {
3263     static const WCHAR slash[] = { '\\',0 };
3264     WCHAR FullBuffer[MAX_PATH];
3265     WCHAR Buffer[MAX_PATH];
3266     DWORD RequiredSize;
3267     HKEY hClassKey;
3268
3269     if (!SetupGetLineTextW(NULL,
3270                            hInf,
3271                            Version,
3272                            ClassGUID,
3273                            Buffer,
3274                            MAX_PATH,
3275                            &RequiredSize))
3276     {
3277         return INVALID_HANDLE_VALUE;
3278     }
3279
3280     lstrcpyW(FullBuffer, ControlClass);
3281     lstrcatW(FullBuffer, slash);
3282     lstrcatW(FullBuffer, Buffer);
3283
3284     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3285                       FullBuffer,
3286                       0,
3287                       KEY_ALL_ACCESS,
3288                       &hClassKey))
3289     {
3290         if (!SetupGetLineTextW(NULL,
3291                                hInf,
3292                                Version,
3293                                Class,
3294                                Buffer,
3295                                MAX_PATH,
3296                                &RequiredSize))
3297         {
3298             return INVALID_HANDLE_VALUE;
3299         }
3300
3301         if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
3302                             FullBuffer,
3303                             0,
3304                             NULL,
3305                             REG_OPTION_NON_VOLATILE,
3306                             KEY_ALL_ACCESS,
3307                             NULL,
3308                             &hClassKey,
3309                             NULL))
3310         {
3311             return INVALID_HANDLE_VALUE;
3312         }
3313
3314     }
3315
3316     if (RegSetValueExW(hClassKey,
3317                        Class,
3318                        0,
3319                        REG_SZ,
3320                        (LPBYTE)Buffer,
3321                        RequiredSize * sizeof(WCHAR)))
3322     {
3323         RegCloseKey(hClassKey);
3324         RegDeleteKeyW(HKEY_LOCAL_MACHINE,
3325                       FullBuffer);
3326         return INVALID_HANDLE_VALUE;
3327     }
3328
3329     return hClassKey;
3330 }
3331
3332 /***********************************************************************
3333  *              SetupDiInstallClassW (SETUPAPI.@)
3334  */
3335 BOOL WINAPI SetupDiInstallClassW(
3336         HWND hwndParent,
3337         PCWSTR InfFileName,
3338         DWORD Flags,
3339         HSPFILEQ FileQueue)
3340 {
3341     WCHAR SectionName[MAX_PATH];
3342     DWORD SectionNameLength = 0;
3343     HINF hInf;
3344     BOOL bFileQueueCreated = FALSE;
3345     HKEY hClassKey;
3346
3347
3348     FIXME("\n");
3349
3350     if (!InfFileName)
3351     {
3352         SetLastError(ERROR_INVALID_PARAMETER);
3353         return FALSE;
3354     }
3355     if ((Flags & DI_NOVCP) && (FileQueue == NULL || FileQueue == INVALID_HANDLE_VALUE))
3356     {
3357         SetLastError(ERROR_INVALID_PARAMETER);
3358         return FALSE;
3359     }
3360
3361     /* Open the .inf file */
3362     hInf = SetupOpenInfFileW(InfFileName,
3363                              NULL,
3364                              INF_STYLE_WIN4,
3365                              NULL);
3366     if (hInf == INVALID_HANDLE_VALUE)
3367     {
3368
3369         return FALSE;
3370     }
3371
3372     /* Create or open the class registry key 'HKLM\\CurrentControlSet\\Class\\{GUID}' */
3373     hClassKey = CreateClassKey(hInf);
3374     if (hClassKey == INVALID_HANDLE_VALUE)
3375     {
3376         SetupCloseInfFile(hInf);
3377         return FALSE;
3378     }
3379
3380
3381     /* Try to append a layout file */
3382     SetupOpenAppendInfFileW(NULL, hInf, NULL);
3383
3384     /* Retrieve the actual section name */
3385     SetupDiGetActualSectionToInstallW(hInf,
3386                                       ClassInstall32,
3387                                       SectionName,
3388                                       MAX_PATH,
3389                                       &SectionNameLength,
3390                                       NULL);
3391
3392 #if 0
3393     if (!(Flags & DI_NOVCP))
3394     {
3395         FileQueue = SetupOpenFileQueue();
3396         if (FileQueue == INVALID_HANDLE_VALUE)
3397         {
3398             SetupCloseInfFile(hInf);
3399             return FALSE;
3400         }
3401
3402         bFileQueueCreated = TRUE;
3403
3404     }
3405 #endif
3406
3407     SetupInstallFromInfSectionW(NULL,
3408                                 hInf,
3409                                 SectionName,
3410                                 SPINST_COPYINF | SPINST_FILES | SPINST_REGISTRY,
3411                                 hClassKey,
3412                                 NULL,
3413                                 0,
3414                                 NULL,
3415                                 NULL,
3416                                 INVALID_HANDLE_VALUE,
3417                                 NULL);
3418
3419     /* FIXME: More code! */
3420
3421     if (bFileQueueCreated)
3422         SetupCloseFileQueue(FileQueue);
3423
3424     SetupCloseInfFile(hInf);
3425
3426     return TRUE;
3427 }
3428
3429
3430 /***********************************************************************
3431  *              SetupDiOpenClassRegKey  (SETUPAPI.@)
3432  */
3433 HKEY WINAPI SetupDiOpenClassRegKey(
3434         const GUID* ClassGuid,
3435         REGSAM samDesired)
3436 {
3437     return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3438                                      DIOCR_INSTALLER, NULL, NULL);
3439 }
3440
3441
3442 /***********************************************************************
3443  *              SetupDiOpenClassRegKeyExA  (SETUPAPI.@)
3444  */
3445 HKEY WINAPI SetupDiOpenClassRegKeyExA(
3446         const GUID* ClassGuid,
3447         REGSAM samDesired,
3448         DWORD Flags,
3449         PCSTR MachineName,
3450         PVOID Reserved)
3451 {
3452     PWSTR MachineNameW = NULL;
3453     HKEY hKey;
3454
3455     TRACE("\n");
3456
3457     if (MachineName)
3458     {
3459         MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
3460         if (MachineNameW == NULL)
3461             return INVALID_HANDLE_VALUE;
3462     }
3463
3464     hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3465                                      Flags, MachineNameW, Reserved);
3466
3467     MyFree(MachineNameW);
3468
3469     return hKey;
3470 }
3471
3472
3473 /***********************************************************************
3474  *              SetupDiOpenClassRegKeyExW  (SETUPAPI.@)
3475  */
3476 HKEY WINAPI SetupDiOpenClassRegKeyExW(
3477         const GUID* ClassGuid,
3478         REGSAM samDesired,
3479         DWORD Flags,
3480         PCWSTR MachineName,
3481         PVOID Reserved)
3482 {
3483     HKEY hClassesKey;
3484     HKEY key;
3485     LPCWSTR lpKeyName;
3486     LONG l;
3487
3488     if (MachineName != NULL)
3489     {
3490         FIXME("Remote access not supported yet!\n");
3491         return INVALID_HANDLE_VALUE;
3492     }
3493
3494     if (Flags == DIOCR_INSTALLER)
3495     {
3496         lpKeyName = ControlClass;
3497     }
3498     else if (Flags == DIOCR_INTERFACE)
3499     {
3500         lpKeyName = DeviceClasses;
3501     }
3502     else
3503     {
3504         ERR("Invalid Flags parameter!\n");
3505         SetLastError(ERROR_INVALID_PARAMETER);
3506         return INVALID_HANDLE_VALUE;
3507     }
3508
3509     if (!ClassGuid)
3510     {
3511         if ((l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3512                           lpKeyName,
3513                           0,
3514                           samDesired,
3515                           &hClassesKey)))
3516         {
3517             SetLastError(l);
3518             hClassesKey = INVALID_HANDLE_VALUE;
3519         }
3520         key = hClassesKey;
3521     }
3522     else
3523     {
3524         WCHAR bracedGuidString[39];
3525
3526         SETUPDI_GuidToString(ClassGuid, bracedGuidString);
3527
3528         if (!(l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3529                           lpKeyName,
3530                           0,
3531                           samDesired,
3532                           &hClassesKey)))
3533         {
3534             if ((l = RegOpenKeyExW(hClassesKey,
3535                               bracedGuidString,
3536                               0,
3537                               samDesired,
3538                               &key)))
3539             {
3540                 SetLastError(l);
3541                 key = INVALID_HANDLE_VALUE;
3542             }
3543             RegCloseKey(hClassesKey);
3544         }
3545         else
3546         {
3547             SetLastError(l);
3548             key = INVALID_HANDLE_VALUE;
3549         }
3550     }
3551     return key;
3552 }
3553
3554 /***********************************************************************
3555  *              SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3556  */
3557 BOOL WINAPI SetupDiOpenDeviceInterfaceW(
3558        HDEVINFO DeviceInfoSet,
3559        PCWSTR DevicePath,
3560        DWORD OpenFlags,
3561        PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3562 {
3563     FIXME("%p %s %08x %p\n",
3564         DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
3565     return FALSE;
3566 }
3567
3568 /***********************************************************************
3569  *              SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3570  */
3571 BOOL WINAPI SetupDiOpenDeviceInterfaceA(
3572        HDEVINFO DeviceInfoSet,
3573        PCSTR DevicePath,
3574        DWORD OpenFlags,
3575        PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3576 {
3577     FIXME("%p %s %08x %p\n", DeviceInfoSet,
3578         debugstr_a(DevicePath), OpenFlags, DeviceInterfaceData);
3579     return FALSE;
3580 }
3581
3582 /***********************************************************************
3583  *              SetupDiSetClassInstallParamsA (SETUPAPI.@)
3584  */
3585 BOOL WINAPI SetupDiSetClassInstallParamsA(
3586        HDEVINFO  DeviceInfoSet,
3587        PSP_DEVINFO_DATA DeviceInfoData,
3588        PSP_CLASSINSTALL_HEADER ClassInstallParams,
3589        DWORD ClassInstallParamsSize)
3590 {
3591     FIXME("%p %p %x %u\n",DeviceInfoSet, DeviceInfoData,
3592           ClassInstallParams->InstallFunction, ClassInstallParamsSize);
3593     return FALSE;
3594 }
3595
3596 /***********************************************************************
3597  *              SetupDiCallClassInstaller (SETUPAPI.@)
3598  */
3599 BOOL WINAPI SetupDiCallClassInstaller(
3600        DI_FUNCTION InstallFunction,
3601        HDEVINFO DeviceInfoSet,
3602        PSP_DEVINFO_DATA DeviceInfoData)
3603 {
3604     FIXME("%d %p %p\n", InstallFunction, DeviceInfoSet, DeviceInfoData);
3605     return FALSE;
3606 }
3607
3608 /***********************************************************************
3609  *              SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
3610  */
3611 BOOL WINAPI SetupDiGetDeviceInstallParamsA(
3612        HDEVINFO DeviceInfoSet,
3613        PSP_DEVINFO_DATA DeviceInfoData,
3614        PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
3615 {
3616     FIXME("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
3617     return FALSE;
3618 }
3619
3620 static HKEY SETUPDI_OpenDevKey(struct DeviceInfo *devInfo, REGSAM samDesired)
3621 {
3622     HKEY enumKey, key = INVALID_HANDLE_VALUE;
3623     LONG l;
3624
3625     l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
3626             NULL, &enumKey, NULL);
3627     if (!l)
3628     {
3629         RegOpenKeyExW(enumKey, devInfo->instanceId, 0, samDesired, &key);
3630         RegCloseKey(enumKey);
3631     }
3632     return key;
3633 }
3634
3635 static HKEY SETUPDI_OpenDrvKey(struct DeviceInfo *devInfo, REGSAM samDesired)
3636 {
3637     static const WCHAR slash[] = { '\\',0 };
3638     WCHAR classKeyPath[MAX_PATH];
3639     HKEY classKey, key = INVALID_HANDLE_VALUE;
3640     LONG l;
3641
3642     lstrcpyW(classKeyPath, ControlClass);
3643     lstrcatW(classKeyPath, slash);
3644     SETUPDI_GuidToString(&devInfo->set->ClassGuid,
3645             classKeyPath + lstrlenW(classKeyPath));
3646     l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
3647             KEY_ALL_ACCESS, NULL, &classKey, NULL);
3648     if (!l)
3649     {
3650         static const WCHAR fmt[] = { '%','0','4','u',0 };
3651         WCHAR devId[10];
3652
3653         sprintfW(devId, fmt, devInfo->devId);
3654         RegOpenKeyExW(classKey, devId, 0, samDesired, &key);
3655         RegCloseKey(classKey);
3656     }
3657     return key;
3658 }
3659
3660 /***********************************************************************
3661  *              SetupDiOpenDevRegKey (SETUPAPI.@)
3662  */
3663 HKEY WINAPI SetupDiOpenDevRegKey(
3664        HDEVINFO DeviceInfoSet,
3665        PSP_DEVINFO_DATA DeviceInfoData,
3666        DWORD Scope,
3667        DWORD HwProfile,
3668        DWORD KeyType,
3669        REGSAM samDesired)
3670 {
3671     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3672     struct DeviceInfo *devInfo;
3673     HKEY key = INVALID_HANDLE_VALUE;
3674
3675     TRACE("%p %p %d %d %d %x\n", DeviceInfoSet, DeviceInfoData,
3676           Scope, HwProfile, KeyType, samDesired);
3677
3678     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3679     {
3680         SetLastError(ERROR_INVALID_HANDLE);
3681         return INVALID_HANDLE_VALUE;
3682     }
3683     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3684     {
3685         SetLastError(ERROR_INVALID_HANDLE);
3686         return INVALID_HANDLE_VALUE;
3687     }
3688     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3689             || !DeviceInfoData->Reserved)
3690     {
3691         SetLastError(ERROR_INVALID_PARAMETER);
3692         return INVALID_HANDLE_VALUE;
3693     }
3694     if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
3695     {
3696         SetLastError(ERROR_INVALID_FLAGS);
3697         return INVALID_HANDLE_VALUE;
3698     }
3699     if (KeyType != DIREG_DEV && KeyType != DIREG_DRV)
3700     {
3701         SetLastError(ERROR_INVALID_FLAGS);
3702         return INVALID_HANDLE_VALUE;
3703     }
3704     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3705     if (devInfo->set != set)
3706     {
3707         SetLastError(ERROR_INVALID_PARAMETER);
3708         return INVALID_HANDLE_VALUE;
3709     }
3710     if (devInfo->phantom)
3711     {
3712         SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
3713         return INVALID_HANDLE_VALUE;
3714     }
3715     if (Scope != DICS_FLAG_GLOBAL)
3716         FIXME("unimplemented for scope %d\n", Scope);
3717     switch (KeyType)
3718     {
3719         case DIREG_DEV:
3720             key = SETUPDI_OpenDevKey(devInfo, samDesired);
3721             break;
3722         case DIREG_DRV:
3723             key = SETUPDI_OpenDrvKey(devInfo, samDesired);
3724             break;
3725         default:
3726             WARN("unknown KeyType %d\n", KeyType);
3727     }
3728     return key;
3729 }
3730
3731 static BOOL SETUPDI_DeleteDevKey(struct DeviceInfo *devInfo)
3732 {
3733     HKEY enumKey;
3734     BOOL ret = FALSE;
3735     LONG l;
3736
3737     l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
3738             NULL, &enumKey, NULL);
3739     if (!l)
3740     {
3741         ret = RegDeleteTreeW(enumKey, devInfo->instanceId);
3742         RegCloseKey(enumKey);
3743     }
3744     else
3745         SetLastError(l);
3746     return ret;
3747 }
3748
3749 static BOOL SETUPDI_DeleteDrvKey(struct DeviceInfo *devInfo)
3750 {
3751     static const WCHAR slash[] = { '\\',0 };
3752     WCHAR classKeyPath[MAX_PATH];
3753     HKEY classKey;
3754     LONG l;
3755     BOOL ret = FALSE;
3756
3757     lstrcpyW(classKeyPath, ControlClass);
3758     lstrcatW(classKeyPath, slash);
3759     SETUPDI_GuidToString(&devInfo->set->ClassGuid,
3760             classKeyPath + lstrlenW(classKeyPath));
3761     l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
3762             KEY_ALL_ACCESS, NULL, &classKey, NULL);
3763     if (!l)
3764     {
3765         static const WCHAR fmt[] = { '%','0','4','u',0 };
3766         WCHAR devId[10];
3767
3768         sprintfW(devId, fmt, devInfo->devId);
3769         ret = RegDeleteTreeW(classKey, devId);
3770         RegCloseKey(classKey);
3771     }
3772     else
3773         SetLastError(l);
3774     return ret;
3775 }
3776
3777 /***********************************************************************
3778  *              SetupDiOpenDevRegKey (SETUPAPI.@)
3779  */
3780 BOOL WINAPI SetupDiDeleteDevRegKey(
3781        HDEVINFO DeviceInfoSet,
3782        PSP_DEVINFO_DATA DeviceInfoData,
3783        DWORD Scope,
3784        DWORD HwProfile,
3785        DWORD KeyType)
3786 {
3787     struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3788     struct DeviceInfo *devInfo;
3789     BOOL ret = FALSE;
3790
3791     TRACE("%p %p %d %d %d\n", DeviceInfoSet, DeviceInfoData, Scope, HwProfile,
3792             KeyType);
3793
3794     if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3795     {
3796         SetLastError(ERROR_INVALID_HANDLE);
3797         return FALSE;
3798     }
3799     if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3800     {
3801         SetLastError(ERROR_INVALID_HANDLE);
3802         return FALSE;
3803     }
3804     if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3805             || !DeviceInfoData->Reserved)
3806     {
3807         SetLastError(ERROR_INVALID_PARAMETER);
3808         return FALSE;
3809     }
3810     if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
3811     {
3812         SetLastError(ERROR_INVALID_FLAGS);
3813         return FALSE;
3814     }
3815     if (KeyType != DIREG_DEV && KeyType != DIREG_DRV && KeyType != DIREG_BOTH)
3816     {
3817         SetLastError(ERROR_INVALID_FLAGS);
3818         return FALSE;
3819     }
3820     devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3821     if (devInfo->set != set)
3822     {
3823         SetLastError(ERROR_INVALID_PARAMETER);
3824         return FALSE;
3825     }
3826     if (devInfo->phantom)
3827     {
3828         SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
3829         return FALSE;
3830     }
3831     if (Scope != DICS_FLAG_GLOBAL)
3832         FIXME("unimplemented for scope %d\n", Scope);
3833     switch (KeyType)
3834     {
3835         case DIREG_DEV:
3836             ret = SETUPDI_DeleteDevKey(devInfo);
3837             break;
3838         case DIREG_DRV:
3839             ret = SETUPDI_DeleteDrvKey(devInfo);
3840             break;
3841         case DIREG_BOTH:
3842             ret = SETUPDI_DeleteDevKey(devInfo);
3843             if (ret)
3844                 ret = SETUPDI_DeleteDrvKey(devInfo);
3845             break;
3846         default:
3847             WARN("unknown KeyType %d\n", KeyType);
3848     }
3849     return ret;
3850 }
3851
3852 /***********************************************************************
3853  *              CM_Get_Device_IDA  (SETUPAPI.@)
3854  */
3855 CONFIGRET WINAPI CM_Get_Device_IDA( DEVINST dnDevInst, PSTR Buffer,
3856                                    ULONG  BufferLen, ULONG  ulFlags)
3857 {
3858     struct DeviceInfo *devInfo = GlobalLock((HANDLE)dnDevInst);
3859
3860     TRACE("%x->%p, %p, %u %u\n", dnDevInst, devInfo, Buffer, BufferLen, ulFlags);
3861
3862     if (!devInfo)
3863         return CR_NO_SUCH_DEVINST;
3864
3865     WideCharToMultiByte(CP_ACP, 0, devInfo->instanceId, -1, Buffer, BufferLen, 0, 0);
3866     TRACE("Returning %s\n", debugstr_a(Buffer));
3867     return CR_SUCCESS;
3868 }
3869
3870 /***********************************************************************
3871  *              CM_Get_Device_IDW  (SETUPAPI.@)
3872  */
3873 CONFIGRET WINAPI CM_Get_Device_IDW( DEVINST dnDevInst, LPWSTR Buffer,
3874                                    ULONG  BufferLen, ULONG  ulFlags)
3875 {
3876     struct DeviceInfo *devInfo = GlobalLock((HANDLE)dnDevInst);
3877
3878     TRACE("%x->%p, %p, %u %u\n", dnDevInst, devInfo, Buffer, BufferLen, ulFlags);
3879
3880     if (!devInfo)
3881     {
3882         WARN("dev instance %d not found!\n", dnDevInst);
3883         return CR_NO_SUCH_DEVINST;
3884     }
3885
3886     lstrcpynW(Buffer, devInfo->instanceId, BufferLen);
3887     TRACE("Returning %s\n", debugstr_w(Buffer));
3888     GlobalUnlock((HANDLE)dnDevInst);
3889     return CR_SUCCESS;
3890 }
3891
3892
3893
3894 /***********************************************************************
3895  *              CM_Get_Device_ID_Size  (SETUPAPI.@)
3896  */
3897 CONFIGRET WINAPI CM_Get_Device_ID_Size( PULONG  pulLen, DEVINST dnDevInst,
3898                                         ULONG  ulFlags)
3899 {
3900     struct DeviceInfo *ppdevInfo = GlobalLock((HANDLE)dnDevInst);
3901
3902     TRACE("%x->%p, %p, %u\n", dnDevInst, ppdevInfo, pulLen, ulFlags);
3903
3904     if (!ppdevInfo)
3905     {
3906         WARN("dev instance %d not found!\n", dnDevInst);
3907         return CR_NO_SUCH_DEVINST;
3908     }
3909
3910     *pulLen = lstrlenW(ppdevInfo->instanceId);
3911     GlobalUnlock((HANDLE)dnDevInst);
3912     return CR_SUCCESS;
3913 }