push(c) 7dcddf6204ed5518706a76fe66fd836d81e70dd4
[wine/hacks.git] / dlls / setupapi / devinst.c
blob0ec77b9e8ac1780e6f8586f045f133b80f57a3ed
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 = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, NULL, NULL,
1765 NULL);
1766 HeapFree(GetProcessHeap(), 0, enumstrW);
1768 end:
1769 return ret;
1772 /***********************************************************************
1773 * SetupDiGetClassDevsExA (SETUPAPI.@)
1775 HDEVINFO WINAPI SetupDiGetClassDevsExA(
1776 const GUID *class,
1777 PCSTR enumstr,
1778 HWND parent,
1779 DWORD flags,
1780 HDEVINFO deviceset,
1781 PCSTR machine,
1782 PVOID reserved)
1784 HDEVINFO ret;
1785 LPWSTR enumstrW = NULL, machineW = NULL;
1787 if (enumstr)
1789 int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1790 enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1791 if (!enumstrW)
1793 ret = (HDEVINFO)INVALID_HANDLE_VALUE;
1794 goto end;
1796 MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1798 if (machine)
1800 int len = MultiByteToWideChar(CP_ACP, 0, machine, -1, NULL, 0);
1801 machineW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1802 if (!machineW)
1804 HeapFree(GetProcessHeap(), 0, enumstrW);
1805 ret = (HDEVINFO)INVALID_HANDLE_VALUE;
1806 goto end;
1808 MultiByteToWideChar(CP_ACP, 0, machine, -1, machineW, len);
1810 ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, deviceset,
1811 machineW, reserved);
1812 HeapFree(GetProcessHeap(), 0, enumstrW);
1813 HeapFree(GetProcessHeap(), 0, machineW);
1815 end:
1816 return ret;
1819 static void SETUPDI_AddDeviceInterfaces(SP_DEVINFO_DATA *dev, HKEY key,
1820 const GUID *interface)
1822 DWORD i, len;
1823 WCHAR subKeyName[MAX_PATH];
1824 LONG l = ERROR_SUCCESS;
1826 for (i = 0; !l; i++)
1828 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1829 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
1830 if (!l)
1832 HKEY subKey;
1833 SP_DEVICE_INTERFACE_DATA *iface = NULL;
1835 /* The subkey name is the reference string, with a '#' prepended */
1836 SETUPDI_AddInterfaceInstance(dev, interface, subKeyName + 1,
1837 &iface);
1838 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
1839 if (!l)
1841 WCHAR symbolicLink[MAX_PATH];
1842 DWORD dataType;
1844 len = sizeof(symbolicLink);
1845 l = RegQueryValueExW(subKey, SymbolicLink, NULL, &dataType,
1846 (BYTE *)symbolicLink, &len);
1847 if (!l && dataType == REG_SZ)
1848 SETUPDI_SetInterfaceSymbolicLink(iface, symbolicLink);
1849 RegCloseKey(subKey);
1853 /* FIXME: find and add all the device's interfaces to the device */
1856 static void SETUPDI_EnumerateMatchingInterfaces(HDEVINFO DeviceInfoSet,
1857 HKEY key, const GUID *interface, LPCWSTR enumstr)
1859 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1860 DWORD i, len;
1861 WCHAR subKeyName[MAX_PATH];
1862 LONG l;
1863 HKEY enumKey = INVALID_HANDLE_VALUE;
1865 TRACE("%s\n", debugstr_w(enumstr));
1867 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_READ, NULL,
1868 &enumKey, NULL);
1869 for (i = 0; !l; i++)
1871 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1872 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
1873 if (!l)
1875 HKEY subKey;
1877 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
1878 if (!l)
1880 WCHAR deviceInst[MAX_PATH * 3];
1881 DWORD dataType;
1883 len = sizeof(deviceInst);
1884 l = RegQueryValueExW(subKey, DeviceInstance, NULL, &dataType,
1885 (BYTE *)deviceInst, &len);
1886 if (!l && dataType == REG_SZ)
1888 TRACE("found instance ID %s\n", debugstr_w(deviceInst));
1889 if (!enumstr || !lstrcmpiW(enumstr, deviceInst))
1891 HKEY deviceKey;
1893 l = RegOpenKeyExW(enumKey, deviceInst, 0, KEY_READ,
1894 &deviceKey);
1895 if (!l)
1897 WCHAR deviceClassStr[40];
1899 len = sizeof(deviceClassStr);
1900 l = RegQueryValueExW(deviceKey, ClassGUID, NULL,
1901 &dataType, (BYTE *)deviceClassStr, &len);
1902 if (!l && dataType == REG_SZ &&
1903 deviceClassStr[0] == '{' &&
1904 deviceClassStr[37] == '}')
1906 GUID deviceClass;
1907 SP_DEVINFO_DATA *dev;
1909 deviceClassStr[37] = 0;
1910 UuidFromStringW(&deviceClassStr[1],
1911 &deviceClass);
1912 if (SETUPDI_AddDeviceToSet(set, &deviceClass,
1913 0 /* FIXME: DevInst */, deviceInst,
1914 FALSE, &dev))
1915 SETUPDI_AddDeviceInterfaces(dev, subKey,
1916 interface);
1918 RegCloseKey(deviceKey);
1922 RegCloseKey(subKey);
1926 if (enumKey != INVALID_HANDLE_VALUE)
1927 RegCloseKey(enumKey);
1930 static void SETUPDI_EnumerateInterfaces(HDEVINFO DeviceInfoSet,
1931 const GUID *interface, LPCWSTR enumstr, DWORD flags)
1933 HKEY interfacesKey = SetupDiOpenClassRegKeyExW(interface, KEY_READ,
1934 DIOCR_INTERFACE, NULL, NULL);
1936 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(interface),
1937 debugstr_w(enumstr), flags);
1939 if (interfacesKey != INVALID_HANDLE_VALUE)
1941 if (flags & DIGCF_ALLCLASSES)
1943 DWORD i, len;
1944 WCHAR interfaceGuidStr[40];
1945 LONG l = ERROR_SUCCESS;
1947 for (i = 0; !l; i++)
1949 len = sizeof(interfaceGuidStr) / sizeof(interfaceGuidStr[0]);
1950 l = RegEnumKeyExW(interfacesKey, i, interfaceGuidStr, &len,
1951 NULL, NULL, NULL, NULL);
1952 if (!l)
1954 if (interfaceGuidStr[0] == '{' &&
1955 interfaceGuidStr[37] == '}')
1957 HKEY interfaceKey;
1958 GUID interfaceGuid;
1960 interfaceGuidStr[37] = 0;
1961 UuidFromStringW(&interfaceGuidStr[1], &interfaceGuid);
1962 l = RegOpenKeyExW(interfacesKey, interfaceGuidStr, 0,
1963 KEY_READ, &interfaceKey);
1964 if (!l)
1966 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
1967 interfaceKey, &interfaceGuid, enumstr);
1968 RegCloseKey(interfaceKey);
1974 else
1976 /* In this case, SetupDiOpenClassRegKeyExW opened the specific
1977 * interface's key, so just pass that long
1979 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
1980 interfacesKey, interface, enumstr);
1982 RegCloseKey(interfacesKey);
1986 static void SETUPDI_EnumerateMatchingDevices(HDEVINFO DeviceInfoSet,
1987 LPCWSTR parent, HKEY key, const GUID *class, DWORD flags)
1989 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1990 DWORD i, len;
1991 WCHAR subKeyName[MAX_PATH];
1992 LONG l = ERROR_SUCCESS;
1994 TRACE("%s\n", debugstr_w(parent));
1996 for (i = 0; !l; i++)
1998 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1999 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
2000 if (!l)
2002 HKEY subKey;
2004 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
2005 if (!l)
2007 WCHAR classGuid[40];
2008 DWORD dataType;
2010 len = sizeof(classGuid);
2011 l = RegQueryValueExW(subKey, ClassGUID, NULL, &dataType,
2012 (BYTE *)classGuid, &len);
2013 if (!l && dataType == REG_SZ)
2015 if (classGuid[0] == '{' && classGuid[37] == '}')
2017 GUID deviceClass;
2019 classGuid[37] = 0;
2020 UuidFromStringW(&classGuid[1], &deviceClass);
2021 if ((flags & DIGCF_ALLCLASSES) ||
2022 IsEqualGUID(class, &deviceClass))
2024 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
2025 LPWSTR instanceId;
2027 instanceId = HeapAlloc(GetProcessHeap(), 0,
2028 (lstrlenW(parent) + lstrlenW(subKeyName) + 2)
2029 * sizeof(WCHAR));
2030 if (instanceId)
2032 SP_DEVINFO_DATA *dev;
2034 sprintfW(instanceId, fmt, parent, subKeyName);
2035 SETUPDI_AddDeviceToSet(set, &deviceClass,
2036 0 /* FIXME: DevInst */, instanceId,
2037 FALSE, &dev);
2038 HeapFree(GetProcessHeap(), 0, instanceId);
2043 RegCloseKey(subKey);
2049 static void SETUPDI_EnumerateDevices(HDEVINFO DeviceInfoSet, const GUID *class,
2050 LPCWSTR enumstr, DWORD flags)
2052 HKEY classesKey = SetupDiOpenClassRegKeyExW(class, KEY_READ,
2053 DIOCR_INSTALLER, NULL, NULL);
2055 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(class),
2056 debugstr_w(enumstr), flags);
2058 if (classesKey != INVALID_HANDLE_VALUE)
2060 if (enumstr)
2062 HKEY enumKey;
2063 LONG l = RegOpenKeyExW(classesKey, enumstr, 0, KEY_READ,
2064 &enumKey);
2066 if (!l)
2068 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet, enumstr,
2069 enumKey, class, flags);
2070 RegCloseKey(enumKey);
2073 else
2075 DWORD i, len;
2076 WCHAR subKeyName[MAX_PATH];
2077 LONG l = ERROR_SUCCESS;
2079 for (i = 0; !l; i++)
2081 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2082 l = RegEnumKeyExW(classesKey, i, subKeyName, &len, NULL,
2083 NULL, NULL, NULL);
2084 if (!l)
2086 HKEY subKey;
2088 l = RegOpenKeyExW(classesKey, subKeyName, 0, KEY_READ,
2089 &subKey);
2090 if (!l)
2092 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet,
2093 subKeyName, subKey, class, flags);
2094 RegCloseKey(subKey);
2099 RegCloseKey(classesKey);
2103 /***********************************************************************
2104 * SetupDiGetClassDevsW (SETUPAPI.@)
2106 HDEVINFO WINAPI SetupDiGetClassDevsW(
2107 CONST GUID *class,
2108 LPCWSTR enumstr,
2109 HWND parent,
2110 DWORD flags)
2112 return SetupDiGetClassDevsExW(class, enumstr, parent, flags, NULL, NULL,
2113 NULL);
2116 /***********************************************************************
2117 * SetupDiGetClassDevsExW (SETUPAPI.@)
2119 HDEVINFO WINAPI SetupDiGetClassDevsExW(
2120 CONST GUID *class,
2121 PCWSTR enumstr,
2122 HWND parent,
2123 DWORD flags,
2124 HDEVINFO deviceset,
2125 PCWSTR machine,
2126 PVOID reserved)
2128 static const DWORD unsupportedFlags = DIGCF_DEFAULT | DIGCF_PRESENT |
2129 DIGCF_PROFILE;
2130 HDEVINFO set;
2132 TRACE("%s %s %p 0x%08x %p %s %p\n", debugstr_guid(class),
2133 debugstr_w(enumstr), parent, flags, deviceset, debugstr_w(machine),
2134 reserved);
2136 if (!(flags & DIGCF_ALLCLASSES) && !class)
2138 SetLastError(ERROR_INVALID_PARAMETER);
2139 return NULL;
2141 if (flags & unsupportedFlags)
2142 WARN("unsupported flags %08x\n", flags & unsupportedFlags);
2143 if (deviceset)
2144 set = deviceset;
2145 else
2146 set = SetupDiCreateDeviceInfoListExW(class, parent, machine, reserved);
2147 if (set)
2149 if (machine)
2150 FIXME("%s: unimplemented for remote machines\n",
2151 debugstr_w(machine));
2152 else if (flags & DIGCF_DEVICEINTERFACE)
2153 SETUPDI_EnumerateInterfaces(set, class, enumstr, flags);
2154 else
2155 SETUPDI_EnumerateDevices(set, class, enumstr, flags);
2157 return set;
2160 /***********************************************************************
2161 * SetupDiGetDeviceInfoListDetailA (SETUPAPI.@)
2163 BOOL WINAPI SetupDiGetDeviceInfoListDetailA(
2164 HDEVINFO DeviceInfoSet,
2165 PSP_DEVINFO_LIST_DETAIL_DATA_A DevInfoData )
2167 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2169 TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2171 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2173 SetLastError(ERROR_INVALID_HANDLE);
2174 return FALSE;
2176 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2178 SetLastError(ERROR_INVALID_HANDLE);
2179 return FALSE;
2181 if (!DevInfoData ||
2182 DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_A))
2184 SetLastError(ERROR_INVALID_PARAMETER);
2185 return FALSE;
2187 memcpy(&DevInfoData->ClassGuid, &set->ClassGuid, sizeof(GUID));
2188 DevInfoData->RemoteMachineHandle = NULL;
2189 DevInfoData->RemoteMachineName[0] = '\0';
2190 return TRUE;
2193 /***********************************************************************
2194 * SetupDiGetDeviceInfoListDetailW (SETUPAPI.@)
2196 BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
2197 HDEVINFO DeviceInfoSet,
2198 PSP_DEVINFO_LIST_DETAIL_DATA_W DevInfoData )
2200 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2202 TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2204 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2206 SetLastError(ERROR_INVALID_HANDLE);
2207 return FALSE;
2209 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2211 SetLastError(ERROR_INVALID_HANDLE);
2212 return FALSE;
2214 if (!DevInfoData ||
2215 DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W))
2217 SetLastError(ERROR_INVALID_PARAMETER);
2218 return FALSE;
2220 memcpy(&DevInfoData->ClassGuid, &set->ClassGuid, sizeof(GUID));
2221 DevInfoData->RemoteMachineHandle = NULL;
2222 DevInfoData->RemoteMachineName[0] = '\0';
2223 return TRUE;
2226 /***********************************************************************
2227 * SetupDiCreateDeviceInterfaceA (SETUPAPI.@)
2229 BOOL WINAPI SetupDiCreateDeviceInterfaceA(
2230 HDEVINFO DeviceInfoSet,
2231 PSP_DEVINFO_DATA DeviceInfoData,
2232 const GUID *InterfaceClassGuid,
2233 PCSTR ReferenceString,
2234 DWORD CreationFlags,
2235 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2237 BOOL ret;
2238 LPWSTR ReferenceStringW = NULL;
2240 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2241 debugstr_guid(InterfaceClassGuid), debugstr_a(ReferenceString),
2242 CreationFlags, DeviceInterfaceData);
2244 if (ReferenceString)
2246 ReferenceStringW = MultiByteToUnicode(ReferenceString, CP_ACP);
2247 if (ReferenceStringW == NULL) return FALSE;
2250 ret = SetupDiCreateDeviceInterfaceW(DeviceInfoSet, DeviceInfoData,
2251 InterfaceClassGuid, ReferenceStringW, CreationFlags,
2252 DeviceInterfaceData);
2254 MyFree(ReferenceStringW);
2256 return ret;
2259 /***********************************************************************
2260 * SetupDiCreateDeviceInterfaceW (SETUPAPI.@)
2262 BOOL WINAPI SetupDiCreateDeviceInterfaceW(
2263 HDEVINFO DeviceInfoSet,
2264 PSP_DEVINFO_DATA DeviceInfoData,
2265 const GUID *InterfaceClassGuid,
2266 PCWSTR ReferenceString,
2267 DWORD CreationFlags,
2268 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2270 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2271 struct DeviceInfo *devInfo;
2272 SP_DEVICE_INTERFACE_DATA *iface = NULL;
2273 BOOL ret;
2275 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2276 debugstr_guid(InterfaceClassGuid), debugstr_w(ReferenceString),
2277 CreationFlags, DeviceInterfaceData);
2279 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2281 SetLastError(ERROR_INVALID_HANDLE);
2282 return FALSE;
2284 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2286 SetLastError(ERROR_INVALID_HANDLE);
2287 return FALSE;
2289 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2290 || !DeviceInfoData->Reserved)
2292 SetLastError(ERROR_INVALID_PARAMETER);
2293 return FALSE;
2295 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2296 if (devInfo->set != set)
2298 SetLastError(ERROR_INVALID_PARAMETER);
2299 return FALSE;
2301 if (!InterfaceClassGuid)
2303 SetLastError(ERROR_INVALID_USER_BUFFER);
2304 return FALSE;
2306 if ((ret = SETUPDI_AddInterfaceInstance(DeviceInfoData, InterfaceClassGuid,
2307 ReferenceString, &iface)))
2309 if (DeviceInterfaceData)
2311 if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2313 SetLastError(ERROR_INVALID_USER_BUFFER);
2314 ret = FALSE;
2316 else
2317 memcpy(DeviceInterfaceData, iface, sizeof(*iface));
2320 return ret;
2323 /***********************************************************************
2324 * SetupDiCreateDeviceInterfaceRegKeyA (SETUPAPI.@)
2326 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyA(
2327 HDEVINFO DeviceInfoSet,
2328 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2329 DWORD Reserved,
2330 REGSAM samDesired,
2331 HINF InfHandle,
2332 PCSTR InfSectionName)
2334 HKEY key;
2335 PWSTR InfSectionNameW = NULL;
2337 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2338 samDesired, InfHandle, InfSectionName);
2339 if (InfHandle)
2341 if (!InfSectionName)
2343 SetLastError(ERROR_INVALID_PARAMETER);
2344 return INVALID_HANDLE_VALUE;
2346 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
2347 if (!InfSectionNameW)
2348 return INVALID_HANDLE_VALUE;
2350 key = SetupDiCreateDeviceInterfaceRegKeyW(DeviceInfoSet,
2351 DeviceInterfaceData, Reserved, samDesired, InfHandle,
2352 InfSectionNameW);
2353 MyFree(InfSectionNameW);
2354 return key;
2357 static PWSTR SETUPDI_GetInstancePath(struct InterfaceInfo *ifaceInfo)
2359 static const WCHAR hash[] = {'#',0};
2360 PWSTR instancePath = NULL;
2362 if (ifaceInfo->referenceString)
2364 instancePath = HeapAlloc(GetProcessHeap(), 0,
2365 (lstrlenW(ifaceInfo->referenceString) + 2) * sizeof(WCHAR));
2366 if (instancePath)
2368 lstrcpyW(instancePath, hash);
2369 lstrcatW(instancePath, ifaceInfo->referenceString);
2371 else
2372 SetLastError(ERROR_OUTOFMEMORY);
2374 else
2376 instancePath = HeapAlloc(GetProcessHeap(), 0,
2377 (lstrlenW(hash) + 1) * sizeof(WCHAR));
2378 if (instancePath)
2379 lstrcpyW(instancePath, hash);
2381 return instancePath;
2384 /***********************************************************************
2385 * SetupDiCreateDeviceInterfaceRegKeyW (SETUPAPI.@)
2387 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyW(
2388 HDEVINFO DeviceInfoSet,
2389 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2390 DWORD Reserved,
2391 REGSAM samDesired,
2392 HINF InfHandle,
2393 PCWSTR InfSectionName)
2395 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2396 HKEY key = INVALID_HANDLE_VALUE, interfacesKey;
2397 LONG l;
2399 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2400 samDesired, InfHandle, InfSectionName);
2402 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2403 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2405 SetLastError(ERROR_INVALID_HANDLE);
2406 return INVALID_HANDLE_VALUE;
2408 if (!DeviceInterfaceData ||
2409 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2410 !DeviceInterfaceData->Reserved)
2412 SetLastError(ERROR_INVALID_PARAMETER);
2413 return INVALID_HANDLE_VALUE;
2415 if (InfHandle && !InfSectionName)
2417 SetLastError(ERROR_INVALID_PARAMETER);
2418 return INVALID_HANDLE_VALUE;
2420 if (!(l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, DeviceClasses, 0, NULL, 0,
2421 samDesired, NULL, &interfacesKey, NULL)))
2423 HKEY parent;
2424 WCHAR bracedGuidString[39];
2426 SETUPDI_GuidToString(&DeviceInterfaceData->InterfaceClassGuid,
2427 bracedGuidString);
2428 if (!(l = RegCreateKeyExW(interfacesKey, bracedGuidString, 0, NULL, 0,
2429 samDesired, NULL, &parent, NULL)))
2431 struct InterfaceInfo *ifaceInfo =
2432 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2433 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2435 if (instancePath)
2437 LONG l;
2439 l = RegCreateKeyExW(parent, instancePath, 0, NULL, 0,
2440 samDesired, NULL, &key, NULL);
2441 if (l)
2443 SetLastError(l);
2444 key = INVALID_HANDLE_VALUE;
2446 else if (InfHandle)
2447 FIXME("INF section installation unsupported\n");
2449 HeapFree(GetProcessHeap(), 0, instancePath);
2450 RegCloseKey(parent);
2452 else
2453 SetLastError(l);
2454 RegCloseKey(interfacesKey);
2456 else
2457 SetLastError(l);
2458 return key;
2461 /***********************************************************************
2462 * SetupDiDeleteDeviceInterfaceRegKey (SETUPAPI.@)
2464 BOOL WINAPI SetupDiDeleteDeviceInterfaceRegKey(
2465 HDEVINFO DeviceInfoSet,
2466 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2467 DWORD Reserved)
2469 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2470 HKEY parent;
2471 BOOL ret = FALSE;
2473 TRACE("%p %p %d\n", DeviceInfoSet, DeviceInterfaceData, Reserved);
2475 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2476 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2478 SetLastError(ERROR_INVALID_HANDLE);
2479 return FALSE;
2481 if (!DeviceInterfaceData ||
2482 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2483 !DeviceInterfaceData->Reserved)
2485 SetLastError(ERROR_INVALID_PARAMETER);
2486 return FALSE;
2488 parent = SetupDiOpenClassRegKeyExW(&DeviceInterfaceData->InterfaceClassGuid,
2489 KEY_ALL_ACCESS, DIOCR_INTERFACE, NULL, NULL);
2490 if (parent != INVALID_HANDLE_VALUE)
2492 struct InterfaceInfo *ifaceInfo =
2493 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2494 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2496 if (instancePath)
2498 LONG l = RegDeleteKeyW(parent, instancePath);
2500 if (l)
2501 SetLastError(l);
2502 else
2503 ret = TRUE;
2504 HeapFree(GetProcessHeap(), 0, instancePath);
2506 RegCloseKey(parent);
2508 return ret;
2511 /***********************************************************************
2512 * SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2514 * PARAMS
2515 * DeviceInfoSet [I] Set of devices from which to enumerate
2516 * interfaces
2517 * DeviceInfoData [I] (Optional) If specified, a specific device
2518 * instance from which to enumerate interfaces.
2519 * If it isn't specified, all interfaces for all
2520 * devices in the set are enumerated.
2521 * InterfaceClassGuid [I] The interface class to enumerate.
2522 * MemberIndex [I] An index of the interface instance to enumerate.
2523 * A caller should start with MemberIndex set to 0,
2524 * and continue until the function fails with
2525 * ERROR_NO_MORE_ITEMS.
2526 * DeviceInterfaceData [I/O] Returns an enumerated interface. Its cbSize
2527 * member must be set to
2528 * sizeof(SP_DEVICE_INTERFACE_DATA).
2530 * RETURNS
2531 * Success: non-zero value.
2532 * Failure: FALSE. Call GetLastError() for more info.
2534 BOOL WINAPI SetupDiEnumDeviceInterfaces(
2535 HDEVINFO DeviceInfoSet,
2536 PSP_DEVINFO_DATA DeviceInfoData,
2537 CONST GUID * InterfaceClassGuid,
2538 DWORD MemberIndex,
2539 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2541 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2542 BOOL ret = FALSE;
2544 TRACE("%p, %p, %s, %d, %p\n", DeviceInfoSet, DeviceInfoData,
2545 debugstr_guid(InterfaceClassGuid), MemberIndex, DeviceInterfaceData);
2547 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2548 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2550 SetLastError(ERROR_INVALID_HANDLE);
2551 return FALSE;
2553 if (DeviceInfoData && (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA) ||
2554 !DeviceInfoData->Reserved))
2556 SetLastError(ERROR_INVALID_PARAMETER);
2557 return FALSE;
2559 if (!DeviceInterfaceData ||
2560 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2562 SetLastError(ERROR_INVALID_PARAMETER);
2563 return FALSE;
2565 if (DeviceInfoData)
2567 struct DeviceInfo *devInfo =
2568 (struct DeviceInfo *)DeviceInfoData->Reserved;
2569 struct InterfaceInstances *iface;
2571 if ((ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
2573 if (MemberIndex < iface->cInstances)
2574 memcpy(DeviceInterfaceData, &iface->instances[MemberIndex],
2575 sizeof(SP_DEVICE_INTERFACE_DATA));
2576 else
2578 SetLastError(ERROR_NO_MORE_ITEMS);
2579 ret = FALSE;
2582 else
2583 SetLastError(ERROR_NO_MORE_ITEMS);
2585 else
2587 DWORD i, cEnumerated = 0;
2588 BOOL found = FALSE;
2590 for (i = 0; !found && cEnumerated < MemberIndex + 1 &&
2591 i < set->cDevices; i++)
2593 struct DeviceInfo *devInfo =
2594 (struct DeviceInfo *)set->devices[i].Reserved;
2595 struct InterfaceInstances *iface;
2597 if (SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface))
2599 if (cEnumerated + iface->cInstances < MemberIndex + 1)
2600 cEnumerated += iface->cInstances;
2601 else
2603 DWORD instanceIndex = MemberIndex - cEnumerated;
2605 memcpy(DeviceInterfaceData,
2606 &iface->instances[instanceIndex],
2607 sizeof(SP_DEVICE_INTERFACE_DATA));
2608 cEnumerated += instanceIndex + 1;
2609 found = TRUE;
2610 ret = TRUE;
2614 if (!found)
2615 SetLastError(ERROR_NO_MORE_ITEMS);
2617 return ret;
2620 /***********************************************************************
2621 * SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2623 * Destroy a DeviceInfoList and free all used memory of the list.
2625 * PARAMS
2626 * devinfo [I] DeviceInfoList pointer to list to destroy
2628 * RETURNS
2629 * Success: non zero value.
2630 * Failure: zero value.
2632 BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
2634 BOOL ret = FALSE;
2636 TRACE("%p\n", devinfo);
2637 if (devinfo && devinfo != (HDEVINFO)INVALID_HANDLE_VALUE)
2639 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
2641 if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
2643 DWORD i;
2645 for (i = 0; i < list->cDevices; i++)
2646 SETUPDI_FreeDeviceInfo(
2647 (struct DeviceInfo *)list->devices[i].Reserved);
2648 HeapFree(GetProcessHeap(), 0, list->devices);
2649 HeapFree(GetProcessHeap(), 0, list);
2650 ret = TRUE;
2654 if (ret == FALSE)
2655 SetLastError(ERROR_INVALID_HANDLE);
2657 return ret;
2660 /***********************************************************************
2661 * SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2663 BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
2664 HDEVINFO DeviceInfoSet,
2665 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2666 PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
2667 DWORD DeviceInterfaceDetailDataSize,
2668 PDWORD RequiredSize,
2669 PSP_DEVINFO_DATA DeviceInfoData)
2671 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2672 struct InterfaceInfo *info;
2673 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath)
2674 + 1;
2675 BOOL ret = FALSE;
2677 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2678 DeviceInterfaceData, DeviceInterfaceDetailData,
2679 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2681 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2682 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2684 SetLastError(ERROR_INVALID_HANDLE);
2685 return FALSE;
2687 if (!DeviceInterfaceData ||
2688 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2689 !DeviceInterfaceData->Reserved)
2691 SetLastError(ERROR_INVALID_PARAMETER);
2692 return FALSE;
2694 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
2695 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath) + sizeof(char) ||
2696 DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A)))
2698 SetLastError(ERROR_INVALID_USER_BUFFER);
2699 return FALSE;
2701 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2703 SetLastError(ERROR_INVALID_USER_BUFFER);
2704 return FALSE;
2706 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2707 if (info->symbolicLink)
2708 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2709 NULL, 0, NULL, NULL);
2710 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2712 if (info->symbolicLink)
2713 WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2714 DeviceInterfaceDetailData->DevicePath,
2715 DeviceInterfaceDetailDataSize -
2716 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath),
2717 NULL, NULL);
2718 else
2719 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2720 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2721 memcpy(DeviceInfoData, info->device, sizeof(SP_DEVINFO_DATA));
2722 ret = TRUE;
2724 else
2726 if (RequiredSize)
2727 *RequiredSize = bytesNeeded;
2728 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2730 return ret;
2733 /***********************************************************************
2734 * SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
2736 BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
2737 HDEVINFO DeviceInfoSet,
2738 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2739 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
2740 DWORD DeviceInterfaceDetailDataSize,
2741 PDWORD RequiredSize,
2742 PSP_DEVINFO_DATA DeviceInfoData)
2744 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2745 struct InterfaceInfo *info;
2746 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)
2747 + sizeof(WCHAR); /* include NULL terminator */
2748 BOOL ret = FALSE;
2750 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2751 DeviceInterfaceData, DeviceInterfaceDetailData,
2752 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2754 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2755 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2757 SetLastError(ERROR_INVALID_HANDLE);
2758 return FALSE;
2760 if (!DeviceInterfaceData ||
2761 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2762 !DeviceInterfaceData->Reserved)
2764 SetLastError(ERROR_INVALID_PARAMETER);
2765 return FALSE;
2767 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
2768 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR) ||
2769 DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W)))
2771 SetLastError(ERROR_INVALID_USER_BUFFER);
2772 return FALSE;
2774 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2776 SetLastError(ERROR_INVALID_USER_BUFFER);
2777 return FALSE;
2779 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2780 if (info->symbolicLink)
2781 bytesNeeded += lstrlenW(info->symbolicLink);
2782 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2784 if (info->symbolicLink)
2785 lstrcpyW(DeviceInterfaceDetailData->DevicePath, info->symbolicLink);
2786 else
2787 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2788 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2789 memcpy(DeviceInfoData, info->device, sizeof(SP_DEVINFO_DATA));
2790 ret = TRUE;
2792 else
2794 if (RequiredSize)
2795 *RequiredSize = bytesNeeded;
2796 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2798 return ret;
2801 struct PropertyMapEntry
2803 DWORD regType;
2804 LPCSTR nameA;
2805 LPCWSTR nameW;
2808 static struct PropertyMapEntry PropertyMap[] = {
2809 { REG_SZ, "DeviceDesc", DeviceDesc },
2810 { REG_MULTI_SZ, "HardwareId", HardwareId },
2811 { REG_MULTI_SZ, "CompatibleIDs", CompatibleIDs },
2812 { 0, NULL, NULL }, /* SPDRP_UNUSED0 */
2813 { REG_SZ, "Service", Service },
2814 { 0, NULL, NULL }, /* SPDRP_UNUSED1 */
2815 { 0, NULL, NULL }, /* SPDRP_UNUSED2 */
2816 { REG_SZ, "Class", Class },
2817 { REG_SZ, "ClassGUID", ClassGUID },
2818 { REG_SZ, "Driver", Driver },
2819 { REG_DWORD, "ConfigFlags", ConfigFlags },
2820 { REG_SZ, "Mfg", Mfg },
2821 { REG_SZ, "FriendlyName", FriendlyName },
2822 { REG_SZ, "LocationInformation", LocationInformation },
2823 { 0, NULL, NULL }, /* SPDRP_PHYSICAL_DEVICE_OBJECT_NAME */
2824 { REG_DWORD, "Capabilities", Capabilities },
2825 { REG_DWORD, "UINumber", UINumber },
2826 { REG_MULTI_SZ, "UpperFilters", UpperFilters },
2827 { REG_MULTI_SZ, "LowerFilters", LowerFilters },
2830 /***********************************************************************
2831 * SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
2833 BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
2834 HDEVINFO DeviceInfoSet,
2835 PSP_DEVINFO_DATA DeviceInfoData,
2836 DWORD Property,
2837 PDWORD PropertyRegDataType,
2838 PBYTE PropertyBuffer,
2839 DWORD PropertyBufferSize,
2840 PDWORD RequiredSize)
2842 BOOL ret = FALSE;
2843 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2844 struct DeviceInfo *devInfo;
2846 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
2847 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2848 RequiredSize);
2850 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2852 SetLastError(ERROR_INVALID_HANDLE);
2853 return FALSE;
2855 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2857 SetLastError(ERROR_INVALID_HANDLE);
2858 return FALSE;
2860 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2861 || !DeviceInfoData->Reserved)
2863 SetLastError(ERROR_INVALID_PARAMETER);
2864 return FALSE;
2866 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2867 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2868 && PropertyMap[Property].nameA)
2870 DWORD size = PropertyBufferSize;
2871 LONG l = RegQueryValueExA(devInfo->key, PropertyMap[Property].nameA,
2872 NULL, PropertyRegDataType, PropertyBuffer, &size);
2874 if (RequiredSize)
2875 *RequiredSize = size;
2876 if (!l)
2877 ret = TRUE;
2878 else
2879 SetLastError(l);
2881 return ret;
2884 /***********************************************************************
2885 * SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
2887 BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
2888 HDEVINFO DeviceInfoSet,
2889 PSP_DEVINFO_DATA DeviceInfoData,
2890 DWORD Property,
2891 PDWORD PropertyRegDataType,
2892 PBYTE PropertyBuffer,
2893 DWORD PropertyBufferSize,
2894 PDWORD RequiredSize)
2896 BOOL ret = FALSE;
2897 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2898 struct DeviceInfo *devInfo;
2900 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
2901 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2902 RequiredSize);
2904 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2906 SetLastError(ERROR_INVALID_HANDLE);
2907 return FALSE;
2909 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2911 SetLastError(ERROR_INVALID_HANDLE);
2912 return FALSE;
2914 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2915 || !DeviceInfoData->Reserved)
2917 SetLastError(ERROR_INVALID_PARAMETER);
2918 return FALSE;
2920 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2921 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2922 && PropertyMap[Property].nameW)
2924 DWORD size = PropertyBufferSize;
2925 LONG l = RegQueryValueExW(devInfo->key, PropertyMap[Property].nameW,
2926 NULL, PropertyRegDataType, PropertyBuffer, &size);
2928 if (RequiredSize)
2929 *RequiredSize = size;
2930 if (!l)
2931 ret = TRUE;
2932 else
2933 SetLastError(l);
2935 return ret;
2938 /***********************************************************************
2939 * SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
2941 BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
2942 HDEVINFO DeviceInfoSet,
2943 PSP_DEVINFO_DATA DeviceInfoData,
2944 DWORD Property,
2945 const BYTE *PropertyBuffer,
2946 DWORD PropertyBufferSize)
2948 BOOL ret = FALSE;
2949 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2950 struct DeviceInfo *devInfo;
2952 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
2953 PropertyBuffer, PropertyBufferSize);
2955 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2957 SetLastError(ERROR_INVALID_HANDLE);
2958 return FALSE;
2960 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2962 SetLastError(ERROR_INVALID_HANDLE);
2963 return FALSE;
2965 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2966 || !DeviceInfoData->Reserved)
2968 SetLastError(ERROR_INVALID_PARAMETER);
2969 return FALSE;
2971 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2972 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2973 && PropertyMap[Property].nameA)
2975 LONG l = RegSetValueExA(devInfo->key, PropertyMap[Property].nameA, 0,
2976 PropertyMap[Property].regType, PropertyBuffer,
2977 PropertyBufferSize);
2978 if (!l)
2979 ret = TRUE;
2980 else
2981 SetLastError(l);
2983 return ret;
2986 /***********************************************************************
2987 * SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
2989 BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
2990 HDEVINFO DeviceInfoSet,
2991 PSP_DEVINFO_DATA DeviceInfoData,
2992 DWORD Property,
2993 const BYTE *PropertyBuffer,
2994 DWORD PropertyBufferSize)
2996 BOOL ret = FALSE;
2997 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2998 struct DeviceInfo *devInfo;
3000 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
3001 PropertyBuffer, PropertyBufferSize);
3003 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3005 SetLastError(ERROR_INVALID_HANDLE);
3006 return FALSE;
3008 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3010 SetLastError(ERROR_INVALID_HANDLE);
3011 return FALSE;
3013 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3014 || !DeviceInfoData->Reserved)
3016 SetLastError(ERROR_INVALID_PARAMETER);
3017 return FALSE;
3019 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3020 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3021 && PropertyMap[Property].nameW)
3023 LONG l = RegSetValueExW(devInfo->key, PropertyMap[Property].nameW, 0,
3024 PropertyMap[Property].regType, PropertyBuffer,
3025 PropertyBufferSize);
3026 if (!l)
3027 ret = TRUE;
3028 else
3029 SetLastError(l);
3031 return ret;
3034 /***********************************************************************
3035 * SetupDiInstallClassA (SETUPAPI.@)
3037 BOOL WINAPI SetupDiInstallClassA(
3038 HWND hwndParent,
3039 PCSTR InfFileName,
3040 DWORD Flags,
3041 HSPFILEQ FileQueue)
3043 UNICODE_STRING FileNameW;
3044 BOOL Result;
3046 if (!RtlCreateUnicodeStringFromAsciiz(&FileNameW, InfFileName))
3048 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3049 return FALSE;
3052 Result = SetupDiInstallClassW(hwndParent, FileNameW.Buffer, Flags, FileQueue);
3054 RtlFreeUnicodeString(&FileNameW);
3056 return Result;
3059 static HKEY CreateClassKey(HINF hInf)
3061 WCHAR FullBuffer[MAX_PATH];
3062 WCHAR Buffer[MAX_PATH];
3063 DWORD RequiredSize;
3064 HKEY hClassKey;
3066 if (!SetupGetLineTextW(NULL,
3067 hInf,
3068 Version,
3069 ClassGUID,
3070 Buffer,
3071 MAX_PATH,
3072 &RequiredSize))
3074 return INVALID_HANDLE_VALUE;
3077 lstrcpyW(FullBuffer, ControlClass);
3078 lstrcatW(FullBuffer, Buffer);
3080 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3081 FullBuffer,
3083 KEY_ALL_ACCESS,
3084 &hClassKey))
3086 if (!SetupGetLineTextW(NULL,
3087 hInf,
3088 Version,
3089 Class,
3090 Buffer,
3091 MAX_PATH,
3092 &RequiredSize))
3094 return INVALID_HANDLE_VALUE;
3097 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
3098 FullBuffer,
3100 NULL,
3101 REG_OPTION_NON_VOLATILE,
3102 KEY_ALL_ACCESS,
3103 NULL,
3104 &hClassKey,
3105 NULL))
3107 return INVALID_HANDLE_VALUE;
3112 if (RegSetValueExW(hClassKey,
3113 Class,
3115 REG_SZ,
3116 (LPBYTE)Buffer,
3117 RequiredSize * sizeof(WCHAR)))
3119 RegCloseKey(hClassKey);
3120 RegDeleteKeyW(HKEY_LOCAL_MACHINE,
3121 FullBuffer);
3122 return INVALID_HANDLE_VALUE;
3125 return hClassKey;
3128 /***********************************************************************
3129 * SetupDiInstallClassW (SETUPAPI.@)
3131 BOOL WINAPI SetupDiInstallClassW(
3132 HWND hwndParent,
3133 PCWSTR InfFileName,
3134 DWORD Flags,
3135 HSPFILEQ FileQueue)
3137 WCHAR SectionName[MAX_PATH];
3138 DWORD SectionNameLength = 0;
3139 HINF hInf;
3140 BOOL bFileQueueCreated = FALSE;
3141 HKEY hClassKey;
3144 FIXME("\n");
3146 if ((Flags & DI_NOVCP) && (FileQueue == NULL || FileQueue == INVALID_HANDLE_VALUE))
3148 SetLastError(ERROR_INVALID_PARAMETER);
3149 return FALSE;
3152 /* Open the .inf file */
3153 hInf = SetupOpenInfFileW(InfFileName,
3154 NULL,
3155 INF_STYLE_WIN4,
3156 NULL);
3157 if (hInf == INVALID_HANDLE_VALUE)
3160 return FALSE;
3163 /* Create or open the class registry key 'HKLM\\CurrentControlSet\\Class\\{GUID}' */
3164 hClassKey = CreateClassKey(hInf);
3165 if (hClassKey == INVALID_HANDLE_VALUE)
3167 SetupCloseInfFile(hInf);
3168 return FALSE;
3172 /* Try to append a layout file */
3173 #if 0
3174 SetupOpenAppendInfFileW(NULL, hInf, NULL);
3175 #endif
3177 /* Retrieve the actual section name */
3178 SetupDiGetActualSectionToInstallW(hInf,
3179 ClassInstall32,
3180 SectionName,
3181 MAX_PATH,
3182 &SectionNameLength,
3183 NULL);
3185 #if 0
3186 if (!(Flags & DI_NOVCP))
3188 FileQueue = SetupOpenFileQueue();
3189 if (FileQueue == INVALID_HANDLE_VALUE)
3191 SetupCloseInfFile(hInf);
3192 return FALSE;
3195 bFileQueueCreated = TRUE;
3198 #endif
3200 SetupInstallFromInfSectionW(NULL,
3201 hInf,
3202 SectionName,
3203 SPINST_REGISTRY,
3204 hClassKey,
3205 NULL,
3207 NULL,
3208 NULL,
3209 INVALID_HANDLE_VALUE,
3210 NULL);
3212 /* FIXME: More code! */
3214 if (bFileQueueCreated)
3215 SetupCloseFileQueue(FileQueue);
3217 SetupCloseInfFile(hInf);
3219 return TRUE;
3223 /***********************************************************************
3224 * SetupDiOpenClassRegKey (SETUPAPI.@)
3226 HKEY WINAPI SetupDiOpenClassRegKey(
3227 const GUID* ClassGuid,
3228 REGSAM samDesired)
3230 return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3231 DIOCR_INSTALLER, NULL, NULL);
3235 /***********************************************************************
3236 * SetupDiOpenClassRegKeyExA (SETUPAPI.@)
3238 HKEY WINAPI SetupDiOpenClassRegKeyExA(
3239 const GUID* ClassGuid,
3240 REGSAM samDesired,
3241 DWORD Flags,
3242 PCSTR MachineName,
3243 PVOID Reserved)
3245 PWSTR MachineNameW = NULL;
3246 HKEY hKey;
3248 TRACE("\n");
3250 if (MachineName)
3252 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
3253 if (MachineNameW == NULL)
3254 return INVALID_HANDLE_VALUE;
3257 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3258 Flags, MachineNameW, Reserved);
3260 MyFree(MachineNameW);
3262 return hKey;
3266 /***********************************************************************
3267 * SetupDiOpenClassRegKeyExW (SETUPAPI.@)
3269 HKEY WINAPI SetupDiOpenClassRegKeyExW(
3270 const GUID* ClassGuid,
3271 REGSAM samDesired,
3272 DWORD Flags,
3273 PCWSTR MachineName,
3274 PVOID Reserved)
3276 HKEY hClassesKey;
3277 HKEY key;
3278 LPCWSTR lpKeyName;
3279 LONG l;
3281 if (MachineName != NULL)
3283 FIXME("Remote access not supported yet!\n");
3284 return INVALID_HANDLE_VALUE;
3287 if (Flags == DIOCR_INSTALLER)
3289 lpKeyName = ControlClass;
3291 else if (Flags == DIOCR_INTERFACE)
3293 lpKeyName = DeviceClasses;
3295 else
3297 ERR("Invalid Flags parameter!\n");
3298 SetLastError(ERROR_INVALID_PARAMETER);
3299 return INVALID_HANDLE_VALUE;
3302 if (!ClassGuid)
3304 if ((l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3305 lpKeyName,
3307 samDesired,
3308 &hClassesKey)))
3310 SetLastError(l);
3311 hClassesKey = INVALID_HANDLE_VALUE;
3313 key = hClassesKey;
3315 else
3317 WCHAR bracedGuidString[39];
3319 SETUPDI_GuidToString(ClassGuid, bracedGuidString);
3321 if (!(l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3322 lpKeyName,
3324 samDesired,
3325 &hClassesKey)))
3327 if ((l = RegOpenKeyExW(hClassesKey,
3328 bracedGuidString,
3330 samDesired,
3331 &key)))
3333 SetLastError(l);
3334 key = INVALID_HANDLE_VALUE;
3336 RegCloseKey(hClassesKey);
3338 else
3340 SetLastError(l);
3341 key = INVALID_HANDLE_VALUE;
3344 return key;
3347 /***********************************************************************
3348 * SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3350 BOOL WINAPI SetupDiOpenDeviceInterfaceW(
3351 HDEVINFO DeviceInfoSet,
3352 PCWSTR DevicePath,
3353 DWORD OpenFlags,
3354 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3356 FIXME("%p %s %08x %p\n",
3357 DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
3358 return FALSE;
3361 /***********************************************************************
3362 * SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3364 BOOL WINAPI SetupDiOpenDeviceInterfaceA(
3365 HDEVINFO DeviceInfoSet,
3366 PCSTR DevicePath,
3367 DWORD OpenFlags,
3368 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3370 FIXME("%p %s %08x %p\n", DeviceInfoSet,
3371 debugstr_a(DevicePath), OpenFlags, DeviceInterfaceData);
3372 return FALSE;
3375 /***********************************************************************
3376 * SetupDiSetClassInstallParamsA (SETUPAPI.@)
3378 BOOL WINAPI SetupDiSetClassInstallParamsA(
3379 HDEVINFO DeviceInfoSet,
3380 PSP_DEVINFO_DATA DeviceInfoData,
3381 PSP_CLASSINSTALL_HEADER ClassInstallParams,
3382 DWORD ClassInstallParamsSize)
3384 FIXME("%p %p %x %u\n",DeviceInfoSet, DeviceInfoData,
3385 ClassInstallParams->InstallFunction, ClassInstallParamsSize);
3386 return FALSE;
3389 /***********************************************************************
3390 * SetupDiCallClassInstaller (SETUPAPI.@)
3392 BOOL WINAPI SetupDiCallClassInstaller(
3393 DI_FUNCTION InstallFunction,
3394 HDEVINFO DeviceInfoSet,
3395 PSP_DEVINFO_DATA DeviceInfoData)
3397 FIXME("%d %p %p\n", InstallFunction, DeviceInfoSet, DeviceInfoData);
3398 return FALSE;
3401 /***********************************************************************
3402 * SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
3404 BOOL WINAPI SetupDiGetDeviceInstallParamsA(
3405 HDEVINFO DeviceInfoSet,
3406 PSP_DEVINFO_DATA DeviceInfoData,
3407 PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
3409 FIXME("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
3410 return FALSE;
3413 /***********************************************************************
3414 * SetupDiOpenDevRegKey (SETUPAPI.@)
3416 HKEY WINAPI SetupDiOpenDevRegKey(
3417 HDEVINFO DeviceInfoSet,
3418 PSP_DEVINFO_DATA DeviceInfoData,
3419 DWORD Scope,
3420 DWORD HwProfile,
3421 DWORD KeyType,
3422 REGSAM samDesired)
3424 FIXME("%p %p %d %d %d %x\n", DeviceInfoSet, DeviceInfoData,
3425 Scope, HwProfile, KeyType, samDesired);
3426 return INVALID_HANDLE_VALUE;