Conform better to Wine standards
[reactos.git] / reactos / dll / win32 / setupapi / devinst.c
1 /*
2 * SetupAPI device installer
3 *
4 * Copyright 2000 Andreas Mohr for CodeWeavers
5 * 2005-2006 Hervé Poussineau (hpoussin@reactos.org)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "setupapi_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
25
26 /* Unicode constants */
27 static const WCHAR AddInterface[] = {'A','d','d','I','n','t','e','r','f','a','c','e',0};
28 static const WCHAR ClassGUID[] = {'C','l','a','s','s','G','U','I','D',0};
29 static const WCHAR Class[] = {'C','l','a','s','s',0};
30 static const WCHAR ClassInstall32[] = {'C','l','a','s','s','I','n','s','t','a','l','l','3','2',0};
31 static const WCHAR Control[] = {'C','o','n','t','r','o','l',0};
32 static const WCHAR DeviceInstance[] = {'D','e','v','i','c','e','I','n','s','t','a','n','c','e',0};
33 static const WCHAR DotCoInstallers[] = {'.','C','o','I','n','s','t','a','l','l','e','r','s',0};
34 static const WCHAR DotHW[] = {'.','H','W',0};
35 static const WCHAR DotInterfaces[] = {'.','I','n','t','e','r','f','a','c','e','s',0};
36 static const WCHAR DotServices[] = {'.','S','e','r','v','i','c','e','s',0};
37 static const WCHAR InterfaceInstall32[] = {'I','n','t','e','r','f','a','c','e','I','n','s','t','a','l','l','3','2',0};
38 static const WCHAR Linked[] = {'L','i','n','k','e','d',0};
39 static const WCHAR SymbolicLink[] = {'S','y','m','b','o','l','i','c','L','i','n','k',0};
40 static const WCHAR Version[] = {'V','e','r','s','i','o','n',0};
41
42 static const WCHAR REGSTR_DRIVER_DATE[] = {'D','r','i','v','e','r','D','a','t','e',0};
43 static const WCHAR REGSTR_DRIVER_DATE_DATA[] = {'D','r','i','v','e','r','D','a','t','e','D','a','t','a',0};
44 static const WCHAR REGSTR_DRIVER_VERSION[] = {'D','r','i','v','e','r','V','e','r','s','i','o','n',0};
45 static const WCHAR REGSTR_SECURITY[] = {'S','e','c','u','r','i','t','y',0};
46 static const WCHAR REGSTR_UI_NUMBER_DESC_FORMAT[] = {'U','I','N','u','m','b','e','r','D','e','s','c','F','o','r','m','a','t',0};
47
48 static const WCHAR INF_MANUFACTURER[] = {'M','a','n','u','f','a','c','t','u','r','e','r',0};
49 static const WCHAR INF_PROVIDER[] = {'P','r','o','v','i','d','e','r',0};
50 static const WCHAR INF_DRIVER_VER[] = {'D','r','i','v','e','r','V','e','r',0};
51
52 typedef DWORD
53 (CALLBACK* CLASS_INSTALL_PROC) (
54 IN DI_FUNCTION InstallFunction,
55 IN HDEVINFO DeviceInfoSet,
56 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL);
57 typedef BOOL
58 (WINAPI* DEFAULT_CLASS_INSTALL_PROC) (
59 IN HDEVINFO DeviceInfoSet,
60 IN OUT PSP_DEVINFO_DATA DeviceInfoData);
61 typedef DWORD
62 (CALLBACK* COINSTALLER_PROC) (
63 IN DI_FUNCTION InstallFunction,
64 IN HDEVINFO DeviceInfoSet,
65 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
66 IN OUT PCOINSTALLER_CONTEXT_DATA Context);
67 typedef BOOL
68 (WINAPI* PROPERTY_PAGE_PROVIDER) (
69 IN PSP_PROPSHEETPAGE_REQUEST PropPageRequest,
70 IN LPFNADDPROPSHEETPAGE fAddFunc,
71 IN LPARAM lParam);
72 typedef BOOL
73 (*UPDATE_CLASS_PARAM_HANDLER) (
74 IN HDEVINFO DeviceInfoSet,
75 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
76 IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
77 IN DWORD ClassInstallParamsSize);
78
79 struct CoInstallerElement
80 {
81 LIST_ENTRY ListEntry;
82
83 HMODULE Module;
84 COINSTALLER_PROC Function;
85 BOOL DoPostProcessing;
86 PVOID PrivateData;
87 };
88
89 static BOOL
90 PropertyChangeHandler(
91 IN HDEVINFO DeviceInfoSet,
92 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
93 IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
94 IN DWORD ClassInstallParamsSize);
95
96 static UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
97 NULL, /* DIF_SELECTDEVICE */
98 NULL, /* DIF_INSTALLDEVICE */
99 NULL, /* DIF_ASSIGNRESOURCES */
100 NULL, /* DIF_PROPERTIES */
101 NULL, /* DIF_REMOVE */
102 NULL, /* DIF_FIRSTTIMESETUP */
103 NULL, /* DIF_FOUNDDEVICE */
104 NULL, /* DIF_SELECTCLASSDRIVERS */
105 NULL, /* DIF_VALIDATECLASSDRIVERS */
106 NULL, /* DIF_INSTALLCLASSDRIVERS */
107 NULL, /* DIF_CALCDISKSPACE */
108 NULL, /* DIF_DESTROYPRIVATEDATA */
109 NULL, /* DIF_VALIDATEDRIVER */
110 NULL, /* DIF_MOVEDEVICE */
111 NULL, /* DIF_DETECT */
112 NULL, /* DIF_INSTALLWIZARD */
113 NULL, /* DIF_DESTROYWIZARDDATA */
114 PropertyChangeHandler, /* DIF_PROPERTYCHANGE */
115 NULL, /* DIF_ENABLECLASS */
116 NULL, /* DIF_DETECTVERIFY */
117 NULL, /* DIF_INSTALLDEVICEFILES */
118 NULL, /* DIF_UNREMOVE */
119 NULL, /* DIF_SELECTBESTCOMPATDRV */
120 NULL, /* DIF_ALLOW_INSTALL */
121 NULL, /* DIF_REGISTERDEVICE */
122 NULL, /* DIF_NEWDEVICEWIZARD_PRESELECT */
123 NULL, /* DIF_NEWDEVICEWIZARD_SELECT */
124 NULL, /* DIF_NEWDEVICEWIZARD_PREANALYZE */
125 NULL, /* DIF_NEWDEVICEWIZARD_POSTANALYZE */
126 NULL, /* DIF_NEWDEVICEWIZARD_FINISHINSTALL */
127 NULL, /* DIF_UNUSED1 */
128 NULL, /* DIF_INSTALLINTERFACES */
129 NULL, /* DIF_DETECTCANCEL */
130 NULL, /* DIF_REGISTER_COINSTALLERS */
131 NULL, /* DIF_ADDPROPERTYPAGE_ADVANCED */
132 NULL, /* DIF_ADDPROPERTYPAGE_BASIC */
133 NULL, /* DIF_RESERVED1 */
134 NULL, /* DIF_TROUBLESHOOTER */
135 NULL, /* DIF_POWERMESSAGEWAKE */
136 NULL, /* DIF_ADDREMOTEPROPERTYPAGE_ADVANCED */
137 NULL, /* DIF_UPDATEDRIVER_UI */
138 NULL /* DIF_RESERVED2 */
139 };
140
141 /***********************************************************************
142 * SetupDiBuildClassInfoList (SETUPAPI.@)
143 */
144 BOOL WINAPI SetupDiBuildClassInfoList(
145 DWORD Flags,
146 LPGUID ClassGuidList,
147 DWORD ClassGuidListSize,
148 PDWORD RequiredSize)
149 {
150 TRACE("\n");
151 return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
152 ClassGuidListSize, RequiredSize,
153 NULL, NULL);
154 }
155
156 /***********************************************************************
157 * SetupDiBuildClassInfoListExA (SETUPAPI.@)
158 */
159 BOOL WINAPI SetupDiBuildClassInfoListExA(
160 DWORD Flags,
161 LPGUID ClassGuidList,
162 DWORD ClassGuidListSize,
163 PDWORD RequiredSize,
164 LPCSTR MachineName,
165 PVOID Reserved)
166 {
167 LPWSTR MachineNameW = NULL;
168 BOOL bResult;
169
170 TRACE("\n");
171
172 if (MachineName)
173 {
174 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
175 if (MachineNameW == NULL) return FALSE;
176 }
177
178 bResult = SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
179 ClassGuidListSize, RequiredSize,
180 MachineNameW, Reserved);
181
182 if (MachineNameW)
183 MyFree(MachineNameW);
184
185 return bResult;
186 }
187
188 /***********************************************************************
189 * SetupDiBuildClassInfoListExW (SETUPAPI.@)
190 */
191 BOOL WINAPI SetupDiBuildClassInfoListExW(
192 DWORD Flags,
193 LPGUID ClassGuidList,
194 DWORD ClassGuidListSize,
195 PDWORD RequiredSize,
196 LPCWSTR MachineName,
197 PVOID Reserved)
198 {
199 WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
200 HKEY hClassesKey;
201 HKEY hClassKey;
202 DWORD dwLength;
203 DWORD dwIndex;
204 LONG lError;
205 DWORD dwGuidListIndex = 0;
206
207 TRACE("0x%lx %p %lu %p %s %p\n", Flags, ClassGuidList,
208 ClassGuidListSize, RequiredSize, debugstr_w(MachineName), Reserved);
209
210 if (RequiredSize != NULL)
211 *RequiredSize = 0;
212
213 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
214 KEY_ENUMERATE_SUB_KEYS,
215 DIOCR_INSTALLER,
216 MachineName,
217 Reserved);
218 if (hClassesKey == INVALID_HANDLE_VALUE)
219 {
220 return FALSE;
221 }
222
223 for (dwIndex = 0; ; dwIndex++)
224 {
225 dwLength = MAX_GUID_STRING_LEN + 1;
226 lError = RegEnumKeyExW(hClassesKey,
227 dwIndex,
228 szKeyName,
229 &dwLength,
230 NULL,
231 NULL,
232 NULL,
233 NULL);
234 TRACE("RegEnumKeyExW() returns %ld\n", lError);
235 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
236 {
237 TRACE("Key name: %s\n", debugstr_w(szKeyName));
238
239 if (RegOpenKeyExW(hClassesKey,
240 szKeyName,
241 0,
242 KEY_QUERY_VALUE,
243 &hClassKey))
244 {
245 RegCloseKey(hClassesKey);
246 return FALSE;
247 }
248
249 if (!RegQueryValueExW(hClassKey,
250 REGSTR_VAL_NOUSECLASS,
251 NULL,
252 NULL,
253 NULL,
254 NULL))
255 {
256 TRACE("'NoUseClass' value found!\n");
257 RegCloseKey(hClassKey);
258 continue;
259 }
260
261 if ((Flags & DIBCI_NOINSTALLCLASS) &&
262 (!RegQueryValueExW(hClassKey,
263 REGSTR_VAL_NOINSTALLCLASS,
264 NULL,
265 NULL,
266 NULL,
267 NULL)))
268 {
269 TRACE("'NoInstallClass' value found!\n");
270 RegCloseKey(hClassKey);
271 continue;
272 }
273
274 if ((Flags & DIBCI_NODISPLAYCLASS) &&
275 (!RegQueryValueExW(hClassKey,
276 REGSTR_VAL_NODISPLAYCLASS,
277 NULL,
278 NULL,
279 NULL,
280 NULL)))
281 {
282 TRACE("'NoDisplayClass' value found!\n");
283 RegCloseKey(hClassKey);
284 continue;
285 }
286
287 RegCloseKey(hClassKey);
288
289 TRACE("Guid: %s\n", debugstr_w(szKeyName));
290 if (dwGuidListIndex < ClassGuidListSize)
291 {
292 if (szKeyName[0] == L'{' && szKeyName[37] == L'}')
293 {
294 szKeyName[37] = 0;
295 }
296 TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
297
298 UuidFromStringW(&szKeyName[1],
299 &ClassGuidList[dwGuidListIndex]);
300 }
301
302 dwGuidListIndex++;
303 }
304
305 if (lError != ERROR_SUCCESS)
306 break;
307 }
308
309 RegCloseKey(hClassesKey);
310
311 if (RequiredSize != NULL)
312 *RequiredSize = dwGuidListIndex;
313
314 if (ClassGuidListSize < dwGuidListIndex)
315 {
316 SetLastError(ERROR_INSUFFICIENT_BUFFER);
317 return FALSE;
318 }
319
320 return TRUE;
321 }
322
323 /***********************************************************************
324 * SetupDiClassGuidsFromNameA (SETUPAPI.@)
325 */
326 BOOL WINAPI SetupDiClassGuidsFromNameA(
327 LPCSTR ClassName,
328 LPGUID ClassGuidList,
329 DWORD ClassGuidListSize,
330 PDWORD RequiredSize)
331 {
332 return SetupDiClassGuidsFromNameExA(ClassName, ClassGuidList,
333 ClassGuidListSize, RequiredSize,
334 NULL, NULL);
335 }
336
337 /***********************************************************************
338 * SetupDiClassGuidsFromNameW (SETUPAPI.@)
339 */
340 BOOL WINAPI SetupDiClassGuidsFromNameW(
341 LPCWSTR ClassName,
342 LPGUID ClassGuidList,
343 DWORD ClassGuidListSize,
344 PDWORD RequiredSize)
345 {
346 return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
347 ClassGuidListSize, RequiredSize,
348 NULL, NULL);
349 }
350
351 /***********************************************************************
352 * SetupDiClassGuidsFromNameExA (SETUPAPI.@)
353 */
354 BOOL WINAPI SetupDiClassGuidsFromNameExA(
355 LPCSTR ClassName,
356 LPGUID ClassGuidList,
357 DWORD ClassGuidListSize,
358 PDWORD RequiredSize,
359 LPCSTR MachineName,
360 PVOID Reserved)
361 {
362 LPWSTR ClassNameW = NULL;
363 LPWSTR MachineNameW = NULL;
364 BOOL bResult;
365
366 TRACE("\n");
367
368 ClassNameW = MultiByteToUnicode(ClassName, CP_ACP);
369 if (ClassNameW == NULL)
370 return FALSE;
371
372 if (MachineNameW)
373 {
374 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
375 if (MachineNameW == NULL)
376 {
377 MyFree(ClassNameW);
378 return FALSE;
379 }
380 }
381
382 bResult = SetupDiClassGuidsFromNameExW(ClassNameW, ClassGuidList,
383 ClassGuidListSize, RequiredSize,
384 MachineNameW, Reserved);
385
386 if (MachineNameW)
387 MyFree(MachineNameW);
388
389 MyFree(ClassNameW);
390
391 return bResult;
392 }
393
394 /***********************************************************************
395 * SetupDiClassGuidsFromNameExW (SETUPAPI.@)
396 */
397 BOOL WINAPI SetupDiClassGuidsFromNameExW(
398 LPCWSTR ClassName,
399 LPGUID ClassGuidList,
400 DWORD ClassGuidListSize,
401 PDWORD RequiredSize,
402 LPCWSTR MachineName,
403 PVOID Reserved)
404 {
405 WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
406 WCHAR szClassName[256];
407 HKEY hClassesKey;
408 HKEY hClassKey;
409 DWORD dwLength;
410 DWORD dwIndex;
411 LONG lError;
412 DWORD dwGuidListIndex = 0;
413
414 TRACE("%s %p %lu %p %s %p\n", debugstr_w(ClassName), ClassGuidList,
415 ClassGuidListSize, RequiredSize, debugstr_w(MachineName), Reserved);
416
417 if (RequiredSize != NULL)
418 *RequiredSize = 0;
419
420 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
421 KEY_ENUMERATE_SUB_KEYS,
422 DIOCR_INSTALLER,
423 MachineName,
424 Reserved);
425 if (hClassesKey == INVALID_HANDLE_VALUE)
426 {
427 return FALSE;
428 }
429
430 for (dwIndex = 0; ; dwIndex++)
431 {
432 dwLength = MAX_GUID_STRING_LEN + 1;
433 lError = RegEnumKeyExW(hClassesKey,
434 dwIndex,
435 szKeyName,
436 &dwLength,
437 NULL,
438 NULL,
439 NULL,
440 NULL);
441 TRACE("RegEnumKeyExW() returns %ld\n", lError);
442 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
443 {
444 TRACE("Key name: %s\n", debugstr_w(szKeyName));
445
446 if (RegOpenKeyExW(hClassesKey,
447 szKeyName,
448 0,
449 KEY_QUERY_VALUE,
450 &hClassKey))
451 {
452 RegCloseKey(hClassesKey);
453 return FALSE;
454 }
455
456 dwLength = 256 * sizeof(WCHAR);
457 if (!RegQueryValueExW(hClassKey,
458 Class,
459 NULL,
460 NULL,
461 (LPBYTE)szClassName,
462 &dwLength))
463 {
464 TRACE("Class name: %s\n", debugstr_w(szClassName));
465
466 if (strcmpiW(szClassName, ClassName) == 0)
467 {
468 TRACE("Found matching class name\n");
469
470 TRACE("Guid: %s\n", debugstr_w(szKeyName));
471 if (dwGuidListIndex < ClassGuidListSize)
472 {
473 if (szKeyName[0] == L'{' && szKeyName[37] == L'}')
474 {
475 szKeyName[37] = 0;
476 }
477 TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
478
479 UuidFromStringW(&szKeyName[1],
480 &ClassGuidList[dwGuidListIndex]);
481 }
482
483 dwGuidListIndex++;
484 }
485 }
486
487 RegCloseKey(hClassKey);
488 }
489
490 if (lError != ERROR_SUCCESS)
491 break;
492 }
493
494 RegCloseKey(hClassesKey);
495
496 if (RequiredSize != NULL)
497 *RequiredSize = dwGuidListIndex;
498
499 if (ClassGuidListSize < dwGuidListIndex)
500 {
501 SetLastError(ERROR_INSUFFICIENT_BUFFER);
502 return FALSE;
503 }
504
505 return TRUE;
506 }
507
508 /***********************************************************************
509 * SetupDiClassNameFromGuidA (SETUPAPI.@)
510 */
511 BOOL WINAPI SetupDiClassNameFromGuidA(
512 const GUID* ClassGuid,
513 PSTR ClassName,
514 DWORD ClassNameSize,
515 PDWORD RequiredSize)
516 {
517 return SetupDiClassNameFromGuidExA(ClassGuid, ClassName,
518 ClassNameSize, RequiredSize,
519 NULL, NULL);
520 }
521
522 /***********************************************************************
523 * SetupDiClassNameFromGuidW (SETUPAPI.@)
524 */
525 BOOL WINAPI SetupDiClassNameFromGuidW(
526 const GUID* ClassGuid,
527 PWSTR ClassName,
528 DWORD ClassNameSize,
529 PDWORD RequiredSize)
530 {
531 return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
532 ClassNameSize, RequiredSize,
533 NULL, NULL);
534 }
535
536 /***********************************************************************
537 * SetupDiClassNameFromGuidExA (SETUPAPI.@)
538 */
539 BOOL WINAPI SetupDiClassNameFromGuidExA(
540 const GUID* ClassGuid,
541 PSTR ClassName,
542 DWORD ClassNameSize,
543 PDWORD RequiredSize,
544 PCSTR MachineName,
545 PVOID Reserved)
546 {
547 WCHAR ClassNameW[MAX_CLASS_NAME_LEN];
548 LPWSTR MachineNameW = NULL;
549 BOOL ret;
550
551 if (MachineName)
552 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
553 ret = SetupDiClassNameFromGuidExW(ClassGuid, ClassNameW, MAX_CLASS_NAME_LEN,
554 NULL, MachineNameW, Reserved);
555 if (ret)
556 {
557 int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
558 ClassNameSize, NULL, NULL);
559
560 if (!ClassNameSize && RequiredSize)
561 *RequiredSize = len;
562 }
563 MyFree(MachineNameW);
564 return ret;
565 }
566
567 /***********************************************************************
568 * SetupDiClassNameFromGuidExW (SETUPAPI.@)
569 */
570 BOOL WINAPI SetupDiClassNameFromGuidExW(
571 const GUID* ClassGuid,
572 PWSTR ClassName,
573 DWORD ClassNameSize,
574 PDWORD RequiredSize,
575 PCWSTR MachineName,
576 PVOID Reserved)
577 {
578 HKEY hKey;
579 DWORD dwLength;
580 LONG rc;
581
582 TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassName,
583 ClassNameSize, RequiredSize, debugstr_w(MachineName), Reserved);
584
585 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
586 KEY_QUERY_VALUE,
587 DIOCR_INSTALLER,
588 MachineName,
589 Reserved);
590 if (hKey == INVALID_HANDLE_VALUE)
591 {
592 return FALSE;
593 }
594
595 if (RequiredSize != NULL)
596 {
597 dwLength = 0;
598 rc = RegQueryValueExW(hKey,
599 Class,
600 NULL,
601 NULL,
602 NULL,
603 &dwLength);
604 if (rc != ERROR_SUCCESS)
605 {
606 SetLastError(rc);
607 RegCloseKey(hKey);
608 return FALSE;
609 }
610
611 *RequiredSize = dwLength / sizeof(WCHAR);
612 }
613
614 dwLength = ClassNameSize * sizeof(WCHAR);
615 rc = RegQueryValueExW(hKey,
616 Class,
617 NULL,
618 NULL,
619 (LPBYTE)ClassName,
620 &dwLength);
621 if (rc != ERROR_SUCCESS)
622 {
623 SetLastError(rc);
624 RegCloseKey(hKey);
625 return FALSE;
626 }
627
628 RegCloseKey(hKey);
629
630 return TRUE;
631 }
632
633 /***********************************************************************
634 * SetupDiCreateDeviceInfoList (SETUPAPI.@)
635 */
636 HDEVINFO WINAPI
637 SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
638 HWND hwndParent)
639 {
640 return SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent, NULL, NULL);
641 }
642
643 /***********************************************************************
644 * SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
645 */
646 HDEVINFO WINAPI
647 SetupDiCreateDeviceInfoListExA(const GUID *ClassGuid,
648 HWND hwndParent,
649 PCSTR MachineName,
650 PVOID Reserved)
651 {
652 LPWSTR MachineNameW = NULL;
653 HDEVINFO hDevInfo;
654
655 TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid), hwndParent,
656 debugstr_a(MachineName), Reserved);
657
658 if (MachineName)
659 {
660 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
661 if (MachineNameW == NULL)
662 return (HDEVINFO)INVALID_HANDLE_VALUE;
663 }
664
665 hDevInfo = SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent,
666 MachineNameW, Reserved);
667
668 if (MachineNameW)
669 MyFree(MachineNameW);
670
671 return hDevInfo;
672 }
673
674 static DWORD
675 GetErrorCodeFromCrCode(const IN CONFIGRET cr)
676 {
677 switch (cr)
678 {
679 case CR_INVALID_MACHINENAME: return ERROR_INVALID_COMPUTERNAME;
680 case CR_OUT_OF_MEMORY: return ERROR_NOT_ENOUGH_MEMORY;
681 case CR_SUCCESS: return ERROR_SUCCESS;
682 default:
683 /* FIXME */
684 return ERROR_GEN_FAILURE;
685 }
686
687 /* Does not happen */
688 }
689
690 /***********************************************************************
691 * SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
692 */
693 HDEVINFO WINAPI
694 SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
695 HWND hwndParent,
696 PCWSTR MachineName,
697 PVOID Reserved)
698 {
699 struct DeviceInfoSet *list;
700 LPWSTR UNCServerName = NULL;
701 DWORD size;
702 DWORD rc;
703 //CONFIGRET cr;
704 HDEVINFO ret = (HDEVINFO)INVALID_HANDLE_VALUE;;
705
706 TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid), hwndParent,
707 debugstr_w(MachineName), Reserved);
708
709 size = FIELD_OFFSET(struct DeviceInfoSet, szData);
710 if (MachineName)
711 size += (strlenW(MachineName) + 3) * sizeof(WCHAR);
712 list = HeapAlloc(GetProcessHeap(), 0, size);
713 if (!list)
714 {
715 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
716 goto cleanup;
717 }
718 memset(list, 0, sizeof(struct DeviceInfoSet));
719
720 list->magic = SETUP_DEV_INFO_SET_MAGIC;
721 memcpy(
722 &list->ClassGuid,
723 ClassGuid ? ClassGuid : &GUID_NULL,
724 sizeof(list->ClassGuid));
725 list->InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
726 list->InstallParams.Flags |= DI_CLASSINSTALLPARAMS;
727 list->InstallParams.hwndParent = hwndParent;
728 if (MachineName)
729 {
730 rc = RegConnectRegistryW(MachineName, HKEY_LOCAL_MACHINE, &list->HKLM);
731 if (rc != ERROR_SUCCESS)
732 {
733 SetLastError(rc);
734 goto cleanup;
735 }
736 UNCServerName = HeapAlloc(GetProcessHeap(), 0, (strlenW(MachineName) + 3) * sizeof(WCHAR));
737 if (!UNCServerName)
738 {
739 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
740 goto cleanup;
741 }
742
743 strcpyW(UNCServerName + 2, MachineName);
744 list->szData[0] = list->szData[1] = '\\';
745 strcpyW(list->szData + 2, MachineName);
746 list->MachineName = list->szData;
747 }
748 else
749 {
750 DWORD Size = MAX_PATH;
751 list->HKLM = HKEY_LOCAL_MACHINE;
752 UNCServerName = HeapAlloc(GetProcessHeap(), 0, (MAX_PATH + 2) * sizeof(WCHAR));
753 if (!UNCServerName)
754 {
755 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
756 goto cleanup;
757 }
758 if (!GetComputerNameW(UNCServerName + 2, &Size))
759 goto cleanup;
760 list->MachineName = NULL;
761 }
762 #if 0
763 UNCServerName[0] = UNCServerName[1] = '\\';
764 cr = CM_Connect_MachineW(UNCServerName, &list->hMachine);
765 if (cr != CR_SUCCESS)
766 {
767 SetLastError(GetErrorCodeFromCrCode(cr));
768 goto cleanup;
769 }
770 #endif
771 InitializeListHead(&list->DriverListHead);
772 InitializeListHead(&list->ListHead);
773
774 ret = (HDEVINFO)list;
775
776 cleanup:
777 if (ret == INVALID_HANDLE_VALUE)
778 {
779 if (list && list->HKLM != 0 && list->HKLM != HKEY_LOCAL_MACHINE)
780 RegCloseKey(list->HKLM);
781 HeapFree(GetProcessHeap(), 0, list);
782 }
783 HeapFree(GetProcessHeap(), 0, UNCServerName);
784 return ret;
785 }
786
787 /***********************************************************************
788 * SetupDiEnumDeviceInfo (SETUPAPI.@)
789 */
790 BOOL WINAPI SetupDiEnumDeviceInfo(
791 HDEVINFO DeviceInfoSet,
792 DWORD MemberIndex,
793 PSP_DEVINFO_DATA DeviceInfoData)
794 {
795 BOOL ret = FALSE;
796
797 TRACE("%p, 0x%08lx, %p\n", DeviceInfoSet, MemberIndex, DeviceInfoData);
798 if (!DeviceInfoData)
799 SetLastError(ERROR_INVALID_PARAMETER);
800 else if (DeviceInfoSet && DeviceInfoSet != (HDEVINFO)INVALID_HANDLE_VALUE)
801 {
802 struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
803
804 if (list->magic != SETUP_DEV_INFO_SET_MAGIC)
805 SetLastError(ERROR_INVALID_HANDLE);
806 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
807 SetLastError(ERROR_INVALID_USER_BUFFER);
808 else
809 {
810 PLIST_ENTRY ItemList = list->ListHead.Flink;
811 while (ItemList != &list->ListHead && MemberIndex-- > 0)
812 ItemList = ItemList->Flink;
813 if (ItemList == &list->ListHead)
814 SetLastError(ERROR_NO_MORE_ITEMS);
815 else
816 {
817 struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)ItemList;
818 memcpy(&DeviceInfoData->ClassGuid,
819 &DevInfo->ClassGuid,
820 sizeof(GUID));
821 DeviceInfoData->DevInst = DevInfo->dnDevInst;
822 DeviceInfoData->Reserved = (ULONG_PTR)DevInfo;
823 ret = TRUE;
824 }
825 }
826 }
827 else
828 SetLastError(ERROR_INVALID_HANDLE);
829 return ret;
830 }
831
832 /***********************************************************************
833 * SetupDiGetActualSectionToInstallA (SETUPAPI.@)
834 */
835 BOOL WINAPI
836 SetupDiGetActualSectionToInstallA(
837 IN HINF InfHandle,
838 IN PCSTR InfSectionName,
839 OUT PSTR InfSectionWithExt OPTIONAL,
840 IN DWORD InfSectionWithExtSize,
841 OUT PDWORD RequiredSize OPTIONAL,
842 OUT PSTR *Extension OPTIONAL)
843 {
844 return SetupDiGetActualSectionToInstallExA(InfHandle, InfSectionName,
845 NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
846 Extension, NULL);
847 }
848
849 /***********************************************************************
850 * SetupDiGetActualSectionToInstallW (SETUPAPI.@)
851 */
852 BOOL WINAPI
853 SetupDiGetActualSectionToInstallW(
854 IN HINF InfHandle,
855 IN PCWSTR InfSectionName,
856 OUT PWSTR InfSectionWithExt OPTIONAL,
857 IN DWORD InfSectionWithExtSize,
858 OUT PDWORD RequiredSize OPTIONAL,
859 OUT PWSTR *Extension OPTIONAL)
860 {
861 return SetupDiGetActualSectionToInstallExW(InfHandle, InfSectionName,
862 NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
863 Extension, NULL);
864 }
865
866 /***********************************************************************
867 * SetupDiGetActualSectionToInstallExA (SETUPAPI.@)
868 */
869 BOOL WINAPI
870 SetupDiGetActualSectionToInstallExA(
871 IN HINF InfHandle,
872 IN PCSTR InfSectionName,
873 IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
874 OUT PSTR InfSectionWithExt OPTIONAL,
875 IN DWORD InfSectionWithExtSize,
876 OUT PDWORD RequiredSize OPTIONAL,
877 OUT PSTR* Extension OPTIONAL,
878 IN PVOID Reserved)
879 {
880 LPWSTR InfSectionNameW = NULL;
881 LPWSTR InfSectionWithExtW = NULL;
882 PWSTR ExtensionW;
883 BOOL bResult = FALSE;
884
885 TRACE("\n");
886
887 if (InfSectionName)
888 {
889 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
890 if (InfSectionNameW == NULL)
891 goto cleanup;
892 }
893 if (InfSectionWithExt)
894 {
895 InfSectionWithExtW = MyMalloc(InfSectionWithExtSize * sizeof(WCHAR));
896 if (InfSectionWithExtW == NULL)
897 goto cleanup;
898 }
899
900 bResult = SetupDiGetActualSectionToInstallExW(
901 InfHandle, InfSectionNameW, AlternatePlatformInfo,
902 InfSectionWithExt ? InfSectionWithExtW : NULL,
903 InfSectionWithExtSize,
904 RequiredSize,
905 Extension ? &ExtensionW : NULL,
906 Reserved);
907
908 if (bResult && InfSectionWithExt)
909 {
910 bResult = WideCharToMultiByte(CP_ACP, 0, InfSectionWithExtW, -1, InfSectionWithExt,
911 InfSectionWithExtSize, NULL, NULL) != 0;
912 }
913 if (bResult && Extension)
914 {
915 if (ExtensionW == NULL)
916 *Extension = NULL;
917 else
918 *Extension = &InfSectionWithExt[ExtensionW - InfSectionWithExtW];
919 }
920
921 cleanup:
922 MyFree(InfSectionNameW);
923 MyFree(InfSectionWithExtW);
924
925 return bResult;
926 }
927
928 /***********************************************************************
929 * SetupDiGetActualSectionToInstallExW (SETUPAPI.@)
930 */
931 BOOL WINAPI
932 SetupDiGetActualSectionToInstallExW(
933 IN HINF InfHandle,
934 IN PCWSTR InfSectionName,
935 IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
936 OUT PWSTR InfSectionWithExt OPTIONAL,
937 IN DWORD InfSectionWithExtSize,
938 OUT PDWORD RequiredSize OPTIONAL,
939 OUT PWSTR* Extension OPTIONAL,
940 IN PVOID Reserved)
941 {
942 BOOL ret = FALSE;
943
944 TRACE("%p %s %p %p %lu %p %p %p\n", InfHandle, debugstr_w(InfSectionName),
945 AlternatePlatformInfo, InfSectionWithExt, InfSectionWithExtSize,
946 RequiredSize, Extension, Reserved);
947
948 if (!InfHandle || InfHandle == (HINF)INVALID_HANDLE_VALUE)
949 SetLastError(ERROR_INVALID_HANDLE);
950 else if (!InfSectionName)
951 SetLastError(ERROR_INVALID_PARAMETER);
952 else if (AlternatePlatformInfo && AlternatePlatformInfo->cbSize != sizeof(SP_ALTPLATFORM_INFO))
953 SetLastError(ERROR_INVALID_USER_BUFFER);
954 else if (Reserved != NULL)
955 SetLastError(ERROR_INVALID_PARAMETER);
956 else
957 {
958 static SP_ALTPLATFORM_INFO CurrentPlatform = { 0, };
959 PSP_ALTPLATFORM_INFO pPlatformInfo = &CurrentPlatform;
960 LPCWSTR pExtensionPlatform, pExtensionArchitecture;
961 WCHAR SectionName[LINE_LEN + 1];
962 LONG lLineCount = -1;
963 DWORD dwFullLength;
964
965 /* Fill platform info if needed */
966 if (AlternatePlatformInfo)
967 pPlatformInfo = AlternatePlatformInfo;
968 else if (CurrentPlatform.cbSize != sizeof(SP_ALTPLATFORM_INFO))
969 {
970 /* That's the first time we go here. We need to fill in the structure */
971 OSVERSIONINFO VersionInfo;
972 SYSTEM_INFO SystemInfo;
973 VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
974 ret = GetVersionEx(&VersionInfo);
975 if (!ret)
976 goto done;
977 GetSystemInfo(&SystemInfo);
978 CurrentPlatform.cbSize = sizeof(SP_ALTPLATFORM_INFO);
979 CurrentPlatform.Platform = VersionInfo.dwPlatformId;
980 CurrentPlatform.MajorVersion = VersionInfo.dwMajorVersion;
981 CurrentPlatform.MinorVersion = VersionInfo.dwMinorVersion;
982 CurrentPlatform.ProcessorArchitecture = SystemInfo.wProcessorArchitecture;
983 CurrentPlatform.Reserved = 0;
984 }
985
986 static const WCHAR ExtensionPlatformNone[] = {'.',0};
987 static const WCHAR ExtensionPlatformNT[] = {'.','N','T',0};
988 static const WCHAR ExtensionPlatformWindows[] = {'.','W','i','n',0};
989
990 static const WCHAR ExtensionArchitectureNone[] = {0};
991 static const WCHAR ExtensionArchitecturealpha[] = {'a','l','p','h','a',0};
992 static const WCHAR ExtensionArchitectureamd64[] = {'a','m','d','6','4',0};
993 static const WCHAR ExtensionArchitectureia64[] = {'i','a','6','4',0};
994 static const WCHAR ExtensionArchitecturemips[] = {'m','i','p','s',0};
995 static const WCHAR ExtensionArchitectureppc[] = {'p','p','c',0};
996 static const WCHAR ExtensionArchitecturex86[] = {'x','8','6',0};
997
998 /* Set various extensions values */
999 switch (pPlatformInfo->Platform)
1000 {
1001 case VER_PLATFORM_WIN32_WINDOWS:
1002 pExtensionPlatform = ExtensionPlatformWindows;
1003 break;
1004 case VER_PLATFORM_WIN32_NT:
1005 pExtensionPlatform = ExtensionPlatformNT;
1006 break;
1007 default:
1008 ERR("Unkown platform 0x%lx\n", pPlatformInfo->Platform);
1009 pExtensionPlatform = ExtensionPlatformNone;
1010 break;
1011 }
1012 switch (pPlatformInfo->ProcessorArchitecture)
1013 {
1014 case PROCESSOR_ARCHITECTURE_ALPHA:
1015 pExtensionArchitecture = ExtensionArchitecturealpha;
1016 break;
1017 case PROCESSOR_ARCHITECTURE_AMD64:
1018 pExtensionArchitecture = ExtensionArchitectureamd64;
1019 break;
1020 case PROCESSOR_ARCHITECTURE_IA64:
1021 pExtensionArchitecture = ExtensionArchitectureia64;
1022 break;
1023 case PROCESSOR_ARCHITECTURE_INTEL:
1024 pExtensionArchitecture = ExtensionArchitecturex86;
1025 break;
1026 case PROCESSOR_ARCHITECTURE_MIPS:
1027 pExtensionArchitecture = ExtensionArchitecturemips;
1028 break;
1029 case PROCESSOR_ARCHITECTURE_PPC:
1030 pExtensionArchitecture = ExtensionArchitectureppc;
1031 break;
1032 default:
1033 ERR("Unknown processor architecture 0x%x\n", pPlatformInfo->ProcessorArchitecture);
1034 case PROCESSOR_ARCHITECTURE_UNKNOWN:
1035 pExtensionArchitecture = ExtensionArchitectureNone;
1036 break;
1037 }
1038
1039 static const WCHAR FormatPlatformArchitectureMajorMinor[] = {'%','s','%','s','%','s','.','%','l','u','.','%','l','u',0};
1040 static const WCHAR FormatPlatformMajorMinor[] = {'%','s','%','s','.','%','l','u','.','%','l','u',0};
1041 static const WCHAR FormatPlatformArchitectureMajor[] = {'%','s','%','s','%','s','.','%','l','u',0};
1042 static const WCHAR FormatPlatformMajor[] = {'%','s','%','s','.','%','l','u',0};
1043 static const WCHAR FormatPlatformArchitecture[] = {'%','s','%','s','%','s',0};
1044 static const WCHAR FormatPlatform[] = {'%','s','%','s',0};
1045 static const WCHAR FormatNone[] = {'%','s',0};
1046
1047 SectionName[LINE_LEN] = UNICODE_NULL;
1048
1049 /* Test with platform.architecture.major.minor extension */
1050 snprintfW(SectionName, LINE_LEN, FormatPlatformArchitectureMajorMinor, InfSectionName,
1051 pExtensionPlatform, pExtensionArchitecture, pPlatformInfo->MajorVersion, pPlatformInfo->MinorVersion);
1052 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1053 if (lLineCount != -1) goto sectionfound;
1054
1055 /* Test with platform.major.minor extension */
1056 snprintfW(SectionName, LINE_LEN, FormatPlatformMajorMinor, InfSectionName,
1057 pExtensionPlatform, pPlatformInfo->MajorVersion, pPlatformInfo->MinorVersion);
1058 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1059 if (lLineCount != -1) goto sectionfound;
1060
1061 /* Test with platform.architecture.major extension */
1062 snprintfW(SectionName, LINE_LEN, FormatPlatformArchitectureMajor, InfSectionName,
1063 pExtensionPlatform, pExtensionArchitecture, pPlatformInfo->MajorVersion);
1064 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1065 if (lLineCount != -1) goto sectionfound;
1066
1067 /* Test with platform.major extension */
1068 snprintfW(SectionName, LINE_LEN, FormatPlatformMajor, InfSectionName,
1069 pExtensionPlatform, pPlatformInfo->MajorVersion);
1070 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1071 if (lLineCount != -1) goto sectionfound;
1072
1073 /* Test with platform.architecture extension */
1074 snprintfW(SectionName, LINE_LEN, FormatPlatformArchitecture, InfSectionName,
1075 pExtensionPlatform, pExtensionArchitecture);
1076 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1077 if (lLineCount != -1) goto sectionfound;
1078
1079 /* Test with platform extension */
1080 snprintfW(SectionName, LINE_LEN, FormatPlatform, InfSectionName,
1081 pExtensionPlatform);
1082 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1083 if (lLineCount != -1) goto sectionfound;
1084
1085 /* Test without extension */
1086 snprintfW(SectionName, LINE_LEN, FormatNone, InfSectionName);
1087 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1088 if (lLineCount != -1) goto sectionfound;
1089
1090 /* No appropriate section found */
1091 SetLastError(ERROR_INVALID_PARAMETER);
1092 goto done;
1093
1094 sectionfound:
1095 dwFullLength = lstrlenW(SectionName);
1096 if (InfSectionWithExt != NULL && InfSectionWithExtSize != 0)
1097 {
1098 if (InfSectionWithExtSize < (dwFullLength + 1))
1099 {
1100 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1101 goto done;
1102 }
1103
1104 lstrcpyW(InfSectionWithExt, SectionName);
1105 if (Extension != NULL)
1106 {
1107 DWORD dwLength = lstrlenW(SectionName);
1108 *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
1109 }
1110 }
1111
1112 if (RequiredSize != NULL)
1113 *RequiredSize = dwFullLength + 1;
1114
1115 ret = TRUE;
1116 }
1117
1118 done:
1119 TRACE("Returning %d\n", ret);
1120 return ret;
1121 }
1122
1123
1124 /***********************************************************************
1125 * SetupDiGetClassDescriptionA (SETUPAPI.@)
1126 */
1127 BOOL WINAPI SetupDiGetClassDescriptionA(
1128 const GUID* ClassGuid,
1129 PSTR ClassDescription,
1130 DWORD ClassDescriptionSize,
1131 PDWORD RequiredSize)
1132 {
1133 return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
1134 ClassDescriptionSize,
1135 RequiredSize, NULL, NULL);
1136 }
1137
1138 /***********************************************************************
1139 * SetupDiGetClassDescriptionW (SETUPAPI.@)
1140 */
1141 BOOL WINAPI SetupDiGetClassDescriptionW(
1142 const GUID* ClassGuid,
1143 PWSTR ClassDescription,
1144 DWORD ClassDescriptionSize,
1145 PDWORD RequiredSize)
1146 {
1147 return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
1148 ClassDescriptionSize,
1149 RequiredSize, NULL, NULL);
1150 }
1151
1152 /***********************************************************************
1153 * SetupDiGetClassDescriptionExA (SETUPAPI.@)
1154 */
1155 BOOL WINAPI SetupDiGetClassDescriptionExA(
1156 const GUID* ClassGuid,
1157 PSTR ClassDescription,
1158 DWORD ClassDescriptionSize,
1159 PDWORD RequiredSize,
1160 PCSTR MachineName,
1161 PVOID Reserved)
1162 {
1163 PWCHAR ClassDescriptionW;
1164 LPWSTR MachineNameW = NULL;
1165 BOOL ret;
1166
1167 TRACE("\n");
1168 if (ClassDescriptionSize > 0)
1169 {
1170 ClassDescriptionW = HeapAlloc(GetProcessHeap(), 0, ClassDescriptionSize * sizeof(WCHAR));
1171 if (!ClassDescriptionW)
1172 {
1173 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1174 ret = FALSE;
1175 goto end;
1176 }
1177 }
1178 else
1179 ClassDescriptionW = NULL;
1180
1181 if (MachineName)
1182 {
1183 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1184 if (!MachineNameW)
1185 {
1186 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1187 ret = FALSE;
1188 goto end;
1189 }
1190 }
1191
1192 ret = SetupDiGetClassDescriptionExW(ClassGuid, ClassDescriptionW, ClassDescriptionSize * sizeof(WCHAR),
1193 NULL, MachineNameW, Reserved);
1194 if (ret)
1195 {
1196 int len = WideCharToMultiByte(CP_ACP, 0, ClassDescriptionW, -1, ClassDescription,
1197 ClassDescriptionSize, NULL, NULL);
1198
1199 if (!ClassDescriptionSize && RequiredSize)
1200 *RequiredSize = len;
1201 }
1202
1203 end:
1204 HeapFree(GetProcessHeap(), 0, ClassDescriptionW);
1205 MyFree(MachineNameW);
1206 return ret;
1207 }
1208
1209 /***********************************************************************
1210 * SetupDiGetClassDescriptionExW (SETUPAPI.@)
1211 */
1212 BOOL WINAPI SetupDiGetClassDescriptionExW(
1213 const GUID* ClassGuid,
1214 PWSTR ClassDescription,
1215 DWORD ClassDescriptionSize,
1216 PDWORD RequiredSize,
1217 PCWSTR MachineName,
1218 PVOID Reserved)
1219 {
1220 HKEY hKey;
1221 DWORD dwLength;
1222
1223 TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassDescription,
1224 ClassDescriptionSize, RequiredSize, debugstr_w(MachineName), Reserved);
1225
1226 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1227 KEY_QUERY_VALUE,
1228 DIOCR_INSTALLER,
1229 MachineName,
1230 Reserved);
1231 if (hKey == INVALID_HANDLE_VALUE)
1232 {
1233 WARN("SetupDiOpenClassRegKeyExW() failed (Error %lu)\n", GetLastError());
1234 return FALSE;
1235 }
1236
1237 if (RequiredSize != NULL)
1238 {
1239 dwLength = 0;
1240 if (RegQueryValueExW(hKey,
1241 NULL,
1242 NULL,
1243 NULL,
1244 NULL,
1245 &dwLength))
1246 {
1247 RegCloseKey(hKey);
1248 return FALSE;
1249 }
1250
1251 *RequiredSize = dwLength / sizeof(WCHAR);
1252 }
1253
1254 dwLength = ClassDescriptionSize * sizeof(WCHAR);
1255 if (RegQueryValueExW(hKey,
1256 NULL,
1257 NULL,
1258 NULL,
1259 (LPBYTE)ClassDescription,
1260 &dwLength))
1261 {
1262 RegCloseKey(hKey);
1263 return FALSE;
1264 }
1265
1266 RegCloseKey(hKey);
1267
1268 return TRUE;
1269 }
1270
1271 /***********************************************************************
1272 * SetupDiGetClassDevsA (SETUPAPI.@)
1273 */
1274 HDEVINFO WINAPI SetupDiGetClassDevsA(
1275 CONST GUID *class,
1276 LPCSTR enumstr,
1277 HWND parent,
1278 DWORD flags)
1279 {
1280 return SetupDiGetClassDevsExA(class, enumstr, parent,
1281 flags, NULL, NULL, NULL);
1282 }
1283
1284 /***********************************************************************
1285 * SetupDiGetClassDevsW (SETUPAPI.@)
1286 */
1287 HDEVINFO WINAPI SetupDiGetClassDevsW(
1288 CONST GUID *class,
1289 LPCWSTR enumstr,
1290 HWND parent,
1291 DWORD flags)
1292 {
1293 return SetupDiGetClassDevsExW(class, enumstr, parent,
1294 flags, NULL, NULL, NULL);
1295 }
1296
1297 /***********************************************************************
1298 * SetupDiGetClassDevsExA (SETUPAPI.@)
1299 */
1300 HDEVINFO WINAPI SetupDiGetClassDevsExA(
1301 CONST GUID *class,
1302 LPCSTR enumstr,
1303 HWND parent,
1304 DWORD flags,
1305 HDEVINFO deviceset,
1306 LPCSTR machine,
1307 PVOID reserved)
1308 {
1309 HDEVINFO ret;
1310 LPWSTR enumstrW = NULL;
1311 LPWSTR machineW = NULL;
1312
1313 if (enumstr)
1314 {
1315 int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1316 enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1317 if (!enumstrW)
1318 {
1319 ret = (HDEVINFO)INVALID_HANDLE_VALUE;
1320 goto end;
1321 }
1322 MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1323 }
1324 if (machine)
1325 {
1326 int len = MultiByteToWideChar(CP_ACP, 0, machine, -1, NULL, 0);
1327 machineW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1328 if (!machineW)
1329 {
1330 ret = (HDEVINFO)INVALID_HANDLE_VALUE;
1331 goto end;
1332 }
1333 MultiByteToWideChar(CP_ACP, 0, machine, -1, machineW, len);
1334 }
1335 ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, deviceset, machineW, reserved);
1336
1337 end:
1338 HeapFree(GetProcessHeap(), 0, enumstrW);
1339 HeapFree(GetProcessHeap(), 0, machineW);
1340 return ret;
1341 }
1342
1343 static BOOL
1344 CreateDeviceInfoElement(
1345 IN struct DeviceInfoSet *list,
1346 IN LPCWSTR InstancePath,
1347 IN LPCGUID pClassGuid,
1348 OUT struct DeviceInfoElement **pDeviceInfo)
1349 {
1350 DWORD size;
1351 CONFIGRET cr;
1352 struct DeviceInfoElement *deviceInfo;
1353
1354 *pDeviceInfo = NULL;
1355
1356 size = FIELD_OFFSET(struct DeviceInfoElement, Data) + (strlenW(InstancePath) + 1) * sizeof(WCHAR);
1357 deviceInfo = HeapAlloc(GetProcessHeap(), 0, size);
1358 if (!deviceInfo)
1359 {
1360 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1361 return FALSE;
1362 }
1363 memset(deviceInfo, 0, size);
1364
1365 cr = CM_Locate_DevNode_ExW(&deviceInfo->dnDevInst, (DEVINSTID_W)InstancePath, CM_LOCATE_DEVNODE_PHANTOM, list->hMachine);
1366 if (cr != CR_SUCCESS)
1367 {
1368 SetLastError(GetErrorCodeFromCrCode(cr));
1369 return FALSE;
1370 }
1371
1372 deviceInfo->InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
1373 strcpyW(deviceInfo->Data, InstancePath);
1374 deviceInfo->DeviceName = deviceInfo->Data;
1375 deviceInfo->UniqueId = strrchrW(deviceInfo->Data, '\\');
1376 deviceInfo->DeviceDescription = NULL;
1377 memcpy(&deviceInfo->ClassGuid, pClassGuid, sizeof(GUID));
1378 deviceInfo->CreationFlags = 0;
1379 InitializeListHead(&deviceInfo->DriverListHead);
1380 InitializeListHead(&deviceInfo->InterfaceListHead);
1381
1382 *pDeviceInfo = deviceInfo;
1383 return TRUE;
1384 }
1385
1386 static BOOL
1387 CreateDeviceInterface(
1388 IN struct DeviceInfoElement* deviceInfo,
1389 IN LPCWSTR SymbolicLink,
1390 IN LPCGUID pInterfaceGuid,
1391 OUT struct DeviceInterface **pDeviceInterface)
1392 {
1393 struct DeviceInterface *deviceInterface;
1394
1395 *pDeviceInterface = NULL;
1396
1397 deviceInterface = HeapAlloc(GetProcessHeap(), 0,
1398 FIELD_OFFSET(struct DeviceInterface, SymbolicLink) + (strlenW(SymbolicLink) + 1) * sizeof(WCHAR));
1399 if (!deviceInterface)
1400 {
1401 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1402 return FALSE;
1403 }
1404 deviceInterface->DeviceInfo = deviceInfo;
1405 strcpyW(deviceInterface->SymbolicLink, SymbolicLink);
1406 deviceInterface->Flags = 0; /* FIXME */
1407 memcpy(&deviceInterface->InterfaceClassGuid, pInterfaceGuid, sizeof(GUID));
1408
1409 *pDeviceInterface = deviceInterface;
1410 return TRUE;
1411 }
1412
1413 static LONG SETUP_CreateDevListFromEnumerator(
1414 struct DeviceInfoSet *list,
1415 LPCGUID pClassGuid OPTIONAL,
1416 LPCWSTR Enumerator,
1417 HKEY hEnumeratorKey) /* handle to Enumerator registry key */
1418 {
1419 HKEY hDeviceIdKey, hInstanceIdKey;
1420 WCHAR KeyBuffer[MAX_PATH];
1421 WCHAR InstancePath[MAX_PATH];
1422 LPWSTR pEndOfInstancePath; /* Pointer into InstancePath buffer */
1423 struct DeviceInfoElement *deviceInfo;
1424 DWORD i = 0, j;
1425 DWORD dwLength, dwRegType;
1426 DWORD rc;
1427
1428 /* Enumerate device IDs (subkeys of hEnumeratorKey) */
1429 while (TRUE)
1430 {
1431 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1432 rc = RegEnumKeyExW(hEnumeratorKey, i, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1433 if (rc == ERROR_NO_MORE_ITEMS)
1434 break;
1435 if (rc != ERROR_SUCCESS)
1436 return rc;
1437 i++;
1438
1439 /* Open device id sub key */
1440 rc = RegOpenKeyExW(hEnumeratorKey, KeyBuffer, 0, KEY_ENUMERATE_SUB_KEYS, &hDeviceIdKey);
1441 if (rc != ERROR_SUCCESS)
1442 return rc;
1443 strcpyW(InstancePath, Enumerator);
1444 strcatW(InstancePath, L"\\");
1445 strcatW(InstancePath, KeyBuffer);
1446 strcatW(InstancePath, L"\\");
1447 pEndOfInstancePath = &InstancePath[strlenW(InstancePath)];
1448
1449 /* Enumerate instance IDs (subkeys of hDeviceIdKey) */
1450 j = 0;
1451 while (TRUE)
1452 {
1453 GUID KeyGuid;
1454
1455 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1456 rc = RegEnumKeyExW(hDeviceIdKey, j, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1457 if (rc == ERROR_NO_MORE_ITEMS)
1458 break;
1459 if (rc != ERROR_SUCCESS)
1460 {
1461 RegCloseKey(hDeviceIdKey);
1462 return rc;
1463 }
1464 j++;
1465
1466 /* Open instance id sub key */
1467 rc = RegOpenKeyExW(hDeviceIdKey, KeyBuffer, 0, KEY_QUERY_VALUE, &hInstanceIdKey);
1468 if (rc != ERROR_SUCCESS)
1469 {
1470 RegCloseKey(hDeviceIdKey);
1471 return rc;
1472 }
1473 *pEndOfInstancePath = '\0';
1474 strcatW(InstancePath, KeyBuffer);
1475
1476 /* Read ClassGUID value */
1477 dwLength = sizeof(KeyBuffer) - sizeof(WCHAR);
1478 rc = RegQueryValueExW(hInstanceIdKey, ClassGUID, NULL, &dwRegType, (LPBYTE)KeyBuffer, &dwLength);
1479 RegCloseKey(hInstanceIdKey);
1480 if (rc == ERROR_FILE_NOT_FOUND)
1481 {
1482 if (pClassGuid)
1483 /* Skip this bad entry as we can't verify it */
1484 continue;
1485 /* Set a default GUID for this device */
1486 memcpy(&KeyGuid, &GUID_NULL, sizeof(GUID));
1487 }
1488 else if (rc != ERROR_SUCCESS)
1489 {
1490 RegCloseKey(hDeviceIdKey);
1491 return rc;
1492 }
1493 else if (dwRegType != REG_SZ)
1494 {
1495 RegCloseKey(hDeviceIdKey);
1496 return ERROR_GEN_FAILURE;
1497 }
1498 else
1499 {
1500 KeyBuffer[37] = '\0'; /* Replace the } by a NULL character */
1501 if (UuidFromStringW(&KeyBuffer[1], &KeyGuid) != RPC_S_OK)
1502 /* Bad GUID, skip the entry */
1503 continue;
1504 }
1505
1506 if (pClassGuid && !IsEqualIID(&KeyGuid, pClassGuid))
1507 {
1508 /* Skip this entry as it is not the right device class */
1509 continue;
1510 }
1511
1512 /* Add the entry to the list */
1513 if (!CreateDeviceInfoElement(list, InstancePath, &KeyGuid, &deviceInfo))
1514 {
1515 RegCloseKey(hDeviceIdKey);
1516 return GetLastError();
1517 }
1518 TRACE("Adding '%s' to device info set %p\n", debugstr_w(InstancePath), list);
1519 InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
1520 }
1521 RegCloseKey(hDeviceIdKey);
1522 }
1523
1524 return ERROR_SUCCESS;
1525 }
1526
1527 static LONG SETUP_CreateDevList(
1528 struct DeviceInfoSet *list,
1529 PCWSTR MachineName OPTIONAL,
1530 LPGUID class OPTIONAL,
1531 PCWSTR Enumerator OPTIONAL)
1532 {
1533 HKEY HKLM, hEnumKey, hEnumeratorKey;
1534 WCHAR KeyBuffer[MAX_PATH];
1535 DWORD i;
1536 DWORD dwLength;
1537 DWORD rc;
1538
1539 if (class && IsEqualIID(class, &GUID_NULL))
1540 class = NULL;
1541
1542 /* Open Enum key */
1543 if (MachineName != NULL)
1544 {
1545 rc = RegConnectRegistryW(MachineName, HKEY_LOCAL_MACHINE, &HKLM);
1546 if (rc != ERROR_SUCCESS)
1547 return rc;
1548 }
1549 else
1550 HKLM = HKEY_LOCAL_MACHINE;
1551
1552 rc = RegOpenKeyExW(HKLM,
1553 REGSTR_PATH_SYSTEMENUM,
1554 0,
1555 KEY_ENUMERATE_SUB_KEYS,
1556 &hEnumKey);
1557 if (MachineName != NULL) RegCloseKey(HKLM);
1558 if (rc != ERROR_SUCCESS)
1559 return rc;
1560
1561 /* If enumerator is provided, call directly SETUP_CreateDevListFromEnumerator.
1562 * Else, enumerate all enumerators and call SETUP_CreateDevListFromEnumerator
1563 * for each one.
1564 */
1565 if (Enumerator)
1566 {
1567 rc = RegOpenKeyExW(
1568 hEnumKey,
1569 Enumerator,
1570 0,
1571 KEY_ENUMERATE_SUB_KEYS,
1572 &hEnumeratorKey);
1573 RegCloseKey(hEnumKey);
1574 if (rc != ERROR_SUCCESS)
1575 return rc;
1576 rc = SETUP_CreateDevListFromEnumerator(list, class, Enumerator, hEnumeratorKey);
1577 RegCloseKey(hEnumeratorKey);
1578 return rc;
1579 }
1580 else
1581 {
1582 /* Enumerate enumerators */
1583 i = 0;
1584 while (TRUE)
1585 {
1586 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1587 rc = RegEnumKeyExW(hEnumKey, i, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1588 if (rc == ERROR_NO_MORE_ITEMS)
1589 break;
1590 if (rc != ERROR_SUCCESS)
1591 {
1592 RegCloseKey(hEnumKey);
1593 return rc;
1594 }
1595 i++;
1596
1597 /* Open sub key */
1598 rc = RegOpenKeyExW(hEnumKey, KeyBuffer, 0, KEY_ENUMERATE_SUB_KEYS, &hEnumeratorKey);
1599 if (rc != ERROR_SUCCESS)
1600 {
1601 RegCloseKey(hEnumKey);
1602 return rc;
1603 }
1604
1605 /* Call SETUP_CreateDevListFromEnumerator */
1606 rc = SETUP_CreateDevListFromEnumerator(list, class, KeyBuffer, hEnumeratorKey);
1607 RegCloseKey(hEnumeratorKey);
1608 if (rc != ERROR_SUCCESS)
1609 {
1610 RegCloseKey(hEnumKey);
1611 return rc;
1612 }
1613 }
1614 RegCloseKey(hEnumKey);
1615 return ERROR_SUCCESS;
1616 }
1617 }
1618
1619 static LONG SETUP_CreateInterfaceList(
1620 struct DeviceInfoSet *list,
1621 PCWSTR MachineName,
1622 LPGUID InterfaceGuid,
1623 PCWSTR DeviceInstanceW /* OPTIONAL */)
1624 {
1625 HKEY hInterfaceKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID} */
1626 HKEY hDeviceInstanceKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID}\##?#{InstancePath} */
1627 HKEY hReferenceKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID}\##?#{InstancePath}\#{ReferenceString} */
1628 HKEY hControlKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID}\##?#{InstancePath}\#{ReferenceString}\Control */
1629 HKEY hEnumKey; /* HKLM\SYSTEM\CurrentControlSet\Enum */
1630 HKEY hKey; /* HKLM\SYSTEM\CurrentControlSet\Enum\{Instance\Path} */
1631 LONG rc;
1632 WCHAR KeyBuffer[max(MAX_PATH, MAX_GUID_STRING_LEN) + 1];
1633 PWSTR InstancePath;
1634 DWORD i, j;
1635 DWORD dwLength, dwInstancePathLength;
1636 DWORD dwRegType;
1637 DWORD LinkedValue;
1638 GUID ClassGuid;
1639 struct DeviceInfoElement *deviceInfo;
1640
1641 /* Open registry key related to this interface */
1642 hInterfaceKey = SetupDiOpenClassRegKeyExW(InterfaceGuid, KEY_ENUMERATE_SUB_KEYS, DIOCR_INTERFACE, MachineName, NULL);
1643 if (hInterfaceKey == INVALID_HANDLE_VALUE)
1644 return GetLastError();
1645
1646 /* Enumerate sub keys of hInterfaceKey */
1647 i = 0;
1648 while (TRUE)
1649 {
1650 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1651 rc = RegEnumKeyExW(hInterfaceKey, i, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1652 if (rc == ERROR_NO_MORE_ITEMS)
1653 break;
1654 if (rc != ERROR_SUCCESS)
1655 {
1656 RegCloseKey(hInterfaceKey);
1657 return rc;
1658 }
1659 i++;
1660
1661 /* Open sub key */
1662 rc = RegOpenKeyExW(hInterfaceKey, KeyBuffer, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hDeviceInstanceKey);
1663 if (rc != ERROR_SUCCESS)
1664 {
1665 RegCloseKey(hInterfaceKey);
1666 return rc;
1667 }
1668
1669 /* Read DeviceInstance */
1670 rc = RegQueryValueExW(hDeviceInstanceKey, DeviceInstance, NULL, &dwRegType, NULL, &dwInstancePathLength);
1671 if (rc != ERROR_SUCCESS )
1672 {
1673 RegCloseKey(hDeviceInstanceKey);
1674 RegCloseKey(hInterfaceKey);
1675 return rc;
1676 }
1677 if (dwRegType != REG_SZ)
1678 {
1679 RegCloseKey(hDeviceInstanceKey);
1680 RegCloseKey(hInterfaceKey);
1681 return ERROR_GEN_FAILURE;
1682 }
1683 InstancePath = HeapAlloc(GetProcessHeap(), 0, dwInstancePathLength + sizeof(WCHAR));
1684 if (!InstancePath)
1685 {
1686 RegCloseKey(hDeviceInstanceKey);
1687 RegCloseKey(hInterfaceKey);
1688 return ERROR_NOT_ENOUGH_MEMORY;
1689 }
1690 rc = RegQueryValueExW(hDeviceInstanceKey, DeviceInstance, NULL, NULL, (LPBYTE)InstancePath, &dwInstancePathLength);
1691 if (rc != ERROR_SUCCESS)
1692 {
1693 HeapFree(GetProcessHeap(), 0, InstancePath);
1694 RegCloseKey(hDeviceInstanceKey);
1695 RegCloseKey(hInterfaceKey);
1696 return rc;
1697 }
1698 InstancePath[dwInstancePathLength / sizeof(WCHAR)] = '\0';
1699 TRACE("DeviceInstance %s\n", debugstr_w(InstancePath));
1700
1701 if (DeviceInstanceW)
1702 {
1703 /* Check if device enumerator is not the right one */
1704 if (strcmpW(DeviceInstanceW, InstancePath) != 0)
1705 {
1706 HeapFree(GetProcessHeap(), 0, InstancePath);
1707 RegCloseKey(hDeviceInstanceKey);
1708 continue;
1709 }
1710 }
1711
1712 /* Find class GUID associated to the device instance */
1713 rc = RegOpenKeyExW(
1714 list->HKLM,
1715 REGSTR_PATH_SYSTEMENUM,
1716 0, /* Options */
1717 0,
1718 &hEnumKey);
1719 if (rc != ERROR_SUCCESS)
1720 {
1721 HeapFree(GetProcessHeap(), 0, InstancePath);
1722 RegCloseKey(hDeviceInstanceKey);
1723 RegCloseKey(hInterfaceKey);
1724 return rc;
1725 }
1726 rc = RegOpenKeyExW(
1727 hEnumKey,
1728 InstancePath,
1729 0, /* Options */
1730 KEY_QUERY_VALUE,
1731 &hKey);
1732 RegCloseKey(hEnumKey);
1733 if (rc != ERROR_SUCCESS)
1734 {
1735 HeapFree(GetProcessHeap(), 0, InstancePath);
1736 RegCloseKey(hDeviceInstanceKey);
1737 RegCloseKey(hInterfaceKey);
1738 return rc;
1739 }
1740 dwLength = sizeof(KeyBuffer) - sizeof(WCHAR);
1741 rc = RegQueryValueExW(hKey, ClassGUID, NULL, NULL, (LPBYTE)KeyBuffer, &dwLength);
1742 RegCloseKey(hKey);
1743 if (rc != ERROR_SUCCESS)
1744 {
1745 HeapFree(GetProcessHeap(), 0, InstancePath);
1746 RegCloseKey(hDeviceInstanceKey);
1747 RegCloseKey(hInterfaceKey);
1748 return rc;
1749 }
1750 KeyBuffer[dwLength / sizeof(WCHAR)] = '\0';
1751 KeyBuffer[37] = '\0'; /* Replace the } by a NULL character */
1752 if (UuidFromStringW(&KeyBuffer[1], &ClassGuid) != RPC_S_OK)
1753 {
1754 HeapFree(GetProcessHeap(), 0, InstancePath);
1755 RegCloseKey(hDeviceInstanceKey);
1756 RegCloseKey(hInterfaceKey);
1757 return ERROR_GEN_FAILURE;
1758 }
1759 TRACE("ClassGUID %s\n", debugstr_guid(&ClassGuid));
1760
1761 /* If current device doesn't match the list GUID (if any), skip this entry */
1762 if (!IsEqualIID(&list->ClassGuid, &GUID_NULL) && !IsEqualIID(&list->ClassGuid, &ClassGuid))
1763 {
1764 HeapFree(GetProcessHeap(), 0, InstancePath);
1765 RegCloseKey(hDeviceInstanceKey);
1766 continue;
1767 }
1768
1769 /* Enumerate subkeys of hDeviceInstanceKey (ie "#ReferenceString" in IoRegisterDeviceInterface). Skip entries that don't start with '#' */
1770 j = 0;
1771 while (TRUE)
1772 {
1773 LPWSTR pSymbolicLink;
1774 struct DeviceInterface *interfaceInfo;
1775
1776 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1777 rc = RegEnumKeyExW(hDeviceInstanceKey, j, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1778 if (rc == ERROR_NO_MORE_ITEMS)
1779 break;
1780 if (rc != ERROR_SUCCESS)
1781 {
1782 HeapFree(GetProcessHeap(), 0, InstancePath);
1783 RegCloseKey(hDeviceInstanceKey);
1784 RegCloseKey(hInterfaceKey);
1785 return rc;
1786 }
1787 j++;
1788 if (KeyBuffer[0] != '#')
1789 /* This entry doesn't represent an interesting entry */
1790 continue;
1791
1792 /* Open sub key */
1793 rc = RegOpenKeyExW(hDeviceInstanceKey, KeyBuffer, 0, KEY_QUERY_VALUE, &hReferenceKey);
1794 if (rc != ERROR_SUCCESS)
1795 {
1796 RegCloseKey(hDeviceInstanceKey);
1797 RegCloseKey(hInterfaceKey);
1798 return rc;
1799 }
1800
1801 /* Read SymbolicLink value */
1802 rc = RegQueryValueExW(hReferenceKey, SymbolicLink, NULL, &dwRegType, NULL, &dwLength);
1803 if (rc != ERROR_SUCCESS )
1804 {
1805 RegCloseKey(hReferenceKey);
1806 RegCloseKey(hDeviceInstanceKey);
1807 RegCloseKey(hInterfaceKey);
1808 return rc;
1809 }
1810 if (dwRegType != REG_SZ)
1811 {
1812 RegCloseKey(hReferenceKey);
1813 RegCloseKey(hDeviceInstanceKey);
1814 RegCloseKey(hInterfaceKey);
1815 return ERROR_GEN_FAILURE;
1816 }
1817
1818 /* We have found a device */
1819 /* Step 1. Create a device info element */
1820 if (!CreateDeviceInfoElement(list, InstancePath, &ClassGuid, &deviceInfo))
1821 {
1822 RegCloseKey(hReferenceKey);
1823 RegCloseKey(hDeviceInstanceKey);
1824 RegCloseKey(hInterfaceKey);
1825 return GetLastError();
1826 }
1827 TRACE("Adding device %s to list\n", debugstr_w(InstancePath));
1828 InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
1829
1830 /* Step 2. Create an interface list for this element */
1831 pSymbolicLink = HeapAlloc(GetProcessHeap(), 0, (dwLength + 1) * sizeof(WCHAR));
1832 if (!pSymbolicLink)
1833 {
1834 RegCloseKey(hReferenceKey);
1835 RegCloseKey(hDeviceInstanceKey);
1836 RegCloseKey(hInterfaceKey);
1837 return ERROR_NOT_ENOUGH_MEMORY;
1838 }
1839 rc = RegQueryValueExW(hReferenceKey, SymbolicLink, NULL, NULL, (LPBYTE)pSymbolicLink, &dwLength);
1840 pSymbolicLink[dwLength / sizeof(WCHAR)] = '\0';
1841 RegCloseKey(hReferenceKey);
1842 if (rc != ERROR_SUCCESS)
1843 {
1844 HeapFree(GetProcessHeap(), 0, pSymbolicLink);
1845 RegCloseKey(hDeviceInstanceKey);
1846 RegCloseKey(hInterfaceKey);
1847 return rc;
1848 }
1849 if (!CreateDeviceInterface(deviceInfo, pSymbolicLink, InterfaceGuid, &interfaceInfo))
1850 {
1851 HeapFree(GetProcessHeap(), 0, pSymbolicLink);
1852 RegCloseKey(hDeviceInstanceKey);
1853 RegCloseKey(hInterfaceKey);
1854 return GetLastError();
1855 }
1856
1857 /* Step 3. Update flags */
1858 if (KeyBuffer[1] == '\0')
1859 interfaceInfo->Flags |= SPINT_DEFAULT;
1860 rc = RegOpenKeyExW(hReferenceKey, Control, 0, KEY_QUERY_VALUE, &hControlKey);
1861 if (!rc)
1862 interfaceInfo->Flags |= SPINT_REMOVED;
1863 else
1864 {
1865 dwLength = sizeof(DWORD);
1866 if (RegQueryValueExW(hControlKey, Linked, NULL, &dwRegType, (LPBYTE)&LinkedValue, &dwLength)
1867 && dwRegType == REG_DWORD && LinkedValue)
1868 interfaceInfo->Flags |= SPINT_ACTIVE;
1869 RegCloseKey(hControlKey);
1870 }
1871
1872 TRACE("Adding interface %s to list\n", debugstr_w(pSymbolicLink));
1873 HeapFree(GetProcessHeap(), 0, pSymbolicLink);
1874 InsertTailList(&deviceInfo->InterfaceListHead, &interfaceInfo->ListEntry);
1875 }
1876 RegCloseKey(hDeviceInstanceKey);
1877 }
1878 RegCloseKey(hInterfaceKey);
1879 return ERROR_SUCCESS;
1880 }
1881
1882 /***********************************************************************
1883 * SetupDiGetClassDevsExW (SETUPAPI.@)
1884 */
1885 HDEVINFO WINAPI SetupDiGetClassDevsExW(
1886 CONST GUID *class,
1887 LPCWSTR enumstr,
1888 HWND parent,
1889 DWORD flags,
1890 HDEVINFO deviceset,
1891 LPCWSTR machine,
1892 PVOID reserved)
1893 {
1894 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
1895 struct DeviceInfoSet *list;
1896 LPGUID pClassGuid;
1897 LONG rc;
1898
1899 TRACE("%s %s %p 0x%08lx %p %s %p\n", debugstr_guid(class), debugstr_w(enumstr),
1900 parent, flags, deviceset, debugstr_w(machine), reserved);
1901
1902 /* Create the deviceset if not set */
1903 if (deviceset)
1904 {
1905 list = (struct DeviceInfoSet *)deviceset;
1906 if (list->magic != SETUP_DEV_INFO_SET_MAGIC)
1907 {
1908 SetLastError(ERROR_INVALID_HANDLE);
1909 return INVALID_HANDLE_VALUE;
1910 }
1911 hDeviceInfo = deviceset;
1912 }
1913 else
1914 {
1915 hDeviceInfo = SetupDiCreateDeviceInfoListExW(
1916 flags & DIGCF_DEVICEINTERFACE ? NULL : class,
1917 NULL, machine, NULL);
1918 if (hDeviceInfo == INVALID_HANDLE_VALUE)
1919 return INVALID_HANDLE_VALUE;
1920 list = (struct DeviceInfoSet *)hDeviceInfo;
1921 }
1922
1923 if (IsEqualIID(&list->ClassGuid, &GUID_NULL))
1924 pClassGuid = NULL;
1925 else
1926 pClassGuid = &list->ClassGuid;
1927
1928 if (flags & DIGCF_PRESENT)
1929 FIXME(": flag DIGCF_PRESENT ignored\n");
1930 if (flags & DIGCF_PROFILE)
1931 FIXME(": flag DIGCF_PROFILE ignored\n");
1932
1933 if (flags & DIGCF_ALLCLASSES)
1934 {
1935 rc = SETUP_CreateDevList(list, machine, pClassGuid, enumstr);
1936 if (rc != ERROR_SUCCESS)
1937 {
1938 SetLastError(rc);
1939 if (!deviceset)
1940 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1941 return INVALID_HANDLE_VALUE;
1942 }
1943 return hDeviceInfo;
1944 }
1945 else if (flags & DIGCF_DEVICEINTERFACE)
1946 {
1947 if (class == NULL)
1948 {
1949 SetLastError(ERROR_INVALID_PARAMETER);
1950 if (!deviceset)
1951 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1952 return INVALID_HANDLE_VALUE;
1953 }
1954
1955 rc = SETUP_CreateInterfaceList(list, machine, (LPGUID)class, enumstr);
1956 if (rc != ERROR_SUCCESS)
1957 {
1958 SetLastError(rc);
1959 if (!deviceset)
1960 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1961 return INVALID_HANDLE_VALUE;
1962 }
1963 return hDeviceInfo;
1964 }
1965 else
1966 {
1967 rc = SETUP_CreateDevList(list, machine, (LPGUID)class, enumstr);
1968 if (rc != ERROR_SUCCESS)
1969 {
1970 SetLastError(rc);
1971 if (!deviceset)
1972 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1973 return INVALID_HANDLE_VALUE;
1974 }
1975 return hDeviceInfo;
1976 }
1977 }
1978
1979 /***********************************************************************
1980 * SetupDiGetClassImageIndex (SETUPAPI.@)
1981 */
1982
1983 static BOOL GetIconIndex(
1984 IN HKEY hClassKey,
1985 OUT PINT ImageIndex)
1986 {
1987 LPWSTR Buffer = NULL;
1988 DWORD dwRegType, dwLength;
1989 LONG rc;
1990 BOOL ret = FALSE;
1991
1992 /* Read icon registry key */
1993 rc = RegQueryValueExW(hClassKey, REGSTR_VAL_INSICON, NULL, &dwRegType, NULL, &dwLength);
1994 if (rc != ERROR_SUCCESS)
1995 {
1996 SetLastError(rc);
1997 goto cleanup;
1998 } else if (dwRegType != REG_SZ)
1999 {
2000 SetLastError(ERROR_INVALID_INDEX);
2001 goto cleanup;
2002 }
2003 Buffer = MyMalloc(dwLength + sizeof(WCHAR));
2004 if (!Buffer)
2005 {
2006 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2007 goto cleanup;
2008 }
2009 rc = RegQueryValueExW(hClassKey, REGSTR_VAL_INSICON, NULL, NULL, (LPBYTE)Buffer, &dwLength);
2010 if (rc != ERROR_SUCCESS)
2011 {
2012 SetLastError(rc);
2013 goto cleanup;
2014 }
2015 /* make sure the returned buffer is NULL-terminated */
2016 Buffer[dwLength / sizeof(WCHAR)] = 0;
2017
2018 /* Transform icon value to a INT */
2019 *ImageIndex = atoiW(Buffer);
2020 ret = TRUE;
2021
2022 cleanup:
2023 MyFree(Buffer);
2024 return ret;
2025 }
2026
2027 BOOL WINAPI SetupDiGetClassImageIndex(
2028 IN PSP_CLASSIMAGELIST_DATA ClassImageListData,
2029 IN CONST GUID *ClassGuid,
2030 OUT PINT ImageIndex)
2031 {
2032 struct ClassImageList *list;
2033 BOOL ret = FALSE;
2034
2035 TRACE("%p %s %p\n", ClassImageListData, debugstr_guid(ClassGuid), ImageIndex);
2036
2037 if (!ClassImageListData || !ClassGuid || !ImageIndex)
2038 SetLastError(ERROR_INVALID_PARAMETER);
2039 else if (ClassImageListData->cbSize != sizeof(SP_CLASSIMAGELIST_DATA))
2040 SetLastError(ERROR_INVALID_USER_BUFFER);
2041 else if ((list = (struct ClassImageList *)ClassImageListData->Reserved) == NULL)
2042 SetLastError(ERROR_INVALID_USER_BUFFER);
2043 else if (list->magic != SETUP_CLASS_IMAGE_LIST_MAGIC)
2044 SetLastError(ERROR_INVALID_USER_BUFFER);
2045 else if (!ImageIndex)
2046 SetLastError(ERROR_INVALID_PARAMETER);
2047 else
2048 {
2049 HKEY hKey = INVALID_HANDLE_VALUE;
2050 INT iconIndex;
2051
2052 /* Read Icon registry entry into Buffer */
2053 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, KEY_QUERY_VALUE, DIOCR_INTERFACE, list->MachineName, NULL);
2054 if (hKey == INVALID_HANDLE_VALUE)
2055 goto cleanup;
2056 if (!GetIconIndex(hKey, &iconIndex))
2057 goto cleanup;
2058
2059 if (iconIndex >= 0)
2060 {
2061 SetLastError(ERROR_INVALID_INDEX);
2062 goto cleanup;
2063 }
2064
2065 *ImageIndex = -iconIndex;
2066 ret = TRUE;
2067
2068 cleanup:
2069 if (hKey != INVALID_HANDLE_VALUE)
2070 RegCloseKey(hKey);
2071 }
2072
2073 TRACE("Returning %d\n", ret);
2074 return ret;
2075 }
2076
2077 /***********************************************************************
2078 * SetupDiGetClassImageList(SETUPAPI.@)
2079 */
2080 BOOL WINAPI SetupDiGetClassImageList(
2081 OUT PSP_CLASSIMAGELIST_DATA ClassImageListData)
2082 {
2083 return SetupDiGetClassImageListExW(ClassImageListData, NULL, NULL);
2084 }
2085
2086 /***********************************************************************
2087 * SetupDiGetClassImageListExA(SETUPAPI.@)
2088 */
2089 BOOL WINAPI SetupDiGetClassImageListExA(
2090 OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
2091 IN PCSTR MachineName OPTIONAL,
2092 IN PVOID Reserved)
2093 {
2094 PWSTR MachineNameW = NULL;
2095 BOOL ret;
2096
2097 if (MachineName)
2098 {
2099 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
2100 if (MachineNameW == NULL)
2101 return FALSE;
2102 }
2103
2104 ret = SetupDiGetClassImageListExW(ClassImageListData, MachineNameW, Reserved);
2105
2106 if (MachineNameW)
2107 MyFree(MachineNameW);
2108
2109 return ret;
2110 }
2111
2112 /***********************************************************************
2113 * SetupDiGetClassImageListExW(SETUPAPI.@)
2114 */
2115 BOOL WINAPI SetupDiGetClassImageListExW(
2116 OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
2117 IN PCWSTR MachineName OPTIONAL,
2118 IN PVOID Reserved)
2119 {
2120 BOOL ret = FALSE;
2121
2122 TRACE("%p %p %p\n", ClassImageListData, debugstr_w(MachineName), Reserved);
2123
2124 if (!ClassImageListData)
2125 SetLastError(ERROR_INVALID_PARAMETER);
2126 else if (ClassImageListData->cbSize != sizeof(SP_CLASSIMAGELIST_DATA))
2127 SetLastError(ERROR_INVALID_USER_BUFFER);
2128 else if (Reserved)
2129 SetLastError(ERROR_INVALID_PARAMETER);
2130 else
2131 {
2132 struct ClassImageList *list = NULL;
2133 DWORD size;
2134
2135 size = FIELD_OFFSET(struct ClassImageList, szData);
2136 if (MachineName)
2137 size += (strlenW(MachineName) + 3) * sizeof(WCHAR);
2138 list = HeapAlloc(GetProcessHeap(), 0, size);
2139 if (!list)
2140 {
2141 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2142 goto cleanup;
2143 }
2144 list->magic = SETUP_CLASS_IMAGE_LIST_MAGIC;
2145 if (MachineName)
2146 {
2147 list->szData[0] = list->szData[1] = '\\';
2148 strcpyW(list->szData + 2, MachineName);
2149 list->MachineName = list->szData;
2150 }
2151 else
2152 {
2153 list->MachineName = NULL;
2154 }
2155
2156 ClassImageListData->Reserved = (DWORD)list; /* FIXME: 64 bit portability issue */
2157 ret = TRUE;
2158
2159 cleanup:
2160 if (!ret)
2161 MyFree(list);
2162 }
2163
2164 TRACE("Returning %d\n", ret);
2165 return ret;
2166 }
2167
2168 /***********************************************************************
2169 * SetupDiLoadClassIcon(SETUPAPI.@)
2170 */
2171 BOOL WINAPI SetupDiLoadClassIcon(
2172 IN CONST GUID *ClassGuid,
2173 OUT HICON *LargeIcon OPTIONAL,
2174 OUT PINT MiniIconIndex OPTIONAL)
2175 {
2176 BOOL ret = FALSE;
2177
2178 if (!ClassGuid)
2179 SetLastError(ERROR_INVALID_PARAMETER);
2180 else
2181 {
2182 LPWSTR Buffer = NULL;
2183 LPCWSTR DllName;
2184 INT iconIndex;
2185 HKEY hKey = INVALID_HANDLE_VALUE;
2186
2187 hKey = SetupDiOpenClassRegKey(ClassGuid, KEY_QUERY_VALUE);
2188 if (hKey == INVALID_HANDLE_VALUE)
2189 goto cleanup;
2190
2191 if (!GetIconIndex(hKey, &iconIndex))
2192 goto cleanup;
2193
2194 if (iconIndex > 0)
2195 {
2196 /* Look up icon in dll specified by Installer32 or EnumPropPages32 key */
2197 PWCHAR Comma;
2198 LONG rc;
2199 DWORD dwRegType, dwLength;
2200 rc = RegQueryValueExW(hKey, REGSTR_VAL_INSTALLER_32, NULL, &dwRegType, NULL, &dwLength);
2201 if (rc == ERROR_SUCCESS && dwRegType == REG_SZ)
2202 {
2203 Buffer = MyMalloc(dwLength + sizeof(WCHAR));
2204 if (Buffer == NULL)
2205 {
2206 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2207 goto cleanup;
2208 }
2209 rc = RegQueryValueExW(hKey, REGSTR_VAL_INSTALLER_32, NULL, NULL, (LPBYTE)Buffer, &dwLength);
2210 if (rc != ERROR_SUCCESS)
2211 {
2212 SetLastError(rc);
2213 goto cleanup;
2214 }
2215 /* make sure the returned buffer is NULL-terminated */
2216 Buffer[dwLength / sizeof(WCHAR)] = 0;
2217 }
2218 else if
2219 (ERROR_SUCCESS == (rc = RegQueryValueExW(hKey, REGSTR_VAL_ENUMPROPPAGES_32, NULL, &dwRegType, NULL, &dwLength))
2220 && dwRegType == REG_SZ)
2221 {
2222 Buffer = MyMalloc(dwLength + sizeof(WCHAR));
2223 if (Buffer == NULL)
2224 {
2225 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2226 goto cleanup;
2227 }
2228 rc = RegQueryValueExW(hKey, REGSTR_VAL_ENUMPROPPAGES_32, NULL, NULL, (LPBYTE)Buffer, &dwLength);
2229 if (rc != ERROR_SUCCESS)
2230 {
2231 SetLastError(rc);
2232 goto cleanup;
2233 }
2234 /* make sure the returned buffer is NULL-terminated */
2235 Buffer[dwLength / sizeof(WCHAR)] = 0;
2236 }
2237 else
2238 {
2239 /* Unable to find where to load the icon */
2240 SetLastError(ERROR_FILE_NOT_FOUND);
2241 goto cleanup;
2242 }
2243 Comma = strchrW(Buffer, ',');
2244 if (!Comma)
2245 {
2246 SetLastError(ERROR_GEN_FAILURE);
2247 goto cleanup;
2248 }
2249 *Comma = '\0';
2250 DllName = Buffer;
2251 }
2252 else
2253 {
2254 /* Look up icon in setupapi.dll */
2255 DllName = L"setupapi.dll";
2256 iconIndex = -iconIndex;
2257 }
2258
2259 TRACE("Icon index %d, dll name %s\n", iconIndex, debugstr_w(DllName));
2260 if (LargeIcon)
2261 {
2262 if (1 != ExtractIconEx(DllName, iconIndex, LargeIcon, NULL, 1))
2263 {
2264 SetLastError(ERROR_INVALID_INDEX);
2265 goto cleanup;
2266 }
2267 }
2268 if (MiniIconIndex)
2269 *MiniIconIndex = iconIndex;
2270 ret = TRUE;
2271
2272 cleanup:
2273 if (hKey != INVALID_HANDLE_VALUE)
2274 RegCloseKey(hKey);
2275 MyFree(Buffer);
2276 }
2277
2278 TRACE("Returning %d\n", ret);
2279 return ret;
2280 }
2281
2282 /***********************************************************************
2283 * SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2284 */
2285 BOOL WINAPI SetupDiEnumDeviceInterfaces(
2286 HDEVINFO DeviceInfoSet,
2287 PSP_DEVINFO_DATA DeviceInfoData,
2288 CONST GUID * InterfaceClassGuid,
2289 DWORD MemberIndex,
2290 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2291 {
2292 BOOL ret = FALSE;
2293
2294 TRACE("%p, %p, %s, %ld, %p\n", DeviceInfoSet, DeviceInfoData,
2295 debugstr_guid(InterfaceClassGuid), MemberIndex, DeviceInterfaceData);
2296
2297 if (!DeviceInterfaceData)
2298 SetLastError(ERROR_INVALID_PARAMETER);
2299 else if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2300 SetLastError(ERROR_INVALID_USER_BUFFER);
2301 else if (DeviceInfoSet && DeviceInfoSet != (HDEVINFO)INVALID_HANDLE_VALUE)
2302 {
2303 struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
2304
2305 if (list->magic == SETUP_DEV_INFO_SET_MAGIC)
2306 {
2307 PLIST_ENTRY ItemList = list->ListHead.Flink;
2308 BOOL Found = FALSE;
2309 while (ItemList != &list->ListHead && !Found)
2310 {
2311 PLIST_ENTRY InterfaceListEntry;
2312 struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)ItemList;
2313 if (DeviceInfoData && (struct DeviceInfoElement *)DeviceInfoData->Reserved != DevInfo)
2314 {
2315 /* We are not searching for this element */
2316 ItemList = ItemList->Flink;
2317 continue;
2318 }
2319 InterfaceListEntry = DevInfo->InterfaceListHead.Flink;
2320 while (InterfaceListEntry != &DevInfo->InterfaceListHead && !Found)
2321 {
2322 struct DeviceInterface *DevItf = (struct DeviceInterface *)InterfaceListEntry;
2323 if (!IsEqualIID(&DevItf->InterfaceClassGuid, InterfaceClassGuid))
2324 {
2325 InterfaceListEntry = InterfaceListEntry->Flink;
2326 continue;
2327 }
2328 if (MemberIndex-- == 0)
2329 {
2330 /* return this item */
2331 memcpy(&DeviceInterfaceData->InterfaceClassGuid,
2332 &DevItf->InterfaceClassGuid,
2333 sizeof(GUID));
2334 DeviceInterfaceData->Flags = DevItf->Flags;
2335 DeviceInterfaceData->Reserved = (ULONG_PTR)DevItf;
2336 Found = TRUE;
2337 }
2338 InterfaceListEntry = InterfaceListEntry->Flink;
2339 }
2340 ItemList = ItemList->Flink;
2341 }
2342 if (!Found)
2343 SetLastError(ERROR_NO_MORE_ITEMS);
2344 else
2345 ret = TRUE;
2346 }
2347 else
2348 SetLastError(ERROR_INVALID_HANDLE);
2349 }
2350 else
2351 SetLastError(ERROR_INVALID_HANDLE);
2352 return ret;
2353 }
2354
2355 static VOID ReferenceInfFile(struct InfFileDetails* infFile)
2356 {
2357 InterlockedIncrement(&infFile->References);
2358 }
2359
2360 static VOID DereferenceInfFile(struct InfFileDetails* infFile)
2361 {
2362 if (InterlockedDecrement(&infFile->References) == 0)
2363 {
2364 SetupCloseInfFile(infFile->hInf);
2365 HeapFree(GetProcessHeap(), 0, infFile);
2366 }
2367 }
2368
2369 static BOOL DestroyDriverInfoElement(struct DriverInfoElement* driverInfo)
2370 {
2371 DereferenceInfFile(driverInfo->InfFileDetails);
2372 HeapFree(GetProcessHeap(), 0, driverInfo->MatchingId);
2373 HeapFree(GetProcessHeap(), 0, driverInfo);
2374 return TRUE;
2375 }
2376
2377 static BOOL DestroyClassInstallParams(struct ClassInstallParams* installParams)
2378 {
2379 HeapFree(GetProcessHeap(), 0, installParams->PropChange);
2380 return TRUE;
2381 }
2382
2383 static BOOL DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
2384 {
2385 PLIST_ENTRY ListEntry;
2386 struct DriverInfoElement *driverInfo;
2387
2388 while (!IsListEmpty(&deviceInfo->DriverListHead))
2389 {
2390 ListEntry = RemoveHeadList(&deviceInfo->DriverListHead);
2391 driverInfo = (struct DriverInfoElement *)ListEntry;
2392 if (!DestroyDriverInfoElement(driverInfo))
2393 return FALSE;
2394 }
2395 while (!IsListEmpty(&deviceInfo->InterfaceListHead))
2396 {
2397 ListEntry = RemoveHeadList(&deviceInfo->InterfaceListHead);
2398 HeapFree(GetProcessHeap(), 0, ListEntry);
2399 }
2400 DestroyClassInstallParams(&deviceInfo->ClassInstallParams);
2401 HeapFree(GetProcessHeap(), 0, deviceInfo);
2402 return TRUE;
2403 }
2404
2405 static BOOL DestroyDeviceInfoSet(struct DeviceInfoSet* list)
2406 {
2407 PLIST_ENTRY ListEntry;
2408 struct DeviceInfoElement *deviceInfo;
2409
2410 while (!IsListEmpty(&list->ListHead))
2411 {
2412 ListEntry = RemoveHeadList(&list->ListHead);
2413 deviceInfo = (struct DeviceInfoElement *)ListEntry;
2414 if (!DestroyDeviceInfoElement(deviceInfo))
2415 return FALSE;
2416 }
2417 if (list->HKLM != HKEY_LOCAL_MACHINE)
2418 RegCloseKey(list->HKLM);
2419 CM_Disconnect_Machine(list->hMachine);
2420 DestroyClassInstallParams(&list->ClassInstallParams);
2421 HeapFree(GetProcessHeap(), 0, list);
2422 return TRUE;
2423 }
2424
2425 /***********************************************************************
2426 * SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2427 */
2428 BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
2429 {
2430 BOOL ret = FALSE;
2431
2432 TRACE("%p\n", devinfo);
2433 if (devinfo && devinfo != (HDEVINFO)INVALID_HANDLE_VALUE)
2434 {
2435 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
2436
2437 if (list->magic == SETUP_DEV_INFO_SET_MAGIC)
2438 ret = DestroyDeviceInfoSet(list);
2439 else
2440 SetLastError(ERROR_INVALID_HANDLE);
2441 }
2442 else
2443 SetLastError(ERROR_INVALID_HANDLE);
2444
2445 TRACE("Returning %d\n", ret);
2446 return ret;
2447 }
2448
2449 /***********************************************************************
2450 * SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2451 */
2452 BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
2453 HDEVINFO DeviceInfoSet,
2454 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2455 PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
2456 DWORD DeviceInterfaceDetailDataSize,
2457 PDWORD RequiredSize,
2458 PSP_DEVINFO_DATA DeviceInfoData)
2459 {
2460 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailDataW = NULL;
2461 DWORD sizeW = 0, sizeA;
2462 BOOL ret = FALSE;
2463
2464 TRACE("%p %p %p %lu %p %p\n", DeviceInfoSet,
2465 DeviceInterfaceData, DeviceInterfaceDetailData,
2466 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2467
2468 if (DeviceInterfaceDetailData->cbSize != sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A))
2469 SetLastError(ERROR_INVALID_USER_BUFFER);
2470 else if (DeviceInterfaceDetailData == NULL && DeviceInterfaceDetailDataSize != 0)
2471 SetLastError(ERROR_INVALID_PARAMETER);
2472 else if (DeviceInterfaceDetailData != NULL && DeviceInterfaceDetailDataSize < FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath) + 1)
2473 SetLastError(ERROR_INVALID_PARAMETER);
2474 else
2475 {
2476 if (DeviceInterfaceDetailData != NULL)
2477 {
2478 sizeW = FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)
2479 + (DeviceInterfaceDetailDataSize - FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath)) * sizeof(WCHAR);
2480 DeviceInterfaceDetailDataW = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)HeapAlloc(GetProcessHeap(), 0, sizeW);
2481 if (!DeviceInterfaceDetailDataW)
2482 {
2483 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2484 }
2485 }
2486 if (!DeviceInterfaceDetailData || (DeviceInterfaceDetailData && DeviceInterfaceDetailDataW))
2487 {
2488 DeviceInterfaceDetailDataW->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
2489 ret = SetupDiGetDeviceInterfaceDetailW(
2490 DeviceInfoSet,
2491 DeviceInterfaceData,
2492 DeviceInterfaceDetailDataW,
2493 sizeW,
2494 &sizeW,
2495 DeviceInfoData);
2496 sizeA = (sizeW - FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)) / sizeof(WCHAR)
2497 + FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath);
2498 if (RequiredSize)
2499 *RequiredSize = sizeA;
2500 if (ret && DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize <= sizeA)
2501 {
2502 if (!WideCharToMultiByte(
2503 CP_ACP, 0,
2504 DeviceInterfaceDetailDataW->DevicePath, -1,
2505 DeviceInterfaceDetailData->DevicePath, DeviceInterfaceDetailDataSize - FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath),
2506 NULL, NULL))
2507 {
2508 ret = FALSE;
2509 }
2510 }
2511 }
2512 HeapFree(GetProcessHeap(), 0, DeviceInterfaceDetailDataW);
2513 }
2514
2515 TRACE("Returning %d\n", ret);
2516 return ret;
2517 }
2518
2519 /***********************************************************************
2520 * SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
2521 */
2522 BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
2523 HDEVINFO DeviceInfoSet,
2524 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2525 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
2526 DWORD DeviceInterfaceDetailDataSize,
2527 PDWORD RequiredSize,
2528 PSP_DEVINFO_DATA DeviceInfoData)
2529 {
2530 BOOL ret = FALSE;
2531
2532 TRACE("%p %p %p %lu %p %p\n", DeviceInfoSet,
2533 DeviceInterfaceData, DeviceInterfaceDetailData,
2534 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2535
2536 if (!DeviceInfoSet || !DeviceInterfaceData)
2537 SetLastError(ERROR_INVALID_PARAMETER);
2538 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2539 SetLastError(ERROR_INVALID_HANDLE);
2540 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
2541 SetLastError(ERROR_INVALID_HANDLE);
2542 else if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2543 SetLastError(ERROR_INVALID_USER_BUFFER);
2544 else if (DeviceInterfaceDetailData->cbSize != sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W))
2545 SetLastError(ERROR_INVALID_USER_BUFFER);
2546 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
2547 SetLastError(ERROR_INVALID_USER_BUFFER);
2548 else if (DeviceInterfaceDetailData == NULL && DeviceInterfaceDetailDataSize != 0)
2549 SetLastError(ERROR_INVALID_PARAMETER);
2550 else if (DeviceInterfaceDetailData != NULL && DeviceInterfaceDetailDataSize < FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR))
2551 SetLastError(ERROR_INVALID_PARAMETER);
2552 else
2553 {
2554 struct DeviceInterface *deviceInterface = (struct DeviceInterface *)DeviceInterfaceData->Reserved;
2555 LPCWSTR devName = deviceInterface->SymbolicLink;
2556 DWORD sizeRequired = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W) +
2557 (lstrlenW(devName) + 1) * sizeof(WCHAR);
2558
2559 if (sizeRequired > DeviceInterfaceDetailDataSize)
2560 {
2561 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2562 if (RequiredSize)
2563 *RequiredSize = sizeRequired;
2564 }
2565 else
2566 {
2567 strcpyW(DeviceInterfaceDetailData->DevicePath, devName);
2568 TRACE("DevicePath is %s\n", debugstr_w(DeviceInterfaceDetailData->DevicePath));
2569 if (DeviceInfoData)
2570 {
2571 memcpy(&DeviceInfoData->ClassGuid,
2572 &deviceInterface->DeviceInfo->ClassGuid,
2573 sizeof(GUID));
2574 DeviceInfoData->DevInst = deviceInterface->DeviceInfo->dnDevInst;
2575 DeviceInfoData->Reserved = (ULONG_PTR)deviceInterface->DeviceInfo;
2576 }
2577 ret = TRUE;
2578 }
2579 }
2580
2581 TRACE("Returning %d\n", ret);
2582 return ret;
2583 }
2584
2585 /***********************************************************************
2586 * SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
2587 */
2588 BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
2589 HDEVINFO devinfo,
2590 PSP_DEVINFO_DATA DeviceInfoData,
2591 DWORD Property,
2592 PDWORD PropertyRegDataType,
2593 PBYTE PropertyBuffer,
2594 DWORD PropertyBufferSize,
2595 PDWORD RequiredSize)
2596 {
2597 BOOL bResult;
2598 BOOL bIsStringProperty;
2599 DWORD RegType;
2600 DWORD RequiredSizeA, RequiredSizeW;
2601 DWORD PropertyBufferSizeW;
2602 PBYTE PropertyBufferW;
2603
2604 TRACE("%p %p %ld %p %p %ld %p\n", devinfo, DeviceInfoData,
2605 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2606 RequiredSize);
2607
2608 PropertyBufferSizeW = PropertyBufferSize * 2;
2609 PropertyBufferW = HeapAlloc(GetProcessHeap(), 0, PropertyBufferSizeW);
2610
2611 bResult = SetupDiGetDeviceRegistryPropertyW(
2612 devinfo,
2613 DeviceInfoData,
2614 Property,
2615 &RegType,
2616 PropertyBufferW,
2617 PropertyBufferSizeW,
2618 &RequiredSizeW);
2619
2620 if (bResult || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2621 {
2622 bIsStringProperty = (RegType == REG_SZ || RegType == REG_MULTI_SZ || RegType == REG_EXPAND_SZ);
2623
2624 if (bIsStringProperty)
2625 RequiredSizeA = RequiredSizeW / sizeof(WCHAR);
2626 else
2627 RequiredSizeA = RequiredSizeW;
2628 if (RequiredSize)
2629 *RequiredSize = RequiredSizeA;
2630 if (PropertyRegDataType)
2631 *PropertyRegDataType = RegType;
2632 }
2633
2634 if (!bResult)
2635 {
2636 HeapFree(GetProcessHeap(), 0, PropertyBufferW);
2637 return bResult;
2638 }
2639
2640 if (RequiredSizeA <= PropertyBufferSize)
2641 {
2642 if (bIsStringProperty && PropertyBufferSize > 0)
2643 {
2644 if (WideCharToMultiByte(CP_ACP, 0, (LPWSTR)PropertyBufferW, RequiredSizeW / sizeof(WCHAR), (LPSTR)PropertyBuffer, PropertyBufferSize, NULL, NULL) == 0)
2645 {
2646 /* Last error is already set by WideCharToMultiByte */
2647 bResult = FALSE;
2648 }
2649 }
2650 else
2651 memcpy(PropertyBuffer, PropertyBufferW, RequiredSizeA);
2652 }
2653 else
2654 {
2655 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2656 bResult = FALSE;
2657 }
2658
2659 HeapFree(GetProcessHeap(), 0, PropertyBufferW);
2660 return bResult;
2661 }
2662
2663 /***********************************************************************
2664 * SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
2665 */
2666 BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
2667 HDEVINFO DeviceInfoSet,
2668 PSP_DEVINFO_DATA DeviceInfoData,
2669 DWORD Property,
2670 PDWORD PropertyRegDataType,
2671 PBYTE PropertyBuffer,
2672 DWORD PropertyBufferSize,
2673 PDWORD RequiredSize)
2674 {
2675 HKEY hEnumKey, hKey;
2676 DWORD rc;
2677 BOOL ret = FALSE;
2678
2679 TRACE("%p %p %ld %p %p %ld %p\n", DeviceInfoSet, DeviceInfoData,
2680 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2681 RequiredSize);
2682
2683 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2684 SetLastError(ERROR_INVALID_HANDLE);
2685 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
2686 SetLastError(ERROR_INVALID_HANDLE);
2687 else if (!DeviceInfoData)
2688 SetLastError(ERROR_INVALID_PARAMETER);
2689 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
2690 SetLastError(ERROR_INVALID_USER_BUFFER);
2691 else if (Property >= SPDRP_MAXIMUM_PROPERTY)
2692 SetLastError(ERROR_INVALID_PARAMETER);
2693 else
2694 {
2695 struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
2696 struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
2697
2698 switch (Property)
2699 {
2700 case SPDRP_CAPABILITIES:
2701 case SPDRP_CLASS:
2702 case SPDRP_CLASSGUID:
2703 case SPDRP_COMPATIBLEIDS:
2704 case SPDRP_CONFIGFLAGS:
2705 case SPDRP_DEVICEDESC:
2706 case SPDRP_DRIVER:
2707 case SPDRP_FRIENDLYNAME:
2708 case SPDRP_HARDWAREID:
2709 case SPDRP_LOCATION_INFORMATION:
2710 case SPDRP_LOWERFILTERS:
2711 case SPDRP_MFG:
2712 case SPDRP_SECURITY:
2713 case SPDRP_SERVICE:
2714 case SPDRP_UI_NUMBER:
2715 case SPDRP_UI_NUMBER_DESC_FORMAT:
2716 case SPDRP_UPPERFILTERS:
2717 {
2718 LPCWSTR RegistryPropertyName;
2719 DWORD BufferSize;
2720
2721 switch (Property)
2722 {
2723 case SPDRP_CAPABILITIES:
2724 RegistryPropertyName = REGSTR_VAL_CAPABILITIES; break;
2725 case SPDRP_CLASS:
2726 RegistryPropertyName = REGSTR_VAL_CLASS; break;
2727 case SPDRP_CLASSGUID:
2728 RegistryPropertyName = REGSTR_VAL_CLASSGUID; break;
2729 case SPDRP_COMPATIBLEIDS:
2730 RegistryPropertyName = REGSTR_VAL_COMPATIBLEIDS; break;
2731 case SPDRP_CONFIGFLAGS:
2732 RegistryPropertyName = REGSTR_VAL_CONFIGFLAGS; break;
2733 case SPDRP_DEVICEDESC:
2734 RegistryPropertyName = REGSTR_VAL_DEVDESC; break;
2735 case SPDRP_DRIVER:
2736 RegistryPropertyName = REGSTR_VAL_DRIVER; break;
2737 case SPDRP_FRIENDLYNAME:
2738 RegistryPropertyName = REGSTR_VAL_FRIENDLYNAME; break;
2739 case SPDRP_HARDWAREID:
2740 RegistryPropertyName = REGSTR_VAL_HARDWAREID; break;
2741 case SPDRP_LOCATION_INFORMATION:
2742 RegistryPropertyName = REGSTR_VAL_LOCATION_INFORMATION; break;
2743 case SPDRP_LOWERFILTERS:
2744 RegistryPropertyName = REGSTR_VAL_LOWERFILTERS; break;
2745 case SPDRP_MFG:
2746 RegistryPropertyName = REGSTR_VAL_MFG; break;
2747 case SPDRP_SECURITY:
2748 RegistryPropertyName = REGSTR_SECURITY; break;
2749 case SPDRP_SERVICE:
2750 RegistryPropertyName = REGSTR_VAL_SERVICE; break;
2751 case SPDRP_UI_NUMBER:
2752 RegistryPropertyName = REGSTR_VAL_UI_NUMBER; break;
2753 case SPDRP_UI_NUMBER_DESC_FORMAT:
2754 RegistryPropertyName = REGSTR_UI_NUMBER_DESC_FORMAT; break;
2755 case SPDRP_UPPERFILTERS:
2756 RegistryPropertyName = REGSTR_VAL_UPPERFILTERS; break;
2757 default:
2758 /* Should not happen */
2759 RegistryPropertyName = NULL; break;
2760 }
2761
2762 /* Open registry key name */
2763 rc = RegOpenKeyExW(
2764 list->HKLM,
2765 REGSTR_PATH_SYSTEMENUM,
2766 0, /* Options */
2767 0,
2768 &hEnumKey);
2769 if (rc != ERROR_SUCCESS)
2770 {
2771 SetLastError(rc);
2772 break;
2773 }
2774 rc = RegOpenKeyExW(
2775 hEnumKey,
2776 DevInfo->Data,
2777 0, /* Options */
2778 KEY_QUERY_VALUE,
2779 &hKey);
2780 RegCloseKey(hEnumKey);
2781 if (rc != ERROR_SUCCESS)
2782 {
2783 SetLastError(rc);
2784 break;
2785 }
2786 /* Read registry entry */
2787 BufferSize = PropertyBufferSize;
2788 rc = RegQueryValueExW(
2789 hKey,
2790 RegistryPropertyName,
2791 NULL, /* Reserved */
2792 PropertyRegDataType,
2793 PropertyBuffer,
2794 &BufferSize);
2795 if (RequiredSize)
2796 *RequiredSize = BufferSize;
2797 switch(rc) {
2798 case ERROR_SUCCESS:
2799 if (PropertyBuffer != NULL || BufferSize == 0)
2800 ret = TRUE;
2801 else
2802 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2803 break;
2804 case ERROR_MORE_DATA:
2805 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2806 break;
2807 default:
2808 SetLastError(rc);
2809 }
2810 RegCloseKey(hKey);
2811 break;
2812 }
2813
2814 case SPDRP_PHYSICAL_DEVICE_OBJECT_NAME:
2815 {
2816 DWORD required = (strlenW(DevInfo->Data) + 1) * sizeof(WCHAR);
2817
2818 if (PropertyRegDataType)
2819 *PropertyRegDataType = REG_SZ;
2820 if (RequiredSize)
2821 *RequiredSize = required;
2822 if (PropertyBufferSize >= required)
2823 {
2824 strcpyW((LPWSTR)PropertyBuffer, DevInfo->Data);
2825 ret = TRUE;
2826 }
2827 else
2828 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2829 break;
2830 }
2831
2832 /*case SPDRP_BUSTYPEGUID:
2833 case SPDRP_LEGACYBUSTYPE:
2834 case SPDRP_BUSNUMBER:
2835 case SPDRP_ENUMERATOR_NAME:
2836 case SPDRP_SECURITY_SDS:
2837 case SPDRP_DEVTYPE:
2838 case SPDRP_EXCLUSIVE:
2839 case SPDRP_CHARACTERISTICS:
2840 case SPDRP_ADDRESS:
2841 case SPDRP_DEVICE_POWER_DATA:*/
2842 #if (WINVER >= 0x501)
2843 /*case SPDRP_REMOVAL_POLICY:
2844 case SPDRP_REMOVAL_POLICY_HW_DEFAULT:
2845 case SPDRP_REMOVAL_POLICY_OVERRIDE:
2846 case SPDRP_INSTALL_STATE:*/
2847 #endif
2848
2849 default:
2850 {
2851 ERR("Property 0x%lx not implemented\n", Property);
2852 SetLastError(ERROR_NOT_SUPPORTED);
2853 }
2854 }
2855 }
2856
2857 TRACE("Returning %d\n", ret);
2858 return ret;
2859 }
2860
2861 /***********************************************************************
2862 * SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
2863 */
2864 BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
2865 IN HDEVINFO DeviceInfoSet,
2866 IN OUT PSP_DEVINFO_DATA DeviceInfoData,
2867 IN DWORD Property,
2868 IN CONST BYTE *PropertyBuffer,
2869 IN DWORD PropertyBufferSize)
2870 {
2871 FIXME("%p %p 0x%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
2872 Property, PropertyBuffer, PropertyBufferSize);
2873 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2874 return FALSE;
2875 }
2876
2877 /***********************************************************************
2878 * SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
2879 */
2880 BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
2881 IN HDEVINFO DeviceInfoSet,
2882 IN OUT PSP_DEVINFO_DATA DeviceInfoData,
2883 IN DWORD Property,
2884 IN const BYTE *PropertyBuffer,
2885 IN DWORD PropertyBufferSize)
2886 {
2887 struct DeviceInfoSet *list;
2888 BOOL ret = FALSE;
2889
2890 TRACE("%p %p 0x%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
2891 Property, PropertyBuffer, PropertyBufferSize);
2892
2893 if (!DeviceInfoSet)
2894 SetLastError(ERROR_INVALID_HANDLE);
2895 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
2896 SetLastError(ERROR_INVALID_HANDLE);
2897 else if (!DeviceInfoData)
2898 SetLastError(ERROR_INVALID_HANDLE);
2899 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
2900 SetLastError(ERROR_INVALID_USER_BUFFER);
2901 else
2902 {
2903 switch (Property)
2904 {
2905 case SPDRP_COMPATIBLEIDS:
2906 case SPDRP_CONFIGFLAGS:
2907 case SPDRP_FRIENDLYNAME:
2908 case SPDRP_HARDWAREID:
2909 case SPDRP_LOCATION_INFORMATION:
2910 case SPDRP_LOWERFILTERS:
2911 case SPDRP_SECURITY:
2912 case SPDRP_SERVICE:
2913 case SPDRP_UI_NUMBER_DESC_FORMAT:
2914 case SPDRP_UPPERFILTERS:
2915 {
2916 LPCWSTR RegistryPropertyName;
2917 DWORD RegistryDataType;
2918 HKEY hKey;
2919 LONG rc;
2920
2921 switch (Property)
2922 {
2923 case SPDRP_COMPATIBLEIDS:
2924 RegistryPropertyName = REGSTR_VAL_COMPATIBLEIDS;
2925 RegistryDataType = REG_MULTI_SZ;
2926 break;
2927 case SPDRP_CONFIGFLAGS:
2928 RegistryPropertyName = REGSTR_VAL_CONFIGFLAGS;
2929 RegistryDataType = REG_DWORD;
2930 break;
2931 case SPDRP_FRIENDLYNAME:
2932 RegistryPropertyName = REGSTR_VAL_FRIENDLYNAME;
2933 RegistryDataType = REG_SZ;
2934 break;
2935 case SPDRP_HARDWAREID:
2936 RegistryPropertyName = REGSTR_VAL_HARDWAREID;
2937 RegistryDataType = REG_MULTI_SZ;
2938 break;
2939 case SPDRP_LOCATION_INFORMATION:
2940 RegistryPropertyName = REGSTR_VAL_LOCATION_INFORMATION;
2941 RegistryDataType = REG_SZ;
2942 break;
2943 case SPDRP_LOWERFILTERS:
2944 RegistryPropertyName = REGSTR_VAL_LOWERFILTERS;
2945 RegistryDataType = REG_MULTI_SZ;
2946 break;
2947 case SPDRP_SECURITY:
2948 RegistryPropertyName = REGSTR_SECURITY;
2949 RegistryDataType = REG_BINARY;
2950 break;
2951 case SPDRP_SERVICE:
2952 RegistryPropertyName = REGSTR_VAL_SERVICE;
2953 RegistryDataType = REG_SZ;
2954 break;
2955 case SPDRP_UI_NUMBER_DESC_FORMAT:
2956 RegistryPropertyName = REGSTR_UI_NUMBER_DESC_FORMAT;
2957 RegistryDataType = REG_SZ;
2958 break;
2959 case SPDRP_UPPERFILTERS:
2960 RegistryPropertyName = REGSTR_VAL_UPPERFILTERS;
2961 RegistryDataType = REG_MULTI_SZ;
2962 break;
2963 default:
2964 /* Should not happen */
2965 RegistryPropertyName = NULL;
2966 RegistryDataType = REG_BINARY;
2967 break;
2968 }
2969 /* Open device registry key */
2970 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_SET_VALUE);
2971 if (hKey != INVALID_HANDLE_VALUE)
2972 {
2973 /* Write new data */
2974 rc = RegSetValueExW(
2975 hKey,
2976 RegistryPropertyName,
2977 0, /* Reserved */
2978 RegistryDataType,
2979 PropertyBuffer,
2980 PropertyBufferSize);
2981 if (rc == ERROR_SUCCESS)
2982 ret = TRUE;
2983 else
2984 SetLastError(rc);
2985 RegCloseKey(hKey);
2986 }
2987 break;
2988 }
2989
2990 /*case SPDRP_CHARACTERISTICS:
2991 case SPDRP_DEVTYPE:
2992 case SPDRP_EXCLUSIVE:*/
2993 #if (WINVER >= 0x501)
2994 //case SPDRP_REMOVAL_POLICY_OVERRIDE:
2995 #endif
2996 //case SPDRP_SECURITY_SDS:
2997
2998 default:
2999 {
3000 ERR("Property 0x%lx not implemented\n", Property);
3001 SetLastError(ERROR_NOT_SUPPORTED);
3002 }
3003 }
3004 }
3005
3006 TRACE("Returning %d\n", ret);
3007 return ret;
3008 }
3009
3010
3011 /***********************************************************************
3012 * SetupDiInstallClassA (SETUPAPI.@)
3013 */
3014 BOOL WINAPI SetupDiInstallClassA(
3015 IN HWND hwndParent OPTIONAL,
3016 IN PCSTR InfFileName,
3017 IN DWORD Flags,
3018 IN HSPFILEQ FileQueue OPTIONAL)
3019 {
3020 return SetupDiInstallClassExA(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
3021 }
3022
3023
3024 /***********************************************************************
3025 * SetupDiInstallClassW (SETUPAPI.@)
3026 */
3027 BOOL WINAPI SetupDiInstallClassW(
3028 IN HWND hwndParent OPTIONAL,
3029 IN PCWSTR InfFileName,
3030 IN DWORD Flags,
3031 IN HSPFILEQ FileQueue OPTIONAL)
3032 {
3033 return SetupDiInstallClassExW(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
3034 }
3035
3036
3037 /***********************************************************************
3038 * SetupDiInstallClassExA (SETUPAPI.@)
3039 */
3040 BOOL WINAPI SetupDiInstallClassExA(
3041 IN HWND hwndParent OPTIONAL,
3042 IN PCSTR InfFileName OPTIONAL,
3043 IN DWORD Flags,
3044 IN HSPFILEQ FileQueue OPTIONAL,
3045 IN const GUID* InterfaceClassGuid OPTIONAL,
3046 IN PVOID Reserved1,
3047 IN PVOID Reserved2)
3048 {
3049 PWSTR InfFileNameW = NULL;
3050 BOOL Result;
3051
3052 if (InfFileName)
3053 {
3054 InfFileNameW = MultiByteToUnicode(InfFileName, CP_ACP);
3055 if (InfFileNameW == NULL)
3056 {
3057 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3058 return FALSE;
3059 }
3060 }
3061
3062 Result = SetupDiInstallClassExW(hwndParent, InfFileNameW, Flags,
3063 FileQueue, InterfaceClassGuid, Reserved1, Reserved2);
3064
3065 MyFree(InfFileNameW);
3066
3067 return Result;
3068 }
3069
3070
3071 static HKEY CreateClassKey(HINF hInf)
3072 {
3073 WCHAR FullBuffer[MAX_PATH];
3074 WCHAR Buffer[MAX_PATH];
3075 DWORD RequiredSize;
3076 HKEY hClassKey;
3077
3078 Buffer[0] = '\\';
3079 if (!SetupGetLineTextW(NULL,
3080 hInf,
3081 Version,
3082 ClassGUID,
3083 &Buffer[1],
3084 MAX_PATH - 1,
3085 &RequiredSize))
3086 {
3087 return INVALID_HANDLE_VALUE;
3088 }
3089
3090 lstrcpyW(FullBuffer, REGSTR_PATH_CLASS_NT);
3091 lstrcatW(FullBuffer, Buffer);
3092
3093
3094 if (!SetupGetLineTextW(NULL,
3095 hInf,
3096 Version,
3097 Class,
3098 Buffer,
3099 MAX_PATH,
3100 &RequiredSize))
3101 {
3102 RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
3103 return INVALID_HANDLE_VALUE;
3104 }
3105
3106 if (ERROR_SUCCESS != RegCreateKeyExW(HKEY_LOCAL_MACHINE,
3107 FullBuffer,
3108 0,
3109 NULL,
3110 REG_OPTION_NON_VOLATILE,
3111 KEY_SET_VALUE,
3112 NULL,
3113 &hClassKey,
3114 NULL))
3115 {
3116 RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
3117 return INVALID_HANDLE_VALUE;
3118 }
3119
3120 if (ERROR_SUCCESS != RegSetValueExW(hClassKey,
3121 Class,
3122 0,
3123 REG_SZ,
3124 (LPBYTE)Buffer,
3125 RequiredSize * sizeof(WCHAR)))
3126 {
3127 RegCloseKey(hClassKey);
3128 RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
3129 return INVALID_HANDLE_VALUE;
3130 }
3131
3132 return hClassKey;
3133 }
3134
3135
3136 /***********************************************************************
3137 * SetupDiInstallClassExW (SETUPAPI.@)
3138 */
3139 BOOL WINAPI SetupDiInstallClassExW(
3140 IN HWND hwndParent OPTIONAL,
3141 IN PCWSTR InfFileName OPTIONAL,
3142 IN DWORD Flags,
3143 IN HSPFILEQ FileQueue OPTIONAL,
3144 IN const GUID* InterfaceClassGuid OPTIONAL,
3145 IN PVOID Reserved1,
3146 IN PVOID Reserved2)
3147 {
3148 BOOL ret = FALSE;
3149
3150 TRACE("%p %s 0x%lx %p %s %p %p\n", hwndParent, debugstr_w(InfFileName), Flags,
3151 FileQueue, debugstr_guid(InterfaceClassGuid), Reserved1, Reserved2);
3152
3153 if (!InfFileName)
3154 {
3155 FIXME("Case not implemented: InfFileName NULL\n");
3156 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3157 }
3158 else if (Flags & ~(DI_NOVCP | DI_NOBROWSE | DI_FORCECOPY))
3159 {
3160 TRACE("Unknown flags: 0x%08lx\n", Flags & ~(DI_NOVCP | DI_NOBROWSE | DI_FORCECOPY));
3161 SetLastError(ERROR_INVALID_FLAGS);
3162 }
3163 else if ((Flags & DI_NOVCP) && FileQueue == NULL)
3164 SetLastError(ERROR_INVALID_PARAMETER);
3165 else if (Reserved1 != NULL)
3166 SetLastError(ERROR_INVALID_PARAMETER);
3167 else if (Reserved2 != NULL)
3168 SetLastError(ERROR_INVALID_PARAMETER);
3169 else
3170 {
3171 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
3172 SP_DEVINSTALL_PARAMS_W InstallParams;
3173 WCHAR SectionName[MAX_PATH];
3174 HINF hInf = INVALID_HANDLE_VALUE;
3175 HKEY hRootKey = INVALID_HANDLE_VALUE;
3176 PVOID callback_context = NULL;
3177
3178 hDeviceInfo = SetupDiCreateDeviceInfoList(NULL, NULL);
3179
3180 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
3181 if (!SetupDiGetDeviceInstallParamsW(hDeviceInfo, NULL, &InstallParams))
3182 goto cleanup;
3183 InstallParams.Flags &= ~(DI_NOVCP | DI_NOBROWSE | DI_QUIETINSTALL);
3184 InstallParams.Flags |= Flags & (DI_NOVCP | DI_NOBROWSE | DI_QUIETINSTALL);
3185 if (Flags & DI_NOVCP)
3186 InstallParams.FileQueue = FileQueue;
3187 if (!SetupDiSetDeviceInstallParamsW(hDeviceInfo, NULL, &InstallParams))
3188 goto cleanup;
3189
3190 /* Open the .inf file */
3191 hInf = SetupOpenInfFileW(
3192 InfFileName,
3193 NULL,
3194 INF_STYLE_WIN4,
3195 NULL);
3196 if (hInf == INVALID_HANDLE_VALUE)
3197 goto cleanup;
3198
3199 /* Try to append a layout file */
3200 ret = SetupOpenAppendInfFileW(NULL, hInf, NULL);
3201 if (!ret)
3202 goto cleanup;
3203
3204 if (InterfaceClassGuid)
3205 {
3206 /* Retrieve the actual section name */
3207 ret = SetupDiGetActualSectionToInstallW(
3208 hInf,
3209 InterfaceInstall32,
3210 SectionName,
3211 MAX_PATH,
3212 NULL,
3213 NULL);
3214 if (!ret)
3215 goto cleanup;
3216
3217 /* Open registry key related to this interface */
3218 /* FIXME: What happens if the key doesn't exist? */
3219 hRootKey = SetupDiOpenClassRegKeyExW(InterfaceClassGuid, KEY_ENUMERATE_SUB_KEYS, DIOCR_INTERFACE, NULL, NULL);
3220 if (hRootKey == INVALID_HANDLE_VALUE)
3221 goto cleanup;
3222
3223 /* SetupDiCreateDeviceInterface??? */
3224 FIXME("Installing an interface is not implemented\n");
3225 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3226 }
3227 else
3228 {
3229 /* Create or open the class registry key 'HKLM\CurrentControlSet\Class\{GUID}' */
3230 hRootKey = CreateClassKey(hInf);
3231 if (hRootKey == INVALID_HANDLE_VALUE)
3232 goto cleanup;
3233
3234 /* Retrieve the actual section name */
3235 ret = SetupDiGetActualSectionToInstallW(
3236 hInf,
3237 ClassInstall32,
3238 SectionName,
3239 MAX_PATH - strlenW(DotServices),
3240 NULL,
3241 NULL);
3242 if (!ret)
3243 goto cleanup;
3244
3245 callback_context = SetupInitDefaultQueueCallback(hwndParent);
3246 if (!callback_context)
3247 goto cleanup;
3248
3249 ret = SetupInstallFromInfSectionW(
3250 hwndParent,
3251 hInf,
3252 SectionName,
3253 SPINST_REGISTRY | SPINST_FILES | SPINST_BITREG | SPINST_INIFILES | SPINST_INI2REG,
3254 hRootKey,
3255 NULL, /* SourceRootPath */
3256 !(Flags & DI_NOVCP) && (Flags & DI_FORCECOPY) ? SP_COPY_FORCE_IN_USE : 0, /* CopyFlags */
3257 SetupDefaultQueueCallbackW,
3258 callback_context,
3259 hDeviceInfo,
3260 NULL);
3261 if (!ret)
3262 goto cleanup;
3263
3264 /* Install .Services section */
3265 lstrcatW(SectionName, DotServices);
3266 ret = SetupInstallServicesFromInfSectionExW(
3267 hInf,
3268 SectionName,
3269 0,
3270 hDeviceInfo,
3271 NULL,
3272 NULL,
3273 NULL);
3274 if (!ret)
3275 goto cleanup;
3276
3277 ret = TRUE;
3278 }
3279
3280 cleanup:
3281 if (hDeviceInfo != INVALID_HANDLE_VALUE)
3282 SetupDiDestroyDeviceInfoList(hDeviceInfo);
3283 if (hInf != INVALID_HANDLE_VALUE)
3284 SetupCloseInfFile(hInf);
3285 if (hRootKey != INVALID_HANDLE_VALUE)
3286 RegCloseKey(hRootKey);
3287 SetupTermDefaultQueueCallback(callback_context);
3288 }
3289
3290 TRACE("Returning %d\n", ret);
3291 return ret;
3292 }
3293
3294
3295 /***********************************************************************
3296 * SetupDiOpenClassRegKey (SETUPAPI.@)
3297 */
3298 HKEY WINAPI SetupDiOpenClassRegKey(
3299 const GUID* ClassGuid,
3300 REGSAM samDesired)
3301 {
3302 return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3303 DIOCR_INSTALLER, NULL, NULL);
3304 }
3305
3306
3307 /***********************************************************************
3308 * SetupDiOpenClassRegKeyExA (SETUPAPI.@)
3309 */
3310 HKEY WINAPI SetupDiOpenClassRegKeyExA(
3311 const GUID* ClassGuid OPTIONAL,
3312 REGSAM samDesired,
3313 DWORD Flags,
3314 PCSTR MachineName OPTIONAL,
3315 PVOID Reserved)
3316 {
3317 PWSTR MachineNameW = NULL;
3318 HKEY hKey;
3319
3320 TRACE("\n");
3321
3322 if (MachineName)
3323 {
3324 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
3325 if (MachineNameW == NULL)
3326 return INVALID_HANDLE_VALUE;
3327 }
3328
3329 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3330 Flags, MachineNameW, Reserved);
3331
3332 if (MachineNameW)
3333 MyFree(MachineNameW);
3334
3335 return hKey;
3336 }
3337
3338
3339 /***********************************************************************
3340 * SetupDiOpenClassRegKeyExW (SETUPAPI.@)
3341 */
3342 HKEY WINAPI SetupDiOpenClassRegKeyExW(
3343 const GUID* ClassGuid OPTIONAL,
3344 REGSAM samDesired,
3345 DWORD Flags,
3346 PCWSTR MachineName OPTIONAL,
3347 PVOID Reserved)
3348 {
3349 LPWSTR lpGuidString;
3350 LPWSTR lpFullGuidString;
3351 DWORD dwLength;
3352 HKEY HKLM;
3353 HKEY hClassesKey;
3354 HKEY hClassKey;
3355 DWORD rc;
3356 LPCWSTR lpKeyName;
3357
3358 TRACE("%s 0x%lx 0x%lx %s %p\n", debugstr_guid(ClassGuid), samDesired,
3359 Flags, debugstr_w(MachineName), Reserved);
3360
3361 if (Flags == DIOCR_INSTALLER)
3362 {
3363 lpKeyName = REGSTR_PATH_CLASS_NT;
3364 }
3365 else if (Flags == DIOCR_INTERFACE)
3366 {
3367 lpKeyName = REGSTR_PATH_DEVICE_CLASSES;
3368 }
3369 else
3370 {
3371 ERR("Invalid Flags parameter!\n");
3372 SetLastError(ERROR_INVALID_FLAGS);
3373 return INVALID_HANDLE_VALUE;
3374 }
3375
3376 if (MachineName != NULL)
3377 {
3378 rc = RegConnectRegistryW(MachineName, HKEY_LOCAL_MACHINE, &HKLM);
3379 if (rc != ERROR_SUCCESS)
3380 {
3381 SetLastError(rc);
3382 return INVALID_HANDLE_VALUE;
3383 }
3384 }
3385 else
3386 HKLM = HKEY_LOCAL_MACHINE;
3387
3388 rc = RegOpenKeyExW(HKLM,
3389 lpKeyName,
3390 0,
3391 ClassGuid ? 0 : samDesired,
3392 &hClassesKey);
3393 if (MachineName != NULL) RegCloseKey(HKLM);
3394 if (rc != ERROR_SUCCESS)
3395 {
3396 SetLastError(rc);
3397 return INVALID_HANDLE_VALUE;
3398 }
3399
3400 if (ClassGuid == NULL)
3401 return hClassesKey;
3402
3403 if (UuidToStringW((UUID*)ClassGuid, &lpGuidString) != RPC_S_OK)
3404 {
3405 SetLastError(ERROR_GEN_FAILURE);
3406 RegCloseKey(hClassesKey);
3407 return INVALID_HANDLE_VALUE;
3408 }
3409
3410 dwLength = lstrlenW(lpGuidString);
3411 lpFullGuidString = HeapAlloc(GetProcessHeap(), 0, (dwLength + 3) * sizeof(WCHAR));
3412 if (!lpFullGuidString)
3413 {
3414 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3415 RpcStringFreeW(&lpGuidString);
3416 return INVALID_HANDLE_VALUE;
3417 }
3418 lpFullGuidString[0] = '{';
3419 memcpy(&lpFullGuidString[1], lpGuidString, dwLength * sizeof(WCHAR));
3420 lpFullGuidString[dwLength + 1] = '}';
3421 lpFullGuidString[dwLength + 2] = '\0';
3422 RpcStringFreeW(&lpGuidString);
3423
3424 rc = RegOpenKeyExW(hClassesKey,
3425 lpFullGuidString,
3426 0,
3427 samDesired,
3428 &hClassKey);
3429 if (rc != ERROR_SUCCESS)
3430 {
3431 SetLastError(rc);
3432 HeapFree(GetProcessHeap(), 0, lpFullGuidString);
3433 RegCloseKey(hClassesKey);
3434 return INVALID_HANDLE_VALUE;
3435 }
3436
3437 HeapFree(GetProcessHeap(), 0, lpFullGuidString);
3438 RegCloseKey(hClassesKey);
3439
3440 return hClassKey;
3441 }
3442
3443 /***********************************************************************
3444 * SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3445 */
3446 BOOL WINAPI SetupDiOpenDeviceInterfaceW(
3447 HDEVINFO DeviceInfoSet,
3448 PCWSTR DevicePath,
3449 DWORD OpenFlags,
3450 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3451 {
3452 FIXME("%p %s %08lx %p\n",
3453 DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
3454 return FALSE;
3455 }
3456
3457 /***********************************************************************
3458 * SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3459 */
3460 BOOL WINAPI SetupDiOpenDeviceInterfaceA(
3461 HDEVINFO DeviceInfoSet,
3462 PCSTR DevicePath,
3463 DWORD OpenFlags,
3464 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3465 {
3466 LPWSTR DevicePathW = NULL;
3467 BOOL bResult;
3468
3469 TRACE("%p %s %08lx %p\n", DeviceInfoSet, debugstr_a(DevicePath), OpenFlags, DeviceInterfaceData);
3470
3471 DevicePathW = MultiByteToUnicode(DevicePath, CP_ACP);
3472 if (DevicePathW == NULL)
3473 return FALSE;
3474
3475 bResult = SetupDiOpenDeviceInterfaceW(DeviceInfoSet,
3476 DevicePathW, OpenFlags, DeviceInterfaceData);
3477
3478 MyFree(DevicePathW);
3479
3480 return bResult;
3481 }
3482
3483 /***********************************************************************
3484 * SetupDiSetClassInstallParamsA (SETUPAPI.@)
3485 */
3486 BOOL WINAPI SetupDiSetClassInstallParamsA(
3487 HDEVINFO DeviceInfoSet,
3488 PSP_DEVINFO_DATA DeviceInfoData,
3489 PSP_CLASSINSTALL_HEADER ClassInstallParams,
3490 DWORD ClassInstallParamsSize)
3491 {
3492 FIXME("%p %p %x %lu\n",DeviceInfoSet, DeviceInfoData,
3493 ClassInstallParams->InstallFunction, ClassInstallParamsSize);
3494 return FALSE;
3495 }
3496
3497 /***********************************************************************
3498 * SetupDiSetClassInstallParamsW (SETUPAPI.@)
3499 */
3500 BOOL WINAPI SetupDiSetClassInstallParamsW(
3501 IN HDEVINFO DeviceInfoSet,
3502 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
3503 IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
3504 IN DWORD ClassInstallParamsSize)
3505 {
3506 struct DeviceInfoSet *list;
3507 BOOL ret = FALSE;
3508
3509 TRACE("%p %p %p %lu\n", DeviceInfoSet, DeviceInfoData,
3510 ClassInstallParams, ClassInstallParamsSize);
3511
3512 if (!DeviceInfoSet)
3513 SetLastError(ERROR_INVALID_PARAMETER);
3514 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
3515 SetLastError(ERROR_INVALID_HANDLE);
3516 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
3517 SetLastError(ERROR_INVALID_HANDLE);
3518 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
3519 SetLastError(ERROR_INVALID_USER_BUFFER);
3520 else if (ClassInstallParams && ClassInstallParams->cbSize != sizeof(SP_CLASSINSTALL_HEADER))
3521 SetLastError(ERROR_INVALID_USER_BUFFER);
3522 else if (ClassInstallParams && ClassInstallParamsSize < sizeof(SP_CLASSINSTALL_HEADER))
3523 SetLastError(ERROR_INVALID_PARAMETER);
3524 else if (!ClassInstallParams && ClassInstallParamsSize != 0)
3525 SetLastError(ERROR_INVALID_PARAMETER);
3526 else
3527 {
3528 SP_DEVINSTALL_PARAMS_W InstallParams;
3529 BOOL Result;
3530
3531 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
3532 Result = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
3533 if (!Result)
3534 goto done;
3535
3536 if (ClassInstallParams)
3537 {
3538 /* Check parameters in ClassInstallParams */
3539 if (ClassInstallParams->InstallFunction < DIF_SELECTDEVICE
3540 || ClassInstallParams->InstallFunction - DIF_SELECTDEVICE >= sizeof(UpdateClassInstallParamHandlers)/sizeof(UpdateClassInstallParamHandlers[0]))
3541 {
3542 SetLastError(ERROR_INVALID_USER_BUFFER);
3543 goto done;
3544 }
3545 else if (UpdateClassInstallParamHandlers[ClassInstallParams->InstallFunction - DIF_SELECTDEVICE] == NULL)
3546 {
3547 FIXME("InstallFunction %u is valid, but has no associated update handler\n", ClassInstallParams->InstallFunction);
3548 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3549 goto done;
3550 }
3551 ret = UpdateClassInstallParamHandlers[ClassInstallParams->InstallFunction - DIF_SELECTDEVICE](DeviceInfoSet, DeviceInfoData, ClassInstallParams, ClassInstallParamsSize);
3552 if (!ret)
3553 goto done;
3554 InstallParams.Flags |= DI_CLASSINSTALLPARAMS;
3555 }
3556 else
3557 {
3558 InstallParams.Flags &= ~DI_CLASSINSTALLPARAMS;
3559 }
3560
3561 ret = SetupDiSetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
3562 }
3563
3564 done:
3565 TRACE("Returning %d\n", ret);
3566 return ret;
3567 }
3568
3569 static BOOL PropertyChangeHandler(
3570 IN HDEVINFO DeviceInfoSet,
3571 IN PSP_DEVINFO_DATA DeviceInfoData,
3572 IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
3573 IN DWORD ClassInstallParamsSize)
3574 {
3575 PSP_PROPCHANGE_PARAMS PropChangeParams = (PSP_PROPCHANGE_PARAMS)ClassInstallParams;
3576 BOOL ret = FALSE;
3577
3578 if (!DeviceInfoData)
3579 SetLastError(ERROR_INVALID_PARAMETER);
3580 else if (ClassInstallParamsSize != sizeof(SP_PROPCHANGE_PARAMS))
3581 SetLastError(ERROR_INVALID_PARAMETER);
3582 else if (PropChangeParams && PropChangeParams->StateChange != DICS_ENABLE
3583 && PropChangeParams->StateChange != DICS_DISABLE && PropChangeParams->StateChange != DICS_PROPCHANGE
3584 && PropChangeParams->StateChange != DICS_START && PropChangeParams->StateChange != DICS_STOP)
3585 SetLastError(ERROR_INVALID_FLAGS);
3586 else if (PropChangeParams && PropChangeParams->Scope != DICS_FLAG_GLOBAL
3587 && PropChangeParams->Scope != DICS_FLAG_CONFIGSPECIFIC)
3588 SetLastError(ERROR_INVALID_FLAGS);
3589 else if (PropChangeParams
3590 && (PropChangeParams->StateChange == DICS_START || PropChangeParams->StateChange == DICS_STOP)
3591 && PropChangeParams->Scope != DICS_FLAG_CONFIGSPECIFIC)
3592 SetLastError(ERROR_INVALID_USER_BUFFER);
3593 else
3594 {
3595 PSP_PROPCHANGE_PARAMS *CurrentPropChangeParams;
3596 if (!DeviceInfoData)
3597 {
3598 struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
3599 CurrentPropChangeParams = &list->ClassInstallParams.PropChange;
3600 }
3601 else
3602 {
3603 struct DeviceInfoElement *deviceInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
3604 CurrentPropChangeParams = &deviceInfo->ClassInstallParams.PropChange;
3605 }
3606 if (*CurrentPropChangeParams)
3607 {
3608 MyFree(*CurrentPropChangeParams);
3609 *CurrentPropChangeParams = NULL;
3610 }
3611 if (PropChangeParams)
3612 {
3613 *CurrentPropChangeParams = MyMalloc(sizeof(SP_PROPCHANGE_PARAMS));
3614 if (!*CurrentPropChangeParams)
3615 {
3616 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3617 goto done;
3618 }
3619 memcpy(*CurrentPropChangeParams, PropChangeParams, sizeof(SP_PROPCHANGE_PARAMS));
3620 }
3621 ret = TRUE;
3622 }
3623
3624 done:
3625 return ret;
3626 }
3627
3628 static DWORD
3629 GetFunctionPointer(
3630 IN PWSTR InstallerName,
3631 OUT HMODULE* ModulePointer,
3632 OUT PVOID* FunctionPointer)
3633 {
3634 HMODULE hModule = NULL;
3635 LPSTR FunctionNameA = NULL;
3636 PWCHAR Comma;
3637 DWORD rc;
3638
3639 *ModulePointer = NULL;
3640 *FunctionPointer = NULL;
3641
3642 Comma = strchrW(InstallerName, ',');
3643 if (!Comma)
3644 {
3645 rc = ERROR_INVALID_PARAMETER;
3646 goto cleanup;
3647