msi: Break out the publishing tests into their respective actions.
[wine.git] / dlls / setupapi / devinst.c
blobc90170bc0e3f477112d5e5c32a58c8c257b8ba66
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 DWORD devId;
131 LPWSTR instanceId;
132 struct list interfaces;
135 static void SETUPDI_GuidToString(const GUID *guid, LPWSTR guidStr)
137 static const WCHAR fmt[] = {'{','%','0','8','X','-','%','0','4','X','-',
138 '%','0','4','X','-','%','0','2','X','%','0','2','X','-','%','0','2',
139 'X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%',
140 '0','2','X','}',0};
142 sprintfW(guidStr, fmt, guid->Data1, guid->Data2, guid->Data3,
143 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
144 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
147 static void SETUPDI_FreeInterfaceInstances(struct InterfaceInstances *instances)
149 DWORD i;
151 for (i = 0; i < instances->cInstances; i++)
153 struct InterfaceInfo *ifaceInfo =
154 (struct InterfaceInfo *)instances->instances[i].Reserved;
156 if (ifaceInfo->device && ifaceInfo->device->Reserved)
158 struct DeviceInfo *devInfo =
159 (struct DeviceInfo *)ifaceInfo->device->Reserved;
161 if (devInfo->phantom)
162 SetupDiDeleteDeviceInterfaceRegKey(devInfo->set,
163 &instances->instances[i], 0);
165 HeapFree(GetProcessHeap(), 0, ifaceInfo->referenceString);
166 HeapFree(GetProcessHeap(), 0, ifaceInfo->symbolicLink);
167 HeapFree(GetProcessHeap(), 0, ifaceInfo);
169 HeapFree(GetProcessHeap(), 0, instances->instances);
172 /* Finds the interface with interface class InterfaceClassGuid in the device.
173 * Returns TRUE if found, and updates *interface to point to device's
174 * interfaces member where the given interface was found.
175 * Returns FALSE if not found.
177 static BOOL SETUPDI_FindInterface(const struct DeviceInfo *devInfo,
178 const GUID *InterfaceClassGuid, struct InterfaceInstances **interface)
180 BOOL found = FALSE;
181 struct InterfaceInstances *iface;
183 TRACE("%s\n", debugstr_guid(InterfaceClassGuid));
185 LIST_FOR_EACH_ENTRY(iface, &devInfo->interfaces, struct InterfaceInstances,
186 entry)
188 if (IsEqualGUID(&iface->guid, InterfaceClassGuid))
190 *interface = iface;
191 found = TRUE;
192 break;
195 TRACE("returning %d (%p)\n", found, found ? *interface : NULL);
196 return found;
199 /* Finds the interface instance with reference string ReferenceString in the
200 * interface instance map. Returns TRUE if found, and updates instanceIndex to
201 * the index of the interface instance's instances member
202 * where the given instance was found. Returns FALSE if not found.
204 static BOOL SETUPDI_FindInterfaceInstance(
205 const struct InterfaceInstances *instances,
206 LPCWSTR ReferenceString, DWORD *instanceIndex)
208 BOOL found = FALSE;
209 DWORD i;
211 TRACE("%s\n", debugstr_w(ReferenceString));
213 for (i = 0; !found && i < instances->cInstances; i++)
215 SP_DEVICE_INTERFACE_DATA *ifaceData = &instances->instances[i];
216 struct InterfaceInfo *ifaceInfo =
217 (struct InterfaceInfo *)ifaceData->Reserved;
219 if (!ReferenceString && !ifaceInfo->referenceString)
221 *instanceIndex = i;
222 found = TRUE;
224 else if (ReferenceString && ifaceInfo->referenceString &&
225 !lstrcmpiW(ifaceInfo->referenceString, ReferenceString))
227 *instanceIndex = i;
228 found = TRUE;
231 TRACE("returning %d (%d)\n", found, found ? *instanceIndex : 0);
232 return found;
235 static LPWSTR SETUPDI_CreateSymbolicLinkPath(LPCWSTR instanceId,
236 const GUID *InterfaceClassGuid, LPCWSTR ReferenceString)
238 static const WCHAR fmt[] = {'\\','\\','?','\\','%','s','#','%','s',0};
239 WCHAR guidStr[39];
240 DWORD len;
241 LPWSTR ret;
243 SETUPDI_GuidToString(InterfaceClassGuid, guidStr);
244 /* omit length of format specifiers, but include NULL terminator: */
245 len = lstrlenW(fmt) - 4 + 1;
246 len += lstrlenW(instanceId) + lstrlenW(guidStr);
247 if (ReferenceString)
249 /* space for a hash between string and reference string: */
250 len += lstrlenW(ReferenceString) + 1;
252 ret = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
253 if (ret)
255 int printed = sprintfW(ret, fmt, instanceId, guidStr);
256 LPWSTR ptr;
258 /* replace '\\' with '#' after the "\\\\?\\" beginning */
259 for (ptr = strchrW(ret + 4, '\\'); ptr; ptr = strchrW(ptr + 1, '\\'))
260 *ptr = '#';
261 if (ReferenceString)
263 ret[printed - 1] = '\\';
264 lstrcpyW(ret + printed, ReferenceString);
267 return ret;
270 /* Adds an interface with the given interface class and reference string to
271 * the device, if it doesn't already exist in the device. If iface is not
272 * NULL, returns a pointer to the newly added (or already existing) interface.
274 static BOOL SETUPDI_AddInterfaceInstance(PSP_DEVINFO_DATA DeviceInfoData,
275 const GUID *InterfaceClassGuid, LPCWSTR ReferenceString,
276 SP_DEVICE_INTERFACE_DATA **ifaceData)
278 struct DeviceInfo *devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
279 BOOL newInterface = FALSE, ret;
280 struct InterfaceInstances *iface = NULL;
282 TRACE("%p %s %s %p\n", devInfo, debugstr_guid(InterfaceClassGuid),
283 debugstr_w(ReferenceString), iface);
285 if (!(ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
287 iface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
288 sizeof(struct InterfaceInstances));
289 if (iface)
291 list_add_tail(&devInfo->interfaces, &iface->entry);
292 newInterface = TRUE;
295 if (iface)
297 DWORD instanceIndex = 0;
299 if (!(ret = SETUPDI_FindInterfaceInstance(iface, ReferenceString,
300 &instanceIndex)))
302 SP_DEVICE_INTERFACE_DATA *instance = NULL;
304 if (!iface->cInstancesAllocated)
306 iface->instances = HeapAlloc(GetProcessHeap(), 0,
307 sizeof(SP_DEVICE_INTERFACE_DATA));
308 if (iface->instances)
309 instance = &iface->instances[iface->cInstancesAllocated++];
311 else if (iface->cInstances == iface->cInstancesAllocated)
313 iface->instances = HeapReAlloc(GetProcessHeap(), 0,
314 iface->instances,
315 (iface->cInstancesAllocated + 1) *
316 sizeof(SP_DEVICE_INTERFACE_DATA));
317 if (iface->instances)
318 instance = &iface->instances[iface->cInstancesAllocated++];
320 else
321 instance = &iface->instances[iface->cInstances];
322 if (instance)
324 struct InterfaceInfo *ifaceInfo = HeapAlloc(GetProcessHeap(),
325 0, sizeof(struct InterfaceInfo));
327 if (ifaceInfo)
329 ret = TRUE;
330 ifaceInfo->device = DeviceInfoData;
331 ifaceInfo->symbolicLink = SETUPDI_CreateSymbolicLinkPath(
332 devInfo->instanceId, InterfaceClassGuid,
333 ReferenceString);
334 if (ReferenceString)
336 ifaceInfo->referenceString =
337 HeapAlloc(GetProcessHeap(), 0,
338 (lstrlenW(ReferenceString) + 1) *
339 sizeof(WCHAR));
340 if (ifaceInfo->referenceString)
341 lstrcpyW(ifaceInfo->referenceString,
342 ReferenceString);
343 else
344 ret = FALSE;
346 else
347 ifaceInfo->referenceString = NULL;
348 if (ret)
350 HKEY key;
352 iface->cInstances++;
353 instance->cbSize =
354 sizeof(SP_DEVICE_INTERFACE_DATA);
355 memcpy(&instance->InterfaceClassGuid,
356 InterfaceClassGuid, sizeof(GUID));
357 instance->Flags = SPINT_ACTIVE; /* FIXME */
358 instance->Reserved = (ULONG_PTR)ifaceInfo;
359 if (newInterface)
360 memcpy(&iface->guid, InterfaceClassGuid,
361 sizeof(GUID));
362 key = SetupDiCreateDeviceInterfaceRegKeyW(devInfo->set,
363 instance, 0, KEY_WRITE, NULL, NULL);
364 if (key != INVALID_HANDLE_VALUE)
366 RegSetValueExW(key, SymbolicLink, 0, REG_SZ,
367 (BYTE *)ifaceInfo->symbolicLink,
368 lstrlenW(ifaceInfo->symbolicLink) *
369 sizeof(WCHAR));
370 RegCloseKey(key);
372 if (ifaceData)
373 *ifaceData = instance;
375 else
376 HeapFree(GetProcessHeap(), 0, ifaceInfo);
380 else
382 if (ifaceData)
383 *ifaceData = &iface->instances[instanceIndex];
386 else
387 ret = FALSE;
388 TRACE("returning %d\n", ret);
389 return ret;
392 static BOOL SETUPDI_SetInterfaceSymbolicLink(SP_DEVICE_INTERFACE_DATA *iface,
393 LPCWSTR symbolicLink)
395 struct InterfaceInfo *info = (struct InterfaceInfo *)iface->Reserved;
396 BOOL ret = FALSE;
398 if (info)
400 HeapFree(GetProcessHeap(), 0, info->symbolicLink);
401 info->symbolicLink = HeapAlloc(GetProcessHeap(), 0,
402 (lstrlenW(symbolicLink) + 1) * sizeof(WCHAR));
403 if (info->symbolicLink)
405 lstrcpyW(info->symbolicLink, symbolicLink);
406 ret = TRUE;
409 return ret;
412 static HKEY SETUPDI_CreateDevKey(struct DeviceInfo *devInfo)
414 HKEY enumKey, key = INVALID_HANDLE_VALUE;
415 LONG l;
417 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
418 NULL, &enumKey, NULL);
419 if (!l)
421 RegCreateKeyExW(enumKey, devInfo->instanceId, 0, NULL, 0,
422 KEY_READ | KEY_WRITE, NULL, &key, NULL);
423 RegCloseKey(enumKey);
425 return key;
428 static HKEY SETUPDI_CreateDrvKey(struct DeviceInfo *devInfo)
430 static const WCHAR slash[] = { '\\',0 };
431 WCHAR classKeyPath[MAX_PATH];
432 HKEY classKey, key = INVALID_HANDLE_VALUE;
433 LONG l;
435 lstrcpyW(classKeyPath, ControlClass);
436 lstrcatW(classKeyPath, slash);
437 SETUPDI_GuidToString(&devInfo->set->ClassGuid,
438 classKeyPath + lstrlenW(classKeyPath));
439 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
440 KEY_ALL_ACCESS, NULL, &classKey, NULL);
441 if (!l)
443 static const WCHAR fmt[] = { '%','0','4','u',0 };
444 WCHAR devId[10];
446 sprintfW(devId, fmt, devInfo->devId);
447 RegCreateKeyExW(classKey, devId, 0, NULL, 0, KEY_READ | KEY_WRITE,
448 NULL, &key, NULL);
449 RegCloseKey(classKey);
451 return key;
454 static struct DeviceInfo *SETUPDI_AllocateDeviceInfo(struct DeviceInfoSet *set,
455 DWORD devId, LPCWSTR instanceId, BOOL phantom)
457 struct DeviceInfo *devInfo = NULL;
458 HANDLE devInst = GlobalAlloc(GMEM_FIXED, sizeof(struct DeviceInfo));
459 if (devInst)
460 devInfo = GlobalLock(devInst);
462 if (devInfo)
464 devInfo->set = set;
465 devInfo->devId = (DWORD)devInst;
467 devInfo->instanceId = HeapAlloc(GetProcessHeap(), 0,
468 (lstrlenW(instanceId) + 1) * sizeof(WCHAR));
469 if (devInfo->instanceId)
471 devInfo->key = INVALID_HANDLE_VALUE;
472 devInfo->phantom = phantom;
473 lstrcpyW(devInfo->instanceId, instanceId);
474 struprW(devInfo->instanceId);
475 devInfo->key = SETUPDI_CreateDevKey(devInfo);
476 if (devInfo->key != INVALID_HANDLE_VALUE)
478 if (phantom)
479 RegSetValueExW(devInfo->key, Phantom, 0, REG_DWORD,
480 (LPBYTE)&phantom, sizeof(phantom));
482 list_init(&devInfo->interfaces);
483 GlobalUnlock(devInst);
485 else
487 GlobalUnlock(devInst);
488 GlobalFree(devInst);
489 devInfo = NULL;
492 return devInfo;
495 static void SETUPDI_FreeDeviceInfo(struct DeviceInfo *devInfo)
497 struct InterfaceInstances *iface, *next;
499 if (devInfo->key != INVALID_HANDLE_VALUE)
500 RegCloseKey(devInfo->key);
501 if (devInfo->phantom)
503 HKEY enumKey;
504 LONG l;
506 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0,
507 KEY_ALL_ACCESS, NULL, &enumKey, NULL);
508 if (!l)
510 RegDeleteTreeW(enumKey, devInfo->instanceId);
511 RegCloseKey(enumKey);
514 HeapFree(GetProcessHeap(), 0, devInfo->instanceId);
515 LIST_FOR_EACH_ENTRY_SAFE(iface, next, &devInfo->interfaces,
516 struct InterfaceInstances, entry)
518 list_remove(&iface->entry);
519 SETUPDI_FreeInterfaceInstances(iface);
520 HeapFree(GetProcessHeap(), 0, iface);
522 GlobalFree((HANDLE)devInfo->devId);
525 /* Adds a device with GUID guid and identifer devInst to set. Allocates a
526 * struct DeviceInfo, and points the returned device info's Reserved member
527 * to it. "Phantom" devices are deleted from the registry when closed.
528 * Returns a pointer to the newly allocated device info.
530 static BOOL SETUPDI_AddDeviceToSet(struct DeviceInfoSet *set,
531 const GUID *guid,
532 DWORD devInst,
533 LPCWSTR instanceId,
534 BOOL phantom,
535 SP_DEVINFO_DATA **dev)
537 BOOL ret = FALSE;
538 struct DeviceInfo *devInfo = SETUPDI_AllocateDeviceInfo(set, set->cDevices,
539 instanceId, phantom);
541 TRACE("%p, %s, %d, %s, %d\n", set, debugstr_guid(guid), devInst,
542 debugstr_w(instanceId), phantom);
544 if (devInfo)
546 if (set->devices)
547 set->devices = HeapReAlloc(GetProcessHeap(), 0, set->devices,
548 (set->cDevices + 1) * sizeof(SP_DEVINFO_DATA));
549 else
550 set->devices = HeapAlloc(GetProcessHeap(), 0,
551 sizeof(SP_DEVINFO_DATA));
552 if (set->devices)
554 WCHAR classGuidStr[39];
555 SP_DEVINFO_DATA *DeviceInfoData = &set->devices[set->cDevices++];
557 DeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA);
558 memcpy(&DeviceInfoData->ClassGuid, guid, sizeof(GUID));
559 DeviceInfoData->DevInst = devInfo->devId;
560 DeviceInfoData->Reserved = (ULONG_PTR)devInfo;
561 SETUPDI_GuidToString(guid, classGuidStr);
562 SetupDiSetDeviceRegistryPropertyW((HDEVINFO)set,
563 DeviceInfoData, SPDRP_CLASSGUID, (const BYTE *)classGuidStr,
564 lstrlenW(classGuidStr) * sizeof(WCHAR));
565 if (dev)
566 *dev = DeviceInfoData;
567 ret = TRUE;
569 else
571 HeapFree(GetProcessHeap(), 0, devInfo);
572 SetLastError(ERROR_OUTOFMEMORY);
575 return ret;
578 /***********************************************************************
579 * SetupDiBuildClassInfoList (SETUPAPI.@)
581 * Returns a list of setup class GUIDs that identify the classes
582 * that are installed on a local machine.
584 * PARAMS
585 * Flags [I] control exclusion of classes from the list.
586 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
587 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
588 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
590 * RETURNS
591 * Success: TRUE.
592 * Failure: FALSE.
594 BOOL WINAPI SetupDiBuildClassInfoList(
595 DWORD Flags,
596 LPGUID ClassGuidList,
597 DWORD ClassGuidListSize,
598 PDWORD RequiredSize)
600 TRACE("\n");
601 return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
602 ClassGuidListSize, RequiredSize,
603 NULL, NULL);
606 /***********************************************************************
607 * SetupDiBuildClassInfoListExA (SETUPAPI.@)
609 * Returns a list of setup class GUIDs that identify the classes
610 * that are installed on a local or remote macine.
612 * PARAMS
613 * Flags [I] control exclusion of classes from the list.
614 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
615 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
616 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
617 * MachineName [I] name of a remote machine.
618 * Reserved [I] must be NULL.
620 * RETURNS
621 * Success: TRUE.
622 * Failure: FALSE.
624 BOOL WINAPI SetupDiBuildClassInfoListExA(
625 DWORD Flags,
626 LPGUID ClassGuidList,
627 DWORD ClassGuidListSize,
628 PDWORD RequiredSize,
629 LPCSTR MachineName,
630 PVOID Reserved)
632 LPWSTR MachineNameW = NULL;
633 BOOL bResult;
635 TRACE("\n");
637 if (MachineName)
639 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
640 if (MachineNameW == NULL) return FALSE;
643 bResult = SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
644 ClassGuidListSize, RequiredSize,
645 MachineNameW, Reserved);
647 MyFree(MachineNameW);
649 return bResult;
652 /***********************************************************************
653 * SetupDiBuildClassInfoListExW (SETUPAPI.@)
655 * Returns a list of setup class GUIDs that identify the classes
656 * that are installed on a local or remote macine.
658 * PARAMS
659 * Flags [I] control exclusion of classes from the list.
660 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
661 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
662 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
663 * MachineName [I] name of a remote machine.
664 * Reserved [I] must be NULL.
666 * RETURNS
667 * Success: TRUE.
668 * Failure: FALSE.
670 BOOL WINAPI SetupDiBuildClassInfoListExW(
671 DWORD Flags,
672 LPGUID ClassGuidList,
673 DWORD ClassGuidListSize,
674 PDWORD RequiredSize,
675 LPCWSTR MachineName,
676 PVOID Reserved)
678 WCHAR szKeyName[40];
679 HKEY hClassesKey;
680 HKEY hClassKey;
681 DWORD dwLength;
682 DWORD dwIndex;
683 LONG lError;
684 DWORD dwGuidListIndex = 0;
686 TRACE("\n");
688 if (RequiredSize != NULL)
689 *RequiredSize = 0;
691 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
692 KEY_ALL_ACCESS,
693 DIOCR_INSTALLER,
694 MachineName,
695 Reserved);
696 if (hClassesKey == INVALID_HANDLE_VALUE)
698 return FALSE;
701 for (dwIndex = 0; ; dwIndex++)
703 dwLength = 40;
704 lError = RegEnumKeyExW(hClassesKey,
705 dwIndex,
706 szKeyName,
707 &dwLength,
708 NULL,
709 NULL,
710 NULL,
711 NULL);
712 TRACE("RegEnumKeyExW() returns %d\n", lError);
713 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
715 TRACE("Key name: %p\n", szKeyName);
717 if (RegOpenKeyExW(hClassesKey,
718 szKeyName,
720 KEY_ALL_ACCESS,
721 &hClassKey))
723 RegCloseKey(hClassesKey);
724 return FALSE;
727 if (!RegQueryValueExW(hClassKey,
728 NoUseClass,
729 NULL,
730 NULL,
731 NULL,
732 NULL))
734 TRACE("'NoUseClass' value found!\n");
735 RegCloseKey(hClassKey);
736 continue;
739 if ((Flags & DIBCI_NOINSTALLCLASS) &&
740 (!RegQueryValueExW(hClassKey,
741 NoInstallClass,
742 NULL,
743 NULL,
744 NULL,
745 NULL)))
747 TRACE("'NoInstallClass' value found!\n");
748 RegCloseKey(hClassKey);
749 continue;
752 if ((Flags & DIBCI_NODISPLAYCLASS) &&
753 (!RegQueryValueExW(hClassKey,
754 NoDisplayClass,
755 NULL,
756 NULL,
757 NULL,
758 NULL)))
760 TRACE("'NoDisplayClass' value found!\n");
761 RegCloseKey(hClassKey);
762 continue;
765 RegCloseKey(hClassKey);
767 TRACE("Guid: %p\n", szKeyName);
768 if (dwGuidListIndex < ClassGuidListSize)
770 if (szKeyName[0] == '{' && szKeyName[37] == '}')
772 szKeyName[37] = 0;
774 TRACE("Guid: %p\n", &szKeyName[1]);
776 UuidFromStringW(&szKeyName[1],
777 &ClassGuidList[dwGuidListIndex]);
780 dwGuidListIndex++;
783 if (lError != ERROR_SUCCESS)
784 break;
787 RegCloseKey(hClassesKey);
789 if (RequiredSize != NULL)
790 *RequiredSize = dwGuidListIndex;
792 if (ClassGuidListSize < dwGuidListIndex)
794 SetLastError(ERROR_INSUFFICIENT_BUFFER);
795 return FALSE;
798 return TRUE;
801 /***********************************************************************
802 * SetupDiClassGuidsFromNameA (SETUPAPI.@)
804 BOOL WINAPI SetupDiClassGuidsFromNameA(
805 LPCSTR ClassName,
806 LPGUID ClassGuidList,
807 DWORD ClassGuidListSize,
808 PDWORD RequiredSize)
810 return SetupDiClassGuidsFromNameExA(ClassName, ClassGuidList,
811 ClassGuidListSize, RequiredSize,
812 NULL, NULL);
815 /***********************************************************************
816 * SetupDiClassGuidsFromNameW (SETUPAPI.@)
818 BOOL WINAPI SetupDiClassGuidsFromNameW(
819 LPCWSTR ClassName,
820 LPGUID ClassGuidList,
821 DWORD ClassGuidListSize,
822 PDWORD RequiredSize)
824 return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
825 ClassGuidListSize, RequiredSize,
826 NULL, NULL);
829 /***********************************************************************
830 * SetupDiClassGuidsFromNameExA (SETUPAPI.@)
832 BOOL WINAPI SetupDiClassGuidsFromNameExA(
833 LPCSTR ClassName,
834 LPGUID ClassGuidList,
835 DWORD ClassGuidListSize,
836 PDWORD RequiredSize,
837 LPCSTR MachineName,
838 PVOID Reserved)
840 LPWSTR ClassNameW = NULL;
841 LPWSTR MachineNameW = NULL;
842 BOOL bResult;
844 ClassNameW = MultiByteToUnicode(ClassName, CP_ACP);
845 if (ClassNameW == NULL)
846 return FALSE;
848 if (MachineName)
850 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
851 if (MachineNameW == NULL)
853 MyFree(ClassNameW);
854 return FALSE;
858 bResult = SetupDiClassGuidsFromNameExW(ClassNameW, ClassGuidList,
859 ClassGuidListSize, RequiredSize,
860 MachineNameW, Reserved);
862 MyFree(MachineNameW);
863 MyFree(ClassNameW);
865 return bResult;
868 /***********************************************************************
869 * SetupDiClassGuidsFromNameExW (SETUPAPI.@)
871 BOOL WINAPI SetupDiClassGuidsFromNameExW(
872 LPCWSTR ClassName,
873 LPGUID ClassGuidList,
874 DWORD ClassGuidListSize,
875 PDWORD RequiredSize,
876 LPCWSTR MachineName,
877 PVOID Reserved)
879 WCHAR szKeyName[40];
880 WCHAR szClassName[256];
881 HKEY hClassesKey;
882 HKEY hClassKey;
883 DWORD dwLength;
884 DWORD dwIndex;
885 LONG lError;
886 DWORD dwGuidListIndex = 0;
888 if (RequiredSize != NULL)
889 *RequiredSize = 0;
891 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
892 KEY_ALL_ACCESS,
893 DIOCR_INSTALLER,
894 MachineName,
895 Reserved);
896 if (hClassesKey == INVALID_HANDLE_VALUE)
898 return FALSE;
901 for (dwIndex = 0; ; dwIndex++)
903 dwLength = 40;
904 lError = RegEnumKeyExW(hClassesKey,
905 dwIndex,
906 szKeyName,
907 &dwLength,
908 NULL,
909 NULL,
910 NULL,
911 NULL);
912 TRACE("RegEnumKeyExW() returns %d\n", lError);
913 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
915 TRACE("Key name: %p\n", szKeyName);
917 if (RegOpenKeyExW(hClassesKey,
918 szKeyName,
920 KEY_ALL_ACCESS,
921 &hClassKey))
923 RegCloseKey(hClassesKey);
924 return FALSE;
927 dwLength = 256 * sizeof(WCHAR);
928 if (!RegQueryValueExW(hClassKey,
929 Class,
930 NULL,
931 NULL,
932 (LPBYTE)szClassName,
933 &dwLength))
935 TRACE("Class name: %p\n", szClassName);
937 if (strcmpiW(szClassName, ClassName) == 0)
939 TRACE("Found matching class name\n");
941 TRACE("Guid: %p\n", szKeyName);
942 if (dwGuidListIndex < ClassGuidListSize)
944 if (szKeyName[0] == '{' && szKeyName[37] == '}')
946 szKeyName[37] = 0;
948 TRACE("Guid: %p\n", &szKeyName[1]);
950 UuidFromStringW(&szKeyName[1],
951 &ClassGuidList[dwGuidListIndex]);
954 dwGuidListIndex++;
958 RegCloseKey(hClassKey);
961 if (lError != ERROR_SUCCESS)
962 break;
965 RegCloseKey(hClassesKey);
967 if (RequiredSize != NULL)
968 *RequiredSize = dwGuidListIndex;
970 if (ClassGuidListSize < dwGuidListIndex)
972 SetLastError(ERROR_INSUFFICIENT_BUFFER);
973 return FALSE;
976 return TRUE;
979 /***********************************************************************
980 * SetupDiClassNameFromGuidA (SETUPAPI.@)
982 BOOL WINAPI SetupDiClassNameFromGuidA(
983 const GUID* ClassGuid,
984 PSTR ClassName,
985 DWORD ClassNameSize,
986 PDWORD RequiredSize)
988 return SetupDiClassNameFromGuidExA(ClassGuid, ClassName,
989 ClassNameSize, RequiredSize,
990 NULL, NULL);
993 /***********************************************************************
994 * SetupDiClassNameFromGuidW (SETUPAPI.@)
996 BOOL WINAPI SetupDiClassNameFromGuidW(
997 const GUID* ClassGuid,
998 PWSTR ClassName,
999 DWORD ClassNameSize,
1000 PDWORD RequiredSize)
1002 return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
1003 ClassNameSize, RequiredSize,
1004 NULL, NULL);
1007 /***********************************************************************
1008 * SetupDiClassNameFromGuidExA (SETUPAPI.@)
1010 BOOL WINAPI SetupDiClassNameFromGuidExA(
1011 const GUID* ClassGuid,
1012 PSTR ClassName,
1013 DWORD ClassNameSize,
1014 PDWORD RequiredSize,
1015 PCSTR MachineName,
1016 PVOID Reserved)
1018 WCHAR ClassNameW[MAX_CLASS_NAME_LEN];
1019 LPWSTR MachineNameW = NULL;
1020 BOOL ret;
1022 if (MachineName)
1023 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1024 ret = SetupDiClassNameFromGuidExW(ClassGuid, ClassNameW, MAX_CLASS_NAME_LEN,
1025 NULL, MachineNameW, Reserved);
1026 if (ret)
1028 int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
1029 ClassNameSize, NULL, NULL);
1031 if (!ClassNameSize && RequiredSize)
1032 *RequiredSize = len;
1034 MyFree(MachineNameW);
1035 return ret;
1038 /***********************************************************************
1039 * SetupDiClassNameFromGuidExW (SETUPAPI.@)
1041 BOOL WINAPI SetupDiClassNameFromGuidExW(
1042 const GUID* ClassGuid,
1043 PWSTR ClassName,
1044 DWORD ClassNameSize,
1045 PDWORD RequiredSize,
1046 PCWSTR MachineName,
1047 PVOID Reserved)
1049 HKEY hKey;
1050 DWORD dwLength;
1052 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1053 KEY_ALL_ACCESS,
1054 DIOCR_INSTALLER,
1055 MachineName,
1056 Reserved);
1057 if (hKey == INVALID_HANDLE_VALUE)
1059 return FALSE;
1062 if (RequiredSize != NULL)
1064 dwLength = 0;
1065 if (RegQueryValueExW(hKey,
1066 Class,
1067 NULL,
1068 NULL,
1069 NULL,
1070 &dwLength))
1072 RegCloseKey(hKey);
1073 return FALSE;
1076 *RequiredSize = dwLength / sizeof(WCHAR);
1079 dwLength = ClassNameSize * sizeof(WCHAR);
1080 if (RegQueryValueExW(hKey,
1081 Class,
1082 NULL,
1083 NULL,
1084 (LPBYTE)ClassName,
1085 &dwLength))
1087 RegCloseKey(hKey);
1088 return FALSE;
1091 RegCloseKey(hKey);
1093 return TRUE;
1096 /***********************************************************************
1097 * SetupDiCreateDeviceInfoList (SETUPAPI.@)
1099 HDEVINFO WINAPI
1100 SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
1101 HWND hwndParent)
1103 return SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent, NULL, NULL);
1106 /***********************************************************************
1107 * SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
1109 HDEVINFO WINAPI
1110 SetupDiCreateDeviceInfoListExA(const GUID *ClassGuid,
1111 HWND hwndParent,
1112 PCSTR MachineName,
1113 PVOID Reserved)
1115 LPWSTR MachineNameW = NULL;
1116 HDEVINFO hDevInfo;
1118 TRACE("\n");
1120 if (MachineName)
1122 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1123 if (MachineNameW == NULL)
1124 return INVALID_HANDLE_VALUE;
1127 hDevInfo = SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent,
1128 MachineNameW, Reserved);
1130 MyFree(MachineNameW);
1132 return hDevInfo;
1135 /***********************************************************************
1136 * SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
1138 * Create an empty DeviceInfoSet list.
1140 * PARAMS
1141 * ClassGuid [I] if not NULL only devices with GUID ClcassGuid are associated
1142 * with this list.
1143 * hwndParent [I] hwnd needed for interface related actions.
1144 * MachineName [I] name of machine to create emtpy DeviceInfoSet list, if NULL
1145 * local registry will be used.
1146 * Reserved [I] must be NULL
1148 * RETURNS
1149 * Success: empty list.
1150 * Failure: INVALID_HANDLE_VALUE.
1152 HDEVINFO WINAPI
1153 SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
1154 HWND hwndParent,
1155 PCWSTR MachineName,
1156 PVOID Reserved)
1158 struct DeviceInfoSet *list = NULL;
1159 DWORD size = sizeof(struct DeviceInfoSet);
1161 TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid), hwndParent,
1162 debugstr_w(MachineName), Reserved);
1164 if (MachineName != NULL)
1166 FIXME("remote support is not implemented\n");
1167 SetLastError(ERROR_INVALID_MACHINENAME);
1168 return INVALID_HANDLE_VALUE;
1171 if (Reserved != NULL)
1173 SetLastError(ERROR_INVALID_PARAMETER);
1174 return INVALID_HANDLE_VALUE;
1177 list = HeapAlloc(GetProcessHeap(), 0, size);
1178 if (!list)
1180 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1181 return INVALID_HANDLE_VALUE;
1184 list->magic = SETUP_DEVICE_INFO_SET_MAGIC;
1185 list->hwndParent = hwndParent;
1186 memcpy(&list->ClassGuid,
1187 ClassGuid ? ClassGuid : &GUID_NULL,
1188 sizeof(list->ClassGuid));
1189 list->cDevices = 0;
1190 list->devices = NULL;
1192 return (HDEVINFO)list;
1195 /***********************************************************************
1196 * SetupDiCreateDevRegKeyA (SETUPAPI.@)
1198 HKEY WINAPI SetupDiCreateDevRegKeyA(
1199 HDEVINFO DeviceInfoSet,
1200 PSP_DEVINFO_DATA DeviceInfoData,
1201 DWORD Scope,
1202 DWORD HwProfile,
1203 DWORD KeyType,
1204 HINF InfHandle,
1205 PCSTR InfSectionName)
1207 PWSTR InfSectionNameW = NULL;
1208 HKEY key;
1210 TRACE("%p %p %d %d %d %p %s\n", DeviceInfoSet, DeviceInfoData, Scope,
1211 HwProfile, KeyType, InfHandle, debugstr_a(InfSectionName));
1213 if (InfHandle)
1215 if (!InfSectionName)
1217 SetLastError(ERROR_INVALID_PARAMETER);
1218 return INVALID_HANDLE_VALUE;
1220 else
1222 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
1223 if (InfSectionNameW == NULL) return INVALID_HANDLE_VALUE;
1226 key = SetupDiCreateDevRegKeyW(DeviceInfoSet, DeviceInfoData, Scope,
1227 HwProfile, KeyType, InfHandle, InfSectionNameW);
1228 MyFree(InfSectionNameW);
1229 return key;
1232 /***********************************************************************
1233 * SetupDiCreateDevRegKeyW (SETUPAPI.@)
1235 HKEY WINAPI SetupDiCreateDevRegKeyW(
1236 HDEVINFO DeviceInfoSet,
1237 PSP_DEVINFO_DATA DeviceInfoData,
1238 DWORD Scope,
1239 DWORD HwProfile,
1240 DWORD KeyType,
1241 HINF InfHandle,
1242 PCWSTR InfSectionName)
1244 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1245 struct DeviceInfo *devInfo;
1246 HKEY key = INVALID_HANDLE_VALUE;
1248 TRACE("%p %p %d %d %d %p %s\n", DeviceInfoSet, DeviceInfoData, Scope,
1249 HwProfile, KeyType, InfHandle, debugstr_w(InfSectionName));
1251 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1253 SetLastError(ERROR_INVALID_HANDLE);
1254 return INVALID_HANDLE_VALUE;
1256 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1258 SetLastError(ERROR_INVALID_HANDLE);
1259 return INVALID_HANDLE_VALUE;
1261 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1262 || !DeviceInfoData->Reserved)
1264 SetLastError(ERROR_INVALID_PARAMETER);
1265 return INVALID_HANDLE_VALUE;
1267 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1268 if (devInfo->set != set)
1270 SetLastError(ERROR_INVALID_PARAMETER);
1271 return INVALID_HANDLE_VALUE;
1273 if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
1275 SetLastError(ERROR_INVALID_FLAGS);
1276 return INVALID_HANDLE_VALUE;
1278 if (KeyType != DIREG_DEV && KeyType != DIREG_DRV)
1280 SetLastError(ERROR_INVALID_FLAGS);
1281 return INVALID_HANDLE_VALUE;
1283 if (devInfo->phantom)
1285 SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
1286 return INVALID_HANDLE_VALUE;
1288 if (Scope != DICS_FLAG_GLOBAL)
1289 FIXME("unimplemented for scope %d\n", Scope);
1290 switch (KeyType)
1292 case DIREG_DEV:
1293 key = SETUPDI_CreateDevKey(devInfo);
1294 break;
1295 case DIREG_DRV:
1296 key = SETUPDI_CreateDrvKey(devInfo);
1297 break;
1298 default:
1299 WARN("unknown KeyType %d\n", KeyType);
1301 if (InfHandle)
1302 SetupInstallFromInfSectionW(NULL, InfHandle, InfSectionName, SPINST_ALL,
1303 NULL, NULL, SP_COPY_NEWER_ONLY, NULL, NULL, DeviceInfoSet,
1304 DeviceInfoData);
1305 return key;
1308 /***********************************************************************
1309 * SetupDiCreateDeviceInfoA (SETUPAPI.@)
1311 BOOL WINAPI SetupDiCreateDeviceInfoA(
1312 HDEVINFO DeviceInfoSet,
1313 PCSTR DeviceName,
1314 CONST GUID *ClassGuid,
1315 PCSTR DeviceDescription,
1316 HWND hwndParent,
1317 DWORD CreationFlags,
1318 PSP_DEVINFO_DATA DeviceInfoData)
1320 BOOL ret = FALSE;
1321 LPWSTR DeviceNameW = NULL;
1322 LPWSTR DeviceDescriptionW = NULL;
1324 if (DeviceName)
1326 DeviceNameW = MultiByteToUnicode(DeviceName, CP_ACP);
1327 if (DeviceNameW == NULL) return FALSE;
1329 if (DeviceDescription)
1331 DeviceDescriptionW = MultiByteToUnicode(DeviceDescription, CP_ACP);
1332 if (DeviceDescriptionW == NULL)
1334 MyFree(DeviceNameW);
1335 return FALSE;
1339 ret = SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceNameW, ClassGuid, DeviceDescriptionW,
1340 hwndParent, CreationFlags, DeviceInfoData);
1342 MyFree(DeviceNameW);
1343 MyFree(DeviceDescriptionW);
1345 return ret;
1348 static DWORD SETUPDI_DevNameToDevID(LPCWSTR devName)
1350 LPCWSTR ptr;
1351 DWORD devNameLen = lstrlenW(devName), devInst = 0;
1352 BOOL valid = TRUE;
1354 TRACE("%s\n", debugstr_w(devName));
1355 for (ptr = devName; valid && *ptr && ptr - devName < devNameLen; )
1357 if (isdigitW(*ptr))
1359 devInst *= 10;
1360 devInst |= *ptr - '0';
1361 ptr++;
1363 else
1364 valid = FALSE;
1366 TRACE("%d\n", valid ? devInst : 0xffffffff);
1367 return valid ? devInst : 0xffffffff;
1370 /***********************************************************************
1371 * SetupDiCreateDeviceInfoW (SETUPAPI.@)
1373 BOOL WINAPI SetupDiCreateDeviceInfoW(
1374 HDEVINFO DeviceInfoSet,
1375 PCWSTR DeviceName,
1376 CONST GUID *ClassGuid,
1377 PCWSTR DeviceDescription,
1378 HWND hwndParent,
1379 DWORD CreationFlags,
1380 PSP_DEVINFO_DATA DeviceInfoData)
1382 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1383 BOOL ret = FALSE, allocatedInstanceId = FALSE;
1384 LPCWSTR instanceId = NULL;
1386 TRACE("%p %s %s %s %p %x %p\n", DeviceInfoSet, debugstr_w(DeviceName),
1387 debugstr_guid(ClassGuid), debugstr_w(DeviceDescription),
1388 hwndParent, CreationFlags, DeviceInfoData);
1390 if (!DeviceName)
1392 SetLastError(ERROR_INVALID_DEVINST_NAME);
1393 return FALSE;
1395 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1397 SetLastError(ERROR_INVALID_HANDLE);
1398 return FALSE;
1400 if (!ClassGuid)
1402 SetLastError(ERROR_INVALID_PARAMETER);
1403 return FALSE;
1405 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1407 SetLastError(ERROR_INVALID_HANDLE);
1408 return FALSE;
1410 if (!IsEqualGUID(&set->ClassGuid, &GUID_NULL) &&
1411 !IsEqualGUID(ClassGuid, &set->ClassGuid))
1413 SetLastError(ERROR_CLASS_MISMATCH);
1414 return FALSE;
1416 if ((CreationFlags & DICD_GENERATE_ID))
1418 if (strchrW(DeviceName, '\\'))
1419 SetLastError(ERROR_INVALID_DEVINST_NAME);
1420 else
1422 static const WCHAR newDeviceFmt[] = {'R','O','O','T','\\','%','s',
1423 '\\','%','0','4','d',0};
1424 DWORD devId;
1426 if (set->cDevices)
1428 DWORD i, highestDevID = 0;
1430 for (i = 0; i < set->cDevices; i++)
1432 struct DeviceInfo *devInfo =
1433 (struct DeviceInfo *)set->devices[i].Reserved;
1434 LPCWSTR devName = strrchrW(devInfo->instanceId, '\\');
1435 DWORD id;
1437 if (devName)
1438 devName++;
1439 else
1440 devName = devInfo->instanceId;
1441 id = SETUPDI_DevNameToDevID(devName);
1442 if (id != 0xffffffff && id > highestDevID)
1443 highestDevID = id;
1445 devId = highestDevID + 1;
1447 else
1448 devId = 0;
1449 /* 17 == lstrlenW(L"Root\\") + lstrlenW("\\") + 1 + %d max size */
1450 instanceId = HeapAlloc(GetProcessHeap(), 0,
1451 (17 + lstrlenW(DeviceName)) * sizeof(WCHAR));
1452 if (instanceId)
1454 sprintfW((LPWSTR)instanceId, newDeviceFmt, DeviceName,
1455 devId);
1456 allocatedInstanceId = TRUE;
1457 ret = TRUE;
1459 else
1460 ret = FALSE;
1463 else
1465 DWORD i;
1467 ret = TRUE;
1468 instanceId = DeviceName;
1469 for (i = 0; ret && i < set->cDevices; i++)
1471 struct DeviceInfo *devInfo =
1472 (struct DeviceInfo *)set->devices[i].Reserved;
1474 if (!lstrcmpiW(DeviceName, devInfo->instanceId))
1476 SetLastError(ERROR_DEVINST_ALREADY_EXISTS);
1477 ret = FALSE;
1481 if (ret)
1483 SP_DEVINFO_DATA *dev = NULL;
1485 ret = SETUPDI_AddDeviceToSet(set, ClassGuid, 0 /* FIXME: DevInst */,
1486 instanceId, TRUE, &dev);
1487 if (ret)
1489 if (DeviceDescription)
1490 SetupDiSetDeviceRegistryPropertyW(DeviceInfoSet,
1491 dev, SPDRP_DEVICEDESC, (const BYTE *)DeviceDescription,
1492 lstrlenW(DeviceDescription) * sizeof(WCHAR));
1493 if (DeviceInfoData)
1495 if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
1497 SetLastError(ERROR_INVALID_USER_BUFFER);
1498 ret = FALSE;
1500 else
1501 memcpy(DeviceInfoData, dev, sizeof(SP_DEVINFO_DATA));
1505 if (allocatedInstanceId)
1506 HeapFree(GetProcessHeap(), 0, (LPWSTR)instanceId);
1508 return ret;
1511 /***********************************************************************
1512 * SetupDiRegisterDeviceInfo (SETUPAPI.@)
1514 BOOL WINAPI SetupDiRegisterDeviceInfo(
1515 HDEVINFO DeviceInfoSet,
1516 PSP_DEVINFO_DATA DeviceInfoData,
1517 DWORD Flags,
1518 PSP_DETSIG_CMPPROC CompareProc,
1519 PVOID CompareContext,
1520 PSP_DEVINFO_DATA DupDeviceInfoData)
1522 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1523 struct DeviceInfo *devInfo;
1525 TRACE("%p %p %08x %p %p %p\n", DeviceInfoSet, DeviceInfoData, Flags,
1526 CompareProc, CompareContext, DupDeviceInfoData);
1528 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1530 SetLastError(ERROR_INVALID_HANDLE);
1531 return FALSE;
1533 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1535 SetLastError(ERROR_INVALID_HANDLE);
1536 return FALSE;
1538 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1539 || !DeviceInfoData->Reserved)
1541 SetLastError(ERROR_INVALID_PARAMETER);
1542 return FALSE;
1544 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1545 if (devInfo->set != set)
1547 SetLastError(ERROR_INVALID_PARAMETER);
1548 return FALSE;
1550 if (devInfo->phantom)
1552 devInfo->phantom = FALSE;
1553 RegDeleteValueW(devInfo->key, Phantom);
1555 return TRUE;
1558 /***********************************************************************
1559 * SetupDiEnumDeviceInfo (SETUPAPI.@)
1561 BOOL WINAPI SetupDiEnumDeviceInfo(
1562 HDEVINFO devinfo,
1563 DWORD index,
1564 PSP_DEVINFO_DATA info)
1566 BOOL ret = FALSE;
1568 TRACE("%p %d %p\n", devinfo, index, info);
1570 if(info==NULL)
1572 SetLastError(ERROR_INVALID_PARAMETER);
1573 return FALSE;
1575 if (devinfo && devinfo != INVALID_HANDLE_VALUE)
1577 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
1578 if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
1580 if (index < list->cDevices)
1582 if (info->cbSize == sizeof(SP_DEVINFO_DATA))
1584 memcpy(info, &list->devices[index], info->cbSize);
1585 ret = TRUE;
1587 else
1588 SetLastError(ERROR_INVALID_USER_BUFFER);
1590 else
1591 SetLastError(ERROR_NO_MORE_ITEMS);
1593 else
1594 SetLastError(ERROR_INVALID_HANDLE);
1596 else
1597 SetLastError(ERROR_INVALID_HANDLE);
1598 return ret;
1601 /***********************************************************************
1602 * SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
1604 BOOL WINAPI SetupDiGetDeviceInstanceIdA(
1605 HDEVINFO DeviceInfoSet,
1606 PSP_DEVINFO_DATA DeviceInfoData,
1607 PSTR DeviceInstanceId,
1608 DWORD DeviceInstanceIdSize,
1609 PDWORD RequiredSize)
1611 BOOL ret = FALSE;
1612 DWORD size;
1613 PWSTR instanceId;
1615 TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
1616 DeviceInstanceIdSize, RequiredSize);
1618 SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
1619 DeviceInfoData,
1620 NULL,
1622 &size);
1623 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1624 return FALSE;
1625 instanceId = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1626 if (instanceId)
1628 ret = SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
1629 DeviceInfoData,
1630 instanceId,
1631 size,
1632 &size);
1633 if (ret)
1635 int len = WideCharToMultiByte(CP_ACP, 0, instanceId, -1,
1636 DeviceInstanceId,
1637 DeviceInstanceIdSize, NULL, NULL);
1639 if (!len)
1640 ret = FALSE;
1641 else
1643 if (len > DeviceInstanceIdSize)
1645 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1646 ret = FALSE;
1648 if (RequiredSize)
1649 *RequiredSize = len;
1652 HeapFree(GetProcessHeap(), 0, instanceId);
1654 return ret;
1657 /***********************************************************************
1658 * SetupDiGetDeviceInstanceIdW (SETUPAPI.@)
1660 BOOL WINAPI SetupDiGetDeviceInstanceIdW(
1661 HDEVINFO DeviceInfoSet,
1662 PSP_DEVINFO_DATA DeviceInfoData,
1663 PWSTR DeviceInstanceId,
1664 DWORD DeviceInstanceIdSize,
1665 PDWORD RequiredSize)
1667 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
1668 struct DeviceInfo *devInfo;
1670 TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
1671 DeviceInstanceIdSize, RequiredSize);
1673 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
1675 SetLastError(ERROR_INVALID_HANDLE);
1676 return FALSE;
1678 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
1680 SetLastError(ERROR_INVALID_HANDLE);
1681 return FALSE;
1683 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
1684 || !DeviceInfoData->Reserved)
1686 SetLastError(ERROR_INVALID_PARAMETER);
1687 return FALSE;
1689 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
1690 if (devInfo->set != set)
1692 SetLastError(ERROR_INVALID_PARAMETER);
1693 return FALSE;
1695 TRACE("instance ID: %s\n", debugstr_w(devInfo->instanceId));
1696 if (DeviceInstanceIdSize < lstrlenW(devInfo->instanceId) + 1)
1698 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1699 if (RequiredSize)
1700 *RequiredSize = lstrlenW(devInfo->instanceId) + 1;
1701 return FALSE;
1703 lstrcpyW(DeviceInstanceId, devInfo->instanceId);
1704 if (RequiredSize)
1705 *RequiredSize = lstrlenW(devInfo->instanceId) + 1;
1706 return TRUE;
1709 /***********************************************************************
1710 * SetupDiGetActualSectionToInstallA (SETUPAPI.@)
1712 BOOL WINAPI SetupDiGetActualSectionToInstallA(
1713 HINF InfHandle,
1714 PCSTR InfSectionName,
1715 PSTR InfSectionWithExt,
1716 DWORD InfSectionWithExtSize,
1717 PDWORD RequiredSize,
1718 PSTR *Extension)
1720 FIXME("\n");
1721 return FALSE;
1724 /***********************************************************************
1725 * SetupDiGetActualSectionToInstallW (SETUPAPI.@)
1727 BOOL WINAPI SetupDiGetActualSectionToInstallW(
1728 HINF InfHandle,
1729 PCWSTR InfSectionName,
1730 PWSTR InfSectionWithExt,
1731 DWORD InfSectionWithExtSize,
1732 PDWORD RequiredSize,
1733 PWSTR *Extension)
1735 WCHAR szBuffer[MAX_PATH];
1736 DWORD dwLength;
1737 DWORD dwFullLength;
1738 LONG lLineCount = -1;
1740 lstrcpyW(szBuffer, InfSectionName);
1741 dwLength = lstrlenW(szBuffer);
1743 if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
1745 /* Test section name with '.NTx86' extension */
1746 lstrcpyW(&szBuffer[dwLength], NtPlatformExtension);
1747 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1749 if (lLineCount == -1)
1751 /* Test section name with '.NT' extension */
1752 lstrcpyW(&szBuffer[dwLength], NtExtension);
1753 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1756 else
1758 /* Test section name with '.Win' extension */
1759 lstrcpyW(&szBuffer[dwLength], WinExtension);
1760 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1763 if (lLineCount == -1)
1765 /* Test section name without extension */
1766 szBuffer[dwLength] = 0;
1767 lLineCount = SetupGetLineCountW(InfHandle, szBuffer);
1770 if (lLineCount == -1)
1772 SetLastError(ERROR_INVALID_PARAMETER);
1773 return FALSE;
1776 dwFullLength = lstrlenW(szBuffer);
1778 if (InfSectionWithExt != NULL && InfSectionWithExtSize != 0)
1780 if (InfSectionWithExtSize < (dwFullLength + 1))
1782 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1783 return FALSE;
1786 lstrcpyW(InfSectionWithExt, szBuffer);
1787 if (Extension != NULL)
1789 *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
1793 if (RequiredSize != NULL)
1795 *RequiredSize = dwFullLength + 1;
1798 return TRUE;
1801 /***********************************************************************
1802 * SetupDiGetClassDescriptionA (SETUPAPI.@)
1804 BOOL WINAPI SetupDiGetClassDescriptionA(
1805 const GUID* ClassGuid,
1806 PSTR ClassDescription,
1807 DWORD ClassDescriptionSize,
1808 PDWORD RequiredSize)
1810 return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
1811 ClassDescriptionSize,
1812 RequiredSize, NULL, NULL);
1815 /***********************************************************************
1816 * SetupDiGetClassDescriptionW (SETUPAPI.@)
1818 BOOL WINAPI SetupDiGetClassDescriptionW(
1819 const GUID* ClassGuid,
1820 PWSTR ClassDescription,
1821 DWORD ClassDescriptionSize,
1822 PDWORD RequiredSize)
1824 return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
1825 ClassDescriptionSize,
1826 RequiredSize, NULL, NULL);
1829 /***********************************************************************
1830 * SetupDiGetClassDescriptionExA (SETUPAPI.@)
1832 BOOL WINAPI SetupDiGetClassDescriptionExA(
1833 const GUID* ClassGuid,
1834 PSTR ClassDescription,
1835 DWORD ClassDescriptionSize,
1836 PDWORD RequiredSize,
1837 PCSTR MachineName,
1838 PVOID Reserved)
1840 HKEY hKey;
1841 DWORD dwLength;
1842 BOOL ret;
1844 hKey = SetupDiOpenClassRegKeyExA(ClassGuid,
1845 KEY_ALL_ACCESS,
1846 DIOCR_INSTALLER,
1847 MachineName,
1848 Reserved);
1849 if (hKey == INVALID_HANDLE_VALUE)
1851 WARN("SetupDiOpenClassRegKeyExA() failed (Error %u)\n", GetLastError());
1852 return FALSE;
1855 dwLength = ClassDescriptionSize;
1856 ret = !RegQueryValueExA( hKey, NULL, NULL, NULL,
1857 (LPBYTE)ClassDescription, &dwLength );
1858 if (RequiredSize) *RequiredSize = dwLength;
1859 RegCloseKey(hKey);
1860 return ret;
1863 /***********************************************************************
1864 * SetupDiGetClassDescriptionExW (SETUPAPI.@)
1866 BOOL WINAPI SetupDiGetClassDescriptionExW(
1867 const GUID* ClassGuid,
1868 PWSTR ClassDescription,
1869 DWORD ClassDescriptionSize,
1870 PDWORD RequiredSize,
1871 PCWSTR MachineName,
1872 PVOID Reserved)
1874 HKEY hKey;
1875 DWORD dwLength;
1876 BOOL ret;
1878 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1879 KEY_ALL_ACCESS,
1880 DIOCR_INSTALLER,
1881 MachineName,
1882 Reserved);
1883 if (hKey == INVALID_HANDLE_VALUE)
1885 WARN("SetupDiOpenClassRegKeyExW() failed (Error %u)\n", GetLastError());
1886 return FALSE;
1889 dwLength = ClassDescriptionSize * sizeof(WCHAR);
1890 ret = !RegQueryValueExW( hKey, NULL, NULL, NULL,
1891 (LPBYTE)ClassDescription, &dwLength );
1892 if (RequiredSize) *RequiredSize = dwLength / sizeof(WCHAR);
1893 RegCloseKey(hKey);
1894 return ret;
1897 /***********************************************************************
1898 * SetupDiGetClassDevsA (SETUPAPI.@)
1900 HDEVINFO WINAPI SetupDiGetClassDevsA(
1901 CONST GUID *class,
1902 LPCSTR enumstr,
1903 HWND parent,
1904 DWORD flags)
1906 HDEVINFO ret;
1907 LPWSTR enumstrW = NULL;
1909 if (enumstr)
1911 int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1912 enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1913 if (!enumstrW)
1915 ret = INVALID_HANDLE_VALUE;
1916 goto end;
1918 MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1920 ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, NULL, NULL,
1921 NULL);
1922 HeapFree(GetProcessHeap(), 0, enumstrW);
1924 end:
1925 return ret;
1928 /***********************************************************************
1929 * SetupDiGetClassDevsExA (SETUPAPI.@)
1931 HDEVINFO WINAPI SetupDiGetClassDevsExA(
1932 const GUID *class,
1933 PCSTR enumstr,
1934 HWND parent,
1935 DWORD flags,
1936 HDEVINFO deviceset,
1937 PCSTR machine,
1938 PVOID reserved)
1940 HDEVINFO ret;
1941 LPWSTR enumstrW = NULL, machineW = NULL;
1943 if (enumstr)
1945 int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1946 enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1947 if (!enumstrW)
1949 ret = INVALID_HANDLE_VALUE;
1950 goto end;
1952 MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1954 if (machine)
1956 int len = MultiByteToWideChar(CP_ACP, 0, machine, -1, NULL, 0);
1957 machineW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1958 if (!machineW)
1960 HeapFree(GetProcessHeap(), 0, enumstrW);
1961 ret = INVALID_HANDLE_VALUE;
1962 goto end;
1964 MultiByteToWideChar(CP_ACP, 0, machine, -1, machineW, len);
1966 ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, deviceset,
1967 machineW, reserved);
1968 HeapFree(GetProcessHeap(), 0, enumstrW);
1969 HeapFree(GetProcessHeap(), 0, machineW);
1971 end:
1972 return ret;
1975 static void SETUPDI_AddDeviceInterfaces(SP_DEVINFO_DATA *dev, HKEY key,
1976 const GUID *interface)
1978 DWORD i, len;
1979 WCHAR subKeyName[MAX_PATH];
1980 LONG l = ERROR_SUCCESS;
1982 for (i = 0; !l; i++)
1984 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
1985 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
1986 if (!l)
1988 HKEY subKey;
1989 SP_DEVICE_INTERFACE_DATA *iface = NULL;
1991 /* The subkey name is the reference string, with a '#' prepended */
1992 SETUPDI_AddInterfaceInstance(dev, interface, subKeyName + 1,
1993 &iface);
1994 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
1995 if (!l)
1997 WCHAR symbolicLink[MAX_PATH];
1998 DWORD dataType;
2000 len = sizeof(symbolicLink);
2001 l = RegQueryValueExW(subKey, SymbolicLink, NULL, &dataType,
2002 (BYTE *)symbolicLink, &len);
2003 if (!l && dataType == REG_SZ)
2004 SETUPDI_SetInterfaceSymbolicLink(iface, symbolicLink);
2005 RegCloseKey(subKey);
2007 /* Allow enumeration to continue */
2008 l = ERROR_SUCCESS;
2011 /* FIXME: find and add all the device's interfaces to the device */
2014 static void SETUPDI_EnumerateMatchingInterfaces(HDEVINFO DeviceInfoSet,
2015 HKEY key, const GUID *interface, LPCWSTR enumstr)
2017 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2018 DWORD i, len;
2019 WCHAR subKeyName[MAX_PATH];
2020 LONG l;
2021 HKEY enumKey = INVALID_HANDLE_VALUE;
2023 TRACE("%s\n", debugstr_w(enumstr));
2025 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_READ, NULL,
2026 &enumKey, NULL);
2027 for (i = 0; !l; i++)
2029 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2030 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
2031 if (!l)
2033 HKEY subKey;
2035 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
2036 if (!l)
2038 WCHAR deviceInst[MAX_PATH * 3];
2039 DWORD dataType;
2041 len = sizeof(deviceInst);
2042 l = RegQueryValueExW(subKey, DeviceInstance, NULL, &dataType,
2043 (BYTE *)deviceInst, &len);
2044 if (!l && dataType == REG_SZ)
2046 TRACE("found instance ID %s\n", debugstr_w(deviceInst));
2047 if (!enumstr || !lstrcmpiW(enumstr, deviceInst))
2049 HKEY deviceKey;
2051 l = RegOpenKeyExW(enumKey, deviceInst, 0, KEY_READ,
2052 &deviceKey);
2053 if (!l)
2055 WCHAR deviceClassStr[40];
2057 len = sizeof(deviceClassStr);
2058 l = RegQueryValueExW(deviceKey, ClassGUID, NULL,
2059 &dataType, (BYTE *)deviceClassStr, &len);
2060 if (!l && dataType == REG_SZ &&
2061 deviceClassStr[0] == '{' &&
2062 deviceClassStr[37] == '}')
2064 GUID deviceClass;
2065 SP_DEVINFO_DATA *dev;
2067 deviceClassStr[37] = 0;
2068 UuidFromStringW(&deviceClassStr[1],
2069 &deviceClass);
2070 if (SETUPDI_AddDeviceToSet(set, &deviceClass,
2071 0 /* FIXME: DevInst */, deviceInst,
2072 FALSE, &dev))
2073 SETUPDI_AddDeviceInterfaces(dev, subKey,
2074 interface);
2076 RegCloseKey(deviceKey);
2080 RegCloseKey(subKey);
2082 /* Allow enumeration to continue */
2083 l = ERROR_SUCCESS;
2086 if (enumKey != INVALID_HANDLE_VALUE)
2087 RegCloseKey(enumKey);
2090 static void SETUPDI_EnumerateInterfaces(HDEVINFO DeviceInfoSet,
2091 const GUID *interface, LPCWSTR enumstr, DWORD flags)
2093 HKEY interfacesKey = SetupDiOpenClassRegKeyExW(interface, KEY_READ,
2094 DIOCR_INTERFACE, NULL, NULL);
2096 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(interface),
2097 debugstr_w(enumstr), flags);
2099 if (interfacesKey != INVALID_HANDLE_VALUE)
2101 if (flags & DIGCF_ALLCLASSES)
2103 DWORD i, len;
2104 WCHAR interfaceGuidStr[40];
2105 LONG l = ERROR_SUCCESS;
2107 for (i = 0; !l; i++)
2109 len = sizeof(interfaceGuidStr) / sizeof(interfaceGuidStr[0]);
2110 l = RegEnumKeyExW(interfacesKey, i, interfaceGuidStr, &len,
2111 NULL, NULL, NULL, NULL);
2112 if (!l)
2114 if (interfaceGuidStr[0] == '{' &&
2115 interfaceGuidStr[37] == '}')
2117 HKEY interfaceKey;
2118 GUID interfaceGuid;
2120 interfaceGuidStr[37] = 0;
2121 UuidFromStringW(&interfaceGuidStr[1], &interfaceGuid);
2122 l = RegOpenKeyExW(interfacesKey, interfaceGuidStr, 0,
2123 KEY_READ, &interfaceKey);
2124 if (!l)
2126 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
2127 interfaceKey, &interfaceGuid, enumstr);
2128 RegCloseKey(interfaceKey);
2134 else
2136 /* In this case, SetupDiOpenClassRegKeyExW opened the specific
2137 * interface's key, so just pass that long
2139 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet,
2140 interfacesKey, interface, enumstr);
2142 RegCloseKey(interfacesKey);
2146 static void SETUPDI_EnumerateMatchingDeviceInstances(struct DeviceInfoSet *set,
2147 LPCWSTR enumerator, LPCWSTR deviceName, HKEY deviceKey,
2148 const GUID *class, DWORD flags)
2150 DWORD i, len;
2151 WCHAR deviceInstance[MAX_PATH];
2152 LONG l = ERROR_SUCCESS;
2154 TRACE("%s %s\n", debugstr_w(enumerator), debugstr_w(deviceName));
2156 for (i = 0; !l; i++)
2158 len = sizeof(deviceInstance) / sizeof(deviceInstance[0]);
2159 l = RegEnumKeyExW(deviceKey, i, deviceInstance, &len, NULL, NULL, NULL,
2160 NULL);
2161 if (!l)
2163 HKEY subKey;
2165 l = RegOpenKeyExW(deviceKey, deviceInstance, 0, KEY_READ, &subKey);
2166 if (!l)
2168 WCHAR classGuid[40];
2169 DWORD dataType;
2171 len = sizeof(classGuid);
2172 l = RegQueryValueExW(subKey, ClassGUID, NULL, &dataType,
2173 (BYTE *)classGuid, &len);
2174 if (!l && dataType == REG_SZ)
2176 if (classGuid[0] == '{' && classGuid[37] == '}')
2178 GUID deviceClass;
2180 classGuid[37] = 0;
2181 UuidFromStringW(&classGuid[1], &deviceClass);
2182 if ((flags & DIGCF_ALLCLASSES) ||
2183 IsEqualGUID(class, &deviceClass))
2185 static const WCHAR fmt[] =
2186 {'%','s','\\','%','s','\\','%','s',0};
2187 LPWSTR instanceId;
2189 instanceId = HeapAlloc(GetProcessHeap(), 0,
2190 (lstrlenW(enumerator) + lstrlenW(deviceName) +
2191 lstrlenW(deviceInstance) + 3) * sizeof(WCHAR));
2192 if (instanceId)
2194 sprintfW(instanceId, fmt, enumerator,
2195 deviceName, deviceInstance);
2196 SETUPDI_AddDeviceToSet(set, &deviceClass,
2197 0 /* FIXME: DevInst */, instanceId,
2198 FALSE, NULL);
2199 HeapFree(GetProcessHeap(), 0, instanceId);
2204 RegCloseKey(subKey);
2206 /* Allow enumeration to continue */
2207 l = ERROR_SUCCESS;
2212 static void SETUPDI_EnumerateMatchingDevices(HDEVINFO DeviceInfoSet,
2213 LPCWSTR parent, HKEY key, const GUID *class, DWORD flags)
2215 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2216 DWORD i, len;
2217 WCHAR subKeyName[MAX_PATH];
2218 LONG l = ERROR_SUCCESS;
2220 TRACE("%s\n", debugstr_w(parent));
2222 for (i = 0; !l; i++)
2224 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2225 l = RegEnumKeyExW(key, i, subKeyName, &len, NULL, NULL, NULL, NULL);
2226 if (!l)
2228 HKEY subKey;
2230 l = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
2231 if (!l)
2233 TRACE("%s\n", debugstr_w(subKeyName));
2234 SETUPDI_EnumerateMatchingDeviceInstances(set, parent,
2235 subKeyName, subKey, class, flags);
2236 RegCloseKey(subKey);
2238 /* Allow enumeration to continue */
2239 l = ERROR_SUCCESS;
2244 static void SETUPDI_EnumerateDevices(HDEVINFO DeviceInfoSet, const GUID *class,
2245 LPCWSTR enumstr, DWORD flags)
2247 HKEY enumKey;
2248 LONG l;
2250 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet, debugstr_guid(class),
2251 debugstr_w(enumstr), flags);
2253 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_READ, NULL,
2254 &enumKey, NULL);
2255 if (enumKey != INVALID_HANDLE_VALUE)
2257 if (enumstr)
2259 HKEY enumStrKey;
2261 l = RegOpenKeyExW(enumKey, enumstr, 0, KEY_READ,
2262 &enumStrKey);
2263 if (!l)
2265 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet, enumstr,
2266 enumStrKey, class, flags);
2267 RegCloseKey(enumStrKey);
2270 else
2272 DWORD i, len;
2273 WCHAR subKeyName[MAX_PATH];
2275 l = ERROR_SUCCESS;
2276 for (i = 0; !l; i++)
2278 len = sizeof(subKeyName) / sizeof(subKeyName[0]);
2279 l = RegEnumKeyExW(enumKey, i, subKeyName, &len, NULL,
2280 NULL, NULL, NULL);
2281 if (!l)
2283 HKEY subKey;
2285 l = RegOpenKeyExW(enumKey, subKeyName, 0, KEY_READ,
2286 &subKey);
2287 if (!l)
2289 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet,
2290 subKeyName, subKey, class, flags);
2291 RegCloseKey(subKey);
2293 /* Allow enumeration to continue */
2294 l = ERROR_SUCCESS;
2298 RegCloseKey(enumKey);
2302 /***********************************************************************
2303 * SetupDiGetClassDevsW (SETUPAPI.@)
2305 HDEVINFO WINAPI SetupDiGetClassDevsW(
2306 CONST GUID *class,
2307 LPCWSTR enumstr,
2308 HWND parent,
2309 DWORD flags)
2311 return SetupDiGetClassDevsExW(class, enumstr, parent, flags, NULL, NULL,
2312 NULL);
2315 /***********************************************************************
2316 * SetupDiGetClassDevsExW (SETUPAPI.@)
2318 HDEVINFO WINAPI SetupDiGetClassDevsExW(
2319 CONST GUID *class,
2320 PCWSTR enumstr,
2321 HWND parent,
2322 DWORD flags,
2323 HDEVINFO deviceset,
2324 PCWSTR machine,
2325 PVOID reserved)
2327 static const DWORD unsupportedFlags = DIGCF_DEFAULT | DIGCF_PRESENT |
2328 DIGCF_PROFILE;
2329 HDEVINFO set;
2331 TRACE("%s %s %p 0x%08x %p %s %p\n", debugstr_guid(class),
2332 debugstr_w(enumstr), parent, flags, deviceset, debugstr_w(machine),
2333 reserved);
2335 if (!(flags & DIGCF_ALLCLASSES) && !class)
2337 SetLastError(ERROR_INVALID_PARAMETER);
2338 return NULL;
2340 if (flags & unsupportedFlags)
2341 WARN("unsupported flags %08x\n", flags & unsupportedFlags);
2342 if (deviceset)
2343 set = deviceset;
2344 else
2345 set = SetupDiCreateDeviceInfoListExW(class, parent, machine, reserved);
2346 if (set)
2348 if (machine)
2349 FIXME("%s: unimplemented for remote machines\n",
2350 debugstr_w(machine));
2351 else if (flags & DIGCF_DEVICEINTERFACE)
2352 SETUPDI_EnumerateInterfaces(set, class, enumstr, flags);
2353 else
2354 SETUPDI_EnumerateDevices(set, class, enumstr, flags);
2356 return set;
2359 /***********************************************************************
2360 * SetupDiGetDeviceInfoListDetailA (SETUPAPI.@)
2362 BOOL WINAPI SetupDiGetDeviceInfoListDetailA(
2363 HDEVINFO DeviceInfoSet,
2364 PSP_DEVINFO_LIST_DETAIL_DATA_A DevInfoData )
2366 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2368 TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2370 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2372 SetLastError(ERROR_INVALID_HANDLE);
2373 return FALSE;
2375 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2377 SetLastError(ERROR_INVALID_HANDLE);
2378 return FALSE;
2380 if (!DevInfoData ||
2381 DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_A))
2383 SetLastError(ERROR_INVALID_PARAMETER);
2384 return FALSE;
2386 memcpy(&DevInfoData->ClassGuid, &set->ClassGuid, sizeof(GUID));
2387 DevInfoData->RemoteMachineHandle = NULL;
2388 DevInfoData->RemoteMachineName[0] = '\0';
2389 return TRUE;
2392 /***********************************************************************
2393 * SetupDiGetDeviceInfoListDetailW (SETUPAPI.@)
2395 BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
2396 HDEVINFO DeviceInfoSet,
2397 PSP_DEVINFO_LIST_DETAIL_DATA_W DevInfoData )
2399 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2401 TRACE("%p %p\n", DeviceInfoSet, DevInfoData);
2403 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2405 SetLastError(ERROR_INVALID_HANDLE);
2406 return FALSE;
2408 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2410 SetLastError(ERROR_INVALID_HANDLE);
2411 return FALSE;
2413 if (!DevInfoData ||
2414 DevInfoData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W))
2416 SetLastError(ERROR_INVALID_PARAMETER);
2417 return FALSE;
2419 memcpy(&DevInfoData->ClassGuid, &set->ClassGuid, sizeof(GUID));
2420 DevInfoData->RemoteMachineHandle = NULL;
2421 DevInfoData->RemoteMachineName[0] = '\0';
2422 return TRUE;
2425 /***********************************************************************
2426 * SetupDiCreateDeviceInterfaceA (SETUPAPI.@)
2428 BOOL WINAPI SetupDiCreateDeviceInterfaceA(
2429 HDEVINFO DeviceInfoSet,
2430 PSP_DEVINFO_DATA DeviceInfoData,
2431 const GUID *InterfaceClassGuid,
2432 PCSTR ReferenceString,
2433 DWORD CreationFlags,
2434 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2436 BOOL ret;
2437 LPWSTR ReferenceStringW = NULL;
2439 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2440 debugstr_guid(InterfaceClassGuid), debugstr_a(ReferenceString),
2441 CreationFlags, DeviceInterfaceData);
2443 if (ReferenceString)
2445 ReferenceStringW = MultiByteToUnicode(ReferenceString, CP_ACP);
2446 if (ReferenceStringW == NULL) return FALSE;
2449 ret = SetupDiCreateDeviceInterfaceW(DeviceInfoSet, DeviceInfoData,
2450 InterfaceClassGuid, ReferenceStringW, CreationFlags,
2451 DeviceInterfaceData);
2453 MyFree(ReferenceStringW);
2455 return ret;
2458 /***********************************************************************
2459 * SetupDiCreateDeviceInterfaceW (SETUPAPI.@)
2461 BOOL WINAPI SetupDiCreateDeviceInterfaceW(
2462 HDEVINFO DeviceInfoSet,
2463 PSP_DEVINFO_DATA DeviceInfoData,
2464 const GUID *InterfaceClassGuid,
2465 PCWSTR ReferenceString,
2466 DWORD CreationFlags,
2467 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2469 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2470 struct DeviceInfo *devInfo;
2471 SP_DEVICE_INTERFACE_DATA *iface = NULL;
2472 BOOL ret;
2474 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet, DeviceInfoData,
2475 debugstr_guid(InterfaceClassGuid), debugstr_w(ReferenceString),
2476 CreationFlags, DeviceInterfaceData);
2478 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
2480 SetLastError(ERROR_INVALID_HANDLE);
2481 return FALSE;
2483 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2485 SetLastError(ERROR_INVALID_HANDLE);
2486 return FALSE;
2488 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
2489 || !DeviceInfoData->Reserved)
2491 SetLastError(ERROR_INVALID_PARAMETER);
2492 return FALSE;
2494 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
2495 if (devInfo->set != set)
2497 SetLastError(ERROR_INVALID_PARAMETER);
2498 return FALSE;
2500 if (!InterfaceClassGuid)
2502 SetLastError(ERROR_INVALID_USER_BUFFER);
2503 return FALSE;
2505 if ((ret = SETUPDI_AddInterfaceInstance(DeviceInfoData, InterfaceClassGuid,
2506 ReferenceString, &iface)))
2508 if (DeviceInterfaceData)
2510 if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2512 SetLastError(ERROR_INVALID_USER_BUFFER);
2513 ret = FALSE;
2515 else
2516 memcpy(DeviceInterfaceData, iface, sizeof(*iface));
2519 return ret;
2522 /***********************************************************************
2523 * SetupDiCreateDeviceInterfaceRegKeyA (SETUPAPI.@)
2525 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyA(
2526 HDEVINFO DeviceInfoSet,
2527 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2528 DWORD Reserved,
2529 REGSAM samDesired,
2530 HINF InfHandle,
2531 PCSTR InfSectionName)
2533 HKEY key;
2534 PWSTR InfSectionNameW = NULL;
2536 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2537 samDesired, InfHandle, InfSectionName);
2538 if (InfHandle)
2540 if (!InfSectionName)
2542 SetLastError(ERROR_INVALID_PARAMETER);
2543 return INVALID_HANDLE_VALUE;
2545 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
2546 if (!InfSectionNameW)
2547 return INVALID_HANDLE_VALUE;
2549 key = SetupDiCreateDeviceInterfaceRegKeyW(DeviceInfoSet,
2550 DeviceInterfaceData, Reserved, samDesired, InfHandle,
2551 InfSectionNameW);
2552 MyFree(InfSectionNameW);
2553 return key;
2556 static PWSTR SETUPDI_GetInstancePath(struct InterfaceInfo *ifaceInfo)
2558 static const WCHAR hash[] = {'#',0};
2559 PWSTR instancePath = NULL;
2561 if (ifaceInfo->referenceString)
2563 instancePath = HeapAlloc(GetProcessHeap(), 0,
2564 (lstrlenW(ifaceInfo->referenceString) + 2) * sizeof(WCHAR));
2565 if (instancePath)
2567 lstrcpyW(instancePath, hash);
2568 lstrcatW(instancePath, ifaceInfo->referenceString);
2570 else
2571 SetLastError(ERROR_OUTOFMEMORY);
2573 else
2575 instancePath = HeapAlloc(GetProcessHeap(), 0,
2576 (lstrlenW(hash) + 1) * sizeof(WCHAR));
2577 if (instancePath)
2578 lstrcpyW(instancePath, hash);
2580 return instancePath;
2583 /***********************************************************************
2584 * SetupDiCreateDeviceInterfaceRegKeyW (SETUPAPI.@)
2586 HKEY WINAPI SetupDiCreateDeviceInterfaceRegKeyW(
2587 HDEVINFO DeviceInfoSet,
2588 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2589 DWORD Reserved,
2590 REGSAM samDesired,
2591 HINF InfHandle,
2592 PCWSTR InfSectionName)
2594 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2595 HKEY key = INVALID_HANDLE_VALUE, interfacesKey;
2596 LONG l;
2598 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet, DeviceInterfaceData, Reserved,
2599 samDesired, InfHandle, InfSectionName);
2601 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2602 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2604 SetLastError(ERROR_INVALID_HANDLE);
2605 return INVALID_HANDLE_VALUE;
2607 if (!DeviceInterfaceData ||
2608 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2609 !DeviceInterfaceData->Reserved)
2611 SetLastError(ERROR_INVALID_PARAMETER);
2612 return INVALID_HANDLE_VALUE;
2614 if (InfHandle && !InfSectionName)
2616 SetLastError(ERROR_INVALID_PARAMETER);
2617 return INVALID_HANDLE_VALUE;
2619 if (!(l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, DeviceClasses, 0, NULL, 0,
2620 samDesired, NULL, &interfacesKey, NULL)))
2622 HKEY parent;
2623 WCHAR bracedGuidString[39];
2625 SETUPDI_GuidToString(&DeviceInterfaceData->InterfaceClassGuid,
2626 bracedGuidString);
2627 if (!(l = RegCreateKeyExW(interfacesKey, bracedGuidString, 0, NULL, 0,
2628 samDesired, NULL, &parent, NULL)))
2630 struct InterfaceInfo *ifaceInfo =
2631 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2632 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2634 if (instancePath)
2636 LONG l;
2638 l = RegCreateKeyExW(parent, instancePath, 0, NULL, 0,
2639 samDesired, NULL, &key, NULL);
2640 if (l)
2642 SetLastError(l);
2643 key = INVALID_HANDLE_VALUE;
2645 else if (InfHandle)
2646 FIXME("INF section installation unsupported\n");
2648 HeapFree(GetProcessHeap(), 0, instancePath);
2649 RegCloseKey(parent);
2651 else
2652 SetLastError(l);
2653 RegCloseKey(interfacesKey);
2655 else
2656 SetLastError(l);
2657 return key;
2660 /***********************************************************************
2661 * SetupDiDeleteDeviceInterfaceRegKey (SETUPAPI.@)
2663 BOOL WINAPI SetupDiDeleteDeviceInterfaceRegKey(
2664 HDEVINFO DeviceInfoSet,
2665 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2666 DWORD Reserved)
2668 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2669 HKEY parent;
2670 BOOL ret = FALSE;
2672 TRACE("%p %p %d\n", DeviceInfoSet, DeviceInterfaceData, Reserved);
2674 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2675 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2677 SetLastError(ERROR_INVALID_HANDLE);
2678 return FALSE;
2680 if (!DeviceInterfaceData ||
2681 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2682 !DeviceInterfaceData->Reserved)
2684 SetLastError(ERROR_INVALID_PARAMETER);
2685 return FALSE;
2687 parent = SetupDiOpenClassRegKeyExW(&DeviceInterfaceData->InterfaceClassGuid,
2688 KEY_ALL_ACCESS, DIOCR_INTERFACE, NULL, NULL);
2689 if (parent != INVALID_HANDLE_VALUE)
2691 struct InterfaceInfo *ifaceInfo =
2692 (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2693 PWSTR instancePath = SETUPDI_GetInstancePath(ifaceInfo);
2695 if (instancePath)
2697 LONG l = RegDeleteKeyW(parent, instancePath);
2699 if (l)
2700 SetLastError(l);
2701 else
2702 ret = TRUE;
2703 HeapFree(GetProcessHeap(), 0, instancePath);
2705 RegCloseKey(parent);
2707 return ret;
2710 /***********************************************************************
2711 * SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2713 * PARAMS
2714 * DeviceInfoSet [I] Set of devices from which to enumerate
2715 * interfaces
2716 * DeviceInfoData [I] (Optional) If specified, a specific device
2717 * instance from which to enumerate interfaces.
2718 * If it isn't specified, all interfaces for all
2719 * devices in the set are enumerated.
2720 * InterfaceClassGuid [I] The interface class to enumerate.
2721 * MemberIndex [I] An index of the interface instance to enumerate.
2722 * A caller should start with MemberIndex set to 0,
2723 * and continue until the function fails with
2724 * ERROR_NO_MORE_ITEMS.
2725 * DeviceInterfaceData [I/O] Returns an enumerated interface. Its cbSize
2726 * member must be set to
2727 * sizeof(SP_DEVICE_INTERFACE_DATA).
2729 * RETURNS
2730 * Success: non-zero value.
2731 * Failure: FALSE. Call GetLastError() for more info.
2733 BOOL WINAPI SetupDiEnumDeviceInterfaces(
2734 HDEVINFO DeviceInfoSet,
2735 PSP_DEVINFO_DATA DeviceInfoData,
2736 CONST GUID * InterfaceClassGuid,
2737 DWORD MemberIndex,
2738 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2740 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2741 BOOL ret = FALSE;
2743 TRACE("%p, %p, %s, %d, %p\n", DeviceInfoSet, DeviceInfoData,
2744 debugstr_guid(InterfaceClassGuid), MemberIndex, DeviceInterfaceData);
2746 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2747 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2749 SetLastError(ERROR_INVALID_HANDLE);
2750 return FALSE;
2752 if (DeviceInfoData && (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA) ||
2753 !DeviceInfoData->Reserved))
2755 SetLastError(ERROR_INVALID_PARAMETER);
2756 return FALSE;
2758 if (!DeviceInterfaceData ||
2759 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2761 SetLastError(ERROR_INVALID_PARAMETER);
2762 return FALSE;
2764 if (DeviceInfoData)
2766 struct DeviceInfo *devInfo =
2767 (struct DeviceInfo *)DeviceInfoData->Reserved;
2768 struct InterfaceInstances *iface;
2770 if ((ret = SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface)))
2772 if (MemberIndex < iface->cInstances)
2773 memcpy(DeviceInterfaceData, &iface->instances[MemberIndex],
2774 sizeof(SP_DEVICE_INTERFACE_DATA));
2775 else
2777 SetLastError(ERROR_NO_MORE_ITEMS);
2778 ret = FALSE;
2781 else
2782 SetLastError(ERROR_NO_MORE_ITEMS);
2784 else
2786 DWORD i, cEnumerated = 0;
2787 BOOL found = FALSE;
2789 for (i = 0; !found && cEnumerated < MemberIndex + 1 &&
2790 i < set->cDevices; i++)
2792 struct DeviceInfo *devInfo =
2793 (struct DeviceInfo *)set->devices[i].Reserved;
2794 struct InterfaceInstances *iface;
2796 if (SETUPDI_FindInterface(devInfo, InterfaceClassGuid, &iface))
2798 if (cEnumerated + iface->cInstances < MemberIndex + 1)
2799 cEnumerated += iface->cInstances;
2800 else
2802 DWORD instanceIndex = MemberIndex - cEnumerated;
2804 memcpy(DeviceInterfaceData,
2805 &iface->instances[instanceIndex],
2806 sizeof(SP_DEVICE_INTERFACE_DATA));
2807 cEnumerated += instanceIndex + 1;
2808 found = TRUE;
2809 ret = TRUE;
2813 if (!found)
2814 SetLastError(ERROR_NO_MORE_ITEMS);
2816 return ret;
2819 /***********************************************************************
2820 * SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2822 * Destroy a DeviceInfoList and free all used memory of the list.
2824 * PARAMS
2825 * devinfo [I] DeviceInfoList pointer to list to destroy
2827 * RETURNS
2828 * Success: non zero value.
2829 * Failure: zero value.
2831 BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
2833 BOOL ret = FALSE;
2835 TRACE("%p\n", devinfo);
2836 if (devinfo && devinfo != INVALID_HANDLE_VALUE)
2838 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
2840 if (list->magic == SETUP_DEVICE_INFO_SET_MAGIC)
2842 DWORD i;
2844 for (i = 0; i < list->cDevices; i++)
2845 SETUPDI_FreeDeviceInfo(
2846 (struct DeviceInfo *)list->devices[i].Reserved);
2847 HeapFree(GetProcessHeap(), 0, list->devices);
2848 HeapFree(GetProcessHeap(), 0, list);
2849 ret = TRUE;
2853 if (ret == FALSE)
2854 SetLastError(ERROR_INVALID_HANDLE);
2856 return ret;
2859 /***********************************************************************
2860 * SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2862 BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
2863 HDEVINFO DeviceInfoSet,
2864 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2865 PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
2866 DWORD DeviceInterfaceDetailDataSize,
2867 PDWORD RequiredSize,
2868 PSP_DEVINFO_DATA DeviceInfoData)
2870 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2871 struct InterfaceInfo *info;
2872 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath)
2873 + 1;
2874 BOOL ret = FALSE;
2876 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2877 DeviceInterfaceData, DeviceInterfaceDetailData,
2878 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2880 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2881 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2883 SetLastError(ERROR_INVALID_HANDLE);
2884 return FALSE;
2886 if (!DeviceInterfaceData ||
2887 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2888 !DeviceInterfaceData->Reserved)
2890 SetLastError(ERROR_INVALID_PARAMETER);
2891 return FALSE;
2893 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize !=
2894 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath) + sizeof(char)))
2896 SetLastError(ERROR_INVALID_USER_BUFFER);
2897 return FALSE;
2899 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2901 SetLastError(ERROR_INVALID_USER_BUFFER);
2902 return FALSE;
2904 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2905 if (info->symbolicLink)
2906 bytesNeeded += WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2907 NULL, 0, NULL, NULL);
2908 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2910 if (info->symbolicLink)
2911 WideCharToMultiByte(CP_ACP, 0, info->symbolicLink, -1,
2912 DeviceInterfaceDetailData->DevicePath,
2913 DeviceInterfaceDetailDataSize -
2914 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath),
2915 NULL, NULL);
2916 else
2917 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2918 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2919 memcpy(DeviceInfoData, info->device, sizeof(SP_DEVINFO_DATA));
2920 ret = TRUE;
2922 else
2924 if (RequiredSize)
2925 *RequiredSize = bytesNeeded;
2926 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2928 return ret;
2931 /***********************************************************************
2932 * SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
2934 BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
2935 HDEVINFO DeviceInfoSet,
2936 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2937 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
2938 DWORD DeviceInterfaceDetailDataSize,
2939 PDWORD RequiredSize,
2940 PSP_DEVINFO_DATA DeviceInfoData)
2942 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
2943 struct InterfaceInfo *info;
2944 DWORD bytesNeeded = offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)
2945 + sizeof(WCHAR); /* include NULL terminator */
2946 BOOL ret = FALSE;
2948 TRACE("(%p, %p, %p, %d, %p, %p)\n", DeviceInfoSet,
2949 DeviceInterfaceData, DeviceInterfaceDetailData,
2950 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2952 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE ||
2953 set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
2955 SetLastError(ERROR_INVALID_HANDLE);
2956 return FALSE;
2958 if (!DeviceInterfaceData ||
2959 DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA) ||
2960 !DeviceInterfaceData->Reserved)
2962 SetLastError(ERROR_INVALID_PARAMETER);
2963 return FALSE;
2965 if (DeviceInterfaceDetailData && (DeviceInterfaceDetailData->cbSize <
2966 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR) ||
2967 DeviceInterfaceDetailData->cbSize > sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W)))
2969 SetLastError(ERROR_INVALID_USER_BUFFER);
2970 return FALSE;
2972 if (!DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize)
2974 SetLastError(ERROR_INVALID_USER_BUFFER);
2975 return FALSE;
2977 info = (struct InterfaceInfo *)DeviceInterfaceData->Reserved;
2978 if (info->symbolicLink)
2979 bytesNeeded += lstrlenW(info->symbolicLink);
2980 if (DeviceInterfaceDetailDataSize >= bytesNeeded)
2982 if (info->symbolicLink)
2983 lstrcpyW(DeviceInterfaceDetailData->DevicePath, info->symbolicLink);
2984 else
2985 DeviceInterfaceDetailData->DevicePath[0] = '\0';
2986 if (DeviceInfoData && DeviceInfoData->cbSize == sizeof(SP_DEVINFO_DATA))
2987 memcpy(DeviceInfoData, info->device, sizeof(SP_DEVINFO_DATA));
2988 ret = TRUE;
2990 else
2992 if (RequiredSize)
2993 *RequiredSize = bytesNeeded;
2994 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2996 return ret;
2999 struct PropertyMapEntry
3001 DWORD regType;
3002 LPCSTR nameA;
3003 LPCWSTR nameW;
3006 static struct PropertyMapEntry PropertyMap[] = {
3007 { REG_SZ, "DeviceDesc", DeviceDesc },
3008 { REG_MULTI_SZ, "HardwareId", HardwareId },
3009 { REG_MULTI_SZ, "CompatibleIDs", CompatibleIDs },
3010 { 0, NULL, NULL }, /* SPDRP_UNUSED0 */
3011 { REG_SZ, "Service", Service },
3012 { 0, NULL, NULL }, /* SPDRP_UNUSED1 */
3013 { 0, NULL, NULL }, /* SPDRP_UNUSED2 */
3014 { REG_SZ, "Class", Class },
3015 { REG_SZ, "ClassGUID", ClassGUID },
3016 { REG_SZ, "Driver", Driver },
3017 { REG_DWORD, "ConfigFlags", ConfigFlags },
3018 { REG_SZ, "Mfg", Mfg },
3019 { REG_SZ, "FriendlyName", FriendlyName },
3020 { REG_SZ, "LocationInformation", LocationInformation },
3021 { 0, NULL, NULL }, /* SPDRP_PHYSICAL_DEVICE_OBJECT_NAME */
3022 { REG_DWORD, "Capabilities", Capabilities },
3023 { REG_DWORD, "UINumber", UINumber },
3024 { REG_MULTI_SZ, "UpperFilters", UpperFilters },
3025 { REG_MULTI_SZ, "LowerFilters", LowerFilters },
3028 /***********************************************************************
3029 * SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
3031 BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
3032 HDEVINFO DeviceInfoSet,
3033 PSP_DEVINFO_DATA DeviceInfoData,
3034 DWORD Property,
3035 PDWORD PropertyRegDataType,
3036 PBYTE PropertyBuffer,
3037 DWORD PropertyBufferSize,
3038 PDWORD RequiredSize)
3040 BOOL ret = FALSE;
3041 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3042 struct DeviceInfo *devInfo;
3044 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
3045 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
3046 RequiredSize);
3048 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3050 SetLastError(ERROR_INVALID_HANDLE);
3051 return FALSE;
3053 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3055 SetLastError(ERROR_INVALID_HANDLE);
3056 return FALSE;
3058 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3059 || !DeviceInfoData->Reserved)
3061 SetLastError(ERROR_INVALID_PARAMETER);
3062 return FALSE;
3064 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3065 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3066 && PropertyMap[Property].nameA)
3068 DWORD size = PropertyBufferSize;
3069 LONG l = RegQueryValueExA(devInfo->key, PropertyMap[Property].nameA,
3070 NULL, PropertyRegDataType, PropertyBuffer, &size);
3072 if (RequiredSize)
3073 *RequiredSize = size;
3074 if (!PropertyBuffer)
3075 ; /* do nothing, ret is already FALSE, last error is already set */
3076 else if (!l)
3077 ret = TRUE;
3078 else
3079 SetLastError(l);
3081 return ret;
3084 /***********************************************************************
3085 * SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
3087 BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
3088 HDEVINFO DeviceInfoSet,
3089 PSP_DEVINFO_DATA DeviceInfoData,
3090 DWORD Property,
3091 PDWORD PropertyRegDataType,
3092 PBYTE PropertyBuffer,
3093 DWORD PropertyBufferSize,
3094 PDWORD RequiredSize)
3096 BOOL ret = FALSE;
3097 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3098 struct DeviceInfo *devInfo;
3100 TRACE("%04x %p %d %p %p %d %p\n", (DWORD)DeviceInfoSet, DeviceInfoData,
3101 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
3102 RequiredSize);
3104 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3106 SetLastError(ERROR_INVALID_HANDLE);
3107 return FALSE;
3109 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3111 SetLastError(ERROR_INVALID_HANDLE);
3112 return FALSE;
3114 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3115 || !DeviceInfoData->Reserved)
3117 SetLastError(ERROR_INVALID_PARAMETER);
3118 return FALSE;
3120 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3121 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3122 && PropertyMap[Property].nameW)
3124 DWORD size = PropertyBufferSize;
3125 LONG l = RegQueryValueExW(devInfo->key, PropertyMap[Property].nameW,
3126 NULL, PropertyRegDataType, PropertyBuffer, &size);
3128 if (RequiredSize)
3129 *RequiredSize = size;
3130 if (!PropertyBuffer)
3131 ; /* do nothing, ret is already FALSE, last error is already set */
3132 else if (!l)
3133 ret = TRUE;
3134 else
3135 SetLastError(l);
3137 return ret;
3140 /***********************************************************************
3141 * SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
3143 BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
3144 HDEVINFO DeviceInfoSet,
3145 PSP_DEVINFO_DATA DeviceInfoData,
3146 DWORD Property,
3147 const BYTE *PropertyBuffer,
3148 DWORD PropertyBufferSize)
3150 BOOL ret = FALSE;
3151 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3152 struct DeviceInfo *devInfo;
3154 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
3155 PropertyBuffer, PropertyBufferSize);
3157 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3159 SetLastError(ERROR_INVALID_HANDLE);
3160 return FALSE;
3162 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3164 SetLastError(ERROR_INVALID_HANDLE);
3165 return FALSE;
3167 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3168 || !DeviceInfoData->Reserved)
3170 SetLastError(ERROR_INVALID_PARAMETER);
3171 return FALSE;
3173 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3174 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3175 && PropertyMap[Property].nameA)
3177 LONG l = RegSetValueExA(devInfo->key, PropertyMap[Property].nameA, 0,
3178 PropertyMap[Property].regType, PropertyBuffer,
3179 PropertyBufferSize);
3180 if (!l)
3181 ret = TRUE;
3182 else
3183 SetLastError(l);
3185 return ret;
3188 /***********************************************************************
3189 * SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
3191 BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
3192 HDEVINFO DeviceInfoSet,
3193 PSP_DEVINFO_DATA DeviceInfoData,
3194 DWORD Property,
3195 const BYTE *PropertyBuffer,
3196 DWORD PropertyBufferSize)
3198 BOOL ret = FALSE;
3199 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3200 struct DeviceInfo *devInfo;
3202 TRACE("%p %p %d %p %d\n", DeviceInfoSet, DeviceInfoData, Property,
3203 PropertyBuffer, PropertyBufferSize);
3205 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3207 SetLastError(ERROR_INVALID_HANDLE);
3208 return FALSE;
3210 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3212 SetLastError(ERROR_INVALID_HANDLE);
3213 return FALSE;
3215 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3216 || !DeviceInfoData->Reserved)
3218 SetLastError(ERROR_INVALID_PARAMETER);
3219 return FALSE;
3221 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3222 if (Property < sizeof(PropertyMap) / sizeof(PropertyMap[0])
3223 && PropertyMap[Property].nameW)
3225 LONG l = RegSetValueExW(devInfo->key, PropertyMap[Property].nameW, 0,
3226 PropertyMap[Property].regType, PropertyBuffer,
3227 PropertyBufferSize);
3228 if (!l)
3229 ret = TRUE;
3230 else
3231 SetLastError(l);
3233 return ret;
3236 /***********************************************************************
3237 * SetupDiInstallClassA (SETUPAPI.@)
3239 BOOL WINAPI SetupDiInstallClassA(
3240 HWND hwndParent,
3241 PCSTR InfFileName,
3242 DWORD Flags,
3243 HSPFILEQ FileQueue)
3245 UNICODE_STRING FileNameW;
3246 BOOL Result;
3248 if (!InfFileName)
3250 SetLastError(ERROR_INVALID_PARAMETER);
3251 return FALSE;
3253 if (!RtlCreateUnicodeStringFromAsciiz(&FileNameW, InfFileName))
3255 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3256 return FALSE;
3259 Result = SetupDiInstallClassW(hwndParent, FileNameW.Buffer, Flags, FileQueue);
3261 RtlFreeUnicodeString(&FileNameW);
3263 return Result;
3266 static HKEY CreateClassKey(HINF hInf)
3268 static const WCHAR slash[] = { '\\',0 };
3269 WCHAR FullBuffer[MAX_PATH];
3270 WCHAR Buffer[MAX_PATH];
3271 DWORD RequiredSize;
3272 HKEY hClassKey;
3274 if (!SetupGetLineTextW(NULL,
3275 hInf,
3276 Version,
3277 ClassGUID,
3278 Buffer,
3279 MAX_PATH,
3280 &RequiredSize))
3282 return INVALID_HANDLE_VALUE;
3285 lstrcpyW(FullBuffer, ControlClass);
3286 lstrcatW(FullBuffer, slash);
3287 lstrcatW(FullBuffer, Buffer);
3289 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3290 FullBuffer,
3292 KEY_ALL_ACCESS,
3293 &hClassKey))
3295 if (!SetupGetLineTextW(NULL,
3296 hInf,
3297 Version,
3298 Class,
3299 Buffer,
3300 MAX_PATH,
3301 &RequiredSize))
3303 return INVALID_HANDLE_VALUE;
3306 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
3307 FullBuffer,
3309 NULL,
3310 REG_OPTION_NON_VOLATILE,
3311 KEY_ALL_ACCESS,
3312 NULL,
3313 &hClassKey,
3314 NULL))
3316 return INVALID_HANDLE_VALUE;
3321 if (RegSetValueExW(hClassKey,
3322 Class,
3324 REG_SZ,
3325 (LPBYTE)Buffer,
3326 RequiredSize * sizeof(WCHAR)))
3328 RegCloseKey(hClassKey);
3329 RegDeleteKeyW(HKEY_LOCAL_MACHINE,
3330 FullBuffer);
3331 return INVALID_HANDLE_VALUE;
3334 return hClassKey;
3337 /***********************************************************************
3338 * SetupDiInstallClassW (SETUPAPI.@)
3340 BOOL WINAPI SetupDiInstallClassW(
3341 HWND hwndParent,
3342 PCWSTR InfFileName,
3343 DWORD Flags,
3344 HSPFILEQ FileQueue)
3346 WCHAR SectionName[MAX_PATH];
3347 DWORD SectionNameLength = 0;
3348 HINF hInf;
3349 BOOL bFileQueueCreated = FALSE;
3350 HKEY hClassKey;
3353 FIXME("\n");
3355 if (!InfFileName)
3357 SetLastError(ERROR_INVALID_PARAMETER);
3358 return FALSE;
3360 if ((Flags & DI_NOVCP) && (FileQueue == NULL || FileQueue == INVALID_HANDLE_VALUE))
3362 SetLastError(ERROR_INVALID_PARAMETER);
3363 return FALSE;
3366 /* Open the .inf file */
3367 hInf = SetupOpenInfFileW(InfFileName,
3368 NULL,
3369 INF_STYLE_WIN4,
3370 NULL);
3371 if (hInf == INVALID_HANDLE_VALUE)
3374 return FALSE;
3377 /* Create or open the class registry key 'HKLM\\CurrentControlSet\\Class\\{GUID}' */
3378 hClassKey = CreateClassKey(hInf);
3379 if (hClassKey == INVALID_HANDLE_VALUE)
3381 SetupCloseInfFile(hInf);
3382 return FALSE;
3386 /* Try to append a layout file */
3387 SetupOpenAppendInfFileW(NULL, hInf, NULL);
3389 /* Retrieve the actual section name */
3390 SetupDiGetActualSectionToInstallW(hInf,
3391 ClassInstall32,
3392 SectionName,
3393 MAX_PATH,
3394 &SectionNameLength,
3395 NULL);
3397 #if 0
3398 if (!(Flags & DI_NOVCP))
3400 FileQueue = SetupOpenFileQueue();
3401 if (FileQueue == INVALID_HANDLE_VALUE)
3403 SetupCloseInfFile(hInf);
3404 return FALSE;
3407 bFileQueueCreated = TRUE;
3410 #endif
3412 SetupInstallFromInfSectionW(NULL,
3413 hInf,
3414 SectionName,
3415 SPINST_COPYINF | SPINST_FILES | SPINST_REGISTRY,
3416 hClassKey,
3417 NULL,
3419 NULL,
3420 NULL,
3421 INVALID_HANDLE_VALUE,
3422 NULL);
3424 /* FIXME: More code! */
3426 if (bFileQueueCreated)
3427 SetupCloseFileQueue(FileQueue);
3429 SetupCloseInfFile(hInf);
3431 return TRUE;
3435 /***********************************************************************
3436 * SetupDiOpenClassRegKey (SETUPAPI.@)
3438 HKEY WINAPI SetupDiOpenClassRegKey(
3439 const GUID* ClassGuid,
3440 REGSAM samDesired)
3442 return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3443 DIOCR_INSTALLER, NULL, NULL);
3447 /***********************************************************************
3448 * SetupDiOpenClassRegKeyExA (SETUPAPI.@)
3450 HKEY WINAPI SetupDiOpenClassRegKeyExA(
3451 const GUID* ClassGuid,
3452 REGSAM samDesired,
3453 DWORD Flags,
3454 PCSTR MachineName,
3455 PVOID Reserved)
3457 PWSTR MachineNameW = NULL;
3458 HKEY hKey;
3460 TRACE("\n");
3462 if (MachineName)
3464 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
3465 if (MachineNameW == NULL)
3466 return INVALID_HANDLE_VALUE;
3469 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3470 Flags, MachineNameW, Reserved);
3472 MyFree(MachineNameW);
3474 return hKey;
3478 /***********************************************************************
3479 * SetupDiOpenClassRegKeyExW (SETUPAPI.@)
3481 HKEY WINAPI SetupDiOpenClassRegKeyExW(
3482 const GUID* ClassGuid,
3483 REGSAM samDesired,
3484 DWORD Flags,
3485 PCWSTR MachineName,
3486 PVOID Reserved)
3488 HKEY hClassesKey;
3489 HKEY key;
3490 LPCWSTR lpKeyName;
3491 LONG l;
3493 if (MachineName != NULL)
3495 FIXME("Remote access not supported yet!\n");
3496 return INVALID_HANDLE_VALUE;
3499 if (Flags == DIOCR_INSTALLER)
3501 lpKeyName = ControlClass;
3503 else if (Flags == DIOCR_INTERFACE)
3505 lpKeyName = DeviceClasses;
3507 else
3509 ERR("Invalid Flags parameter!\n");
3510 SetLastError(ERROR_INVALID_PARAMETER);
3511 return INVALID_HANDLE_VALUE;
3514 if (!ClassGuid)
3516 if ((l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3517 lpKeyName,
3519 samDesired,
3520 &hClassesKey)))
3522 SetLastError(l);
3523 hClassesKey = INVALID_HANDLE_VALUE;
3525 key = hClassesKey;
3527 else
3529 WCHAR bracedGuidString[39];
3531 SETUPDI_GuidToString(ClassGuid, bracedGuidString);
3533 if (!(l = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
3534 lpKeyName,
3536 samDesired,
3537 &hClassesKey)))
3539 if ((l = RegOpenKeyExW(hClassesKey,
3540 bracedGuidString,
3542 samDesired,
3543 &key)))
3545 SetLastError(l);
3546 key = INVALID_HANDLE_VALUE;
3548 RegCloseKey(hClassesKey);
3550 else
3552 SetLastError(l);
3553 key = INVALID_HANDLE_VALUE;
3556 return key;
3559 /***********************************************************************
3560 * SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3562 BOOL WINAPI SetupDiOpenDeviceInterfaceW(
3563 HDEVINFO DeviceInfoSet,
3564 PCWSTR DevicePath,
3565 DWORD OpenFlags,
3566 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3568 FIXME("%p %s %08x %p\n",
3569 DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
3570 return FALSE;
3573 /***********************************************************************
3574 * SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3576 BOOL WINAPI SetupDiOpenDeviceInterfaceA(
3577 HDEVINFO DeviceInfoSet,
3578 PCSTR DevicePath,
3579 DWORD OpenFlags,
3580 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3582 FIXME("%p %s %08x %p\n", DeviceInfoSet,
3583 debugstr_a(DevicePath), OpenFlags, DeviceInterfaceData);
3584 return FALSE;
3587 /***********************************************************************
3588 * SetupDiSetClassInstallParamsA (SETUPAPI.@)
3590 BOOL WINAPI SetupDiSetClassInstallParamsA(
3591 HDEVINFO DeviceInfoSet,
3592 PSP_DEVINFO_DATA DeviceInfoData,
3593 PSP_CLASSINSTALL_HEADER ClassInstallParams,
3594 DWORD ClassInstallParamsSize)
3596 FIXME("%p %p %x %u\n",DeviceInfoSet, DeviceInfoData,
3597 ClassInstallParams->InstallFunction, ClassInstallParamsSize);
3598 return FALSE;
3601 /***********************************************************************
3602 * SetupDiCallClassInstaller (SETUPAPI.@)
3604 BOOL WINAPI SetupDiCallClassInstaller(
3605 DI_FUNCTION InstallFunction,
3606 HDEVINFO DeviceInfoSet,
3607 PSP_DEVINFO_DATA DeviceInfoData)
3609 FIXME("%d %p %p\n", InstallFunction, DeviceInfoSet, DeviceInfoData);
3610 return FALSE;
3613 /***********************************************************************
3614 * SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
3616 BOOL WINAPI SetupDiGetDeviceInstallParamsA(
3617 HDEVINFO DeviceInfoSet,
3618 PSP_DEVINFO_DATA DeviceInfoData,
3619 PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
3621 FIXME("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
3622 return FALSE;
3625 static HKEY SETUPDI_OpenDevKey(struct DeviceInfo *devInfo, REGSAM samDesired)
3627 HKEY enumKey, key = INVALID_HANDLE_VALUE;
3628 LONG l;
3630 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
3631 NULL, &enumKey, NULL);
3632 if (!l)
3634 RegOpenKeyExW(enumKey, devInfo->instanceId, 0, samDesired, &key);
3635 RegCloseKey(enumKey);
3637 return key;
3640 static HKEY SETUPDI_OpenDrvKey(struct DeviceInfo *devInfo, REGSAM samDesired)
3642 static const WCHAR slash[] = { '\\',0 };
3643 WCHAR classKeyPath[MAX_PATH];
3644 HKEY classKey, key = INVALID_HANDLE_VALUE;
3645 LONG l;
3647 lstrcpyW(classKeyPath, ControlClass);
3648 lstrcatW(classKeyPath, slash);
3649 SETUPDI_GuidToString(&devInfo->set->ClassGuid,
3650 classKeyPath + lstrlenW(classKeyPath));
3651 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
3652 KEY_ALL_ACCESS, NULL, &classKey, NULL);
3653 if (!l)
3655 static const WCHAR fmt[] = { '%','0','4','u',0 };
3656 WCHAR devId[10];
3658 sprintfW(devId, fmt, devInfo->devId);
3659 RegOpenKeyExW(classKey, devId, 0, samDesired, &key);
3660 RegCloseKey(classKey);
3662 return key;
3665 /***********************************************************************
3666 * SetupDiOpenDevRegKey (SETUPAPI.@)
3668 HKEY WINAPI SetupDiOpenDevRegKey(
3669 HDEVINFO DeviceInfoSet,
3670 PSP_DEVINFO_DATA DeviceInfoData,
3671 DWORD Scope,
3672 DWORD HwProfile,
3673 DWORD KeyType,
3674 REGSAM samDesired)
3676 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3677 struct DeviceInfo *devInfo;
3678 HKEY key = INVALID_HANDLE_VALUE;
3680 TRACE("%p %p %d %d %d %x\n", DeviceInfoSet, DeviceInfoData,
3681 Scope, HwProfile, KeyType, samDesired);
3683 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3685 SetLastError(ERROR_INVALID_HANDLE);
3686 return INVALID_HANDLE_VALUE;
3688 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3690 SetLastError(ERROR_INVALID_HANDLE);
3691 return INVALID_HANDLE_VALUE;
3693 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3694 || !DeviceInfoData->Reserved)
3696 SetLastError(ERROR_INVALID_PARAMETER);
3697 return INVALID_HANDLE_VALUE;
3699 if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
3701 SetLastError(ERROR_INVALID_FLAGS);
3702 return INVALID_HANDLE_VALUE;
3704 if (KeyType != DIREG_DEV && KeyType != DIREG_DRV)
3706 SetLastError(ERROR_INVALID_FLAGS);
3707 return INVALID_HANDLE_VALUE;
3709 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3710 if (devInfo->set != set)
3712 SetLastError(ERROR_INVALID_PARAMETER);
3713 return INVALID_HANDLE_VALUE;
3715 if (devInfo->phantom)
3717 SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
3718 return INVALID_HANDLE_VALUE;
3720 if (Scope != DICS_FLAG_GLOBAL)
3721 FIXME("unimplemented for scope %d\n", Scope);
3722 switch (KeyType)
3724 case DIREG_DEV:
3725 key = SETUPDI_OpenDevKey(devInfo, samDesired);
3726 break;
3727 case DIREG_DRV:
3728 key = SETUPDI_OpenDrvKey(devInfo, samDesired);
3729 break;
3730 default:
3731 WARN("unknown KeyType %d\n", KeyType);
3733 return key;
3736 static BOOL SETUPDI_DeleteDevKey(struct DeviceInfo *devInfo)
3738 HKEY enumKey;
3739 BOOL ret = FALSE;
3740 LONG l;
3742 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, Enum, 0, NULL, 0, KEY_ALL_ACCESS,
3743 NULL, &enumKey, NULL);
3744 if (!l)
3746 ret = RegDeleteTreeW(enumKey, devInfo->instanceId);
3747 RegCloseKey(enumKey);
3749 else
3750 SetLastError(l);
3751 return ret;
3754 static BOOL SETUPDI_DeleteDrvKey(struct DeviceInfo *devInfo)
3756 static const WCHAR slash[] = { '\\',0 };
3757 WCHAR classKeyPath[MAX_PATH];
3758 HKEY classKey;
3759 LONG l;
3760 BOOL ret = FALSE;
3762 lstrcpyW(classKeyPath, ControlClass);
3763 lstrcatW(classKeyPath, slash);
3764 SETUPDI_GuidToString(&devInfo->set->ClassGuid,
3765 classKeyPath + lstrlenW(classKeyPath));
3766 l = RegCreateKeyExW(HKEY_LOCAL_MACHINE, classKeyPath, 0, NULL, 0,
3767 KEY_ALL_ACCESS, NULL, &classKey, NULL);
3768 if (!l)
3770 static const WCHAR fmt[] = { '%','0','4','u',0 };
3771 WCHAR devId[10];
3773 sprintfW(devId, fmt, devInfo->devId);
3774 ret = RegDeleteTreeW(classKey, devId);
3775 RegCloseKey(classKey);
3777 else
3778 SetLastError(l);
3779 return ret;
3782 /***********************************************************************
3783 * SetupDiOpenDevRegKey (SETUPAPI.@)
3785 BOOL WINAPI SetupDiDeleteDevRegKey(
3786 HDEVINFO DeviceInfoSet,
3787 PSP_DEVINFO_DATA DeviceInfoData,
3788 DWORD Scope,
3789 DWORD HwProfile,
3790 DWORD KeyType)
3792 struct DeviceInfoSet *set = (struct DeviceInfoSet *)DeviceInfoSet;
3793 struct DeviceInfo *devInfo;
3794 BOOL ret = FALSE;
3796 TRACE("%p %p %d %d %d\n", DeviceInfoSet, DeviceInfoData, Scope, HwProfile,
3797 KeyType);
3799 if (!DeviceInfoSet || DeviceInfoSet == INVALID_HANDLE_VALUE)
3801 SetLastError(ERROR_INVALID_HANDLE);
3802 return FALSE;
3804 if (set->magic != SETUP_DEVICE_INFO_SET_MAGIC)
3806 SetLastError(ERROR_INVALID_HANDLE);
3807 return FALSE;
3809 if (!DeviceInfoData || DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA)
3810 || !DeviceInfoData->Reserved)
3812 SetLastError(ERROR_INVALID_PARAMETER);
3813 return FALSE;
3815 if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
3817 SetLastError(ERROR_INVALID_FLAGS);
3818 return FALSE;
3820 if (KeyType != DIREG_DEV && KeyType != DIREG_DRV && KeyType != DIREG_BOTH)
3822 SetLastError(ERROR_INVALID_FLAGS);
3823 return FALSE;
3825 devInfo = (struct DeviceInfo *)DeviceInfoData->Reserved;
3826 if (devInfo->set != set)
3828 SetLastError(ERROR_INVALID_PARAMETER);
3829 return FALSE;
3831 if (devInfo->phantom)
3833 SetLastError(ERROR_DEVINFO_NOT_REGISTERED);
3834 return FALSE;
3836 if (Scope != DICS_FLAG_GLOBAL)
3837 FIXME("unimplemented for scope %d\n", Scope);
3838 switch (KeyType)
3840 case DIREG_DEV:
3841 ret = SETUPDI_DeleteDevKey(devInfo);
3842 break;
3843 case DIREG_DRV:
3844 ret = SETUPDI_DeleteDrvKey(devInfo);
3845 break;
3846 case DIREG_BOTH:
3847 ret = SETUPDI_DeleteDevKey(devInfo);
3848 if (ret)
3849 ret = SETUPDI_DeleteDrvKey(devInfo);
3850 break;
3851 default:
3852 WARN("unknown KeyType %d\n", KeyType);
3854 return ret;
3857 /***********************************************************************
3858 * CM_Get_Device_IDA (SETUPAPI.@)
3860 CONFIGRET WINAPI CM_Get_Device_IDA( DEVINST dnDevInst, PSTR Buffer,
3861 ULONG BufferLen, ULONG ulFlags)
3863 struct DeviceInfo *devInfo = GlobalLock((HANDLE)dnDevInst);
3865 TRACE("%x->%p, %p, %u %u\n", dnDevInst, devInfo, Buffer, BufferLen, ulFlags);
3867 if (!devInfo)
3868 return CR_NO_SUCH_DEVINST;
3870 WideCharToMultiByte(CP_ACP, 0, devInfo->instanceId, -1, Buffer, BufferLen, 0, 0);
3871 TRACE("Returning %s\n", debugstr_a(Buffer));
3872 return CR_SUCCESS;
3875 /***********************************************************************
3876 * CM_Get_Device_IDW (SETUPAPI.@)
3878 CONFIGRET WINAPI CM_Get_Device_IDW( DEVINST dnDevInst, LPWSTR Buffer,
3879 ULONG BufferLen, ULONG ulFlags)
3881 struct DeviceInfo *devInfo = GlobalLock((HANDLE)dnDevInst);
3883 TRACE("%x->%p, %p, %u %u\n", dnDevInst, devInfo, Buffer, BufferLen, ulFlags);
3885 if (!devInfo)
3887 WARN("dev instance %d not found!\n", dnDevInst);
3888 return CR_NO_SUCH_DEVINST;
3891 lstrcpynW(Buffer, devInfo->instanceId, BufferLen);
3892 TRACE("Returning %s\n", debugstr_w(Buffer));
3893 GlobalUnlock((HANDLE)dnDevInst);
3894 return CR_SUCCESS;
3899 /***********************************************************************
3900 * CM_Get_Device_ID_Size (SETUPAPI.@)
3902 CONFIGRET WINAPI CM_Get_Device_ID_Size( PULONG pulLen, DEVINST dnDevInst,
3903 ULONG ulFlags)
3905 struct DeviceInfo *ppdevInfo = GlobalLock((HANDLE)dnDevInst);
3907 TRACE("%x->%p, %p, %u\n", dnDevInst, ppdevInfo, pulLen, ulFlags);
3909 if (!ppdevInfo)
3911 WARN("dev instance %d not found!\n", dnDevInst);
3912 return CR_NO_SUCH_DEVINST;
3915 *pulLen = lstrlenW(ppdevInfo->instanceId);
3916 GlobalUnlock((HANDLE)dnDevInst);
3917 return CR_SUCCESS;