setupapi: Partially implement SetupDiGetClassDevsExW.
[wine/wine-kai.git] / dlls / setupapi / devinst.c
blob0df3b709bca8f205075f28c45fe2117baf0e9868
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 SP_DEVINFO_DATA *devices;
104 /* Pointed to by SP_DEVICE_INTERFACE_DATA's Reserved member */
105 struct InterfaceInfo
107 LPWSTR referenceString;
108 LPWSTR symbolicLink;
109 PSP_DEVINFO_DATA device;
112 /* A device may have multiple instances of the same interface, so this holds
113 * each instance belonging to a particular interface.
115 struct InterfaceInstances
117 GUID guid;
118 DWORD cInstances;
119 DWORD cInstancesAllocated;
120 SP_DEVICE_INTERFACE_DATA *instances;
121 struct list entry;
124 /* Pointed to by SP_DEVINFO_DATA's Reserved member */
125 struct DeviceInfo
127 struct DeviceInfoSet *set;
128 HKEY key;
129 BOOL phantom;
130 LPWSTR instanceId;
131 struct list interfaces;
134 static void SETUPDI_GuidToString(const GUID *guid, LPWSTR guidStr)
136 static const WCHAR fmt[] = {'{','%','0','8','X','-','%','0','4','X','-',
137 '%','0','4','X','-','%','0','2','X','%','0','2','X','-','%','0','2',
138 'X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%',
139 '0','2','X','}',0};
141 sprintfW(guidStr, fmt, guid->Data1, guid->Data2, guid->Data3,
142 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
143 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
146 static void SETUPDI_FreeInterfaceInstances(struct InterfaceInstances *instances)
148 DWORD i;
150 for (i = 0; i < instances->cInstances; i++)
152 struct InterfaceInfo *ifaceInfo =
153 (struct InterfaceInfo *)instances->instances[i].Reserved;
155 if (ifaceInfo->device && ifaceInfo->device->Reserved)
157 struct DeviceInfo *devInfo =
158 (struct DeviceInfo *)ifaceInfo->device->Reserved;
160 if (devInfo->phantom)
161 SetupDiDeleteDeviceInterfaceRegKey(devInfo->set,
162 &instances->instances[i], 0);
164 HeapFree(GetProcessHeap(), 0, ifaceInfo->referenceString);
165 HeapFree(GetProcessHeap(), 0, ifaceInfo->symbolicLink);
167 HeapFree(GetProcessHeap(), 0, instances->instances);
170 /* Finds the interface with interface class InterfaceClassGuid in the device.
171 * Returns TRUE if found, and updates *interface to point to device's
172 * interfaces member where the given interface was found.
173 * Returns FALSE if not found.
175 static BOOL SETUPDI_FindInterface(const struct DeviceInfo *devInfo,
176 const GUID *InterfaceClassGuid, struct InterfaceInstances **interface)
178 BOOL found = FALSE;
179 struct InterfaceInstances *iface;
181 TRACE("%s\n", debugstr_guid(InterfaceClassGuid));
183 LIST_FOR_EACH_ENTRY(iface, &devInfo->interfaces, struct InterfaceInstances,
184 entry)
186 if (IsEqualGUID(&iface->guid, InterfaceClassGuid))
188 *interface = iface;
189 found = TRUE;
190 break;
193 TRACE("returning %d (%p)\n", found, found ? *interface : NULL);
194 return found;
197 /* Finds the interface instance with reference string ReferenceString in the
198 * interface instance map. Returns TRUE if found, and updates instanceIndex to
199 * the index of the interface instance's instances member
200 * where the given instance was found. Returns FALSE if not found.
202 static BOOL SETUPDI_FindInterfaceInstance(
203 const struct InterfaceInstances *instances,
204 LPCWSTR ReferenceString, DWORD *instanceIndex)
206 BOOL found = FALSE;
207 DWORD i;
209 TRACE("%s\n", debugstr_w(ReferenceString));
211 for (i = 0; !found && i < instances->cInstances; i++)
213 SP_DEVICE_INTERFACE_DATA *ifaceData = &instances->instances[i];
214 struct InterfaceInfo *ifaceInfo =
215 (struct InterfaceInfo *)ifaceData->Reserved;
217 if (!ReferenceString && !ifaceInfo->referenceString)
219 *instanceIndex = i;
220 found = TRUE;
222 else if (ReferenceString && ifaceInfo->referenceString &&
223 !lstrcmpiW(ifaceInfo->referenceString, ReferenceString))
225 *instanceIndex = i;
226 found = TRUE;
229 TRACE("returning %d (%d)\n", found, found ? *instanceIndex : 0);
230 return found;
233 static LPWSTR SETUPDI_CreateSymbolicLinkPath(LPCWSTR instanceId,
234 const GUID *InterfaceClassGuid, LPCWSTR ReferenceString)
236 static const WCHAR fmt[] = {'\\','\\','?','\\','%','s','#','%','s',0};
237 WCHAR guidStr[39];
238 DWORD len;
239 LPWSTR ret;
241 SETUPDI_GuidToString(InterfaceClassGuid, guidStr);
242 /* omit length of format specifiers, but include NULL terminator: */
243 len = lstrlenW(fmt) - 4 + 1;
244 len += lstrlenW(instanceId) + lstrlenW(guidStr);
245 if (ReferenceString)
247 /* space for a hash between string and reference string: */
248 len += lstrlenW(ReferenceString) + 1;
250 ret = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
251 if (ret)
253 int printed = sprintfW(ret, fmt, instanceId, guidStr);
254 LPWSTR ptr;
256 /* replace '\\' with '#' after the "\\\\?\\" beginning */
257 for (ptr = strchrW(ret + 4, '\\'); ptr; ptr = strchrW(ptr + 1, '\\'))
258 *ptr = '#';
259 if (ReferenceString)
261 ret[printed - 1] = '\\';
262 lstrcpyW(ret + printed, ReferenceString);
265 return ret;
268 /* Adds an interface with the given interface class and reference string to
269 * the device, if it doesn't already exist in the device. If iface is not
270 * NULL, returns a pointer to the newly added (or already existing) interface.
272 static BOOL SETUPDI_AddInterfaceInstance(PSP_DEVINFO_DATA DeviceInfoData,
273 const GUID *InterfaceClassGuid, LPCWSTR ReferenceString,
274 SP_DEVICE_INTERFACE_DATA **ifaceData)
276 struct DeviceInfo *devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
277 BOOL newInterface = FALSE, ret;
278 struct InterfaceInstances *iface = NULL;
280 TRACE("%p %s %s %p\n", devInfo, debugstr_guid(InterfaceClassGuid),
281 debugstr_w(ReferenceString), iface);
283 if (!(ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
285 iface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
286 sizeof(struct InterfaceInstances));
287 if (iface)
289 list_add_tail(&devInfo->interfaces, &iface->entry);
290 newInterface = TRUE;
293 if (iface)
295 DWORD instanceIndex = 0;
297 if (!(ret = SETUPDI_FindInterfaceInstance(iface, ReferenceString,
298 &instanceIndex)))
300 SP_DEVICE_INTERFACE_DATA *instance = NULL;
302 if (!iface->cInstancesAllocated)
304 iface->instances = HeapAlloc(GetProcessHeap(), 0,
305 sizeof(SP_DEVICE_INTERFACE_DATA));
306 if (iface->instances)
307 instance = &iface->instances[iface->cInstancesAllocated++];
309 else if (iface->cInstances == iface->cInstancesAllocated)
311 iface->instances = HeapReAlloc(GetProcessHeap(), 0,
312 iface->instances,
313 (iface->cInstancesAllocated + 1) *
314 sizeof(SP_DEVICE_INTERFACE_DATA));
315 if (iface->instances)
316 instance = &iface->instances[iface->cInstancesAllocated++];
318 else
319 instance = &iface->instances[iface->cInstances];
320 if (instance)
322 struct InterfaceInfo *ifaceInfo = HeapAlloc(GetProcessHeap(),
323 0, sizeof(struct InterfaceInfo));
325 if (ifaceInfo)
327 ret = TRUE;
328 ifaceInfo->device = DeviceInfoData;
329 ifaceInfo->symbolicLink = SETUPDI_CreateSymbolicLinkPath(
330 devInfo->instanceId, InterfaceClassGuid,
331 ReferenceString);
332 if (ReferenceString)
334 ifaceInfo->referenceString =
335 HeapAlloc(GetProcessHeap(), 0,
336 (lstrlenW(ReferenceString) + 1) *
337 sizeof(WCHAR));
338 if (ifaceInfo->referenceString)
339 lstrcpyW(ifaceInfo->referenceString,
340 ReferenceString);
341 else
342 ret = FALSE;
344 else
345 ifaceInfo->referenceString = NULL;
346 if (ret)
348 HKEY key;
350 iface->cInstances++;
351 instance->cbSize =
352 sizeof(SP_DEVICE_INTERFACE_DATA);
353 memcpy(&instance->InterfaceClassGuid,
354 InterfaceClassGuid, sizeof(GUID));
355 instance->Flags = SPINT_ACTIVE; /* FIXME */
356 instance->Reserved = (ULONG_PTR)ifaceInfo;
357 if (newInterface)
358 memcpy(&iface->guid, InterfaceClassGuid,
359 sizeof(GUID));
360 key = SetupDiCreateDeviceInterfaceRegKeyW(devInfo->set,
361 instance, 0, KEY_WRITE, NULL, NULL);
362 if (key != INVALID_HANDLE_VALUE)
364 RegSetValueExW(key, SymbolicLink, 0, REG_SZ,
365 (BYTE *)ifaceInfo->symbolicLink,
366 lstrlenW(ifaceInfo->symbolicLink) *
367 sizeof(WCHAR));
368 RegCloseKey(key);
370 if (ifaceData)
371 *ifaceData = instance;
373 else
374 HeapFree(GetProcessHeap(), 0, ifaceInfo);
378 else
380 if (ifaceData)
381 *ifaceData = &iface->instances[instanceIndex];
384 else
385 ret = FALSE;
386 TRACE("returning %d\n", ret);
387 return ret;
390 static BOOL SETUPDI_SetInterfaceSymbolicLink(SP_DEVICE_INTERFACE_DATA *iface,
391 LPCWSTR symbolicLink)
393 struct InterfaceInfo *info = (struct InterfaceInfo *)iface->Reserved;
394 BOOL ret = FALSE;
396 if (info)
398 HeapFree(GetProcessHeap(), 0, info->symbolicLink);
399 info->symbolicLink = HeapAlloc(GetProcessHeap(), 0,
400 (lstrlenW(symbolicLink) + 1) * sizeof(WCHAR));
401 if (info->symbolicLink)
403 lstrcpyW(info->symbolicLink, symbolicLink);
404 ret = TRUE;
407 return ret;
410 static struct DeviceInfo *SETUPDI_AllocateDeviceInfo(struct DeviceInfoSet *set,
411 LPCWSTR instanceId, BOOL phantom)
413 struct DeviceInfo *devInfo = HeapAlloc(GetProcessHeap(), 0,
414 sizeof(struct DeviceInfo));
416 if (devInfo)
418 devInfo->set = set;
419 devInfo->instanceId = HeapAlloc(GetProcessHeap(), 0,
420 (lstrlenW(instanceId) + 1) * sizeof(WCHAR));
421 if (devInfo->instanceId)
423 HKEY enumKey;
424 LONG l;
426 devInfo->key = INVALID_HANDLE_VALUE;
427 devInfo->phantom = phantom;
428 lstrcpyW(devInfo->instanceId, instanceId);
429 struprW(devInfo->instanceId);
430 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0,
431 KEY_ALL_ACCESS, NULL, &enumKey, NULL);
432 if (!l)
434 RegCreateKeyExW(enumKey, devInfo->instanceId, 0, NULL, 0,
435 KEY_ALL_ACCESS, NULL, &devInfo->key, NULL);
436 if (phantom)
437 RegSetValueExW(devInfo->key, Phantom, 0, REG_DWORD,
438 (LPBYTE)&phantom, sizeof(phantom));
439 RegCloseKey(enumKey);
441 list_init(&devInfo->interfaces);
443 else
445 HeapFree(GetProcessHeap(), 0, devInfo);
446 devInfo = NULL;
449 return devInfo;
452 static void SETUPDI_FreeDeviceInfo(struct DeviceInfo *devInfo)
454 struct InterfaceInstances *iface, *next;
456 if (devInfo->key != INVALID_HANDLE_VALUE)
457 RegCloseKey(devInfo->key);
458 if (devInfo->phantom)
460 HKEY enumKey;
461 LONG l;
463 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0,
464 KEY_ALL_ACCESS, NULL, &enumKey, NULL);
465 if (!l)
467 RegDeleteTreeW(enumKey, devInfo->instanceId);
468 RegCloseKey(enumKey);
471 HeapFree(GetProcessHeap(), 0, devInfo->instanceId);
472 LIST_FOR_EACH_ENTRY_SAFE(iface, next, &devInfo->interfaces,
473 struct InterfaceInstances, entry)
475 list_remove(&iface->entry);
476 SETUPDI_FreeInterfaceInstances(iface);
477 HeapFree(GetProcessHeap(), 0, iface);
479 HeapFree(GetProcessHeap(), 0, devInfo);
482 /* Adds a device with GUID guid and identifer devInst to set. Allocates a
483 * struct DeviceInfo, and points the returned device info's Reserved member
484 * to it. "Phantom" devices are deleted from the registry when closed.
485 * Returns a pointer to the newly allocated device info.
487 static BOOL SETUPDI_AddDeviceToSet(struct DeviceInfoSet *set,
488 const GUID *guid,
489 DWORD devInst,
490 LPCWSTR instanceId,
491 BOOL phantom,
492 SP_DEVINFO_DATA **dev)
494 BOOL ret = FALSE;
495 struct DeviceInfo *devInfo = SETUPDI_AllocateDeviceInfo(set, instanceId,
496 phantom);
498 TRACE("%p, %s, %d, %s, %d\n", set, debugstr_guid(guid), devInst,
499 debugstr_w(instanceId), phantom);
501 if (devInfo)
503 if (set->devices)
504 set->devices = HeapReAlloc(GetProcessHeap(), 0, set->devices,
505 (set->cDevices + 1) * sizeof(SP_DEVINFO_DATA));
506 else
507 set->devices = HeapAlloc(GetProcessHeap(), 0,
508 sizeof(SP_DEVINFO_DATA));
509 if (set->devices)
511 WCHAR classGuidStr[39];
513 *dev = &set->devices[set->cDevices++];
514 (*dev)->cbSize = sizeof(SP_DEVINFO_DATA);
515 memcpy(&(*dev)->ClassGuid, guid, sizeof(GUID));
516 (*dev)->DevInst = devInst;
517 (*dev)->Reserved = (ULONG_PTR)devInfo;
518 SETUPDI_GuidToString(guid, classGuidStr);
519 SetupDiSetDeviceRegistryPropertyW((HDEVINFO)set,
520 *dev, SPDRP_CLASSGUID, (const BYTE *)classGuidStr,
521 lstrlenW(classGuidStr) * sizeof(WCHAR));
522 ret = TRUE;
524 else
526 HeapFree(GetProcessHeap(), 0, devInfo);
527 SetLastError(ERROR_OUTOFMEMORY);
530 return ret;
533 /***********************************************************************
534 * SetupDiBuildClassInfoList (SETUPAPI.@)
536 * Returns a list of setup class GUIDs that identify the classes
537 * that are installed on a local machine.
539 * PARAMS
540 * Flags [I] control exclusion of classes from the list.
541 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
542 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
543 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
545 * RETURNS
546 * Success: TRUE.
547 * Failure: FALSE.
549 BOOL WINAPI SetupDiBuildClassInfoList(
550 DWORD Flags,
551 LPGUID ClassGuidList,
552 DWORD ClassGuidListSize,
553 PDWORD RequiredSize)
555 TRACE("\n");
556 return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
557 ClassGuidListSize, RequiredSize,
558 NULL, NULL);
561 /***********************************************************************
562 * SetupDiBuildClassInfoListExA (SETUPAPI.@)
564 * Returns a list of setup class GUIDs that identify the classes
565 * that are installed on a local or remote macine.
567 * PARAMS
568 * Flags [I] control exclusion of classes from the list.
569 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
570 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
571 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
572 * MachineName [I] name of a remote machine.
573 * Reserved [I] must be NULL.
575 * RETURNS
576 * Success: TRUE.
577 * Failure: FALSE.
579 BOOL WINAPI SetupDiBuildClassInfoListExA(
580 DWORD Flags,
581 LPGUID ClassGuidList,
582 DWORD ClassGuidListSize,
583 PDWORD RequiredSize,
584 LPCSTR MachineName,
585 PVOID Reserved)
587 LPWSTR MachineNameW = NULL;
588 BOOL bResult;
590 TRACE("\n");
592 if (MachineName)
594 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
595 if (MachineNameW == NULL) return FALSE;
598 bResult = SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
599 ClassGuidListSize, RequiredSize,
600 MachineNameW, Reserved);
602 MyFree(MachineNameW);
604 return bResult;
607 /***********************************************************************
608 * SetupDiBuildClassInfoListExW (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 SetupDiBuildClassInfoListExW(
626 DWORD Flags,
627 LPGUID ClassGuidList,
628 DWORD ClassGuidListSize,
629 PDWORD RequiredSize,
630 LPCWSTR MachineName,
631 PVOID Reserved)
633 WCHAR szKeyName[40];
634 HKEY hClassesKey;
635 HKEY hClassKey;
636 DWORD dwLength;
637 DWORD dwIndex;
638 LONG lError;
639 DWORD dwGuidListIndex = 0;
641 TRACE("\n");
643 if (RequiredSize != NULL)
644 *RequiredSize = 0;
646 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
647 KEY_ALL_ACCESS,
648 DIOCR_INSTALLER,
649 MachineName,
650 Reserved);
651 if (hClassesKey == INVALID_HANDLE_VALUE)
653 return FALSE;
656 for (dwIndex = 0; ; dwIndex++)
658 dwLength = 40;
659 lError = RegEnumKeyExW(hClassesKey,
660 dwIndex,
661 szKeyName,
662 &dwLength,
663 NULL,
664 NULL,
665 NULL,
666 NULL);
667 TRACE("RegEnumKeyExW() returns %d\n", lError);
668 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
670 TRACE("Key name: %p\n", szKeyName);
672 if (RegOpenKeyExW(hClassesKey,
673 szKeyName,
675 KEY_ALL_ACCESS,
676 &hClassKey))
678 RegCloseKey(hClassesKey);
679 return FALSE;
682 if (!RegQueryValueExW(hClassKey,
683 NoUseClass,
684 NULL,
685 NULL,
686 NULL,
687 NULL))
689 TRACE("'NoUseClass' value found!\n");
690 RegCloseKey(hClassKey);
691 continue;
694 if ((Flags & DIBCI_NOINSTALLCLASS) &&
695 (!RegQueryValueExW(hClassKey,
696 NoInstallClass,
697 NULL,
698 NULL,
699 NULL,
700 NULL)))
702 TRACE("'NoInstallClass' value found!\n");
703 RegCloseKey(hClassKey);
704 continue;
707 if ((Flags & DIBCI_NODISPLAYCLASS) &&
708 (!RegQueryValueExW(hClassKey,
709 NoDisplayClass,
710 NULL,
711 NULL,
712 NULL,
713 NULL)))
715 TRACE("'NoDisplayClass' value found!\n");
716 RegCloseKey(hClassKey);
717 continue;
720 RegCloseKey(hClassKey);
722 TRACE("Guid: %p\n", szKeyName);
723 if (dwGuidListIndex < ClassGuidListSize)
725 if (szKeyName[0] == '{' && szKeyName[37] == '}')
727 szKeyName[37] = 0;
729 TRACE("Guid: %p\n", &szKeyName[1]);
731 UuidFromStringW(&szKeyName[1],
732 &ClassGuidList[dwGuidListIndex]);
735 dwGuidListIndex++;
738 if (lError != ERROR_SUCCESS)
739 break;
742 RegCloseKey(hClassesKey);
744 if (RequiredSize != NULL)
745 *RequiredSize = dwGuidListIndex;
747 if (ClassGuidListSize < dwGuidListIndex)
749 SetLastError(ERROR_INSUFFICIENT_BUFFER);
750 return FALSE;
753 return TRUE;
756 /***********************************************************************
757 * SetupDiClassGuidsFromNameA (SETUPAPI.@)
759 BOOL WINAPI SetupDiClassGuidsFromNameA(
760 LPCSTR ClassName,
761 LPGUID ClassGuidList,
762 DWORD ClassGuidListSize,
763 PDWORD RequiredSize)
765 return SetupDiClassGuidsFromNameExA(ClassName, ClassGuidList,
766 ClassGuidListSize, RequiredSize,
767 NULL, NULL);
770 /***********************************************************************
771 * SetupDiClassGuidsFromNameW (SETUPAPI.@)
773 BOOL WINAPI SetupDiClassGuidsFromNameW(
774 LPCWSTR ClassName,
775 LPGUID ClassGuidList,
776 DWORD ClassGuidListSize,
777 PDWORD RequiredSize)
779 return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
780 ClassGuidListSize, RequiredSize,
781 NULL, NULL);
784 /***********************************************************************
785 * SetupDiClassGuidsFromNameExA (SETUPAPI.@)
787 BOOL WINAPI SetupDiClassGuidsFromNameExA(
788 LPCSTR ClassName,
789 LPGUID ClassGuidList,
790 DWORD ClassGuidListSize,
791 PDWORD RequiredSize,
792 LPCSTR MachineName,
793 PVOID Reserved)
795 LPWSTR ClassNameW = NULL;
796 LPWSTR MachineNameW = NULL;
797 BOOL bResult;
799 FIXME("\n");
801 ClassNameW = MultiByteToUnicode(ClassName, CP_ACP);
802 if (ClassNameW == NULL)
803 return FALSE;
805 if (MachineName)
807 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
808 if (MachineNameW == NULL)
810 MyFree(ClassNameW);
811 return FALSE;
815 bResult = SetupDiClassGuidsFromNameExW(ClassNameW, ClassGuidList,
816 ClassGuidListSize, RequiredSize,
817 MachineNameW, Reserved);
819 MyFree(MachineNameW);
820 MyFree(ClassNameW);
822 return bResult;
825 /***********************************************************************
826 * SetupDiClassGuidsFromNameExW (SETUPAPI.@)
828 BOOL WINAPI SetupDiClassGuidsFromNameExW(
829 LPCWSTR ClassName,
830 LPGUID ClassGuidList,
831 DWORD ClassGuidListSize,
832 PDWORD RequiredSize,
833 LPCWSTR MachineName,
834 PVOID Reserved)
836 WCHAR szKeyName[40];
837 WCHAR szClassName[256];
838 HKEY hClassesKey;
839 HKEY hClassKey;
840 DWORD dwLength;
841 DWORD dwIndex;
842 LONG lError;
843 DWORD dwGuidListIndex = 0;
845 if (RequiredSize != NULL)
846 *RequiredSize = 0;
848 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
849 KEY_ALL_ACCESS,
850 DIOCR_INSTALLER,
851 MachineName,
852 Reserved);
853 if (hClassesKey == INVALID_HANDLE_VALUE)
855 return FALSE;
858 for (dwIndex = 0; ; dwIndex++)
860 dwLength = 40;
861 lError = RegEnumKeyExW(hClassesKey,
862 dwIndex,
863 szKeyName,
864 &dwLength,
865 NULL,
866 NULL,
867 NULL,
868 NULL);
869 TRACE("RegEnumKeyExW() returns %d\n", lError);
870 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
872 TRACE("Key name: %p\n", szKeyName);
874 if (RegOpenKeyExW(hClassesKey,
875 szKeyName,
877 KEY_ALL_ACCESS,
878 &hClassKey))
880 RegCloseKey(hClassesKey);
881 return FALSE;
884 dwLength = 256 * sizeof(WCHAR);
885 if (!RegQueryValueExW(hClassKey,
886 Class,
887 NULL,
888 NULL,
889 (LPBYTE)szClassName,
890 &dwLength))
892 TRACE("Class name: %p\n", szClassName);
894 if (strcmpiW(szClassName, ClassName) == 0)
896 TRACE("Found matching class name\n");
898 TRACE("Guid: %p\n", szKeyName);
899 if (dwGuidListIndex < ClassGuidListSize)
901 if (szKeyName[0] == '{' && szKeyName[37] == '}')
903 szKeyName[37] = 0;
905 TRACE("Guid: %p\n", &szKeyName[1]);
907 UuidFromStringW(&szKeyName[1],
908 &ClassGuidList[dwGuidListIndex]);
911 dwGuidListIndex++;
915 RegCloseKey(hClassKey);
918 if (lError != ERROR_SUCCESS)
919 break;
922 RegCloseKey(hClassesKey);
924 if (RequiredSize != NULL)
925 *RequiredSize = dwGuidListIndex;
927 if (ClassGuidListSize < dwGuidListIndex)
929 SetLastError(ERROR_INSUFFICIENT_BUFFER);
930 return FALSE;
933 return TRUE;
936 /***********************************************************************
937 * SetupDiClassNameFromGuidA (SETUPAPI.@)
939 BOOL WINAPI SetupDiClassNameFromGuidA(
940 const GUID* ClassGuid,
941 PSTR ClassName,
942 DWORD ClassNameSize,
943 PDWORD RequiredSize)
945 return SetupDiClassNameFromGuidExA(ClassGuid, ClassName,
946 ClassNameSize, RequiredSize,
947 NULL, NULL);
950 /***********************************************************************
951 * SetupDiClassNameFromGuidW (SETUPAPI.@)
953 BOOL WINAPI SetupDiClassNameFromGuidW(
954 const GUID* ClassGuid,
955 PWSTR ClassName,
956 DWORD ClassNameSize,
957 PDWORD RequiredSize)
959 return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
960 ClassNameSize, RequiredSize,
961 NULL, NULL);
964 /***********************************************************************
965 * SetupDiClassNameFromGuidExA (SETUPAPI.@)
967 BOOL WINAPI SetupDiClassNameFromGuidExA(
968 const GUID* ClassGuid,
969 PSTR ClassName,
970 DWORD ClassNameSize,
971 PDWORD RequiredSize,
972 PCSTR MachineName,
973 PVOID Reserved)
975 WCHAR ClassNameW[MAX_CLASS_NAME_LEN];
976 LPWSTR MachineNameW = NULL;
977 BOOL ret;
979 if (MachineName)
980 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
981 ret = SetupDiClassNameFromGuidExW(ClassGuid, ClassNameW, MAX_CLASS_NAME_LEN,
982 NULL, MachineNameW, Reserved);
983 if (ret)
985 int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
986 ClassNameSize, NULL, NULL);
988 if (!ClassNameSize && RequiredSize)
989 *RequiredSize = len;
991 MyFree(MachineNameW);
992 return ret;
995 /***********************************************************************
996 * SetupDiClassNameFromGuidExW (SETUPAPI.@)
998 BOOL WINAPI SetupDiClassNameFromGuidExW(
999 const GUID* ClassGuid,
1000 PWSTR ClassName,
1001 DWORD ClassNameSize,
1002 PDWORD RequiredSize,
1003 PCWSTR MachineName,
1004 PVOID Reserved)
1006 HKEY hKey;
1007 DWORD dwLength;
1009 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1010 KEY_ALL_ACCESS,
1011 DIOCR_INSTALLER,
1012 MachineName,
1013 Reserved);
1014 if (hKey == INVALID_HANDLE_VALUE)
1016 return FALSE;
1019 if (RequiredSize != NULL)
1021 dwLength = 0;
1022 if (RegQueryValueExW(hKey,
1023 Class,
1024 NULL,
1025 NULL,
1026 NULL,
1027 &dwLength))
1029 RegCloseKey(hKey);
1030 return FALSE;
1033 *RequiredSize = dwLength / sizeof(WCHAR);
1036 dwLength = ClassNameSize * sizeof(WCHAR);
1037 if (RegQueryValueExW(hKey,
1038 Class,
1039 NULL,
1040 NULL,
1041 (LPBYTE)ClassName,
1042 &dwLength))
1044 RegCloseKey(hKey);
1045 return FALSE;
1048 RegCloseKey(hKey);
1050 return TRUE;
1053 /***********************************************************************
1054 * SetupDiCreateDeviceInfoList (SETUPAPI.@)
1056 HDEVINFO WINAPI
1057 SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
1058 HWND hwndParent)
1060 return SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent, NULL, NULL);
1063 /***********************************************************************
1064 * SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
1066 HDEVINFO WINAPI
1067 SetupDiCreateDeviceInfoListExA(const GUID *ClassGuid,
1068 HWND hwndParent,
1069 PCSTR MachineName,
1070 PVOID Reserved)
1072 LPWSTR MachineNameW = NULL;
1073 HDEVINFO hDevInfo;
1075 TRACE("\n");
1077 if (MachineName)
1079 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1080 if (MachineNameW == NULL)
1081 return (HDEVINFO)INVALID_HANDLE_VALUE;
1084 hDevInfo = SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent,
1085 MachineNameW, Reserved);
1087 MyFree(MachineNameW);
1089 return hDevInfo;
1092 /***********************************************************************
1093 * SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
1095 * Create an empty DeviceInfoSet list.
1097 * PARAMS
1098 * ClassGuid [I] if not NULL only devices with GUID ClcassGuid are associated
1099 * with this list.
1100 * hwndParent [I] hwnd needed for interface related actions.
1101 * MachineName [I] name of machine to create emtpy DeviceInfoSet list, if NULL
1102 * local registry will be used.
1103 * Reserved [I] must be NULL
1105 * RETURNS
1106 * Success: empty list.
1107 * Failure: INVALID_HANDLE_VALUE.
1109 HDEVINFO WINAPI
1110 SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
1111 HWND hwndParent,
1112 PCWSTR MachineName,
1113 PVOID Reserved)
1115 struct DeviceInfoSet *list = NULL;
1116 DWORD size = sizeof(struct DeviceInfoSet);
1118 TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid), hwndParent,
1119 debugstr_w(MachineName), Reserved);
1121 if (MachineName != NULL)
1123 FIXME("remote support is not implemented\n");
1124 SetLastError(ERROR_INVALID_MACHINENAME);
1125 return (HDEVINFO)INVALID_HANDLE_VALUE;
1128 if (Reserved != NULL)
1130 SetLastError(ERROR_INVALID_PARAMETER);
1131 return (HDEVINFO)INVALID_HANDLE_VALUE;
1134 list = HeapAlloc(GetProcessHeap(), 0, size);
1135 if (!list)
1137 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1138 return (HDEVINFO)INVALID_HANDLE_VALUE;
1141 list->magic = SETUP_DEVICE_INFO_SET_MAGIC;
1142 list->hwndParent = hwndParent;
1143 memcpy(&list->ClassGuid,
1144 ClassGuid ? ClassGuid : &GUID_NULL,
1145 sizeof(list->ClassGuid));
1146 list->cDevices = 0;
1147 list->devices = NULL;
1149 return (HDEVINFO)list;
1152 /***********************************************************************
1153 * SetupDiCreateDeviceInfoA (SETUPAPI.@)
1155 BOOL WINAPI SetupDiCreateDeviceInfoA(
1156 HDEVINFO DeviceInfoSet,
1157 PCSTR DeviceName,
1158 CONST GUID *ClassGuid,
1159 PCSTR DeviceDescription,
1160 HWND hwndParent,
1161 DWORD CreationFlags,
1162 PSP_DEVINFO_DATA DeviceInfoData)
1164 BOOL ret = FALSE;
1165 LPWSTR DeviceNameW = NULL;
1166 LPWSTR DeviceDescriptionW = NULL;
1168 if (DeviceName)
1170 DeviceNameW = MultiByteToUnicode(DeviceName, CP_ACP);
1171 if (DeviceNameW == NULL) return FALSE;
1173 if (DeviceDescription)
1175 DeviceDescriptionW = MultiByteToUnicode(DeviceDescription, CP_ACP);
1176 if (DeviceDescriptionW == NULL)
1178 MyFree(DeviceNameW);
1179 return FALSE;
1183 ret = SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceNameW, ClassGuid, DeviceDescriptionW,
1184 hwndParent, CreationFlags, DeviceInfoData);
1186 MyFree(DeviceNameW);
1187 MyFree(DeviceDescriptionW);
1189 return ret;
1192 static DWORD SETUPDI_DevNameToDevID(LPCWSTR devName)
1194 LPCWSTR ptr;
1195 DWORD devNameLen = lstrlenW(devName), devInst = 0;
1196 BOOL valid = TRUE;
1198 TRACE("%s\n", debugstr_w(devName));
1199 for (ptr = devName; valid && *ptr && ptr - devName < devNameLen; )
1201 if (isdigitW(*ptr))
1203 devInst *= 10;
1204 devInst |= *ptr - '0';
1205 ptr++;
1207 else
1208 valid = FALSE;
1210 TRACE("%d\n", valid ? devInst : 0xffffffff);
1211 return valid ? devInst : 0xffffffff;
1214 /***********************************************************************
1215 * SetupDiCreateDeviceInfoW (SETUPAPI.@)
1217 BOOL WINAPI SetupDiCreateDeviceInfoW(
1218 HDEVINFO DeviceInfoSet,
1219 PCWSTR DeviceName,
1220 CONST GUID *ClassGuid,
1221 PCWSTR DeviceDescription,
1222 HWND hwndParent,
1223 DWORD CreationFlags,
1224 PSP_DEVINFO_DATA DeviceInfoData)
1226 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1227 BOOL ret = FALSE, allocatedInstanceId = FALSE;
1228 LPCWSTR instanceId = NULL;
1230 TRACE("%p %s %s %s %p %x %p\n", DeviceInfoSet, debugstr_w(DeviceName),
1231 debugstr_guid(ClassGuid), debugstr_w(DeviceDescription),
1232 hwndParent, CreationFlags, DeviceInfoData);
1234 if (!DeviceName)
1236 SetLastError(ERROR_INVALID_DEVINST_NAME);
1237 return FALSE;
1239 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
1241 SetLastError(ERROR_INVALID_HANDLE);
1242 return FALSE;
1244 if (!ClassGuid)
1246 SetLastError(ERROR_INVALID_PARAMETER);
1247 return FALSE;
1249 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1251 SetLastError(ERROR_INVALID_HANDLE);
1252 return FALSE;
1254 if (!IsEqualGUID(&set->ClassGuid, &GUID_NULL) &&
1255 !IsEqualGUID(ClassGuid, &set->ClassGuid))
1257 SetLastError(ERROR_CLASS_MISMATCH);
1258 return FALSE;
1260 if ((CreationFlags & DICD_GENERATE_ID))
1262 if (strchrW(DeviceName, '\\'))
1263 SetLastError(ERROR_INVALID_DEVINST_NAME);
1264 else
1266 static const WCHAR newDeviceFmt[] = {'R','O','O','T','\\','%','s',
1267 '\\','%','0','4','d',0};
1268 DWORD devId;
1270 if (set->cDevices)
1272 DWORD i, highestDevID = 0;
1274 for (i = 0; i < set->cDevices; i++)
1276 struct DeviceInfo *devInfo =
1277 (struct DeviceInfo *)set->devices[i].Reserved;
1278 LPCWSTR devName = strrchrW(devInfo->instanceId, '\\');
1279 DWORD id;
1281 if (devName)
1282 devName++;
1283 else
1284 devName = devInfo->instanceId;
1285 id = SETUPDI_DevNameToDevID(devName);
1286 if (id != 0xffffffff && id > highestDevID)
1287 highestDevID = id;
1289 devId = highestDevID + 1;
1291 else
1292 devId = 0;
1293 /* 17 == lstrlenW(L"Root\\") + lstrlenW("\\") + 1 + %d max size */
1294 instanceId = HeapAlloc(GetProcessHeap(), 0,
1295 (17 + lstrlenW(DeviceName)) * sizeof(WCHAR));
1296 if (instanceId)
1298 sprintfW((LPWSTR)instanceId, newDeviceFmt, DeviceName,
1299 devId);
1300 allocatedInstanceId = TRUE;
1301 ret = TRUE;
1303 else
1304 ret = FALSE;
1307 else
1309 DWORD i;
1311 ret = TRUE;
1312 instanceId = DeviceName;
1313 for (i = 0; ret && i < set->cDevices; i++)
1315 struct DeviceInfo *devInfo =
1316 (struct DeviceInfo *)set->devices[i].Reserved;
1318 if (!lstrcmpiW(DeviceName, devInfo->instanceId))
1320 SetLastError(ERROR_DEVINST_ALREADY_EXISTS);
1321 ret = FALSE;
1325 if (ret)
1327 SP_DEVINFO_DATA *dev = NULL;
1329 ret = SETUPDI_AddDeviceToSet(set, ClassGuid, 0 /* FIXME: DevInst */,
1330 instanceId, TRUE, &dev);
1331 if (ret)
1333 if (DeviceDescription)
1334 SetupDiSetDeviceRegistryPropertyW(DeviceInfoSet,
1335 dev, SPDRP_DEVICEDESC, (const BYTE *)DeviceDescription,
1336 lstrlenW(DeviceDescription) * sizeof(WCHAR));
1337 if (DeviceInfoData)
1339 if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
1341 SetLastError(ERROR_INVALID_USER_BUFFER);
1342 ret = FALSE;
1344 else
1345 memcpy(DeviceInfoData, dev, sizeof(SP_DEVINFO_DATA));
1349 if (allocatedInstanceId)
1350 HeapFree(GetProcessHeap(), 0, (LPWSTR)instanceId);
1352 return ret;
1355 /***********************************************************************
1356 * SetupDiRegisterDeviceInfo (SETUPAPI.@)
1358 BOOL WINAPI SetupDiRegisterDeviceInfo(
1359 HDEVINFO DeviceInfoSet,
1360 PSP_DEVINFO_DATA DeviceInfoData,
1361 DWORD Flags,
1362 PSP_DETSIG_CMPPROC CompareProc,
1363 PVOID CompareContext,
1364 PSP_DEVINFO_DATA DupDeviceInfoData)
1366 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1367 struct DeviceInfo *devInfo;
1369 TRACE("%p %p %08x %p %p %p\n", DeviceInfoSet, DeviceInfoData, Flags,
1370 CompareProc, CompareContext, DupDeviceInfoData);
1372 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
1374 SetLastError(ERROR_INVALID_HANDLE);
1375 return FALSE;
1377 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1379 SetLastError(ERROR_INVALID_HANDLE);
1380 return FALSE;
1382 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1383 || !DeviceInfoData->Reserved)
1385 SetLastError(ERROR_INVALID_PARAMETER);
1386 return FALSE;
1388 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1389 if (devInfo->set != set)
1391 SetLastError(ERROR_INVALID_PARAMETER);
1392 return FALSE;
1394 if (devInfo->phantom)
1396 devInfo->phantom = FALSE;
1397 RegDeleteValueW(devInfo->key, Phantom);
1399 return TRUE;
1402 /***********************************************************************
1403 * SetupDiEnumDeviceInfo (SETUPAPI.@)
1405 BOOL WINAPI SetupDiEnumDeviceInfo(
1406 HDEVINFO devinfo,
1407 DWORD index,
1408 PSP_DEVINFO_DATA info)
1410 BOOL ret = FALSE;
1412 TRACE("%p %d %p\n", devinfo, index, info);
1414 if(info==NULL)
1416 SetLastError(ERROR_INVALID_PARAMETER);
1417 return FALSE;
1419 if (devinfo && devinfo != (HDEVINFO)INVALID_HANDLE_VALUE)
1421 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
1422 if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
1424 if (index < list->cDevices)
1426 if (info->cbSize == sizeof(SP_DEVINFO_DATA))
1428 memcpy(info, &list->devices[index], info->cbSize);
1429 ret = TRUE;
1431 else
1432 SetLastError(ERROR_INVALID_USER_BUFFER);
1434 else
1435 SetLastError(ERROR_NO_MORE_ITEMS);
1437 else
1438 SetLastError(ERROR_INVALID_HANDLE);
1440 else
1441 SetLastError(ERROR_INVALID_HANDLE);
1442 return ret;
1445 /***********************************************************************
1446 * SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
1448 BOOL WINAPI SetupDiGetDeviceInstanceIdA(
1449 HDEVINFO DeviceInfoSet,
1450 PSP_DEVINFO_DATA DeviceInfoData,
1451 PSTR DeviceInstanceId,
1452 DWORD DeviceInstanceIdSize,
1453 PDWORD RequiredSize)
1455 BOOL ret = FALSE;
1456 DWORD size;
1457 PWSTR instanceId;
1459 TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
1460 DeviceInstanceIdSize, RequiredSize);
1462 SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
1463 DeviceInfoData,
1464 NULL,
1466 &size);
1467 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1468 return FALSE;
1469 instanceId = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1470 if (instanceId)
1472 ret = SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
1473 DeviceInfoData,
1474 instanceId,
1475 size,
1476 &size);
1477 if (ret)
1479 int len = WideCharToMultiByte(CP_ACP, 0, instanceId, -1,
1480 DeviceInstanceId,
1481 DeviceInstanceIdSize, NULL, NULL);
1483 if (!len)
1484 ret = FALSE;
1485 else
1487 if (len > DeviceInstanceIdSize)
1489 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1490 ret = FALSE;
1492 if (RequiredSize)
1493 *RequiredSize = len;
1496 HeapFree(GetProcessHeap(), 0, instanceId);
1498 return ret;
1501 /***********************************************************************
1502 * SetupDiGetDeviceInstanceIdW (SETUPAPI.@)
1504 BOOL WINAPI SetupDiGetDeviceInstanceIdW(
1505 HDEVINFO DeviceInfoSet,
1506 PSP_DEVINFO_DATA DeviceInfoData,
1507 PWSTR DeviceInstanceId,
1508 DWORD DeviceInstanceIdSize,
1509 PDWORD RequiredSize)
1511 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1512 struct DeviceInfo *devInfo;
1514 TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
1515 DeviceInstanceIdSize, RequiredSize);
1517 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1519 SetLastError(ERROR_INVALID_HANDLE);
1520 return FALSE;
1522 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1524 SetLastError(ERROR_INVALID_HANDLE);
1525 return FALSE;
1527 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1528 || !DeviceInfoData->Reserved)
1530 SetLastError(ERROR_INVALID_PARAMETER);
1531 return FALSE;
1533 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1534 if (devInfo->set != set)
1536 SetLastError(ERROR_INVALID_PARAMETER);
1537 return FALSE;
1539 TRACE("instance ID: %s\n", debugstr_w(devInfo->instanceId));
1540 if (DeviceInstanceIdSize < lstrlenW(devInfo->instanceId) + 1)
1542 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1543 if (RequiredSize)
1544 *RequiredSize = lstrlenW(devInfo->instanceId) + 1;
1545 return FALSE;
1547 lstrcpyW(DeviceInstanceId, devInfo->instanceId);
1548 if (RequiredSize)
1549 *RequiredSize = lstrlenW(devInfo->instanceId) + 1;
1550 return TRUE;
1553 /***********************************************************************
1554 * SetupDiGetActualSectionToInstallA (SETUPAPI.@)
1556 BOOL WINAPI SetupDiGetActualSectionToInstallA(
1557 HINF InfHandle,
1558 PCSTR InfSectionName,
1559 PSTR InfSectionWithExt,
1560 DWORD InfSectionWithExtSize,
1561 PDWORD RequiredSize,
1562 PSTR *Extension)
1564 FIXME("\n");
1565 return FALSE;
1568 /***********************************************************************
1569 * SetupDiGetActualSectionToInstallW (SETUPAPI.@)
1571 BOOL WINAPI SetupDiGetActualSectionToInstallW(
1572 HINF InfHandle,
1573 PCWSTR InfSectionName,
1574 PWSTR InfSectionWithExt,
1575 DWORD InfSectionWithExtSize,
1576 PDWORD RequiredSize,
1577 PWSTR *Extension)
1579 WCHAR szBuffer[MAX_PATH];
1580 DWORD dwLength;
1581 DWORD dwFullLength;
1582 LONG lLineCount = -1;
1584 lstrcpyW(szBuffer, InfSectionName);
1585 dwLength = lstrlenW(szBuffer);
1587 if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
1589 /* Test section name with '.NTx86' extension */
1590 lstrcpyW(&szBuffer[dwLength], NtPlatformExtension);
1591 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1593 if (lLineCount == -1)
1595 /* Test section name with '.NT' extension */
1596 lstrcpyW(&szBuffer[dwLength], NtExtension);
1597 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1600 else
1602 /* Test section name with '.Win' extension */
1603 lstrcpyW(&szBuffer[dwLength], WinExtension);
1604 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1607 if (lLineCount == -1)
1609 /* Test section name without extension */
1610 szBuffer[dwLength] = 0;
1611 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1614 if (lLineCount == -1)
1616 SetLastError(ERROR_INVALID_PARAMETER);
1617 return FALSE;
1620 dwFullLength = lstrlenW(szBuffer);
1622 if (InfSectionWithExt != NULL && InfSectionWithExtSize != 0)
1624 if (InfSectionWithExtSize < (dwFullLength + 1))
1626 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1627 return FALSE;
1630 lstrcpyW(InfSectionWithExt, szBuffer);
1631 if (Extension != NULL)
1633 *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
1637 if (RequiredSize != NULL)
1639 *RequiredSize = dwFullLength + 1;
1642 return TRUE;
1645 /***********************************************************************
1646 * SetupDiGetClassDescriptionA (SETUPAPI.@)
1648 BOOL WINAPI SetupDiGetClassDescriptionA(
1649 const GUID* ClassGuid,
1650 PSTR ClassDescription,
1651 DWORD ClassDescriptionSize,
1652 PDWORD RequiredSize)
1654 return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
1655 ClassDescriptionSize,
1656 RequiredSize, NULL, NULL);
1659 /***********************************************************************
1660 * SetupDiGetClassDescriptionW (SETUPAPI.@)
1662 BOOL WINAPI SetupDiGetClassDescriptionW(
1663 const GUID* ClassGuid,
1664 PWSTR ClassDescription,
1665 DWORD ClassDescriptionSize,
1666 PDWORD RequiredSize)
1668 return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
1669 ClassDescriptionSize,
1670 RequiredSize, NULL, NULL);
1673 /***********************************************************************
1674 * SetupDiGetClassDescriptionExA (SETUPAPI.@)
1676 BOOL WINAPI SetupDiGetClassDescriptionExA(
1677 const GUID* ClassGuid,
1678 PSTR ClassDescription,
1679 DWORD ClassDescriptionSize,
1680 PDWORD RequiredSize,
1681 PCSTR MachineName,
1682 PVOID Reserved)
1684 HKEY hKey;
1685 DWORD dwLength;
1686 BOOL ret;
1688 hKey = SetupDiOpenClassRegKeyExA(ClassGuid,
1689 KEY_ALL_ACCESS,
1690 DIOCR_INSTALLER,
1691 MachineName,
1692 Reserved);
1693 if (hKey == INVALID_HANDLE_VALUE)
1695 WARN("SetupDiOpenClassRegKeyExA() failed (Error %u)\n", GetLastError());
1696 return FALSE;
1699 dwLength = ClassDescriptionSize;
1700 ret = !RegQueryValueExA( hKey, NULL, NULL, NULL,
1701 (LPBYTE)ClassDescription, &dwLength );
1702 if (RequiredSize) *RequiredSize = dwLength;
1703 RegCloseKey(hKey);
1704 return ret;
1707 /***********************************************************************
1708 * SetupDiGetClassDescriptionExW (SETUPAPI.@)
1710 BOOL WINAPI SetupDiGetClassDescriptionExW(
1711 const GUID* ClassGuid,
1712 PWSTR ClassDescription,
1713 DWORD ClassDescriptionSize,
1714 PDWORD RequiredSize,
1715 PCWSTR MachineName,
1716 PVOID Reserved)
1718 HKEY hKey;
1719 DWORD dwLength;
1720 BOOL ret;
1722 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1723 KEY_ALL_ACCESS,
1724 DIOCR_INSTALLER,
1725 MachineName,
1726 Reserved);
1727 if (hKey == INVALID_HANDLE_VALUE)
1729 WARN("SetupDiOpenClassRegKeyExW() failed (Error %u)\n", GetLastError());
1730 return FALSE;
1733 dwLength = ClassDescriptionSize * sizeof(WCHAR);
1734 ret = !RegQueryValueExW( hKey, NULL, NULL, NULL,
1735 (LPBYTE)ClassDescription, &dwLength );
1736 if (RequiredSize) *RequiredSize = dwLength / sizeof(WCHAR);
1737 RegCloseKey(hKey);
1738 return ret;
1741 /***********************************************************************
1742 * SetupDiGetClassDevsA (SETUPAPI.@)
1744 HDEVINFO WINAPI SetupDiGetClassDevsA(
1745 CONST GUID *class,
1746 LPCSTR enumstr,
1747 HWND parent,
1748 DWORD flags)
1750 HDEVINFO ret;
1751 LPWSTR enumstrW = NULL;
1753 if (enumstr)
1755 int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1756 enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1757 if (!enumstrW)
1759 ret = (HDEVINFO)INVALID_HANDLE_VALUE;
1760 goto end;
1762 MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1764 ret = SetupDiGetClassDevsW(class, enumstrW, parent, flags);
1765 HeapFree(GetProcessHeap(), 0, enumstrW);
1767 end:
1768 return ret;
1771 static void SETUPDI_AddDeviceInterfaces(SP_DEVINFO_DATA *dev, HKEY key,
1772 const GUID *interface)
1774 DWORD i, len;
1775 WCHAR subKeyName[MAX_PATH];
1776 LONG l = ERROR_SUCCESS;
1778 for (i = 0; !l; i++)
1780 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1781 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
1782 if (!l)
1784 HKEY subKey;
1785 SP_DEVICE_INTERFACE_DATA *iface = NULL;
1787 /* The subkey name is the reference string, with a '#' prepended */
1788 SETUPDI_AddInterfaceInstance(dev, interface, subKeyName + 1,
1789 &iface);
1790 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
1791 if (!l)
1793 WCHAR symbolicLink[MAX_PATH];
1794 DWORD dataType;
1796 len = sizeof(symbolicLink);
1797 l = RegQueryValueExW(subKey, SymbolicLink, NULL, &dataType,
1798 (BYTE *)symbolicLink, &len);
1799 if (!l && dataType == REG_SZ)
1800 SETUPDI_SetInterfaceSymbolicLink(iface, symbolicLink);
1801 RegCloseKey(subKey);
1805 /* FIXME: find and add all the device's interfaces to the device */
1808 static void SETUPDI_EnumerateMatchingInterfaces(HDEVINFO DeviceInfoSet,
1809 HKEY key, const GUID *interface, LPCWSTR enumstr)
1811 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1812 DWORD i, len;
1813 WCHAR subKeyName[MAX_PATH];
1814 LONG l;
1815 HKEY enumKey = INVALID_HANDLE_VALUE;
1817 TRACE("%s\n", debugstr_w(enumstr));
1819 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_READ, NULL,
1820 &enumKey, NULL);
1821 for (i = 0; !l; i++)
1823 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1824 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
1825 if (!l)
1827 HKEY subKey;
1829 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
1830 if (!l)
1832 WCHAR deviceInst[MAX_PATH * 3];
1833 DWORD dataType;
1835 len = sizeof(deviceInst);
1836 l = RegQueryValueExW(subKey, DeviceInstance, NULL, &dataType,
1837 (BYTE *)deviceInst, &len);
1838 if (!l && dataType == REG_SZ)
1840 TRACE("found instance ID %s\n", debugstr_w(deviceInst));
1841 if (!enumstr || !lstrcmpiW(enumstr, deviceInst))
1843 HKEY deviceKey;
1845 l = RegOpenKeyExW(enumKey, deviceInst, 0, KEY_READ,
1846 &deviceKey);
1847 if (!l)
1849 WCHAR deviceClassStr[40];
1851 len = sizeof(deviceClassStr);
1852 l = RegQueryValueExW(deviceKey, ClassGUID, NULL,
1853 &dataType, (BYTE *)deviceClassStr, &len);
1854 if (!l && dataType == REG_SZ &&
1855 deviceClassStr[0] == '{' &&
1856 deviceClassStr[37] == '}')
1858 GUID deviceClass;
1859 SP_DEVINFO_DATA *dev;
1861 deviceClassStr[37] = 0;
1862 UuidFromStringW(&deviceClassStr[1],
1863 &deviceClass);
1864 if (SETUPDI_AddDeviceToSet(set, &deviceClass,
1865 0 /* FIXME: DevInst */, deviceInst,
1866 FALSE, &dev))
1867 SETUPDI_AddDeviceInterfaces(dev, subKey,
1868 interface);
1870 RegCloseKey(deviceKey);
1874 RegCloseKey(subKey);
1878 if (enumKey != INVALID_HANDLE_VALUE)
1879 RegCloseKey(enumKey);
1882 static void SETUPDI_EnumerateInterfaces(HDEVINFO DeviceInfoSet,
1883 const GUID *interface, LPCWSTR enumstr, DWORD flags)
1885 HKEY interfacesKey = SetupDiOpenClassRegKeyExW(interface, KEY_READ,
1886 DIOCR_INTERFACE, NULL, NULL);
1888 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(interface),
1889 debugstr_w(enumstr), flags);
1891 if (interfacesKey != INVALID_HANDLE_VALUE)
1893 if (flags & DIGCF_ALLCLASSES)
1895 DWORD i, len;
1896 WCHAR interfaceGuidStr[40];
1897 LONG l = ERROR_SUCCESS;
1899 for (i = 0; !l; i++)
1901 len = sizeof(interfaceGuidStr) / sizeof(interfaceGuidStr[0]);
1902 l = RegEnumKeyExW(interfacesKey, i, interfaceGuidStr, &len,
1903 NULL, NULL, NULL, NULL);
1904 if (!l)
1906 if (interfaceGuidStr[0] == '{' &&
1907 interfaceGuidStr[37] == '}')
1909 HKEY interfaceKey;
1910 GUID interfaceGuid;
1912 interfaceGuidStr[37] = 0;
1913 UuidFromStringW(&interfaceGuidStr[1], &interfaceGuid);
1914 l = RegOpenKeyExW(interfacesKey, interfaceGuidStr, 0,
1915 KEY_READ, &interfaceKey);
1916 if (!l)
1918 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
1919 interfaceKey, &interfaceGuid, enumstr);
1920 RegCloseKey(interfaceKey);
1926 else
1928 /* In this case, SetupDiOpenClassRegKeyExW opened the specific
1929 * interface's key, so just pass that long
1931 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
1932 interfacesKey, interface, enumstr);
1934 RegCloseKey(interfacesKey);
1938 static void SETUPDI_EnumerateMatchingDevices(HDEVINFO DeviceInfoSet,
1939 LPCWSTR parent, HKEY key, const GUID *class, DWORD flags)
1941 struct DeviceInfoSet *set = (struct DeviceInfoSet *)set;
1942 DWORD i, len;
1943 WCHAR subKeyName[MAX_PATH];
1944 LONG l = ERROR_SUCCESS;
1946 TRACE("%s\n", debugstr_w(parent));
1948 for (i = 0; !l; i++)
1950 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1951 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
1952 if (!l)
1954 HKEY subKey;
1956 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
1957 if (!l)
1959 WCHAR classGuid[40];
1960 DWORD dataType;
1962 len = sizeof(classGuid);
1963 l = RegQueryValueExW(subKey, ClassGUID, NULL, &dataType,
1964 (BYTE *)classGuid, &len);
1965 if (!l && dataType == REG_SZ)
1967 if (classGuid[0] == '{' && classGuid[37] == '}')
1969 GUID deviceClass;
1971 classGuid[37] = 0;
1972 UuidFromStringW(&classGuid[1], &deviceClass);
1973 if ((flags & DIGCF_ALLCLASSES) ||
1974 IsEqualGUID(class, &deviceClass))
1976 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
1977 LPWSTR instanceId;
1979 instanceId = HeapAlloc(GetProcessHeap(), 0,
1980 (lstrlenW(parent) + lstrlenW(subKeyName) + 2)
1981 * sizeof(WCHAR));
1982 if (instanceId)
1984 SP_DEVINFO_DATA *dev;
1986 sprintfW(instanceId, fmt, parent, subKeyName);
1987 SETUPDI_AddDeviceToSet(set, &deviceClass,
1988 0 /* FIXME: DevInst */, instanceId,
1989 FALSE, &dev);
1990 HeapFree(GetProcessHeap(), 0, instanceId);
1995 RegCloseKey(subKey);
2001 static void SETUPDI_EnumerateDevices(HDEVINFO DeviceInfoSet, const GUID *class,
2002 LPCWSTR enumstr, DWORD flags)
2004 HKEY classesKey = SetupDiOpenClassRegKeyExW(class, KEY_READ,
2005 DIOCR_INSTALLER, NULL, NULL);
2007 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(class),
2008 debugstr_w(enumstr), flags);
2010 if (classesKey != INVALID_HANDLE_VALUE)
2012 if (enumstr)
2014 HKEY enumKey;
2015 LONG l = RegOpenKeyExW(classesKey, enumstr, 0, KEY_READ,
2016 &enumKey);
2018 if (!l)
2020 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet, enumstr,
2021 enumKey, class, flags);
2022 RegCloseKey(enumKey);
2025 else
2027 DWORD i, len;
2028 WCHAR subKeyName[MAX_PATH];
2029 LONG l = ERROR_SUCCESS;
2031 for (i = 0; !l; i++)
2033 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2034 l = RegEnumKeyExW(classesKey, i, subKeyName, &len, NULL,
2035 NULL, NULL, NULL);
2036 if (!l)
2038 HKEY subKey;
2040 l = RegOpenKeyExW(classesKey, subKeyName, 0, KEY_READ,
2041 &subKey);
2042 if (!l)
2044 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet,
2045 subKeyName, subKey, class, flags);
2046 RegCloseKey(subKey);
2051 RegCloseKey(classesKey);
2055 /***********************************************************************
2056 * SetupDiGetClassDevsW (SETUPAPI.@)
2058 HDEVINFO WINAPI SetupDiGetClassDevsW(
2059 CONST GUID *class,
2060 LPCWSTR enumstr,
2061 HWND parent,
2062 DWORD flags)
2064 static const DWORD unsupportedFlags = DIGCF_DEFAULT | DIGCF_PRESENT |
2065 DIGCF_PROFILE;
2066 HDEVINFO set;
2068 TRACE("%s %s %p 0x%08x\n", debugstr_guid(class), debugstr_w(enumstr), parent, flags);
2070 if (!(flags & DIGCF_ALLCLASSES) && !class)
2072 SetLastError(ERROR_INVALID_PARAMETER);
2073 return NULL;
2075 if (flags & unsupportedFlags)
2076 WARN("unsupported flags %08x\n", flags & unsupportedFlags);
2077 /* WinXP always succeeds, returns empty list for unknown classes */
2078 set = SetupDiCreateDeviceInfoList(class, parent);
2079 if (set)
2081 if (flags & DIGCF_DEVICEINTERFACE)
2082 SETUPDI_EnumerateInterfaces(set, class, enumstr, flags);
2083 else
2084 SETUPDI_EnumerateDevices(set, class, enumstr, flags);
2086 return set;
2089 /***********************************************************************
2090 * SetupDiGetClassDevsExW (SETUPAPI.@)
2092 HDEVINFO WINAPI SetupDiGetClassDevsExW(
2093 CONST GUID *class,
2094 PCWSTR enumstr,
2095 HWND parent,
2096 DWORD flags,
2097 HDEVINFO deviceset,
2098 PCWSTR machine,
2099 PVOID reserved)
2101 static const DWORD unsupportedFlags = DIGCF_DEFAULT | DIGCF_PRESENT |
2102 DIGCF_PROFILE;
2103 HDEVINFO set;
2105 TRACE("%s %s %p 0x%08x %p %s %p\n", debugstr_guid(class),
2106 debugstr_w(enumstr), parent, flags, deviceset, debugstr_w(machine),
2107 reserved);
2109 if (!(flags & DIGCF_ALLCLASSES) && !class)
2111 SetLastError(ERROR_INVALID_PARAMETER);
2112 return NULL;
2114 if (flags & unsupportedFlags)
2115 WARN("unsupported flags %08x\n", flags & unsupportedFlags);
2116 if (deviceset)
2117 set = deviceset;
2118 else
2119 set = SetupDiCreateDeviceInfoListExW(class, parent, machine, reserved);
2120 if (set)
2122 if (machine)
2123 FIXME("%s: unimplemented for remote machines\n",
2124 debugstr_w(machine));
2125 else if (flags & DIGCF_DEVICEINTERFACE)
2126 SETUPDI_EnumerateInterfaces(set, class, enumstr, flags);
2127 else
2128 SETUPDI_EnumerateDevices(set, class, enumstr, flags);
2130 return set;
2133 /***********************************************************************
2134 * SetupDiGetDeviceInfoListDetailA (SETUPAPI.@)
2136 BOOL WINAPI SetupDiGetDeviceInfoListDetailA(
2137 HDEVINFO DeviceInfoSet,
2138 PSP_DEVINFO_LIST_DETAIL_DATA_A DevInfoData )
2140 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2142 TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2144 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2146 SetLastError(ERROR_INVALID_HANDLE);
2147 return FALSE;
2149 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2151 SetLastError(ERROR_INVALID_HANDLE);
2152 return FALSE;
2154 if (!DevInfoData ||
2155 DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_A))
2157 SetLastError(ERROR_INVALID_PARAMETER);
2158 return FALSE;
2160 memcpy(&DevInfoData->ClassGuid, &set->ClassGuid, sizeof(GUID));
2161 DevInfoData->RemoteMachineHandle = NULL;
2162 DevInfoData->RemoteMachineName[0] = '\0';
2163 return TRUE;
2166 /***********************************************************************
2167 * SetupDiGetDeviceInfoListDetailW (SETUPAPI.@)
2169 BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
2170 HDEVINFO DeviceInfoSet,
2171 PSP_DEVINFO_LIST_DETAIL_DATA_W DevInfoData )
2173 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2175 TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2177 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2179 SetLastError(ERROR_INVALID_HANDLE);
2180 return FALSE;
2182 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2184 SetLastError(ERROR_INVALID_HANDLE);
2185 return FALSE;
2187 if (!DevInfoData ||
2188 DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W))
2190 SetLastError(ERROR_INVALID_PARAMETER);
2191 return FALSE;
2193 memcpy(&DevInfoData->ClassGuid, &set->ClassGuid, sizeof(GUID));
2194 DevInfoData->RemoteMachineHandle = NULL;
2195 DevInfoData->RemoteMachineName[0] = '\0';
2196 return TRUE;
2199 /***********************************************************************
2200 * SetupDiCreateDeviceInterfaceA (SETUPAPI.@)
2202 BOOL WINAPI SetupDiCreateDeviceInterfaceA(
2203 HDEVINFO DeviceInfoSet,
2204 PSP_DEVINFO_DATA DeviceInfoData,
2205 const GUID *InterfaceClassGuid,
2206 PCSTR ReferenceString,
2207 DWORD CreationFlags,
2208 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2210 BOOL ret;
2211 LPWSTR ReferenceStringW = NULL;
2213 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2214 debugstr_guid(InterfaceClassGuid), debugstr_a(ReferenceString),
2215 CreationFlags, DeviceInterfaceData);
2217 if (ReferenceString)
2219 ReferenceStringW = MultiByteToUnicode(ReferenceString, CP_ACP);
2220 if (ReferenceStringW == NULL) return FALSE;
2223 ret = SetupDiCreateDeviceInterfaceW(DeviceInfoSet, DeviceInfoData,
2224 InterfaceClassGuid, ReferenceStringW, CreationFlags,
2225 DeviceInterfaceData);
2227 MyFree(ReferenceStringW);
2229 return ret;
2232 /***********************************************************************
2233 * SetupDiCreateDeviceInterfaceW (SETUPAPI.@)
2235 BOOL WINAPI SetupDiCreateDeviceInterfaceW(
2236 HDEVINFO DeviceInfoSet,
2237 PSP_DEVINFO_DATA DeviceInfoData,
2238 const GUID *InterfaceClassGuid,
2239 PCWSTR ReferenceString,
2240 DWORD CreationFlags,
2241 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2243 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2244 struct DeviceInfo *devInfo;
2245 SP_DEVICE_INTERFACE_DATA *iface = NULL;
2246 BOOL ret;
2248 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2249 debugstr_guid(InterfaceClassGuid), debugstr_w(ReferenceString),
2250 CreationFlags, DeviceInterfaceData);
2252 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2254 SetLastError(ERROR_INVALID_HANDLE);
2255 return FALSE;
2257 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2259 SetLastError(ERROR_INVALID_HANDLE);
2260 return FALSE;
2262 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2263 || !DeviceInfoData->Reserved)
2265 SetLastError(ERROR_INVALID_PARAMETER);
2266 return FALSE;
2268 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2269 if (devInfo->set != set)
2271 SetLastError(ERROR_INVALID_PARAMETER);
2272 return FALSE;
2274 if (!InterfaceClassGuid)
2276 SetLastError(ERROR_INVALID_USER_BUFFER);
2277 return FALSE;
2279 if ((ret = SETUPDI_AddInterfaceInstance(DeviceInfoData, InterfaceClassGuid,
2280 ReferenceString, &iface)))
2282 if (DeviceInterfaceData)
2284 if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2286 SetLastError(ERROR_INVALID_USER_BUFFER);
2287 ret = FALSE;
2289 else
2290 memcpy(DeviceInterfaceData, iface, sizeof(*iface));
2293 return ret;
2296 /***********************************************************************
2297 * SetupDiCreateDeviceInterfaceRegKeyA (SETUPAPI.@)
2299 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyA(
2300 HDEVINFO DeviceInfoSet,
2301 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2302 DWORD Reserved,
2303 REGSAM samDesired,
2304 HINF InfHandle,
2305 PCSTR InfSectionName)
2307 HKEY key;
2308 PWSTR InfSectionNameW = NULL;
2310 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2311 samDesired, InfHandle, InfSectionName);
2312 if (InfHandle)
2314 if (!InfSectionName)
2316 SetLastError(ERROR_INVALID_PARAMETER);
2317 return INVALID_HANDLE_VALUE;
2319 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
2320 if (!InfSectionNameW)
2321 return INVALID_HANDLE_VALUE;
2323 key = SetupDiCreateDeviceInterfaceRegKeyW(DeviceInfoSet,
2324 DeviceInterfaceData, Reserved, samDesired, InfHandle,
2325 InfSectionNameW);
2326 MyFree(InfSectionNameW);
2327 return key;
2330 static PWSTR SETUPDI_GetInstancePath(struct InterfaceInfo *ifaceInfo)
2332 static const WCHAR hash[] = {'#',0};
2333 PWSTR instancePath = NULL;
2335 if (ifaceInfo->referenceString)
2337 instancePath = HeapAlloc(GetProcessHeap(), 0,
2338 (lstrlenW(ifaceInfo->referenceString) + 2) * sizeof(WCHAR));
2339 if (instancePath)
2341 lstrcpyW(instancePath, hash);
2342 lstrcatW(instancePath, ifaceInfo->referenceString);
2344 else
2345 SetLastError(ERROR_OUTOFMEMORY);
2347 else
2349 instancePath = HeapAlloc(GetProcessHeap(), 0,
2350 (lstrlenW(hash) + 1) * sizeof(WCHAR));
2351 if (instancePath)
2352 lstrcpyW(instancePath, hash);
2354 return instancePath;
2357 /***********************************************************************
2358 * SetupDiCreateDeviceInterfaceRegKeyW (SETUPAPI.@)
2360 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyW(
2361 HDEVINFO DeviceInfoSet,
2362 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2363 DWORD Reserved,
2364 REGSAM samDesired,
2365 HINF InfHandle,
2366 PCWSTR InfSectionName)
2368 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2369 HKEY key = INVALID_HANDLE_VALUE, interfacesKey;
2370 LONG l;
2372 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2373 samDesired, InfHandle, InfSectionName);
2375 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2376 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2378 SetLastError(ERROR_INVALID_HANDLE);
2379 return INVALID_HANDLE_VALUE;
2381 if (!DeviceInterfaceData ||
2382 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2383 !DeviceInterfaceData->Reserved)
2385 SetLastError(ERROR_INVALID_PARAMETER);
2386 return INVALID_HANDLE_VALUE;
2388 if (InfHandle && !InfSectionName)
2390 SetLastError(ERROR_INVALID_PARAMETER);
2391 return INVALID_HANDLE_VALUE;
2393 if (!(l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, DeviceClasses, 0, NULL, 0,
2394 samDesired, NULL, &interfacesKey, NULL)))
2396 HKEY parent;
2397 WCHAR bracedGuidString[39];
2399 SETUPDI_GuidToString(&DeviceInterfaceData->InterfaceClassGuid,
2400 bracedGuidString);
2401 if (!(l = RegCreateKeyExW(interfacesKey, bracedGuidString, 0, NULL, 0,
2402 samDesired, NULL, &parent, NULL)))
2404 struct InterfaceInfo *ifaceInfo =
2405 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2406 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2408 if (instancePath)
2410 LONG l;
2412 l = RegCreateKeyExW(parent, instancePath, 0, NULL, 0,
2413 samDesired, NULL, &key, NULL);
2414 if (l)
2416 SetLastError(l);
2417 key = INVALID_HANDLE_VALUE;
2419 else if (InfHandle)
2420 FIXME("INF section installation unsupported\n");
2422 HeapFree(GetProcessHeap(), 0, instancePath);
2423 RegCloseKey(parent);
2425 else
2426 SetLastError(l);
2427 RegCloseKey(interfacesKey);
2429 else
2430 SetLastError(l);
2431 return key;
2434 /***********************************************************************
2435 * SetupDiDeleteDeviceInterfaceRegKey (SETUPAPI.@)
2437 BOOL WINAPI SetupDiDeleteDeviceInterfaceRegKey(
2438 HDEVINFO DeviceInfoSet,
2439 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2440 DWORD Reserved)
2442 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2443 HKEY parent;
2444 BOOL ret = FALSE;
2446 TRACE("%p %p %d\n", DeviceInfoSet, DeviceInterfaceData, Reserved);
2448 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2449 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2451 SetLastError(ERROR_INVALID_HANDLE);
2452 return FALSE;
2454 if (!DeviceInterfaceData ||
2455 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2456 !DeviceInterfaceData->Reserved)
2458 SetLastError(ERROR_INVALID_PARAMETER);
2459 return FALSE;
2461 parent = SetupDiOpenClassRegKeyExW(&DeviceInterfaceData->InterfaceClassGuid,
2462 KEY_ALL_ACCESS, DIOCR_INTERFACE, NULL, NULL);
2463 if (parent != INVALID_HANDLE_VALUE)
2465 struct InterfaceInfo *ifaceInfo =
2466 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2467 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2469 if (instancePath)
2471 LONG l = RegDeleteKeyW(parent, instancePath);
2473 if (l)
2474 SetLastError(l);
2475 else
2476 ret = TRUE;
2477 HeapFree(GetProcessHeap(), 0, instancePath);
2479 RegCloseKey(parent);
2481 return ret;
2484 /***********************************************************************
2485 * SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2487 * PARAMS
2488 * DeviceInfoSet [I] Set of devices from which to enumerate
2489 * interfaces
2490 * DeviceInfoData [I] (Optional) If specified, a specific device
2491 * instance from which to enumerate interfaces.
2492 * If it isn't specified, all interfaces for all
2493 * devices in the set are enumerated.
2494 * InterfaceClassGuid [I] The interface class to enumerate.
2495 * MemberIndex [I] An index of the interface instance to enumerate.
2496 * A caller should start with MemberIndex set to 0,
2497 * and continue until the function fails with
2498 * ERROR_NO_MORE_ITEMS.
2499 * DeviceInterfaceData [I/O] Returns an enumerated interface. Its cbSize
2500 * member must be set to
2501 * sizeof(SP_DEVICE_INTERFACE_DATA).
2503 * RETURNS
2504 * Success: non-zero value.
2505 * Failure: FALSE. Call GetLastError() for more info.
2507 BOOL WINAPI SetupDiEnumDeviceInterfaces(
2508 HDEVINFO DeviceInfoSet,
2509 PSP_DEVINFO_DATA DeviceInfoData,
2510 CONST GUID * InterfaceClassGuid,
2511 DWORD MemberIndex,
2512 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2514 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2515 BOOL ret = FALSE;
2517 TRACE("%p, %p, %s, %d, %p\n", DeviceInfoSet, DeviceInfoData,
2518 debugstr_guid(InterfaceClassGuid), MemberIndex, DeviceInterfaceData);
2520 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2521 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2523 SetLastError(ERROR_INVALID_HANDLE);
2524 return FALSE;
2526 if (DeviceInfoData && (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA) ||
2527 !DeviceInfoData->Reserved))
2529 SetLastError(ERROR_INVALID_PARAMETER);
2530 return FALSE;
2532 if (!DeviceInterfaceData ||
2533 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2535 SetLastError(ERROR_INVALID_PARAMETER);
2536 return FALSE;
2538 if (DeviceInfoData)
2540 struct DeviceInfo *devInfo =
2541 (struct DeviceInfo *)DeviceInfoData->Reserved;
2542 struct InterfaceInstances *iface;
2544 if ((ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
2546 if (MemberIndex < iface->cInstances)
2547 memcpy(DeviceInterfaceData, &iface->instances[MemberIndex],
2548 sizeof(SP_DEVICE_INTERFACE_DATA));
2549 else
2551 SetLastError(ERROR_NO_MORE_ITEMS);
2552 ret = FALSE;
2555 else
2556 SetLastError(ERROR_NO_MORE_ITEMS);
2558 else
2560 DWORD i, cEnumerated = 0;
2561 BOOL found = FALSE;
2563 for (i = 0; !found && cEnumerated < MemberIndex + 1 &&
2564 i < set->cDevices; i++)
2566 struct DeviceInfo *devInfo =
2567 (struct DeviceInfo *)set->devices[i].Reserved;
2568 struct InterfaceInstances *iface;
2570 if (SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface))
2572 if (cEnumerated + iface->cInstances < MemberIndex + 1)
2573 cEnumerated += iface->cInstances;
2574 else
2576 DWORD instanceIndex = MemberIndex - cEnumerated;
2578 memcpy(DeviceInterfaceData,
2579 &iface->instances[instanceIndex],
2580 sizeof(SP_DEVICE_INTERFACE_DATA));
2581 cEnumerated += instanceIndex + 1;
2582 found = TRUE;
2583 ret = TRUE;
2587 if (!found)
2588 SetLastError(ERROR_NO_MORE_ITEMS);
2590 return ret;
2593 /***********************************************************************
2594 * SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2596 * Destroy a DeviceInfoList and free all used memory of the list.
2598 * PARAMS
2599 * devinfo [I] DeviceInfoList pointer to list to destroy
2601 * RETURNS
2602 * Success: non zero value.
2603 * Failure: zero value.
2605 BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
2607 BOOL ret = FALSE;
2609 TRACE("%p\n", devinfo);
2610 if (devinfo && devinfo != (HDEVINFO)INVALID_HANDLE_VALUE)
2612 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
2614 if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
2616 DWORD i;
2618 for (i = 0; i < list->cDevices; i++)
2619 SETUPDI_FreeDeviceInfo(
2620 (struct DeviceInfo *)list->devices[i].Reserved);
2621 HeapFree(GetProcessHeap(), 0, list->devices);
2622 HeapFree(GetProcessHeap(), 0, list);
2623 ret = TRUE;
2627 if (ret == FALSE)
2628 SetLastError(ERROR_INVALID_HANDLE);
2630 return ret;
2633 /***********************************************************************
2634 * SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2636 BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
2637 HDEVINFO DeviceInfoSet,
2638 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2639 PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
2640 DWORD DeviceInterfaceDetailDataSize,
2641 PDWORD RequiredSize,
2642 PSP_DEVINFO_DATA DeviceInfoData)
2644 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2645 struct InterfaceInfo *info;
2646 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath)
2647 + 1;
2648 BOOL ret = FALSE;
2650 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2651 DeviceInterfaceData, DeviceInterfaceDetailData,
2652 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2654 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2655 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2657 SetLastError(ERROR_INVALID_HANDLE);
2658 return FALSE;
2660 if (!DeviceInterfaceData ||
2661 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2662 !DeviceInterfaceData->Reserved)
2664 SetLastError(ERROR_INVALID_PARAMETER);
2665 return FALSE;
2667 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
2668 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath) + sizeof(char) ||
2669 DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A)))
2671 SetLastError(ERROR_INVALID_USER_BUFFER);
2672 return FALSE;
2674 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2676 SetLastError(ERROR_INVALID_USER_BUFFER);
2677 return FALSE;
2679 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2680 if (info->symbolicLink)
2681 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2682 NULL, 0, NULL, NULL);
2683 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2685 if (info->symbolicLink)
2686 WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2687 DeviceInterfaceDetailData->DevicePath,
2688 DeviceInterfaceDetailDataSize -
2689 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath),
2690 NULL, NULL);
2691 else
2692 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2693 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2694 memcpy(DeviceInfoData, info->device, sizeof(SP_DEVINFO_DATA));
2695 ret = TRUE;
2697 else
2699 if (RequiredSize)
2700 *RequiredSize = bytesNeeded;
2701 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2703 return ret;
2706 /***********************************************************************
2707 * SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
2709 BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
2710 HDEVINFO DeviceInfoSet,
2711 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2712 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
2713 DWORD DeviceInterfaceDetailDataSize,
2714 PDWORD RequiredSize,
2715 PSP_DEVINFO_DATA DeviceInfoData)
2717 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2718 struct InterfaceInfo *info;
2719 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)
2720 + sizeof(WCHAR); /* include NULL terminator */
2721 BOOL ret = FALSE;
2723 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2724 DeviceInterfaceData, DeviceInterfaceDetailData,
2725 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2727 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2728 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2730 SetLastError(ERROR_INVALID_HANDLE);
2731 return FALSE;
2733 if (!DeviceInterfaceData ||
2734 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2735 !DeviceInterfaceData->Reserved)
2737 SetLastError(ERROR_INVALID_PARAMETER);
2738 return FALSE;
2740 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
2741 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR) ||
2742 DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W)))
2744 SetLastError(ERROR_INVALID_USER_BUFFER);
2745 return FALSE;
2747 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2749 SetLastError(ERROR_INVALID_USER_BUFFER);
2750 return FALSE;
2752 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2753 if (info->symbolicLink)
2754 bytesNeeded += lstrlenW(info->symbolicLink);
2755 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2757 if (info->symbolicLink)
2758 lstrcpyW(DeviceInterfaceDetailData->DevicePath, info->symbolicLink);
2759 else
2760 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2761 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2762 memcpy(DeviceInfoData, info->device, sizeof(SP_DEVINFO_DATA));
2763 ret = TRUE;
2765 else
2767 if (RequiredSize)
2768 *RequiredSize = bytesNeeded;
2769 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2771 return ret;
2774 struct PropertyMapEntry
2776 DWORD regType;
2777 LPCSTR nameA;
2778 LPCWSTR nameW;
2781 static struct PropertyMapEntry PropertyMap[] = {
2782 { REG_SZ, "DeviceDesc", DeviceDesc },
2783 { REG_MULTI_SZ, "HardwareId", HardwareId },
2784 { REG_MULTI_SZ, "CompatibleIDs", CompatibleIDs },
2785 { 0, NULL, NULL }, /* SPDRP_UNUSED0 */
2786 { REG_SZ, "Service", Service },
2787 { 0, NULL, NULL }, /* SPDRP_UNUSED1 */
2788 { 0, NULL, NULL }, /* SPDRP_UNUSED2 */
2789 { REG_SZ, "Class", Class },
2790 { REG_SZ, "ClassGUID", ClassGUID },
2791 { REG_SZ, "Driver", Driver },
2792 { REG_DWORD, "ConfigFlags", ConfigFlags },
2793 { REG_SZ, "Mfg", Mfg },
2794 { REG_SZ, "FriendlyName", FriendlyName },
2795 { REG_SZ, "LocationInformation", LocationInformation },
2796 { 0, NULL, NULL }, /* SPDRP_PHYSICAL_DEVICE_OBJECT_NAME */
2797 { REG_DWORD, "Capabilities", Capabilities },
2798 { REG_DWORD, "UINumber", UINumber },
2799 { REG_MULTI_SZ, "UpperFilters", UpperFilters },
2800 { REG_MULTI_SZ, "LowerFilters", LowerFilters },
2803 /***********************************************************************
2804 * SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
2806 BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
2807 HDEVINFO DeviceInfoSet,
2808 PSP_DEVINFO_DATA DeviceInfoData,
2809 DWORD Property,
2810 PDWORD PropertyRegDataType,
2811 PBYTE PropertyBuffer,
2812 DWORD PropertyBufferSize,
2813 PDWORD RequiredSize)
2815 BOOL ret = FALSE;
2816 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2817 struct DeviceInfo *devInfo;
2819 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
2820 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2821 RequiredSize);
2823 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2825 SetLastError(ERROR_INVALID_HANDLE);
2826 return FALSE;
2828 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2830 SetLastError(ERROR_INVALID_HANDLE);
2831 return FALSE;
2833 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2834 || !DeviceInfoData->Reserved)
2836 SetLastError(ERROR_INVALID_PARAMETER);
2837 return FALSE;
2839 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2840 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2841 && PropertyMap[Property].nameA)
2843 DWORD size = PropertyBufferSize;
2844 LONG l = RegQueryValueExA(devInfo->key, PropertyMap[Property].nameA,
2845 NULL, PropertyRegDataType, PropertyBuffer, &size);
2847 if (RequiredSize)
2848 *RequiredSize = size;
2849 if (!l)
2850 ret = TRUE;
2851 else
2852 SetLastError(l);
2854 return ret;
2857 /***********************************************************************
2858 * SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
2860 BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
2861 HDEVINFO DeviceInfoSet,
2862 PSP_DEVINFO_DATA DeviceInfoData,
2863 DWORD Property,
2864 PDWORD PropertyRegDataType,
2865 PBYTE PropertyBuffer,
2866 DWORD PropertyBufferSize,
2867 PDWORD RequiredSize)
2869 BOOL ret = FALSE;
2870 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2871 struct DeviceInfo *devInfo;
2873 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
2874 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2875 RequiredSize);
2877 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2879 SetLastError(ERROR_INVALID_HANDLE);
2880 return FALSE;
2882 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2884 SetLastError(ERROR_INVALID_HANDLE);
2885 return FALSE;
2887 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2888 || !DeviceInfoData->Reserved)
2890 SetLastError(ERROR_INVALID_PARAMETER);
2891 return FALSE;
2893 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2894 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2895 && PropertyMap[Property].nameW)
2897 DWORD size = PropertyBufferSize;
2898 LONG l = RegQueryValueExW(devInfo->key, PropertyMap[Property].nameW,
2899 NULL, PropertyRegDataType, PropertyBuffer, &size);
2901 if (RequiredSize)
2902 *RequiredSize = size;
2903 if (!l)
2904 ret = TRUE;
2905 else
2906 SetLastError(l);
2908 return ret;
2911 /***********************************************************************
2912 * SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
2914 BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
2915 HDEVINFO DeviceInfoSet,
2916 PSP_DEVINFO_DATA DeviceInfoData,
2917 DWORD Property,
2918 const BYTE *PropertyBuffer,
2919 DWORD PropertyBufferSize)
2921 BOOL ret = FALSE;
2922 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2923 struct DeviceInfo *devInfo;
2925 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
2926 PropertyBuffer, PropertyBufferSize);
2928 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2930 SetLastError(ERROR_INVALID_HANDLE);
2931 return FALSE;
2933 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2935 SetLastError(ERROR_INVALID_HANDLE);
2936 return FALSE;
2938 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2939 || !DeviceInfoData->Reserved)
2941 SetLastError(ERROR_INVALID_PARAMETER);
2942 return FALSE;
2944 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2945 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2946 && PropertyMap[Property].nameA)
2948 LONG l = RegSetValueExA(devInfo->key, PropertyMap[Property].nameA, 0,
2949 PropertyMap[Property].regType, PropertyBuffer,
2950 PropertyBufferSize);
2951 if (!l)
2952 ret = TRUE;
2953 else
2954 SetLastError(l);
2956 return ret;
2959 /***********************************************************************
2960 * SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
2962 BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
2963 HDEVINFO DeviceInfoSet,
2964 PSP_DEVINFO_DATA DeviceInfoData,
2965 DWORD Property,
2966 const BYTE *PropertyBuffer,
2967 DWORD PropertyBufferSize)
2969 BOOL ret = FALSE;
2970 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2971 struct DeviceInfo *devInfo;
2973 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
2974 PropertyBuffer, PropertyBufferSize);
2976 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2978 SetLastError(ERROR_INVALID_HANDLE);
2979 return FALSE;
2981 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2983 SetLastError(ERROR_INVALID_HANDLE);
2984 return FALSE;
2986 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2987 || !DeviceInfoData->Reserved)
2989 SetLastError(ERROR_INVALID_PARAMETER);
2990 return FALSE;
2992 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2993 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2994 && PropertyMap[Property].nameW)
2996 LONG l = RegSetValueExW(devInfo->key, PropertyMap[Property].nameW, 0,
2997 PropertyMap[Property].regType, PropertyBuffer,
2998 PropertyBufferSize);
2999 if (!l)
3000 ret = TRUE;
3001 else
3002 SetLastError(l);
3004 return ret;
3007 /***********************************************************************
3008 * SetupDiInstallClassA (SETUPAPI.@)
3010 BOOL WINAPI SetupDiInstallClassA(
3011 HWND hwndParent,
3012 PCSTR InfFileName,
3013 DWORD Flags,
3014 HSPFILEQ FileQueue)
3016 UNICODE_STRING FileNameW;
3017 BOOL Result;
3019 if (!RtlCreateUnicodeStringFromAsciiz(&FileNameW, InfFileName))
3021 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3022 return FALSE;
3025 Result = SetupDiInstallClassW(hwndParent, FileNameW.Buffer, Flags, FileQueue);
3027 RtlFreeUnicodeString(&FileNameW);
3029 return Result;
3032 static HKEY CreateClassKey(HINF hInf)
3034 WCHAR FullBuffer[MAX_PATH];
3035 WCHAR Buffer[MAX_PATH];
3036 DWORD RequiredSize;
3037 HKEY hClassKey;
3039 if (!SetupGetLineTextW(NULL,
3040 hInf,
3041 Version,
3042 ClassGUID,
3043 Buffer,
3044 MAX_PATH,
3045 &RequiredSize))
3047 return INVALID_HANDLE_VALUE;
3050 lstrcpyW(FullBuffer, ControlClass);
3051 lstrcatW(FullBuffer, Buffer);
3053 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3054 FullBuffer,
3056 KEY_ALL_ACCESS,
3057 &hClassKey))
3059 if (!SetupGetLineTextW(NULL,
3060 hInf,
3061 Version,
3062 Class,
3063 Buffer,
3064 MAX_PATH,
3065 &RequiredSize))
3067 return INVALID_HANDLE_VALUE;
3070 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
3071 FullBuffer,
3073 NULL,
3074 REG_OPTION_NON_VOLATILE,
3075 KEY_ALL_ACCESS,
3076 NULL,
3077 &hClassKey,
3078 NULL))
3080 return INVALID_HANDLE_VALUE;
3085 if (RegSetValueExW(hClassKey,
3086 Class,
3088 REG_SZ,
3089 (LPBYTE)Buffer,
3090 RequiredSize * sizeof(WCHAR)))
3092 RegCloseKey(hClassKey);
3093 RegDeleteKeyW(HKEY_LOCAL_MACHINE,
3094 FullBuffer);
3095 return INVALID_HANDLE_VALUE;
3098 return hClassKey;
3101 /***********************************************************************
3102 * SetupDiInstallClassW (SETUPAPI.@)
3104 BOOL WINAPI SetupDiInstallClassW(
3105 HWND hwndParent,
3106 PCWSTR InfFileName,
3107 DWORD Flags,
3108 HSPFILEQ FileQueue)
3110 WCHAR SectionName[MAX_PATH];
3111 DWORD SectionNameLength = 0;
3112 HINF hInf;
3113 BOOL bFileQueueCreated = FALSE;
3114 HKEY hClassKey;
3117 FIXME("\n");
3119 if ((Flags & DI_NOVCP) && (FileQueue == NULL || FileQueue == INVALID_HANDLE_VALUE))
3121 SetLastError(ERROR_INVALID_PARAMETER);
3122 return FALSE;
3125 /* Open the .inf file */
3126 hInf = SetupOpenInfFileW(InfFileName,
3127 NULL,
3128 INF_STYLE_WIN4,
3129 NULL);
3130 if (hInf == INVALID_HANDLE_VALUE)
3133 return FALSE;
3136 /* Create or open the class registry key 'HKLM\\CurrentControlSet\\Class\\{GUID}' */
3137 hClassKey = CreateClassKey(hInf);
3138 if (hClassKey == INVALID_HANDLE_VALUE)
3140 SetupCloseInfFile(hInf);
3141 return FALSE;
3145 /* Try to append a layout file */
3146 #if 0
3147 SetupOpenAppendInfFileW(NULL, hInf, NULL);
3148 #endif
3150 /* Retrieve the actual section name */
3151 SetupDiGetActualSectionToInstallW(hInf,
3152 ClassInstall32,
3153 SectionName,
3154 MAX_PATH,
3155 &SectionNameLength,
3156 NULL);
3158 #if 0
3159 if (!(Flags & DI_NOVCP))
3161 FileQueue = SetupOpenFileQueue();
3162 if (FileQueue == INVALID_HANDLE_VALUE)
3164 SetupCloseInfFile(hInf);
3165 return FALSE;
3168 bFileQueueCreated = TRUE;
3171 #endif
3173 SetupInstallFromInfSectionW(NULL,
3174 hInf,
3175 SectionName,
3176 SPINST_REGISTRY,
3177 hClassKey,
3178 NULL,
3180 NULL,
3181 NULL,
3182 INVALID_HANDLE_VALUE,
3183 NULL);
3185 /* FIXME: More code! */
3187 if (bFileQueueCreated)
3188 SetupCloseFileQueue(FileQueue);
3190 SetupCloseInfFile(hInf);
3192 return TRUE;
3196 /***********************************************************************
3197 * SetupDiOpenClassRegKey (SETUPAPI.@)
3199 HKEY WINAPI SetupDiOpenClassRegKey(
3200 const GUID* ClassGuid,
3201 REGSAM samDesired)
3203 return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3204 DIOCR_INSTALLER, NULL, NULL);
3208 /***********************************************************************
3209 * SetupDiOpenClassRegKeyExA (SETUPAPI.@)
3211 HKEY WINAPI SetupDiOpenClassRegKeyExA(
3212 const GUID* ClassGuid,
3213 REGSAM samDesired,
3214 DWORD Flags,
3215 PCSTR MachineName,
3216 PVOID Reserved)
3218 PWSTR MachineNameW = NULL;
3219 HKEY hKey;
3221 TRACE("\n");
3223 if (MachineName)
3225 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
3226 if (MachineNameW == NULL)
3227 return INVALID_HANDLE_VALUE;
3230 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3231 Flags, MachineNameW, Reserved);
3233 MyFree(MachineNameW);
3235 return hKey;
3239 /***********************************************************************
3240 * SetupDiOpenClassRegKeyExW (SETUPAPI.@)
3242 HKEY WINAPI SetupDiOpenClassRegKeyExW(
3243 const GUID* ClassGuid,
3244 REGSAM samDesired,
3245 DWORD Flags,
3246 PCWSTR MachineName,
3247 PVOID Reserved)
3249 HKEY hClassesKey;
3250 HKEY key;
3251 LPCWSTR lpKeyName;
3252 LONG l;
3254 if (MachineName != NULL)
3256 FIXME("Remote access not supported yet!\n");
3257 return INVALID_HANDLE_VALUE;
3260 if (Flags == DIOCR_INSTALLER)
3262 lpKeyName = ControlClass;
3264 else if (Flags == DIOCR_INTERFACE)
3266 lpKeyName = DeviceClasses;
3268 else
3270 ERR("Invalid Flags parameter!\n");
3271 SetLastError(ERROR_INVALID_PARAMETER);
3272 return INVALID_HANDLE_VALUE;
3275 if (!ClassGuid)
3277 if ((l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3278 lpKeyName,
3280 samDesired,
3281 &hClassesKey)))
3283 SetLastError(l);
3284 hClassesKey = INVALID_HANDLE_VALUE;
3286 key = hClassesKey;
3288 else
3290 WCHAR bracedGuidString[39];
3292 SETUPDI_GuidToString(ClassGuid, bracedGuidString);
3294 if (!(l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3295 lpKeyName,
3297 samDesired,
3298 &hClassesKey)))
3300 if ((l = RegOpenKeyExW(hClassesKey,
3301 bracedGuidString,
3303 samDesired,
3304 &key)))
3306 SetLastError(l);
3307 key = INVALID_HANDLE_VALUE;
3309 RegCloseKey(hClassesKey);
3311 else
3313 SetLastError(l);
3314 key = INVALID_HANDLE_VALUE;
3317 return key;
3320 /***********************************************************************
3321 * SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3323 BOOL WINAPI SetupDiOpenDeviceInterfaceW(
3324 HDEVINFO DeviceInfoSet,
3325 PCWSTR DevicePath,
3326 DWORD OpenFlags,
3327 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3329 FIXME("%p %s %08x %p\n",
3330 DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
3331 return FALSE;
3334 /***********************************************************************
3335 * SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3337 BOOL WINAPI SetupDiOpenDeviceInterfaceA(
3338 HDEVINFO DeviceInfoSet,
3339 PCSTR DevicePath,
3340 DWORD OpenFlags,
3341 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3343 FIXME("%p %s %08x %p\n", DeviceInfoSet,
3344 debugstr_a(DevicePath), OpenFlags, DeviceInterfaceData);
3345 return FALSE;
3348 /***********************************************************************
3349 * SetupDiSetClassInstallParamsA (SETUPAPI.@)
3351 BOOL WINAPI SetupDiSetClassInstallParamsA(
3352 HDEVINFO DeviceInfoSet,
3353 PSP_DEVINFO_DATA DeviceInfoData,
3354 PSP_CLASSINSTALL_HEADER ClassInstallParams,
3355 DWORD ClassInstallParamsSize)
3357 FIXME("%p %p %x %u\n",DeviceInfoSet, DeviceInfoData,
3358 ClassInstallParams->InstallFunction, ClassInstallParamsSize);
3359 return FALSE;
3362 /***********************************************************************
3363 * SetupDiCallClassInstaller (SETUPAPI.@)
3365 BOOL WINAPI SetupDiCallClassInstaller(
3366 DI_FUNCTION InstallFunction,
3367 HDEVINFO DeviceInfoSet,
3368 PSP_DEVINFO_DATA DeviceInfoData)
3370 FIXME("%d %p %p\n", InstallFunction, DeviceInfoSet, DeviceInfoData);
3371 return FALSE;
3374 /***********************************************************************
3375 * SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
3377 BOOL WINAPI SetupDiGetDeviceInstallParamsA(
3378 HDEVINFO DeviceInfoSet,
3379 PSP_DEVINFO_DATA DeviceInfoData,
3380 PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
3382 FIXME("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
3383 return FALSE;
3386 /***********************************************************************
3387 * SetupDiOpenDevRegKey (SETUPAPI.@)
3389 HKEY WINAPI SetupDiOpenDevRegKey(
3390 HDEVINFO DeviceInfoSet,
3391 PSP_DEVINFO_DATA DeviceInfoData,
3392 DWORD Scope,
3393 DWORD HwProfile,
3394 DWORD KeyType,
3395 REGSAM samDesired)
3397 FIXME("%p %p %d %d %d %x\n", DeviceInfoSet, DeviceInfoData,
3398 Scope, HwProfile, KeyType, samDesired);
3399 return INVALID_HANDLE_VALUE;