inetcomm: Add an implementation of IPOP3Transport::CommandRETR.
[wine/wine64.git] / dlls / setupapi / devinst.c
blobaf4f4d3b067e9fcc6b8cff51dcb3274865006e6d
1 /*
2 * SetupAPI device installer
4 * Copyright 2000 Andreas Mohr for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
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"
44 #include "setupapi_private.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
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};
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};
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};
91 /* is used to identify if a DeviceInfoSet pointer is
92 valid or not */
93 #define SETUP_DEVICE_INFO_SET_MAGIC 0xd00ff056
95 struct DeviceInfoSet
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 struct list devices;
104 struct DeviceInstance
106 struct list entry;
107 SP_DEVINFO_DATA data;
110 /* Pointed to by SP_DEVICE_INTERFACE_DATA's Reserved member */
111 struct InterfaceInfo
113 LPWSTR referenceString;
114 LPWSTR symbolicLink;
115 PSP_DEVINFO_DATA device;
118 /* A device may have multiple instances of the same interface, so this holds
119 * each instance belonging to a particular interface.
121 struct InterfaceInstances
123 GUID guid;
124 DWORD cInstances;
125 DWORD cInstancesAllocated;
126 SP_DEVICE_INTERFACE_DATA *instances;
127 struct list entry;
130 /* Pointed to by SP_DEVINFO_DATA's Reserved member */
131 struct DeviceInfo
133 struct DeviceInfoSet *set;
134 HKEY key;
135 BOOL phantom;
136 DWORD devId;
137 LPWSTR instanceId;
138 struct list interfaces;
141 static void SETUPDI_GuidToString(const GUID *guid, LPWSTR guidStr)
143 static const WCHAR fmt[] = {'{','%','0','8','X','-','%','0','4','X','-',
144 '%','0','4','X','-','%','0','2','X','%','0','2','X','-','%','0','2',
145 'X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%',
146 '0','2','X','}',0};
148 sprintfW(guidStr, fmt, guid->Data1, guid->Data2, guid->Data3,
149 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
150 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
153 static void SETUPDI_FreeInterfaceInstances(struct InterfaceInstances *instances)
155 DWORD i;
157 for (i = 0; i < instances->cInstances; i++)
159 struct InterfaceInfo *ifaceInfo =
160 (struct InterfaceInfo *)instances->instances[i].Reserved;
162 if (ifaceInfo->device && ifaceInfo->device->Reserved)
164 struct DeviceInfo *devInfo =
165 (struct DeviceInfo *)ifaceInfo->device->Reserved;
167 if (devInfo->phantom)
168 SetupDiDeleteDeviceInterfaceRegKey(devInfo->set,
169 &instances->instances[i], 0);
171 HeapFree(GetProcessHeap(), 0, ifaceInfo->referenceString);
172 HeapFree(GetProcessHeap(), 0, ifaceInfo->symbolicLink);
173 HeapFree(GetProcessHeap(), 0, ifaceInfo);
175 HeapFree(GetProcessHeap(), 0, instances->instances);
178 /* Finds the interface with interface class InterfaceClassGuid in the device.
179 * Returns TRUE if found, and updates *interface to point to device's
180 * interfaces member where the given interface was found.
181 * Returns FALSE if not found.
183 static BOOL SETUPDI_FindInterface(const struct DeviceInfo *devInfo,
184 const GUID *InterfaceClassGuid, struct InterfaceInstances **interface)
186 BOOL found = FALSE;
187 struct InterfaceInstances *iface;
189 TRACE("%s\n", debugstr_guid(InterfaceClassGuid));
191 LIST_FOR_EACH_ENTRY(iface, &devInfo->interfaces, struct InterfaceInstances,
192 entry)
194 if (IsEqualGUID(&iface->guid, InterfaceClassGuid))
196 *interface = iface;
197 found = TRUE;
198 break;
201 TRACE("returning %d (%p)\n", found, found ? *interface : NULL);
202 return found;
205 /* Finds the interface instance with reference string ReferenceString in the
206 * interface instance map. Returns TRUE if found, and updates instanceIndex to
207 * the index of the interface instance's instances member
208 * where the given instance was found. Returns FALSE if not found.
210 static BOOL SETUPDI_FindInterfaceInstance(
211 const struct InterfaceInstances *instances,
212 LPCWSTR ReferenceString, DWORD *instanceIndex)
214 BOOL found = FALSE;
215 DWORD i;
217 TRACE("%s\n", debugstr_w(ReferenceString));
219 for (i = 0; !found && i < instances->cInstances; i++)
221 SP_DEVICE_INTERFACE_DATA *ifaceData = &instances->instances[i];
222 struct InterfaceInfo *ifaceInfo =
223 (struct InterfaceInfo *)ifaceData->Reserved;
225 if (!ReferenceString && !ifaceInfo->referenceString)
227 *instanceIndex = i;
228 found = TRUE;
230 else if (ReferenceString && ifaceInfo->referenceString &&
231 !lstrcmpiW(ifaceInfo->referenceString, ReferenceString))
233 *instanceIndex = i;
234 found = TRUE;
237 TRACE("returning %d (%d)\n", found, found ? *instanceIndex : 0);
238 return found;
241 static LPWSTR SETUPDI_CreateSymbolicLinkPath(LPCWSTR instanceId,
242 const GUID *InterfaceClassGuid, LPCWSTR ReferenceString)
244 static const WCHAR fmt[] = {'\\','\\','?','\\','%','s','#','%','s',0};
245 WCHAR guidStr[39];
246 DWORD len;
247 LPWSTR ret;
249 SETUPDI_GuidToString(InterfaceClassGuid, guidStr);
250 /* omit length of format specifiers, but include NULL terminator: */
251 len = lstrlenW(fmt) - 4 + 1;
252 len += lstrlenW(instanceId) + lstrlenW(guidStr);
253 if (ReferenceString && *ReferenceString)
255 /* space for a hash between string and reference string: */
256 len += lstrlenW(ReferenceString) + 1;
258 ret = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
259 if (ret)
261 int printed = sprintfW(ret, fmt, instanceId, guidStr);
262 LPWSTR ptr;
264 /* replace '\\' with '#' after the "\\\\?\\" beginning */
265 for (ptr = strchrW(ret + 4, '\\'); ptr; ptr = strchrW(ptr + 1, '\\'))
266 *ptr = '#';
267 if (ReferenceString && *ReferenceString)
269 ret[printed] = '\\';
270 lstrcpyW(ret + printed + 1, ReferenceString);
273 return ret;
276 /* Adds an interface with the given interface class and reference string to
277 * the device, if it doesn't already exist in the device. If iface is not
278 * NULL, returns a pointer to the newly added (or already existing) interface.
280 static BOOL SETUPDI_AddInterfaceInstance(PSP_DEVINFO_DATA DeviceInfoData,
281 const GUID *InterfaceClassGuid, LPCWSTR ReferenceString,
282 SP_DEVICE_INTERFACE_DATA **ifaceData)
284 struct DeviceInfo *devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
285 BOOL newInterface = FALSE, ret;
286 struct InterfaceInstances *iface = NULL;
288 TRACE("%p %s %s %p\n", devInfo, debugstr_guid(InterfaceClassGuid),
289 debugstr_w(ReferenceString), iface);
291 if (!(ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
293 iface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
294 sizeof(struct InterfaceInstances));
295 if (iface)
297 list_add_tail(&devInfo->interfaces, &iface->entry);
298 newInterface = TRUE;
301 if (iface)
303 DWORD instanceIndex = 0;
305 if (!(ret = SETUPDI_FindInterfaceInstance(iface, ReferenceString,
306 &instanceIndex)))
308 SP_DEVICE_INTERFACE_DATA *instance = NULL;
310 if (!iface->cInstancesAllocated)
312 iface->instances = HeapAlloc(GetProcessHeap(), 0,
313 sizeof(SP_DEVICE_INTERFACE_DATA));
314 if (iface->instances)
315 instance = &iface->instances[iface->cInstancesAllocated++];
317 else if (iface->cInstances == iface->cInstancesAllocated)
319 iface->instances = HeapReAlloc(GetProcessHeap(), 0,
320 iface->instances,
321 (iface->cInstancesAllocated + 1) *
322 sizeof(SP_DEVICE_INTERFACE_DATA));
323 if (iface->instances)
324 instance = &iface->instances[iface->cInstancesAllocated++];
326 else
327 instance = &iface->instances[iface->cInstances];
328 if (instance)
330 struct InterfaceInfo *ifaceInfo = HeapAlloc(GetProcessHeap(),
331 0, sizeof(struct InterfaceInfo));
333 if (ifaceInfo)
335 ret = TRUE;
336 ifaceInfo->device = DeviceInfoData;
337 ifaceInfo->symbolicLink = SETUPDI_CreateSymbolicLinkPath(
338 devInfo->instanceId, InterfaceClassGuid,
339 ReferenceString);
340 if (ReferenceString)
342 ifaceInfo->referenceString =
343 HeapAlloc(GetProcessHeap(), 0,
344 (lstrlenW(ReferenceString) + 1) *
345 sizeof(WCHAR));
346 if (ifaceInfo->referenceString)
347 lstrcpyW(ifaceInfo->referenceString,
348 ReferenceString);
349 else
350 ret = FALSE;
352 else
353 ifaceInfo->referenceString = NULL;
354 if (ret)
356 HKEY key;
358 iface->cInstances++;
359 instance->cbSize =
360 sizeof(SP_DEVICE_INTERFACE_DATA);
361 instance->InterfaceClassGuid = *InterfaceClassGuid;
362 instance->Flags = SPINT_ACTIVE; /* FIXME */
363 instance->Reserved = (ULONG_PTR)ifaceInfo;
364 if (newInterface)
365 iface->guid = *InterfaceClassGuid;
366 key = SetupDiCreateDeviceInterfaceRegKeyW(devInfo->set,
367 instance, 0, KEY_WRITE, NULL, NULL);
368 if (key != INVALID_HANDLE_VALUE)
370 RegSetValueExW(key, SymbolicLink, 0, REG_SZ,
371 (BYTE *)ifaceInfo->symbolicLink,
372 lstrlenW(ifaceInfo->symbolicLink) *
373 sizeof(WCHAR));
374 RegCloseKey(key);
376 if (ifaceData)
377 *ifaceData = instance;
379 else
380 HeapFree(GetProcessHeap(), 0, ifaceInfo);
384 else
386 if (ifaceData)
387 *ifaceData = &iface->instances[instanceIndex];
390 else
391 ret = FALSE;
392 TRACE("returning %d\n", ret);
393 return ret;
396 static BOOL SETUPDI_SetInterfaceSymbolicLink(SP_DEVICE_INTERFACE_DATA *iface,
397 LPCWSTR symbolicLink)
399 struct InterfaceInfo *info = (struct InterfaceInfo *)iface->Reserved;
400 BOOL ret = FALSE;
402 if (info)
404 HeapFree(GetProcessHeap(), 0, info->symbolicLink);
405 info->symbolicLink = HeapAlloc(GetProcessHeap(), 0,
406 (lstrlenW(symbolicLink) + 1) * sizeof(WCHAR));
407 if (info->symbolicLink)
409 lstrcpyW(info->symbolicLink, symbolicLink);
410 ret = TRUE;
413 return ret;
416 static HKEY SETUPDI_CreateDevKey(struct DeviceInfo *devInfo)
418 HKEY enumKey, key = INVALID_HANDLE_VALUE;
419 LONG l;
421 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
422 NULL, &enumKey, NULL);
423 if (!l)
425 RegCreateKeyExW(enumKey, devInfo->instanceId, 0, NULL, 0,
426 KEY_READ | KEY_WRITE, NULL, &key, NULL);
427 RegCloseKey(enumKey);
429 return key;
432 static HKEY SETUPDI_CreateDrvKey(struct DeviceInfo *devInfo)
434 static const WCHAR slash[] = { '\\',0 };
435 WCHAR classKeyPath[MAX_PATH];
436 HKEY classKey, key = INVALID_HANDLE_VALUE;
437 LONG l;
439 lstrcpyW(classKeyPath, ControlClass);
440 lstrcatW(classKeyPath, slash);
441 SETUPDI_GuidToString(&devInfo->set->ClassGuid,
442 classKeyPath + lstrlenW(classKeyPath));
443 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
444 KEY_ALL_ACCESS, NULL, &classKey, NULL);
445 if (!l)
447 static const WCHAR fmt[] = { '%','0','4','u',0 };
448 WCHAR devId[10];
450 sprintfW(devId, fmt, devInfo->devId);
451 RegCreateKeyExW(classKey, devId, 0, NULL, 0, KEY_READ | KEY_WRITE,
452 NULL, &key, NULL);
453 RegCloseKey(classKey);
455 return key;
458 static struct DeviceInfo *SETUPDI_AllocateDeviceInfo(struct DeviceInfoSet *set,
459 DWORD devId, LPCWSTR instanceId, BOOL phantom)
461 struct DeviceInfo *devInfo = NULL;
462 HANDLE devInst = GlobalAlloc(GMEM_FIXED, sizeof(struct DeviceInfo));
463 if (devInst)
464 devInfo = GlobalLock(devInst);
466 if (devInfo)
468 devInfo->set = set;
469 devInfo->devId = (DWORD)devInst;
471 devInfo->instanceId = HeapAlloc(GetProcessHeap(), 0,
472 (lstrlenW(instanceId) + 1) * sizeof(WCHAR));
473 if (devInfo->instanceId)
475 devInfo->key = INVALID_HANDLE_VALUE;
476 devInfo->phantom = phantom;
477 lstrcpyW(devInfo->instanceId, instanceId);
478 struprW(devInfo->instanceId);
479 devInfo->key = SETUPDI_CreateDevKey(devInfo);
480 if (devInfo->key != INVALID_HANDLE_VALUE)
482 if (phantom)
483 RegSetValueExW(devInfo->key, Phantom, 0, REG_DWORD,
484 (LPBYTE)&phantom, sizeof(phantom));
486 list_init(&devInfo->interfaces);
487 GlobalUnlock(devInst);
489 else
491 GlobalUnlock(devInst);
492 GlobalFree(devInst);
493 devInfo = NULL;
496 return devInfo;
499 static void SETUPDI_FreeDeviceInfo(struct DeviceInfo *devInfo)
501 struct InterfaceInstances *iface, *next;
503 if (devInfo->key != INVALID_HANDLE_VALUE)
504 RegCloseKey(devInfo->key);
505 if (devInfo->phantom)
507 HKEY enumKey;
508 LONG l;
510 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0,
511 KEY_ALL_ACCESS, NULL, &enumKey, NULL);
512 if (!l)
514 RegDeleteTreeW(enumKey, devInfo->instanceId);
515 RegCloseKey(enumKey);
518 HeapFree(GetProcessHeap(), 0, devInfo->instanceId);
519 LIST_FOR_EACH_ENTRY_SAFE(iface, next, &devInfo->interfaces,
520 struct InterfaceInstances, entry)
522 list_remove(&iface->entry);
523 SETUPDI_FreeInterfaceInstances(iface);
524 HeapFree(GetProcessHeap(), 0, iface);
526 GlobalFree((HANDLE)devInfo->devId);
529 /* Adds a device with GUID guid and identifer devInst to set. Allocates a
530 * struct DeviceInfo, and points the returned device info's Reserved member
531 * to it. "Phantom" devices are deleted from the registry when closed.
532 * Returns a pointer to the newly allocated device info.
534 static BOOL SETUPDI_AddDeviceToSet(struct DeviceInfoSet *set,
535 const GUID *guid,
536 DWORD devInst,
537 LPCWSTR instanceId,
538 BOOL phantom,
539 SP_DEVINFO_DATA **dev)
541 BOOL ret = FALSE;
542 struct DeviceInfo *devInfo = SETUPDI_AllocateDeviceInfo(set, set->cDevices,
543 instanceId, phantom);
545 TRACE("%p, %s, %d, %s, %d\n", set, debugstr_guid(guid), devInst,
546 debugstr_w(instanceId), phantom);
548 if (devInfo)
550 struct DeviceInstance *devInst =
551 HeapAlloc(GetProcessHeap(), 0, sizeof(struct DeviceInstance));
553 if (devInst)
555 WCHAR classGuidStr[39];
557 list_add_tail(&set->devices, &devInst->entry);
558 set->cDevices++;
559 devInst->data.cbSize = sizeof(SP_DEVINFO_DATA);
560 devInst->data.ClassGuid = *guid;
561 devInst->data.DevInst = devInfo->devId;
562 devInst->data.Reserved = (ULONG_PTR)devInfo;
563 SETUPDI_GuidToString(guid, classGuidStr);
564 SetupDiSetDeviceRegistryPropertyW((HDEVINFO)set,
565 &devInst->data, SPDRP_CLASSGUID, (const BYTE *)classGuidStr,
566 lstrlenW(classGuidStr) * sizeof(WCHAR));
567 if (dev) *dev = &devInst->data;
568 ret = TRUE;
570 else
572 HeapFree(GetProcessHeap(), 0, devInfo);
573 SetLastError(ERROR_OUTOFMEMORY);
576 return ret;
579 /***********************************************************************
580 * SetupDiBuildClassInfoList (SETUPAPI.@)
582 * Returns a list of setup class GUIDs that identify the classes
583 * that are installed on a local machine.
585 * PARAMS
586 * Flags [I] control exclusion of classes from the list.
587 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
588 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
589 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
591 * RETURNS
592 * Success: TRUE.
593 * Failure: FALSE.
595 BOOL WINAPI SetupDiBuildClassInfoList(
596 DWORD Flags,
597 LPGUID ClassGuidList,
598 DWORD ClassGuidListSize,
599 PDWORD RequiredSize)
601 TRACE("\n");
602 return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
603 ClassGuidListSize, RequiredSize,
604 NULL, NULL);
607 /***********************************************************************
608 * SetupDiBuildClassInfoListExA (SETUPAPI.@)
610 * Returns a list of setup class GUIDs that identify the classes
611 * that are installed on a local or remote macine.
613 * PARAMS
614 * Flags [I] control exclusion of classes from the list.
615 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
616 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
617 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
618 * MachineName [I] name of a remote machine.
619 * Reserved [I] must be NULL.
621 * RETURNS
622 * Success: TRUE.
623 * Failure: FALSE.
625 BOOL WINAPI SetupDiBuildClassInfoListExA(
626 DWORD Flags,
627 LPGUID ClassGuidList,
628 DWORD ClassGuidListSize,
629 PDWORD RequiredSize,
630 LPCSTR MachineName,
631 PVOID Reserved)
633 LPWSTR MachineNameW = NULL;
634 BOOL bResult;
636 TRACE("\n");
638 if (MachineName)
640 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
641 if (MachineNameW == NULL) return FALSE;
644 bResult = SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
645 ClassGuidListSize, RequiredSize,
646 MachineNameW, Reserved);
648 MyFree(MachineNameW);
650 return bResult;
653 /***********************************************************************
654 * SetupDiBuildClassInfoListExW (SETUPAPI.@)
656 * Returns a list of setup class GUIDs that identify the classes
657 * that are installed on a local or remote macine.
659 * PARAMS
660 * Flags [I] control exclusion of classes from the list.
661 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
662 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
663 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
664 * MachineName [I] name of a remote machine.
665 * Reserved [I] must be NULL.
667 * RETURNS
668 * Success: TRUE.
669 * Failure: FALSE.
671 BOOL WINAPI SetupDiBuildClassInfoListExW(
672 DWORD Flags,
673 LPGUID ClassGuidList,
674 DWORD ClassGuidListSize,
675 PDWORD RequiredSize,
676 LPCWSTR MachineName,
677 PVOID Reserved)
679 WCHAR szKeyName[40];
680 HKEY hClassesKey;
681 HKEY hClassKey;
682 DWORD dwLength;
683 DWORD dwIndex;
684 LONG lError;
685 DWORD dwGuidListIndex = 0;
687 TRACE("\n");
689 if (RequiredSize != NULL)
690 *RequiredSize = 0;
692 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
693 KEY_ALL_ACCESS,
694 DIOCR_INSTALLER,
695 MachineName,
696 Reserved);
697 if (hClassesKey == INVALID_HANDLE_VALUE)
699 return FALSE;
702 for (dwIndex = 0; ; dwIndex++)
704 dwLength = 40;
705 lError = RegEnumKeyExW(hClassesKey,
706 dwIndex,
707 szKeyName,
708 &dwLength,
709 NULL,
710 NULL,
711 NULL,
712 NULL);
713 TRACE("RegEnumKeyExW() returns %d\n", lError);
714 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
716 TRACE("Key name: %p\n", szKeyName);
718 if (RegOpenKeyExW(hClassesKey,
719 szKeyName,
721 KEY_ALL_ACCESS,
722 &hClassKey))
724 RegCloseKey(hClassesKey);
725 return FALSE;
728 if (!RegQueryValueExW(hClassKey,
729 NoUseClass,
730 NULL,
731 NULL,
732 NULL,
733 NULL))
735 TRACE("'NoUseClass' value found!\n");
736 RegCloseKey(hClassKey);
737 continue;
740 if ((Flags & DIBCI_NOINSTALLCLASS) &&
741 (!RegQueryValueExW(hClassKey,
742 NoInstallClass,
743 NULL,
744 NULL,
745 NULL,
746 NULL)))
748 TRACE("'NoInstallClass' value found!\n");
749 RegCloseKey(hClassKey);
750 continue;
753 if ((Flags & DIBCI_NODISPLAYCLASS) &&
754 (!RegQueryValueExW(hClassKey,
755 NoDisplayClass,
756 NULL,
757 NULL,
758 NULL,
759 NULL)))
761 TRACE("'NoDisplayClass' value found!\n");
762 RegCloseKey(hClassKey);
763 continue;
766 RegCloseKey(hClassKey);
768 TRACE("Guid: %p\n", szKeyName);
769 if (dwGuidListIndex < ClassGuidListSize)
771 if (szKeyName[0] == '{' && szKeyName[37] == '}')
773 szKeyName[37] = 0;
775 TRACE("Guid: %p\n", &szKeyName[1]);
777 UuidFromStringW(&szKeyName[1],
778 &ClassGuidList[dwGuidListIndex]);
781 dwGuidListIndex++;
784 if (lError != ERROR_SUCCESS)
785 break;
788 RegCloseKey(hClassesKey);
790 if (RequiredSize != NULL)
791 *RequiredSize = dwGuidListIndex;
793 if (ClassGuidListSize < dwGuidListIndex)
795 SetLastError(ERROR_INSUFFICIENT_BUFFER);
796 return FALSE;
799 return TRUE;
802 /***********************************************************************
803 * SetupDiClassGuidsFromNameA (SETUPAPI.@)
805 BOOL WINAPI SetupDiClassGuidsFromNameA(
806 LPCSTR ClassName,
807 LPGUID ClassGuidList,
808 DWORD ClassGuidListSize,
809 PDWORD RequiredSize)
811 return SetupDiClassGuidsFromNameExA(ClassName, ClassGuidList,
812 ClassGuidListSize, RequiredSize,
813 NULL, NULL);
816 /***********************************************************************
817 * SetupDiClassGuidsFromNameW (SETUPAPI.@)
819 BOOL WINAPI SetupDiClassGuidsFromNameW(
820 LPCWSTR ClassName,
821 LPGUID ClassGuidList,
822 DWORD ClassGuidListSize,
823 PDWORD RequiredSize)
825 return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
826 ClassGuidListSize, RequiredSize,
827 NULL, NULL);
830 /***********************************************************************
831 * SetupDiClassGuidsFromNameExA (SETUPAPI.@)
833 BOOL WINAPI SetupDiClassGuidsFromNameExA(
834 LPCSTR ClassName,
835 LPGUID ClassGuidList,
836 DWORD ClassGuidListSize,
837 PDWORD RequiredSize,
838 LPCSTR MachineName,
839 PVOID Reserved)
841 LPWSTR ClassNameW = NULL;
842 LPWSTR MachineNameW = NULL;
843 BOOL bResult;
845 ClassNameW = MultiByteToUnicode(ClassName, CP_ACP);
846 if (ClassNameW == NULL)
847 return FALSE;
849 if (MachineName)
851 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
852 if (MachineNameW == NULL)
854 MyFree(ClassNameW);
855 return FALSE;
859 bResult = SetupDiClassGuidsFromNameExW(ClassNameW, ClassGuidList,
860 ClassGuidListSize, RequiredSize,
861 MachineNameW, Reserved);
863 MyFree(MachineNameW);
864 MyFree(ClassNameW);
866 return bResult;
869 /***********************************************************************
870 * SetupDiClassGuidsFromNameExW (SETUPAPI.@)
872 BOOL WINAPI SetupDiClassGuidsFromNameExW(
873 LPCWSTR ClassName,
874 LPGUID ClassGuidList,
875 DWORD ClassGuidListSize,
876 PDWORD RequiredSize,
877 LPCWSTR MachineName,
878 PVOID Reserved)
880 WCHAR szKeyName[40];
881 WCHAR szClassName[256];
882 HKEY hClassesKey;
883 HKEY hClassKey;
884 DWORD dwLength;
885 DWORD dwIndex;
886 LONG lError;
887 DWORD dwGuidListIndex = 0;
889 if (RequiredSize != NULL)
890 *RequiredSize = 0;
892 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
893 KEY_ALL_ACCESS,
894 DIOCR_INSTALLER,
895 MachineName,
896 Reserved);
897 if (hClassesKey == INVALID_HANDLE_VALUE)
899 return FALSE;
902 for (dwIndex = 0; ; dwIndex++)
904 dwLength = 40;
905 lError = RegEnumKeyExW(hClassesKey,
906 dwIndex,
907 szKeyName,
908 &dwLength,
909 NULL,
910 NULL,
911 NULL,
912 NULL);
913 TRACE("RegEnumKeyExW() returns %d\n", lError);
914 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
916 TRACE("Key name: %p\n", szKeyName);
918 if (RegOpenKeyExW(hClassesKey,
919 szKeyName,
921 KEY_ALL_ACCESS,
922 &hClassKey))
924 RegCloseKey(hClassesKey);
925 return FALSE;
928 dwLength = 256 * sizeof(WCHAR);
929 if (!RegQueryValueExW(hClassKey,
930 Class,
931 NULL,
932 NULL,
933 (LPBYTE)szClassName,
934 &dwLength))
936 TRACE("Class name: %p\n", szClassName);
938 if (strcmpiW(szClassName, ClassName) == 0)
940 TRACE("Found matching class name\n");
942 TRACE("Guid: %p\n", szKeyName);
943 if (dwGuidListIndex < ClassGuidListSize)
945 if (szKeyName[0] == '{' && szKeyName[37] == '}')
947 szKeyName[37] = 0;
949 TRACE("Guid: %p\n", &szKeyName[1]);
951 UuidFromStringW(&szKeyName[1],
952 &ClassGuidList[dwGuidListIndex]);
955 dwGuidListIndex++;
959 RegCloseKey(hClassKey);
962 if (lError != ERROR_SUCCESS)
963 break;
966 RegCloseKey(hClassesKey);
968 if (RequiredSize != NULL)
969 *RequiredSize = dwGuidListIndex;
971 if (ClassGuidListSize < dwGuidListIndex)
973 SetLastError(ERROR_INSUFFICIENT_BUFFER);
974 return FALSE;
977 return TRUE;
980 /***********************************************************************
981 * SetupDiClassNameFromGuidA (SETUPAPI.@)
983 BOOL WINAPI SetupDiClassNameFromGuidA(
984 const GUID* ClassGuid,
985 PSTR ClassName,
986 DWORD ClassNameSize,
987 PDWORD RequiredSize)
989 return SetupDiClassNameFromGuidExA(ClassGuid, ClassName,
990 ClassNameSize, RequiredSize,
991 NULL, NULL);
994 /***********************************************************************
995 * SetupDiClassNameFromGuidW (SETUPAPI.@)
997 BOOL WINAPI SetupDiClassNameFromGuidW(
998 const GUID* ClassGuid,
999 PWSTR ClassName,
1000 DWORD ClassNameSize,
1001 PDWORD RequiredSize)
1003 return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
1004 ClassNameSize, RequiredSize,
1005 NULL, NULL);
1008 /***********************************************************************
1009 * SetupDiClassNameFromGuidExA (SETUPAPI.@)
1011 BOOL WINAPI SetupDiClassNameFromGuidExA(
1012 const GUID* ClassGuid,
1013 PSTR ClassName,
1014 DWORD ClassNameSize,
1015 PDWORD RequiredSize,
1016 PCSTR MachineName,
1017 PVOID Reserved)
1019 WCHAR ClassNameW[MAX_CLASS_NAME_LEN];
1020 LPWSTR MachineNameW = NULL;
1021 BOOL ret;
1023 if (MachineName)
1024 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1025 ret = SetupDiClassNameFromGuidExW(ClassGuid, ClassNameW, MAX_CLASS_NAME_LEN,
1026 NULL, MachineNameW, Reserved);
1027 if (ret)
1029 int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
1030 ClassNameSize, NULL, NULL);
1032 if (!ClassNameSize && RequiredSize)
1033 *RequiredSize = len;
1035 MyFree(MachineNameW);
1036 return ret;
1039 /***********************************************************************
1040 * SetupDiClassNameFromGuidExW (SETUPAPI.@)
1042 BOOL WINAPI SetupDiClassNameFromGuidExW(
1043 const GUID* ClassGuid,
1044 PWSTR ClassName,
1045 DWORD ClassNameSize,
1046 PDWORD RequiredSize,
1047 PCWSTR MachineName,
1048 PVOID Reserved)
1050 HKEY hKey;
1051 DWORD dwLength;
1053 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1054 KEY_ALL_ACCESS,
1055 DIOCR_INSTALLER,
1056 MachineName,
1057 Reserved);
1058 if (hKey == INVALID_HANDLE_VALUE)
1060 return FALSE;
1063 if (RequiredSize != NULL)
1065 dwLength = 0;
1066 if (RegQueryValueExW(hKey,
1067 Class,
1068 NULL,
1069 NULL,
1070 NULL,
1071 &dwLength))
1073 RegCloseKey(hKey);
1074 return FALSE;
1077 *RequiredSize = dwLength / sizeof(WCHAR);
1080 dwLength = ClassNameSize * sizeof(WCHAR);
1081 if (RegQueryValueExW(hKey,
1082 Class,
1083 NULL,
1084 NULL,
1085 (LPBYTE)ClassName,
1086 &dwLength))
1088 RegCloseKey(hKey);
1089 return FALSE;
1092 RegCloseKey(hKey);
1094 return TRUE;
1097 /***********************************************************************
1098 * SetupDiCreateDeviceInfoList (SETUPAPI.@)
1100 HDEVINFO WINAPI
1101 SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
1102 HWND hwndParent)
1104 return SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent, NULL, NULL);
1107 /***********************************************************************
1108 * SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
1110 HDEVINFO WINAPI
1111 SetupDiCreateDeviceInfoListExA(const GUID *ClassGuid,
1112 HWND hwndParent,
1113 PCSTR MachineName,
1114 PVOID Reserved)
1116 LPWSTR MachineNameW = NULL;
1117 HDEVINFO hDevInfo;
1119 TRACE("\n");
1121 if (MachineName)
1123 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1124 if (MachineNameW == NULL)
1125 return INVALID_HANDLE_VALUE;
1128 hDevInfo = SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent,
1129 MachineNameW, Reserved);
1131 MyFree(MachineNameW);
1133 return hDevInfo;
1136 /***********************************************************************
1137 * SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
1139 * Create an empty DeviceInfoSet list.
1141 * PARAMS
1142 * ClassGuid [I] if not NULL only devices with GUID ClassGuid are associated
1143 * with this list.
1144 * hwndParent [I] hwnd needed for interface related actions.
1145 * MachineName [I] name of machine to create emtpy DeviceInfoSet list, if NULL
1146 * local registry will be used.
1147 * Reserved [I] must be NULL
1149 * RETURNS
1150 * Success: empty list.
1151 * Failure: INVALID_HANDLE_VALUE.
1153 HDEVINFO WINAPI
1154 SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
1155 HWND hwndParent,
1156 PCWSTR MachineName,
1157 PVOID Reserved)
1159 struct DeviceInfoSet *list = NULL;
1160 DWORD size = sizeof(struct DeviceInfoSet);
1162 TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid), hwndParent,
1163 debugstr_w(MachineName), Reserved);
1165 if (MachineName != NULL)
1167 FIXME("remote support is not implemented\n");
1168 SetLastError(ERROR_INVALID_MACHINENAME);
1169 return INVALID_HANDLE_VALUE;
1172 if (Reserved != NULL)
1174 SetLastError(ERROR_INVALID_PARAMETER);
1175 return INVALID_HANDLE_VALUE;
1178 list = HeapAlloc(GetProcessHeap(), 0, size);
1179 if (!list)
1181 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1182 return INVALID_HANDLE_VALUE;
1185 list->magic = SETUP_DEVICE_INFO_SET_MAGIC;
1186 list->hwndParent = hwndParent;
1187 memcpy(&list->ClassGuid,
1188 ClassGuid ? ClassGuid : &GUID_NULL,
1189 sizeof(list->ClassGuid));
1190 list->cDevices = 0;
1191 list_init(&list->devices);
1193 return (HDEVINFO)list;
1196 /***********************************************************************
1197 * SetupDiCreateDevRegKeyA (SETUPAPI.@)
1199 HKEY WINAPI SetupDiCreateDevRegKeyA(
1200 HDEVINFO DeviceInfoSet,
1201 PSP_DEVINFO_DATA DeviceInfoData,
1202 DWORD Scope,
1203 DWORD HwProfile,
1204 DWORD KeyType,
1205 HINF InfHandle,
1206 PCSTR InfSectionName)
1208 PWSTR InfSectionNameW = NULL;
1209 HKEY key;
1211 TRACE("%p %p %d %d %d %p %s\n", DeviceInfoSet, DeviceInfoData, Scope,
1212 HwProfile, KeyType, InfHandle, debugstr_a(InfSectionName));
1214 if (InfHandle)
1216 if (!InfSectionName)
1218 SetLastError(ERROR_INVALID_PARAMETER);
1219 return INVALID_HANDLE_VALUE;
1221 else
1223 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
1224 if (InfSectionNameW == NULL) return INVALID_HANDLE_VALUE;
1227 key = SetupDiCreateDevRegKeyW(DeviceInfoSet, DeviceInfoData, Scope,
1228 HwProfile, KeyType, InfHandle, InfSectionNameW);
1229 MyFree(InfSectionNameW);
1230 return key;
1233 /***********************************************************************
1234 * SetupDiCreateDevRegKeyW (SETUPAPI.@)
1236 HKEY WINAPI SetupDiCreateDevRegKeyW(
1237 HDEVINFO DeviceInfoSet,
1238 PSP_DEVINFO_DATA DeviceInfoData,
1239 DWORD Scope,
1240 DWORD HwProfile,
1241 DWORD KeyType,
1242 HINF InfHandle,
1243 PCWSTR InfSectionName)
1245 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1246 struct DeviceInfo *devInfo;
1247 HKEY key = INVALID_HANDLE_VALUE;
1249 TRACE("%p %p %d %d %d %p %s\n", DeviceInfoSet, DeviceInfoData, Scope,
1250 HwProfile, KeyType, InfHandle, debugstr_w(InfSectionName));
1252 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1254 SetLastError(ERROR_INVALID_HANDLE);
1255 return INVALID_HANDLE_VALUE;
1257 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1259 SetLastError(ERROR_INVALID_HANDLE);
1260 return INVALID_HANDLE_VALUE;
1262 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1263 || !DeviceInfoData->Reserved)
1265 SetLastError(ERROR_INVALID_PARAMETER);
1266 return INVALID_HANDLE_VALUE;
1268 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1269 if (devInfo->set != set)
1271 SetLastError(ERROR_INVALID_PARAMETER);
1272 return INVALID_HANDLE_VALUE;
1274 if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
1276 SetLastError(ERROR_INVALID_FLAGS);
1277 return INVALID_HANDLE_VALUE;
1279 if (KeyType != DIREG_DEV && KeyType != DIREG_DRV)
1281 SetLastError(ERROR_INVALID_FLAGS);
1282 return INVALID_HANDLE_VALUE;
1284 if (devInfo->phantom)
1286 SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
1287 return INVALID_HANDLE_VALUE;
1289 if (Scope != DICS_FLAG_GLOBAL)
1290 FIXME("unimplemented for scope %d\n", Scope);
1291 switch (KeyType)
1293 case DIREG_DEV:
1294 key = SETUPDI_CreateDevKey(devInfo);
1295 break;
1296 case DIREG_DRV:
1297 key = SETUPDI_CreateDrvKey(devInfo);
1298 break;
1299 default:
1300 WARN("unknown KeyType %d\n", KeyType);
1302 if (InfHandle)
1303 SetupInstallFromInfSectionW(NULL, InfHandle, InfSectionName, SPINST_ALL,
1304 NULL, NULL, SP_COPY_NEWER_ONLY, NULL, NULL, DeviceInfoSet,
1305 DeviceInfoData);
1306 return key;
1309 /***********************************************************************
1310 * SetupDiCreateDeviceInfoA (SETUPAPI.@)
1312 BOOL WINAPI SetupDiCreateDeviceInfoA(
1313 HDEVINFO DeviceInfoSet,
1314 PCSTR DeviceName,
1315 CONST GUID *ClassGuid,
1316 PCSTR DeviceDescription,
1317 HWND hwndParent,
1318 DWORD CreationFlags,
1319 PSP_DEVINFO_DATA DeviceInfoData)
1321 BOOL ret = FALSE;
1322 LPWSTR DeviceNameW = NULL;
1323 LPWSTR DeviceDescriptionW = NULL;
1325 if (DeviceName)
1327 DeviceNameW = MultiByteToUnicode(DeviceName, CP_ACP);
1328 if (DeviceNameW == NULL) return FALSE;
1330 if (DeviceDescription)
1332 DeviceDescriptionW = MultiByteToUnicode(DeviceDescription, CP_ACP);
1333 if (DeviceDescriptionW == NULL)
1335 MyFree(DeviceNameW);
1336 return FALSE;
1340 ret = SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceNameW, ClassGuid, DeviceDescriptionW,
1341 hwndParent, CreationFlags, DeviceInfoData);
1343 MyFree(DeviceNameW);
1344 MyFree(DeviceDescriptionW);
1346 return ret;
1349 static DWORD SETUPDI_DevNameToDevID(LPCWSTR devName)
1351 LPCWSTR ptr;
1352 DWORD devNameLen = lstrlenW(devName), devInst = 0;
1353 BOOL valid = TRUE;
1355 TRACE("%s\n", debugstr_w(devName));
1356 for (ptr = devName; valid && *ptr && ptr - devName < devNameLen; )
1358 if (isdigitW(*ptr))
1360 devInst *= 10;
1361 devInst |= *ptr - '0';
1362 ptr++;
1364 else
1365 valid = FALSE;
1367 TRACE("%d\n", valid ? devInst : 0xffffffff);
1368 return valid ? devInst : 0xffffffff;
1371 /***********************************************************************
1372 * SetupDiCreateDeviceInfoW (SETUPAPI.@)
1374 BOOL WINAPI SetupDiCreateDeviceInfoW(
1375 HDEVINFO DeviceInfoSet,
1376 PCWSTR DeviceName,
1377 CONST GUID *ClassGuid,
1378 PCWSTR DeviceDescription,
1379 HWND hwndParent,
1380 DWORD CreationFlags,
1381 PSP_DEVINFO_DATA DeviceInfoData)
1383 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1384 BOOL ret = FALSE, allocatedInstanceId = FALSE;
1385 LPCWSTR instanceId = NULL;
1387 TRACE("%p %s %s %s %p %x %p\n", DeviceInfoSet, debugstr_w(DeviceName),
1388 debugstr_guid(ClassGuid), debugstr_w(DeviceDescription),
1389 hwndParent, CreationFlags, DeviceInfoData);
1391 if (!DeviceName)
1393 SetLastError(ERROR_INVALID_DEVINST_NAME);
1394 return FALSE;
1396 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1398 SetLastError(ERROR_INVALID_HANDLE);
1399 return FALSE;
1401 if (!ClassGuid)
1403 SetLastError(ERROR_INVALID_PARAMETER);
1404 return FALSE;
1406 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1408 SetLastError(ERROR_INVALID_HANDLE);
1409 return FALSE;
1411 if (!IsEqualGUID(&set->ClassGuid, &GUID_NULL) &&
1412 !IsEqualGUID(ClassGuid, &set->ClassGuid))
1414 SetLastError(ERROR_CLASS_MISMATCH);
1415 return FALSE;
1417 if ((CreationFlags & DICD_GENERATE_ID))
1419 if (strchrW(DeviceName, '\\'))
1420 SetLastError(ERROR_INVALID_DEVINST_NAME);
1421 else
1423 static const WCHAR newDeviceFmt[] = {'R','O','O','T','\\','%','s',
1424 '\\','%','0','4','d',0};
1425 DWORD devId;
1427 if (set->cDevices)
1429 DWORD highestDevID = 0;
1430 struct DeviceInstance *devInst;
1432 LIST_FOR_EACH_ENTRY(devInst, &set->devices, struct DeviceInstance, entry)
1434 struct DeviceInfo *devInfo = (struct DeviceInfo *)devInst->data.Reserved;
1435 LPCWSTR devName = strrchrW(devInfo->instanceId, '\\');
1436 DWORD id;
1438 if (devName)
1439 devName++;
1440 else
1441 devName = devInfo->instanceId;
1442 id = SETUPDI_DevNameToDevID(devName);
1443 if (id != 0xffffffff && id > highestDevID)
1444 highestDevID = id;
1446 devId = highestDevID + 1;
1448 else
1449 devId = 0;
1450 /* 17 == lstrlenW(L"Root\\") + lstrlenW("\\") + 1 + %d max size */
1451 instanceId = HeapAlloc(GetProcessHeap(), 0,
1452 (17 + lstrlenW(DeviceName)) * sizeof(WCHAR));
1453 if (instanceId)
1455 sprintfW((LPWSTR)instanceId, newDeviceFmt, DeviceName,
1456 devId);
1457 allocatedInstanceId = TRUE;
1458 ret = TRUE;
1460 else
1461 ret = FALSE;
1464 else
1466 struct DeviceInstance *devInst;
1468 ret = TRUE;
1469 instanceId = DeviceName;
1470 LIST_FOR_EACH_ENTRY(devInst, &set->devices, struct DeviceInstance, entry)
1472 struct DeviceInfo *devInfo = (struct DeviceInfo *)devInst->data.Reserved;
1474 if (!lstrcmpiW(DeviceName, devInfo->instanceId))
1476 SetLastError(ERROR_DEVINST_ALREADY_EXISTS);
1477 ret = FALSE;
1481 if (ret)
1483 SP_DEVINFO_DATA *dev = NULL;
1485 ret = SETUPDI_AddDeviceToSet(set, ClassGuid, 0 /* FIXME: DevInst */,
1486 instanceId, TRUE, &dev);
1487 if (ret)
1489 if (DeviceDescription)
1490 SetupDiSetDeviceRegistryPropertyW(DeviceInfoSet,
1491 dev, SPDRP_DEVICEDESC, (const BYTE *)DeviceDescription,
1492 lstrlenW(DeviceDescription) * sizeof(WCHAR));
1493 if (DeviceInfoData)
1495 if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
1497 SetLastError(ERROR_INVALID_USER_BUFFER);
1498 ret = FALSE;
1500 else
1501 *DeviceInfoData = *dev;
1505 if (allocatedInstanceId)
1506 HeapFree(GetProcessHeap(), 0, (LPWSTR)instanceId);
1508 return ret;
1511 /***********************************************************************
1512 * SetupDiRegisterDeviceInfo (SETUPAPI.@)
1514 BOOL WINAPI SetupDiRegisterDeviceInfo(
1515 HDEVINFO DeviceInfoSet,
1516 PSP_DEVINFO_DATA DeviceInfoData,
1517 DWORD Flags,
1518 PSP_DETSIG_CMPPROC CompareProc,
1519 PVOID CompareContext,
1520 PSP_DEVINFO_DATA DupDeviceInfoData)
1522 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1523 struct DeviceInfo *devInfo;
1525 TRACE("%p %p %08x %p %p %p\n", DeviceInfoSet, DeviceInfoData, Flags,
1526 CompareProc, CompareContext, DupDeviceInfoData);
1528 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1530 SetLastError(ERROR_INVALID_HANDLE);
1531 return FALSE;
1533 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1535 SetLastError(ERROR_INVALID_HANDLE);
1536 return FALSE;
1538 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1539 || !DeviceInfoData->Reserved)
1541 SetLastError(ERROR_INVALID_PARAMETER);
1542 return FALSE;
1544 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1545 if (devInfo->set != set)
1547 SetLastError(ERROR_INVALID_PARAMETER);
1548 return FALSE;
1550 if (devInfo->phantom)
1552 devInfo->phantom = FALSE;
1553 RegDeleteValueW(devInfo->key, Phantom);
1555 return TRUE;
1558 /***********************************************************************
1559 * SetupDiEnumDeviceInfo (SETUPAPI.@)
1561 BOOL WINAPI SetupDiEnumDeviceInfo(
1562 HDEVINFO devinfo,
1563 DWORD index,
1564 PSP_DEVINFO_DATA info)
1566 BOOL ret = FALSE;
1568 TRACE("%p %d %p\n", devinfo, index, info);
1570 if(info==NULL)
1572 SetLastError(ERROR_INVALID_PARAMETER);
1573 return FALSE;
1575 if (devinfo && devinfo != INVALID_HANDLE_VALUE)
1577 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
1578 if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
1580 if (index < list->cDevices)
1582 if (info->cbSize == sizeof(SP_DEVINFO_DATA))
1584 struct DeviceInstance *devInst;
1585 DWORD i = 0;
1587 LIST_FOR_EACH_ENTRY(devInst, &list->devices,
1588 struct DeviceInstance, entry)
1590 if (i++ == index)
1592 *info = devInst->data;
1593 break;
1596 ret = TRUE;
1598 else
1599 SetLastError(ERROR_INVALID_USER_BUFFER);
1601 else
1602 SetLastError(ERROR_NO_MORE_ITEMS);
1604 else
1605 SetLastError(ERROR_INVALID_HANDLE);
1607 else
1608 SetLastError(ERROR_INVALID_HANDLE);
1609 return ret;
1612 /***********************************************************************
1613 * SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
1615 BOOL WINAPI SetupDiGetDeviceInstanceIdA(
1616 HDEVINFO DeviceInfoSet,
1617 PSP_DEVINFO_DATA DeviceInfoData,
1618 PSTR DeviceInstanceId,
1619 DWORD DeviceInstanceIdSize,
1620 PDWORD RequiredSize)
1622 BOOL ret = FALSE;
1623 DWORD size;
1624 PWSTR instanceId;
1626 TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
1627 DeviceInstanceIdSize, RequiredSize);
1629 SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
1630 DeviceInfoData,
1631 NULL,
1633 &size);
1634 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1635 return FALSE;
1636 instanceId = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1637 if (instanceId)
1639 ret = SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
1640 DeviceInfoData,
1641 instanceId,
1642 size,
1643 &size);
1644 if (ret)
1646 int len = WideCharToMultiByte(CP_ACP, 0, instanceId, -1,
1647 DeviceInstanceId,
1648 DeviceInstanceIdSize, NULL, NULL);
1650 if (!len)
1651 ret = FALSE;
1652 else
1654 if (len > DeviceInstanceIdSize)
1656 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1657 ret = FALSE;
1659 if (RequiredSize)
1660 *RequiredSize = len;
1663 HeapFree(GetProcessHeap(), 0, instanceId);
1665 return ret;
1668 /***********************************************************************
1669 * SetupDiGetDeviceInstanceIdW (SETUPAPI.@)
1671 BOOL WINAPI SetupDiGetDeviceInstanceIdW(
1672 HDEVINFO DeviceInfoSet,
1673 PSP_DEVINFO_DATA DeviceInfoData,
1674 PWSTR DeviceInstanceId,
1675 DWORD DeviceInstanceIdSize,
1676 PDWORD RequiredSize)
1678 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1679 struct DeviceInfo *devInfo;
1681 TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
1682 DeviceInstanceIdSize, RequiredSize);
1684 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1686 SetLastError(ERROR_INVALID_HANDLE);
1687 return FALSE;
1689 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1691 SetLastError(ERROR_INVALID_HANDLE);
1692 return FALSE;
1694 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1695 || !DeviceInfoData->Reserved)
1697 SetLastError(ERROR_INVALID_PARAMETER);
1698 return FALSE;
1700 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1701 if (devInfo->set != set)
1703 SetLastError(ERROR_INVALID_PARAMETER);
1704 return FALSE;
1706 TRACE("instance ID: %s\n", debugstr_w(devInfo->instanceId));
1707 if (DeviceInstanceIdSize < lstrlenW(devInfo->instanceId) + 1)
1709 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1710 if (RequiredSize)
1711 *RequiredSize = lstrlenW(devInfo->instanceId) + 1;
1712 return FALSE;
1714 lstrcpyW(DeviceInstanceId, devInfo->instanceId);
1715 if (RequiredSize)
1716 *RequiredSize = lstrlenW(devInfo->instanceId) + 1;
1717 return TRUE;
1720 /***********************************************************************
1721 * SetupDiGetActualSectionToInstallA (SETUPAPI.@)
1723 BOOL WINAPI SetupDiGetActualSectionToInstallA(
1724 HINF InfHandle,
1725 PCSTR InfSectionName,
1726 PSTR InfSectionWithExt,
1727 DWORD InfSectionWithExtSize,
1728 PDWORD RequiredSize,
1729 PSTR *Extension)
1731 FIXME("\n");
1732 return FALSE;
1735 /***********************************************************************
1736 * SetupDiGetActualSectionToInstallW (SETUPAPI.@)
1738 BOOL WINAPI SetupDiGetActualSectionToInstallW(
1739 HINF InfHandle,
1740 PCWSTR InfSectionName,
1741 PWSTR InfSectionWithExt,
1742 DWORD InfSectionWithExtSize,
1743 PDWORD RequiredSize,
1744 PWSTR *Extension)
1746 WCHAR szBuffer[MAX_PATH];
1747 DWORD dwLength;
1748 DWORD dwFullLength;
1749 LONG lLineCount = -1;
1751 lstrcpyW(szBuffer, InfSectionName);
1752 dwLength = lstrlenW(szBuffer);
1754 if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
1756 /* Test section name with '.NTx86' extension */
1757 lstrcpyW(&szBuffer[dwLength], NtPlatformExtension);
1758 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1760 if (lLineCount == -1)
1762 /* Test section name with '.NT' extension */
1763 lstrcpyW(&szBuffer[dwLength], NtExtension);
1764 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1767 else
1769 /* Test section name with '.Win' extension */
1770 lstrcpyW(&szBuffer[dwLength], WinExtension);
1771 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1774 if (lLineCount == -1)
1776 /* Test section name without extension */
1777 szBuffer[dwLength] = 0;
1778 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1781 if (lLineCount == -1)
1783 SetLastError(ERROR_INVALID_PARAMETER);
1784 return FALSE;
1787 dwFullLength = lstrlenW(szBuffer);
1789 if (InfSectionWithExt != NULL && InfSectionWithExtSize != 0)
1791 if (InfSectionWithExtSize < (dwFullLength + 1))
1793 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1794 return FALSE;
1797 lstrcpyW(InfSectionWithExt, szBuffer);
1798 if (Extension != NULL)
1800 *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
1804 if (RequiredSize != NULL)
1806 *RequiredSize = dwFullLength + 1;
1809 return TRUE;
1812 /***********************************************************************
1813 * SetupDiGetClassDescriptionA (SETUPAPI.@)
1815 BOOL WINAPI SetupDiGetClassDescriptionA(
1816 const GUID* ClassGuid,
1817 PSTR ClassDescription,
1818 DWORD ClassDescriptionSize,
1819 PDWORD RequiredSize)
1821 return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
1822 ClassDescriptionSize,
1823 RequiredSize, NULL, NULL);
1826 /***********************************************************************
1827 * SetupDiGetClassDescriptionW (SETUPAPI.@)
1829 BOOL WINAPI SetupDiGetClassDescriptionW(
1830 const GUID* ClassGuid,
1831 PWSTR ClassDescription,
1832 DWORD ClassDescriptionSize,
1833 PDWORD RequiredSize)
1835 return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
1836 ClassDescriptionSize,
1837 RequiredSize, NULL, NULL);
1840 /***********************************************************************
1841 * SetupDiGetClassDescriptionExA (SETUPAPI.@)
1843 BOOL WINAPI SetupDiGetClassDescriptionExA(
1844 const GUID* ClassGuid,
1845 PSTR ClassDescription,
1846 DWORD ClassDescriptionSize,
1847 PDWORD RequiredSize,
1848 PCSTR MachineName,
1849 PVOID Reserved)
1851 HKEY hKey;
1852 DWORD dwLength;
1853 BOOL ret;
1855 hKey = SetupDiOpenClassRegKeyExA(ClassGuid,
1856 KEY_ALL_ACCESS,
1857 DIOCR_INSTALLER,
1858 MachineName,
1859 Reserved);
1860 if (hKey == INVALID_HANDLE_VALUE)
1862 WARN("SetupDiOpenClassRegKeyExA() failed (Error %u)\n", GetLastError());
1863 return FALSE;
1866 dwLength = ClassDescriptionSize;
1867 ret = !RegQueryValueExA( hKey, NULL, NULL, NULL,
1868 (LPBYTE)ClassDescription, &dwLength );
1869 if (RequiredSize) *RequiredSize = dwLength;
1870 RegCloseKey(hKey);
1871 return ret;
1874 /***********************************************************************
1875 * SetupDiGetClassDescriptionExW (SETUPAPI.@)
1877 BOOL WINAPI SetupDiGetClassDescriptionExW(
1878 const GUID* ClassGuid,
1879 PWSTR ClassDescription,
1880 DWORD ClassDescriptionSize,
1881 PDWORD RequiredSize,
1882 PCWSTR MachineName,
1883 PVOID Reserved)
1885 HKEY hKey;
1886 DWORD dwLength;
1887 BOOL ret;
1889 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1890 KEY_ALL_ACCESS,
1891 DIOCR_INSTALLER,
1892 MachineName,
1893 Reserved);
1894 if (hKey == INVALID_HANDLE_VALUE)
1896 WARN("SetupDiOpenClassRegKeyExW() failed (Error %u)\n", GetLastError());
1897 return FALSE;
1900 dwLength = ClassDescriptionSize * sizeof(WCHAR);
1901 ret = !RegQueryValueExW( hKey, NULL, NULL, NULL,
1902 (LPBYTE)ClassDescription, &dwLength );
1903 if (RequiredSize) *RequiredSize = dwLength / sizeof(WCHAR);
1904 RegCloseKey(hKey);
1905 return ret;
1908 /***********************************************************************
1909 * SetupDiGetClassDevsA (SETUPAPI.@)
1911 HDEVINFO WINAPI SetupDiGetClassDevsA(
1912 CONST GUID *class,
1913 LPCSTR enumstr,
1914 HWND parent,
1915 DWORD flags)
1917 HDEVINFO ret;
1918 LPWSTR enumstrW = NULL;
1920 if (enumstr)
1922 int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1923 enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1924 if (!enumstrW)
1926 ret = INVALID_HANDLE_VALUE;
1927 goto end;
1929 MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1931 ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, NULL, NULL,
1932 NULL);
1933 HeapFree(GetProcessHeap(), 0, enumstrW);
1935 end:
1936 return ret;
1939 /***********************************************************************
1940 * SetupDiGetClassDevsExA (SETUPAPI.@)
1942 HDEVINFO WINAPI SetupDiGetClassDevsExA(
1943 const GUID *class,
1944 PCSTR enumstr,
1945 HWND parent,
1946 DWORD flags,
1947 HDEVINFO deviceset,
1948 PCSTR machine,
1949 PVOID reserved)
1951 HDEVINFO ret;
1952 LPWSTR enumstrW = NULL, machineW = NULL;
1954 if (enumstr)
1956 int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1957 enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1958 if (!enumstrW)
1960 ret = INVALID_HANDLE_VALUE;
1961 goto end;
1963 MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1965 if (machine)
1967 int len = MultiByteToWideChar(CP_ACP, 0, machine, -1, NULL, 0);
1968 machineW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1969 if (!machineW)
1971 HeapFree(GetProcessHeap(), 0, enumstrW);
1972 ret = INVALID_HANDLE_VALUE;
1973 goto end;
1975 MultiByteToWideChar(CP_ACP, 0, machine, -1, machineW, len);
1977 ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, deviceset,
1978 machineW, reserved);
1979 HeapFree(GetProcessHeap(), 0, enumstrW);
1980 HeapFree(GetProcessHeap(), 0, machineW);
1982 end:
1983 return ret;
1986 static void SETUPDI_AddDeviceInterfaces(SP_DEVINFO_DATA *dev, HKEY key,
1987 const GUID *interface)
1989 DWORD i, len;
1990 WCHAR subKeyName[MAX_PATH];
1991 LONG l = ERROR_SUCCESS;
1993 for (i = 0; !l; i++)
1995 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1996 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
1997 if (!l)
1999 HKEY subKey;
2000 SP_DEVICE_INTERFACE_DATA *iface = NULL;
2002 if (*subKeyName == '#')
2004 /* The subkey name is the reference string, with a '#' prepended */
2005 SETUPDI_AddInterfaceInstance(dev, interface, subKeyName + 1,
2006 &iface);
2007 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
2008 if (!l)
2010 WCHAR symbolicLink[MAX_PATH];
2011 DWORD dataType;
2013 len = sizeof(symbolicLink);
2014 l = RegQueryValueExW(subKey, SymbolicLink, NULL, &dataType,
2015 (BYTE *)symbolicLink, &len);
2016 if (!l && dataType == REG_SZ)
2017 SETUPDI_SetInterfaceSymbolicLink(iface, symbolicLink);
2018 RegCloseKey(subKey);
2021 /* Allow enumeration to continue */
2022 l = ERROR_SUCCESS;
2025 /* FIXME: find and add all the device's interfaces to the device */
2028 static void SETUPDI_EnumerateMatchingInterfaces(HDEVINFO DeviceInfoSet,
2029 HKEY key, const GUID *interface, LPCWSTR enumstr)
2031 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2032 DWORD i, len;
2033 WCHAR subKeyName[MAX_PATH];
2034 LONG l;
2035 HKEY enumKey = INVALID_HANDLE_VALUE;
2037 TRACE("%s\n", debugstr_w(enumstr));
2039 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_READ, NULL,
2040 &enumKey, NULL);
2041 for (i = 0; !l; i++)
2043 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2044 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
2045 if (!l)
2047 HKEY subKey;
2049 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
2050 if (!l)
2052 WCHAR deviceInst[MAX_PATH * 3];
2053 DWORD dataType;
2055 len = sizeof(deviceInst);
2056 l = RegQueryValueExW(subKey, DeviceInstance, NULL, &dataType,
2057 (BYTE *)deviceInst, &len);
2058 if (!l && dataType == REG_SZ)
2060 TRACE("found instance ID %s\n", debugstr_w(deviceInst));
2061 if (!enumstr || !lstrcmpiW(enumstr, deviceInst))
2063 HKEY deviceKey;
2065 l = RegOpenKeyExW(enumKey, deviceInst, 0, KEY_READ,
2066 &deviceKey);
2067 if (!l)
2069 WCHAR deviceClassStr[40];
2071 len = sizeof(deviceClassStr);
2072 l = RegQueryValueExW(deviceKey, ClassGUID, NULL,
2073 &dataType, (BYTE *)deviceClassStr, &len);
2074 if (!l && dataType == REG_SZ &&
2075 deviceClassStr[0] == '{' &&
2076 deviceClassStr[37] == '}')
2078 GUID deviceClass;
2079 SP_DEVINFO_DATA *dev;
2081 deviceClassStr[37] = 0;
2082 UuidFromStringW(&deviceClassStr[1],
2083 &deviceClass);
2084 if (SETUPDI_AddDeviceToSet(set, &deviceClass,
2085 0 /* FIXME: DevInst */, deviceInst,
2086 FALSE, &dev))
2087 SETUPDI_AddDeviceInterfaces(dev, subKey,
2088 interface);
2090 RegCloseKey(deviceKey);
2094 RegCloseKey(subKey);
2096 /* Allow enumeration to continue */
2097 l = ERROR_SUCCESS;
2100 if (enumKey != INVALID_HANDLE_VALUE)
2101 RegCloseKey(enumKey);
2104 static void SETUPDI_EnumerateInterfaces(HDEVINFO DeviceInfoSet,
2105 const GUID *interface, LPCWSTR enumstr, DWORD flags)
2107 HKEY interfacesKey = SetupDiOpenClassRegKeyExW(interface, KEY_READ,
2108 DIOCR_INTERFACE, NULL, NULL);
2110 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(interface),
2111 debugstr_w(enumstr), flags);
2113 if (interfacesKey != INVALID_HANDLE_VALUE)
2115 if (flags & DIGCF_ALLCLASSES)
2117 DWORD i, len;
2118 WCHAR interfaceGuidStr[40];
2119 LONG l = ERROR_SUCCESS;
2121 for (i = 0; !l; i++)
2123 len = sizeof(interfaceGuidStr) / sizeof(interfaceGuidStr[0]);
2124 l = RegEnumKeyExW(interfacesKey, i, interfaceGuidStr, &len,
2125 NULL, NULL, NULL, NULL);
2126 if (!l)
2128 if (interfaceGuidStr[0] == '{' &&
2129 interfaceGuidStr[37] == '}')
2131 HKEY interfaceKey;
2132 GUID interfaceGuid;
2134 interfaceGuidStr[37] = 0;
2135 UuidFromStringW(&interfaceGuidStr[1], &interfaceGuid);
2136 l = RegOpenKeyExW(interfacesKey, interfaceGuidStr, 0,
2137 KEY_READ, &interfaceKey);
2138 if (!l)
2140 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
2141 interfaceKey, &interfaceGuid, enumstr);
2142 RegCloseKey(interfaceKey);
2148 else
2150 /* In this case, SetupDiOpenClassRegKeyExW opened the specific
2151 * interface's key, so just pass that long
2153 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
2154 interfacesKey, interface, enumstr);
2156 RegCloseKey(interfacesKey);
2160 static void SETUPDI_EnumerateMatchingDeviceInstances(struct DeviceInfoSet *set,
2161 LPCWSTR enumerator, LPCWSTR deviceName, HKEY deviceKey,
2162 const GUID *class, DWORD flags)
2164 DWORD i, len;
2165 WCHAR deviceInstance[MAX_PATH];
2166 LONG l = ERROR_SUCCESS;
2168 TRACE("%s %s\n", debugstr_w(enumerator), debugstr_w(deviceName));
2170 for (i = 0; !l; i++)
2172 len = sizeof(deviceInstance) / sizeof(deviceInstance[0]);
2173 l = RegEnumKeyExW(deviceKey, i, deviceInstance, &len, NULL, NULL, NULL,
2174 NULL);
2175 if (!l)
2177 HKEY subKey;
2179 l = RegOpenKeyExW(deviceKey, deviceInstance, 0, KEY_READ, &subKey);
2180 if (!l)
2182 WCHAR classGuid[40];
2183 DWORD dataType;
2185 len = sizeof(classGuid);
2186 l = RegQueryValueExW(subKey, ClassGUID, NULL, &dataType,
2187 (BYTE *)classGuid, &len);
2188 if (!l && dataType == REG_SZ)
2190 if (classGuid[0] == '{' && classGuid[37] == '}')
2192 GUID deviceClass;
2194 classGuid[37] = 0;
2195 UuidFromStringW(&classGuid[1], &deviceClass);
2196 if ((flags & DIGCF_ALLCLASSES) ||
2197 IsEqualGUID(class, &deviceClass))
2199 static const WCHAR fmt[] =
2200 {'%','s','\\','%','s','\\','%','s',0};
2201 LPWSTR instanceId;
2203 instanceId = HeapAlloc(GetProcessHeap(), 0,
2204 (lstrlenW(enumerator) + lstrlenW(deviceName) +
2205 lstrlenW(deviceInstance) + 3) * sizeof(WCHAR));
2206 if (instanceId)
2208 sprintfW(instanceId, fmt, enumerator,
2209 deviceName, deviceInstance);
2210 SETUPDI_AddDeviceToSet(set, &deviceClass,
2211 0 /* FIXME: DevInst */, instanceId,
2212 FALSE, NULL);
2213 HeapFree(GetProcessHeap(), 0, instanceId);
2218 RegCloseKey(subKey);
2220 /* Allow enumeration to continue */
2221 l = ERROR_SUCCESS;
2226 static void SETUPDI_EnumerateMatchingDevices(HDEVINFO DeviceInfoSet,
2227 LPCWSTR parent, HKEY key, const GUID *class, DWORD flags)
2229 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2230 DWORD i, len;
2231 WCHAR subKeyName[MAX_PATH];
2232 LONG l = ERROR_SUCCESS;
2234 TRACE("%s\n", debugstr_w(parent));
2236 for (i = 0; !l; i++)
2238 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2239 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
2240 if (!l)
2242 HKEY subKey;
2244 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
2245 if (!l)
2247 TRACE("%s\n", debugstr_w(subKeyName));
2248 SETUPDI_EnumerateMatchingDeviceInstances(set, parent,
2249 subKeyName, subKey, class, flags);
2250 RegCloseKey(subKey);
2252 /* Allow enumeration to continue */
2253 l = ERROR_SUCCESS;
2258 static void SETUPDI_EnumerateDevices(HDEVINFO DeviceInfoSet, const GUID *class,
2259 LPCWSTR enumstr, DWORD flags)
2261 HKEY enumKey;
2262 LONG l;
2264 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(class),
2265 debugstr_w(enumstr), flags);
2267 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_READ, NULL,
2268 &enumKey, NULL);
2269 if (enumKey != INVALID_HANDLE_VALUE)
2271 if (enumstr)
2273 HKEY enumStrKey;
2275 l = RegOpenKeyExW(enumKey, enumstr, 0, KEY_READ,
2276 &enumStrKey);
2277 if (!l)
2279 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet, enumstr,
2280 enumStrKey, class, flags);
2281 RegCloseKey(enumStrKey);
2284 else
2286 DWORD i, len;
2287 WCHAR subKeyName[MAX_PATH];
2289 l = ERROR_SUCCESS;
2290 for (i = 0; !l; i++)
2292 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2293 l = RegEnumKeyExW(enumKey, i, subKeyName, &len, NULL,
2294 NULL, NULL, NULL);
2295 if (!l)
2297 HKEY subKey;
2299 l = RegOpenKeyExW(enumKey, subKeyName, 0, KEY_READ,
2300 &subKey);
2301 if (!l)
2303 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet,
2304 subKeyName, subKey, class, flags);
2305 RegCloseKey(subKey);
2307 /* Allow enumeration to continue */
2308 l = ERROR_SUCCESS;
2312 RegCloseKey(enumKey);
2316 /***********************************************************************
2317 * SetupDiGetClassDevsW (SETUPAPI.@)
2319 HDEVINFO WINAPI SetupDiGetClassDevsW(
2320 CONST GUID *class,
2321 LPCWSTR enumstr,
2322 HWND parent,
2323 DWORD flags)
2325 return SetupDiGetClassDevsExW(class, enumstr, parent, flags, NULL, NULL,
2326 NULL);
2329 /***********************************************************************
2330 * SetupDiGetClassDevsExW (SETUPAPI.@)
2332 HDEVINFO WINAPI SetupDiGetClassDevsExW(
2333 CONST GUID *class,
2334 PCWSTR enumstr,
2335 HWND parent,
2336 DWORD flags,
2337 HDEVINFO deviceset,
2338 PCWSTR machine,
2339 PVOID reserved)
2341 static const DWORD unsupportedFlags = DIGCF_DEFAULT | DIGCF_PRESENT |
2342 DIGCF_PROFILE;
2343 HDEVINFO set;
2345 TRACE("%s %s %p 0x%08x %p %s %p\n", debugstr_guid(class),
2346 debugstr_w(enumstr), parent, flags, deviceset, debugstr_w(machine),
2347 reserved);
2349 if (!(flags & DIGCF_ALLCLASSES) && !class)
2351 SetLastError(ERROR_INVALID_PARAMETER);
2352 return NULL;
2354 if (flags & unsupportedFlags)
2355 WARN("unsupported flags %08x\n", flags & unsupportedFlags);
2356 if (deviceset)
2357 set = deviceset;
2358 else
2359 set = SetupDiCreateDeviceInfoListExW(class, parent, machine, reserved);
2360 if (set)
2362 if (machine)
2363 FIXME("%s: unimplemented for remote machines\n",
2364 debugstr_w(machine));
2365 else if (flags & DIGCF_DEVICEINTERFACE)
2366 SETUPDI_EnumerateInterfaces(set, class, enumstr, flags);
2367 else
2368 SETUPDI_EnumerateDevices(set, class, enumstr, flags);
2370 return set;
2373 /***********************************************************************
2374 * SetupDiGetDeviceInfoListDetailA (SETUPAPI.@)
2376 BOOL WINAPI SetupDiGetDeviceInfoListDetailA(
2377 HDEVINFO DeviceInfoSet,
2378 PSP_DEVINFO_LIST_DETAIL_DATA_A DevInfoData )
2380 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2382 TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2384 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2386 SetLastError(ERROR_INVALID_HANDLE);
2387 return FALSE;
2389 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2391 SetLastError(ERROR_INVALID_HANDLE);
2392 return FALSE;
2394 if (!DevInfoData ||
2395 DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_A))
2397 SetLastError(ERROR_INVALID_PARAMETER);
2398 return FALSE;
2400 DevInfoData->ClassGuid = set->ClassGuid;
2401 DevInfoData->RemoteMachineHandle = NULL;
2402 DevInfoData->RemoteMachineName[0] = '\0';
2403 return TRUE;
2406 /***********************************************************************
2407 * SetupDiGetDeviceInfoListDetailW (SETUPAPI.@)
2409 BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
2410 HDEVINFO DeviceInfoSet,
2411 PSP_DEVINFO_LIST_DETAIL_DATA_W DevInfoData )
2413 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2415 TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2417 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2419 SetLastError(ERROR_INVALID_HANDLE);
2420 return FALSE;
2422 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2424 SetLastError(ERROR_INVALID_HANDLE);
2425 return FALSE;
2427 if (!DevInfoData ||
2428 DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W))
2430 SetLastError(ERROR_INVALID_PARAMETER);
2431 return FALSE;
2433 DevInfoData->ClassGuid = set->ClassGuid;
2434 DevInfoData->RemoteMachineHandle = NULL;
2435 DevInfoData->RemoteMachineName[0] = '\0';
2436 return TRUE;
2439 /***********************************************************************
2440 * SetupDiCreateDeviceInterfaceA (SETUPAPI.@)
2442 BOOL WINAPI SetupDiCreateDeviceInterfaceA(
2443 HDEVINFO DeviceInfoSet,
2444 PSP_DEVINFO_DATA DeviceInfoData,
2445 const GUID *InterfaceClassGuid,
2446 PCSTR ReferenceString,
2447 DWORD CreationFlags,
2448 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2450 BOOL ret;
2451 LPWSTR ReferenceStringW = NULL;
2453 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2454 debugstr_guid(InterfaceClassGuid), debugstr_a(ReferenceString),
2455 CreationFlags, DeviceInterfaceData);
2457 if (ReferenceString)
2459 ReferenceStringW = MultiByteToUnicode(ReferenceString, CP_ACP);
2460 if (ReferenceStringW == NULL) return FALSE;
2463 ret = SetupDiCreateDeviceInterfaceW(DeviceInfoSet, DeviceInfoData,
2464 InterfaceClassGuid, ReferenceStringW, CreationFlags,
2465 DeviceInterfaceData);
2467 MyFree(ReferenceStringW);
2469 return ret;
2472 /***********************************************************************
2473 * SetupDiCreateDeviceInterfaceW (SETUPAPI.@)
2475 BOOL WINAPI SetupDiCreateDeviceInterfaceW(
2476 HDEVINFO DeviceInfoSet,
2477 PSP_DEVINFO_DATA DeviceInfoData,
2478 const GUID *InterfaceClassGuid,
2479 PCWSTR ReferenceString,
2480 DWORD CreationFlags,
2481 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2483 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2484 struct DeviceInfo *devInfo;
2485 SP_DEVICE_INTERFACE_DATA *iface = NULL;
2486 BOOL ret;
2488 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2489 debugstr_guid(InterfaceClassGuid), debugstr_w(ReferenceString),
2490 CreationFlags, DeviceInterfaceData);
2492 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2494 SetLastError(ERROR_INVALID_HANDLE);
2495 return FALSE;
2497 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2499 SetLastError(ERROR_INVALID_HANDLE);
2500 return FALSE;
2502 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2503 || !DeviceInfoData->Reserved)
2505 SetLastError(ERROR_INVALID_PARAMETER);
2506 return FALSE;
2508 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2509 if (devInfo->set != set)
2511 SetLastError(ERROR_INVALID_PARAMETER);
2512 return FALSE;
2514 if (!InterfaceClassGuid)
2516 SetLastError(ERROR_INVALID_USER_BUFFER);
2517 return FALSE;
2519 if ((ret = SETUPDI_AddInterfaceInstance(DeviceInfoData, InterfaceClassGuid,
2520 ReferenceString, &iface)))
2522 if (DeviceInterfaceData)
2524 if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2526 SetLastError(ERROR_INVALID_USER_BUFFER);
2527 ret = FALSE;
2529 else
2530 *DeviceInterfaceData = *iface;
2533 return ret;
2536 /***********************************************************************
2537 * SetupDiCreateDeviceInterfaceRegKeyA (SETUPAPI.@)
2539 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyA(
2540 HDEVINFO DeviceInfoSet,
2541 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2542 DWORD Reserved,
2543 REGSAM samDesired,
2544 HINF InfHandle,
2545 PCSTR InfSectionName)
2547 HKEY key;
2548 PWSTR InfSectionNameW = NULL;
2550 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2551 samDesired, InfHandle, InfSectionName);
2552 if (InfHandle)
2554 if (!InfSectionName)
2556 SetLastError(ERROR_INVALID_PARAMETER);
2557 return INVALID_HANDLE_VALUE;
2559 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
2560 if (!InfSectionNameW)
2561 return INVALID_HANDLE_VALUE;
2563 key = SetupDiCreateDeviceInterfaceRegKeyW(DeviceInfoSet,
2564 DeviceInterfaceData, Reserved, samDesired, InfHandle,
2565 InfSectionNameW);
2566 MyFree(InfSectionNameW);
2567 return key;
2570 static PWSTR SETUPDI_GetInstancePath(struct InterfaceInfo *ifaceInfo)
2572 static const WCHAR hash[] = {'#',0};
2573 PWSTR instancePath = NULL;
2575 if (ifaceInfo->referenceString)
2577 instancePath = HeapAlloc(GetProcessHeap(), 0,
2578 (lstrlenW(ifaceInfo->referenceString) + 2) * sizeof(WCHAR));
2579 if (instancePath)
2581 lstrcpyW(instancePath, hash);
2582 lstrcatW(instancePath, ifaceInfo->referenceString);
2584 else
2585 SetLastError(ERROR_OUTOFMEMORY);
2587 else
2589 instancePath = HeapAlloc(GetProcessHeap(), 0,
2590 (lstrlenW(hash) + 1) * sizeof(WCHAR));
2591 if (instancePath)
2592 lstrcpyW(instancePath, hash);
2594 return instancePath;
2597 /***********************************************************************
2598 * SetupDiCreateDeviceInterfaceRegKeyW (SETUPAPI.@)
2600 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyW(
2601 HDEVINFO DeviceInfoSet,
2602 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2603 DWORD Reserved,
2604 REGSAM samDesired,
2605 HINF InfHandle,
2606 PCWSTR InfSectionName)
2608 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2609 HKEY key = INVALID_HANDLE_VALUE, interfacesKey;
2610 LONG l;
2612 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2613 samDesired, InfHandle, InfSectionName);
2615 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2616 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2618 SetLastError(ERROR_INVALID_HANDLE);
2619 return INVALID_HANDLE_VALUE;
2621 if (!DeviceInterfaceData ||
2622 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2623 !DeviceInterfaceData->Reserved)
2625 SetLastError(ERROR_INVALID_PARAMETER);
2626 return INVALID_HANDLE_VALUE;
2628 if (InfHandle && !InfSectionName)
2630 SetLastError(ERROR_INVALID_PARAMETER);
2631 return INVALID_HANDLE_VALUE;
2633 if (!(l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, DeviceClasses, 0, NULL, 0,
2634 samDesired, NULL, &interfacesKey, NULL)))
2636 HKEY parent;
2637 WCHAR bracedGuidString[39];
2639 SETUPDI_GuidToString(&DeviceInterfaceData->InterfaceClassGuid,
2640 bracedGuidString);
2641 if (!(l = RegCreateKeyExW(interfacesKey, bracedGuidString, 0, NULL, 0,
2642 samDesired, NULL, &parent, NULL)))
2644 struct InterfaceInfo *ifaceInfo =
2645 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2646 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2647 PWSTR interfKeyName = HeapAlloc(GetProcessHeap(), 0,
2648 (lstrlenW(ifaceInfo->symbolicLink) + 1) * sizeof(WCHAR));
2649 HKEY interfKey;
2650 WCHAR *ptr;
2652 lstrcpyW(interfKeyName, ifaceInfo->symbolicLink);
2653 if (lstrlenW(ifaceInfo->symbolicLink) > 3)
2655 interfKeyName[0] = '#';
2656 interfKeyName[1] = '#';
2657 interfKeyName[3] = '#';
2659 ptr = strchrW(interfKeyName, '\\');
2660 if (ptr)
2661 *ptr = 0;
2662 l = RegCreateKeyExW(parent, interfKeyName, 0, NULL, 0,
2663 samDesired, NULL, &interfKey, NULL);
2664 if (!l)
2666 struct DeviceInfo *devInfo =
2667 (struct DeviceInfo *)ifaceInfo->device->Reserved;
2669 l = RegSetValueExW(interfKey, DeviceInstance, 0, REG_SZ,
2670 (BYTE *)devInfo->instanceId,
2671 (lstrlenW(devInfo->instanceId) + 1) * sizeof(WCHAR));
2672 if (!l)
2674 if (instancePath)
2676 LONG l;
2678 l = RegCreateKeyExW(interfKey, instancePath, 0, NULL, 0,
2679 samDesired, NULL, &key, NULL);
2680 if (l)
2682 SetLastError(l);
2683 key = INVALID_HANDLE_VALUE;
2685 else if (InfHandle)
2686 FIXME("INF section installation unsupported\n");
2689 else
2690 SetLastError(l);
2691 RegCloseKey(interfKey);
2693 else
2694 SetLastError(l);
2695 HeapFree(GetProcessHeap(), 0, interfKeyName);
2696 HeapFree(GetProcessHeap(), 0, instancePath);
2697 RegCloseKey(parent);
2699 else
2700 SetLastError(l);
2701 RegCloseKey(interfacesKey);
2703 else
2704 SetLastError(l);
2705 return key;
2708 /***********************************************************************
2709 * SetupDiDeleteDeviceInterfaceRegKey (SETUPAPI.@)
2711 BOOL WINAPI SetupDiDeleteDeviceInterfaceRegKey(
2712 HDEVINFO DeviceInfoSet,
2713 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2714 DWORD Reserved)
2716 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2717 HKEY parent;
2718 BOOL ret = FALSE;
2720 TRACE("%p %p %d\n", DeviceInfoSet, DeviceInterfaceData, Reserved);
2722 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2723 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2725 SetLastError(ERROR_INVALID_HANDLE);
2726 return FALSE;
2728 if (!DeviceInterfaceData ||
2729 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2730 !DeviceInterfaceData->Reserved)
2732 SetLastError(ERROR_INVALID_PARAMETER);
2733 return FALSE;
2735 parent = SetupDiOpenClassRegKeyExW(&DeviceInterfaceData->InterfaceClassGuid,
2736 KEY_ALL_ACCESS, DIOCR_INTERFACE, NULL, NULL);
2737 if (parent != INVALID_HANDLE_VALUE)
2739 struct InterfaceInfo *ifaceInfo =
2740 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2741 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2743 if (instancePath)
2745 LONG l = RegDeleteKeyW(parent, instancePath);
2747 if (l)
2748 SetLastError(l);
2749 else
2750 ret = TRUE;
2751 HeapFree(GetProcessHeap(), 0, instancePath);
2753 RegCloseKey(parent);
2755 return ret;
2758 /***********************************************************************
2759 * SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2761 * PARAMS
2762 * DeviceInfoSet [I] Set of devices from which to enumerate
2763 * interfaces
2764 * DeviceInfoData [I] (Optional) If specified, a specific device
2765 * instance from which to enumerate interfaces.
2766 * If it isn't specified, all interfaces for all
2767 * devices in the set are enumerated.
2768 * InterfaceClassGuid [I] The interface class to enumerate.
2769 * MemberIndex [I] An index of the interface instance to enumerate.
2770 * A caller should start with MemberIndex set to 0,
2771 * and continue until the function fails with
2772 * ERROR_NO_MORE_ITEMS.
2773 * DeviceInterfaceData [I/O] Returns an enumerated interface. Its cbSize
2774 * member must be set to
2775 * sizeof(SP_DEVICE_INTERFACE_DATA).
2777 * RETURNS
2778 * Success: non-zero value.
2779 * Failure: FALSE. Call GetLastError() for more info.
2781 BOOL WINAPI SetupDiEnumDeviceInterfaces(
2782 HDEVINFO DeviceInfoSet,
2783 PSP_DEVINFO_DATA DeviceInfoData,
2784 CONST GUID * InterfaceClassGuid,
2785 DWORD MemberIndex,
2786 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2788 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2789 BOOL ret = FALSE;
2791 TRACE("%p, %p, %s, %d, %p\n", DeviceInfoSet, DeviceInfoData,
2792 debugstr_guid(InterfaceClassGuid), MemberIndex, DeviceInterfaceData);
2794 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2795 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2797 SetLastError(ERROR_INVALID_HANDLE);
2798 return FALSE;
2800 if (DeviceInfoData && (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA) ||
2801 !DeviceInfoData->Reserved))
2803 SetLastError(ERROR_INVALID_PARAMETER);
2804 return FALSE;
2806 if (!DeviceInterfaceData ||
2807 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2809 SetLastError(ERROR_INVALID_PARAMETER);
2810 return FALSE;
2812 if (DeviceInfoData)
2814 struct DeviceInfo *devInfo =
2815 (struct DeviceInfo *)DeviceInfoData->Reserved;
2816 struct InterfaceInstances *iface;
2818 if ((ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
2820 if (MemberIndex < iface->cInstances)
2821 *DeviceInterfaceData = iface->instances[MemberIndex];
2822 else
2824 SetLastError(ERROR_NO_MORE_ITEMS);
2825 ret = FALSE;
2828 else
2829 SetLastError(ERROR_NO_MORE_ITEMS);
2831 else
2833 struct DeviceInstance *devInst;
2834 DWORD cEnumerated = 0;
2835 BOOL found = FALSE;
2837 LIST_FOR_EACH_ENTRY(devInst, &set->devices, struct DeviceInstance, entry)
2839 struct DeviceInfo *devInfo = (struct DeviceInfo *)devInst->data.Reserved;
2840 struct InterfaceInstances *iface;
2842 if (found || cEnumerated >= MemberIndex + 1)
2843 break;
2844 if (SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface))
2846 if (cEnumerated + iface->cInstances < MemberIndex + 1)
2847 cEnumerated += iface->cInstances;
2848 else
2850 DWORD instanceIndex = MemberIndex - cEnumerated;
2852 *DeviceInterfaceData = iface->instances[instanceIndex];
2853 cEnumerated += instanceIndex + 1;
2854 found = TRUE;
2855 ret = TRUE;
2859 if (!found)
2860 SetLastError(ERROR_NO_MORE_ITEMS);
2862 return ret;
2865 /***********************************************************************
2866 * SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2868 * Destroy a DeviceInfoList and free all used memory of the list.
2870 * PARAMS
2871 * devinfo [I] DeviceInfoList pointer to list to destroy
2873 * RETURNS
2874 * Success: non zero value.
2875 * Failure: zero value.
2877 BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
2879 BOOL ret = FALSE;
2881 TRACE("%p\n", devinfo);
2882 if (devinfo && devinfo != INVALID_HANDLE_VALUE)
2884 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
2886 if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
2888 struct DeviceInstance *devInst, *devInst2;
2890 LIST_FOR_EACH_ENTRY_SAFE(devInst, devInst2, &list->devices,
2891 struct DeviceInstance, entry)
2893 SETUPDI_FreeDeviceInfo( (struct DeviceInfo *)devInst->data.Reserved );
2894 list_remove(&devInst->entry);
2895 HeapFree(GetProcessHeap(), 0, devInst);
2897 HeapFree(GetProcessHeap(), 0, list);
2898 ret = TRUE;
2902 if (ret == FALSE)
2903 SetLastError(ERROR_INVALID_HANDLE);
2905 return ret;
2908 /***********************************************************************
2909 * SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2911 BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
2912 HDEVINFO DeviceInfoSet,
2913 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2914 PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
2915 DWORD DeviceInterfaceDetailDataSize,
2916 PDWORD RequiredSize,
2917 PSP_DEVINFO_DATA DeviceInfoData)
2919 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2920 struct InterfaceInfo *info;
2921 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath)
2922 + 1;
2923 BOOL ret = FALSE;
2925 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2926 DeviceInterfaceData, DeviceInterfaceDetailData,
2927 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2929 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2930 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2932 SetLastError(ERROR_INVALID_HANDLE);
2933 return FALSE;
2935 if (!DeviceInterfaceData ||
2936 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2937 !DeviceInterfaceData->Reserved)
2939 SetLastError(ERROR_INVALID_PARAMETER);
2940 return FALSE;
2942 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize !=
2943 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath) + sizeof(char)))
2945 SetLastError(ERROR_INVALID_USER_BUFFER);
2946 return FALSE;
2948 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2950 SetLastError(ERROR_INVALID_USER_BUFFER);
2951 return FALSE;
2953 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2954 if (info->symbolicLink)
2955 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2956 NULL, 0, NULL, NULL);
2957 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2959 if (info->symbolicLink)
2960 WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2961 DeviceInterfaceDetailData->DevicePath,
2962 DeviceInterfaceDetailDataSize -
2963 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath),
2964 NULL, NULL);
2965 else
2966 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2967 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2968 *DeviceInfoData = *info->device;
2969 ret = TRUE;
2971 else
2973 if (RequiredSize)
2974 *RequiredSize = bytesNeeded;
2975 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2977 return ret;
2980 /***********************************************************************
2981 * SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
2983 BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
2984 HDEVINFO DeviceInfoSet,
2985 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2986 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
2987 DWORD DeviceInterfaceDetailDataSize,
2988 PDWORD RequiredSize,
2989 PSP_DEVINFO_DATA DeviceInfoData)
2991 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2992 struct InterfaceInfo *info;
2993 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)
2994 + sizeof(WCHAR); /* include NULL terminator */
2995 BOOL ret = FALSE;
2997 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2998 DeviceInterfaceData, DeviceInterfaceDetailData,
2999 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
3001 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
3002 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3004 SetLastError(ERROR_INVALID_HANDLE);
3005 return FALSE;
3007 if (!DeviceInterfaceData ||
3008 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
3009 !DeviceInterfaceData->Reserved)
3011 SetLastError(ERROR_INVALID_PARAMETER);
3012 return FALSE;
3014 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
3015 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR) ||
3016 DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W)))
3018 SetLastError(ERROR_INVALID_USER_BUFFER);
3019 return FALSE;
3021 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
3023 SetLastError(ERROR_INVALID_USER_BUFFER);
3024 return FALSE;
3026 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
3027 if (info->symbolicLink)
3028 bytesNeeded += sizeof(WCHAR)*lstrlenW(info->symbolicLink);
3029 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
3031 if (info->symbolicLink)
3032 lstrcpyW(DeviceInterfaceDetailData->DevicePath, info->symbolicLink);
3033 else
3034 DeviceInterfaceDetailData->DevicePath[0] = '\0';
3035 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
3036 *DeviceInfoData = *info->device;
3037 ret = TRUE;
3039 else
3041 if (RequiredSize)
3042 *RequiredSize = bytesNeeded;
3043 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3045 return ret;
3048 struct PropertyMapEntry
3050 DWORD regType;
3051 LPCSTR nameA;
3052 LPCWSTR nameW;
3055 static const struct PropertyMapEntry PropertyMap[] = {
3056 { REG_SZ, "DeviceDesc", DeviceDesc },
3057 { REG_MULTI_SZ, "HardwareId", HardwareId },
3058 { REG_MULTI_SZ, "CompatibleIDs", CompatibleIDs },
3059 { 0, NULL, NULL }, /* SPDRP_UNUSED0 */
3060 { REG_SZ, "Service", Service },
3061 { 0, NULL, NULL }, /* SPDRP_UNUSED1 */
3062 { 0, NULL, NULL }, /* SPDRP_UNUSED2 */
3063 { REG_SZ, "Class", Class },
3064 { REG_SZ, "ClassGUID", ClassGUID },
3065 { REG_SZ, "Driver", Driver },
3066 { REG_DWORD, "ConfigFlags", ConfigFlags },
3067 { REG_SZ, "Mfg", Mfg },
3068 { REG_SZ, "FriendlyName", FriendlyName },
3069 { REG_SZ, "LocationInformation", LocationInformation },
3070 { 0, NULL, NULL }, /* SPDRP_PHYSICAL_DEVICE_OBJECT_NAME */
3071 { REG_DWORD, "Capabilities", Capabilities },
3072 { REG_DWORD, "UINumber", UINumber },
3073 { REG_MULTI_SZ, "UpperFilters", UpperFilters },
3074 { REG_MULTI_SZ, "LowerFilters", LowerFilters },
3077 /***********************************************************************
3078 * SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
3080 BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
3081 HDEVINFO DeviceInfoSet,
3082 PSP_DEVINFO_DATA DeviceInfoData,
3083 DWORD Property,
3084 PDWORD PropertyRegDataType,
3085 PBYTE PropertyBuffer,
3086 DWORD PropertyBufferSize,
3087 PDWORD RequiredSize)
3089 BOOL ret = FALSE;
3090 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3091 struct DeviceInfo *devInfo;
3093 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
3094 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
3095 RequiredSize);
3097 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3099 SetLastError(ERROR_INVALID_HANDLE);
3100 return FALSE;
3102 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3104 SetLastError(ERROR_INVALID_HANDLE);
3105 return FALSE;
3107 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3108 || !DeviceInfoData->Reserved)
3110 SetLastError(ERROR_INVALID_PARAMETER);
3111 return FALSE;
3113 if (PropertyBufferSize && PropertyBuffer == NULL)
3115 SetLastError(ERROR_INVALID_DATA);
3116 return FALSE;
3118 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3119 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3120 && PropertyMap[Property].nameA)
3122 DWORD size = PropertyBufferSize;
3123 LONG l = RegQueryValueExA(devInfo->key, PropertyMap[Property].nameA,
3124 NULL, PropertyRegDataType, PropertyBuffer, &size);
3126 if (l == ERROR_MORE_DATA || !PropertyBufferSize)
3127 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3128 else if (!l)
3129 ret = TRUE;
3130 else
3131 SetLastError(l);
3132 if (RequiredSize)
3133 *RequiredSize = size;
3135 return ret;
3138 /***********************************************************************
3139 * SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
3141 BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
3142 HDEVINFO DeviceInfoSet,
3143 PSP_DEVINFO_DATA DeviceInfoData,
3144 DWORD Property,
3145 PDWORD PropertyRegDataType,
3146 PBYTE PropertyBuffer,
3147 DWORD PropertyBufferSize,
3148 PDWORD RequiredSize)
3150 BOOL ret = FALSE;
3151 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3152 struct DeviceInfo *devInfo;
3154 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
3155 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
3156 RequiredSize);
3158 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3160 SetLastError(ERROR_INVALID_HANDLE);
3161 return FALSE;
3163 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3165 SetLastError(ERROR_INVALID_HANDLE);
3166 return FALSE;
3168 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3169 || !DeviceInfoData->Reserved)
3171 SetLastError(ERROR_INVALID_PARAMETER);
3172 return FALSE;
3174 if (PropertyBufferSize && PropertyBuffer == NULL)
3176 SetLastError(ERROR_INVALID_DATA);
3177 return FALSE;
3179 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3180 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3181 && PropertyMap[Property].nameW)
3183 DWORD size = PropertyBufferSize;
3184 LONG l = RegQueryValueExW(devInfo->key, PropertyMap[Property].nameW,
3185 NULL, PropertyRegDataType, PropertyBuffer, &size);
3187 if (l == ERROR_MORE_DATA || !PropertyBufferSize)
3188 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3189 else if (!l)
3190 ret = TRUE;
3191 else
3192 SetLastError(l);
3193 if (RequiredSize)
3194 *RequiredSize = size;
3196 return ret;
3199 /***********************************************************************
3200 * SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
3202 BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
3203 HDEVINFO DeviceInfoSet,
3204 PSP_DEVINFO_DATA DeviceInfoData,
3205 DWORD Property,
3206 const BYTE *PropertyBuffer,
3207 DWORD PropertyBufferSize)
3209 BOOL ret = FALSE;
3210 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3211 struct DeviceInfo *devInfo;
3213 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
3214 PropertyBuffer, PropertyBufferSize);
3216 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3218 SetLastError(ERROR_INVALID_HANDLE);
3219 return FALSE;
3221 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3223 SetLastError(ERROR_INVALID_HANDLE);
3224 return FALSE;
3226 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3227 || !DeviceInfoData->Reserved)
3229 SetLastError(ERROR_INVALID_PARAMETER);
3230 return FALSE;
3232 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3233 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3234 && PropertyMap[Property].nameA)
3236 LONG l = RegSetValueExA(devInfo->key, PropertyMap[Property].nameA, 0,
3237 PropertyMap[Property].regType, PropertyBuffer,
3238 PropertyBufferSize);
3239 if (!l)
3240 ret = TRUE;
3241 else
3242 SetLastError(l);
3244 return ret;
3247 /***********************************************************************
3248 * SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
3250 BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
3251 HDEVINFO DeviceInfoSet,
3252 PSP_DEVINFO_DATA DeviceInfoData,
3253 DWORD Property,
3254 const BYTE *PropertyBuffer,
3255 DWORD PropertyBufferSize)
3257 BOOL ret = FALSE;
3258 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3259 struct DeviceInfo *devInfo;
3261 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
3262 PropertyBuffer, PropertyBufferSize);
3264 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3266 SetLastError(ERROR_INVALID_HANDLE);
3267 return FALSE;
3269 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3271 SetLastError(ERROR_INVALID_HANDLE);
3272 return FALSE;
3274 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3275 || !DeviceInfoData->Reserved)
3277 SetLastError(ERROR_INVALID_PARAMETER);
3278 return FALSE;
3280 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3281 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3282 && PropertyMap[Property].nameW)
3284 LONG l = RegSetValueExW(devInfo->key, PropertyMap[Property].nameW, 0,
3285 PropertyMap[Property].regType, PropertyBuffer,
3286 PropertyBufferSize);
3287 if (!l)
3288 ret = TRUE;
3289 else
3290 SetLastError(l);
3292 return ret;
3295 /***********************************************************************
3296 * SetupDiInstallClassA (SETUPAPI.@)
3298 BOOL WINAPI SetupDiInstallClassA(
3299 HWND hwndParent,
3300 PCSTR InfFileName,
3301 DWORD Flags,
3302 HSPFILEQ FileQueue)
3304 UNICODE_STRING FileNameW;
3305 BOOL Result;
3307 if (!InfFileName)
3309 SetLastError(ERROR_INVALID_PARAMETER);
3310 return FALSE;
3312 if (!RtlCreateUnicodeStringFromAsciiz(&FileNameW, InfFileName))
3314 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3315 return FALSE;
3318 Result = SetupDiInstallClassW(hwndParent, FileNameW.Buffer, Flags, FileQueue);
3320 RtlFreeUnicodeString(&FileNameW);
3322 return Result;
3325 static HKEY CreateClassKey(HINF hInf)
3327 static const WCHAR slash[] = { '\\',0 };
3328 WCHAR FullBuffer[MAX_PATH];
3329 WCHAR Buffer[MAX_PATH];
3330 DWORD RequiredSize;
3331 HKEY hClassKey;
3333 if (!SetupGetLineTextW(NULL,
3334 hInf,
3335 Version,
3336 ClassGUID,
3337 Buffer,
3338 MAX_PATH,
3339 &RequiredSize))
3341 return INVALID_HANDLE_VALUE;
3344 lstrcpyW(FullBuffer, ControlClass);
3345 lstrcatW(FullBuffer, slash);
3346 lstrcatW(FullBuffer, Buffer);
3348 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3349 FullBuffer,
3351 KEY_ALL_ACCESS,
3352 &hClassKey))
3354 if (!SetupGetLineTextW(NULL,
3355 hInf,
3356 Version,
3357 Class,
3358 Buffer,
3359 MAX_PATH,
3360 &RequiredSize))
3362 return INVALID_HANDLE_VALUE;
3365 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
3366 FullBuffer,
3368 NULL,
3369 REG_OPTION_NON_VOLATILE,
3370 KEY_ALL_ACCESS,
3371 NULL,
3372 &hClassKey,
3373 NULL))
3375 return INVALID_HANDLE_VALUE;
3380 if (RegSetValueExW(hClassKey,
3381 Class,
3383 REG_SZ,
3384 (LPBYTE)Buffer,
3385 RequiredSize * sizeof(WCHAR)))
3387 RegCloseKey(hClassKey);
3388 RegDeleteKeyW(HKEY_LOCAL_MACHINE,
3389 FullBuffer);
3390 return INVALID_HANDLE_VALUE;
3393 return hClassKey;
3396 /***********************************************************************
3397 * SetupDiInstallClassW (SETUPAPI.@)
3399 BOOL WINAPI SetupDiInstallClassW(
3400 HWND hwndParent,
3401 PCWSTR InfFileName,
3402 DWORD Flags,
3403 HSPFILEQ FileQueue)
3405 WCHAR SectionName[MAX_PATH];
3406 DWORD SectionNameLength = 0;
3407 HINF hInf;
3408 BOOL bFileQueueCreated = FALSE;
3409 HKEY hClassKey;
3412 FIXME("\n");
3414 if (!InfFileName)
3416 SetLastError(ERROR_INVALID_PARAMETER);
3417 return FALSE;
3419 if ((Flags & DI_NOVCP) && (FileQueue == NULL || FileQueue == INVALID_HANDLE_VALUE))
3421 SetLastError(ERROR_INVALID_PARAMETER);
3422 return FALSE;
3425 /* Open the .inf file */
3426 hInf = SetupOpenInfFileW(InfFileName,
3427 NULL,
3428 INF_STYLE_WIN4,
3429 NULL);
3430 if (hInf == INVALID_HANDLE_VALUE)
3433 return FALSE;
3436 /* Create or open the class registry key 'HKLM\\CurrentControlSet\\Class\\{GUID}' */
3437 hClassKey = CreateClassKey(hInf);
3438 if (hClassKey == INVALID_HANDLE_VALUE)
3440 SetupCloseInfFile(hInf);
3441 return FALSE;
3445 /* Try to append a layout file */
3446 SetupOpenAppendInfFileW(NULL, hInf, NULL);
3448 /* Retrieve the actual section name */
3449 SetupDiGetActualSectionToInstallW(hInf,
3450 ClassInstall32,
3451 SectionName,
3452 MAX_PATH,
3453 &SectionNameLength,
3454 NULL);
3456 #if 0
3457 if (!(Flags & DI_NOVCP))
3459 FileQueue = SetupOpenFileQueue();
3460 if (FileQueue == INVALID_HANDLE_VALUE)
3462 SetupCloseInfFile(hInf);
3463 return FALSE;
3466 bFileQueueCreated = TRUE;
3469 #endif
3471 SetupInstallFromInfSectionW(NULL,
3472 hInf,
3473 SectionName,
3474 SPINST_COPYINF | SPINST_FILES | SPINST_REGISTRY,
3475 hClassKey,
3476 NULL,
3478 NULL,
3479 NULL,
3480 INVALID_HANDLE_VALUE,
3481 NULL);
3483 /* FIXME: More code! */
3485 if (bFileQueueCreated)
3486 SetupCloseFileQueue(FileQueue);
3488 SetupCloseInfFile(hInf);
3490 return TRUE;
3494 /***********************************************************************
3495 * SetupDiOpenClassRegKey (SETUPAPI.@)
3497 HKEY WINAPI SetupDiOpenClassRegKey(
3498 const GUID* ClassGuid,
3499 REGSAM samDesired)
3501 return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3502 DIOCR_INSTALLER, NULL, NULL);
3506 /***********************************************************************
3507 * SetupDiOpenClassRegKeyExA (SETUPAPI.@)
3509 HKEY WINAPI SetupDiOpenClassRegKeyExA(
3510 const GUID* ClassGuid,
3511 REGSAM samDesired,
3512 DWORD Flags,
3513 PCSTR MachineName,
3514 PVOID Reserved)
3516 PWSTR MachineNameW = NULL;
3517 HKEY hKey;
3519 TRACE("\n");
3521 if (MachineName)
3523 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
3524 if (MachineNameW == NULL)
3525 return INVALID_HANDLE_VALUE;
3528 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3529 Flags, MachineNameW, Reserved);
3531 MyFree(MachineNameW);
3533 return hKey;
3537 /***********************************************************************
3538 * SetupDiOpenClassRegKeyExW (SETUPAPI.@)
3540 HKEY WINAPI SetupDiOpenClassRegKeyExW(
3541 const GUID* ClassGuid,
3542 REGSAM samDesired,
3543 DWORD Flags,
3544 PCWSTR MachineName,
3545 PVOID Reserved)
3547 HKEY hClassesKey;
3548 HKEY key;
3549 LPCWSTR lpKeyName;
3550 LONG l;
3552 if (MachineName != NULL)
3554 FIXME("Remote access not supported yet!\n");
3555 return INVALID_HANDLE_VALUE;
3558 if (Flags == DIOCR_INSTALLER)
3560 lpKeyName = ControlClass;
3562 else if (Flags == DIOCR_INTERFACE)
3564 lpKeyName = DeviceClasses;
3566 else
3568 ERR("Invalid Flags parameter!\n");
3569 SetLastError(ERROR_INVALID_PARAMETER);
3570 return INVALID_HANDLE_VALUE;
3573 if (!ClassGuid)
3575 if ((l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3576 lpKeyName,
3578 samDesired,
3579 &hClassesKey)))
3581 SetLastError(l);
3582 hClassesKey = INVALID_HANDLE_VALUE;
3584 key = hClassesKey;
3586 else
3588 WCHAR bracedGuidString[39];
3590 SETUPDI_GuidToString(ClassGuid, bracedGuidString);
3592 if (!(l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3593 lpKeyName,
3595 samDesired,
3596 &hClassesKey)))
3598 if ((l = RegOpenKeyExW(hClassesKey,
3599 bracedGuidString,
3601 samDesired,
3602 &key)))
3604 SetLastError(l);
3605 key = INVALID_HANDLE_VALUE;
3607 RegCloseKey(hClassesKey);
3609 else
3611 SetLastError(l);
3612 key = INVALID_HANDLE_VALUE;
3615 return key;
3618 /***********************************************************************
3619 * SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3621 BOOL WINAPI SetupDiOpenDeviceInterfaceW(
3622 HDEVINFO DeviceInfoSet,
3623 PCWSTR DevicePath,
3624 DWORD OpenFlags,
3625 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3627 FIXME("%p %s %08x %p\n",
3628 DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
3629 return FALSE;
3632 /***********************************************************************
3633 * SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3635 BOOL WINAPI SetupDiOpenDeviceInterfaceA(
3636 HDEVINFO DeviceInfoSet,
3637 PCSTR DevicePath,
3638 DWORD OpenFlags,
3639 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3641 FIXME("%p %s %08x %p\n", DeviceInfoSet,
3642 debugstr_a(DevicePath), OpenFlags, DeviceInterfaceData);
3643 return FALSE;
3646 /***********************************************************************
3647 * SetupDiSetClassInstallParamsA (SETUPAPI.@)
3649 BOOL WINAPI SetupDiSetClassInstallParamsA(
3650 HDEVINFO DeviceInfoSet,
3651 PSP_DEVINFO_DATA DeviceInfoData,
3652 PSP_CLASSINSTALL_HEADER ClassInstallParams,
3653 DWORD ClassInstallParamsSize)
3655 FIXME("%p %p %x %u\n",DeviceInfoSet, DeviceInfoData,
3656 ClassInstallParams->InstallFunction, ClassInstallParamsSize);
3657 return FALSE;
3660 /***********************************************************************
3661 * SetupDiCallClassInstaller (SETUPAPI.@)
3663 BOOL WINAPI SetupDiCallClassInstaller(
3664 DI_FUNCTION InstallFunction,
3665 HDEVINFO DeviceInfoSet,
3666 PSP_DEVINFO_DATA DeviceInfoData)
3668 FIXME("%d %p %p\n", InstallFunction, DeviceInfoSet, DeviceInfoData);
3669 return FALSE;
3672 /***********************************************************************
3673 * SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
3675 BOOL WINAPI SetupDiGetDeviceInstallParamsA(
3676 HDEVINFO DeviceInfoSet,
3677 PSP_DEVINFO_DATA DeviceInfoData,
3678 PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
3680 FIXME("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
3681 return FALSE;
3684 static HKEY SETUPDI_OpenDevKey(struct DeviceInfo *devInfo, REGSAM samDesired)
3686 HKEY enumKey, key = INVALID_HANDLE_VALUE;
3687 LONG l;
3689 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
3690 NULL, &enumKey, NULL);
3691 if (!l)
3693 RegOpenKeyExW(enumKey, devInfo->instanceId, 0, samDesired, &key);
3694 RegCloseKey(enumKey);
3696 return key;
3699 static HKEY SETUPDI_OpenDrvKey(struct DeviceInfo *devInfo, REGSAM samDesired)
3701 static const WCHAR slash[] = { '\\',0 };
3702 WCHAR classKeyPath[MAX_PATH];
3703 HKEY classKey, key = INVALID_HANDLE_VALUE;
3704 LONG l;
3706 lstrcpyW(classKeyPath, ControlClass);
3707 lstrcatW(classKeyPath, slash);
3708 SETUPDI_GuidToString(&devInfo->set->ClassGuid,
3709 classKeyPath + lstrlenW(classKeyPath));
3710 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
3711 KEY_ALL_ACCESS, NULL, &classKey, NULL);
3712 if (!l)
3714 static const WCHAR fmt[] = { '%','0','4','u',0 };
3715 WCHAR devId[10];
3717 sprintfW(devId, fmt, devInfo->devId);
3718 RegOpenKeyExW(classKey, devId, 0, samDesired, &key);
3719 RegCloseKey(classKey);
3721 return key;
3724 /***********************************************************************
3725 * SetupDiOpenDevRegKey (SETUPAPI.@)
3727 HKEY WINAPI SetupDiOpenDevRegKey(
3728 HDEVINFO DeviceInfoSet,
3729 PSP_DEVINFO_DATA DeviceInfoData,
3730 DWORD Scope,
3731 DWORD HwProfile,
3732 DWORD KeyType,
3733 REGSAM samDesired)
3735 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3736 struct DeviceInfo *devInfo;
3737 HKEY key = INVALID_HANDLE_VALUE;
3739 TRACE("%p %p %d %d %d %x\n", DeviceInfoSet, DeviceInfoData,
3740 Scope, HwProfile, KeyType, samDesired);
3742 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3744 SetLastError(ERROR_INVALID_HANDLE);
3745 return INVALID_HANDLE_VALUE;
3747 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3749 SetLastError(ERROR_INVALID_HANDLE);
3750 return INVALID_HANDLE_VALUE;
3752 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3753 || !DeviceInfoData->Reserved)
3755 SetLastError(ERROR_INVALID_PARAMETER);
3756 return INVALID_HANDLE_VALUE;
3758 if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
3760 SetLastError(ERROR_INVALID_FLAGS);
3761 return INVALID_HANDLE_VALUE;
3763 if (KeyType != DIREG_DEV && KeyType != DIREG_DRV)
3765 SetLastError(ERROR_INVALID_FLAGS);
3766 return INVALID_HANDLE_VALUE;
3768 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3769 if (devInfo->set != set)
3771 SetLastError(ERROR_INVALID_PARAMETER);
3772 return INVALID_HANDLE_VALUE;
3774 if (devInfo->phantom)
3776 SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
3777 return INVALID_HANDLE_VALUE;
3779 if (Scope != DICS_FLAG_GLOBAL)
3780 FIXME("unimplemented for scope %d\n", Scope);
3781 switch (KeyType)
3783 case DIREG_DEV:
3784 key = SETUPDI_OpenDevKey(devInfo, samDesired);
3785 break;
3786 case DIREG_DRV:
3787 key = SETUPDI_OpenDrvKey(devInfo, samDesired);
3788 break;
3789 default:
3790 WARN("unknown KeyType %d\n", KeyType);
3792 return key;
3795 static BOOL SETUPDI_DeleteDevKey(struct DeviceInfo *devInfo)
3797 HKEY enumKey;
3798 BOOL ret = FALSE;
3799 LONG l;
3801 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
3802 NULL, &enumKey, NULL);
3803 if (!l)
3805 ret = RegDeleteTreeW(enumKey, devInfo->instanceId);
3806 RegCloseKey(enumKey);
3808 else
3809 SetLastError(l);
3810 return ret;
3813 static BOOL SETUPDI_DeleteDrvKey(struct DeviceInfo *devInfo)
3815 static const WCHAR slash[] = { '\\',0 };
3816 WCHAR classKeyPath[MAX_PATH];
3817 HKEY classKey;
3818 LONG l;
3819 BOOL ret = FALSE;
3821 lstrcpyW(classKeyPath, ControlClass);
3822 lstrcatW(classKeyPath, slash);
3823 SETUPDI_GuidToString(&devInfo->set->ClassGuid,
3824 classKeyPath + lstrlenW(classKeyPath));
3825 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
3826 KEY_ALL_ACCESS, NULL, &classKey, NULL);
3827 if (!l)
3829 static const WCHAR fmt[] = { '%','0','4','u',0 };
3830 WCHAR devId[10];
3832 sprintfW(devId, fmt, devInfo->devId);
3833 ret = RegDeleteTreeW(classKey, devId);
3834 RegCloseKey(classKey);
3836 else
3837 SetLastError(l);
3838 return ret;
3841 /***********************************************************************
3842 * SetupDiOpenDevRegKey (SETUPAPI.@)
3844 BOOL WINAPI SetupDiDeleteDevRegKey(
3845 HDEVINFO DeviceInfoSet,
3846 PSP_DEVINFO_DATA DeviceInfoData,
3847 DWORD Scope,
3848 DWORD HwProfile,
3849 DWORD KeyType)
3851 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3852 struct DeviceInfo *devInfo;
3853 BOOL ret = FALSE;
3855 TRACE("%p %p %d %d %d\n", DeviceInfoSet, DeviceInfoData, Scope, HwProfile,
3856 KeyType);
3858 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3860 SetLastError(ERROR_INVALID_HANDLE);
3861 return FALSE;
3863 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3865 SetLastError(ERROR_INVALID_HANDLE);
3866 return FALSE;
3868 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3869 || !DeviceInfoData->Reserved)
3871 SetLastError(ERROR_INVALID_PARAMETER);
3872 return FALSE;
3874 if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
3876 SetLastError(ERROR_INVALID_FLAGS);
3877 return FALSE;
3879 if (KeyType != DIREG_DEV && KeyType != DIREG_DRV && KeyType != DIREG_BOTH)
3881 SetLastError(ERROR_INVALID_FLAGS);
3882 return FALSE;
3884 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3885 if (devInfo->set != set)
3887 SetLastError(ERROR_INVALID_PARAMETER);
3888 return FALSE;
3890 if (devInfo->phantom)
3892 SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
3893 return FALSE;
3895 if (Scope != DICS_FLAG_GLOBAL)
3896 FIXME("unimplemented for scope %d\n", Scope);
3897 switch (KeyType)
3899 case DIREG_DEV:
3900 ret = SETUPDI_DeleteDevKey(devInfo);
3901 break;
3902 case DIREG_DRV:
3903 ret = SETUPDI_DeleteDrvKey(devInfo);
3904 break;
3905 case DIREG_BOTH:
3906 ret = SETUPDI_DeleteDevKey(devInfo);
3907 if (ret)
3908 ret = SETUPDI_DeleteDrvKey(devInfo);
3909 break;
3910 default:
3911 WARN("unknown KeyType %d\n", KeyType);
3913 return ret;
3916 /***********************************************************************
3917 * CM_Get_Device_IDA (SETUPAPI.@)
3919 CONFIGRET WINAPI CM_Get_Device_IDA( DEVINST dnDevInst, PSTR Buffer,
3920 ULONG BufferLen, ULONG ulFlags)
3922 struct DeviceInfo *devInfo = GlobalLock((HANDLE)dnDevInst);
3924 TRACE("%x->%p, %p, %u %u\n", dnDevInst, devInfo, Buffer, BufferLen, ulFlags);
3926 if (!devInfo)
3927 return CR_NO_SUCH_DEVINST;
3929 WideCharToMultiByte(CP_ACP, 0, devInfo->instanceId, -1, Buffer, BufferLen, 0, 0);
3930 TRACE("Returning %s\n", debugstr_a(Buffer));
3931 return CR_SUCCESS;
3934 /***********************************************************************
3935 * CM_Get_Device_IDW (SETUPAPI.@)
3937 CONFIGRET WINAPI CM_Get_Device_IDW( DEVINST dnDevInst, LPWSTR Buffer,
3938 ULONG BufferLen, ULONG ulFlags)
3940 struct DeviceInfo *devInfo = GlobalLock((HANDLE)dnDevInst);
3942 TRACE("%x->%p, %p, %u %u\n", dnDevInst, devInfo, Buffer, BufferLen, ulFlags);
3944 if (!devInfo)
3946 WARN("dev instance %d not found!\n", dnDevInst);
3947 return CR_NO_SUCH_DEVINST;
3950 lstrcpynW(Buffer, devInfo->instanceId, BufferLen);
3951 TRACE("Returning %s\n", debugstr_w(Buffer));
3952 GlobalUnlock((HANDLE)dnDevInst);
3953 return CR_SUCCESS;
3958 /***********************************************************************
3959 * CM_Get_Device_ID_Size (SETUPAPI.@)
3961 CONFIGRET WINAPI CM_Get_Device_ID_Size( PULONG pulLen, DEVINST dnDevInst,
3962 ULONG ulFlags)
3964 struct DeviceInfo *ppdevInfo = GlobalLock((HANDLE)dnDevInst);
3966 TRACE("%x->%p, %p, %u\n", dnDevInst, ppdevInfo, pulLen, ulFlags);
3968 if (!ppdevInfo)
3970 WARN("dev instance %d not found!\n", dnDevInst);
3971 return CR_NO_SUCH_DEVINST;
3974 *pulLen = lstrlenW(ppdevInfo->instanceId);
3975 GlobalUnlock((HANDLE)dnDevInst);
3976 return CR_SUCCESS;