setupapi: When freeing an interface, delete it from the registry if its device is...
[wine/multimedia.git] / dlls / setupapi / devinst.c
bloba5356e5e7c85b1cb1f9966cc00dbf673e7139ec3
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 FIXME("stub\n");
2102 return NULL;
2105 /***********************************************************************
2106 * SetupDiCreateDeviceInterfaceA (SETUPAPI.@)
2108 BOOL WINAPI SetupDiCreateDeviceInterfaceA(
2109 HDEVINFO DeviceInfoSet,
2110 PSP_DEVINFO_DATA DeviceInfoData,
2111 const GUID *InterfaceClassGuid,
2112 PCSTR ReferenceString,
2113 DWORD CreationFlags,
2114 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2116 BOOL ret;
2117 LPWSTR ReferenceStringW = NULL;
2119 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2120 debugstr_guid(InterfaceClassGuid), debugstr_a(ReferenceString),
2121 CreationFlags, DeviceInterfaceData);
2123 if (ReferenceString)
2125 ReferenceStringW = MultiByteToUnicode(ReferenceString, CP_ACP);
2126 if (ReferenceStringW == NULL) return FALSE;
2129 ret = SetupDiCreateDeviceInterfaceW(DeviceInfoSet, DeviceInfoData,
2130 InterfaceClassGuid, ReferenceStringW, CreationFlags,
2131 DeviceInterfaceData);
2133 MyFree(ReferenceStringW);
2135 return ret;
2138 /***********************************************************************
2139 * SetupDiCreateDeviceInterfaceW (SETUPAPI.@)
2141 BOOL WINAPI SetupDiCreateDeviceInterfaceW(
2142 HDEVINFO DeviceInfoSet,
2143 PSP_DEVINFO_DATA DeviceInfoData,
2144 const GUID *InterfaceClassGuid,
2145 PCWSTR ReferenceString,
2146 DWORD CreationFlags,
2147 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2149 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2150 struct DeviceInfo *devInfo;
2151 SP_DEVICE_INTERFACE_DATA *iface = NULL;
2152 BOOL ret;
2154 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2155 debugstr_guid(InterfaceClassGuid), debugstr_w(ReferenceString),
2156 CreationFlags, DeviceInterfaceData);
2158 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2160 SetLastError(ERROR_INVALID_HANDLE);
2161 return FALSE;
2163 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2165 SetLastError(ERROR_INVALID_HANDLE);
2166 return FALSE;
2168 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2169 || !DeviceInfoData->Reserved)
2171 SetLastError(ERROR_INVALID_PARAMETER);
2172 return FALSE;
2174 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2175 if (devInfo->set != set)
2177 SetLastError(ERROR_INVALID_PARAMETER);
2178 return FALSE;
2180 if (!InterfaceClassGuid)
2182 SetLastError(ERROR_INVALID_USER_BUFFER);
2183 return FALSE;
2185 if ((ret = SETUPDI_AddInterfaceInstance(DeviceInfoData, InterfaceClassGuid,
2186 ReferenceString, &iface)))
2188 if (DeviceInterfaceData)
2190 if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2192 SetLastError(ERROR_INVALID_USER_BUFFER);
2193 ret = FALSE;
2195 else
2196 memcpy(DeviceInterfaceData, iface, sizeof(*iface));
2199 return ret;
2202 /***********************************************************************
2203 * SetupDiCreateDeviceInterfaceRegKeyA (SETUPAPI.@)
2205 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyA(
2206 HDEVINFO DeviceInfoSet,
2207 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2208 DWORD Reserved,
2209 REGSAM samDesired,
2210 HINF InfHandle,
2211 PCSTR InfSectionName)
2213 HKEY key;
2214 PWSTR InfSectionNameW = NULL;
2216 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2217 samDesired, InfHandle, InfSectionName);
2218 if (InfHandle)
2220 if (!InfSectionName)
2222 SetLastError(ERROR_INVALID_PARAMETER);
2223 return INVALID_HANDLE_VALUE;
2225 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
2226 if (!InfSectionNameW)
2227 return INVALID_HANDLE_VALUE;
2229 key = SetupDiCreateDeviceInterfaceRegKeyW(DeviceInfoSet,
2230 DeviceInterfaceData, Reserved, samDesired, InfHandle,
2231 InfSectionNameW);
2232 MyFree(InfSectionNameW);
2233 return key;
2236 static PWSTR SETUPDI_GetInstancePath(struct InterfaceInfo *ifaceInfo)
2238 static const WCHAR hash[] = {'#',0};
2239 PWSTR instancePath = NULL;
2241 if (ifaceInfo->referenceString)
2243 instancePath = HeapAlloc(GetProcessHeap(), 0,
2244 (lstrlenW(ifaceInfo->referenceString) + 2) * sizeof(WCHAR));
2245 if (instancePath)
2247 lstrcpyW(instancePath, hash);
2248 lstrcatW(instancePath, ifaceInfo->referenceString);
2250 else
2251 SetLastError(ERROR_OUTOFMEMORY);
2253 else
2255 instancePath = HeapAlloc(GetProcessHeap(), 0,
2256 (lstrlenW(hash) + 1) * sizeof(WCHAR));
2257 if (instancePath)
2258 lstrcpyW(instancePath, hash);
2260 return instancePath;
2263 /***********************************************************************
2264 * SetupDiCreateDeviceInterfaceRegKeyW (SETUPAPI.@)
2266 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyW(
2267 HDEVINFO DeviceInfoSet,
2268 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2269 DWORD Reserved,
2270 REGSAM samDesired,
2271 HINF InfHandle,
2272 PCWSTR InfSectionName)
2274 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2275 HKEY key = INVALID_HANDLE_VALUE, interfacesKey;
2276 LONG l;
2278 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2279 samDesired, InfHandle, InfSectionName);
2281 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2282 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2284 SetLastError(ERROR_INVALID_HANDLE);
2285 return INVALID_HANDLE_VALUE;
2287 if (!DeviceInterfaceData ||
2288 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2289 !DeviceInterfaceData->Reserved)
2291 SetLastError(ERROR_INVALID_PARAMETER);
2292 return INVALID_HANDLE_VALUE;
2294 if (InfHandle && !InfSectionName)
2296 SetLastError(ERROR_INVALID_PARAMETER);
2297 return INVALID_HANDLE_VALUE;
2299 if (!(l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, DeviceClasses, 0, NULL, 0,
2300 samDesired, NULL, &interfacesKey, NULL)))
2302 HKEY parent;
2303 WCHAR bracedGuidString[39];
2305 SETUPDI_GuidToString(&DeviceInterfaceData->InterfaceClassGuid,
2306 bracedGuidString);
2307 if (!(l = RegCreateKeyExW(interfacesKey, bracedGuidString, 0, NULL, 0,
2308 samDesired, NULL, &parent, NULL)))
2310 struct InterfaceInfo *ifaceInfo =
2311 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2312 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2314 if (instancePath)
2316 LONG l;
2318 l = RegCreateKeyExW(parent, instancePath, 0, NULL, 0,
2319 samDesired, NULL, &key, NULL);
2320 if (l)
2322 SetLastError(l);
2323 key = INVALID_HANDLE_VALUE;
2325 else if (InfHandle)
2326 FIXME("INF section installation unsupported\n");
2328 HeapFree(GetProcessHeap(), 0, instancePath);
2329 RegCloseKey(parent);
2331 else
2332 SetLastError(l);
2333 RegCloseKey(interfacesKey);
2335 else
2336 SetLastError(l);
2337 return key;
2340 /***********************************************************************
2341 * SetupDiDeleteDeviceInterfaceRegKey (SETUPAPI.@)
2343 BOOL WINAPI SetupDiDeleteDeviceInterfaceRegKey(
2344 HDEVINFO DeviceInfoSet,
2345 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2346 DWORD Reserved)
2348 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2349 HKEY parent;
2350 BOOL ret = FALSE;
2352 TRACE("%p %p %d\n", DeviceInfoSet, DeviceInterfaceData, Reserved);
2354 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2355 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2357 SetLastError(ERROR_INVALID_HANDLE);
2358 return FALSE;
2360 if (!DeviceInterfaceData ||
2361 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2362 !DeviceInterfaceData->Reserved)
2364 SetLastError(ERROR_INVALID_PARAMETER);
2365 return FALSE;
2367 parent = SetupDiOpenClassRegKeyExW(&DeviceInterfaceData->InterfaceClassGuid,
2368 KEY_ALL_ACCESS, DIOCR_INTERFACE, NULL, NULL);
2369 if (parent != INVALID_HANDLE_VALUE)
2371 struct InterfaceInfo *ifaceInfo =
2372 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2373 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2375 if (instancePath)
2377 LONG l = RegDeleteKeyW(parent, instancePath);
2379 if (l)
2380 SetLastError(l);
2381 else
2382 ret = TRUE;
2383 HeapFree(GetProcessHeap(), 0, instancePath);
2385 RegCloseKey(parent);
2387 return ret;
2390 /***********************************************************************
2391 * SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2393 * PARAMS
2394 * DeviceInfoSet [I] Set of devices from which to enumerate
2395 * interfaces
2396 * DeviceInfoData [I] (Optional) If specified, a specific device
2397 * instance from which to enumerate interfaces.
2398 * If it isn't specified, all interfaces for all
2399 * devices in the set are enumerated.
2400 * InterfaceClassGuid [I] The interface class to enumerate.
2401 * MemberIndex [I] An index of the interface instance to enumerate.
2402 * A caller should start with MemberIndex set to 0,
2403 * and continue until the function fails with
2404 * ERROR_NO_MORE_ITEMS.
2405 * DeviceInterfaceData [I/O] Returns an enumerated interface. Its cbSize
2406 * member must be set to
2407 * sizeof(SP_DEVICE_INTERFACE_DATA).
2409 * RETURNS
2410 * Success: non-zero value.
2411 * Failure: FALSE. Call GetLastError() for more info.
2413 BOOL WINAPI SetupDiEnumDeviceInterfaces(
2414 HDEVINFO DeviceInfoSet,
2415 PSP_DEVINFO_DATA DeviceInfoData,
2416 CONST GUID * InterfaceClassGuid,
2417 DWORD MemberIndex,
2418 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2420 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2421 BOOL ret = FALSE;
2423 TRACE("%p, %p, %s, %d, %p\n", DeviceInfoSet, DeviceInfoData,
2424 debugstr_guid(InterfaceClassGuid), MemberIndex, DeviceInterfaceData);
2426 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2427 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2429 SetLastError(ERROR_INVALID_HANDLE);
2430 return FALSE;
2432 if (DeviceInfoData && (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA) ||
2433 !DeviceInfoData->Reserved))
2435 SetLastError(ERROR_INVALID_PARAMETER);
2436 return FALSE;
2438 if (!DeviceInterfaceData ||
2439 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2441 SetLastError(ERROR_INVALID_PARAMETER);
2442 return FALSE;
2444 if (DeviceInfoData)
2446 struct DeviceInfo *devInfo =
2447 (struct DeviceInfo *)DeviceInfoData->Reserved;
2448 struct InterfaceInstances *iface;
2450 if ((ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
2452 if (MemberIndex < iface->cInstances)
2453 memcpy(DeviceInterfaceData, &iface->instances[MemberIndex],
2454 sizeof(SP_DEVICE_INTERFACE_DATA));
2455 else
2457 SetLastError(ERROR_NO_MORE_ITEMS);
2458 ret = FALSE;
2461 else
2462 SetLastError(ERROR_NO_MORE_ITEMS);
2464 else
2466 DWORD i, cEnumerated = 0;
2467 BOOL found = FALSE;
2469 for (i = 0; !found && cEnumerated < MemberIndex + 1 &&
2470 i < set->cDevices; i++)
2472 struct DeviceInfo *devInfo =
2473 (struct DeviceInfo *)set->devices[i].Reserved;
2474 struct InterfaceInstances *iface;
2476 if (SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface))
2478 if (cEnumerated + iface->cInstances < MemberIndex + 1)
2479 cEnumerated += iface->cInstances;
2480 else
2482 DWORD instanceIndex = MemberIndex - cEnumerated;
2484 memcpy(DeviceInterfaceData,
2485 &iface->instances[instanceIndex],
2486 sizeof(SP_DEVICE_INTERFACE_DATA));
2487 cEnumerated += instanceIndex + 1;
2488 found = TRUE;
2489 ret = TRUE;
2493 if (!found)
2494 SetLastError(ERROR_NO_MORE_ITEMS);
2496 return ret;
2499 /***********************************************************************
2500 * SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2502 * Destroy a DeviceInfoList and free all used memory of the list.
2504 * PARAMS
2505 * devinfo [I] DeviceInfoList pointer to list to destroy
2507 * RETURNS
2508 * Success: non zero value.
2509 * Failure: zero value.
2511 BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
2513 BOOL ret = FALSE;
2515 TRACE("%p\n", devinfo);
2516 if (devinfo && devinfo != (HDEVINFO)INVALID_HANDLE_VALUE)
2518 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
2520 if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
2522 DWORD i;
2524 for (i = 0; i < list->cDevices; i++)
2525 SETUPDI_FreeDeviceInfo(
2526 (struct DeviceInfo *)list->devices[i].Reserved);
2527 HeapFree(GetProcessHeap(), 0, list->devices);
2528 HeapFree(GetProcessHeap(), 0, list);
2529 ret = TRUE;
2533 if (ret == FALSE)
2534 SetLastError(ERROR_INVALID_HANDLE);
2536 return ret;
2539 /***********************************************************************
2540 * SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2542 BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
2543 HDEVINFO DeviceInfoSet,
2544 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2545 PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
2546 DWORD DeviceInterfaceDetailDataSize,
2547 PDWORD RequiredSize,
2548 PSP_DEVINFO_DATA DeviceInfoData)
2550 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2551 struct InterfaceInfo *info;
2552 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath)
2553 + 1;
2554 BOOL ret = FALSE;
2556 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2557 DeviceInterfaceData, DeviceInterfaceDetailData,
2558 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2560 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2561 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2563 SetLastError(ERROR_INVALID_HANDLE);
2564 return FALSE;
2566 if (!DeviceInterfaceData ||
2567 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2568 !DeviceInterfaceData->Reserved)
2570 SetLastError(ERROR_INVALID_PARAMETER);
2571 return FALSE;
2573 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
2574 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath) + sizeof(char) ||
2575 DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A)))
2577 SetLastError(ERROR_INVALID_USER_BUFFER);
2578 return FALSE;
2580 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2582 SetLastError(ERROR_INVALID_USER_BUFFER);
2583 return FALSE;
2585 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2586 if (info->symbolicLink)
2587 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2588 NULL, 0, NULL, NULL);
2589 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2591 if (info->symbolicLink)
2592 WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2593 DeviceInterfaceDetailData->DevicePath,
2594 DeviceInterfaceDetailDataSize -
2595 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath),
2596 NULL, NULL);
2597 else
2598 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2599 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2600 memcpy(DeviceInfoData, info->device, sizeof(SP_DEVINFO_DATA));
2601 ret = TRUE;
2603 else
2605 if (RequiredSize)
2606 *RequiredSize = bytesNeeded;
2607 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2609 return ret;
2612 /***********************************************************************
2613 * SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
2615 BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
2616 HDEVINFO DeviceInfoSet,
2617 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2618 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
2619 DWORD DeviceInterfaceDetailDataSize,
2620 PDWORD RequiredSize,
2621 PSP_DEVINFO_DATA DeviceInfoData)
2623 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2624 struct InterfaceInfo *info;
2625 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)
2626 + sizeof(WCHAR); /* include NULL terminator */
2627 BOOL ret = FALSE;
2629 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2630 DeviceInterfaceData, DeviceInterfaceDetailData,
2631 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2633 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE ||
2634 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2636 SetLastError(ERROR_INVALID_HANDLE);
2637 return FALSE;
2639 if (!DeviceInterfaceData ||
2640 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2641 !DeviceInterfaceData->Reserved)
2643 SetLastError(ERROR_INVALID_PARAMETER);
2644 return FALSE;
2646 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
2647 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR) ||
2648 DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W)))
2650 SetLastError(ERROR_INVALID_USER_BUFFER);
2651 return FALSE;
2653 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2655 SetLastError(ERROR_INVALID_USER_BUFFER);
2656 return FALSE;
2658 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2659 if (info->symbolicLink)
2660 bytesNeeded += lstrlenW(info->symbolicLink);
2661 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2663 if (info->symbolicLink)
2664 lstrcpyW(DeviceInterfaceDetailData->DevicePath, info->symbolicLink);
2665 else
2666 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2667 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2668 memcpy(DeviceInfoData, info->device, sizeof(SP_DEVINFO_DATA));
2669 ret = TRUE;
2671 else
2673 if (RequiredSize)
2674 *RequiredSize = bytesNeeded;
2675 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2677 return ret;
2680 struct PropertyMapEntry
2682 DWORD regType;
2683 LPCSTR nameA;
2684 LPCWSTR nameW;
2687 static struct PropertyMapEntry PropertyMap[] = {
2688 { REG_SZ, "DeviceDesc", DeviceDesc },
2689 { REG_MULTI_SZ, "HardwareId", HardwareId },
2690 { REG_MULTI_SZ, "CompatibleIDs", CompatibleIDs },
2691 { 0, NULL, NULL }, /* SPDRP_UNUSED0 */
2692 { REG_SZ, "Service", Service },
2693 { 0, NULL, NULL }, /* SPDRP_UNUSED1 */
2694 { 0, NULL, NULL }, /* SPDRP_UNUSED2 */
2695 { REG_SZ, "Class", Class },
2696 { REG_SZ, "ClassGUID", ClassGUID },
2697 { REG_SZ, "Driver", Driver },
2698 { REG_DWORD, "ConfigFlags", ConfigFlags },
2699 { REG_SZ, "Mfg", Mfg },
2700 { REG_SZ, "FriendlyName", FriendlyName },
2701 { REG_SZ, "LocationInformation", LocationInformation },
2702 { 0, NULL, NULL }, /* SPDRP_PHYSICAL_DEVICE_OBJECT_NAME */
2703 { REG_DWORD, "Capabilities", Capabilities },
2704 { REG_DWORD, "UINumber", UINumber },
2705 { REG_MULTI_SZ, "UpperFilters", UpperFilters },
2706 { REG_MULTI_SZ, "LowerFilters", LowerFilters },
2709 /***********************************************************************
2710 * SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
2712 BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
2713 HDEVINFO DeviceInfoSet,
2714 PSP_DEVINFO_DATA DeviceInfoData,
2715 DWORD Property,
2716 PDWORD PropertyRegDataType,
2717 PBYTE PropertyBuffer,
2718 DWORD PropertyBufferSize,
2719 PDWORD RequiredSize)
2721 BOOL ret = FALSE;
2722 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2723 struct DeviceInfo *devInfo;
2725 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
2726 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2727 RequiredSize);
2729 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2731 SetLastError(ERROR_INVALID_HANDLE);
2732 return FALSE;
2734 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2736 SetLastError(ERROR_INVALID_HANDLE);
2737 return FALSE;
2739 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2740 || !DeviceInfoData->Reserved)
2742 SetLastError(ERROR_INVALID_PARAMETER);
2743 return FALSE;
2745 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2746 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2747 && PropertyMap[Property].nameA)
2749 DWORD size = PropertyBufferSize;
2750 LONG l = RegQueryValueExA(devInfo->key, PropertyMap[Property].nameA,
2751 NULL, PropertyRegDataType, PropertyBuffer, &size);
2753 if (RequiredSize)
2754 *RequiredSize = size;
2755 if (!l)
2756 ret = TRUE;
2757 else
2758 SetLastError(l);
2760 return ret;
2763 /***********************************************************************
2764 * SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
2766 BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
2767 HDEVINFO DeviceInfoSet,
2768 PSP_DEVINFO_DATA DeviceInfoData,
2769 DWORD Property,
2770 PDWORD PropertyRegDataType,
2771 PBYTE PropertyBuffer,
2772 DWORD PropertyBufferSize,
2773 PDWORD RequiredSize)
2775 BOOL ret = FALSE;
2776 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2777 struct DeviceInfo *devInfo;
2779 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
2780 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2781 RequiredSize);
2783 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2785 SetLastError(ERROR_INVALID_HANDLE);
2786 return FALSE;
2788 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2790 SetLastError(ERROR_INVALID_HANDLE);
2791 return FALSE;
2793 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2794 || !DeviceInfoData->Reserved)
2796 SetLastError(ERROR_INVALID_PARAMETER);
2797 return FALSE;
2799 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2800 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2801 && PropertyMap[Property].nameW)
2803 DWORD size = PropertyBufferSize;
2804 LONG l = RegQueryValueExW(devInfo->key, PropertyMap[Property].nameW,
2805 NULL, PropertyRegDataType, PropertyBuffer, &size);
2807 if (RequiredSize)
2808 *RequiredSize = size;
2809 if (!l)
2810 ret = TRUE;
2811 else
2812 SetLastError(l);
2814 return ret;
2817 /***********************************************************************
2818 * SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
2820 BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
2821 HDEVINFO DeviceInfoSet,
2822 PSP_DEVINFO_DATA DeviceInfoData,
2823 DWORD Property,
2824 const BYTE *PropertyBuffer,
2825 DWORD PropertyBufferSize)
2827 BOOL ret = FALSE;
2828 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2829 struct DeviceInfo *devInfo;
2831 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
2832 PropertyBuffer, PropertyBufferSize);
2834 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2836 SetLastError(ERROR_INVALID_HANDLE);
2837 return FALSE;
2839 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2841 SetLastError(ERROR_INVALID_HANDLE);
2842 return FALSE;
2844 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2845 || !DeviceInfoData->Reserved)
2847 SetLastError(ERROR_INVALID_PARAMETER);
2848 return FALSE;
2850 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2851 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2852 && PropertyMap[Property].nameA)
2854 LONG l = RegSetValueExA(devInfo->key, PropertyMap[Property].nameA, 0,
2855 PropertyMap[Property].regType, PropertyBuffer,
2856 PropertyBufferSize);
2857 if (!l)
2858 ret = TRUE;
2859 else
2860 SetLastError(l);
2862 return ret;
2865 /***********************************************************************
2866 * SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
2868 BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
2869 HDEVINFO DeviceInfoSet,
2870 PSP_DEVINFO_DATA DeviceInfoData,
2871 DWORD Property,
2872 const BYTE *PropertyBuffer,
2873 DWORD PropertyBufferSize)
2875 BOOL ret = FALSE;
2876 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2877 struct DeviceInfo *devInfo;
2879 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
2880 PropertyBuffer, PropertyBufferSize);
2882 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2884 SetLastError(ERROR_INVALID_HANDLE);
2885 return FALSE;
2887 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2889 SetLastError(ERROR_INVALID_HANDLE);
2890 return FALSE;
2892 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2893 || !DeviceInfoData->Reserved)
2895 SetLastError(ERROR_INVALID_PARAMETER);
2896 return FALSE;
2898 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2899 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
2900 && PropertyMap[Property].nameW)
2902 LONG l = RegSetValueExW(devInfo->key, PropertyMap[Property].nameW, 0,
2903 PropertyMap[Property].regType, PropertyBuffer,
2904 PropertyBufferSize);
2905 if (!l)
2906 ret = TRUE;
2907 else
2908 SetLastError(l);
2910 return ret;
2913 /***********************************************************************
2914 * SetupDiInstallClassA (SETUPAPI.@)
2916 BOOL WINAPI SetupDiInstallClassA(
2917 HWND hwndParent,
2918 PCSTR InfFileName,
2919 DWORD Flags,
2920 HSPFILEQ FileQueue)
2922 UNICODE_STRING FileNameW;
2923 BOOL Result;
2925 if (!RtlCreateUnicodeStringFromAsciiz(&FileNameW, InfFileName))
2927 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2928 return FALSE;
2931 Result = SetupDiInstallClassW(hwndParent, FileNameW.Buffer, Flags, FileQueue);
2933 RtlFreeUnicodeString(&FileNameW);
2935 return Result;
2938 static HKEY CreateClassKey(HINF hInf)
2940 WCHAR FullBuffer[MAX_PATH];
2941 WCHAR Buffer[MAX_PATH];
2942 DWORD RequiredSize;
2943 HKEY hClassKey;
2945 if (!SetupGetLineTextW(NULL,
2946 hInf,
2947 Version,
2948 ClassGUID,
2949 Buffer,
2950 MAX_PATH,
2951 &RequiredSize))
2953 return INVALID_HANDLE_VALUE;
2956 lstrcpyW(FullBuffer, ControlClass);
2957 lstrcatW(FullBuffer, Buffer);
2959 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
2960 FullBuffer,
2962 KEY_ALL_ACCESS,
2963 &hClassKey))
2965 if (!SetupGetLineTextW(NULL,
2966 hInf,
2967 Version,
2968 Class,
2969 Buffer,
2970 MAX_PATH,
2971 &RequiredSize))
2973 return INVALID_HANDLE_VALUE;
2976 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
2977 FullBuffer,
2979 NULL,
2980 REG_OPTION_NON_VOLATILE,
2981 KEY_ALL_ACCESS,
2982 NULL,
2983 &hClassKey,
2984 NULL))
2986 return INVALID_HANDLE_VALUE;
2991 if (RegSetValueExW(hClassKey,
2992 Class,
2994 REG_SZ,
2995 (LPBYTE)Buffer,
2996 RequiredSize * sizeof(WCHAR)))
2998 RegCloseKey(hClassKey);
2999 RegDeleteKeyW(HKEY_LOCAL_MACHINE,
3000 FullBuffer);
3001 return INVALID_HANDLE_VALUE;
3004 return hClassKey;
3007 /***********************************************************************
3008 * SetupDiInstallClassW (SETUPAPI.@)
3010 BOOL WINAPI SetupDiInstallClassW(
3011 HWND hwndParent,
3012 PCWSTR InfFileName,
3013 DWORD Flags,
3014 HSPFILEQ FileQueue)
3016 WCHAR SectionName[MAX_PATH];
3017 DWORD SectionNameLength = 0;
3018 HINF hInf;
3019 BOOL bFileQueueCreated = FALSE;
3020 HKEY hClassKey;
3023 FIXME("\n");
3025 if ((Flags & DI_NOVCP) && (FileQueue == NULL || FileQueue == INVALID_HANDLE_VALUE))
3027 SetLastError(ERROR_INVALID_PARAMETER);
3028 return FALSE;
3031 /* Open the .inf file */
3032 hInf = SetupOpenInfFileW(InfFileName,
3033 NULL,
3034 INF_STYLE_WIN4,
3035 NULL);
3036 if (hInf == INVALID_HANDLE_VALUE)
3039 return FALSE;
3042 /* Create or open the class registry key 'HKLM\\CurrentControlSet\\Class\\{GUID}' */
3043 hClassKey = CreateClassKey(hInf);
3044 if (hClassKey == INVALID_HANDLE_VALUE)
3046 SetupCloseInfFile(hInf);
3047 return FALSE;
3051 /* Try to append a layout file */
3052 #if 0
3053 SetupOpenAppendInfFileW(NULL, hInf, NULL);
3054 #endif
3056 /* Retrieve the actual section name */
3057 SetupDiGetActualSectionToInstallW(hInf,
3058 ClassInstall32,
3059 SectionName,
3060 MAX_PATH,
3061 &SectionNameLength,
3062 NULL);
3064 #if 0
3065 if (!(Flags & DI_NOVCP))
3067 FileQueue = SetupOpenFileQueue();
3068 if (FileQueue == INVALID_HANDLE_VALUE)
3070 SetupCloseInfFile(hInf);
3071 return FALSE;
3074 bFileQueueCreated = TRUE;
3077 #endif
3079 SetupInstallFromInfSectionW(NULL,
3080 hInf,
3081 SectionName,
3082 SPINST_REGISTRY,
3083 hClassKey,
3084 NULL,
3086 NULL,
3087 NULL,
3088 INVALID_HANDLE_VALUE,
3089 NULL);
3091 /* FIXME: More code! */
3093 if (bFileQueueCreated)
3094 SetupCloseFileQueue(FileQueue);
3096 SetupCloseInfFile(hInf);
3098 return TRUE;
3102 /***********************************************************************
3103 * SetupDiOpenClassRegKey (SETUPAPI.@)
3105 HKEY WINAPI SetupDiOpenClassRegKey(
3106 const GUID* ClassGuid,
3107 REGSAM samDesired)
3109 return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3110 DIOCR_INSTALLER, NULL, NULL);
3114 /***********************************************************************
3115 * SetupDiOpenClassRegKeyExA (SETUPAPI.@)
3117 HKEY WINAPI SetupDiOpenClassRegKeyExA(
3118 const GUID* ClassGuid,
3119 REGSAM samDesired,
3120 DWORD Flags,
3121 PCSTR MachineName,
3122 PVOID Reserved)
3124 PWSTR MachineNameW = NULL;
3125 HKEY hKey;
3127 TRACE("\n");
3129 if (MachineName)
3131 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
3132 if (MachineNameW == NULL)
3133 return INVALID_HANDLE_VALUE;
3136 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3137 Flags, MachineNameW, Reserved);
3139 MyFree(MachineNameW);
3141 return hKey;
3145 /***********************************************************************
3146 * SetupDiOpenClassRegKeyExW (SETUPAPI.@)
3148 HKEY WINAPI SetupDiOpenClassRegKeyExW(
3149 const GUID* ClassGuid,
3150 REGSAM samDesired,
3151 DWORD Flags,
3152 PCWSTR MachineName,
3153 PVOID Reserved)
3155 HKEY hClassesKey;
3156 HKEY key;
3157 LPCWSTR lpKeyName;
3158 LONG l;
3160 if (MachineName != NULL)
3162 FIXME("Remote access not supported yet!\n");
3163 return INVALID_HANDLE_VALUE;
3166 if (Flags == DIOCR_INSTALLER)
3168 lpKeyName = ControlClass;
3170 else if (Flags == DIOCR_INTERFACE)
3172 lpKeyName = DeviceClasses;
3174 else
3176 ERR("Invalid Flags parameter!\n");
3177 SetLastError(ERROR_INVALID_PARAMETER);
3178 return INVALID_HANDLE_VALUE;
3181 if (!ClassGuid)
3183 if ((l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3184 lpKeyName,
3186 samDesired,
3187 &hClassesKey)))
3189 SetLastError(l);
3190 hClassesKey = INVALID_HANDLE_VALUE;
3192 key = hClassesKey;
3194 else
3196 WCHAR bracedGuidString[39];
3198 SETUPDI_GuidToString(ClassGuid, bracedGuidString);
3200 if (!(l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3201 lpKeyName,
3203 samDesired,
3204 &hClassesKey)))
3206 if ((l = RegOpenKeyExW(hClassesKey,
3207 bracedGuidString,
3209 samDesired,
3210 &key)))
3212 SetLastError(l);
3213 key = INVALID_HANDLE_VALUE;
3215 RegCloseKey(hClassesKey);
3217 else
3219 SetLastError(l);
3220 key = INVALID_HANDLE_VALUE;
3223 return key;
3226 /***********************************************************************
3227 * SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3229 BOOL WINAPI SetupDiOpenDeviceInterfaceW(
3230 HDEVINFO DeviceInfoSet,
3231 PCWSTR DevicePath,
3232 DWORD OpenFlags,
3233 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3235 FIXME("%p %s %08x %p\n",
3236 DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
3237 return FALSE;
3240 /***********************************************************************
3241 * SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3243 BOOL WINAPI SetupDiOpenDeviceInterfaceA(
3244 HDEVINFO DeviceInfoSet,
3245 PCSTR DevicePath,
3246 DWORD OpenFlags,
3247 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3249 FIXME("%p %s %08x %p\n", DeviceInfoSet,
3250 debugstr_a(DevicePath), OpenFlags, DeviceInterfaceData);
3251 return FALSE;
3254 /***********************************************************************
3255 * SetupDiSetClassInstallParamsA (SETUPAPI.@)
3257 BOOL WINAPI SetupDiSetClassInstallParamsA(
3258 HDEVINFO DeviceInfoSet,
3259 PSP_DEVINFO_DATA DeviceInfoData,
3260 PSP_CLASSINSTALL_HEADER ClassInstallParams,
3261 DWORD ClassInstallParamsSize)
3263 FIXME("%p %p %x %u\n",DeviceInfoSet, DeviceInfoData,
3264 ClassInstallParams->InstallFunction, ClassInstallParamsSize);
3265 return FALSE;
3268 /***********************************************************************
3269 * SetupDiCallClassInstaller (SETUPAPI.@)
3271 BOOL WINAPI SetupDiCallClassInstaller(
3272 DI_FUNCTION InstallFunction,
3273 HDEVINFO DeviceInfoSet,
3274 PSP_DEVINFO_DATA DeviceInfoData)
3276 FIXME("%d %p %p\n", InstallFunction, DeviceInfoSet, DeviceInfoData);
3277 return FALSE;
3280 /***********************************************************************
3281 * SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
3283 BOOL WINAPI SetupDiGetDeviceInstallParamsA(
3284 HDEVINFO DeviceInfoSet,
3285 PSP_DEVINFO_DATA DeviceInfoData,
3286 PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
3288 FIXME("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
3289 return FALSE;
3292 /***********************************************************************
3293 * SetupDiOpenDevRegKey (SETUPAPI.@)
3295 HKEY WINAPI SetupDiOpenDevRegKey(
3296 HDEVINFO DeviceInfoSet,
3297 PSP_DEVINFO_DATA DeviceInfoData,
3298 DWORD Scope,
3299 DWORD HwProfile,
3300 DWORD KeyType,
3301 REGSAM samDesired)
3303 FIXME("%p %p %d %d %d %x\n", DeviceInfoSet, DeviceInfoData,
3304 Scope, HwProfile, KeyType, samDesired);
3305 return INVALID_HANDLE_VALUE;