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
34 #include "wine/debug.h"
35 #include "wine/heap.h"
36 #include "wine/list.h"
43 #include "setupapi_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
48 /* Unicode constants */
49 static const WCHAR Chicago
[] = {'$','C','h','i','c','a','g','o','$',0};
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};
58 static const WCHAR NtPlatformExtension
[] = {'.','N','T','x','8','6',0};
59 #elif defined(__x86_64__)
60 static const WCHAR NtPlatformExtension
[] = {'.','N','T','a','m','d','6','4',0};
61 #elif defined(__arm__)
62 static const WCHAR NtPlatformExtension
[] = {'.','N','T','a','r','m',0};
63 #elif defined(__aarch64__)
64 static const WCHAR NtPlatformExtension
[] = {'.','N','T','a','r','m','6','4',0};
66 static const WCHAR Signature
[] = {'S','i','g','n','a','t','u','r','e',0};
67 static const WCHAR Version
[] = {'V','e','r','s','i','o','n',0};
68 static const WCHAR WinExtension
[] = {'.','W','i','n',0};
69 static const WCHAR WindowsNT
[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
71 /* Registry key and value names */
72 static const WCHAR ControlClass
[] = {'S','y','s','t','e','m','\\',
73 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
74 'C','o','n','t','r','o','l','\\',
75 'C','l','a','s','s',0};
77 static const WCHAR DeviceClasses
[] = {'S','y','s','t','e','m','\\',
78 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
79 'C','o','n','t','r','o','l','\\',
80 'D','e','v','i','c','e','C','l','a','s','s','e','s',0};
81 static const WCHAR Enum
[] = {'S','y','s','t','e','m','\\',
82 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
84 static const WCHAR DeviceDesc
[] = {'D','e','v','i','c','e','D','e','s','c',0};
85 static const WCHAR DeviceInstance
[] = {'D','e','v','i','c','e','I','n','s','t','a','n','c','e',0};
86 static const WCHAR DeviceParameters
[] = {'D','e','v','i','c','e',' ','P','a','r','a','m','e','t','e','r','s',0};
87 static const WCHAR HardwareId
[] = {'H','a','r','d','w','a','r','e','I','D',0};
88 static const WCHAR CompatibleIDs
[] = {'C','o','m','p','a','t','i','b','l','e','I','d','s',0};
89 static const WCHAR Service
[] = {'S','e','r','v','i','c','e',0};
90 static const WCHAR Driver
[] = {'D','r','i','v','e','r',0};
91 static const WCHAR ConfigFlags
[] = {'C','o','n','f','i','g','F','l','a','g','s',0};
92 static const WCHAR Mfg
[] = {'M','f','g',0};
93 static const WCHAR FriendlyName
[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
94 static const WCHAR LocationInformation
[] = {'L','o','c','a','t','i','o','n','I','n','f','o','r','m','a','t','i','o','n',0};
95 static const WCHAR Capabilities
[] = {'C','a','p','a','b','i','l','i','t','i','e','s',0};
96 static const WCHAR UINumber
[] = {'U','I','N','u','m','b','e','r',0};
97 static const WCHAR UpperFilters
[] = {'U','p','p','e','r','F','i','l','t','e','r','s',0};
98 static const WCHAR LowerFilters
[] = {'L','o','w','e','r','F','i','l','t','e','r','s',0};
99 static const WCHAR Phantom
[] = {'P','h','a','n','t','o','m',0};
100 static const WCHAR SymbolicLink
[] = {'S','y','m','b','o','l','i','c','L','i','n','k',0};
101 static const WCHAR Control
[] = {'C','o','n','t','r','o','l',0};
102 static const WCHAR Linked
[] = {'L','i','n','k','e','d',0};
103 static const WCHAR dotInterfaces
[] = {'.','I','n','t','e','r','f','a','c','e','s',0};
104 static const WCHAR AddInterface
[] = {'A','d','d','I','n','t','e','r','f','a','c','e',0};
105 static const WCHAR backslashW
[] = {'\\',0};
106 static const WCHAR emptyW
[] = {0};
108 #define SERVICE_CONTROL_REENUMERATE_ROOT_DEVICES 128
112 WCHAR inf_path
[MAX_PATH
];
113 WCHAR manufacturer
[LINE_LEN
];
114 WCHAR mfg_key
[LINE_LEN
];
115 WCHAR description
[LINE_LEN
];
116 WCHAR section
[LINE_LEN
];
119 /* is used to identify if a DeviceInfoSet pointer is
121 #define SETUP_DEVICE_INFO_SET_MAGIC 0xd00ff056
125 DWORD magic
; /* if is equal to SETUP_DEVICE_INFO_SET_MAGIC struct is okay */
133 struct DeviceInfoSet
*set
;
137 struct list interfaces
;
142 SP_DEVINSTALL_PARAMS_W params
;
143 struct driver
*drivers
;
144 unsigned int driver_count
;
145 struct driver
*selected_driver
;
152 struct device
*device
;
160 static struct DeviceInfoSet
*get_device_set(HDEVINFO devinfo
)
162 struct DeviceInfoSet
*set
= devinfo
;
164 if (!devinfo
|| devinfo
== INVALID_HANDLE_VALUE
|| set
->magic
!= SETUP_DEVICE_INFO_SET_MAGIC
)
166 SetLastError(ERROR_INVALID_HANDLE
);
173 static struct device
*get_device(HDEVINFO devinfo
, const SP_DEVINFO_DATA
*data
)
175 struct DeviceInfoSet
*set
;
176 struct device
*device
;
178 if (!(set
= get_device_set(devinfo
)))
181 if (!data
|| data
->cbSize
!= sizeof(*data
) || !data
->Reserved
)
183 SetLastError(ERROR_INVALID_PARAMETER
);
187 device
= (struct device
*)data
->Reserved
;
189 if (device
->set
!= set
)
191 SetLastError(ERROR_INVALID_PARAMETER
);
197 SetLastError(ERROR_NO_SUCH_DEVINST
);
204 static struct device_iface
*get_device_iface(HDEVINFO devinfo
, const SP_DEVICE_INTERFACE_DATA
*data
)
206 if (!get_device_set(devinfo
))
209 if (!data
|| data
->cbSize
!= sizeof(*data
) || !data
->Reserved
)
211 SetLastError(ERROR_INVALID_PARAMETER
);
215 return (struct device_iface
*)data
->Reserved
;
218 static inline void copy_device_data(SP_DEVINFO_DATA
*data
, const struct device
*device
)
220 data
->ClassGuid
= device
->class;
221 data
->DevInst
= device
->devnode
;
222 data
->Reserved
= (ULONG_PTR
)device
;
225 static inline void copy_device_iface_data(SP_DEVICE_INTERFACE_DATA
*data
,
226 const struct device_iface
*iface
)
228 data
->InterfaceClassGuid
= iface
->class;
229 data
->Flags
= iface
->flags
;
230 data
->Reserved
= (ULONG_PTR
)iface
;
233 static struct device
**devnode_table
;
234 static unsigned int devnode_table_size
;
236 static DEVINST
alloc_devnode(struct device
*device
)
240 for (i
= 0; i
< devnode_table_size
; ++i
)
242 if (!devnode_table
[i
])
246 if (i
== devnode_table_size
)
250 devnode_table_size
*= 2;
251 devnode_table
= heap_realloc_zero(devnode_table
,
252 devnode_table_size
* sizeof(*devnode_table
));
256 devnode_table_size
= 256;
257 devnode_table
= heap_alloc_zero(devnode_table_size
* sizeof(*devnode_table
));
261 devnode_table
[i
] = device
;
265 static void free_devnode(DEVINST devnode
)
267 devnode_table
[devnode
] = NULL
;
270 static struct device
*get_devnode_device(DEVINST devnode
)
272 if (devnode
< devnode_table_size
)
273 return devnode_table
[devnode
];
275 WARN("device node %u not found\n", devnode
);
279 static void SETUPDI_GuidToString(const GUID
*guid
, LPWSTR guidStr
)
281 static const WCHAR fmt
[] = {'{','%','0','8','X','-','%','0','4','X','-',
282 '%','0','4','X','-','%','0','2','X','%','0','2','X','-','%','0','2',
283 'X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%',
286 swprintf(guidStr
, 39, fmt
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
287 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
288 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
291 static WCHAR
*get_iface_key_path(struct device_iface
*iface
)
293 static const WCHAR slashW
[] = {'\\',0};
295 size_t len
= lstrlenW(DeviceClasses
) + 1 + 38 + 1 + lstrlenW(iface
->symlink
);
297 if (!(path
= heap_alloc((len
+ 1) * sizeof(WCHAR
))))
299 SetLastError(ERROR_OUTOFMEMORY
);
303 lstrcpyW(path
, DeviceClasses
);
304 lstrcatW(path
, slashW
);
305 SETUPDI_GuidToString(&iface
->class, path
+ lstrlenW(path
));
306 lstrcatW(path
, slashW
);
307 ptr
= path
+ lstrlenW(path
);
308 lstrcatW(path
, iface
->symlink
);
309 if (lstrlenW(iface
->symlink
) > 3)
310 ptr
[0] = ptr
[1] = ptr
[3] = '#';
312 ptr
= wcschr(ptr
, '\\');
318 static WCHAR
*get_refstr_key_path(struct device_iface
*iface
)
320 static const WCHAR hashW
[] = {'#',0};
321 static const WCHAR slashW
[] = {'\\',0};
323 size_t len
= lstrlenW(DeviceClasses
) + 1 + 38 + 1 + lstrlenW(iface
->symlink
) + 1 + 1;
326 len
+= lstrlenW(iface
->refstr
);
328 if (!(path
= heap_alloc((len
+ 1) * sizeof(WCHAR
))))
330 SetLastError(ERROR_OUTOFMEMORY
);
334 lstrcpyW(path
, DeviceClasses
);
335 lstrcatW(path
, slashW
);
336 SETUPDI_GuidToString(&iface
->class, path
+ lstrlenW(path
));
337 lstrcatW(path
, slashW
);
338 ptr
= path
+ lstrlenW(path
);
339 lstrcatW(path
, iface
->symlink
);
340 if (lstrlenW(iface
->symlink
) > 3)
341 ptr
[0] = ptr
[1] = ptr
[3] = '#';
343 ptr
= wcschr(ptr
, '\\');
346 lstrcatW(path
, slashW
);
347 lstrcatW(path
, hashW
);
350 lstrcatW(path
, iface
->refstr
);
355 static BOOL
is_valid_property_type(DEVPROPTYPE prop_type
)
357 DWORD type
= prop_type
& DEVPROP_MASK_TYPE
;
358 DWORD typemod
= prop_type
& DEVPROP_MASK_TYPEMOD
;
360 if (type
> MAX_DEVPROP_TYPE
)
362 if (typemod
> MAX_DEVPROP_TYPEMOD
)
365 if (typemod
== DEVPROP_TYPEMOD_ARRAY
366 && (type
== DEVPROP_TYPE_EMPTY
|| type
== DEVPROP_TYPE_NULL
|| type
== DEVPROP_TYPE_STRING
367 || type
== DEVPROP_TYPE_SECURITY_DESCRIPTOR_STRING
))
370 if (typemod
== DEVPROP_TYPEMOD_LIST
371 && !(type
== DEVPROP_TYPE_STRING
|| type
== DEVPROP_TYPE_SECURITY_DESCRIPTOR_STRING
))
377 static LPWSTR
SETUPDI_CreateSymbolicLinkPath(LPCWSTR instanceId
,
378 const GUID
*InterfaceClassGuid
, LPCWSTR ReferenceString
)
380 static const WCHAR fmt
[] = {'\\','\\','?','\\','%','s','#','%','s',0};
385 SETUPDI_GuidToString(InterfaceClassGuid
, guidStr
);
386 /* omit length of format specifiers, but include NULL terminator: */
387 len
= lstrlenW(fmt
) - 4 + 1;
388 len
+= lstrlenW(instanceId
) + lstrlenW(guidStr
);
389 if (ReferenceString
&& *ReferenceString
)
391 /* space for a hash between string and reference string: */
392 len
+= lstrlenW(ReferenceString
) + 1;
394 ret
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
397 int printed
= swprintf(ret
, len
, fmt
, instanceId
, guidStr
);
400 /* replace '\\' with '#' after the "\\\\?\\" beginning */
401 for (ptr
= wcschr(ret
+ 4, '\\'); ptr
; ptr
= wcschr(ptr
+ 1, '\\'))
403 if (ReferenceString
&& *ReferenceString
)
406 lstrcpyW(ret
+ printed
+ 1, ReferenceString
);
412 static BOOL
is_linked(HKEY key
)
414 DWORD linked
, type
, size
;
418 if (!RegOpenKeyW(key
, Control
, &control_key
))
420 size
= sizeof(DWORD
);
421 if (!RegQueryValueExW(control_key
, Linked
, NULL
, &type
, (BYTE
*)&linked
, &size
)
422 && type
== REG_DWORD
&& linked
)
425 RegCloseKey(control_key
);
431 static struct device_iface
*SETUPDI_CreateDeviceInterface(struct device
*device
,
432 const GUID
*class, const WCHAR
*refstr
)
434 struct device_iface
*iface
= NULL
;
435 WCHAR
*refstr2
= NULL
, *symlink
= NULL
, *path
= NULL
;
439 TRACE("%p %s %s\n", device
, debugstr_guid(class), debugstr_w(refstr
));
441 /* check if it already exists */
442 LIST_FOR_EACH_ENTRY(iface
, &device
->interfaces
, struct device_iface
, entry
)
444 if (IsEqualGUID(&iface
->class, class) && !lstrcmpiW(iface
->refstr
, refstr
))
448 iface
= heap_alloc(sizeof(*iface
));
449 symlink
= SETUPDI_CreateSymbolicLinkPath(device
->instanceId
, class, refstr
);
451 if (!iface
|| !symlink
)
453 SetLastError(ERROR_OUTOFMEMORY
);
457 if (refstr
&& !(refstr2
= strdupW(refstr
)))
459 SetLastError(ERROR_OUTOFMEMORY
);
462 iface
->refstr
= refstr2
;
463 iface
->symlink
= symlink
;
464 iface
->device
= device
;
465 iface
->class = *class;
468 if (!(path
= get_iface_key_path(iface
)))
470 SetLastError(ERROR_OUTOFMEMORY
);
474 if ((ret
= RegCreateKeyW(HKEY_LOCAL_MACHINE
, path
, &key
)))
479 RegSetValueExW(key
, DeviceInstance
, 0, REG_SZ
, (BYTE
*)device
->instanceId
,
480 lstrlenW(device
->instanceId
) * sizeof(WCHAR
));
483 iface
->class_key
= key
;
485 if (!(path
= get_refstr_key_path(iface
)))
487 SetLastError(ERROR_OUTOFMEMORY
);
491 if ((ret
= RegCreateKeyW(HKEY_LOCAL_MACHINE
, path
, &key
)))
496 RegSetValueExW(key
, SymbolicLink
, 0, REG_SZ
, (BYTE
*)iface
->symlink
,
497 lstrlenW(iface
->symlink
) * sizeof(WCHAR
));
500 iface
->flags
|= SPINT_ACTIVE
;
504 iface
->refstr_key
= key
;
506 list_add_tail(&device
->interfaces
, &iface
->entry
);
517 static BOOL
SETUPDI_SetInterfaceSymbolicLink(struct device_iface
*iface
,
518 const WCHAR
*symlink
)
520 heap_free(iface
->symlink
);
521 if ((iface
->symlink
= strdupW(symlink
)))
526 static HKEY
SETUPDI_CreateDevKey(struct device
*device
)
528 HKEY enumKey
, key
= INVALID_HANDLE_VALUE
;
531 l
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, Enum
, 0, NULL
, 0, KEY_ALL_ACCESS
,
532 NULL
, &enumKey
, NULL
);
535 RegCreateKeyExW(enumKey
, device
->instanceId
, 0, NULL
, 0,
536 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
537 RegCloseKey(enumKey
);
542 static LONG
open_driver_key(struct device
*device
, REGSAM access
, HKEY
*key
)
546 DWORD size
= sizeof(path
);
549 if ((l
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, ControlClass
, 0, NULL
, 0,
550 KEY_CREATE_SUB_KEY
, NULL
, &class_key
, NULL
)))
552 ERR("Failed to open driver class root key, error %u.\n", l
);
556 if (!(l
= RegGetValueW(device
->key
, NULL
, Driver
, RRF_RT_REG_SZ
, NULL
, path
, &size
)))
558 if (!(l
= RegOpenKeyExW(class_key
, path
, 0, access
, key
)))
560 RegCloseKey(class_key
);
563 ERR("Failed to open driver key, error %u.\n", l
);
566 RegCloseKey(class_key
);
570 static LONG
create_driver_key(struct device
*device
, HKEY
*key
)
572 static const WCHAR formatW
[] = {'%','0','4','u',0};
573 static const WCHAR slash
[] = { '\\',0 };
580 if (!open_driver_key(device
, KEY_READ
| KEY_WRITE
, key
))
581 return ERROR_SUCCESS
;
583 if ((l
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, ControlClass
, 0, NULL
, 0,
584 KEY_CREATE_SUB_KEY
, NULL
, &class_key
, NULL
)))
586 ERR("Failed to open driver class root key, error %u.\n", l
);
590 SETUPDI_GuidToString(&device
->class, path
);
591 lstrcatW(path
, slash
);
592 /* Allocate a new driver key, by finding the first integer value that's not
596 swprintf(path
+ 39, ARRAY_SIZE(path
) - 39, formatW
, i
++);
597 if ((l
= RegCreateKeyExW(class_key
, path
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, key
, &dispos
)))
599 else if (dispos
== REG_CREATED_NEW_KEY
)
601 RegSetValueExW(device
->key
, Driver
, 0, REG_SZ
, (BYTE
*)path
, lstrlenW(path
) * sizeof(WCHAR
));
602 RegCloseKey(class_key
);
603 return ERROR_SUCCESS
;
607 ERR("Failed to create driver key, error %u.\n", l
);
608 RegCloseKey(class_key
);
612 static LONG
delete_driver_key(struct device
*device
)
617 if (!(l
= open_driver_key(device
, KEY_READ
| KEY_WRITE
, &key
)))
619 l
= RegDeleteKeyW(key
, emptyW
);
626 struct PropertyMapEntry
633 static const struct PropertyMapEntry PropertyMap
[] = {
634 { REG_SZ
, "DeviceDesc", DeviceDesc
},
635 { REG_MULTI_SZ
, "HardwareId", HardwareId
},
636 { REG_MULTI_SZ
, "CompatibleIDs", CompatibleIDs
},
637 { 0, NULL
, NULL
}, /* SPDRP_UNUSED0 */
638 { REG_SZ
, "Service", Service
},
639 { 0, NULL
, NULL
}, /* SPDRP_UNUSED1 */
640 { 0, NULL
, NULL
}, /* SPDRP_UNUSED2 */
641 { REG_SZ
, "Class", Class
},
642 { REG_SZ
, "ClassGUID", ClassGUID
},
643 { REG_SZ
, "Driver", Driver
},
644 { REG_DWORD
, "ConfigFlags", ConfigFlags
},
645 { REG_SZ
, "Mfg", Mfg
},
646 { REG_SZ
, "FriendlyName", FriendlyName
},
647 { REG_SZ
, "LocationInformation", LocationInformation
},
648 { 0, NULL
, NULL
}, /* SPDRP_PHYSICAL_DEVICE_OBJECT_NAME */
649 { REG_DWORD
, "Capabilities", Capabilities
},
650 { REG_DWORD
, "UINumber", UINumber
},
651 { REG_MULTI_SZ
, "UpperFilters", UpperFilters
},
652 { REG_MULTI_SZ
, "LowerFilters", LowerFilters
},
655 static BOOL
SETUPDI_SetDeviceRegistryPropertyW(struct device
*device
,
656 DWORD prop
, const BYTE
*buffer
, DWORD size
)
658 if (prop
< ARRAY_SIZE(PropertyMap
) && PropertyMap
[prop
].nameW
)
660 LONG ret
= RegSetValueExW(device
->key
, PropertyMap
[prop
].nameW
, 0,
661 PropertyMap
[prop
].regType
, buffer
, size
);
670 static void remove_device_iface(struct device_iface
*iface
)
672 RegDeleteTreeW(iface
->refstr_key
, NULL
);
673 RegDeleteKeyW(iface
->refstr_key
, emptyW
);
674 RegCloseKey(iface
->refstr_key
);
675 iface
->refstr_key
= NULL
;
676 /* Also remove the class key if it's empty. */
677 RegDeleteKeyW(iface
->class_key
, emptyW
);
678 RegCloseKey(iface
->class_key
);
679 iface
->class_key
= NULL
;
680 iface
->flags
|= SPINT_REMOVED
;
683 static void delete_device_iface(struct device_iface
*iface
)
685 list_remove(&iface
->entry
);
686 RegCloseKey(iface
->refstr_key
);
687 RegCloseKey(iface
->class_key
);
688 heap_free(iface
->refstr
);
689 heap_free(iface
->symlink
);
693 /* remove all interfaces associated with the device, including those not
694 * enumerated in the set */
695 static void remove_all_device_ifaces(struct device
*device
)
701 if ((ret
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, DeviceClasses
, 0, KEY_READ
, &classes_key
)))
703 ERR("Failed to open classes key, error %u.\n", ret
);
709 WCHAR class_name
[40];
713 len
= ARRAY_SIZE(class_name
);
714 if ((ret
= RegEnumKeyExW(classes_key
, i
, class_name
, &len
, NULL
, NULL
, NULL
, NULL
)))
716 if (ret
!= ERROR_NO_MORE_ITEMS
) ERR("Failed to enumerate classes, error %u.\n", ret
);
720 if ((ret
= RegOpenKeyExW(classes_key
, class_name
, 0, KEY_READ
, &class_key
)))
722 ERR("Failed to open class %s, error %u.\n", debugstr_w(class_name
), ret
);
728 WCHAR iface_name
[MAX_DEVICE_ID_LEN
+ 39], device_name
[MAX_DEVICE_ID_LEN
];
731 len
= ARRAY_SIZE(iface_name
);
732 if ((ret
= RegEnumKeyExW(class_key
, j
, iface_name
, &len
, NULL
, NULL
, NULL
, NULL
)))
734 if (ret
!= ERROR_NO_MORE_ITEMS
) ERR("Failed to enumerate interfaces, error %u.\n", ret
);
738 if ((ret
= RegOpenKeyExW(class_key
, iface_name
, 0, KEY_ALL_ACCESS
, &iface_key
)))
740 ERR("Failed to open interface %s, error %u.\n", debugstr_w(iface_name
), ret
);
744 len
= sizeof(device_name
);
745 if ((ret
= RegQueryValueExW(iface_key
, L
"DeviceInstance", NULL
, NULL
, (BYTE
*)device_name
, &len
)))
747 ERR("Failed to query device instance, error %u.\n", ret
);
748 RegCloseKey(iface_key
);
752 if (!wcsicmp(device_name
, device
->instanceId
))
754 if ((ret
= RegDeleteTreeW(iface_key
, NULL
)))
755 ERR("Failed to delete interface %s subkeys, error %u.\n", debugstr_w(iface_name
), ret
);
756 if ((ret
= RegDeleteKeyW(iface_key
, L
"")))
757 ERR("Failed to delete interface %s, error %u.\n", debugstr_w(iface_name
), ret
);
760 RegCloseKey(iface_key
);
762 RegCloseKey(class_key
);
765 RegCloseKey(classes_key
);
768 static void remove_device(struct device
*device
)
770 WCHAR id
[MAX_DEVICE_ID_LEN
], *p
;
771 struct device_iface
*iface
;
774 delete_driver_key(device
);
776 LIST_FOR_EACH_ENTRY(iface
, &device
->interfaces
, struct device_iface
, entry
)
778 remove_device_iface(iface
);
781 RegDeleteTreeW(device
->key
, NULL
);
782 RegDeleteKeyW(device
->key
, emptyW
);
784 /* delete all empty parents of the key */
785 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE
, Enum
, 0, 0, &enum_key
))
787 lstrcpyW(id
, device
->instanceId
);
789 while ((p
= wcsrchr(id
, '\\')))
792 RegDeleteKeyW(enum_key
, id
);
795 RegCloseKey(enum_key
);
798 RegCloseKey(device
->key
);
800 device
->removed
= TRUE
;
803 static void delete_device(struct device
*device
)
805 struct device_iface
*iface
, *next
;
806 SP_DEVINFO_DATA device_data
;
808 device_data
.cbSize
= sizeof(device_data
);
809 copy_device_data(&device_data
, device
);
810 SetupDiCallClassInstaller(DIF_DESTROYPRIVATEDATA
, device
->set
, &device_data
);
814 remove_device(device
);
815 remove_all_device_ifaces(device
);
818 RegCloseKey(device
->key
);
819 heap_free(device
->instanceId
);
820 heap_free(device
->drivers
);
822 LIST_FOR_EACH_ENTRY_SAFE(iface
, next
, &device
->interfaces
,
823 struct device_iface
, entry
)
825 delete_device_iface(iface
);
827 free_devnode(device
->devnode
);
828 list_remove(&device
->entry
);
832 /* Create a new device, or return a device already in the set. */
833 static struct device
*create_device(struct DeviceInfoSet
*set
,
834 const GUID
*class, const WCHAR
*instanceid
, BOOL phantom
)
837 struct device
*device
;
838 WCHAR guidstr
[MAX_GUID_STRING_LEN
];
839 WCHAR class_name
[MAX_CLASS_NAME_LEN
];
842 TRACE("%p, %s, %s, %d\n", set
, debugstr_guid(class),
843 debugstr_w(instanceid
), phantom
);
845 LIST_FOR_EACH_ENTRY(device
, &set
->devices
, struct device
, entry
)
847 if (!wcsicmp(instanceid
, device
->instanceId
))
849 TRACE("Found device %p already in set.\n", device
);
854 if (!(device
= heap_alloc_zero(sizeof(*device
))))
856 SetLastError(ERROR_OUTOFMEMORY
);
860 if (!(device
->instanceId
= strdupW(instanceid
)))
862 SetLastError(ERROR_OUTOFMEMORY
);
867 wcsupr(device
->instanceId
);
869 device
->key
= SETUPDI_CreateDevKey(device
);
870 device
->phantom
= phantom
;
871 list_init(&device
->interfaces
);
872 device
->class = *class;
873 device
->devnode
= alloc_devnode(device
);
874 device
->removed
= FALSE
;
875 list_add_tail(&set
->devices
, &device
->entry
);
876 device
->params
.cbSize
= sizeof(SP_DEVINSTALL_PARAMS_W
);
879 RegSetValueExW(device
->key
, Phantom
, 0, REG_DWORD
, (const BYTE
*)&one
, sizeof(one
));
881 SETUPDI_GuidToString(class, guidstr
);
882 SETUPDI_SetDeviceRegistryPropertyW(device
, SPDRP_CLASSGUID
,
883 (const BYTE
*)guidstr
, sizeof(guidstr
));
885 if (SetupDiClassNameFromGuidW(class, class_name
, ARRAY_SIZE(class_name
), NULL
))
887 size
= (lstrlenW(class_name
) + 1) * sizeof(WCHAR
);
888 SETUPDI_SetDeviceRegistryPropertyW(device
, SPDRP_CLASS
, (const BYTE
*)class_name
, size
);
891 TRACE("Created new device %p.\n", device
);
895 /***********************************************************************
896 * SetupDiBuildClassInfoList (SETUPAPI.@)
898 * Returns a list of setup class GUIDs that identify the classes
899 * that are installed on a local machine.
902 * Flags [I] control exclusion of classes from the list.
903 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
904 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
905 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
911 BOOL WINAPI
SetupDiBuildClassInfoList(
913 LPGUID ClassGuidList
,
914 DWORD ClassGuidListSize
,
918 return SetupDiBuildClassInfoListExW(Flags
, ClassGuidList
,
919 ClassGuidListSize
, RequiredSize
,
923 /***********************************************************************
924 * SetupDiBuildClassInfoListExA (SETUPAPI.@)
926 * Returns a list of setup class GUIDs that identify the classes
927 * that are installed on a local or remote machine.
930 * Flags [I] control exclusion of classes from the list.
931 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
932 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
933 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
934 * MachineName [I] name of a remote machine.
935 * Reserved [I] must be NULL.
941 BOOL WINAPI
SetupDiBuildClassInfoListExA(
943 LPGUID ClassGuidList
,
944 DWORD ClassGuidListSize
,
949 LPWSTR MachineNameW
= NULL
;
956 MachineNameW
= MultiByteToUnicode(MachineName
, CP_ACP
);
957 if (MachineNameW
== NULL
) return FALSE
;
960 bResult
= SetupDiBuildClassInfoListExW(Flags
, ClassGuidList
,
961 ClassGuidListSize
, RequiredSize
,
962 MachineNameW
, Reserved
);
964 MyFree(MachineNameW
);
969 /***********************************************************************
970 * SetupDiBuildClassInfoListExW (SETUPAPI.@)
972 * Returns a list of setup class GUIDs that identify the classes
973 * that are installed on a local or remote machine.
976 * Flags [I] control exclusion of classes from the list.
977 * ClassGuidList [O] pointer to a GUID-typed array that receives a list of setup class GUIDs.
978 * ClassGuidListSize [I] The number of GUIDs in the array (ClassGuidList).
979 * RequiredSize [O] pointer, which receives the number of GUIDs that are returned.
980 * MachineName [I] name of a remote machine.
981 * Reserved [I] must be NULL.
987 BOOL WINAPI
SetupDiBuildClassInfoListExW(
989 LPGUID ClassGuidList
,
990 DWORD ClassGuidListSize
,
1001 DWORD dwGuidListIndex
= 0;
1005 if (RequiredSize
!= NULL
)
1008 hClassesKey
= SetupDiOpenClassRegKeyExW(NULL
,
1013 if (hClassesKey
== INVALID_HANDLE_VALUE
)
1018 for (dwIndex
= 0; ; dwIndex
++)
1021 lError
= RegEnumKeyExW(hClassesKey
,
1029 TRACE("RegEnumKeyExW() returns %d\n", lError
);
1030 if (lError
== ERROR_SUCCESS
|| lError
== ERROR_MORE_DATA
)
1032 TRACE("Key name: %p\n", szKeyName
);
1034 if (RegOpenKeyExW(hClassesKey
,
1040 RegCloseKey(hClassesKey
);
1044 if (!RegQueryValueExW(hClassKey
,
1051 TRACE("'NoUseClass' value found!\n");
1052 RegCloseKey(hClassKey
);
1056 if ((Flags
& DIBCI_NOINSTALLCLASS
) &&
1057 (!RegQueryValueExW(hClassKey
,
1064 TRACE("'NoInstallClass' value found!\n");
1065 RegCloseKey(hClassKey
);
1069 if ((Flags
& DIBCI_NODISPLAYCLASS
) &&
1070 (!RegQueryValueExW(hClassKey
,
1077 TRACE("'NoDisplayClass' value found!\n");
1078 RegCloseKey(hClassKey
);
1082 RegCloseKey(hClassKey
);
1084 TRACE("Guid: %p\n", szKeyName
);
1085 if (dwGuidListIndex
< ClassGuidListSize
)
1087 if (szKeyName
[0] == '{' && szKeyName
[37] == '}')
1091 TRACE("Guid: %p\n", &szKeyName
[1]);
1093 UuidFromStringW(&szKeyName
[1],
1094 &ClassGuidList
[dwGuidListIndex
]);
1100 if (lError
!= ERROR_SUCCESS
)
1104 RegCloseKey(hClassesKey
);
1106 if (RequiredSize
!= NULL
)
1107 *RequiredSize
= dwGuidListIndex
;
1109 if (ClassGuidListSize
< dwGuidListIndex
)
1111 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1118 /***********************************************************************
1119 * SetupDiClassGuidsFromNameA (SETUPAPI.@)
1121 BOOL WINAPI
SetupDiClassGuidsFromNameA(
1123 LPGUID ClassGuidList
,
1124 DWORD ClassGuidListSize
,
1125 PDWORD RequiredSize
)
1127 return SetupDiClassGuidsFromNameExA(ClassName
, ClassGuidList
,
1128 ClassGuidListSize
, RequiredSize
,
1132 /***********************************************************************
1133 * SetupDiClassGuidsFromNameW (SETUPAPI.@)
1135 BOOL WINAPI
SetupDiClassGuidsFromNameW(
1137 LPGUID ClassGuidList
,
1138 DWORD ClassGuidListSize
,
1139 PDWORD RequiredSize
)
1141 return SetupDiClassGuidsFromNameExW(ClassName
, ClassGuidList
,
1142 ClassGuidListSize
, RequiredSize
,
1146 /***********************************************************************
1147 * SetupDiClassGuidsFromNameExA (SETUPAPI.@)
1149 BOOL WINAPI
SetupDiClassGuidsFromNameExA(
1151 LPGUID ClassGuidList
,
1152 DWORD ClassGuidListSize
,
1153 PDWORD RequiredSize
,
1157 LPWSTR ClassNameW
= NULL
;
1158 LPWSTR MachineNameW
= NULL
;
1161 ClassNameW
= MultiByteToUnicode(ClassName
, CP_ACP
);
1162 if (ClassNameW
== NULL
)
1167 MachineNameW
= MultiByteToUnicode(MachineName
, CP_ACP
);
1168 if (MachineNameW
== NULL
)
1175 bResult
= SetupDiClassGuidsFromNameExW(ClassNameW
, ClassGuidList
,
1176 ClassGuidListSize
, RequiredSize
,
1177 MachineNameW
, Reserved
);
1179 MyFree(MachineNameW
);
1185 /***********************************************************************
1186 * SetupDiClassGuidsFromNameExW (SETUPAPI.@)
1188 BOOL WINAPI
SetupDiClassGuidsFromNameExW(
1190 LPGUID ClassGuidList
,
1191 DWORD ClassGuidListSize
,
1192 PDWORD RequiredSize
,
1193 LPCWSTR MachineName
,
1196 WCHAR szKeyName
[40];
1197 WCHAR szClassName
[256];
1203 DWORD dwGuidListIndex
= 0;
1205 if (RequiredSize
!= NULL
)
1208 hClassesKey
= SetupDiOpenClassRegKeyExW(NULL
,
1213 if (hClassesKey
== INVALID_HANDLE_VALUE
)
1218 for (dwIndex
= 0; ; dwIndex
++)
1220 dwLength
= ARRAY_SIZE(szKeyName
);
1221 lError
= RegEnumKeyExW(hClassesKey
,
1229 TRACE("RegEnumKeyExW() returns %d\n", lError
);
1230 if (lError
== ERROR_SUCCESS
|| lError
== ERROR_MORE_DATA
)
1232 TRACE("Key name: %p\n", szKeyName
);
1234 if (RegOpenKeyExW(hClassesKey
,
1240 RegCloseKey(hClassesKey
);
1244 dwLength
= sizeof(szClassName
);
1245 if (!RegQueryValueExW(hClassKey
,
1249 (LPBYTE
)szClassName
,
1252 TRACE("Class name: %p\n", szClassName
);
1254 if (wcsicmp(szClassName
, ClassName
) == 0)
1256 TRACE("Found matching class name\n");
1258 TRACE("Guid: %p\n", szKeyName
);
1259 if (dwGuidListIndex
< ClassGuidListSize
)
1261 if (szKeyName
[0] == '{' && szKeyName
[37] == '}')
1265 TRACE("Guid: %p\n", &szKeyName
[1]);
1267 UuidFromStringW(&szKeyName
[1],
1268 &ClassGuidList
[dwGuidListIndex
]);
1275 RegCloseKey(hClassKey
);
1278 if (lError
!= ERROR_SUCCESS
)
1282 RegCloseKey(hClassesKey
);
1284 if (RequiredSize
!= NULL
)
1285 *RequiredSize
= dwGuidListIndex
;
1287 if (ClassGuidListSize
< dwGuidListIndex
)
1289 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1296 /***********************************************************************
1297 * SetupDiClassNameFromGuidA (SETUPAPI.@)
1299 BOOL WINAPI
SetupDiClassNameFromGuidA(
1300 const GUID
* ClassGuid
,
1302 DWORD ClassNameSize
,
1303 PDWORD RequiredSize
)
1305 return SetupDiClassNameFromGuidExA(ClassGuid
, ClassName
,
1306 ClassNameSize
, RequiredSize
,
1310 /***********************************************************************
1311 * SetupDiClassNameFromGuidW (SETUPAPI.@)
1313 BOOL WINAPI
SetupDiClassNameFromGuidW(
1314 const GUID
* ClassGuid
,
1316 DWORD ClassNameSize
,
1317 PDWORD RequiredSize
)
1319 return SetupDiClassNameFromGuidExW(ClassGuid
, ClassName
,
1320 ClassNameSize
, RequiredSize
,
1324 /***********************************************************************
1325 * SetupDiClassNameFromGuidExA (SETUPAPI.@)
1327 BOOL WINAPI
SetupDiClassNameFromGuidExA(
1328 const GUID
* ClassGuid
,
1330 DWORD ClassNameSize
,
1331 PDWORD RequiredSize
,
1335 WCHAR ClassNameW
[MAX_CLASS_NAME_LEN
];
1336 LPWSTR MachineNameW
= NULL
;
1340 MachineNameW
= MultiByteToUnicode(MachineName
, CP_ACP
);
1341 ret
= SetupDiClassNameFromGuidExW(ClassGuid
, ClassNameW
, MAX_CLASS_NAME_LEN
,
1342 NULL
, MachineNameW
, Reserved
);
1345 int len
= WideCharToMultiByte(CP_ACP
, 0, ClassNameW
, -1, ClassName
,
1346 ClassNameSize
, NULL
, NULL
);
1348 if (!ClassNameSize
&& RequiredSize
)
1349 *RequiredSize
= len
;
1351 MyFree(MachineNameW
);
1355 /***********************************************************************
1356 * SetupDiClassNameFromGuidExW (SETUPAPI.@)
1358 BOOL WINAPI
SetupDiClassNameFromGuidExW(
1359 const GUID
* ClassGuid
,
1361 DWORD ClassNameSize
,
1362 PDWORD RequiredSize
,
1369 hKey
= SetupDiOpenClassRegKeyExW(ClassGuid
,
1374 if (hKey
== INVALID_HANDLE_VALUE
)
1379 if (RequiredSize
!= NULL
)
1382 if (RegQueryValueExW(hKey
,
1393 *RequiredSize
= dwLength
/ sizeof(WCHAR
);
1396 dwLength
= ClassNameSize
* sizeof(WCHAR
);
1397 if (RegQueryValueExW(hKey
,
1413 /***********************************************************************
1414 * SetupDiCreateDeviceInfoList (SETUPAPI.@)
1417 SetupDiCreateDeviceInfoList(const GUID
*ClassGuid
,
1420 return SetupDiCreateDeviceInfoListExW(ClassGuid
, hwndParent
, NULL
, NULL
);
1423 /***********************************************************************
1424 * SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
1427 SetupDiCreateDeviceInfoListExA(const GUID
*ClassGuid
,
1432 LPWSTR MachineNameW
= NULL
;
1439 MachineNameW
= MultiByteToUnicode(MachineName
, CP_ACP
);
1440 if (MachineNameW
== NULL
)
1441 return INVALID_HANDLE_VALUE
;
1444 hDevInfo
= SetupDiCreateDeviceInfoListExW(ClassGuid
, hwndParent
,
1445 MachineNameW
, Reserved
);
1447 MyFree(MachineNameW
);
1452 /***********************************************************************
1453 * SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
1455 * Create an empty DeviceInfoSet list.
1458 * ClassGuid [I] if not NULL only devices with GUID ClassGuid are associated
1460 * hwndParent [I] hwnd needed for interface related actions.
1461 * MachineName [I] name of machine to create empty DeviceInfoSet list, if NULL
1462 * local registry will be used.
1463 * Reserved [I] must be NULL
1466 * Success: empty list.
1467 * Failure: INVALID_HANDLE_VALUE.
1470 SetupDiCreateDeviceInfoListExW(const GUID
*ClassGuid
,
1475 struct DeviceInfoSet
*list
= NULL
;
1476 DWORD size
= sizeof(struct DeviceInfoSet
);
1478 TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid
), hwndParent
,
1479 debugstr_w(MachineName
), Reserved
);
1481 if (MachineName
&& *MachineName
)
1483 FIXME("remote support is not implemented\n");
1484 SetLastError(ERROR_INVALID_MACHINENAME
);
1485 return INVALID_HANDLE_VALUE
;
1488 if (Reserved
!= NULL
)
1490 SetLastError(ERROR_INVALID_PARAMETER
);
1491 return INVALID_HANDLE_VALUE
;
1494 list
= HeapAlloc(GetProcessHeap(), 0, size
);
1497 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1498 return INVALID_HANDLE_VALUE
;
1501 list
->magic
= SETUP_DEVICE_INFO_SET_MAGIC
;
1502 list
->hwndParent
= hwndParent
;
1503 memcpy(&list
->ClassGuid
,
1504 ClassGuid
? ClassGuid
: &GUID_NULL
,
1505 sizeof(list
->ClassGuid
));
1506 list_init(&list
->devices
);
1511 /***********************************************************************
1512 * SetupDiCreateDevRegKeyA (SETUPAPI.@)
1514 HKEY WINAPI
SetupDiCreateDevRegKeyA(
1515 HDEVINFO DeviceInfoSet
,
1516 PSP_DEVINFO_DATA DeviceInfoData
,
1521 PCSTR InfSectionName
)
1523 PWSTR InfSectionNameW
= NULL
;
1526 TRACE("%p %p %d %d %d %p %s\n", DeviceInfoSet
, DeviceInfoData
, Scope
,
1527 HwProfile
, KeyType
, InfHandle
, debugstr_a(InfSectionName
));
1531 if (!InfSectionName
)
1533 SetLastError(ERROR_INVALID_PARAMETER
);
1534 return INVALID_HANDLE_VALUE
;
1538 InfSectionNameW
= MultiByteToUnicode(InfSectionName
, CP_ACP
);
1539 if (InfSectionNameW
== NULL
) return INVALID_HANDLE_VALUE
;
1542 key
= SetupDiCreateDevRegKeyW(DeviceInfoSet
, DeviceInfoData
, Scope
,
1543 HwProfile
, KeyType
, InfHandle
, InfSectionNameW
);
1544 MyFree(InfSectionNameW
);
1548 /***********************************************************************
1549 * SetupDiCreateDevRegKeyW (SETUPAPI.@)
1551 HKEY WINAPI
SetupDiCreateDevRegKeyW(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
, DWORD Scope
,
1552 DWORD HwProfile
, DWORD KeyType
, HINF InfHandle
, const WCHAR
*InfSectionName
)
1554 struct device
*device
;
1555 HKEY key
= INVALID_HANDLE_VALUE
;
1558 TRACE("devinfo %p, device_data %p, scope %d, profile %d, type %d, inf_handle %p, inf_section %s.\n",
1559 devinfo
, device_data
, Scope
, HwProfile
, KeyType
, InfHandle
, debugstr_w(InfSectionName
));
1561 if (!(device
= get_device(devinfo
, device_data
)))
1562 return INVALID_HANDLE_VALUE
;
1564 if (Scope
!= DICS_FLAG_GLOBAL
&& Scope
!= DICS_FLAG_CONFIGSPECIFIC
)
1566 SetLastError(ERROR_INVALID_FLAGS
);
1567 return INVALID_HANDLE_VALUE
;
1569 if (KeyType
!= DIREG_DEV
&& KeyType
!= DIREG_DRV
)
1571 SetLastError(ERROR_INVALID_FLAGS
);
1572 return INVALID_HANDLE_VALUE
;
1574 if (device
->phantom
)
1576 SetLastError(ERROR_DEVINFO_NOT_REGISTERED
);
1577 return INVALID_HANDLE_VALUE
;
1579 if (Scope
!= DICS_FLAG_GLOBAL
)
1580 FIXME("unimplemented for scope %d\n", Scope
);
1584 l
= RegCreateKeyExW(device
->key
, DeviceParameters
, 0, NULL
, 0,
1585 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
1588 l
= create_driver_key(device
, &key
);
1591 FIXME("Unhandled type %#x.\n", KeyType
);
1592 l
= ERROR_CALL_NOT_IMPLEMENTED
;
1595 SetupInstallFromInfSectionW(NULL
, InfHandle
, InfSectionName
, SPINST_ALL
,
1596 NULL
, NULL
, SP_COPY_NEWER_ONLY
, NULL
, NULL
, devinfo
, device_data
);
1598 return l
? INVALID_HANDLE_VALUE
: key
;
1601 /***********************************************************************
1602 * SetupDiCreateDeviceInfoA (SETUPAPI.@)
1604 BOOL WINAPI
SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet
, const char *name
,
1605 const GUID
*ClassGuid
, PCSTR DeviceDescription
, HWND hwndParent
, DWORD CreationFlags
,
1606 PSP_DEVINFO_DATA DeviceInfoData
)
1608 WCHAR nameW
[MAX_DEVICE_ID_LEN
];
1610 LPWSTR DeviceDescriptionW
= NULL
;
1612 if (!name
|| strlen(name
) >= MAX_DEVICE_ID_LEN
)
1614 SetLastError(ERROR_INVALID_DEVINST_NAME
);
1618 MultiByteToWideChar(CP_ACP
, 0, name
, -1, nameW
, ARRAY_SIZE(nameW
));
1620 if (DeviceDescription
)
1622 DeviceDescriptionW
= MultiByteToUnicode(DeviceDescription
, CP_ACP
);
1623 if (DeviceDescriptionW
== NULL
)
1627 ret
= SetupDiCreateDeviceInfoW(DeviceInfoSet
, nameW
, ClassGuid
, DeviceDescriptionW
,
1628 hwndParent
, CreationFlags
, DeviceInfoData
);
1630 MyFree(DeviceDescriptionW
);
1635 /***********************************************************************
1636 * SetupDiCreateDeviceInfoW (SETUPAPI.@)
1638 BOOL WINAPI
SetupDiCreateDeviceInfoW(HDEVINFO devinfo
, const WCHAR
*name
, const GUID
*class,
1639 const WCHAR
*description
, HWND parent
, DWORD flags
, SP_DEVINFO_DATA
*device_data
)
1641 WCHAR id
[MAX_DEVICE_ID_LEN
];
1642 struct DeviceInfoSet
*set
;
1645 struct device
*device
;
1648 TRACE("devinfo %p, name %s, class %s, description %s, hwnd %p, flags %#x, device_data %p.\n",
1649 devinfo
, debugstr_w(name
), debugstr_guid(class), debugstr_w(description
),
1650 parent
, flags
, device_data
);
1652 if (!name
|| lstrlenW(name
) >= MAX_DEVICE_ID_LEN
)
1654 SetLastError(ERROR_INVALID_DEVINST_NAME
);
1658 if (!(set
= get_device_set(devinfo
)))
1663 SetLastError(ERROR_INVALID_PARAMETER
);
1667 if (!IsEqualGUID(&set
->ClassGuid
, &GUID_NULL
) && !IsEqualGUID(class, &set
->ClassGuid
))
1669 SetLastError(ERROR_CLASS_MISMATCH
);
1672 if ((flags
& DICD_GENERATE_ID
))
1674 static const WCHAR formatW
[] = {'R','O','O','T','\\','%','s','\\','%','0','4','u',0};
1675 unsigned int instance_id
;
1677 if (wcschr(name
, '\\'))
1679 SetLastError(ERROR_INVALID_DEVINST_NAME
);
1683 for (instance_id
= 0; ; ++instance_id
)
1685 if (swprintf(id
, ARRAY_SIZE(id
), formatW
, name
, instance_id
) == -1)
1687 SetLastError(ERROR_INVALID_DEVINST_NAME
);
1691 RegCreateKeyExW(HKEY_LOCAL_MACHINE
, Enum
, 0, NULL
, 0, KEY_READ
, NULL
, &enum_hkey
, NULL
);
1692 if (!(l
= RegOpenKeyExW(enum_hkey
, id
, 0, KEY_READ
, &instance_hkey
)))
1693 RegCloseKey(instance_hkey
);
1694 if (l
== ERROR_FILE_NOT_FOUND
)
1696 RegCloseKey(enum_hkey
);
1701 /* Check if instance is already in registry */
1702 RegCreateKeyExW(HKEY_LOCAL_MACHINE
, Enum
, 0, NULL
, 0, KEY_READ
, NULL
, &enum_hkey
, NULL
);
1703 if (!RegOpenKeyExW(enum_hkey
, name
, 0, KEY_READ
, &instance_hkey
))
1705 RegCloseKey(instance_hkey
);
1706 RegCloseKey(enum_hkey
);
1707 SetLastError(ERROR_DEVINST_ALREADY_EXISTS
);
1710 RegCloseKey(enum_hkey
);
1712 /* Check if instance is already in set */
1714 LIST_FOR_EACH_ENTRY(device
, &set
->devices
, struct device
, entry
)
1716 if (!lstrcmpiW(name
, device
->instanceId
))
1718 SetLastError(ERROR_DEVINST_ALREADY_EXISTS
);
1724 if (!(device
= create_device(set
, class, id
, TRUE
)))
1729 SETUPDI_SetDeviceRegistryPropertyW(device
, SPDRP_DEVICEDESC
,
1730 (const BYTE
*)description
, lstrlenW(description
) * sizeof(WCHAR
));
1735 if (device_data
->cbSize
!= sizeof(SP_DEVINFO_DATA
))
1737 SetLastError(ERROR_INVALID_USER_BUFFER
);
1741 copy_device_data(device_data
, device
);
1747 /***********************************************************************
1748 * SetupDiRegisterDeviceInfo (SETUPAPI.@)
1750 BOOL WINAPI
SetupDiRegisterDeviceInfo(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
, DWORD flags
,
1751 PSP_DETSIG_CMPPROC compare_proc
, void *context
, SP_DEVINFO_DATA
*duplicate_data
)
1753 struct device
*device
;
1755 TRACE("devinfo %p, data %p, flags %#x, compare_proc %p, context %p, duplicate_data %p.\n",
1756 devinfo
, device_data
, flags
, compare_proc
, context
, duplicate_data
);
1758 if (!(device
= get_device(devinfo
, device_data
)))
1761 if (device
->phantom
)
1763 device
->phantom
= FALSE
;
1764 RegDeleteValueW(device
->key
, Phantom
);
1769 /***********************************************************************
1770 * SetupDiRemoveDevice (SETUPAPI.@)
1772 BOOL WINAPI
SetupDiRemoveDevice(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
1774 SC_HANDLE manager
= NULL
, service
= NULL
;
1775 struct device
*device
;
1776 WCHAR
*service_name
= NULL
;
1779 TRACE("devinfo %p, device_data %p.\n", devinfo
, device_data
);
1781 if (!(device
= get_device(devinfo
, device_data
)))
1784 if (!(manager
= OpenSCManagerW(NULL
, NULL
, SC_MANAGER_CONNECT
)))
1787 if (!RegGetValueW(device
->key
, NULL
, L
"Service", RRF_RT_REG_SZ
, NULL
, NULL
, &size
))
1789 service_name
= malloc(size
);
1790 if (!RegGetValueW(device
->key
, NULL
, L
"Service", RRF_RT_REG_SZ
, NULL
, service_name
, &size
))
1791 service
= OpenServiceW(manager
, service_name
, SERVICE_USER_DEFINED_CONTROL
);
1794 remove_device(device
);
1798 SERVICE_STATUS status
;
1799 if (!ControlService(service
, SERVICE_CONTROL_REENUMERATE_ROOT_DEVICES
, &status
))
1800 ERR("Failed to control service %s, error %u.\n", debugstr_w(service_name
), GetLastError());
1801 CloseServiceHandle(service
);
1803 CloseServiceHandle(manager
);
1807 remove_all_device_ifaces(device
);
1812 /***********************************************************************
1813 * SetupDiDeleteDeviceInfo (SETUPAPI.@)
1815 BOOL WINAPI
SetupDiDeleteDeviceInfo(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
1817 struct device
*device
;
1819 TRACE("devinfo %p, device_data %p.\n", devinfo
, device_data
);
1821 if (!(device
= get_device(devinfo
, device_data
)))
1824 delete_device(device
);
1829 /***********************************************************************
1830 * SetupDiRemoveDeviceInterface (SETUPAPI.@)
1832 BOOL WINAPI
SetupDiRemoveDeviceInterface(HDEVINFO devinfo
, SP_DEVICE_INTERFACE_DATA
*iface_data
)
1834 struct device_iface
*iface
;
1836 TRACE("devinfo %p, iface_data %p.\n", devinfo
, iface_data
);
1838 if (!(iface
= get_device_iface(devinfo
, iface_data
)))
1841 remove_device_iface(iface
);
1846 /***********************************************************************
1847 * SetupDiDeleteDeviceInterfaceData (SETUPAPI.@)
1849 BOOL WINAPI
SetupDiDeleteDeviceInterfaceData(HDEVINFO devinfo
, SP_DEVICE_INTERFACE_DATA
*iface_data
)
1851 struct device_iface
*iface
;
1853 TRACE("devinfo %p, iface_data %p.\n", devinfo
, iface_data
);
1855 if (!(iface
= get_device_iface(devinfo
, iface_data
)))
1858 delete_device_iface(iface
);
1863 /***********************************************************************
1864 * SetupDiEnumDeviceInfo (SETUPAPI.@)
1866 BOOL WINAPI DECLSPEC_HOTPATCH
SetupDiEnumDeviceInfo(HDEVINFO devinfo
, DWORD index
, SP_DEVINFO_DATA
*device_data
)
1868 struct DeviceInfoSet
*set
;
1869 struct device
*device
;
1872 TRACE("devinfo %p, index %d, device_data %p\n", devinfo
, index
, device_data
);
1874 if (!(set
= get_device_set(devinfo
)))
1879 SetLastError(ERROR_INVALID_PARAMETER
);
1883 if (device_data
->cbSize
!= sizeof(SP_DEVINFO_DATA
))
1885 SetLastError(ERROR_INVALID_USER_BUFFER
);
1889 LIST_FOR_EACH_ENTRY(device
, &set
->devices
, struct device
, entry
)
1893 copy_device_data(device_data
, device
);
1898 SetLastError(ERROR_NO_MORE_ITEMS
);
1902 /***********************************************************************
1903 * SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
1905 BOOL WINAPI
SetupDiGetDeviceInstanceIdA(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
1906 char *id
, DWORD size
, DWORD
*needed
)
1908 WCHAR idW
[MAX_DEVICE_ID_LEN
];
1910 TRACE("devinfo %p, device_data %p, id %p, size %d, needed %p.\n",
1911 devinfo
, device_data
, id
, size
, needed
);
1913 if (!SetupDiGetDeviceInstanceIdW(devinfo
, device_data
, idW
, ARRAY_SIZE(idW
), NULL
))
1917 *needed
= WideCharToMultiByte(CP_ACP
, 0, idW
, -1, NULL
, 0, NULL
, NULL
);
1919 if (size
&& WideCharToMultiByte(CP_ACP
, 0, idW
, -1, id
, size
, NULL
, NULL
))
1922 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1926 /***********************************************************************
1927 * SetupDiGetDeviceInstanceIdW (SETUPAPI.@)
1929 BOOL WINAPI
SetupDiGetDeviceInstanceIdW(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
1930 WCHAR
*DeviceInstanceId
, DWORD DeviceInstanceIdSize
, DWORD
*RequiredSize
)
1932 struct device
*device
;
1934 TRACE("devinfo %p, device_data %p, DeviceInstanceId %p, DeviceInstanceIdSize %d, RequiredSize %p.\n",
1935 devinfo
, device_data
, DeviceInstanceId
, DeviceInstanceIdSize
, RequiredSize
);
1937 if (!(device
= get_device(devinfo
, device_data
)))
1940 TRACE("instance ID: %s\n", debugstr_w(device
->instanceId
));
1941 if (DeviceInstanceIdSize
< lstrlenW(device
->instanceId
) + 1)
1943 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1945 *RequiredSize
= lstrlenW(device
->instanceId
) + 1;
1948 lstrcpyW(DeviceInstanceId
, device
->instanceId
);
1950 *RequiredSize
= lstrlenW(device
->instanceId
) + 1;
1954 /***********************************************************************
1955 * SetupDiGetActualSectionToInstallExA (SETUPAPI.@)
1957 BOOL WINAPI
SetupDiGetActualSectionToInstallExA(HINF hinf
, const char *section
, SP_ALTPLATFORM_INFO
*altplatform
,
1958 char *section_ext
, DWORD size
, DWORD
*needed
, char **extptr
, void *reserved
)
1960 WCHAR sectionW
[LINE_LEN
], section_extW
[LINE_LEN
], *extptrW
;
1963 MultiByteToWideChar(CP_ACP
, 0, section
, -1, sectionW
, ARRAY_SIZE(sectionW
));
1965 ret
= SetupDiGetActualSectionToInstallExW(hinf
, sectionW
, altplatform
, section_extW
,
1966 ARRAY_SIZE(section_extW
), NULL
, &extptrW
, reserved
);
1970 *needed
= WideCharToMultiByte(CP_ACP
, 0, section_extW
, -1, NULL
, 0, NULL
, NULL
);
1973 ret
= !!WideCharToMultiByte(CP_ACP
, 0, section_extW
, -1, section_ext
, size
, NULL
, NULL
);
1978 *extptr
= section_ext
+ WideCharToMultiByte(CP_ACP
, 0, section_extW
,
1979 extptrW
- section_extW
, NULL
, 0, NULL
, NULL
);
1988 /***********************************************************************
1989 * SetupDiGetActualSectionToInstallA (SETUPAPI.@)
1991 BOOL WINAPI
SetupDiGetActualSectionToInstallA(HINF hinf
, const char *section
, char *section_ext
,
1992 DWORD size
, DWORD
*needed
, char **extptr
)
1994 return SetupDiGetActualSectionToInstallExA(hinf
, section
, NULL
, section_ext
, size
,
1995 needed
, extptr
, NULL
);
1998 /***********************************************************************
1999 * SetupDiGetActualSectionToInstallExW (SETUPAPI.@)
2001 BOOL WINAPI
SetupDiGetActualSectionToInstallExW(HINF hinf
, const WCHAR
*section
, SP_ALTPLATFORM_INFO
*altplatform
,
2002 WCHAR
*section_ext
, DWORD size
, DWORD
*needed
, WCHAR
**extptr
, void *reserved
)
2004 WCHAR buffer
[MAX_PATH
];
2007 LONG line_count
= -1;
2009 TRACE("hinf %p, section %s, altplatform %p, ext %p, size %d, needed %p, extptr %p, reserved %p.\n",
2010 hinf
, debugstr_w(section
), altplatform
, section_ext
, size
, needed
, extptr
, reserved
);
2013 FIXME("SP_ALTPLATFORM_INFO unsupported\n");
2015 lstrcpyW(buffer
, section
);
2016 len
= lstrlenW(buffer
);
2018 if (OsVersionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
2020 /* Test section name with '.NTx86' extension */
2021 lstrcpyW(&buffer
[len
], NtPlatformExtension
);
2022 line_count
= SetupGetLineCountW(hinf
, buffer
);
2024 if (line_count
== -1)
2026 /* Test section name with '.NT' extension */
2027 lstrcpyW(&buffer
[len
], NtExtension
);
2028 line_count
= SetupGetLineCountW(hinf
, buffer
);
2033 /* Test section name with '.Win' extension */
2034 lstrcpyW(&buffer
[len
], WinExtension
);
2035 line_count
= SetupGetLineCountW(hinf
, buffer
);
2038 if (line_count
== -1)
2041 full_len
= lstrlenW(buffer
);
2043 if (section_ext
!= NULL
&& size
!= 0)
2045 if (size
< (full_len
+ 1))
2047 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2051 lstrcpyW(section_ext
, buffer
);
2054 *extptr
= (len
== full_len
) ? NULL
: §ion_ext
[len
];
2060 *needed
= full_len
+ 1;
2066 /***********************************************************************
2067 * SetupDiGetActualSectionToInstallW (SETUPAPI.@)
2069 BOOL WINAPI
SetupDiGetActualSectionToInstallW(HINF hinf
, const WCHAR
*section
, WCHAR
*section_ext
,
2070 DWORD size
, DWORD
*needed
, WCHAR
**extptr
)
2072 return SetupDiGetActualSectionToInstallExW(hinf
, section
, NULL
, section_ext
, size
,
2073 needed
, extptr
, NULL
);
2076 /***********************************************************************
2077 * SetupDiGetClassDescriptionA (SETUPAPI.@)
2079 BOOL WINAPI
SetupDiGetClassDescriptionA(
2080 const GUID
* ClassGuid
,
2081 PSTR ClassDescription
,
2082 DWORD ClassDescriptionSize
,
2083 PDWORD RequiredSize
)
2085 return SetupDiGetClassDescriptionExA(ClassGuid
, ClassDescription
,
2086 ClassDescriptionSize
,
2087 RequiredSize
, NULL
, NULL
);
2090 /***********************************************************************
2091 * SetupDiGetClassDescriptionW (SETUPAPI.@)
2093 BOOL WINAPI
SetupDiGetClassDescriptionW(
2094 const GUID
* ClassGuid
,
2095 PWSTR ClassDescription
,
2096 DWORD ClassDescriptionSize
,
2097 PDWORD RequiredSize
)
2099 return SetupDiGetClassDescriptionExW(ClassGuid
, ClassDescription
,
2100 ClassDescriptionSize
,
2101 RequiredSize
, NULL
, NULL
);
2104 /***********************************************************************
2105 * SetupDiGetClassDescriptionExA (SETUPAPI.@)
2107 BOOL WINAPI
SetupDiGetClassDescriptionExA(
2108 const GUID
* ClassGuid
,
2109 PSTR ClassDescription
,
2110 DWORD ClassDescriptionSize
,
2111 PDWORD RequiredSize
,
2119 hKey
= SetupDiOpenClassRegKeyExA(ClassGuid
,
2124 if (hKey
== INVALID_HANDLE_VALUE
)
2126 WARN("SetupDiOpenClassRegKeyExA() failed (Error %u)\n", GetLastError());
2130 dwLength
= ClassDescriptionSize
;
2131 ret
= !RegQueryValueExA( hKey
, NULL
, NULL
, NULL
,
2132 (LPBYTE
)ClassDescription
, &dwLength
);
2133 if (RequiredSize
) *RequiredSize
= dwLength
;
2138 /***********************************************************************
2139 * SetupDiGetClassDescriptionExW (SETUPAPI.@)
2141 BOOL WINAPI
SetupDiGetClassDescriptionExW(
2142 const GUID
* ClassGuid
,
2143 PWSTR ClassDescription
,
2144 DWORD ClassDescriptionSize
,
2145 PDWORD RequiredSize
,
2153 hKey
= SetupDiOpenClassRegKeyExW(ClassGuid
,
2158 if (hKey
== INVALID_HANDLE_VALUE
)
2160 WARN("SetupDiOpenClassRegKeyExW() failed (Error %u)\n", GetLastError());
2164 dwLength
= ClassDescriptionSize
* sizeof(WCHAR
);
2165 ret
= !RegQueryValueExW( hKey
, NULL
, NULL
, NULL
,
2166 (LPBYTE
)ClassDescription
, &dwLength
);
2167 if (RequiredSize
) *RequiredSize
= dwLength
/ sizeof(WCHAR
);
2172 /***********************************************************************
2173 * SetupDiGetClassDevsA (SETUPAPI.@)
2175 HDEVINFO WINAPI
SetupDiGetClassDevsA(const GUID
*class, LPCSTR enumstr
, HWND parent
, DWORD flags
)
2178 LPWSTR enumstrW
= NULL
;
2182 int len
= MultiByteToWideChar(CP_ACP
, 0, enumstr
, -1, NULL
, 0);
2183 enumstrW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2186 ret
= INVALID_HANDLE_VALUE
;
2189 MultiByteToWideChar(CP_ACP
, 0, enumstr
, -1, enumstrW
, len
);
2191 ret
= SetupDiGetClassDevsExW(class, enumstrW
, parent
, flags
, NULL
, NULL
,
2193 HeapFree(GetProcessHeap(), 0, enumstrW
);
2199 /***********************************************************************
2200 * SetupDiGetClassDevsExA (SETUPAPI.@)
2202 HDEVINFO WINAPI
SetupDiGetClassDevsExA(
2212 LPWSTR enumstrW
= NULL
, machineW
= NULL
;
2216 int len
= MultiByteToWideChar(CP_ACP
, 0, enumstr
, -1, NULL
, 0);
2217 enumstrW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2220 ret
= INVALID_HANDLE_VALUE
;
2223 MultiByteToWideChar(CP_ACP
, 0, enumstr
, -1, enumstrW
, len
);
2227 int len
= MultiByteToWideChar(CP_ACP
, 0, machine
, -1, NULL
, 0);
2228 machineW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2231 HeapFree(GetProcessHeap(), 0, enumstrW
);
2232 ret
= INVALID_HANDLE_VALUE
;
2235 MultiByteToWideChar(CP_ACP
, 0, machine
, -1, machineW
, len
);
2237 ret
= SetupDiGetClassDevsExW(class, enumstrW
, parent
, flags
, deviceset
,
2238 machineW
, reserved
);
2239 HeapFree(GetProcessHeap(), 0, enumstrW
);
2240 HeapFree(GetProcessHeap(), 0, machineW
);
2246 static void SETUPDI_AddDeviceInterfaces(struct device
*device
, HKEY key
,
2247 const GUID
*guid
, DWORD flags
)
2250 WCHAR subKeyName
[MAX_PATH
];
2251 LONG l
= ERROR_SUCCESS
;
2253 for (i
= 0; !l
; i
++)
2255 len
= ARRAY_SIZE(subKeyName
);
2256 l
= RegEnumKeyExW(key
, i
, subKeyName
, &len
, NULL
, NULL
, NULL
, NULL
);
2260 struct device_iface
*iface
;
2262 if (*subKeyName
== '#')
2264 /* The subkey name is the reference string, with a '#' prepended */
2265 l
= RegOpenKeyExW(key
, subKeyName
, 0, KEY_READ
, &subKey
);
2268 WCHAR symbolicLink
[MAX_PATH
];
2271 if (!(flags
& DIGCF_PRESENT
) || is_linked(subKey
))
2273 iface
= SETUPDI_CreateDeviceInterface(device
, guid
, subKeyName
+ 1);
2275 len
= sizeof(symbolicLink
);
2276 l
= RegQueryValueExW(subKey
, SymbolicLink
, NULL
, &dataType
,
2277 (BYTE
*)symbolicLink
, &len
);
2278 if (!l
&& dataType
== REG_SZ
)
2279 SETUPDI_SetInterfaceSymbolicLink(iface
, symbolicLink
);
2280 RegCloseKey(subKey
);
2284 /* Allow enumeration to continue */
2288 /* FIXME: find and add all the device's interfaces to the device */
2291 static void SETUPDI_EnumerateMatchingInterfaces(HDEVINFO DeviceInfoSet
,
2292 HKEY key
, const GUID
*guid
, const WCHAR
*enumstr
, DWORD flags
)
2294 struct DeviceInfoSet
*set
= DeviceInfoSet
;
2296 WCHAR subKeyName
[MAX_PATH
];
2298 HKEY enumKey
= INVALID_HANDLE_VALUE
;
2300 TRACE("%s\n", debugstr_w(enumstr
));
2302 l
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, Enum
, 0, NULL
, 0, KEY_READ
, NULL
,
2304 for (i
= 0; !l
; i
++)
2306 len
= ARRAY_SIZE(subKeyName
);
2307 l
= RegEnumKeyExW(key
, i
, subKeyName
, &len
, NULL
, NULL
, NULL
, NULL
);
2312 l
= RegOpenKeyExW(key
, subKeyName
, 0, KEY_READ
, &subKey
);
2315 WCHAR deviceInst
[MAX_PATH
* 3];
2318 len
= sizeof(deviceInst
);
2319 l
= RegQueryValueExW(subKey
, DeviceInstance
, NULL
, &dataType
,
2320 (BYTE
*)deviceInst
, &len
);
2321 if (!l
&& dataType
== REG_SZ
)
2323 TRACE("found instance ID %s\n", debugstr_w(deviceInst
));
2324 if (!enumstr
|| !lstrcmpiW(enumstr
, deviceInst
))
2328 l
= RegOpenKeyExW(enumKey
, deviceInst
, 0, KEY_READ
,
2332 WCHAR deviceClassStr
[40];
2334 len
= sizeof(deviceClassStr
);
2335 l
= RegQueryValueExW(deviceKey
, ClassGUID
, NULL
,
2336 &dataType
, (BYTE
*)deviceClassStr
, &len
);
2337 if (!l
&& dataType
== REG_SZ
&&
2338 deviceClassStr
[0] == '{' &&
2339 deviceClassStr
[37] == '}')
2342 struct device
*device
;
2344 deviceClassStr
[37] = 0;
2345 UuidFromStringW(&deviceClassStr
[1],
2347 if ((device
= create_device(set
, &deviceClass
, deviceInst
, FALSE
)))
2348 SETUPDI_AddDeviceInterfaces(device
, subKey
, guid
, flags
);
2350 RegCloseKey(deviceKey
);
2354 RegCloseKey(subKey
);
2356 /* Allow enumeration to continue */
2360 if (enumKey
!= INVALID_HANDLE_VALUE
)
2361 RegCloseKey(enumKey
);
2364 static void SETUPDI_EnumerateInterfaces(HDEVINFO DeviceInfoSet
,
2365 const GUID
*guid
, LPCWSTR enumstr
, DWORD flags
)
2367 HKEY interfacesKey
= SetupDiOpenClassRegKeyExW(guid
, KEY_READ
,
2368 DIOCR_INTERFACE
, NULL
, NULL
);
2370 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet
, debugstr_guid(guid
),
2371 debugstr_w(enumstr
), flags
);
2373 if (interfacesKey
!= INVALID_HANDLE_VALUE
)
2375 if (flags
& DIGCF_ALLCLASSES
)
2378 WCHAR interfaceGuidStr
[40];
2379 LONG l
= ERROR_SUCCESS
;
2381 for (i
= 0; !l
; i
++)
2383 len
= ARRAY_SIZE(interfaceGuidStr
);
2384 l
= RegEnumKeyExW(interfacesKey
, i
, interfaceGuidStr
, &len
,
2385 NULL
, NULL
, NULL
, NULL
);
2388 if (interfaceGuidStr
[0] == '{' &&
2389 interfaceGuidStr
[37] == '}')
2394 interfaceGuidStr
[37] = 0;
2395 UuidFromStringW(&interfaceGuidStr
[1], &interfaceGuid
);
2396 interfaceGuidStr
[37] = '}';
2397 interfaceGuidStr
[38] = 0;
2398 l
= RegOpenKeyExW(interfacesKey
, interfaceGuidStr
, 0,
2399 KEY_READ
, &interfaceKey
);
2402 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet
,
2403 interfaceKey
, &interfaceGuid
, enumstr
, flags
);
2404 RegCloseKey(interfaceKey
);
2412 /* In this case, SetupDiOpenClassRegKeyExW opened the specific
2413 * interface's key, so just pass that long
2415 SETUPDI_EnumerateMatchingInterfaces(DeviceInfoSet
,
2416 interfacesKey
, guid
, enumstr
, flags
);
2418 RegCloseKey(interfacesKey
);
2422 static void SETUPDI_EnumerateMatchingDeviceInstances(struct DeviceInfoSet
*set
,
2423 LPCWSTR enumerator
, LPCWSTR deviceName
, HKEY deviceKey
,
2424 const GUID
*class, DWORD flags
)
2426 WCHAR id
[MAX_DEVICE_ID_LEN
];
2428 WCHAR deviceInstance
[MAX_PATH
];
2429 LONG l
= ERROR_SUCCESS
;
2431 TRACE("%s %s\n", debugstr_w(enumerator
), debugstr_w(deviceName
));
2433 for (i
= 0; !l
; i
++)
2435 len
= ARRAY_SIZE(deviceInstance
);
2436 l
= RegEnumKeyExW(deviceKey
, i
, deviceInstance
, &len
, NULL
, NULL
, NULL
,
2442 l
= RegOpenKeyExW(deviceKey
, deviceInstance
, 0, KEY_READ
, &subKey
);
2445 WCHAR classGuid
[40];
2448 len
= sizeof(classGuid
);
2449 l
= RegQueryValueExW(subKey
, ClassGUID
, NULL
, &dataType
,
2450 (BYTE
*)classGuid
, &len
);
2451 if (!l
&& dataType
== REG_SZ
)
2453 if (classGuid
[0] == '{' && classGuid
[37] == '}')
2458 UuidFromStringW(&classGuid
[1], &deviceClass
);
2459 if ((flags
& DIGCF_ALLCLASSES
) ||
2460 IsEqualGUID(class, &deviceClass
))
2462 static const WCHAR fmt
[] =
2463 {'%','s','\\','%','s','\\','%','s',0};
2465 if (swprintf(id
, ARRAY_SIZE(id
), fmt
, enumerator
,
2466 deviceName
, deviceInstance
) != -1)
2468 create_device(set
, &deviceClass
, id
, FALSE
);
2473 RegCloseKey(subKey
);
2475 /* Allow enumeration to continue */
2481 static void SETUPDI_EnumerateMatchingDevices(HDEVINFO DeviceInfoSet
,
2482 LPCWSTR parent
, HKEY key
, const GUID
*class, DWORD flags
)
2484 struct DeviceInfoSet
*set
= DeviceInfoSet
;
2486 WCHAR subKeyName
[MAX_PATH
];
2487 LONG l
= ERROR_SUCCESS
;
2489 TRACE("%s\n", debugstr_w(parent
));
2491 for (i
= 0; !l
; i
++)
2493 len
= ARRAY_SIZE(subKeyName
);
2494 l
= RegEnumKeyExW(key
, i
, subKeyName
, &len
, NULL
, NULL
, NULL
, NULL
);
2499 l
= RegOpenKeyExW(key
, subKeyName
, 0, KEY_READ
, &subKey
);
2502 TRACE("%s\n", debugstr_w(subKeyName
));
2503 SETUPDI_EnumerateMatchingDeviceInstances(set
, parent
,
2504 subKeyName
, subKey
, class, flags
);
2505 RegCloseKey(subKey
);
2507 /* Allow enumeration to continue */
2513 static void SETUPDI_EnumerateDevices(HDEVINFO DeviceInfoSet
, const GUID
*class,
2514 LPCWSTR enumstr
, DWORD flags
)
2519 TRACE("%p, %s, %s, %08x\n", DeviceInfoSet
, debugstr_guid(class),
2520 debugstr_w(enumstr
), flags
);
2522 l
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, Enum
, 0, NULL
, 0, KEY_READ
, NULL
,
2524 if (enumKey
!= INVALID_HANDLE_VALUE
)
2530 l
= RegOpenKeyExW(enumKey
, enumstr
, 0, KEY_READ
,
2534 WCHAR
*bus
, *device
;
2536 if (!wcschr(enumstr
, '\\'))
2538 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet
, enumstr
, enumStrKey
, class, flags
);
2540 else if ((bus
= strdupW(enumstr
)))
2542 device
= wcschr(bus
, '\\');
2545 SETUPDI_EnumerateMatchingDeviceInstances(DeviceInfoSet
, bus
, device
, enumStrKey
, class, flags
);
2546 HeapFree(GetProcessHeap(), 0, bus
);
2549 RegCloseKey(enumStrKey
);
2555 WCHAR subKeyName
[MAX_PATH
];
2558 for (i
= 0; !l
; i
++)
2560 len
= ARRAY_SIZE(subKeyName
);
2561 l
= RegEnumKeyExW(enumKey
, i
, subKeyName
, &len
, NULL
,
2567 l
= RegOpenKeyExW(enumKey
, subKeyName
, 0, KEY_READ
,
2571 SETUPDI_EnumerateMatchingDevices(DeviceInfoSet
,
2572 subKeyName
, subKey
, class, flags
);
2573 RegCloseKey(subKey
);
2575 /* Allow enumeration to continue */
2580 RegCloseKey(enumKey
);
2584 /***********************************************************************
2585 * SetupDiGetClassDevsW (SETUPAPI.@)
2587 HDEVINFO WINAPI
SetupDiGetClassDevsW(const GUID
*class, LPCWSTR enumstr
, HWND parent
, DWORD flags
)
2589 return SetupDiGetClassDevsExW(class, enumstr
, parent
, flags
, NULL
, NULL
,
2593 /***********************************************************************
2594 * SetupDiGetClassDevsExW (SETUPAPI.@)
2596 HDEVINFO WINAPI
SetupDiGetClassDevsExW(const GUID
*class, PCWSTR enumstr
, HWND parent
, DWORD flags
,
2597 HDEVINFO deviceset
, PCWSTR machine
, void *reserved
)
2599 static const DWORD unsupportedFlags
= DIGCF_DEFAULT
| DIGCF_PROFILE
;
2602 TRACE("%s %s %p 0x%08x %p %s %p\n", debugstr_guid(class),
2603 debugstr_w(enumstr
), parent
, flags
, deviceset
, debugstr_w(machine
),
2606 if (!(flags
& DIGCF_ALLCLASSES
) && !class)
2608 SetLastError(ERROR_INVALID_PARAMETER
);
2609 return INVALID_HANDLE_VALUE
;
2611 if (flags
& DIGCF_ALLCLASSES
)
2614 if (flags
& unsupportedFlags
)
2615 WARN("unsupported flags %08x\n", flags
& unsupportedFlags
);
2619 set
= SetupDiCreateDeviceInfoListExW((flags
& DIGCF_DEVICEINTERFACE
) ? NULL
: class, parent
, machine
, reserved
);
2620 if (set
!= INVALID_HANDLE_VALUE
)
2622 if (machine
&& *machine
)
2623 FIXME("%s: unimplemented for remote machines\n",
2624 debugstr_w(machine
));
2625 else if (flags
& DIGCF_DEVICEINTERFACE
)
2626 SETUPDI_EnumerateInterfaces(set
, class, enumstr
, flags
);
2628 SETUPDI_EnumerateDevices(set
, class, enumstr
, flags
);
2633 /***********************************************************************
2634 * SetupDiGetDeviceInfoListDetailA (SETUPAPI.@)
2636 BOOL WINAPI
SetupDiGetDeviceInfoListDetailA(HDEVINFO devinfo
, SP_DEVINFO_LIST_DETAIL_DATA_A
*DevInfoData
)
2638 struct DeviceInfoSet
*set
;
2640 TRACE("devinfo %p, detail_data %p.\n", devinfo
, DevInfoData
);
2642 if (!(set
= get_device_set(devinfo
)))
2646 DevInfoData
->cbSize
!= sizeof(SP_DEVINFO_LIST_DETAIL_DATA_A
))
2648 SetLastError(ERROR_INVALID_PARAMETER
);
2651 DevInfoData
->ClassGuid
= set
->ClassGuid
;
2652 DevInfoData
->RemoteMachineHandle
= NULL
;
2653 DevInfoData
->RemoteMachineName
[0] = '\0';
2657 /***********************************************************************
2658 * SetupDiGetDeviceInfoListDetailW (SETUPAPI.@)
2660 BOOL WINAPI
SetupDiGetDeviceInfoListDetailW(HDEVINFO devinfo
, SP_DEVINFO_LIST_DETAIL_DATA_W
*DevInfoData
)
2662 struct DeviceInfoSet
*set
;
2664 TRACE("devinfo %p, detail_data %p.\n", devinfo
, DevInfoData
);
2666 if (!(set
= get_device_set(devinfo
)))
2670 DevInfoData
->cbSize
!= sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W
))
2672 SetLastError(ERROR_INVALID_PARAMETER
);
2675 DevInfoData
->ClassGuid
= set
->ClassGuid
;
2676 DevInfoData
->RemoteMachineHandle
= NULL
;
2677 DevInfoData
->RemoteMachineName
[0] = '\0';
2681 /***********************************************************************
2682 * SetupDiCreateDeviceInterfaceA (SETUPAPI.@)
2684 BOOL WINAPI
SetupDiCreateDeviceInterfaceA(
2685 HDEVINFO DeviceInfoSet
,
2686 PSP_DEVINFO_DATA DeviceInfoData
,
2687 const GUID
*InterfaceClassGuid
,
2688 PCSTR ReferenceString
,
2689 DWORD CreationFlags
,
2690 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData
)
2693 LPWSTR ReferenceStringW
= NULL
;
2695 TRACE("%p %p %s %s %08x %p\n", DeviceInfoSet
, DeviceInfoData
,
2696 debugstr_guid(InterfaceClassGuid
), debugstr_a(ReferenceString
),
2697 CreationFlags
, DeviceInterfaceData
);
2699 if (ReferenceString
)
2701 ReferenceStringW
= MultiByteToUnicode(ReferenceString
, CP_ACP
);
2702 if (ReferenceStringW
== NULL
) return FALSE
;
2705 ret
= SetupDiCreateDeviceInterfaceW(DeviceInfoSet
, DeviceInfoData
,
2706 InterfaceClassGuid
, ReferenceStringW
, CreationFlags
,
2707 DeviceInterfaceData
);
2709 MyFree(ReferenceStringW
);
2714 /***********************************************************************
2715 * SetupDiCreateDeviceInterfaceW (SETUPAPI.@)
2717 BOOL WINAPI
SetupDiCreateDeviceInterfaceW(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
2718 const GUID
*class, const WCHAR
*refstr
, DWORD flags
, SP_DEVICE_INTERFACE_DATA
*iface_data
)
2720 struct device
*device
;
2721 struct device_iface
*iface
;
2723 TRACE("devinfo %p, device_data %p, class %s, refstr %s, flags %#x, iface_data %p.\n",
2724 devinfo
, device_data
, debugstr_guid(class), debugstr_w(refstr
), flags
, iface_data
);
2726 if (!(device
= get_device(devinfo
, device_data
)))
2731 SetLastError(ERROR_INVALID_USER_BUFFER
);
2735 if (!(iface
= SETUPDI_CreateDeviceInterface(device
, class, refstr
)))
2740 if (iface_data
->cbSize
!= sizeof(SP_DEVICE_INTERFACE_DATA
))
2742 SetLastError(ERROR_INVALID_USER_BUFFER
);
2746 copy_device_iface_data(iface_data
, iface
);
2751 /***********************************************************************
2752 * SetupDiCreateDeviceInterfaceRegKeyA (SETUPAPI.@)
2754 HKEY WINAPI
SetupDiCreateDeviceInterfaceRegKeyA(
2755 HDEVINFO DeviceInfoSet
,
2756 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData
,
2760 PCSTR InfSectionName
)
2763 PWSTR InfSectionNameW
= NULL
;
2765 TRACE("%p %p %d %08x %p %p\n", DeviceInfoSet
, DeviceInterfaceData
, Reserved
,
2766 samDesired
, InfHandle
, InfSectionName
);
2769 if (!InfSectionName
)
2771 SetLastError(ERROR_INVALID_PARAMETER
);
2772 return INVALID_HANDLE_VALUE
;
2774 InfSectionNameW
= MultiByteToUnicode(InfSectionName
, CP_ACP
);
2775 if (!InfSectionNameW
)
2776 return INVALID_HANDLE_VALUE
;
2778 key
= SetupDiCreateDeviceInterfaceRegKeyW(DeviceInfoSet
,
2779 DeviceInterfaceData
, Reserved
, samDesired
, InfHandle
,
2781 MyFree(InfSectionNameW
);
2785 static LONG
create_iface_key(const struct device_iface
*iface
, REGSAM access
, HKEY
*key
)
2787 return RegCreateKeyExW(iface
->refstr_key
, DeviceParameters
, 0, NULL
, 0, access
, NULL
, key
, NULL
);
2790 /***********************************************************************
2791 * SetupDiCreateDeviceInterfaceRegKeyW (SETUPAPI.@)
2793 HKEY WINAPI
SetupDiCreateDeviceInterfaceRegKeyW(HDEVINFO devinfo
,
2794 SP_DEVICE_INTERFACE_DATA
*iface_data
, DWORD reserved
, REGSAM access
,
2795 HINF hinf
, const WCHAR
*section
)
2797 struct device_iface
*iface
;
2801 TRACE("devinfo %p, iface_data %p, reserved %d, access %#x, hinf %p, section %s.\n",
2802 devinfo
, iface_data
, reserved
, access
, hinf
, debugstr_w(section
));
2804 if (!(iface
= get_device_iface(devinfo
, iface_data
)))
2805 return INVALID_HANDLE_VALUE
;
2807 if (hinf
&& !section
)
2809 SetLastError(ERROR_INVALID_PARAMETER
);
2810 return INVALID_HANDLE_VALUE
;
2813 ret
= create_iface_key(iface
, access
, ¶ms_key
);
2817 return INVALID_HANDLE_VALUE
;
2823 /***********************************************************************
2824 * SetupDiDeleteDeviceInterfaceRegKey (SETUPAPI.@)
2826 BOOL WINAPI
SetupDiDeleteDeviceInterfaceRegKey(HDEVINFO devinfo
,
2827 SP_DEVICE_INTERFACE_DATA
*iface_data
, DWORD reserved
)
2829 struct device_iface
*iface
;
2832 TRACE("devinfo %p, iface_data %p, reserved %d.\n", devinfo
, iface_data
, reserved
);
2834 if (!(iface
= get_device_iface(devinfo
, iface_data
)))
2837 ret
= RegDeleteKeyW(iface
->refstr_key
, DeviceParameters
);
2847 /***********************************************************************
2848 * SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2851 * DeviceInfoSet [I] Set of devices from which to enumerate
2853 * DeviceInfoData [I] (Optional) If specified, a specific device
2854 * instance from which to enumerate interfaces.
2855 * If it isn't specified, all interfaces for all
2856 * devices in the set are enumerated.
2857 * InterfaceClassGuid [I] The interface class to enumerate.
2858 * MemberIndex [I] An index of the interface instance to enumerate.
2859 * A caller should start with MemberIndex set to 0,
2860 * and continue until the function fails with
2861 * ERROR_NO_MORE_ITEMS.
2862 * DeviceInterfaceData [I/O] Returns an enumerated interface. Its cbSize
2863 * member must be set to
2864 * sizeof(SP_DEVICE_INTERFACE_DATA).
2867 * Success: non-zero value.
2868 * Failure: FALSE. Call GetLastError() for more info.
2870 BOOL WINAPI
SetupDiEnumDeviceInterfaces(HDEVINFO devinfo
,
2871 SP_DEVINFO_DATA
*device_data
, const GUID
*class, DWORD index
,
2872 SP_DEVICE_INTERFACE_DATA
*iface_data
)
2874 struct DeviceInfoSet
*set
;
2875 struct device
*device
;
2876 struct device_iface
*iface
;
2879 TRACE("devinfo %p, device_data %p, class %s, index %u, iface_data %p.\n",
2880 devinfo
, device_data
, debugstr_guid(class), index
, iface_data
);
2882 if (!iface_data
|| iface_data
->cbSize
!= sizeof(SP_DEVICE_INTERFACE_DATA
))
2884 SetLastError(ERROR_INVALID_PARAMETER
);
2888 /* In case application fails to check return value, clear output */
2889 memset(iface_data
, 0, sizeof(*iface_data
));
2890 iface_data
->cbSize
= sizeof(SP_DEVICE_INTERFACE_DATA
);
2894 if (!(device
= get_device(devinfo
, device_data
)))
2897 LIST_FOR_EACH_ENTRY(iface
, &device
->interfaces
, struct device_iface
, entry
)
2899 if (IsEqualGUID(&iface
->class, class))
2903 copy_device_iface_data(iface_data
, iface
);
2912 if (!(set
= get_device_set(devinfo
)))
2915 LIST_FOR_EACH_ENTRY(device
, &set
->devices
, struct device
, entry
)
2917 LIST_FOR_EACH_ENTRY(iface
, &device
->interfaces
, struct device_iface
, entry
)
2919 if (IsEqualGUID(&iface
->class, class))
2923 copy_device_iface_data(iface_data
, iface
);
2932 SetLastError(ERROR_NO_MORE_ITEMS
);
2936 /***********************************************************************
2937 * SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2939 * Destroy a DeviceInfoList and free all used memory of the list.
2942 * devinfo [I] DeviceInfoList pointer to list to destroy
2945 * Success: non zero value.
2946 * Failure: zero value.
2948 BOOL WINAPI
SetupDiDestroyDeviceInfoList(HDEVINFO devinfo
)
2950 struct DeviceInfoSet
*set
;
2951 struct device
*device
, *device2
;
2953 TRACE("devinfo %p.\n", devinfo
);
2955 if (!(set
= get_device_set(devinfo
)))
2958 LIST_FOR_EACH_ENTRY_SAFE(device
, device2
, &set
->devices
, struct device
, entry
)
2960 delete_device(device
);
2964 SetLastError(ERROR_SUCCESS
);
2968 /***********************************************************************
2969 * SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2971 BOOL WINAPI
SetupDiGetDeviceInterfaceDetailA(HDEVINFO devinfo
, SP_DEVICE_INTERFACE_DATA
*iface_data
,
2972 SP_DEVICE_INTERFACE_DETAIL_DATA_A
*DeviceInterfaceDetailData
,
2973 DWORD DeviceInterfaceDetailDataSize
, DWORD
*RequiredSize
, SP_DEVINFO_DATA
*device_data
)
2975 struct device_iface
*iface
;
2976 DWORD bytesNeeded
= FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A
, DevicePath
[1]);
2979 TRACE("devinfo %p, iface_data %p, detail_data %p, size %d, needed %p, device_data %p.\n",
2980 devinfo
, iface_data
, DeviceInterfaceDetailData
, DeviceInterfaceDetailDataSize
,
2981 RequiredSize
, device_data
);
2983 if (!(iface
= get_device_iface(devinfo
, iface_data
)))
2986 if (DeviceInterfaceDetailData
&&
2987 DeviceInterfaceDetailData
->cbSize
!= sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A
))
2989 SetLastError(ERROR_INVALID_USER_BUFFER
);
2992 if (!DeviceInterfaceDetailData
&& DeviceInterfaceDetailDataSize
)
2994 SetLastError(ERROR_INVALID_USER_BUFFER
);
2999 bytesNeeded
+= WideCharToMultiByte(CP_ACP
, 0, iface
->symlink
, -1,
3000 NULL
, 0, NULL
, NULL
);
3001 if (DeviceInterfaceDetailDataSize
>= bytesNeeded
)
3004 WideCharToMultiByte(CP_ACP
, 0, iface
->symlink
, -1,
3005 DeviceInterfaceDetailData
->DevicePath
,
3006 DeviceInterfaceDetailDataSize
-
3007 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_A
, DevicePath
),
3010 DeviceInterfaceDetailData
->DevicePath
[0] = '\0';
3017 *RequiredSize
= bytesNeeded
;
3018 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
3021 if (device_data
&& device_data
->cbSize
== sizeof(SP_DEVINFO_DATA
))
3022 copy_device_data(device_data
, iface
->device
);
3027 /***********************************************************************
3028 * SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
3030 BOOL WINAPI
SetupDiGetDeviceInterfaceDetailW(HDEVINFO devinfo
, SP_DEVICE_INTERFACE_DATA
*iface_data
,
3031 SP_DEVICE_INTERFACE_DETAIL_DATA_W
*DeviceInterfaceDetailData
,
3032 DWORD DeviceInterfaceDetailDataSize
, DWORD
*RequiredSize
, SP_DEVINFO_DATA
*device_data
)
3034 struct device_iface
*iface
;
3035 DWORD bytesNeeded
= offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W
, DevicePath
)
3036 + sizeof(WCHAR
); /* include NULL terminator */
3039 TRACE("devinfo %p, iface_data %p, detail_data %p, size %d, needed %p, device_data %p.\n",
3040 devinfo
, iface_data
, DeviceInterfaceDetailData
, DeviceInterfaceDetailDataSize
,
3041 RequiredSize
, device_data
);
3043 if (!(iface
= get_device_iface(devinfo
, iface_data
)))
3046 if (DeviceInterfaceDetailData
&& (DeviceInterfaceDetailData
->cbSize
<
3047 offsetof(SP_DEVICE_INTERFACE_DETAIL_DATA_W
, DevicePath
) + sizeof(WCHAR
) ||
3048 DeviceInterfaceDetailData
->cbSize
> sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W
)))
3050 SetLastError(ERROR_INVALID_USER_BUFFER
);
3053 if (!DeviceInterfaceDetailData
&& DeviceInterfaceDetailDataSize
)
3055 SetLastError(ERROR_INVALID_USER_BUFFER
);
3060 bytesNeeded
+= sizeof(WCHAR
) * lstrlenW(iface
->symlink
);
3061 if (DeviceInterfaceDetailDataSize
>= bytesNeeded
)
3064 lstrcpyW(DeviceInterfaceDetailData
->DevicePath
, iface
->symlink
);
3066 DeviceInterfaceDetailData
->DevicePath
[0] = '\0';
3073 *RequiredSize
= bytesNeeded
;
3074 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
3077 if (device_data
&& device_data
->cbSize
== sizeof(SP_DEVINFO_DATA
))
3078 copy_device_data(device_data
, iface
->device
);
3083 /***********************************************************************
3084 * SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
3086 BOOL WINAPI
SetupDiGetDeviceRegistryPropertyA(HDEVINFO devinfo
,
3087 SP_DEVINFO_DATA
*device_data
, DWORD Property
, DWORD
*PropertyRegDataType
,
3088 BYTE
*PropertyBuffer
, DWORD PropertyBufferSize
, DWORD
*RequiredSize
)
3091 struct device
*device
;
3093 TRACE("devinfo %p, device_data %p, property %d, type %p, buffer %p, size %d, required %p\n",
3094 devinfo
, device_data
, Property
, PropertyRegDataType
, PropertyBuffer
, PropertyBufferSize
, RequiredSize
);
3096 if (!(device
= get_device(devinfo
, device_data
)))
3099 if (PropertyBufferSize
&& PropertyBuffer
== NULL
)
3101 SetLastError(ERROR_INVALID_DATA
);
3105 if (Property
< ARRAY_SIZE(PropertyMap
) && PropertyMap
[Property
].nameA
)
3107 DWORD size
= PropertyBufferSize
;
3108 LONG l
= RegQueryValueExA(device
->key
, PropertyMap
[Property
].nameA
,
3109 NULL
, PropertyRegDataType
, PropertyBuffer
, &size
);
3111 if (l
== ERROR_FILE_NOT_FOUND
)
3112 SetLastError(ERROR_INVALID_DATA
);
3113 else if (l
== ERROR_MORE_DATA
|| !PropertyBufferSize
)
3114 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
3120 *RequiredSize
= size
;
3125 /***********************************************************************
3126 * SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
3128 BOOL WINAPI
SetupDiGetDeviceRegistryPropertyW(HDEVINFO devinfo
,
3129 SP_DEVINFO_DATA
*device_data
, DWORD Property
, DWORD
*PropertyRegDataType
,
3130 BYTE
*PropertyBuffer
, DWORD PropertyBufferSize
, DWORD
*RequiredSize
)
3133 struct device
*device
;
3135 TRACE("devinfo %p, device_data %p, prop %d, type %p, buffer %p, size %d, required %p\n",
3136 devinfo
, device_data
, Property
, PropertyRegDataType
, PropertyBuffer
, PropertyBufferSize
, RequiredSize
);
3138 if (!(device
= get_device(devinfo
, device_data
)))
3141 if (PropertyBufferSize
&& PropertyBuffer
== NULL
)
3143 SetLastError(ERROR_INVALID_DATA
);
3147 if (Property
< ARRAY_SIZE(PropertyMap
) && PropertyMap
[Property
].nameW
)
3149 DWORD size
= PropertyBufferSize
;
3150 LONG l
= RegQueryValueExW(device
->key
, PropertyMap
[Property
].nameW
,
3151 NULL
, PropertyRegDataType
, PropertyBuffer
, &size
);
3153 if (l
== ERROR_FILE_NOT_FOUND
)
3154 SetLastError(ERROR_INVALID_DATA
);
3155 else if (l
== ERROR_MORE_DATA
|| !PropertyBufferSize
)
3156 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
3162 *RequiredSize
= size
;
3167 /***********************************************************************
3168 * SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
3170 BOOL WINAPI
SetupDiSetDeviceRegistryPropertyA(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
3171 DWORD Property
, const BYTE
*PropertyBuffer
, DWORD PropertyBufferSize
)
3174 struct device
*device
;
3176 TRACE("devinfo %p, device_data %p, prop %d, buffer %p, size %d.\n",
3177 devinfo
, device_data
, Property
, PropertyBuffer
, PropertyBufferSize
);
3179 if (!(device
= get_device(devinfo
, device_data
)))
3182 if (Property
< ARRAY_SIZE(PropertyMap
) && PropertyMap
[Property
].nameA
)
3184 LONG l
= RegSetValueExA(device
->key
, PropertyMap
[Property
].nameA
, 0,
3185 PropertyMap
[Property
].regType
, PropertyBuffer
,
3186 PropertyBufferSize
);
3195 /***********************************************************************
3196 * SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
3198 BOOL WINAPI
SetupDiSetDeviceRegistryPropertyW(HDEVINFO devinfo
,
3199 SP_DEVINFO_DATA
*device_data
, DWORD prop
, const BYTE
*buffer
, DWORD size
)
3201 struct device
*device
;
3203 TRACE("devinfo %p, device_data %p, prop %d, buffer %p, size %d.\n",
3204 devinfo
, device_data
, prop
, buffer
, size
);
3206 if (!(device
= get_device(devinfo
, device_data
)))
3209 return SETUPDI_SetDeviceRegistryPropertyW(device
, prop
, buffer
, size
);
3212 /***********************************************************************
3213 * SetupDiInstallClassA (SETUPAPI.@)
3215 BOOL WINAPI
SetupDiInstallClassA(
3221 UNICODE_STRING FileNameW
;
3226 SetLastError(ERROR_INVALID_PARAMETER
);
3229 if (!RtlCreateUnicodeStringFromAsciiz(&FileNameW
, InfFileName
))
3231 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3235 Result
= SetupDiInstallClassW(hwndParent
, FileNameW
.Buffer
, Flags
, FileQueue
);
3237 RtlFreeUnicodeString(&FileNameW
);
3242 static HKEY
CreateClassKey(HINF hInf
)
3244 static const WCHAR slash
[] = { '\\',0 };
3245 WCHAR FullBuffer
[MAX_PATH
];
3246 WCHAR Buffer
[MAX_PATH
];
3250 if (!SetupGetLineTextW(NULL
,
3258 return INVALID_HANDLE_VALUE
;
3261 lstrcpyW(FullBuffer
, ControlClass
);
3262 lstrcatW(FullBuffer
, slash
);
3263 lstrcatW(FullBuffer
, Buffer
);
3265 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3271 if (!SetupGetLineTextW(NULL
,
3279 return INVALID_HANDLE_VALUE
;
3282 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
3286 REG_OPTION_NON_VOLATILE
,
3292 return INVALID_HANDLE_VALUE
;
3297 if (RegSetValueExW(hClassKey
,
3302 RequiredSize
* sizeof(WCHAR
)))
3304 RegCloseKey(hClassKey
);
3305 RegDeleteKeyW(HKEY_LOCAL_MACHINE
,
3307 return INVALID_HANDLE_VALUE
;
3313 /***********************************************************************
3314 * SetupDiInstallClassW (SETUPAPI.@)
3316 BOOL WINAPI
SetupDiInstallClassW(
3322 WCHAR SectionName
[MAX_PATH
];
3323 DWORD SectionNameLength
= 0;
3325 BOOL bFileQueueCreated
= FALSE
;
3333 SetLastError(ERROR_INVALID_PARAMETER
);
3336 if ((Flags
& DI_NOVCP
) && (FileQueue
== NULL
|| FileQueue
== INVALID_HANDLE_VALUE
))
3338 SetLastError(ERROR_INVALID_PARAMETER
);
3342 /* Open the .inf file */
3343 hInf
= SetupOpenInfFileW(InfFileName
,
3347 if (hInf
== INVALID_HANDLE_VALUE
)
3353 /* Create or open the class registry key 'HKLM\\CurrentControlSet\\Class\\{GUID}' */
3354 hClassKey
= CreateClassKey(hInf
);
3355 if (hClassKey
== INVALID_HANDLE_VALUE
)
3357 SetupCloseInfFile(hInf
);
3362 /* Try to append a layout file */
3363 SetupOpenAppendInfFileW(NULL
, hInf
, NULL
);
3365 /* Retrieve the actual section name */
3366 SetupDiGetActualSectionToInstallW(hInf
,
3374 if (!(Flags
& DI_NOVCP
))
3376 FileQueue
= SetupOpenFileQueue();
3377 if (FileQueue
== INVALID_HANDLE_VALUE
)
3379 SetupCloseInfFile(hInf
);
3383 bFileQueueCreated
= TRUE
;
3388 SetupInstallFromInfSectionW(NULL
,
3391 SPINST_COPYINF
| SPINST_FILES
| SPINST_REGISTRY
,
3397 INVALID_HANDLE_VALUE
,
3400 /* FIXME: More code! */
3402 if (bFileQueueCreated
)
3403 SetupCloseFileQueue(FileQueue
);
3405 SetupCloseInfFile(hInf
);
3411 /***********************************************************************
3412 * SetupDiOpenClassRegKey (SETUPAPI.@)
3414 HKEY WINAPI
SetupDiOpenClassRegKey(
3415 const GUID
* ClassGuid
,
3418 return SetupDiOpenClassRegKeyExW(ClassGuid
, samDesired
,
3419 DIOCR_INSTALLER
, NULL
, NULL
);
3423 /***********************************************************************
3424 * SetupDiOpenClassRegKeyExA (SETUPAPI.@)
3426 HKEY WINAPI
SetupDiOpenClassRegKeyExA(
3427 const GUID
* ClassGuid
,
3433 PWSTR MachineNameW
= NULL
;
3440 MachineNameW
= MultiByteToUnicode(MachineName
, CP_ACP
);
3441 if (MachineNameW
== NULL
)
3442 return INVALID_HANDLE_VALUE
;
3445 hKey
= SetupDiOpenClassRegKeyExW(ClassGuid
, samDesired
,
3446 Flags
, MachineNameW
, Reserved
);
3448 MyFree(MachineNameW
);
3454 /***********************************************************************
3455 * SetupDiOpenClassRegKeyExW (SETUPAPI.@)
3457 HKEY WINAPI
SetupDiOpenClassRegKeyExW(
3458 const GUID
* ClassGuid
,
3469 if (MachineName
&& *MachineName
)
3471 FIXME("Remote access not supported yet!\n");
3472 return INVALID_HANDLE_VALUE
;
3475 if (Flags
== DIOCR_INSTALLER
)
3477 lpKeyName
= ControlClass
;
3479 else if (Flags
== DIOCR_INTERFACE
)
3481 lpKeyName
= DeviceClasses
;
3485 ERR("Invalid Flags parameter!\n");
3486 SetLastError(ERROR_INVALID_PARAMETER
);
3487 return INVALID_HANDLE_VALUE
;
3492 if ((l
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3499 hClassesKey
= INVALID_HANDLE_VALUE
;
3505 WCHAR bracedGuidString
[39];
3507 SETUPDI_GuidToString(ClassGuid
, bracedGuidString
);
3509 if (!(l
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3515 if ((l
= RegOpenKeyExW(hClassesKey
,
3522 key
= INVALID_HANDLE_VALUE
;
3524 RegCloseKey(hClassesKey
);
3529 key
= INVALID_HANDLE_VALUE
;
3535 /***********************************************************************
3536 * SetupDiOpenDeviceInfoA (SETUPAPI.@)
3538 BOOL WINAPI
SetupDiOpenDeviceInfoA(HDEVINFO devinfo
, PCSTR instance_id
, HWND hwnd_parent
, DWORD flags
,
3539 PSP_DEVINFO_DATA device_data
)
3541 WCHAR instance_idW
[MAX_DEVICE_ID_LEN
];
3543 TRACE("%p %s %p 0x%08x %p\n", devinfo
, debugstr_a(instance_id
), hwnd_parent
, flags
, device_data
);
3545 if (!instance_id
|| strlen(instance_id
) >= MAX_DEVICE_ID_LEN
)
3547 SetLastError(ERROR_INVALID_PARAMETER
);
3551 MultiByteToWideChar(CP_ACP
, 0, instance_id
, -1, instance_idW
, ARRAY_SIZE(instance_idW
));
3552 return SetupDiOpenDeviceInfoW(devinfo
, instance_idW
, hwnd_parent
, flags
, device_data
);
3555 /***********************************************************************
3556 * SetupDiOpenDeviceInfoW (SETUPAPI.@)
3558 BOOL WINAPI
SetupDiOpenDeviceInfoW(HDEVINFO devinfo
, PCWSTR instance_id
, HWND hwnd_parent
, DWORD flags
,
3559 PSP_DEVINFO_DATA device_data
)
3561 struct DeviceInfoSet
*set
;
3562 struct device
*device
;
3565 HKEY enumKey
= NULL
;
3566 HKEY instanceKey
= NULL
;
3569 DWORD error
= ERROR_NO_SUCH_DEVINST
;
3571 TRACE("%p %s %p 0x%08x %p\n", devinfo
, debugstr_w(instance_id
), hwnd_parent
, flags
, device_data
);
3573 if (!(set
= get_device_set(devinfo
)))
3578 SetLastError(ERROR_INVALID_PARAMETER
);
3583 FIXME("hwnd_parent unsupported\n");
3586 FIXME("flags unsupported: 0x%08x\n", flags
);
3588 RegCreateKeyExW(HKEY_LOCAL_MACHINE
, Enum
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &enumKey
, NULL
);
3589 /* Instance needs to be already existent in registry, if not, report ERROR_NO_SUCH_DEVINST */
3590 if (RegOpenKeyExW(enumKey
, instance_id
, 0, KEY_READ
, &instanceKey
))
3593 /* If it's an unregistered instance, aka phantom instance, report ERROR_NO_SUCH_DEVINST */
3594 size
= sizeof(phantom
);
3595 if (!RegQueryValueExW(instanceKey
, Phantom
, NULL
, NULL
, (BYTE
*)&phantom
, &size
))
3598 /* Check class GUID */
3599 size
= sizeof(classW
);
3600 if (RegQueryValueExW(instanceKey
, ClassGUID
, NULL
, NULL
, (BYTE
*)classW
, &size
))
3604 UuidFromStringW(&classW
[1], &guid
);
3606 if (!IsEqualGUID(&set
->ClassGuid
, &GUID_NULL
) && !IsEqualGUID(&guid
, &set
->ClassGuid
))
3608 error
= ERROR_CLASS_MISMATCH
;
3612 if (!(device
= create_device(set
, &guid
, instance_id
, FALSE
)))
3615 if (!device_data
|| device_data
->cbSize
== sizeof(SP_DEVINFO_DATA
))
3618 copy_device_data(device_data
, device
);
3622 error
= ERROR_INVALID_USER_BUFFER
;
3625 RegCloseKey(instanceKey
);
3626 RegCloseKey(enumKey
);
3627 SetLastError(error
);
3631 /***********************************************************************
3632 * SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3634 BOOL WINAPI
SetupDiOpenDeviceInterfaceW(
3635 HDEVINFO DeviceInfoSet
,
3638 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData
)
3640 FIXME("%p %s %08x %p\n",
3641 DeviceInfoSet
, debugstr_w(DevicePath
), OpenFlags
, DeviceInterfaceData
);
3645 /***********************************************************************
3646 * SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3648 BOOL WINAPI
SetupDiOpenDeviceInterfaceA(
3649 HDEVINFO DeviceInfoSet
,
3652 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData
)
3654 FIXME("%p %s %08x %p\n", DeviceInfoSet
,
3655 debugstr_a(DevicePath
), OpenFlags
, DeviceInterfaceData
);
3659 /***********************************************************************
3660 * SetupDiOpenDeviceInterfaceRegKey (SETUPAPI.@)
3662 HKEY WINAPI
SetupDiOpenDeviceInterfaceRegKey(HDEVINFO devinfo
, PSP_DEVICE_INTERFACE_DATA iface_data
,
3663 DWORD reserved
, REGSAM access
)
3665 struct device_iface
*iface
;
3669 TRACE("devinfo %p, iface_data %p, reserved %d, access %#x.\n", devinfo
, iface_data
, reserved
, access
);
3671 if (!(iface
= get_device_iface(devinfo
, iface_data
)))
3672 return INVALID_HANDLE_VALUE
;
3674 lr
= RegOpenKeyExW(iface
->refstr_key
, DeviceParameters
, 0, access
, &key
);
3678 return INVALID_HANDLE_VALUE
;
3684 /***********************************************************************
3685 * SetupDiSetClassInstallParamsA (SETUPAPI.@)
3687 BOOL WINAPI
SetupDiSetClassInstallParamsA(
3688 HDEVINFO DeviceInfoSet
,
3689 PSP_DEVINFO_DATA DeviceInfoData
,
3690 PSP_CLASSINSTALL_HEADER ClassInstallParams
,
3691 DWORD ClassInstallParamsSize
)
3693 FIXME("%p %p %x %u\n",DeviceInfoSet
, DeviceInfoData
,
3694 ClassInstallParams
->InstallFunction
, ClassInstallParamsSize
);
3698 /***********************************************************************
3699 * SetupDiSetClassInstallParamsW (SETUPAPI.@)
3701 BOOL WINAPI
SetupDiSetClassInstallParamsW(
3702 HDEVINFO DeviceInfoSet
,
3703 PSP_DEVINFO_DATA DeviceInfoData
,
3704 PSP_CLASSINSTALL_HEADER ClassInstallParams
,
3705 DWORD ClassInstallParamsSize
)
3707 FIXME("%p %p %x %u\n",DeviceInfoSet
, DeviceInfoData
,
3708 ClassInstallParams
->InstallFunction
, ClassInstallParamsSize
);
3712 static BOOL
call_coinstallers(WCHAR
*list
, DI_FUNCTION function
, HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
3714 DWORD (CALLBACK
*coinst_proc
)(DI_FUNCTION
, HDEVINFO
, SP_DEVINFO_DATA
*, COINSTALLER_CONTEXT_DATA
*);
3715 COINSTALLER_CONTEXT_DATA coinst_ctx
;
3716 WCHAR
*p
, *procnameW
;
3721 for (p
= list
; *p
; p
+= lstrlenW(p
) + 1)
3723 TRACE("Found co-installer %s.\n", debugstr_w(p
));
3724 if ((procnameW
= wcschr(p
, ',')))
3727 if ((module
= LoadLibraryExW(p
, NULL
, LOAD_LIBRARY_SEARCH_SYSTEM32
)))
3731 procname
= strdupWtoA(procnameW
+ 1);
3732 coinst_proc
= (void *)GetProcAddress(module
, procname
);
3733 heap_free(procname
);
3736 coinst_proc
= (void *)GetProcAddress(module
, "CoDeviceInstall");
3739 memset(&coinst_ctx
, 0, sizeof(coinst_ctx
));
3740 TRACE("Calling co-installer %p.\n", coinst_proc
);
3741 ret
= coinst_proc(function
, devinfo
, device_data
, &coinst_ctx
);
3742 TRACE("Co-installer %p returned %#x.\n", coinst_proc
, ret
);
3743 if (ret
== ERROR_DI_POSTPROCESSING_REQUIRED
)
3744 FIXME("Co-installer postprocessing not implemented.\n");
3747 ERR("Co-installer returned error %#x.\n", ret
);
3748 FreeLibrary(module
);
3753 FreeLibrary(module
);
3760 /***********************************************************************
3761 * SetupDiCallClassInstaller (SETUPAPI.@)
3763 BOOL WINAPI
SetupDiCallClassInstaller(DI_FUNCTION function
, HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
3765 static const WCHAR class_coinst_pathW
[] = {'S','y','s','t','e','m',
3766 '\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t',
3767 '\\','C','o','n','t','r','o','l',
3768 '\\','C','o','D','e','v','i','c','e','I','n','s','t','a','l','l','e','r','s',0};
3769 static const WCHAR coinstallers32W
[] = {'C','o','I','n','s','t','a','l','l','e','r','s','3','2',0};
3770 static const WCHAR installer32W
[] = {'I','n','s','t','a','l','l','e','r','3','2',0};
3771 DWORD (CALLBACK
*classinst_proc
)(DI_FUNCTION
, HDEVINFO
, SP_DEVINFO_DATA
*);
3772 DWORD ret
= ERROR_DI_DO_DEFAULT
;
3773 HKEY class_key
, coinst_key
;
3774 WCHAR
*path
, *procnameW
;
3775 struct device
*device
;
3782 TRACE("function %#x, devinfo %p, device_data %p.\n", function
, devinfo
, device_data
);
3784 if (!(device
= get_device(devinfo
, device_data
)))
3787 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE
, class_coinst_pathW
, 0, KEY_READ
, &coinst_key
))
3789 SETUPDI_GuidToString(&device
->class, guidstr
);
3790 if (!RegGetValueW(coinst_key
, NULL
, guidstr
, RRF_RT_REG_MULTI_SZ
, NULL
, NULL
, &size
))
3792 path
= heap_alloc(size
);
3793 if (!RegGetValueW(coinst_key
, NULL
, guidstr
, RRF_RT_REG_MULTI_SZ
, NULL
, path
, &size
))
3794 coret
= call_coinstallers(path
, function
, devinfo
, device_data
);
3797 RegCloseKey(coinst_key
);
3803 if (!open_driver_key(device
, KEY_READ
, &coinst_key
))
3805 if (!RegGetValueW(coinst_key
, NULL
, coinstallers32W
, RRF_RT_REG_MULTI_SZ
, NULL
, NULL
, &size
))
3807 path
= heap_alloc(size
);
3808 if (!RegGetValueW(coinst_key
, NULL
, coinstallers32W
, RRF_RT_REG_MULTI_SZ
, NULL
, path
, &size
))
3809 coret
= call_coinstallers(path
, function
, devinfo
, device_data
);
3812 RegCloseKey(coinst_key
);
3815 if ((class_key
= SetupDiOpenClassRegKey(&device
->class, KEY_READ
)) != INVALID_HANDLE_VALUE
)
3817 if (!RegGetValueW(class_key
, NULL
, installer32W
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
))
3819 path
= heap_alloc(size
);
3820 if (!RegGetValueW(class_key
, NULL
, installer32W
, RRF_RT_REG_SZ
, NULL
, path
, &size
))
3822 TRACE("Found class installer %s.\n", debugstr_w(path
));
3823 if ((procnameW
= wcschr(path
, ',')))
3826 if ((module
= LoadLibraryExW(path
, NULL
, LOAD_LIBRARY_SEARCH_SYSTEM32
)))
3830 procname
= strdupWtoA(procnameW
+ 1);
3831 classinst_proc
= (void *)GetProcAddress(module
, procname
);
3832 heap_free(procname
);
3835 classinst_proc
= (void *)GetProcAddress(module
, "ClassInstall");
3838 TRACE("Calling class installer %p.\n", classinst_proc
);
3839 ret
= classinst_proc(function
, devinfo
, device_data
);
3840 TRACE("Class installer %p returned %#x.\n", classinst_proc
, ret
);
3842 FreeLibrary(module
);
3847 RegCloseKey(class_key
);
3850 if (ret
== ERROR_DI_DO_DEFAULT
)
3854 case DIF_REGISTERDEVICE
:
3855 return SetupDiRegisterDeviceInfo(devinfo
, device_data
, 0, NULL
, NULL
, NULL
);
3857 return SetupDiRemoveDevice(devinfo
, device_data
);
3858 case DIF_SELECTBESTCOMPATDRV
:
3859 return SetupDiSelectBestCompatDrv(devinfo
, device_data
);
3860 case DIF_REGISTER_COINSTALLERS
:
3861 return SetupDiRegisterCoDeviceInstallers(devinfo
, device_data
);
3862 case DIF_INSTALLDEVICEFILES
:
3863 return SetupDiInstallDriverFiles(devinfo
, device_data
);
3864 case DIF_INSTALLINTERFACES
:
3865 return SetupDiInstallDeviceInterfaces(devinfo
, device_data
);
3866 case DIF_INSTALLDEVICE
:
3867 return SetupDiInstallDevice(devinfo
, device_data
);
3868 case DIF_FINISHINSTALL_ACTION
:
3869 case DIF_PROPERTYCHANGE
:
3870 case DIF_SELECTDEVICE
:
3872 FIXME("Unhandled function %#x.\n", function
);
3876 if (ret
) SetLastError(ret
);
3880 /***********************************************************************
3881 * SetupDiGetDeviceInstallParamsW (SETUPAPI.@)
3883 BOOL WINAPI
SetupDiGetDeviceInstallParamsW(HDEVINFO devinfo
,
3884 SP_DEVINFO_DATA
*device_data
, SP_DEVINSTALL_PARAMS_W
*params
)
3886 struct device
*device
;
3888 TRACE("devinfo %p, device_data %p, params %p.\n", devinfo
, device_data
, params
);
3890 if (params
->cbSize
!= sizeof(SP_DEVINSTALL_PARAMS_W
))
3892 SetLastError(ERROR_INVALID_USER_BUFFER
);
3896 if (!(device
= get_device(devinfo
, device_data
)))
3899 *params
= device
->params
;
3904 /***********************************************************************
3905 * SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
3907 BOOL WINAPI
SetupDiGetDeviceInstallParamsA(HDEVINFO devinfo
,
3908 SP_DEVINFO_DATA
*device_data
, SP_DEVINSTALL_PARAMS_A
*params
)
3910 SP_DEVINSTALL_PARAMS_W paramsW
;
3913 if (params
->cbSize
!= sizeof(SP_DEVINSTALL_PARAMS_A
))
3915 SetLastError(ERROR_INVALID_USER_BUFFER
);
3919 paramsW
.cbSize
= sizeof(paramsW
);
3920 ret
= SetupDiGetDeviceInstallParamsW(devinfo
, device_data
, ¶msW
);
3921 params
->Flags
= paramsW
.Flags
;
3922 params
->FlagsEx
= paramsW
.FlagsEx
;
3923 params
->hwndParent
= paramsW
.hwndParent
;
3924 params
->InstallMsgHandler
= paramsW
.InstallMsgHandler
;
3925 params
->InstallMsgHandlerContext
= paramsW
.InstallMsgHandlerContext
;
3926 params
->FileQueue
= paramsW
.FileQueue
;
3927 params
->ClassInstallReserved
= paramsW
.ClassInstallReserved
;
3928 params
->Reserved
= paramsW
.Reserved
;
3929 WideCharToMultiByte(CP_ACP
, 0, paramsW
.DriverPath
, -1, params
->DriverPath
, sizeof(params
->DriverPath
), NULL
, NULL
);
3934 /***********************************************************************
3935 * SetupDiSetDeviceInstallParamsA (SETUPAPI.@)
3937 BOOL WINAPI
SetupDiSetDeviceInstallParamsA(HDEVINFO devinfo
,
3938 SP_DEVINFO_DATA
*device_data
, SP_DEVINSTALL_PARAMS_A
*params
)
3940 SP_DEVINSTALL_PARAMS_W paramsW
;
3942 if (params
->cbSize
!= sizeof(SP_DEVINSTALL_PARAMS_A
))
3944 SetLastError(ERROR_INVALID_USER_BUFFER
);
3948 paramsW
.cbSize
= sizeof(paramsW
);
3949 paramsW
.Flags
= params
->Flags
;
3950 paramsW
.FlagsEx
= params
->FlagsEx
;
3951 paramsW
.hwndParent
= params
->hwndParent
;
3952 paramsW
.InstallMsgHandler
= params
->InstallMsgHandler
;
3953 paramsW
.InstallMsgHandlerContext
= params
->InstallMsgHandlerContext
;
3954 paramsW
.FileQueue
= params
->FileQueue
;
3955 paramsW
.ClassInstallReserved
= params
->ClassInstallReserved
;
3956 paramsW
.Reserved
= params
->Reserved
;
3957 MultiByteToWideChar(CP_ACP
, 0, params
->DriverPath
, -1, paramsW
.DriverPath
, ARRAY_SIZE(paramsW
.DriverPath
));
3959 return SetupDiSetDeviceInstallParamsW(devinfo
, device_data
, ¶msW
);
3962 /***********************************************************************
3963 * SetupDiSetDeviceInstallParamsW (SETUPAPI.@)
3965 BOOL WINAPI
SetupDiSetDeviceInstallParamsW(HDEVINFO devinfo
,
3966 SP_DEVINFO_DATA
*device_data
, SP_DEVINSTALL_PARAMS_W
*params
)
3968 struct device
*device
;
3970 TRACE("devinfo %p, device_data %p, params %p.\n", devinfo
, device_data
, params
);
3972 if (params
->cbSize
!= sizeof(SP_DEVINSTALL_PARAMS_W
))
3974 SetLastError(ERROR_INVALID_USER_BUFFER
);
3978 if (!(device
= get_device(devinfo
, device_data
)))
3981 device
->params
= *params
;
3986 BOOL WINAPI
SetupDiSetDevicePropertyW(HDEVINFO devinfo
, PSP_DEVINFO_DATA device_data
, const DEVPROPKEY
*key
,
3987 DEVPROPTYPE type
, const BYTE
*buffer
, DWORD size
, DWORD flags
)
3989 static const WCHAR propertiesW
[] = {'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's', 0};
3990 static const WCHAR formatW
[] = {'\\', '%', '0', '4', 'X', 0};
3991 struct device
*device
;
3992 HKEY properties_hkey
, property_hkey
;
3993 WCHAR property_hkey_path
[44];
3996 TRACE("%p %p %p %#x %p %d %#x\n", devinfo
, device_data
, key
, type
, buffer
, size
, flags
);
3998 if (!(device
= get_device(devinfo
, device_data
)))
4001 if (!key
|| !is_valid_property_type(type
)
4002 || (buffer
&& !size
&& !(type
== DEVPROP_TYPE_EMPTY
|| type
== DEVPROP_TYPE_NULL
))
4003 || (buffer
&& size
&& (type
== DEVPROP_TYPE_EMPTY
|| type
== DEVPROP_TYPE_NULL
)))
4005 SetLastError(ERROR_INVALID_DATA
);
4009 if (size
&& !buffer
)
4011 SetLastError(ERROR_INVALID_USER_BUFFER
);
4017 SetLastError(ERROR_INVALID_FLAGS
);
4021 ls
= RegCreateKeyExW(device
->key
, propertiesW
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, &properties_hkey
, NULL
);
4028 SETUPDI_GuidToString(&key
->fmtid
, property_hkey_path
);
4029 swprintf(property_hkey_path
+ 38, ARRAY_SIZE(property_hkey_path
) - 38, formatW
, key
->pid
);
4031 if (type
== DEVPROP_TYPE_EMPTY
)
4033 ls
= RegDeleteKeyW(properties_hkey
, property_hkey_path
);
4034 RegCloseKey(properties_hkey
);
4035 SetLastError(ls
== ERROR_FILE_NOT_FOUND
? ERROR_NOT_FOUND
: ls
);
4038 else if (type
== DEVPROP_TYPE_NULL
)
4040 if (!(ls
= RegOpenKeyW(properties_hkey
, property_hkey_path
, &property_hkey
)))
4042 ls
= RegDeleteValueW(property_hkey
, NULL
);
4043 RegCloseKey(property_hkey
);
4046 RegCloseKey(properties_hkey
);
4047 SetLastError(ls
== ERROR_FILE_NOT_FOUND
? ERROR_NOT_FOUND
: ls
);
4052 if (!(ls
= RegCreateKeyExW(properties_hkey
, property_hkey_path
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
,
4053 &property_hkey
, NULL
)))
4055 ls
= RegSetValueExW(property_hkey
, NULL
, 0, 0xffff0000 | (0xffff & type
), buffer
, size
);
4056 RegCloseKey(property_hkey
);
4059 RegCloseKey(properties_hkey
);
4065 /***********************************************************************
4066 * SetupDiOpenDevRegKey (SETUPAPI.@)
4068 HKEY WINAPI
SetupDiOpenDevRegKey(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
4069 DWORD Scope
, DWORD HwProfile
, DWORD KeyType
, REGSAM samDesired
)
4071 struct device
*device
;
4072 HKEY key
= INVALID_HANDLE_VALUE
;
4075 TRACE("devinfo %p, device_data %p, scope %d, profile %d, type %d, access %#x.\n",
4076 devinfo
, device_data
, Scope
, HwProfile
, KeyType
, samDesired
);
4078 if (!(device
= get_device(devinfo
, device_data
)))
4079 return INVALID_HANDLE_VALUE
;
4081 if (Scope
!= DICS_FLAG_GLOBAL
&& Scope
!= DICS_FLAG_CONFIGSPECIFIC
)
4083 SetLastError(ERROR_INVALID_FLAGS
);
4084 return INVALID_HANDLE_VALUE
;
4086 if (KeyType
!= DIREG_DEV
&& KeyType
!= DIREG_DRV
)
4088 SetLastError(ERROR_INVALID_FLAGS
);
4089 return INVALID_HANDLE_VALUE
;
4092 if (device
->phantom
)
4094 SetLastError(ERROR_DEVINFO_NOT_REGISTERED
);
4095 return INVALID_HANDLE_VALUE
;
4097 if (Scope
!= DICS_FLAG_GLOBAL
)
4098 FIXME("unimplemented for scope %d\n", Scope
);
4102 l
= RegOpenKeyExW(device
->key
, DeviceParameters
, 0, samDesired
, &key
);
4105 l
= open_driver_key(device
, samDesired
, &key
);
4108 FIXME("Unhandled type %#x.\n", KeyType
);
4109 l
= ERROR_CALL_NOT_IMPLEMENTED
;
4111 SetLastError(l
== ERROR_FILE_NOT_FOUND
? ERROR_KEY_DOES_NOT_EXIST
: l
);
4112 return l
? INVALID_HANDLE_VALUE
: key
;
4115 /***********************************************************************
4116 * SetupDiDeleteDevRegKey (SETUPAPI.@)
4118 BOOL WINAPI
SetupDiDeleteDevRegKey(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
4119 DWORD Scope
, DWORD HwProfile
, DWORD KeyType
)
4121 struct device
*device
;
4124 TRACE("devinfo %p, device_data %p, scope %d, profile %d, type %d.\n",
4125 devinfo
, device_data
, Scope
, HwProfile
, KeyType
);
4127 if (!(device
= get_device(devinfo
, device_data
)))
4130 if (Scope
!= DICS_FLAG_GLOBAL
&& Scope
!= DICS_FLAG_CONFIGSPECIFIC
)
4132 SetLastError(ERROR_INVALID_FLAGS
);
4135 if (KeyType
!= DIREG_DEV
&& KeyType
!= DIREG_DRV
&& KeyType
!= DIREG_BOTH
)
4137 SetLastError(ERROR_INVALID_FLAGS
);
4141 if (device
->phantom
)
4143 SetLastError(ERROR_DEVINFO_NOT_REGISTERED
);
4146 if (Scope
!= DICS_FLAG_GLOBAL
)
4147 FIXME("unimplemented for scope %d\n", Scope
);
4151 l
= delete_driver_key(device
);
4154 if ((l
= delete_driver_key(device
)))
4158 l
= RegDeleteKeyW(device
->key
, DeviceParameters
);
4161 FIXME("Unhandled type %#x.\n", KeyType
);
4162 l
= ERROR_CALL_NOT_IMPLEMENTED
;
4168 /***********************************************************************
4169 * CM_Get_Device_IDA (SETUPAPI.@)
4171 CONFIGRET WINAPI
CM_Get_Device_IDA(DEVINST devnode
, char *buffer
, ULONG len
, ULONG flags
)
4173 struct device
*device
= get_devnode_device(devnode
);
4175 TRACE("%u, %p, %u, %#x\n", devnode
, buffer
, len
, flags
);
4178 return CR_NO_SUCH_DEVINST
;
4180 WideCharToMultiByte(CP_ACP
, 0, device
->instanceId
, -1, buffer
, len
, 0, 0);
4181 TRACE("Returning %s\n", debugstr_a(buffer
));
4185 /***********************************************************************
4186 * CM_Get_Device_IDW (SETUPAPI.@)
4188 CONFIGRET WINAPI
CM_Get_Device_IDW(DEVINST devnode
, WCHAR
*buffer
, ULONG len
, ULONG flags
)
4190 struct device
*device
= get_devnode_device(devnode
);
4192 TRACE("%u, %p, %u, %#x\n", devnode
, buffer
, len
, flags
);
4195 return CR_NO_SUCH_DEVINST
;
4197 lstrcpynW(buffer
, device
->instanceId
, len
);
4198 TRACE("Returning %s\n", debugstr_w(buffer
));
4202 /***********************************************************************
4203 * CM_Get_Device_ID_Size (SETUPAPI.@)
4205 CONFIGRET WINAPI
CM_Get_Device_ID_Size(ULONG
*len
, DEVINST devnode
, ULONG flags
)
4207 struct device
*device
= get_devnode_device(devnode
);
4209 TRACE("%p, %u, %#x\n", len
, devnode
, flags
);
4212 return CR_NO_SUCH_DEVINST
;
4214 *len
= lstrlenW(device
->instanceId
);
4218 /***********************************************************************
4219 * SetupDiGetINFClassA (SETUPAPI.@)
4221 BOOL WINAPI
SetupDiGetINFClassA(PCSTR inf
, LPGUID class_guid
, PSTR class_name
,
4222 DWORD size
, PDWORD required_size
)
4225 DWORD required_sizeA
, required_sizeW
;
4226 PWSTR class_nameW
= NULL
;
4227 UNICODE_STRING infW
;
4231 if (!RtlCreateUnicodeStringFromAsciiz(&infW
, inf
))
4233 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4240 if (class_name
&& size
)
4242 if (!(class_nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
))))
4244 RtlFreeUnicodeString(&infW
);
4245 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
4250 retval
= SetupDiGetINFClassW(infW
.Buffer
, class_guid
, class_nameW
, size
, &required_sizeW
);
4254 required_sizeA
= WideCharToMultiByte( CP_ACP
, 0, class_nameW
, required_sizeW
,
4255 class_name
, size
, NULL
, NULL
);
4257 if(required_size
) *required_size
= required_sizeA
;
4260 if(required_size
) *required_size
= required_sizeW
;
4262 HeapFree(GetProcessHeap(), 0, class_nameW
);
4263 RtlFreeUnicodeString(&infW
);
4267 /***********************************************************************
4268 * SetupDiGetINFClassW (SETUPAPI.@)
4270 BOOL WINAPI
SetupDiGetINFClassW(PCWSTR inf
, LPGUID class_guid
, PWSTR class_name
,
4271 DWORD size
, PDWORD required_size
)
4273 BOOL have_guid
, have_name
;
4275 WCHAR buffer
[MAX_PATH
];
4279 SetLastError(ERROR_INVALID_PARAMETER
);
4283 if (INVALID_FILE_ATTRIBUTES
== GetFileAttributesW(inf
))
4285 FIXME("%s not found. Searching via DevicePath not implemented\n", debugstr_w(inf
));
4286 SetLastError(ERROR_FILE_NOT_FOUND
);
4290 if (!class_guid
|| !class_name
|| !size
)
4292 SetLastError(ERROR_INVALID_PARAMETER
);
4296 if (!GetPrivateProfileStringW(Version
, Signature
, NULL
, buffer
, MAX_PATH
, inf
))
4299 if (lstrcmpiW(buffer
, Chicago
) && lstrcmpiW(buffer
, WindowsNT
))
4303 have_guid
= 0 < GetPrivateProfileStringW(Version
, ClassGUID
, NULL
, buffer
, MAX_PATH
, inf
);
4306 buffer
[lstrlenW(buffer
)-1] = 0;
4307 if (RPC_S_OK
!= UuidFromStringW(buffer
+ 1, class_guid
))
4309 FIXME("failed to convert \"%s\" into a guid\n", debugstr_w(buffer
));
4310 SetLastError(ERROR_INVALID_PARAMETER
);
4316 dret
= GetPrivateProfileStringW(Version
, Class
, NULL
, buffer
, MAX_PATH
, inf
);
4317 have_name
= 0 < dret
;
4319 if (dret
>= MAX_PATH
-1) FIXME("buffer might be too small\n");
4320 if (have_guid
&& !have_name
)
4322 class_name
[0] = '\0';
4323 FIXME("class name lookup via guid not implemented\n");
4328 if (dret
< size
) lstrcpyW(class_name
, buffer
);
4331 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
4336 if (required_size
) *required_size
= dret
+ ((dret
) ? 1 : 0);
4338 return (have_guid
|| have_name
);
4341 static LSTATUS
get_device_property(struct device
*device
, const DEVPROPKEY
*prop_key
, DEVPROPTYPE
*prop_type
,
4342 BYTE
*prop_buff
, DWORD prop_buff_size
, DWORD
*required_size
, DWORD flags
)
4344 WCHAR key_path
[55] = L
"Properties\\";
4347 DWORD value_size
= 0;
4351 return ERROR_INVALID_DATA
;
4353 if (!prop_type
|| (!prop_buff
&& prop_buff_size
))
4354 return ERROR_INVALID_USER_BUFFER
;
4357 return ERROR_INVALID_FLAGS
;
4359 SETUPDI_GuidToString(&prop_key
->fmtid
, key_path
+ 11);
4360 swprintf(key_path
+ 49, ARRAY_SIZE(key_path
) - 49, L
"\\%04X", prop_key
->pid
);
4362 ls
= RegOpenKeyExW(device
->key
, key_path
, 0, KEY_QUERY_VALUE
, &hkey
);
4365 value_size
= prop_buff_size
;
4366 ls
= RegQueryValueExW(hkey
, NULL
, NULL
, &value_type
, prop_buff
, &value_size
);
4373 case ERROR_MORE_DATA
:
4374 *prop_type
= 0xffff & value_type
;
4375 ls
= (ls
== ERROR_MORE_DATA
|| !prop_buff
) ? ERROR_INSUFFICIENT_BUFFER
: NO_ERROR
;
4377 case ERROR_FILE_NOT_FOUND
:
4378 *prop_type
= DEVPROP_TYPE_EMPTY
;
4380 ls
= ERROR_NOT_FOUND
;
4383 *prop_type
= DEVPROP_TYPE_EMPTY
;
4385 FIXME("Unhandled error %#x\n", ls
);
4390 *required_size
= value_size
;
4395 /***********************************************************************
4396 * SetupDiGetDevicePropertyW (SETUPAPI.@)
4398 BOOL WINAPI
SetupDiGetDevicePropertyW(HDEVINFO devinfo
, PSP_DEVINFO_DATA device_data
,
4399 const DEVPROPKEY
*prop_key
, DEVPROPTYPE
*prop_type
, BYTE
*prop_buff
,
4400 DWORD prop_buff_size
, DWORD
*required_size
, DWORD flags
)
4402 struct device
*device
;
4405 TRACE("%p, %p, %p, %p, %p, %d, %p, %#x\n", devinfo
, device_data
, prop_key
, prop_type
, prop_buff
, prop_buff_size
,
4406 required_size
, flags
);
4408 if (!(device
= get_device(devinfo
, device_data
)))
4411 ls
= get_device_property(device
, prop_key
, prop_type
, prop_buff
, prop_buff_size
, required_size
, flags
);
4417 /***********************************************************************
4418 * CM_Get_DevNode_Property_ExW (SETUPAPI.@)
4420 CONFIGRET WINAPI
CM_Get_DevNode_Property_ExW(DEVINST devnode
, const DEVPROPKEY
*prop_key
, DEVPROPTYPE
*prop_type
,
4421 BYTE
*prop_buff
, ULONG
*prop_buff_size
, ULONG flags
, HMACHINE machine
)
4423 struct device
*device
= get_devnode_device(devnode
);
4426 TRACE("%u, %p, %p, %p, %p, %#x, %p\n", devnode
, prop_key
, prop_type
, prop_buff
, prop_buff_size
,
4430 return CR_MACHINE_UNAVAILABLE
;
4433 return CR_NO_SUCH_DEVINST
;
4435 if (!prop_buff_size
)
4436 return CR_INVALID_POINTER
;
4438 ls
= get_device_property(device
, prop_key
, prop_type
, prop_buff
, *prop_buff_size
, prop_buff_size
, flags
);
4443 case ERROR_INVALID_DATA
:
4444 return CR_INVALID_DATA
;
4445 case ERROR_INVALID_USER_BUFFER
:
4446 return CR_INVALID_POINTER
;
4447 case ERROR_INVALID_FLAGS
:
4448 return CR_INVALID_FLAG
;
4449 case ERROR_INSUFFICIENT_BUFFER
:
4450 return CR_BUFFER_SMALL
;
4451 case ERROR_NOT_FOUND
:
4452 return CR_NO_SUCH_VALUE
;
4457 /***********************************************************************
4458 * CM_Get_DevNode_PropertyW (SETUPAPI.@)
4460 CONFIGRET WINAPI
CM_Get_DevNode_PropertyW(DEVINST dev
, const DEVPROPKEY
*key
, DEVPROPTYPE
*type
,
4461 PVOID buf
, PULONG len
, ULONG flags
)
4463 return CM_Get_DevNode_Property_ExW(dev
, key
, type
, buf
, len
, flags
, NULL
);
4466 /***********************************************************************
4467 * SetupDiInstallDeviceInterfaces (SETUPAPI.@)
4469 BOOL WINAPI
SetupDiInstallDeviceInterfaces(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
4471 WCHAR section_ext
[LINE_LEN
], iface_section
[LINE_LEN
], refstr
[LINE_LEN
], guidstr
[39];
4472 UINT install_flags
= SPINST_ALL
;
4473 struct device_iface
*iface
;
4474 struct device
*device
;
4475 struct driver
*driver
;
4483 TRACE("devinfo %p, device_data %p.\n", devinfo
, device_data
);
4485 if (!(device
= get_device(devinfo
, device_data
)))
4488 if (!(driver
= device
->selected_driver
))
4490 ERR("No driver selected for device %p.\n", devinfo
);
4491 SetLastError(ERROR_NO_DRIVER_SELECTED
);
4495 if ((hinf
= SetupOpenInfFileW(driver
->inf_path
, NULL
, INF_STYLE_WIN4
, NULL
)) == INVALID_HANDLE_VALUE
)
4498 SetupDiGetActualSectionToInstallW(hinf
, driver
->section
, section_ext
, ARRAY_SIZE(section_ext
), NULL
, NULL
);
4500 if (device
->params
.Flags
& DI_NOFILECOPY
)
4501 install_flags
&= ~SPINST_FILES
;
4503 callback_ctx
= SetupInitDefaultQueueCallback(NULL
);
4505 lstrcatW(section_ext
, dotInterfaces
);
4506 if (SetupFindFirstLineW(hinf
, section_ext
, AddInterface
, &ctx
))
4509 SetupGetStringFieldW(&ctx
, 1, guidstr
, ARRAY_SIZE(guidstr
), NULL
);
4510 SetupGetStringFieldW(&ctx
, 2, refstr
, ARRAY_SIZE(refstr
), NULL
);
4512 UuidFromStringW(&guidstr
[1], &iface_guid
);
4514 if (!(iface
= SETUPDI_CreateDeviceInterface(device
, &iface_guid
, refstr
)))
4516 ERR("Failed to create device interface, error %#x.\n", GetLastError());
4520 if ((l
= create_iface_key(iface
, KEY_ALL_ACCESS
, &iface_key
)))
4522 ERR("Failed to create interface key, error %u.\n", l
);
4526 SetupGetStringFieldW(&ctx
, 3, iface_section
, ARRAY_SIZE(iface_section
), NULL
);
4527 SetupInstallFromInfSectionW(NULL
, hinf
, iface_section
, install_flags
, iface_key
,
4528 NULL
, SP_COPY_NEWER_ONLY
, SetupDefaultQueueCallbackW
, callback_ctx
, NULL
, NULL
);
4530 RegCloseKey(iface_key
);
4531 } while (SetupFindNextMatchLineW(&ctx
, AddInterface
, &ctx
));
4534 SetupTermDefaultQueueCallback(callback_ctx
);
4536 SetupCloseInfFile(hinf
);
4540 /***********************************************************************
4541 * SetupDiRegisterCoDeviceInstallers (SETUPAPI.@)
4543 BOOL WINAPI
SetupDiRegisterCoDeviceInstallers(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
4545 static const WCHAR coinstallersW
[] = {'.','C','o','I','n','s','t','a','l','l','e','r','s',0};
4546 WCHAR coinst_key_ext
[LINE_LEN
];
4547 struct device
*device
;
4548 struct driver
*driver
;
4554 TRACE("devinfo %p, device_data %p.\n", devinfo
, device_data
);
4556 if (!(device
= get_device(devinfo
, device_data
)))
4559 if (!(driver
= device
->selected_driver
))
4561 ERR("No driver selected for device %p.\n", devinfo
);
4562 SetLastError(ERROR_NO_DRIVER_SELECTED
);
4566 if ((hinf
= SetupOpenInfFileW(driver
->inf_path
, NULL
, INF_STYLE_WIN4
, NULL
)) == INVALID_HANDLE_VALUE
)
4569 SetupDiGetActualSectionToInstallW(hinf
, driver
->section
, coinst_key_ext
, ARRAY_SIZE(coinst_key_ext
), NULL
, NULL
);
4570 lstrcatW(coinst_key_ext
, coinstallersW
);
4572 if ((l
= create_driver_key(device
, &driver_key
)))
4575 SetupCloseInfFile(hinf
);
4579 callback_ctx
= SetupInitDefaultQueueCallback(NULL
);
4580 SetupInstallFromInfSectionW(NULL
, hinf
, coinst_key_ext
, SPINST_ALL
, driver_key
, NULL
,
4581 SP_COPY_NEWER_ONLY
, SetupDefaultQueueCallbackW
, callback_ctx
, NULL
, NULL
);
4582 SetupTermDefaultQueueCallback(callback_ctx
);
4584 RegCloseKey(driver_key
);
4585 SetupCloseInfFile(hinf
);
4589 /* Check whether the given hardware or compatible ID matches any of the device's
4590 * own hardware or compatible IDs. */
4591 static BOOL
device_matches_id(const struct device
*device
, const WCHAR
*id_type
, const WCHAR
*id
)
4597 if (!RegGetValueW(device
->key
, NULL
, id_type
, RRF_RT_REG_MULTI_SZ
, NULL
, NULL
, &size
))
4599 device_ids
= heap_alloc(size
);
4600 if (!RegGetValueW(device
->key
, NULL
, id_type
, RRF_RT_REG_MULTI_SZ
, NULL
, device_ids
, &size
))
4602 for (p
= device_ids
; *p
; p
+= lstrlenW(p
) + 1)
4604 if (!wcsicmp(p
, id
))
4606 heap_free(device_ids
);
4611 heap_free(device_ids
);
4617 static BOOL
version_is_compatible(const WCHAR
*version
)
4619 const WCHAR
*machine_ext
= NtPlatformExtension
+ 1, *p
;
4620 size_t len
= lstrlenW(version
);
4623 /* We are only concerned with architecture. */
4624 if ((p
= wcschr(version
, '.')))
4627 if (!wcsnicmp(version
, NtExtension
+ 1, len
))
4630 if (IsWow64Process(GetCurrentProcess(), &wow64
) && wow64
)
4633 static const WCHAR wow_ext
[] = {'N','T','a','m','d','6','4',0};
4634 machine_ext
= wow_ext
;
4635 #elif defined(__arm__)
4636 static const WCHAR wow_ext
[] = {'N','T','a','r','m','6','4',0};
4637 machine_ext
= wow_ext
;
4641 return !wcsnicmp(version
, machine_ext
, len
);
4644 static void enum_compat_drivers_from_file(struct device
*device
, const WCHAR
*path
)
4646 static const WCHAR manufacturerW
[] = {'M','a','n','u','f','a','c','t','u','r','e','r',0};
4647 WCHAR mfg_name
[LINE_LEN
], mfg_key
[LINE_LEN
], mfg_key_ext
[LINE_LEN
], id
[MAX_DEVICE_ID_LEN
], version
[MAX_DEVICE_ID_LEN
];
4652 TRACE("Enumerating drivers from %s.\n", debugstr_w(path
));
4654 if ((hinf
= SetupOpenInfFileW(path
, NULL
, INF_STYLE_WIN4
, NULL
)) == INVALID_HANDLE_VALUE
)
4657 for (i
= 0; SetupGetLineByIndexW(hinf
, manufacturerW
, i
, &ctx
); ++i
)
4659 SetupGetStringFieldW(&ctx
, 0, mfg_name
, ARRAY_SIZE(mfg_name
), NULL
);
4660 if (!SetupGetStringFieldW(&ctx
, 1, mfg_key
, ARRAY_SIZE(mfg_key
), NULL
))
4661 lstrcpyW(mfg_key
, mfg_name
);
4663 if (SetupGetFieldCount(&ctx
) >= 2)
4665 BOOL compatible
= FALSE
;
4666 for (j
= 2; SetupGetStringFieldW(&ctx
, j
, version
, ARRAY_SIZE(version
), NULL
); ++j
)
4668 if (version_is_compatible(version
))
4678 if (!SetupDiGetActualSectionToInstallW(hinf
, mfg_key
, mfg_key_ext
, ARRAY_SIZE(mfg_key_ext
), NULL
, NULL
))
4680 WARN("Failed to find section for %s, skipping.\n", debugstr_w(mfg_key
));
4684 for (j
= 0; SetupGetLineByIndexW(hinf
, mfg_key_ext
, j
, &ctx
); ++j
)
4686 for (k
= 2; SetupGetStringFieldW(&ctx
, k
, id
, ARRAY_SIZE(id
), NULL
); ++k
)
4688 if (device_matches_id(device
, HardwareId
, id
) || device_matches_id(device
, CompatibleIDs
, id
))
4690 unsigned int count
= ++device
->driver_count
;
4692 device
->drivers
= heap_realloc(device
->drivers
, count
* sizeof(*device
->drivers
));
4693 lstrcpyW(device
->drivers
[count
- 1].inf_path
, path
);
4694 lstrcpyW(device
->drivers
[count
- 1].manufacturer
, mfg_name
);
4695 lstrcpyW(device
->drivers
[count
- 1].mfg_key
, mfg_key_ext
);
4696 SetupGetStringFieldW(&ctx
, 0, device
->drivers
[count
- 1].description
,
4697 ARRAY_SIZE(device
->drivers
[count
- 1].description
), NULL
);
4698 SetupGetStringFieldW(&ctx
, 1, device
->drivers
[count
- 1].section
,
4699 ARRAY_SIZE(device
->drivers
[count
- 1].section
), NULL
);
4701 TRACE("Found compatible driver: manufacturer %s, desc %s.\n",
4702 debugstr_w(mfg_name
), debugstr_w(device
->drivers
[count
- 1].description
));
4708 SetupCloseInfFile(hinf
);
4711 /***********************************************************************
4712 * SetupDiBuildDriverInfoList (SETUPAPI.@)
4714 BOOL WINAPI
SetupDiBuildDriverInfoList(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
, DWORD type
)
4716 struct device
*device
;
4718 TRACE("devinfo %p, device_data %p, type %#x.\n", devinfo
, device_data
, type
);
4720 if (type
!= SPDIT_COMPATDRIVER
)
4722 FIXME("Unhandled type %#x.\n", type
);
4723 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
4727 if (!(device
= get_device(devinfo
, device_data
)))
4730 if (device
->params
.Flags
& DI_ENUMSINGLEINF
)
4732 enum_compat_drivers_from_file(device
, device
->params
.DriverPath
);
4736 static const WCHAR default_path
[] = {'C',':','/','w','i','n','d','o','w','s','/','i','n','f',0};
4737 static const WCHAR wildcardW
[] = {'*',0};
4738 WCHAR dir
[MAX_PATH
], file
[MAX_PATH
];
4739 WIN32_FIND_DATAW find_data
;
4742 if (device
->params
.DriverPath
[0])
4743 lstrcpyW(dir
, device
->params
.DriverPath
);
4745 lstrcpyW(dir
, default_path
);
4746 lstrcatW(dir
, backslashW
);
4747 lstrcatW(dir
, wildcardW
);
4749 TRACE("Searching for drivers in %s.\n", debugstr_w(dir
));
4751 if ((find_handle
= FindFirstFileW(dir
, &find_data
)) != INVALID_HANDLE_VALUE
)
4755 lstrcpyW(file
, dir
);
4756 lstrcpyW(file
+ lstrlenW(file
) - 1, find_data
.cFileName
);
4757 enum_compat_drivers_from_file(device
, file
);
4758 } while (FindNextFileW(find_handle
, &find_data
));
4760 FindClose(find_handle
);
4764 if (device
->driver_count
)
4766 WCHAR classname
[MAX_CLASS_NAME_LEN
], guidstr
[39];
4769 if (SetupDiGetINFClassW(device
->drivers
[0].inf_path
, &class, classname
, ARRAY_SIZE(classname
), NULL
))
4771 device_data
->ClassGuid
= device
->class = class;
4772 SETUPDI_GuidToString(&class, guidstr
);
4773 RegSetValueExW(device
->key
, L
"ClassGUID", 0, REG_SZ
, (BYTE
*)guidstr
, sizeof(guidstr
));
4774 RegSetValueExW(device
->key
, L
"Class", 0, REG_SZ
, (BYTE
*)classname
, wcslen(classname
) * sizeof(WCHAR
));
4781 static BOOL
copy_driver_data(SP_DRVINFO_DATA_W
*data
, const struct driver
*driver
)
4786 if ((hinf
= SetupOpenInfFileW(driver
->inf_path
, NULL
, INF_STYLE_WIN4
, NULL
)) == INVALID_HANDLE_VALUE
)
4789 data
->ProviderName
[0] = 0;
4790 if (SetupFindFirstLineW(hinf
, L
"Version", L
"Provider", &ctx
))
4791 SetupGetStringFieldW(&ctx
, 1, data
->ProviderName
, ARRAY_SIZE(data
->ProviderName
), NULL
);
4792 wcscpy(data
->Description
, driver
->description
);
4793 wcscpy(data
->MfgName
, driver
->manufacturer
);
4794 data
->DriverType
= SPDIT_COMPATDRIVER
;
4795 data
->Reserved
= (ULONG_PTR
)driver
;
4797 SetupCloseInfFile(hinf
);
4802 static void driver_data_wtoa(SP_DRVINFO_DATA_A
*a
, const SP_DRVINFO_DATA_W
*w
)
4804 a
->DriverType
= w
->DriverType
;
4805 a
->Reserved
= w
->Reserved
;
4806 WideCharToMultiByte(CP_ACP
, 0, w
->Description
, -1, a
->Description
, sizeof(a
->Description
), NULL
, NULL
);
4807 WideCharToMultiByte(CP_ACP
, 0, w
->MfgName
, -1, a
->MfgName
, sizeof(a
->MfgName
), NULL
, NULL
);
4808 WideCharToMultiByte(CP_ACP
, 0, w
->ProviderName
, -1, a
->ProviderName
, sizeof(a
->ProviderName
), NULL
, NULL
);
4811 /***********************************************************************
4812 * SetupDiEnumDriverInfoW (SETUPAPI.@)
4814 BOOL WINAPI
SetupDiEnumDriverInfoW(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
4815 DWORD type
, DWORD index
, SP_DRVINFO_DATA_W
*driver_data
)
4817 struct device
*device
;
4819 TRACE("devinfo %p, device_data %p, type %#x, index %u, driver_data %p.\n",
4820 devinfo
, device_data
, type
, index
, driver_data
);
4822 if (type
!= SPDIT_COMPATDRIVER
)
4824 FIXME("Unhandled type %#x.\n", type
);
4825 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
4829 if (!(device
= get_device(devinfo
, device_data
)))
4832 if (index
>= device
->driver_count
)
4834 SetLastError(ERROR_NO_MORE_ITEMS
);
4838 return copy_driver_data(driver_data
, &device
->drivers
[index
]);
4841 /***********************************************************************
4842 * SetupDiEnumDriverInfoA (SETUPAPI.@)
4844 BOOL WINAPI
SetupDiEnumDriverInfoA(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
4845 DWORD type
, DWORD index
, SP_DRVINFO_DATA_A
*driver_data
)
4847 SP_DRVINFO_DATA_W driver_dataW
;
4850 driver_dataW
.cbSize
= sizeof(driver_dataW
);
4851 ret
= SetupDiEnumDriverInfoW(devinfo
, device_data
, type
, index
, &driver_dataW
);
4852 if (ret
) driver_data_wtoa(driver_data
, &driver_dataW
);
4857 /***********************************************************************
4858 * SetupDiSelectBestCompatDrv (SETUPAPI.@)
4860 BOOL WINAPI
SetupDiSelectBestCompatDrv(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
4862 struct device
*device
;
4864 TRACE("devinfo %p, device_data %p.\n", devinfo
, device_data
);
4866 if (!(device
= get_device(devinfo
, device_data
)))
4869 if (!device
->driver_count
)
4871 WARN("No compatible drivers were enumerated for device %s.\n", debugstr_w(device
->instanceId
));
4872 SetLastError(ERROR_NO_COMPAT_DRIVERS
);
4876 WARN("Semi-stub, selecting the first available driver.\n");
4878 device
->selected_driver
= &device
->drivers
[0];
4883 /***********************************************************************
4884 * SetupDiGetSelectedDriverW (SETUPAPI.@)
4886 BOOL WINAPI
SetupDiGetSelectedDriverW(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
, SP_DRVINFO_DATA_W
*driver_data
)
4888 struct device
*device
;
4890 TRACE("devinfo %p, device_data %p, driver_data %p.\n", devinfo
, device_data
, driver_data
);
4892 if (!(device
= get_device(devinfo
, device_data
)))
4895 if (!device
->selected_driver
)
4897 SetLastError(ERROR_NO_DRIVER_SELECTED
);
4901 return copy_driver_data(driver_data
, device
->selected_driver
);
4904 /***********************************************************************
4905 * SetupDiGetSelectedDriverA (SETUPAPI.@)
4907 BOOL WINAPI
SetupDiGetSelectedDriverA(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
, SP_DRVINFO_DATA_A
*driver_data
)
4909 SP_DRVINFO_DATA_W driver_dataW
;
4912 driver_dataW
.cbSize
= sizeof(driver_dataW
);
4913 if ((ret
= SetupDiGetSelectedDriverW(devinfo
, device_data
, &driver_dataW
)))
4914 driver_data_wtoa(driver_data
, &driver_dataW
);
4918 /***********************************************************************
4919 * SetupDiGetDriverInfoDetailW (SETUPAPI.@)
4921 BOOL WINAPI
SetupDiGetDriverInfoDetailW(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
4922 SP_DRVINFO_DATA_W
*driver_data
, SP_DRVINFO_DETAIL_DATA_W
*detail_data
, const DWORD size
, DWORD
*ret_size
)
4924 struct driver
*driver
= (struct driver
*)driver_data
->Reserved
;
4925 DWORD size_needed
, i
, id_size
= 1;
4926 WCHAR id
[MAX_DEVICE_ID_LEN
];
4931 TRACE("devinfo %p, device_data %p, driver_data %p, detail_data %p, size %u, ret_size %p.\n",
4932 devinfo
, device_data
, driver_data
, detail_data
, size
, ret_size
);
4934 if ((detail_data
|| size
) && size
< sizeof(SP_DRVINFO_DETAIL_DATA_W
))
4936 SetLastError(ERROR_INVALID_USER_BUFFER
);
4940 if ((hinf
= SetupOpenInfFileW(driver
->inf_path
, NULL
, INF_STYLE_WIN4
, NULL
)) == INVALID_HANDLE_VALUE
)
4943 SetupFindFirstLineW(hinf
, driver
->mfg_key
, driver
->description
, &ctx
);
4944 for (i
= 2; SetupGetStringFieldW(&ctx
, i
, id
, ARRAY_SIZE(id
), NULL
); ++i
)
4945 id_size
+= wcslen(id
) + 1;
4947 size_needed
= FIELD_OFFSET(SP_DRVINFO_DETAIL_DATA_W
, HardwareID
[id_size
]);
4949 *ret_size
= size_needed
;
4953 detail_data
->CompatIDsLength
= detail_data
->CompatIDsOffset
= 0;
4954 detail_data
->HardwareID
[0] = 0;
4956 if (size
>= size_needed
)
4959 for (i
= 2; SetupGetStringFieldW(&ctx
, i
, id
, ARRAY_SIZE(id
), NULL
); ++i
)
4961 wcscpy(&detail_data
->HardwareID
[id_size
], id
);
4963 detail_data
->CompatIDsOffset
= id_size
;
4964 id_size
+= wcslen(id
) + 1;
4966 detail_data
->HardwareID
[id_size
++] = 0;
4968 detail_data
->CompatIDsLength
= id_size
- detail_data
->CompatIDsOffset
;
4971 SetupCloseInfFile(hinf
);
4973 if ((file
= CreateFileW(driver
->inf_path
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
)) == INVALID_HANDLE_VALUE
)
4975 GetFileTime(file
, NULL
, NULL
, &detail_data
->InfDate
);
4978 wcscpy(detail_data
->SectionName
, driver
->section
);
4979 wcscpy(detail_data
->InfFileName
, driver
->inf_path
);
4980 wcscpy(detail_data
->DrvDescription
, driver
->description
);
4982 if (size
< size_needed
)
4984 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
4991 /***********************************************************************
4992 * SetupDiGetDriverInfoDetailA (SETUPAPI.@)
4994 BOOL WINAPI
SetupDiGetDriverInfoDetailA(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
,
4995 SP_DRVINFO_DATA_A
*driver_data
, SP_DRVINFO_DETAIL_DATA_A
*detail_data
, const DWORD size
, DWORD
*ret_size
)
4997 struct driver
*driver
= (struct driver
*)driver_data
->Reserved
;
4998 DWORD size_needed
, i
, id_size
= 1;
4999 char id
[MAX_DEVICE_ID_LEN
];
5004 TRACE("devinfo %p, device_data %p, driver_data %p, detail_data %p, size %u, ret_size %p.\n",
5005 devinfo
, device_data
, driver_data
, detail_data
, size
, ret_size
);
5007 if ((detail_data
|| size
) && size
< sizeof(SP_DRVINFO_DETAIL_DATA_A
))
5009 SetLastError(ERROR_INVALID_USER_BUFFER
);
5013 if ((hinf
= SetupOpenInfFileW(driver
->inf_path
, NULL
, INF_STYLE_WIN4
, NULL
)) == INVALID_HANDLE_VALUE
)
5016 SetupFindFirstLineW(hinf
, driver
->mfg_key
, driver
->description
, &ctx
);
5017 for (i
= 2; SetupGetStringFieldA(&ctx
, i
, id
, ARRAY_SIZE(id
), NULL
); ++i
)
5018 id_size
+= strlen(id
) + 1;
5020 size_needed
= FIELD_OFFSET(SP_DRVINFO_DETAIL_DATA_A
, HardwareID
[id_size
]);
5022 *ret_size
= size_needed
;
5025 SetupCloseInfFile(hinf
);
5029 detail_data
->CompatIDsLength
= detail_data
->CompatIDsOffset
= 0;
5030 detail_data
->HardwareID
[0] = 0;
5032 if (size
>= size_needed
)
5035 for (i
= 2; SetupGetStringFieldA(&ctx
, i
, id
, ARRAY_SIZE(id
), NULL
); ++i
)
5037 strcpy(&detail_data
->HardwareID
[id_size
], id
);
5039 detail_data
->CompatIDsOffset
= id_size
;
5040 id_size
+= strlen(id
) + 1;
5042 detail_data
->HardwareID
[id_size
++] = 0;
5044 detail_data
->CompatIDsLength
= id_size
- detail_data
->CompatIDsOffset
;
5047 SetupCloseInfFile(hinf
);
5049 if ((file
= CreateFileW(driver
->inf_path
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
)) == INVALID_HANDLE_VALUE
)
5051 GetFileTime(file
, NULL
, NULL
, &detail_data
->InfDate
);
5054 WideCharToMultiByte(CP_ACP
, 0, driver
->section
, -1, detail_data
->SectionName
,
5055 sizeof(detail_data
->SectionName
), NULL
, NULL
);
5056 WideCharToMultiByte(CP_ACP
, 0, driver
->inf_path
, -1, detail_data
->InfFileName
,
5057 sizeof(detail_data
->InfFileName
), NULL
, NULL
);
5058 WideCharToMultiByte(CP_ACP
, 0, driver
->description
, -1, detail_data
->DrvDescription
,
5059 sizeof(detail_data
->InfFileName
), NULL
, NULL
);
5061 if (size
< size_needed
)
5063 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
5070 /***********************************************************************
5071 * SetupDiInstallDriverFiles (SETUPAPI.@)
5073 BOOL WINAPI
SetupDiInstallDriverFiles(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
5075 WCHAR section
[LINE_LEN
], section_ext
[LINE_LEN
], iface_section
[LINE_LEN
];
5076 struct device
*device
;
5077 struct driver
*driver
;
5082 TRACE("devinfo %p, device_data %p.\n", devinfo
, device_data
);
5084 if (!(device
= get_device(devinfo
, device_data
)))
5087 if (!(driver
= device
->selected_driver
))
5089 ERR("No driver selected for device %p.\n", devinfo
);
5090 SetLastError(ERROR_NO_DRIVER_SELECTED
);
5094 if ((hinf
= SetupOpenInfFileW(driver
->inf_path
, NULL
, INF_STYLE_WIN4
, NULL
)) == INVALID_HANDLE_VALUE
)
5097 SetupFindFirstLineW(hinf
, driver
->mfg_key
, driver
->description
, &ctx
);
5098 SetupGetStringFieldW(&ctx
, 1, section
, ARRAY_SIZE(section
), NULL
);
5099 SetupDiGetActualSectionToInstallW(hinf
, section
, section_ext
, ARRAY_SIZE(section_ext
), NULL
, NULL
);
5101 callback_ctx
= SetupInitDefaultQueueCallback(NULL
);
5103 SetupInstallFromInfSectionW(NULL
, hinf
, section_ext
, SPINST_FILES
, NULL
, NULL
,
5104 SP_COPY_NEWER_ONLY
, SetupDefaultQueueCallbackW
, callback_ctx
, NULL
, NULL
);
5106 lstrcatW(section_ext
, dotInterfaces
);
5107 if (SetupFindFirstLineW(hinf
, section_ext
, AddInterface
, &ctx
))
5110 SetupGetStringFieldW(&ctx
, 3, iface_section
, ARRAY_SIZE(iface_section
), NULL
);
5111 SetupInstallFromInfSectionW(NULL
, hinf
, iface_section
, SPINST_FILES
, NULL
, NULL
,
5112 SP_COPY_NEWER_ONLY
, SetupDefaultQueueCallbackW
, callback_ctx
, NULL
, NULL
);
5113 } while (SetupFindNextMatchLineW(&ctx
, AddInterface
, &ctx
));
5116 SetupTermDefaultQueueCallback(callback_ctx
);
5118 SetupCloseInfFile(hinf
);
5122 /***********************************************************************
5123 * SetupDiInstallDevice (SETUPAPI.@)
5125 BOOL WINAPI
SetupDiInstallDevice(HDEVINFO devinfo
, SP_DEVINFO_DATA
*device_data
)
5127 static const WCHAR infpathW
[] = {'I','n','f','P','a','t','h',0};
5128 static const WCHAR infsectionW
[] = {'I','n','f','S','e','c','t','i','o','n',0};
5129 static const WCHAR infsectionextW
[] = {'I','n','f','S','e','c','t','i','o','n','E','x','t',0};
5130 static const WCHAR dothwW
[] = {'.','H','W',0};
5131 static const WCHAR dotservicesW
[] = {'.','S','e','r','v','i','c','e','s',0};
5132 static const WCHAR addserviceW
[] = {'A','d','d','S','e','r','v','i','c','e',0};
5133 static const WCHAR rootW
[] = {'r','o','o','t','\\',0};
5134 WCHAR section_ext
[LINE_LEN
], subsection
[LINE_LEN
], inf_path
[MAX_PATH
], *extptr
, *filepart
;
5135 UINT install_flags
= SPINST_ALL
;
5136 HKEY driver_key
, device_key
;
5137 SC_HANDLE manager
, service
;
5138 WCHAR svc_name
[LINE_LEN
];
5139 struct device
*device
;
5140 struct driver
*driver
;
5146 TRACE("devinfo %p, device_data %p.\n", devinfo
, device_data
);
5148 if (!(device
= get_device(devinfo
, device_data
)))
5151 if (!(driver
= device
->selected_driver
))
5153 ERR("No driver selected for device %p.\n", devinfo
);
5154 SetLastError(ERROR_NO_DRIVER_SELECTED
);
5158 if ((hinf
= SetupOpenInfFileW(driver
->inf_path
, NULL
, INF_STYLE_WIN4
, NULL
)) == INVALID_HANDLE_VALUE
)
5161 RegSetValueExW(device
->key
, L
"DeviceDesc", 0, REG_SZ
, (BYTE
*)driver
->description
,
5162 wcslen(driver
->description
) * sizeof(WCHAR
));
5164 SetupDiGetActualSectionToInstallW(hinf
, driver
->section
, section_ext
, ARRAY_SIZE(section_ext
), NULL
, &extptr
);
5166 if ((l
= create_driver_key(device
, &driver_key
)))
5169 SetupCloseInfFile(hinf
);
5173 if ((l
= RegCreateKeyExW(device
->key
, DeviceParameters
, 0, NULL
, 0,
5174 KEY_READ
| KEY_WRITE
, NULL
, &device_key
, NULL
)))
5177 RegCloseKey(driver_key
);
5178 SetupCloseInfFile(hinf
);
5182 if (device
->params
.Flags
& DI_NOFILECOPY
)
5183 install_flags
&= ~SPINST_FILES
;
5185 callback_ctx
= SetupInitDefaultQueueCallback(NULL
);
5187 SetupInstallFromInfSectionW(NULL
, hinf
, section_ext
, install_flags
, driver_key
, NULL
,
5188 SP_COPY_NEWER_ONLY
, SetupDefaultQueueCallbackW
, callback_ctx
, NULL
, NULL
);
5190 lstrcpyW(subsection
, section_ext
);
5191 lstrcatW(subsection
, dothwW
);
5193 SetupInstallFromInfSectionW(NULL
, hinf
, subsection
, install_flags
, device_key
, NULL
,
5194 SP_COPY_NEWER_ONLY
, SetupDefaultQueueCallbackW
, callback_ctx
, NULL
, NULL
);
5196 lstrcpyW(subsection
, section_ext
);
5197 lstrcatW(subsection
, dotservicesW
);
5198 SetupInstallServicesFromInfSectionW(hinf
, subsection
, 0);
5201 if (SetupFindFirstLineW(hinf
, subsection
, addserviceW
, &ctx
))
5207 if (SetupGetIntField(&ctx
, 2, &flags
) && (flags
& SPSVCINST_ASSOCSERVICE
))
5209 if (SetupGetStringFieldW(&ctx
, 1, svc_name
, ARRAY_SIZE(svc_name
), NULL
) && svc_name
[0])
5210 RegSetValueExW(device
->key
, Service
, 0, REG_SZ
, (BYTE
*)svc_name
, lstrlenW(svc_name
) * sizeof(WCHAR
));
5213 } while (SetupFindNextMatchLineW(&ctx
, addserviceW
, &ctx
));
5216 SetupTermDefaultQueueCallback(callback_ctx
);
5217 SetupCloseInfFile(hinf
);
5219 SetupCopyOEMInfW(driver
->inf_path
, NULL
, SPOST_NONE
, 0, inf_path
, ARRAY_SIZE(inf_path
), NULL
, &filepart
);
5220 TRACE("Copied INF file %s to %s.\n", debugstr_w(driver
->inf_path
), debugstr_w(inf_path
));
5222 RegSetValueExW(driver_key
, infpathW
, 0, REG_SZ
, (BYTE
*)filepart
, lstrlenW(filepart
) * sizeof(WCHAR
));
5223 RegSetValueExW(driver_key
, infsectionW
, 0, REG_SZ
, (BYTE
*)driver
->section
, lstrlenW(driver
->section
) * sizeof(WCHAR
));
5225 RegSetValueExW(driver_key
, infsectionextW
, 0, REG_SZ
, (BYTE
*)extptr
, lstrlenW(extptr
) * sizeof(WCHAR
));
5227 RegCloseKey(device_key
);
5228 RegCloseKey(driver_key
);
5230 if (!wcsnicmp(device
->instanceId
, rootW
, lstrlenW(rootW
)) && svc_name
[0]
5231 && (manager
= OpenSCManagerW(NULL
, NULL
, SC_MANAGER_CONNECT
)))
5233 if ((service
= OpenServiceW(manager
, svc_name
, SERVICE_START
| SERVICE_USER_DEFINED_CONTROL
)))
5235 SERVICE_STATUS status
;
5237 if (!StartServiceW(service
, 0, NULL
) && GetLastError() != ERROR_SERVICE_ALREADY_RUNNING
)
5239 ERR("Failed to start service %s for device %s, error %u.\n",
5240 debugstr_w(svc_name
), debugstr_w(device
->instanceId
), GetLastError());
5242 if (!ControlService(service
, SERVICE_CONTROL_REENUMERATE_ROOT_DEVICES
, &status
))
5244 ERR("Failed to control service %s for device %s, error %u.\n",
5245 debugstr_w(svc_name
), debugstr_w(device
->instanceId
), GetLastError());
5247 CloseServiceHandle(service
);
5250 ERR("Failed to open service %s for device %s.\n", debugstr_w(svc_name
), debugstr_w(device
->instanceId
));
5251 CloseServiceHandle(manager
);