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