ecf0fd736d882946baa7333b8598f3551cfedb55
[reactos.git] / reactos / dll / win32 / setupapi / devinst.c
1 /*
2 * SetupAPI device installer
3 *
4 * Copyright 2000 Andreas Mohr for CodeWeavers
5 * 2005-2006 Hervé Poussineau (hpoussin@reactos.org)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "setupapi_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
25
26 /* Unicode constants */
27 static const WCHAR AddInterface[] = {'A','d','d','I','n','t','e','r','f','a','c','e',0};
28 static const WCHAR ClassGUID[] = {'C','l','a','s','s','G','U','I','D',0};
29 static const WCHAR Class[] = {'C','l','a','s','s',0};
30 static const WCHAR ClassInstall32[] = {'C','l','a','s','s','I','n','s','t','a','l','l','3','2',0};
31 static const WCHAR Control[] = {'C','o','n','t','r','o','l',0};
32 static const WCHAR DeviceInstance[] = {'D','e','v','i','c','e','I','n','s','t','a','n','c','e',0};
33 static const WCHAR DotCoInstallers[] = {'.','C','o','I','n','s','t','a','l','l','e','r','s',0};
34 static const WCHAR DotHW[] = {'.','H','W',0};
35 static const WCHAR DotInterfaces[] = {'.','I','n','t','e','r','f','a','c','e','s',0};
36 static const WCHAR DotServices[] = {'.','S','e','r','v','i','c','e','s',0};
37 static const WCHAR InterfaceInstall32[] = {'I','n','t','e','r','f','a','c','e','I','n','s','t','a','l','l','3','2',0};
38 static const WCHAR Linked[] = {'L','i','n','k','e','d',0};
39 static const WCHAR SymbolicLink[] = {'S','y','m','b','o','l','i','c','L','i','n','k',0};
40 static const WCHAR Version[] = {'V','e','r','s','i','o','n',0};
41
42 static const WCHAR REGSTR_DRIVER_DATE[] = {'D','r','i','v','e','r','D','a','t','e',0};
43 static const WCHAR REGSTR_DRIVER_DATE_DATA[] = {'D','r','i','v','e','r','D','a','t','e','D','a','t','a',0};
44 static const WCHAR REGSTR_DRIVER_VERSION[] = {'D','r','i','v','e','r','V','e','r','s','i','o','n',0};
45 static const WCHAR REGSTR_SECURITY[] = {'S','e','c','u','r','i','t','y',0};
46 static const WCHAR REGSTR_UI_NUMBER_DESC_FORMAT[] = {'U','I','N','u','m','b','e','r','D','e','s','c','F','o','r','m','a','t',0};
47
48 static const WCHAR INF_MANUFACTURER[] = {'M','a','n','u','f','a','c','t','u','r','e','r',0};
49 static const WCHAR INF_PROVIDER[] = {'P','r','o','v','i','d','e','r',0};
50 static const WCHAR INF_DRIVER_VER[] = {'D','r','i','v','e','r','V','e','r',0};
51
52 typedef DWORD
53 (CALLBACK* CLASS_INSTALL_PROC) (
54 IN DI_FUNCTION InstallFunction,
55 IN HDEVINFO DeviceInfoSet,
56 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL);
57 typedef BOOL
58 (WINAPI* DEFAULT_CLASS_INSTALL_PROC) (
59 IN HDEVINFO DeviceInfoSet,
60 IN OUT PSP_DEVINFO_DATA DeviceInfoData);
61 typedef DWORD
62 (CALLBACK* COINSTALLER_PROC) (
63 IN DI_FUNCTION InstallFunction,
64 IN HDEVINFO DeviceInfoSet,
65 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
66 IN OUT PCOINSTALLER_CONTEXT_DATA Context);
67 typedef BOOL
68 (WINAPI* PROPERTY_PAGE_PROVIDER) (
69 IN PSP_PROPSHEETPAGE_REQUEST PropPageRequest,
70 IN LPFNADDPROPSHEETPAGE fAddFunc,
71 IN LPARAM lParam);
72 typedef BOOL
73 (*UPDATE_CLASS_PARAM_HANDLER) (
74 IN HDEVINFO DeviceInfoSet,
75 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
76 IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
77 IN DWORD ClassInstallParamsSize);
78
79 struct CoInstallerElement
80 {
81 LIST_ENTRY ListEntry;
82
83 HMODULE Module;
84 COINSTALLER_PROC Function;
85 BOOL DoPostProcessing;
86 PVOID PrivateData;
87 };
88
89 static BOOL
90 PropertyChangeHandler(
91 IN HDEVINFO DeviceInfoSet,
92 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
93 IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
94 IN DWORD ClassInstallParamsSize);
95
96 static UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
97 NULL, /* DIF_SELECTDEVICE */
98 NULL, /* DIF_INSTALLDEVICE */
99 NULL, /* DIF_ASSIGNRESOURCES */
100 NULL, /* DIF_PROPERTIES */
101 NULL, /* DIF_REMOVE */
102 NULL, /* DIF_FIRSTTIMESETUP */
103 NULL, /* DIF_FOUNDDEVICE */
104 NULL, /* DIF_SELECTCLASSDRIVERS */
105 NULL, /* DIF_VALIDATECLASSDRIVERS */
106 NULL, /* DIF_INSTALLCLASSDRIVERS */
107 NULL, /* DIF_CALCDISKSPACE */
108 NULL, /* DIF_DESTROYPRIVATEDATA */
109 NULL, /* DIF_VALIDATEDRIVER */
110 NULL, /* DIF_MOVEDEVICE */
111 NULL, /* DIF_DETECT */
112 NULL, /* DIF_INSTALLWIZARD */
113 NULL, /* DIF_DESTROYWIZARDDATA */
114 PropertyChangeHandler, /* DIF_PROPERTYCHANGE */
115 NULL, /* DIF_ENABLECLASS */
116 NULL, /* DIF_DETECTVERIFY */
117 NULL, /* DIF_INSTALLDEVICEFILES */
118 NULL, /* DIF_UNREMOVE */
119 NULL, /* DIF_SELECTBESTCOMPATDRV */
120 NULL, /* DIF_ALLOW_INSTALL */
121 NULL, /* DIF_REGISTERDEVICE */
122 NULL, /* DIF_NEWDEVICEWIZARD_PRESELECT */
123 NULL, /* DIF_NEWDEVICEWIZARD_SELECT */
124 NULL, /* DIF_NEWDEVICEWIZARD_PREANALYZE */
125 NULL, /* DIF_NEWDEVICEWIZARD_POSTANALYZE */
126 NULL, /* DIF_NEWDEVICEWIZARD_FINISHINSTALL */
127 NULL, /* DIF_UNUSED1 */
128 NULL, /* DIF_INSTALLINTERFACES */
129 NULL, /* DIF_DETECTCANCEL */
130 NULL, /* DIF_REGISTER_COINSTALLERS */
131 NULL, /* DIF_ADDPROPERTYPAGE_ADVANCED */
132 NULL, /* DIF_ADDPROPERTYPAGE_BASIC */
133 NULL, /* DIF_RESERVED1 */
134 NULL, /* DIF_TROUBLESHOOTER */
135 NULL, /* DIF_POWERMESSAGEWAKE */
136 NULL, /* DIF_ADDREMOTEPROPERTYPAGE_ADVANCED */
137 NULL, /* DIF_UPDATEDRIVER_UI */
138 NULL /* DIF_RESERVED2 */
139 };
140
141 /***********************************************************************
142 * SetupDiBuildClassInfoList (SETUPAPI.@)
143 */
144 BOOL WINAPI SetupDiBuildClassInfoList(
145 DWORD Flags,
146 LPGUID ClassGuidList,
147 DWORD ClassGuidListSize,
148 PDWORD RequiredSize)
149 {
150 TRACE("\n");
151 return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
152 ClassGuidListSize, RequiredSize,
153 NULL, NULL);
154 }
155
156 /***********************************************************************
157 * SetupDiBuildClassInfoListExA (SETUPAPI.@)
158 */
159 BOOL WINAPI SetupDiBuildClassInfoListExA(
160 DWORD Flags,
161 LPGUID ClassGuidList,
162 DWORD ClassGuidListSize,
163 PDWORD RequiredSize,
164 LPCSTR MachineName,
165 PVOID Reserved)
166 {
167 LPWSTR MachineNameW = NULL;
168 BOOL bResult;
169
170 TRACE("\n");
171
172 if (MachineName)
173 {
174 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
175 if (MachineNameW == NULL) return FALSE;
176 }
177
178 bResult = SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
179 ClassGuidListSize, RequiredSize,
180 MachineNameW, Reserved);
181
182 if (MachineNameW)
183 MyFree(MachineNameW);
184
185 return bResult;
186 }
187
188 /***********************************************************************
189 * SetupDiBuildClassInfoListExW (SETUPAPI.@)
190 */
191 BOOL WINAPI SetupDiBuildClassInfoListExW(
192 DWORD Flags,
193 LPGUID ClassGuidList,
194 DWORD ClassGuidListSize,
195 PDWORD RequiredSize,
196 LPCWSTR MachineName,
197 PVOID Reserved)
198 {
199 WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
200 HKEY hClassesKey;
201 HKEY hClassKey;
202 DWORD dwLength;
203 DWORD dwIndex;
204 LONG lError;
205 DWORD dwGuidListIndex = 0;
206
207 TRACE("0x%lx %p %lu %p %s %p\n", Flags, ClassGuidList,
208 ClassGuidListSize, RequiredSize, debugstr_w(MachineName), Reserved);
209
210 if (RequiredSize != NULL)
211 *RequiredSize = 0;
212
213 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
214 KEY_ENUMERATE_SUB_KEYS,
215 DIOCR_INSTALLER,
216 MachineName,
217 Reserved);
218 if (hClassesKey == INVALID_HANDLE_VALUE)
219 {
220 return FALSE;
221 }
222
223 for (dwIndex = 0; ; dwIndex++)
224 {
225 dwLength = MAX_GUID_STRING_LEN + 1;
226 lError = RegEnumKeyExW(hClassesKey,
227 dwIndex,
228 szKeyName,
229 &dwLength,
230 NULL,
231 NULL,
232 NULL,
233 NULL);
234 TRACE("RegEnumKeyExW() returns %ld\n", lError);
235 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
236 {
237 TRACE("Key name: %s\n", debugstr_w(szKeyName));
238
239 if (RegOpenKeyExW(hClassesKey,
240 szKeyName,
241 0,
242 KEY_QUERY_VALUE,
243 &hClassKey))
244 {
245 RegCloseKey(hClassesKey);
246 return FALSE;
247 }
248
249 if (!RegQueryValueExW(hClassKey,
250 REGSTR_VAL_NOUSECLASS,
251 NULL,
252 NULL,
253 NULL,
254 NULL))
255 {
256 TRACE("'NoUseClass' value found!\n");
257 RegCloseKey(hClassKey);
258 continue;
259 }
260
261 if ((Flags & DIBCI_NOINSTALLCLASS) &&
262 (!RegQueryValueExW(hClassKey,
263 REGSTR_VAL_NOINSTALLCLASS,
264 NULL,
265 NULL,
266 NULL,
267 NULL)))
268 {
269 TRACE("'NoInstallClass' value found!\n");
270 RegCloseKey(hClassKey);
271 continue;
272 }
273
274 if ((Flags & DIBCI_NODISPLAYCLASS) &&
275 (!RegQueryValueExW(hClassKey,
276 REGSTR_VAL_NODISPLAYCLASS,
277 NULL,
278 NULL,
279 NULL,
280 NULL)))
281 {
282 TRACE("'NoDisplayClass' value found!\n");
283 RegCloseKey(hClassKey);
284 continue;
285 }
286
287 RegCloseKey(hClassKey);
288
289 TRACE("Guid: %s\n", debugstr_w(szKeyName));
290 if (dwGuidListIndex < ClassGuidListSize)
291 {
292 if (szKeyName[0] == L'{' && szKeyName[37] == L'}')
293 {
294 szKeyName[37] = 0;
295 }
296 TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
297
298 UuidFromStringW(&szKeyName[1],
299 &ClassGuidList[dwGuidListIndex]);
300 }
301
302 dwGuidListIndex++;
303 }
304
305 if (lError != ERROR_SUCCESS)
306 break;
307 }
308
309 RegCloseKey(hClassesKey);
310
311 if (RequiredSize != NULL)
312 *RequiredSize = dwGuidListIndex;
313
314 if (ClassGuidListSize < dwGuidListIndex)
315 {
316 SetLastError(ERROR_INSUFFICIENT_BUFFER);
317 return FALSE;
318 }
319
320 return TRUE;
321 }
322
323 /***********************************************************************
324 * SetupDiClassGuidsFromNameA (SETUPAPI.@)
325 */
326 BOOL WINAPI SetupDiClassGuidsFromNameA(
327 LPCSTR ClassName,
328 LPGUID ClassGuidList,
329 DWORD ClassGuidListSize,
330 PDWORD RequiredSize)
331 {
332 return SetupDiClassGuidsFromNameExA(ClassName, ClassGuidList,
333 ClassGuidListSize, RequiredSize,
334 NULL, NULL);
335 }
336
337 /***********************************************************************
338 * SetupDiClassGuidsFromNameW (SETUPAPI.@)
339 */
340 BOOL WINAPI SetupDiClassGuidsFromNameW(
341 LPCWSTR ClassName,
342 LPGUID ClassGuidList,
343 DWORD ClassGuidListSize,
344 PDWORD RequiredSize)
345 {
346 return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
347 ClassGuidListSize, RequiredSize,
348 NULL, NULL);
349 }
350
351 /***********************************************************************
352 * SetupDiClassGuidsFromNameExA (SETUPAPI.@)
353 */
354 BOOL WINAPI SetupDiClassGuidsFromNameExA(
355 LPCSTR ClassName,
356 LPGUID ClassGuidList,
357 DWORD ClassGuidListSize,
358 PDWORD RequiredSize,
359 LPCSTR MachineName,
360 PVOID Reserved)
361 {
362 LPWSTR ClassNameW = NULL;
363 LPWSTR MachineNameW = NULL;
364 BOOL bResult;
365
366 TRACE("\n");
367
368 ClassNameW = MultiByteToUnicode(ClassName, CP_ACP);
369 if (ClassNameW == NULL)
370 return FALSE;
371
372 if (MachineNameW)
373 {
374 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
375 if (MachineNameW == NULL)
376 {
377 MyFree(ClassNameW);
378 return FALSE;
379 }
380 }
381
382 bResult = SetupDiClassGuidsFromNameExW(ClassNameW, ClassGuidList,
383 ClassGuidListSize, RequiredSize,
384 MachineNameW, Reserved);
385
386 if (MachineNameW)
387 MyFree(MachineNameW);
388
389 MyFree(ClassNameW);
390
391 return bResult;
392 }
393
394 /***********************************************************************
395 * SetupDiClassGuidsFromNameExW (SETUPAPI.@)
396 */
397 BOOL WINAPI SetupDiClassGuidsFromNameExW(
398 LPCWSTR ClassName,
399 LPGUID ClassGuidList,
400 DWORD ClassGuidListSize,
401 PDWORD RequiredSize,
402 LPCWSTR MachineName,
403 PVOID Reserved)
404 {
405 WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
406 WCHAR szClassName[256];
407 HKEY hClassesKey;
408 HKEY hClassKey;
409 DWORD dwLength;
410 DWORD dwIndex;
411 LONG lError;
412 DWORD dwGuidListIndex = 0;
413
414 TRACE("%s %p %lu %p %s %p\n", debugstr_w(ClassName), ClassGuidList,
415 ClassGuidListSize, RequiredSize, debugstr_w(MachineName), Reserved);
416
417 if (RequiredSize != NULL)
418 *RequiredSize = 0;
419
420 hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
421 KEY_ENUMERATE_SUB_KEYS,
422 DIOCR_INSTALLER,
423 MachineName,
424 Reserved);
425 if (hClassesKey == INVALID_HANDLE_VALUE)
426 {
427 return FALSE;
428 }
429
430 for (dwIndex = 0; ; dwIndex++)
431 {
432 dwLength = MAX_GUID_STRING_LEN + 1;
433 lError = RegEnumKeyExW(hClassesKey,
434 dwIndex,
435 szKeyName,
436 &dwLength,
437 NULL,
438 NULL,
439 NULL,
440 NULL);
441 TRACE("RegEnumKeyExW() returns %ld\n", lError);
442 if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
443 {
444 TRACE("Key name: %s\n", debugstr_w(szKeyName));
445
446 if (RegOpenKeyExW(hClassesKey,
447 szKeyName,
448 0,
449 KEY_QUERY_VALUE,
450 &hClassKey))
451 {
452 RegCloseKey(hClassesKey);
453 return FALSE;
454 }
455
456 dwLength = 256 * sizeof(WCHAR);
457 if (!RegQueryValueExW(hClassKey,
458 Class,
459 NULL,
460 NULL,
461 (LPBYTE)szClassName,
462 &dwLength))
463 {
464 TRACE("Class name: %s\n", debugstr_w(szClassName));
465
466 if (strcmpiW(szClassName, ClassName) == 0)
467 {
468 TRACE("Found matching class name\n");
469
470 TRACE("Guid: %s\n", debugstr_w(szKeyName));
471 if (dwGuidListIndex < ClassGuidListSize)
472 {
473 if (szKeyName[0] == L'{' && szKeyName[37] == L'}')
474 {
475 szKeyName[37] = 0;
476 }
477 TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
478
479 UuidFromStringW(&szKeyName[1],
480 &ClassGuidList[dwGuidListIndex]);
481 }
482
483 dwGuidListIndex++;
484 }
485 }
486
487 RegCloseKey(hClassKey);
488 }
489
490 if (lError != ERROR_SUCCESS)
491 break;
492 }
493
494 RegCloseKey(hClassesKey);
495
496 if (RequiredSize != NULL)
497 *RequiredSize = dwGuidListIndex;
498
499 if (ClassGuidListSize < dwGuidListIndex)
500 {
501 SetLastError(ERROR_INSUFFICIENT_BUFFER);
502 return FALSE;
503 }
504
505 return TRUE;
506 }
507
508 /***********************************************************************
509 * SetupDiClassNameFromGuidA (SETUPAPI.@)
510 */
511 BOOL WINAPI SetupDiClassNameFromGuidA(
512 const GUID* ClassGuid,
513 PSTR ClassName,
514 DWORD ClassNameSize,
515 PDWORD RequiredSize)
516 {
517 return SetupDiClassNameFromGuidExA(ClassGuid, ClassName,
518 ClassNameSize, RequiredSize,
519 NULL, NULL);
520 }
521
522 /***********************************************************************
523 * SetupDiClassNameFromGuidW (SETUPAPI.@)
524 */
525 BOOL WINAPI SetupDiClassNameFromGuidW(
526 const GUID* ClassGuid,
527 PWSTR ClassName,
528 DWORD ClassNameSize,
529 PDWORD RequiredSize)
530 {
531 return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
532 ClassNameSize, RequiredSize,
533 NULL, NULL);
534 }
535
536 /***********************************************************************
537 * SetupDiClassNameFromGuidExA (SETUPAPI.@)
538 */
539 BOOL WINAPI SetupDiClassNameFromGuidExA(
540 const GUID* ClassGuid,
541 PSTR ClassName,
542 DWORD ClassNameSize,
543 PDWORD RequiredSize,
544 PCSTR MachineName,
545 PVOID Reserved)
546 {
547 WCHAR ClassNameW[MAX_CLASS_NAME_LEN];
548 LPWSTR MachineNameW = NULL;
549 BOOL ret;
550
551 if (MachineName)
552 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
553 ret = SetupDiClassNameFromGuidExW(ClassGuid, ClassNameW, MAX_CLASS_NAME_LEN,
554 NULL, MachineNameW, Reserved);
555 if (ret)
556 {
557 int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
558 ClassNameSize, NULL, NULL);
559
560 if (!ClassNameSize && RequiredSize)
561 *RequiredSize = len;
562 }
563 MyFree(MachineNameW);
564 return ret;
565 }
566
567 /***********************************************************************
568 * SetupDiClassNameFromGuidExW (SETUPAPI.@)
569 */
570 BOOL WINAPI SetupDiClassNameFromGuidExW(
571 const GUID* ClassGuid,
572 PWSTR ClassName,
573 DWORD ClassNameSize,
574 PDWORD RequiredSize,
575 PCWSTR MachineName,
576 PVOID Reserved)
577 {
578 HKEY hKey;
579 DWORD dwLength;
580 LONG rc;
581
582 TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassName,
583 ClassNameSize, RequiredSize, debugstr_w(MachineName), Reserved);
584
585 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
586 KEY_QUERY_VALUE,
587 DIOCR_INSTALLER,
588 MachineName,
589 Reserved);
590 if (hKey == INVALID_HANDLE_VALUE)
591 {
592 return FALSE;
593 }
594
595 if (RequiredSize != NULL)
596 {
597 dwLength = 0;
598 rc = RegQueryValueExW(hKey,
599 Class,
600 NULL,
601 NULL,
602 NULL,
603 &dwLength);
604 if (rc != ERROR_SUCCESS)
605 {
606 SetLastError(rc);
607 RegCloseKey(hKey);
608 return FALSE;
609 }
610
611 *RequiredSize = dwLength / sizeof(WCHAR);
612 }
613
614 dwLength = ClassNameSize * sizeof(WCHAR);
615 rc = RegQueryValueExW(hKey,
616 Class,
617 NULL,
618 NULL,
619 (LPBYTE)ClassName,
620 &dwLength);
621 if (rc != ERROR_SUCCESS)
622 {
623 SetLastError(rc);
624 RegCloseKey(hKey);
625 return FALSE;
626 }
627
628 RegCloseKey(hKey);
629
630 return TRUE;
631 }
632
633 /***********************************************************************
634 * SetupDiCreateDeviceInfoList (SETUPAPI.@)
635 */
636 HDEVINFO WINAPI
637 SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
638 HWND hwndParent)
639 {
640 return SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent, NULL, NULL);
641 }
642
643 /***********************************************************************
644 * SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
645 */
646 HDEVINFO WINAPI
647 SetupDiCreateDeviceInfoListExA(const GUID *ClassGuid,
648 HWND hwndParent,
649 PCSTR MachineName,
650 PVOID Reserved)
651 {
652 LPWSTR MachineNameW = NULL;
653 HDEVINFO hDevInfo;
654
655 TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid), hwndParent,
656 debugstr_a(MachineName), Reserved);
657
658 if (MachineName)
659 {
660 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
661 if (MachineNameW == NULL)
662 return (HDEVINFO)INVALID_HANDLE_VALUE;
663 }
664
665 hDevInfo = SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent,
666 MachineNameW, Reserved);
667
668 if (MachineNameW)
669 MyFree(MachineNameW);
670
671 return hDevInfo;
672 }
673
674 static DWORD
675 GetErrorCodeFromCrCode(const IN CONFIGRET cr)
676 {
677 switch (cr)
678 {
679 case CR_INVALID_MACHINENAME: return ERROR_INVALID_COMPUTERNAME;
680 case CR_OUT_OF_MEMORY: return ERROR_NOT_ENOUGH_MEMORY;
681 case CR_SUCCESS: return ERROR_SUCCESS;
682 default:
683 /* FIXME */
684 return ERROR_GEN_FAILURE;
685 }
686
687 /* Does not happen */
688 }
689
690 /***********************************************************************
691 * SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
692 */
693 HDEVINFO WINAPI
694 SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
695 HWND hwndParent,
696 PCWSTR MachineName,
697 PVOID Reserved)
698 {
699 struct DeviceInfoSet *list;
700 LPWSTR UNCServerName = NULL;
701 DWORD size;
702 DWORD rc;
703 //CONFIGRET cr;
704 HDEVINFO ret = (HDEVINFO)INVALID_HANDLE_VALUE;;
705
706 TRACE("%s %p %s %p\n", debugstr_guid(ClassGuid), hwndParent,
707 debugstr_w(MachineName), Reserved);
708
709 size = FIELD_OFFSET(struct DeviceInfoSet, szData);
710 if (MachineName)
711 size += (strlenW(MachineName) + 3) * sizeof(WCHAR);
712 list = HeapAlloc(GetProcessHeap(), 0, size);
713 if (!list)
714 {
715 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
716 goto cleanup;
717 }
718 memset(list, 0, sizeof(struct DeviceInfoSet));
719
720 list->magic = SETUP_DEV_INFO_SET_MAGIC;
721 memcpy(
722 &list->ClassGuid,
723 ClassGuid ? ClassGuid : &GUID_NULL,
724 sizeof(list->ClassGuid));
725 list->InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
726 list->InstallParams.Flags |= DI_CLASSINSTALLPARAMS;
727 list->InstallParams.hwndParent = hwndParent;
728 if (MachineName)
729 {
730 rc = RegConnectRegistryW(MachineName, HKEY_LOCAL_MACHINE, &list->HKLM);
731 if (rc != ERROR_SUCCESS)
732 {
733 SetLastError(rc);
734 goto cleanup;
735 }
736 UNCServerName = HeapAlloc(GetProcessHeap(), 0, (strlenW(MachineName) + 3) * sizeof(WCHAR));
737 if (!UNCServerName)
738 {
739 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
740 goto cleanup;
741 }
742
743 strcpyW(UNCServerName + 2, MachineName);
744 list->szData[0] = list->szData[1] = '\\';
745 strcpyW(list->szData + 2, MachineName);
746 list->MachineName = list->szData;
747 }
748 else
749 {
750 DWORD Size = MAX_PATH;
751 list->HKLM = HKEY_LOCAL_MACHINE;
752 UNCServerName = HeapAlloc(GetProcessHeap(), 0, (MAX_PATH + 2) * sizeof(WCHAR));
753 if (!UNCServerName)
754 {
755 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
756 goto cleanup;
757 }
758 if (!GetComputerNameW(UNCServerName + 2, &Size))
759 goto cleanup;
760 list->MachineName = NULL;
761 }
762 #if 0
763 UNCServerName[0] = UNCServerName[1] = '\\';
764 cr = CM_Connect_MachineW(UNCServerName, &list->hMachine);
765 if (cr != CR_SUCCESS)
766 {
767 SetLastError(GetErrorCodeFromCrCode(cr));
768 goto cleanup;
769 }
770 #endif
771 InitializeListHead(&list->DriverListHead);
772 InitializeListHead(&list->ListHead);
773
774 ret = (HDEVINFO)list;
775
776 cleanup:
777 if (ret == INVALID_HANDLE_VALUE)
778 {
779 if (list && list->HKLM != 0 && list->HKLM != HKEY_LOCAL_MACHINE)
780 RegCloseKey(list->HKLM);
781 HeapFree(GetProcessHeap(), 0, list);
782 }
783 HeapFree(GetProcessHeap(), 0, UNCServerName);
784 return ret;
785 }
786
787 /***********************************************************************
788 * SetupDiEnumDeviceInfo (SETUPAPI.@)
789 */
790 BOOL WINAPI SetupDiEnumDeviceInfo(
791 HDEVINFO DeviceInfoSet,
792 DWORD MemberIndex,
793 PSP_DEVINFO_DATA DeviceInfoData)
794 {
795 BOOL ret = FALSE;
796
797 TRACE("%p, 0x%08lx, %p\n", DeviceInfoSet, MemberIndex, DeviceInfoData);
798 if (!DeviceInfoData)
799 SetLastError(ERROR_INVALID_PARAMETER);
800 else if (DeviceInfoSet && DeviceInfoSet != (HDEVINFO)INVALID_HANDLE_VALUE)
801 {
802 struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
803
804 if (list->magic != SETUP_DEV_INFO_SET_MAGIC)
805 SetLastError(ERROR_INVALID_HANDLE);
806 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
807 SetLastError(ERROR_INVALID_USER_BUFFER);
808 else
809 {
810 PLIST_ENTRY ItemList = list->ListHead.Flink;
811 while (ItemList != &list->ListHead && MemberIndex-- > 0)
812 ItemList = ItemList->Flink;
813 if (ItemList == &list->ListHead)
814 SetLastError(ERROR_NO_MORE_ITEMS);
815 else
816 {
817 struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)ItemList;
818 memcpy(&DeviceInfoData->ClassGuid,
819 &DevInfo->ClassGuid,
820 sizeof(GUID));
821 DeviceInfoData->DevInst = DevInfo->dnDevInst;
822 DeviceInfoData->Reserved = (ULONG_PTR)DevInfo;
823 ret = TRUE;
824 }
825 }
826 }
827 else
828 SetLastError(ERROR_INVALID_HANDLE);
829 return ret;
830 }
831
832 /***********************************************************************
833 * SetupDiGetActualSectionToInstallA (SETUPAPI.@)
834 */
835 BOOL WINAPI
836 SetupDiGetActualSectionToInstallA(
837 IN HINF InfHandle,
838 IN PCSTR InfSectionName,
839 OUT PSTR InfSectionWithExt OPTIONAL,
840 IN DWORD InfSectionWithExtSize,
841 OUT PDWORD RequiredSize OPTIONAL,
842 OUT PSTR *Extension OPTIONAL)
843 {
844 return SetupDiGetActualSectionToInstallExA(InfHandle, InfSectionName,
845 NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
846 Extension, NULL);
847 }
848
849 /***********************************************************************
850 * SetupDiGetActualSectionToInstallW (SETUPAPI.@)
851 */
852 BOOL WINAPI
853 SetupDiGetActualSectionToInstallW(
854 IN HINF InfHandle,
855 IN PCWSTR InfSectionName,
856 OUT PWSTR InfSectionWithExt OPTIONAL,
857 IN DWORD InfSectionWithExtSize,
858 OUT PDWORD RequiredSize OPTIONAL,
859 OUT PWSTR *Extension OPTIONAL)
860 {
861 return SetupDiGetActualSectionToInstallExW(InfHandle, InfSectionName,
862 NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
863 Extension, NULL);
864 }
865
866 /***********************************************************************
867 * SetupDiGetActualSectionToInstallExA (SETUPAPI.@)
868 */
869 BOOL WINAPI
870 SetupDiGetActualSectionToInstallExA(
871 IN HINF InfHandle,
872 IN PCSTR InfSectionName,
873 IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
874 OUT PSTR InfSectionWithExt OPTIONAL,
875 IN DWORD InfSectionWithExtSize,
876 OUT PDWORD RequiredSize OPTIONAL,
877 OUT PSTR* Extension OPTIONAL,
878 IN PVOID Reserved)
879 {
880 LPWSTR InfSectionNameW = NULL;
881 LPWSTR InfSectionWithExtW = NULL;
882 PWSTR ExtensionW;
883 BOOL bResult = FALSE;
884
885 TRACE("\n");
886
887 if (InfSectionName)
888 {
889 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
890 if (InfSectionNameW == NULL)
891 goto cleanup;
892 }
893 if (InfSectionWithExt)
894 {
895 InfSectionWithExtW = MyMalloc(InfSectionWithExtSize * sizeof(WCHAR));
896 if (InfSectionWithExtW == NULL)
897 goto cleanup;
898 }
899
900 bResult = SetupDiGetActualSectionToInstallExW(
901 InfHandle, InfSectionNameW, AlternatePlatformInfo,
902 InfSectionWithExt ? InfSectionWithExtW : NULL,
903 InfSectionWithExtSize,
904 RequiredSize,
905 Extension ? &ExtensionW : NULL,
906 Reserved);
907
908 if (bResult && InfSectionWithExt)
909 {
910 bResult = WideCharToMultiByte(CP_ACP, 0, InfSectionWithExtW, -1, InfSectionWithExt,
911 InfSectionWithExtSize, NULL, NULL) != 0;
912 }
913 if (bResult && Extension)
914 {
915 if (ExtensionW == NULL)
916 *Extension = NULL;
917 else
918 *Extension = &InfSectionWithExt[ExtensionW - InfSectionWithExtW];
919 }
920
921 cleanup:
922 MyFree(InfSectionNameW);
923 MyFree(InfSectionWithExtW);
924
925 return bResult;
926 }
927
928 /***********************************************************************
929 * SetupDiGetActualSectionToInstallExW (SETUPAPI.@)
930 */
931 BOOL WINAPI
932 SetupDiGetActualSectionToInstallExW(
933 IN HINF InfHandle,
934 IN PCWSTR InfSectionName,
935 IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
936 OUT PWSTR InfSectionWithExt OPTIONAL,
937 IN DWORD InfSectionWithExtSize,
938 OUT PDWORD RequiredSize OPTIONAL,
939 OUT PWSTR* Extension OPTIONAL,
940 IN PVOID Reserved)
941 {
942 BOOL ret = FALSE;
943
944 TRACE("%p %s %p %p %lu %p %p %p\n", InfHandle, debugstr_w(InfSectionName),
945 AlternatePlatformInfo, InfSectionWithExt, InfSectionWithExtSize,
946 RequiredSize, Extension, Reserved);
947
948 if (!InfHandle || InfHandle == (HINF)INVALID_HANDLE_VALUE)
949 SetLastError(ERROR_INVALID_HANDLE);
950 else if (!InfSectionName)
951 SetLastError(ERROR_INVALID_PARAMETER);
952 else if (AlternatePlatformInfo && AlternatePlatformInfo->cbSize != sizeof(SP_ALTPLATFORM_INFO))
953 SetLastError(ERROR_INVALID_USER_BUFFER);
954 else if (Reserved != NULL)
955 SetLastError(ERROR_INVALID_PARAMETER);
956 else
957 {
958 static SP_ALTPLATFORM_INFO CurrentPlatform = { 0, };
959 PSP_ALTPLATFORM_INFO pPlatformInfo = &CurrentPlatform;
960 LPCWSTR pExtensionPlatform, pExtensionArchitecture;
961 WCHAR SectionName[LINE_LEN + 1];
962 LONG lLineCount = -1;
963 DWORD dwFullLength;
964
965 /* Fill platform info if needed */
966 if (AlternatePlatformInfo)
967 pPlatformInfo = AlternatePlatformInfo;
968 else if (CurrentPlatform.cbSize != sizeof(SP_ALTPLATFORM_INFO))
969 {
970 /* That's the first time we go here. We need to fill in the structure */
971 OSVERSIONINFO VersionInfo;
972 SYSTEM_INFO SystemInfo;
973 VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
974 ret = GetVersionEx(&VersionInfo);
975 if (!ret)
976 goto done;
977 GetSystemInfo(&SystemInfo);
978 CurrentPlatform.cbSize = sizeof(SP_ALTPLATFORM_INFO);
979 CurrentPlatform.Platform = VersionInfo.dwPlatformId;
980 CurrentPlatform.MajorVersion = VersionInfo.dwMajorVersion;
981 CurrentPlatform.MinorVersion = VersionInfo.dwMinorVersion;
982 CurrentPlatform.ProcessorArchitecture = SystemInfo.wProcessorArchitecture;
983 CurrentPlatform.Reserved = 0;
984 }
985
986 static const WCHAR ExtensionPlatformNone[] = {'.',0};
987 static const WCHAR ExtensionPlatformNT[] = {'.','N','T',0};
988 static const WCHAR ExtensionPlatformWindows[] = {'.','W','i','n',0};
989
990 static const WCHAR ExtensionArchitectureNone[] = {0};
991 static const WCHAR ExtensionArchitecturealpha[] = {'a','l','p','h','a',0};
992 static const WCHAR ExtensionArchitectureamd64[] = {'a','m','d','6','4',0};
993 static const WCHAR ExtensionArchitectureia64[] = {'i','a','6','4',0};
994 static const WCHAR ExtensionArchitecturemips[] = {'m','i','p','s',0};
995 static const WCHAR ExtensionArchitectureppc[] = {'p','p','c',0};
996 static const WCHAR ExtensionArchitecturex86[] = {'x','8','6',0};
997
998 /* Set various extensions values */
999 switch (pPlatformInfo->Platform)
1000 {
1001 case VER_PLATFORM_WIN32_WINDOWS:
1002 pExtensionPlatform = ExtensionPlatformWindows;
1003 break;
1004 case VER_PLATFORM_WIN32_NT:
1005 pExtensionPlatform = ExtensionPlatformNT;
1006 break;
1007 default:
1008 ERR("Unkown platform 0x%lx\n", pPlatformInfo->Platform);
1009 pExtensionPlatform = ExtensionPlatformNone;
1010 break;
1011 }
1012 switch (pPlatformInfo->ProcessorArchitecture)
1013 {
1014 case PROCESSOR_ARCHITECTURE_ALPHA:
1015 pExtensionArchitecture = ExtensionArchitecturealpha;
1016 break;
1017 case PROCESSOR_ARCHITECTURE_AMD64:
1018 pExtensionArchitecture = ExtensionArchitectureamd64;
1019 break;
1020 case PROCESSOR_ARCHITECTURE_IA64:
1021 pExtensionArchitecture = ExtensionArchitectureia64;
1022 break;
1023 case PROCESSOR_ARCHITECTURE_INTEL:
1024 pExtensionArchitecture = ExtensionArchitecturex86;
1025 break;
1026 case PROCESSOR_ARCHITECTURE_MIPS:
1027 pExtensionArchitecture = ExtensionArchitecturemips;
1028 break;
1029 case PROCESSOR_ARCHITECTURE_PPC:
1030 pExtensionArchitecture = ExtensionArchitectureppc;
1031 break;
1032 default:
1033 ERR("Unknown processor architecture 0x%x\n", pPlatformInfo->ProcessorArchitecture);
1034 case PROCESSOR_ARCHITECTURE_UNKNOWN:
1035 pExtensionArchitecture = ExtensionArchitectureNone;
1036 break;
1037 }
1038
1039 static const WCHAR FormatPlatformArchitectureMajorMinor[] = {'%','s','%','s','%','s','.','%','l','u','.','%','l','u',0};
1040 static const WCHAR FormatPlatformMajorMinor[] = {'%','s','%','s','.','%','l','u','.','%','l','u',0};
1041 static const WCHAR FormatPlatformArchitectureMajor[] = {'%','s','%','s','%','s','.','%','l','u',0};
1042 static const WCHAR FormatPlatformMajor[] = {'%','s','%','s','.','%','l','u',0};
1043 static const WCHAR FormatPlatformArchitecture[] = {'%','s','%','s','%','s',0};
1044 static const WCHAR FormatPlatform[] = {'%','s','%','s',0};
1045 static const WCHAR FormatNone[] = {'%','s',0};
1046
1047 SectionName[LINE_LEN] = UNICODE_NULL;
1048
1049 /* Test with platform.architecture.major.minor extension */
1050 snprintfW(SectionName, LINE_LEN, FormatPlatformArchitectureMajorMinor, InfSectionName,
1051 pExtensionPlatform, pExtensionArchitecture, pPlatformInfo->MajorVersion, pPlatformInfo->MinorVersion);
1052 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1053 if (lLineCount != -1) goto sectionfound;
1054
1055 /* Test with platform.major.minor extension */
1056 snprintfW(SectionName, LINE_LEN, FormatPlatformMajorMinor, InfSectionName,
1057 pExtensionPlatform, pPlatformInfo->MajorVersion, pPlatformInfo->MinorVersion);
1058 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1059 if (lLineCount != -1) goto sectionfound;
1060
1061 /* Test with platform.architecture.major extension */
1062 snprintfW(SectionName, LINE_LEN, FormatPlatformArchitectureMajor, InfSectionName,
1063 pExtensionPlatform, pExtensionArchitecture, pPlatformInfo->MajorVersion);
1064 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1065 if (lLineCount != -1) goto sectionfound;
1066
1067 /* Test with platform.major extension */
1068 snprintfW(SectionName, LINE_LEN, FormatPlatformMajor, InfSectionName,
1069 pExtensionPlatform, pPlatformInfo->MajorVersion);
1070 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1071 if (lLineCount != -1) goto sectionfound;
1072
1073 /* Test with platform.architecture extension */
1074 snprintfW(SectionName, LINE_LEN, FormatPlatformArchitecture, InfSectionName,
1075 pExtensionPlatform, pExtensionArchitecture);
1076 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1077 if (lLineCount != -1) goto sectionfound;
1078
1079 /* Test with platform extension */
1080 snprintfW(SectionName, LINE_LEN, FormatPlatform, InfSectionName,
1081 pExtensionPlatform);
1082 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1083 if (lLineCount != -1) goto sectionfound;
1084
1085 /* Test without extension */
1086 snprintfW(SectionName, LINE_LEN, FormatNone, InfSectionName);
1087 lLineCount = SetupGetLineCountW(InfHandle, SectionName);
1088 if (lLineCount != -1) goto sectionfound;
1089
1090 /* No appropriate section found */
1091 SetLastError(ERROR_INVALID_PARAMETER);
1092 goto done;
1093
1094 sectionfound:
1095 dwFullLength = lstrlenW(SectionName);
1096 if (InfSectionWithExt != NULL && InfSectionWithExtSize != 0)
1097 {
1098 if (InfSectionWithExtSize < (dwFullLength + 1))
1099 {
1100 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1101 goto done;
1102 }
1103
1104 lstrcpyW(InfSectionWithExt, SectionName);
1105 if (Extension != NULL)
1106 {
1107 DWORD dwLength = lstrlenW(SectionName);
1108 *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
1109 }
1110 }
1111
1112 if (RequiredSize != NULL)
1113 *RequiredSize = dwFullLength + 1;
1114
1115 ret = TRUE;
1116 }
1117
1118 done:
1119 TRACE("Returning %d\n", ret);
1120 return ret;
1121 }
1122
1123
1124 /***********************************************************************
1125 * SetupDiGetClassDescriptionA (SETUPAPI.@)
1126 */
1127 BOOL WINAPI SetupDiGetClassDescriptionA(
1128 const GUID* ClassGuid,
1129 PSTR ClassDescription,
1130 DWORD ClassDescriptionSize,
1131 PDWORD RequiredSize)
1132 {
1133 return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
1134 ClassDescriptionSize,
1135 RequiredSize, NULL, NULL);
1136 }
1137
1138 /***********************************************************************
1139 * SetupDiGetClassDescriptionW (SETUPAPI.@)
1140 */
1141 BOOL WINAPI SetupDiGetClassDescriptionW(
1142 const GUID* ClassGuid,
1143 PWSTR ClassDescription,
1144 DWORD ClassDescriptionSize,
1145 PDWORD RequiredSize)
1146 {
1147 return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
1148 ClassDescriptionSize,
1149 RequiredSize, NULL, NULL);
1150 }
1151
1152 /***********************************************************************
1153 * SetupDiGetClassDescriptionExA (SETUPAPI.@)
1154 */
1155 BOOL WINAPI SetupDiGetClassDescriptionExA(
1156 const GUID* ClassGuid,
1157 PSTR ClassDescription,
1158 DWORD ClassDescriptionSize,
1159 PDWORD RequiredSize,
1160 PCSTR MachineName,
1161 PVOID Reserved)
1162 {
1163 PWCHAR ClassDescriptionW;
1164 LPWSTR MachineNameW = NULL;
1165 BOOL ret;
1166
1167 TRACE("\n");
1168 if (ClassDescriptionSize > 0)
1169 {
1170 ClassDescriptionW = HeapAlloc(GetProcessHeap(), 0, ClassDescriptionSize * sizeof(WCHAR));
1171 if (!ClassDescriptionW)
1172 {
1173 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1174 ret = FALSE;
1175 goto end;
1176 }
1177 }
1178 else
1179 ClassDescriptionW = NULL;
1180
1181 if (MachineName)
1182 {
1183 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
1184 if (!MachineNameW)
1185 {
1186 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1187 ret = FALSE;
1188 goto end;
1189 }
1190 }
1191
1192 ret = SetupDiGetClassDescriptionExW(ClassGuid, ClassDescriptionW, ClassDescriptionSize * sizeof(WCHAR),
1193 NULL, MachineNameW, Reserved);
1194 if (ret)
1195 {
1196 int len = WideCharToMultiByte(CP_ACP, 0, ClassDescriptionW, -1, ClassDescription,
1197 ClassDescriptionSize, NULL, NULL);
1198
1199 if (!ClassDescriptionSize && RequiredSize)
1200 *RequiredSize = len;
1201 }
1202
1203 end:
1204 HeapFree(GetProcessHeap(), 0, ClassDescriptionW);
1205 MyFree(MachineNameW);
1206 return ret;
1207 }
1208
1209 /***********************************************************************
1210 * SetupDiGetClassDescriptionExW (SETUPAPI.@)
1211 */
1212 BOOL WINAPI SetupDiGetClassDescriptionExW(
1213 const GUID* ClassGuid,
1214 PWSTR ClassDescription,
1215 DWORD ClassDescriptionSize,
1216 PDWORD RequiredSize,
1217 PCWSTR MachineName,
1218 PVOID Reserved)
1219 {
1220 HKEY hKey;
1221 DWORD dwLength;
1222
1223 TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassDescription,
1224 ClassDescriptionSize, RequiredSize, debugstr_w(MachineName), Reserved);
1225
1226 hKey = SetupDiOpenClassRegKeyExW(ClassGuid,
1227 KEY_QUERY_VALUE,
1228 DIOCR_INSTALLER,
1229 MachineName,
1230 Reserved);
1231 if (hKey == INVALID_HANDLE_VALUE)
1232 {
1233 WARN("SetupDiOpenClassRegKeyExW() failed (Error %lu)\n", GetLastError());
1234 return FALSE;
1235 }
1236
1237 if (RequiredSize != NULL)
1238 {
1239 dwLength = 0;
1240 if (RegQueryValueExW(hKey,
1241 NULL,
1242 NULL,
1243 NULL,
1244 NULL,
1245 &dwLength))
1246 {
1247 RegCloseKey(hKey);
1248 return FALSE;
1249 }
1250
1251 *RequiredSize = dwLength / sizeof(WCHAR);
1252 }
1253
1254 dwLength = ClassDescriptionSize * sizeof(WCHAR);
1255 if (RegQueryValueExW(hKey,
1256 NULL,
1257 NULL,
1258 NULL,
1259 (LPBYTE)ClassDescription,
1260 &dwLength))
1261 {
1262 RegCloseKey(hKey);
1263 return FALSE;
1264 }
1265
1266 RegCloseKey(hKey);
1267
1268 return TRUE;
1269 }
1270
1271 /***********************************************************************
1272 * SetupDiGetClassDevsA (SETUPAPI.@)
1273 */
1274 HDEVINFO WINAPI SetupDiGetClassDevsA(
1275 CONST GUID *class,
1276 LPCSTR enumstr,
1277 HWND parent,
1278 DWORD flags)
1279 {
1280 return SetupDiGetClassDevsExA(class, enumstr, parent,
1281 flags, NULL, NULL, NULL);
1282 }
1283
1284 /***********************************************************************
1285 * SetupDiGetClassDevsW (SETUPAPI.@)
1286 */
1287 HDEVINFO WINAPI SetupDiGetClassDevsW(
1288 CONST GUID *class,
1289 LPCWSTR enumstr,
1290 HWND parent,
1291 DWORD flags)
1292 {
1293 return SetupDiGetClassDevsExW(class, enumstr, parent,
1294 flags, NULL, NULL, NULL);
1295 }
1296
1297 /***********************************************************************
1298 * SetupDiGetClassDevsExA (SETUPAPI.@)
1299 */
1300 HDEVINFO WINAPI SetupDiGetClassDevsExA(
1301 CONST GUID *class,
1302 LPCSTR enumstr,
1303 HWND parent,
1304 DWORD flags,
1305 HDEVINFO deviceset,
1306 LPCSTR machine,
1307 PVOID reserved)
1308 {
1309 HDEVINFO ret;
1310 LPWSTR enumstrW = NULL;
1311 LPWSTR machineW = NULL;
1312
1313 if (enumstr)
1314 {
1315 int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
1316 enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1317 if (!enumstrW)
1318 {
1319 ret = (HDEVINFO)INVALID_HANDLE_VALUE;
1320 goto end;
1321 }
1322 MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
1323 }
1324 if (machine)
1325 {
1326 int len = MultiByteToWideChar(CP_ACP, 0, machine, -1, NULL, 0);
1327 machineW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1328 if (!machineW)
1329 {
1330 ret = (HDEVINFO)INVALID_HANDLE_VALUE;
1331 goto end;
1332 }
1333 MultiByteToWideChar(CP_ACP, 0, machine, -1, machineW, len);
1334 }
1335 ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, deviceset, machineW, reserved);
1336
1337 end:
1338 HeapFree(GetProcessHeap(), 0, enumstrW);
1339 HeapFree(GetProcessHeap(), 0, machineW);
1340 return ret;
1341 }
1342
1343 static BOOL
1344 CreateDeviceInfoElement(
1345 IN struct DeviceInfoSet *list,
1346 IN LPCWSTR InstancePath,
1347 IN LPCGUID pClassGuid,
1348 OUT struct DeviceInfoElement **pDeviceInfo)
1349 {
1350 DWORD size;
1351 CONFIGRET cr;
1352 struct DeviceInfoElement *deviceInfo;
1353
1354 *pDeviceInfo = NULL;
1355
1356 size = FIELD_OFFSET(struct DeviceInfoElement, Data) + (strlenW(InstancePath) + 1) * sizeof(WCHAR);
1357 deviceInfo = HeapAlloc(GetProcessHeap(), 0, size);
1358 if (!deviceInfo)
1359 {
1360 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1361 return FALSE;
1362 }
1363 memset(deviceInfo, 0, size);
1364
1365 cr = CM_Locate_DevNode_ExW(&deviceInfo->dnDevInst, (DEVINSTID_W)InstancePath, CM_LOCATE_DEVNODE_PHANTOM, list->hMachine);
1366 if (cr != CR_SUCCESS)
1367 {
1368 SetLastError(GetErrorCodeFromCrCode(cr));
1369 return FALSE;
1370 }
1371
1372 deviceInfo->InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
1373 strcpyW(deviceInfo->Data, InstancePath);
1374 deviceInfo->DeviceName = deviceInfo->Data;
1375 deviceInfo->UniqueId = strrchrW(deviceInfo->Data, '\\');
1376 deviceInfo->DeviceDescription = NULL;
1377 memcpy(&deviceInfo->ClassGuid, pClassGuid, sizeof(GUID));
1378 deviceInfo->CreationFlags = 0;
1379 InitializeListHead(&deviceInfo->DriverListHead);
1380 InitializeListHead(&deviceInfo->InterfaceListHead);
1381
1382 *pDeviceInfo = deviceInfo;
1383 return TRUE;
1384 }
1385
1386 static BOOL
1387 CreateDeviceInterface(
1388 IN struct DeviceInfoElement* deviceInfo,
1389 IN LPCWSTR SymbolicLink,
1390 IN LPCGUID pInterfaceGuid,
1391 OUT struct DeviceInterface **pDeviceInterface)
1392 {
1393 struct DeviceInterface *deviceInterface;
1394
1395 *pDeviceInterface = NULL;
1396
1397 deviceInterface = HeapAlloc(GetProcessHeap(), 0,
1398 FIELD_OFFSET(struct DeviceInterface, SymbolicLink) + (strlenW(SymbolicLink) + 1) * sizeof(WCHAR));
1399 if (!deviceInterface)
1400 {
1401 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1402 return FALSE;
1403 }
1404 deviceInterface->DeviceInfo = deviceInfo;
1405 strcpyW(deviceInterface->SymbolicLink, SymbolicLink);
1406 deviceInterface->Flags = 0; /* FIXME */
1407 memcpy(&deviceInterface->InterfaceClassGuid, pInterfaceGuid, sizeof(GUID));
1408
1409 *pDeviceInterface = deviceInterface;
1410 return TRUE;
1411 }
1412
1413 static LONG SETUP_CreateDevListFromEnumerator(
1414 struct DeviceInfoSet *list,
1415 LPCGUID pClassGuid OPTIONAL,
1416 LPCWSTR Enumerator,
1417 HKEY hEnumeratorKey) /* handle to Enumerator registry key */
1418 {
1419 HKEY hDeviceIdKey, hInstanceIdKey;
1420 WCHAR KeyBuffer[MAX_PATH];
1421 WCHAR InstancePath[MAX_PATH];
1422 LPWSTR pEndOfInstancePath; /* Pointer into InstancePath buffer */
1423 struct DeviceInfoElement *deviceInfo;
1424 DWORD i = 0, j;
1425 DWORD dwLength, dwRegType;
1426 DWORD rc;
1427
1428 /* Enumerate device IDs (subkeys of hEnumeratorKey) */
1429 while (TRUE)
1430 {
1431 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1432 rc = RegEnumKeyExW(hEnumeratorKey, i, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1433 if (rc == ERROR_NO_MORE_ITEMS)
1434 break;
1435 if (rc != ERROR_SUCCESS)
1436 return rc;
1437 i++;
1438
1439 /* Open device id sub key */
1440 rc = RegOpenKeyExW(hEnumeratorKey, KeyBuffer, 0, KEY_ENUMERATE_SUB_KEYS, &hDeviceIdKey);
1441 if (rc != ERROR_SUCCESS)
1442 return rc;
1443 strcpyW(InstancePath, Enumerator);
1444 strcatW(InstancePath, L"\\");
1445 strcatW(InstancePath, KeyBuffer);
1446 strcatW(InstancePath, L"\\");
1447 pEndOfInstancePath = &InstancePath[strlenW(InstancePath)];
1448
1449 /* Enumerate instance IDs (subkeys of hDeviceIdKey) */
1450 j = 0;
1451 while (TRUE)
1452 {
1453 GUID KeyGuid;
1454
1455 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1456 rc = RegEnumKeyExW(hDeviceIdKey, j, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1457 if (rc == ERROR_NO_MORE_ITEMS)
1458 break;
1459 if (rc != ERROR_SUCCESS)
1460 {
1461 RegCloseKey(hDeviceIdKey);
1462 return rc;
1463 }
1464 j++;
1465
1466 /* Open instance id sub key */
1467 rc = RegOpenKeyExW(hDeviceIdKey, KeyBuffer, 0, KEY_QUERY_VALUE, &hInstanceIdKey);
1468 if (rc != ERROR_SUCCESS)
1469 {
1470 RegCloseKey(hDeviceIdKey);
1471 return rc;
1472 }
1473 *pEndOfInstancePath = '\0';
1474 strcatW(InstancePath, KeyBuffer);
1475
1476 /* Read ClassGUID value */
1477 dwLength = sizeof(KeyBuffer) - sizeof(WCHAR);
1478 rc = RegQueryValueExW(hInstanceIdKey, ClassGUID, NULL, &dwRegType, (LPBYTE)KeyBuffer, &dwLength);
1479 RegCloseKey(hInstanceIdKey);
1480 if (rc == ERROR_FILE_NOT_FOUND)
1481 {
1482 if (pClassGuid)
1483 /* Skip this bad entry as we can't verify it */
1484 continue;
1485 /* Set a default GUID for this device */
1486 memcpy(&KeyGuid, &GUID_NULL, sizeof(GUID));
1487 }
1488 else if (rc != ERROR_SUCCESS)
1489 {
1490 RegCloseKey(hDeviceIdKey);
1491 return rc;
1492 }
1493 else if (dwRegType != REG_SZ)
1494 {
1495 RegCloseKey(hDeviceIdKey);
1496 return ERROR_GEN_FAILURE;
1497 }
1498 else
1499 {
1500 KeyBuffer[37] = '\0'; /* Replace the } by a NULL character */
1501 if (UuidFromStringW(&KeyBuffer[1], &KeyGuid) != RPC_S_OK)
1502 /* Bad GUID, skip the entry */
1503 continue;
1504 }
1505
1506 if (pClassGuid && !IsEqualIID(&KeyGuid, pClassGuid))
1507 {
1508 /* Skip this entry as it is not the right device class */
1509 continue;
1510 }
1511
1512 /* Add the entry to the list */
1513 if (!CreateDeviceInfoElement(list, InstancePath, &KeyGuid, &deviceInfo))
1514 {
1515 RegCloseKey(hDeviceIdKey);
1516 return GetLastError();
1517 }
1518 TRACE("Adding '%s' to device info set %p\n", debugstr_w(InstancePath), list);
1519 InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
1520 }
1521 RegCloseKey(hDeviceIdKey);
1522 }
1523
1524 return ERROR_SUCCESS;
1525 }
1526
1527 static LONG SETUP_CreateDevList(
1528 struct DeviceInfoSet *list,
1529 PCWSTR MachineName OPTIONAL,
1530 LPGUID class OPTIONAL,
1531 PCWSTR Enumerator OPTIONAL)
1532 {
1533 HKEY HKLM, hEnumKey, hEnumeratorKey;
1534 WCHAR KeyBuffer[MAX_PATH];
1535 DWORD i;
1536 DWORD dwLength;
1537 DWORD rc;
1538
1539 if (class && IsEqualIID(class, &GUID_NULL))
1540 class = NULL;
1541
1542 /* Open Enum key */
1543 if (MachineName != NULL)
1544 {
1545 rc = RegConnectRegistryW(MachineName, HKEY_LOCAL_MACHINE, &HKLM);
1546 if (rc != ERROR_SUCCESS)
1547 return rc;
1548 }
1549 else
1550 HKLM = HKEY_LOCAL_MACHINE;
1551
1552 rc = RegOpenKeyExW(HKLM,
1553 REGSTR_PATH_SYSTEMENUM,
1554 0,
1555 KEY_ENUMERATE_SUB_KEYS,
1556 &hEnumKey);
1557 if (MachineName != NULL) RegCloseKey(HKLM);
1558 if (rc != ERROR_SUCCESS)
1559 return rc;
1560
1561 /* If enumerator is provided, call directly SETUP_CreateDevListFromEnumerator.
1562 * Else, enumerate all enumerators and call SETUP_CreateDevListFromEnumerator
1563 * for each one.
1564 */
1565 if (Enumerator)
1566 {
1567 rc = RegOpenKeyExW(
1568 hEnumKey,
1569 Enumerator,
1570 0,
1571 KEY_ENUMERATE_SUB_KEYS,
1572 &hEnumeratorKey);
1573 RegCloseKey(hEnumKey);
1574 if (rc != ERROR_SUCCESS)
1575 return rc;
1576 rc = SETUP_CreateDevListFromEnumerator(list, class, Enumerator, hEnumeratorKey);
1577 RegCloseKey(hEnumeratorKey);
1578 return rc;
1579 }
1580 else
1581 {
1582 /* Enumerate enumerators */
1583 i = 0;
1584 while (TRUE)
1585 {
1586 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1587 rc = RegEnumKeyExW(hEnumKey, i, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1588 if (rc == ERROR_NO_MORE_ITEMS)
1589 break;
1590 if (rc != ERROR_SUCCESS)
1591 {
1592 RegCloseKey(hEnumKey);
1593 return rc;
1594 }
1595 i++;
1596
1597 /* Open sub key */
1598 rc = RegOpenKeyExW(hEnumKey, KeyBuffer, 0, KEY_ENUMERATE_SUB_KEYS, &hEnumeratorKey);
1599 if (rc != ERROR_SUCCESS)
1600 {
1601 RegCloseKey(hEnumKey);
1602 return rc;
1603 }
1604
1605 /* Call SETUP_CreateDevListFromEnumerator */
1606 rc = SETUP_CreateDevListFromEnumerator(list, class, KeyBuffer, hEnumeratorKey);
1607 RegCloseKey(hEnumeratorKey);
1608 if (rc != ERROR_SUCCESS)
1609 {
1610 RegCloseKey(hEnumKey);
1611 return rc;
1612 }
1613 }
1614 RegCloseKey(hEnumKey);
1615 return ERROR_SUCCESS;
1616 }
1617 }
1618
1619 static LONG SETUP_CreateInterfaceList(
1620 struct DeviceInfoSet *list,
1621 PCWSTR MachineName,
1622 LPGUID InterfaceGuid,
1623 PCWSTR DeviceInstanceW /* OPTIONAL */)
1624 {
1625 HKEY hInterfaceKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID} */
1626 HKEY hDeviceInstanceKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID}\##?#{InstancePath} */
1627 HKEY hReferenceKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID}\##?#{InstancePath}\#{ReferenceString} */
1628 HKEY hControlKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID}\##?#{InstancePath}\#{ReferenceString}\Control */
1629 HKEY hEnumKey; /* HKLM\SYSTEM\CurrentControlSet\Enum */
1630 HKEY hKey; /* HKLM\SYSTEM\CurrentControlSet\Enum\{Instance\Path} */
1631 LONG rc;
1632 WCHAR KeyBuffer[max(MAX_PATH, MAX_GUID_STRING_LEN) + 1];
1633 PWSTR InstancePath;
1634 DWORD i, j;
1635 DWORD dwLength, dwInstancePathLength;
1636 DWORD dwRegType;
1637 DWORD LinkedValue;
1638 GUID ClassGuid;
1639 struct DeviceInfoElement *deviceInfo;
1640
1641 /* Open registry key related to this interface */
1642 hInterfaceKey = SetupDiOpenClassRegKeyExW(InterfaceGuid, KEY_ENUMERATE_SUB_KEYS, DIOCR_INTERFACE, MachineName, NULL);
1643 if (hInterfaceKey == INVALID_HANDLE_VALUE)
1644 return GetLastError();
1645
1646 /* Enumerate sub keys of hInterfaceKey */
1647 i = 0;
1648 while (TRUE)
1649 {
1650 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1651 rc = RegEnumKeyExW(hInterfaceKey, i, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1652 if (rc == ERROR_NO_MORE_ITEMS)
1653 break;
1654 if (rc != ERROR_SUCCESS)
1655 {
1656 RegCloseKey(hInterfaceKey);
1657 return rc;
1658 }
1659 i++;
1660
1661 /* Open sub key */
1662 rc = RegOpenKeyExW(hInterfaceKey, KeyBuffer, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hDeviceInstanceKey);
1663 if (rc != ERROR_SUCCESS)
1664 {
1665 RegCloseKey(hInterfaceKey);
1666 return rc;
1667 }
1668
1669 /* Read DeviceInstance */
1670 rc = RegQueryValueExW(hDeviceInstanceKey, DeviceInstance, NULL, &dwRegType, NULL, &dwInstancePathLength);
1671 if (rc != ERROR_SUCCESS )
1672 {
1673 RegCloseKey(hDeviceInstanceKey);
1674 RegCloseKey(hInterfaceKey);
1675 return rc;
1676 }
1677 if (dwRegType != REG_SZ)
1678 {
1679 RegCloseKey(hDeviceInstanceKey);
1680 RegCloseKey(hInterfaceKey);
1681 return ERROR_GEN_FAILURE;
1682 }
1683 InstancePath = HeapAlloc(GetProcessHeap(), 0, dwInstancePathLength + sizeof(WCHAR));
1684 if (!InstancePath)
1685 {
1686 RegCloseKey(hDeviceInstanceKey);
1687 RegCloseKey(hInterfaceKey);
1688 return ERROR_NOT_ENOUGH_MEMORY;
1689 }
1690 rc = RegQueryValueExW(hDeviceInstanceKey, DeviceInstance, NULL, NULL, (LPBYTE)InstancePath, &dwInstancePathLength);
1691 if (rc != ERROR_SUCCESS)
1692 {
1693 HeapFree(GetProcessHeap(), 0, InstancePath);
1694 RegCloseKey(hDeviceInstanceKey);
1695 RegCloseKey(hInterfaceKey);
1696 return rc;
1697 }
1698 InstancePath[dwInstancePathLength / sizeof(WCHAR)] = '\0';
1699 TRACE("DeviceInstance %s\n", debugstr_w(InstancePath));
1700
1701 if (DeviceInstanceW)
1702 {
1703 /* Check if device enumerator is not the right one */
1704 if (strcmpW(DeviceInstanceW, InstancePath) != 0)
1705 {
1706 HeapFree(GetProcessHeap(), 0, InstancePath);
1707 RegCloseKey(hDeviceInstanceKey);
1708 continue;
1709 }
1710 }
1711
1712 /* Find class GUID associated to the device instance */
1713 rc = RegOpenKeyExW(
1714 list->HKLM,
1715 REGSTR_PATH_SYSTEMENUM,
1716 0, /* Options */
1717 0,
1718 &hEnumKey);
1719 if (rc != ERROR_SUCCESS)
1720 {
1721 HeapFree(GetProcessHeap(), 0, InstancePath);
1722 RegCloseKey(hDeviceInstanceKey);
1723 RegCloseKey(hInterfaceKey);
1724 return rc;
1725 }
1726 rc = RegOpenKeyExW(
1727 hEnumKey,
1728 InstancePath,
1729 0, /* Options */
1730 KEY_QUERY_VALUE,
1731 &hKey);
1732 RegCloseKey(hEnumKey);
1733 if (rc != ERROR_SUCCESS)
1734 {
1735 HeapFree(GetProcessHeap(), 0, InstancePath);
1736 RegCloseKey(hDeviceInstanceKey);
1737 RegCloseKey(hInterfaceKey);
1738 return rc;
1739 }
1740 dwLength = sizeof(KeyBuffer) - sizeof(WCHAR);
1741 rc = RegQueryValueExW(hKey, ClassGUID, NULL, NULL, (LPBYTE)KeyBuffer, &dwLength);
1742 RegCloseKey(hKey);
1743 if (rc != ERROR_SUCCESS)
1744 {
1745 HeapFree(GetProcessHeap(), 0, InstancePath);
1746 RegCloseKey(hDeviceInstanceKey);
1747 RegCloseKey(hInterfaceKey);
1748 return rc;
1749 }
1750 KeyBuffer[dwLength / sizeof(WCHAR)] = '\0';
1751 KeyBuffer[37] = '\0'; /* Replace the } by a NULL character */
1752 if (UuidFromStringW(&KeyBuffer[1], &ClassGuid) != RPC_S_OK)
1753 {
1754 HeapFree(GetProcessHeap(), 0, InstancePath);
1755 RegCloseKey(hDeviceInstanceKey);
1756 RegCloseKey(hInterfaceKey);
1757 return ERROR_GEN_FAILURE;
1758 }
1759 TRACE("ClassGUID %s\n", debugstr_guid(&ClassGuid));
1760
1761 /* If current device doesn't match the list GUID (if any), skip this entry */
1762 if (!IsEqualIID(&list->ClassGuid, &GUID_NULL) && !IsEqualIID(&list->ClassGuid, &ClassGuid))
1763 {
1764 HeapFree(GetProcessHeap(), 0, InstancePath);
1765 RegCloseKey(hDeviceInstanceKey);
1766 continue;
1767 }
1768
1769 /* Enumerate subkeys of hDeviceInstanceKey (ie "#ReferenceString" in IoRegisterDeviceInterface). Skip entries that don't start with '#' */
1770 j = 0;
1771 while (TRUE)
1772 {
1773 LPWSTR pSymbolicLink;
1774 struct DeviceInterface *interfaceInfo;
1775
1776 dwLength = sizeof(KeyBuffer) / sizeof(KeyBuffer[0]);
1777 rc = RegEnumKeyExW(hDeviceInstanceKey, j, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
1778 if (rc == ERROR_NO_MORE_ITEMS)
1779 break;
1780 if (rc != ERROR_SUCCESS)
1781 {
1782 HeapFree(GetProcessHeap(), 0, InstancePath);
1783 RegCloseKey(hDeviceInstanceKey);
1784 RegCloseKey(hInterfaceKey);
1785 return rc;
1786 }
1787 j++;
1788 if (KeyBuffer[0] != '#')
1789 /* This entry doesn't represent an interesting entry */
1790 continue;
1791
1792 /* Open sub key */
1793 rc = RegOpenKeyExW(hDeviceInstanceKey, KeyBuffer, 0, KEY_QUERY_VALUE, &hReferenceKey);
1794 if (rc != ERROR_SUCCESS)
1795 {
1796 RegCloseKey(hDeviceInstanceKey);
1797 RegCloseKey(hInterfaceKey);
1798 return rc;
1799 }
1800
1801 /* Read SymbolicLink value */
1802 rc = RegQueryValueExW(hReferenceKey, SymbolicLink, NULL, &dwRegType, NULL, &dwLength);
1803 if (rc != ERROR_SUCCESS )
1804 {
1805 RegCloseKey(hReferenceKey);
1806 RegCloseKey(hDeviceInstanceKey);
1807 RegCloseKey(hInterfaceKey);
1808 return rc;
1809 }
1810 if (dwRegType != REG_SZ)
1811 {
1812 RegCloseKey(hReferenceKey);
1813 RegCloseKey(hDeviceInstanceKey);
1814 RegCloseKey(hInterfaceKey);
1815 return ERROR_GEN_FAILURE;
1816 }
1817
1818 /* We have found a device */
1819 /* Step 1. Create a device info element */
1820 if (!CreateDeviceInfoElement(list, InstancePath, &ClassGuid, &deviceInfo))
1821 {
1822 RegCloseKey(hReferenceKey);
1823 RegCloseKey(hDeviceInstanceKey);
1824 RegCloseKey(hInterfaceKey);
1825 return GetLastError();
1826 }
1827 TRACE("Adding device %s to list\n", debugstr_w(InstancePath));
1828 InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
1829
1830 /* Step 2. Create an interface list for this element */
1831 pSymbolicLink = HeapAlloc(GetProcessHeap(), 0, (dwLength + 1) * sizeof(WCHAR));
1832 if (!pSymbolicLink)
1833 {
1834 RegCloseKey(hReferenceKey);
1835 RegCloseKey(hDeviceInstanceKey);
1836 RegCloseKey(hInterfaceKey);
1837 return ERROR_NOT_ENOUGH_MEMORY;
1838 }
1839 rc = RegQueryValueExW(hReferenceKey, SymbolicLink, NULL, NULL, (LPBYTE)pSymbolicLink, &dwLength);
1840 pSymbolicLink[dwLength / sizeof(WCHAR)] = '\0';
1841 RegCloseKey(hReferenceKey);
1842 if (rc != ERROR_SUCCESS)
1843 {
1844 HeapFree(GetProcessHeap(), 0, pSymbolicLink);
1845 RegCloseKey(hDeviceInstanceKey);
1846 RegCloseKey(hInterfaceKey);
1847 return rc;
1848 }
1849 if (!CreateDeviceInterface(deviceInfo, pSymbolicLink, InterfaceGuid, &interfaceInfo))
1850 {
1851 HeapFree(GetProcessHeap(), 0, pSymbolicLink);
1852 RegCloseKey(hDeviceInstanceKey);
1853 RegCloseKey(hInterfaceKey);
1854 return GetLastError();
1855 }
1856
1857 /* Step 3. Update flags */
1858 if (KeyBuffer[1] == '\0')
1859 interfaceInfo->Flags |= SPINT_DEFAULT;
1860 rc = RegOpenKeyExW(hReferenceKey, Control, 0, KEY_QUERY_VALUE, &hControlKey);
1861 if (!rc)
1862 interfaceInfo->Flags |= SPINT_REMOVED;
1863 else
1864 {
1865 dwLength = sizeof(DWORD);
1866 if (RegQueryValueExW(hControlKey, Linked, NULL, &dwRegType, (LPBYTE)&LinkedValue, &dwLength)
1867 && dwRegType == REG_DWORD && LinkedValue)
1868 interfaceInfo->Flags |= SPINT_ACTIVE;
1869 RegCloseKey(hControlKey);
1870 }
1871
1872 TRACE("Adding interface %s to list\n", debugstr_w(pSymbolicLink));
1873 HeapFree(GetProcessHeap(), 0, pSymbolicLink);
1874 InsertTailList(&deviceInfo->InterfaceListHead, &interfaceInfo->ListEntry);
1875 }
1876 RegCloseKey(hDeviceInstanceKey);
1877 }
1878 RegCloseKey(hInterfaceKey);
1879 return ERROR_SUCCESS;
1880 }
1881
1882 /***********************************************************************
1883 * SetupDiGetClassDevsExW (SETUPAPI.@)
1884 */
1885 HDEVINFO WINAPI SetupDiGetClassDevsExW(
1886 CONST GUID *class,
1887 LPCWSTR enumstr,
1888 HWND parent,
1889 DWORD flags,
1890 HDEVINFO deviceset,
1891 LPCWSTR machine,
1892 PVOID reserved)
1893 {
1894 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
1895 struct DeviceInfoSet *list;
1896 LPGUID pClassGuid;
1897 LONG rc;
1898
1899 TRACE("%s %s %p 0x%08lx %p %s %p\n", debugstr_guid(class), debugstr_w(enumstr),
1900 parent, flags, deviceset, debugstr_w(machine), reserved);
1901
1902 /* Create the deviceset if not set */
1903 if (deviceset)
1904 {
1905 list = (struct DeviceInfoSet *)deviceset;
1906 if (list->magic != SETUP_DEV_INFO_SET_MAGIC)
1907 {
1908 SetLastError(ERROR_INVALID_HANDLE);
1909 return INVALID_HANDLE_VALUE;
1910 }
1911 hDeviceInfo = deviceset;
1912 }
1913 else
1914 {
1915 hDeviceInfo = SetupDiCreateDeviceInfoListExW(
1916 flags & DIGCF_DEVICEINTERFACE ? NULL : class,
1917 NULL, machine, NULL);
1918 if (hDeviceInfo == INVALID_HANDLE_VALUE)
1919 return INVALID_HANDLE_VALUE;
1920 list = (struct DeviceInfoSet *)hDeviceInfo;
1921 }
1922
1923 if (IsEqualIID(&list->ClassGuid, &GUID_NULL))
1924 pClassGuid = NULL;
1925 else
1926 pClassGuid = &list->ClassGuid;
1927
1928 if (flags & DIGCF_PRESENT)
1929 FIXME(": flag DIGCF_PRESENT ignored\n");
1930 if (flags & DIGCF_PROFILE)
1931 FIXME(": flag DIGCF_PROFILE ignored\n");
1932
1933 if (flags & DIGCF_ALLCLASSES)
1934 {
1935 rc = SETUP_CreateDevList(list, machine, pClassGuid, enumstr);
1936 if (rc != ERROR_SUCCESS)
1937 {
1938 SetLastError(rc);
1939 if (!deviceset)
1940 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1941 return INVALID_HANDLE_VALUE;
1942 }
1943 return hDeviceInfo;
1944 }
1945 else if (flags & DIGCF_DEVICEINTERFACE)
1946 {
1947 if (class == NULL)
1948 {
1949 SetLastError(ERROR_INVALID_PARAMETER);
1950 if (!deviceset)
1951 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1952 return INVALID_HANDLE_VALUE;
1953 }
1954
1955 rc = SETUP_CreateInterfaceList(list, machine, (LPGUID)class, enumstr);
1956 if (rc != ERROR_SUCCESS)
1957 {
1958 SetLastError(rc);
1959 if (!deviceset)
1960 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1961 return INVALID_HANDLE_VALUE;
1962 }
1963 return hDeviceInfo;
1964 }
1965 else
1966 {
1967 rc = SETUP_CreateDevList(list, machine, (LPGUID)class, enumstr);
1968 if (rc != ERROR_SUCCESS)
1969 {
1970 SetLastError(rc);
1971 if (!deviceset)
1972 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1973 return INVALID_HANDLE_VALUE;
1974 }
1975 return hDeviceInfo;
1976 }
1977 }
1978
1979 /***********************************************************************
1980 * SetupDiGetClassImageIndex (SETUPAPI.@)
1981 */
1982
1983 static BOOL GetIconIndex(
1984 IN HKEY hClassKey,
1985 OUT PINT ImageIndex)
1986 {
1987 LPWSTR Buffer = NULL;
1988 DWORD dwRegType, dwLength;
1989 LONG rc;
1990 BOOL ret = FALSE;
1991
1992 /* Read icon registry key */
1993 rc = RegQueryValueExW(hClassKey, REGSTR_VAL_INSICON, NULL, &dwRegType, NULL, &dwLength);
1994 if (rc != ERROR_SUCCESS)
1995 {
1996 SetLastError(rc);
1997 goto cleanup;
1998 } else if (dwRegType != REG_SZ)
1999 {
2000 SetLastError(ERROR_INVALID_INDEX);
2001 goto cleanup;
2002 }
2003 Buffer = MyMalloc(dwLength + sizeof(WCHAR));
2004 if (!Buffer)
2005 {
2006 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2007 goto cleanup;
2008 }
2009 rc = RegQueryValueExW(hClassKey, REGSTR_VAL_INSICON, NULL, NULL, (LPBYTE)Buffer, &dwLength);
2010 if (rc != ERROR_SUCCESS)
2011 {
2012 SetLastError(rc);
2013 goto cleanup;
2014 }
2015 /* make sure the returned buffer is NULL-terminated */
2016 Buffer[dwLength / sizeof(WCHAR)] = 0;
2017
2018 /* Transform icon value to a INT */
2019 *ImageIndex = atoiW(Buffer);
2020 ret = TRUE;
2021
2022 cleanup:
2023 MyFree(Buffer);
2024 return ret;
2025 }
2026
2027 BOOL WINAPI SetupDiGetClassImageIndex(
2028 IN PSP_CLASSIMAGELIST_DATA ClassImageListData,
2029 IN CONST GUID *ClassGuid,
2030 OUT PINT ImageIndex)
2031 {
2032 struct ClassImageList *list;
2033 BOOL ret = FALSE;
2034
2035 TRACE("%p %s %p\n", ClassImageListData, debugstr_guid(ClassGuid), ImageIndex);
2036
2037 if (!ClassImageListData || !ClassGuid || !ImageIndex)
2038 SetLastError(ERROR_INVALID_PARAMETER);
2039 else if (ClassImageListData->cbSize != sizeof(SP_CLASSIMAGELIST_DATA))
2040 SetLastError(ERROR_INVALID_USER_BUFFER);
2041 else if ((list = (struct ClassImageList *)ClassImageListData->Reserved) == NULL)
2042 SetLastError(ERROR_INVALID_USER_BUFFER);
2043 else if (list->magic != SETUP_CLASS_IMAGE_LIST_MAGIC)
2044 SetLastError(ERROR_INVALID_USER_BUFFER);
2045 else if (!ImageIndex)
2046 SetLastError(ERROR_INVALID_PARAMETER);
2047 else
2048 {
2049 HKEY hKey = INVALID_HANDLE_VALUE;
2050 INT iconIndex;
2051
2052 /* Read Icon registry entry into Buffer */
2053 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, KEY_QUERY_VALUE, DIOCR_INTERFACE, list->MachineName, NULL);
2054 if (hKey == INVALID_HANDLE_VALUE)
2055 goto cleanup;
2056 if (!GetIconIndex(hKey, &iconIndex))
2057 goto cleanup;
2058
2059 if (iconIndex >= 0)
2060 {
2061 SetLastError(ERROR_INVALID_INDEX);
2062 goto cleanup;
2063 }
2064
2065 *ImageIndex = -iconIndex;
2066 ret = TRUE;
2067
2068 cleanup:
2069 if (hKey != INVALID_HANDLE_VALUE)
2070 RegCloseKey(hKey);
2071 }
2072
2073 TRACE("Returning %d\n", ret);
2074 return ret;
2075 }
2076
2077 /***********************************************************************
2078 * SetupDiGetClassImageList(SETUPAPI.@)
2079 */
2080 BOOL WINAPI SetupDiGetClassImageList(
2081 OUT PSP_CLASSIMAGELIST_DATA ClassImageListData)
2082 {
2083 return SetupDiGetClassImageListExW(ClassImageListData, NULL, NULL);
2084 }
2085
2086 /***********************************************************************
2087 * SetupDiGetClassImageListExA(SETUPAPI.@)
2088 */
2089 BOOL WINAPI SetupDiGetClassImageListExA(
2090 OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
2091 IN PCSTR MachineName OPTIONAL,
2092 IN PVOID Reserved)
2093 {
2094 PWSTR MachineNameW = NULL;
2095 BOOL ret;
2096
2097 if (MachineName)
2098 {
2099 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
2100 if (MachineNameW == NULL)
2101 return FALSE;
2102 }
2103
2104 ret = SetupDiGetClassImageListExW(ClassImageListData, MachineNameW, Reserved);
2105
2106 if (MachineNameW)
2107 MyFree(MachineNameW);
2108
2109 return ret;
2110 }
2111
2112 /***********************************************************************
2113 * SetupDiGetClassImageListExW(SETUPAPI.@)
2114 */
2115 BOOL WINAPI SetupDiGetClassImageListExW(
2116 OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
2117 IN PCWSTR MachineName OPTIONAL,
2118 IN PVOID Reserved)
2119 {
2120 BOOL ret = FALSE;
2121
2122 TRACE("%p %p %p\n", ClassImageListData, debugstr_w(MachineName), Reserved);
2123
2124 if (!ClassImageListData)
2125 SetLastError(ERROR_INVALID_PARAMETER);
2126 else if (ClassImageListData->cbSize != sizeof(SP_CLASSIMAGELIST_DATA))
2127 SetLastError(ERROR_INVALID_USER_BUFFER);
2128 else if (Reserved)
2129 SetLastError(ERROR_INVALID_PARAMETER);
2130 else
2131 {
2132 struct ClassImageList *list = NULL;
2133 DWORD size;
2134
2135 size = FIELD_OFFSET(struct ClassImageList, szData);
2136 if (MachineName)
2137 size += (strlenW(MachineName) + 3) * sizeof(WCHAR);
2138 list = HeapAlloc(GetProcessHeap(), 0, size);
2139 if (!list)
2140 {
2141 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2142 goto cleanup;
2143 }
2144 list->magic = SETUP_CLASS_IMAGE_LIST_MAGIC;
2145 if (MachineName)
2146 {
2147 list->szData[0] = list->szData[1] = '\\';
2148 strcpyW(list->szData + 2, MachineName);
2149 list->MachineName = list->szData;
2150 }
2151 else
2152 {
2153 list->MachineName = NULL;
2154 }
2155
2156 ClassImageListData->Reserved = (DWORD)list; /* FIXME: 64 bit portability issue */
2157 ret = TRUE;
2158
2159 cleanup:
2160 if (!ret)
2161 MyFree(list);
2162 }
2163
2164 TRACE("Returning %d\n", ret);
2165 return ret;
2166 }
2167
2168 /***********************************************************************
2169 * SetupDiLoadClassIcon(SETUPAPI.@)
2170 */
2171 BOOL WINAPI SetupDiLoadClassIcon(
2172 IN CONST GUID *ClassGuid,
2173 OUT HICON *LargeIcon OPTIONAL,
2174 OUT PINT MiniIconIndex OPTIONAL)
2175 {
2176 BOOL ret = FALSE;
2177
2178 if (!ClassGuid)
2179 SetLastError(ERROR_INVALID_PARAMETER);
2180 else
2181 {
2182 LPWSTR Buffer = NULL;
2183 LPCWSTR DllName;
2184 INT iconIndex;
2185 HKEY hKey = INVALID_HANDLE_VALUE;
2186
2187 hKey = SetupDiOpenClassRegKey(ClassGuid, KEY_QUERY_VALUE);
2188 if (hKey == INVALID_HANDLE_VALUE)
2189 goto cleanup;
2190
2191 if (!GetIconIndex(hKey, &iconIndex))
2192 goto cleanup;
2193
2194 if (iconIndex > 0)
2195 {
2196 /* Look up icon in dll specified by Installer32 or EnumPropPages32 key */
2197 PWCHAR Comma;
2198 LONG rc;
2199 DWORD dwRegType, dwLength;
2200 rc = RegQueryValueExW(hKey, REGSTR_VAL_INSTALLER_32, NULL, &dwRegType, NULL, &dwLength);
2201 if (rc == ERROR_SUCCESS && dwRegType == REG_SZ)
2202 {
2203 Buffer = MyMalloc(dwLength + sizeof(WCHAR));
2204 if (Buffer == NULL)
2205 {
2206 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2207 goto cleanup;
2208 }
2209 rc = RegQueryValueExW(hKey, REGSTR_VAL_INSTALLER_32, NULL, NULL, (LPBYTE)Buffer, &dwLength);
2210 if (rc != ERROR_SUCCESS)
2211 {
2212 SetLastError(rc);
2213 goto cleanup;
2214 }
2215 /* make sure the returned buffer is NULL-terminated */
2216 Buffer[dwLength / sizeof(WCHAR)] = 0;
2217 }
2218 else if
2219 (ERROR_SUCCESS == (rc = RegQueryValueExW(hKey, REGSTR_VAL_ENUMPROPPAGES_32, NULL, &dwRegType, NULL, &dwLength))
2220 && dwRegType == REG_SZ)
2221 {
2222 Buffer = MyMalloc(dwLength + sizeof(WCHAR));
2223 if (Buffer == NULL)
2224 {
2225 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2226 goto cleanup;
2227 }
2228 rc = RegQueryValueExW(hKey, REGSTR_VAL_ENUMPROPPAGES_32, NULL, NULL, (LPBYTE)Buffer, &dwLength);
2229 if (rc != ERROR_SUCCESS)
2230 {
2231 SetLastError(rc);
2232 goto cleanup;
2233 }
2234 /* make sure the returned buffer is NULL-terminated */
2235 Buffer[dwLength / sizeof(WCHAR)] = 0;
2236 }
2237 else
2238 {
2239 /* Unable to find where to load the icon */
2240 SetLastError(ERROR_FILE_NOT_FOUND);
2241 goto cleanup;
2242 }
2243 Comma = strchrW(Buffer, ',');
2244 if (!Comma)
2245 {
2246 SetLastError(ERROR_GEN_FAILURE);
2247 goto cleanup;
2248 }
2249 *Comma = '\0';
2250 DllName = Buffer;
2251 }
2252 else
2253 {
2254 /* Look up icon in setupapi.dll */
2255 DllName = L"setupapi.dll";
2256 iconIndex = -iconIndex;
2257 }
2258
2259 TRACE("Icon index %d, dll name %s\n", iconIndex, debugstr_w(DllName));
2260 if (LargeIcon)
2261 {
2262 if (1 != ExtractIconEx(DllName, iconIndex, LargeIcon, NULL, 1))
2263 {
2264 SetLastError(ERROR_INVALID_INDEX);
2265 goto cleanup;
2266 }
2267 }
2268 if (MiniIconIndex)
2269 *MiniIconIndex = iconIndex;
2270 ret = TRUE;
2271
2272 cleanup:
2273 if (hKey != INVALID_HANDLE_VALUE)
2274 RegCloseKey(hKey);
2275 MyFree(Buffer);
2276 }
2277
2278 TRACE("Returning %d\n", ret);
2279 return ret;
2280 }
2281
2282 /***********************************************************************
2283 * SetupDiEnumDeviceInterfaces (SETUPAPI.@)
2284 */
2285 BOOL WINAPI SetupDiEnumDeviceInterfaces(
2286 HDEVINFO DeviceInfoSet,
2287 PSP_DEVINFO_DATA DeviceInfoData,
2288 CONST GUID * InterfaceClassGuid,
2289 DWORD MemberIndex,
2290 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
2291 {
2292 BOOL ret = FALSE;
2293
2294 TRACE("%p, %p, %s, %ld, %p\n", DeviceInfoSet, DeviceInfoData,
2295 debugstr_guid(InterfaceClassGuid), MemberIndex, DeviceInterfaceData);
2296
2297 if (!DeviceInterfaceData)
2298 SetLastError(ERROR_INVALID_PARAMETER);
2299 else if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2300 SetLastError(ERROR_INVALID_USER_BUFFER);
2301 else if (DeviceInfoSet && DeviceInfoSet != (HDEVINFO)INVALID_HANDLE_VALUE)
2302 {
2303 struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
2304
2305 if (list->magic == SETUP_DEV_INFO_SET_MAGIC)
2306 {
2307 PLIST_ENTRY ItemList = list->ListHead.Flink;
2308 BOOL Found = FALSE;
2309 while (ItemList != &list->ListHead && !Found)
2310 {
2311 PLIST_ENTRY InterfaceListEntry;
2312 struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)ItemList;
2313 if (DeviceInfoData && (struct DeviceInfoElement *)DeviceInfoData->Reserved != DevInfo)
2314 {
2315 /* We are not searching for this element */
2316 ItemList = ItemList->Flink;
2317 continue;
2318 }
2319 InterfaceListEntry = DevInfo->InterfaceListHead.Flink;
2320 while (InterfaceListEntry != &DevInfo->InterfaceListHead && !Found)
2321 {
2322 struct DeviceInterface *DevItf = (struct DeviceInterface *)InterfaceListEntry;
2323 if (!IsEqualIID(&DevItf->InterfaceClassGuid, InterfaceClassGuid))
2324 {
2325 InterfaceListEntry = InterfaceListEntry->Flink;
2326 continue;
2327 }
2328 if (MemberIndex-- == 0)
2329 {
2330 /* return this item */
2331 memcpy(&DeviceInterfaceData->InterfaceClassGuid,
2332 &DevItf->InterfaceClassGuid,
2333 sizeof(GUID));
2334 DeviceInterfaceData->Flags = DevItf->Flags;
2335 DeviceInterfaceData->Reserved = (ULONG_PTR)DevItf;
2336 Found = TRUE;
2337 }
2338 InterfaceListEntry = InterfaceListEntry->Flink;
2339 }
2340 ItemList = ItemList->Flink;
2341 }
2342 if (!Found)
2343 SetLastError(ERROR_NO_MORE_ITEMS);
2344 else
2345 ret = TRUE;
2346 }
2347 else
2348 SetLastError(ERROR_INVALID_HANDLE);
2349 }
2350 else
2351 SetLastError(ERROR_INVALID_HANDLE);
2352 return ret;
2353 }
2354
2355 static VOID ReferenceInfFile(struct InfFileDetails* infFile)
2356 {
2357 InterlockedIncrement(&infFile->References);
2358 }
2359
2360 static VOID DereferenceInfFile(struct InfFileDetails* infFile)
2361 {
2362 if (InterlockedDecrement(&infFile->References) == 0)
2363 {
2364 SetupCloseInfFile(infFile->hInf);
2365 HeapFree(GetProcessHeap(), 0, infFile);
2366 }
2367 }
2368
2369 static BOOL DestroyDriverInfoElement(struct DriverInfoElement* driverInfo)
2370 {
2371 DereferenceInfFile(driverInfo->InfFileDetails);
2372 HeapFree(GetProcessHeap(), 0, driverInfo->MatchingId);
2373 HeapFree(GetProcessHeap(), 0, driverInfo);
2374 return TRUE;
2375 }
2376
2377 static BOOL DestroyClassInstallParams(struct ClassInstallParams* installParams)
2378 {
2379 HeapFree(GetProcessHeap(), 0, installParams->PropChange);
2380 return TRUE;
2381 }
2382
2383 static BOOL DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
2384 {
2385 PLIST_ENTRY ListEntry;
2386 struct DriverInfoElement *driverInfo;
2387
2388 while (!IsListEmpty(&deviceInfo->DriverListHead))
2389 {
2390 ListEntry = RemoveHeadList(&deviceInfo->DriverListHead);
2391 driverInfo = (struct DriverInfoElement *)ListEntry;
2392 if (!DestroyDriverInfoElement(driverInfo))
2393 return FALSE;
2394 }
2395 while (!IsListEmpty(&deviceInfo->InterfaceListHead))
2396 {
2397 ListEntry = RemoveHeadList(&deviceInfo->InterfaceListHead);
2398 HeapFree(GetProcessHeap(), 0, ListEntry);
2399 }
2400 DestroyClassInstallParams(&deviceInfo->ClassInstallParams);
2401 HeapFree(GetProcessHeap(), 0, deviceInfo);
2402 return TRUE;
2403 }
2404
2405 static BOOL DestroyDeviceInfoSet(struct DeviceInfoSet* list)
2406 {
2407 PLIST_ENTRY ListEntry;
2408 struct DeviceInfoElement *deviceInfo;
2409
2410 while (!IsListEmpty(&list->ListHead))
2411 {
2412 ListEntry = RemoveHeadList(&list->ListHead);
2413 deviceInfo = (struct DeviceInfoElement *)ListEntry;
2414 if (!DestroyDeviceInfoElement(deviceInfo))
2415 return FALSE;
2416 }
2417 if (list->HKLM != HKEY_LOCAL_MACHINE)
2418 RegCloseKey(list->HKLM);
2419 CM_Disconnect_Machine(list->hMachine);
2420 DestroyClassInstallParams(&list->ClassInstallParams);
2421 HeapFree(GetProcessHeap(), 0, list);
2422 return TRUE;
2423 }
2424
2425 /***********************************************************************
2426 * SetupDiDestroyDeviceInfoList (SETUPAPI.@)
2427 */
2428 BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
2429 {
2430 BOOL ret = FALSE;
2431
2432 TRACE("%p\n", devinfo);
2433 if (devinfo && devinfo != (HDEVINFO)INVALID_HANDLE_VALUE)
2434 {
2435 struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
2436
2437 if (list->magic == SETUP_DEV_INFO_SET_MAGIC)
2438 ret = DestroyDeviceInfoSet(list);
2439 else
2440 SetLastError(ERROR_INVALID_HANDLE);
2441 }
2442 else
2443 SetLastError(ERROR_INVALID_HANDLE);
2444
2445 TRACE("Returning %d\n", ret);
2446 return ret;
2447 }
2448
2449 /***********************************************************************
2450 * SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
2451 */
2452 BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
2453 HDEVINFO DeviceInfoSet,
2454 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2455 PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
2456 DWORD DeviceInterfaceDetailDataSize,
2457 PDWORD RequiredSize,
2458 PSP_DEVINFO_DATA DeviceInfoData)
2459 {
2460 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailDataW = NULL;
2461 DWORD sizeW = 0, sizeA;
2462 BOOL ret = FALSE;
2463
2464 TRACE("%p %p %p %lu %p %p\n", DeviceInfoSet,
2465 DeviceInterfaceData, DeviceInterfaceDetailData,
2466 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2467
2468 if (DeviceInterfaceDetailData->cbSize != sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A))
2469 SetLastError(ERROR_INVALID_USER_BUFFER);
2470 else if (DeviceInterfaceDetailData == NULL && DeviceInterfaceDetailDataSize != 0)
2471 SetLastError(ERROR_INVALID_PARAMETER);
2472 else if (DeviceInterfaceDetailData != NULL && DeviceInterfaceDetailDataSize < FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath) + 1)
2473 SetLastError(ERROR_INVALID_PARAMETER);
2474 else
2475 {
2476 if (DeviceInterfaceDetailData != NULL)
2477 {
2478 sizeW = FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)
2479 + (DeviceInterfaceDetailDataSize - FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath)) * sizeof(WCHAR);
2480 DeviceInterfaceDetailDataW = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)HeapAlloc(GetProcessHeap(), 0, sizeW);
2481 if (!DeviceInterfaceDetailDataW)
2482 {
2483 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2484 }
2485 }
2486 if (!DeviceInterfaceDetailData || (DeviceInterfaceDetailData && DeviceInterfaceDetailDataW))
2487 {
2488 DeviceInterfaceDetailDataW->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
2489 ret = SetupDiGetDeviceInterfaceDetailW(
2490 DeviceInfoSet,
2491 DeviceInterfaceData,
2492 DeviceInterfaceDetailDataW,
2493 sizeW,
2494 &sizeW,
2495 DeviceInfoData);
2496 sizeA = (sizeW - FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath)) / sizeof(WCHAR)
2497 + FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath);
2498 if (RequiredSize)
2499 *RequiredSize = sizeA;
2500 if (ret && DeviceInterfaceDetailData && DeviceInterfaceDetailDataSize <= sizeA)
2501 {
2502 if (!WideCharToMultiByte(
2503 CP_ACP, 0,
2504 DeviceInterfaceDetailDataW->DevicePath, -1,
2505 DeviceInterfaceDetailData->DevicePath, DeviceInterfaceDetailDataSize - FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_A, DevicePath),
2506 NULL, NULL))
2507 {
2508 ret = FALSE;
2509 }
2510 }
2511 }
2512 HeapFree(GetProcessHeap(), 0, DeviceInterfaceDetailDataW);
2513 }
2514
2515 TRACE("Returning %d\n", ret);
2516 return ret;
2517 }
2518
2519 /***********************************************************************
2520 * SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
2521 */
2522 BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
2523 HDEVINFO DeviceInfoSet,
2524 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
2525 PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
2526 DWORD DeviceInterfaceDetailDataSize,
2527 PDWORD RequiredSize,
2528 PSP_DEVINFO_DATA DeviceInfoData)
2529 {
2530 BOOL ret = FALSE;
2531
2532 TRACE("%p %p %p %lu %p %p\n", DeviceInfoSet,
2533 DeviceInterfaceData, DeviceInterfaceDetailData,
2534 DeviceInterfaceDetailDataSize, RequiredSize, DeviceInfoData);
2535
2536 if (!DeviceInfoSet || !DeviceInterfaceData)
2537 SetLastError(ERROR_INVALID_PARAMETER);
2538 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2539 SetLastError(ERROR_INVALID_HANDLE);
2540 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
2541 SetLastError(ERROR_INVALID_HANDLE);
2542 else if (DeviceInterfaceData->cbSize != sizeof(SP_DEVICE_INTERFACE_DATA))
2543 SetLastError(ERROR_INVALID_USER_BUFFER);
2544 else if (DeviceInterfaceDetailData->cbSize != sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W))
2545 SetLastError(ERROR_INVALID_USER_BUFFER);
2546 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
2547 SetLastError(ERROR_INVALID_USER_BUFFER);
2548 else if (DeviceInterfaceDetailData == NULL && DeviceInterfaceDetailDataSize != 0)
2549 SetLastError(ERROR_INVALID_PARAMETER);
2550 else if (DeviceInterfaceDetailData != NULL && DeviceInterfaceDetailDataSize < FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR))
2551 SetLastError(ERROR_INVALID_PARAMETER);
2552 else
2553 {
2554 struct DeviceInterface *deviceInterface = (struct DeviceInterface *)DeviceInterfaceData->Reserved;
2555 LPCWSTR devName = deviceInterface->SymbolicLink;
2556 DWORD sizeRequired = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W) +
2557 (lstrlenW(devName) + 1) * sizeof(WCHAR);
2558
2559 if (sizeRequired > DeviceInterfaceDetailDataSize)
2560 {
2561 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2562 if (RequiredSize)
2563 *RequiredSize = sizeRequired;
2564 }
2565 else
2566 {
2567 strcpyW(DeviceInterfaceDetailData->DevicePath, devName);
2568 TRACE("DevicePath is %s\n", debugstr_w(DeviceInterfaceDetailData->DevicePath));
2569 if (DeviceInfoData)
2570 {
2571 memcpy(&DeviceInfoData->ClassGuid,
2572 &deviceInterface->DeviceInfo->ClassGuid,
2573 sizeof(GUID));
2574 DeviceInfoData->DevInst = deviceInterface->DeviceInfo->dnDevInst;
2575 DeviceInfoData->Reserved = (ULONG_PTR)deviceInterface->DeviceInfo;
2576 }
2577 ret = TRUE;
2578 }
2579 }
2580
2581 TRACE("Returning %d\n", ret);
2582 return ret;
2583 }
2584
2585 /***********************************************************************
2586 * SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
2587 */
2588 BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
2589 HDEVINFO devinfo,
2590 PSP_DEVINFO_DATA DeviceInfoData,
2591 DWORD Property,
2592 PDWORD PropertyRegDataType,
2593 PBYTE PropertyBuffer,
2594 DWORD PropertyBufferSize,
2595 PDWORD RequiredSize)
2596 {
2597 BOOL bResult;
2598 BOOL bIsStringProperty;
2599 DWORD RegType;
2600 DWORD RequiredSizeA, RequiredSizeW;
2601 DWORD PropertyBufferSizeW;
2602 PBYTE PropertyBufferW;
2603
2604 TRACE("%p %p %ld %p %p %ld %p\n", devinfo, DeviceInfoData,
2605 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2606 RequiredSize);
2607
2608 PropertyBufferSizeW = PropertyBufferSize * 2;
2609 PropertyBufferW = HeapAlloc(GetProcessHeap(), 0, PropertyBufferSizeW);
2610
2611 bResult = SetupDiGetDeviceRegistryPropertyW(
2612 devinfo,
2613 DeviceInfoData,
2614 Property,
2615 &RegType,
2616 PropertyBufferW,
2617 PropertyBufferSizeW,
2618 &RequiredSizeW);
2619
2620 if (bResult || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2621 {
2622 bIsStringProperty = (RegType == REG_SZ || RegType == REG_MULTI_SZ || RegType == REG_EXPAND_SZ);
2623
2624 if (bIsStringProperty)
2625 RequiredSizeA = RequiredSizeW / sizeof(WCHAR);
2626 else
2627 RequiredSizeA = RequiredSizeW;
2628 if (RequiredSize)
2629 *RequiredSize = RequiredSizeA;
2630 if (PropertyRegDataType)
2631 *PropertyRegDataType = RegType;
2632 }
2633
2634 if (!bResult)
2635 {
2636 HeapFree(GetProcessHeap(), 0, PropertyBufferW);
2637 return bResult;
2638 }
2639
2640 if (RequiredSizeA <= PropertyBufferSize)
2641 {
2642 if (bIsStringProperty && PropertyBufferSize > 0)
2643 {
2644 if (WideCharToMultiByte(CP_ACP, 0, (LPWSTR)PropertyBufferW, RequiredSizeW / sizeof(WCHAR), (LPSTR)PropertyBuffer, PropertyBufferSize, NULL, NULL) == 0)
2645 {
2646 /* Last error is already set by WideCharToMultiByte */
2647 bResult = FALSE;
2648 }
2649 }
2650 else
2651 memcpy(PropertyBuffer, PropertyBufferW, RequiredSizeA);
2652 }
2653 else
2654 {
2655 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2656 bResult = FALSE;
2657 }
2658
2659 HeapFree(GetProcessHeap(), 0, PropertyBufferW);
2660 return bResult;
2661 }
2662
2663 /***********************************************************************
2664 * SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
2665 */
2666 BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
2667 HDEVINFO DeviceInfoSet,
2668 PSP_DEVINFO_DATA DeviceInfoData,
2669 DWORD Property,
2670 PDWORD PropertyRegDataType,
2671 PBYTE PropertyBuffer,
2672 DWORD PropertyBufferSize,
2673 PDWORD RequiredSize)
2674 {
2675 HKEY hEnumKey, hKey;
2676 DWORD rc;
2677 BOOL ret = FALSE;
2678
2679 TRACE("%p %p %ld %p %p %ld %p\n", DeviceInfoSet, DeviceInfoData,
2680 Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
2681 RequiredSize);
2682
2683 if (!DeviceInfoSet || DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
2684 SetLastError(ERROR_INVALID_HANDLE);
2685 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
2686 SetLastError(ERROR_INVALID_HANDLE);
2687 else if (!DeviceInfoData)
2688 SetLastError(ERROR_INVALID_PARAMETER);
2689 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
2690 SetLastError(ERROR_INVALID_USER_BUFFER);
2691 else if (Property >= SPDRP_MAXIMUM_PROPERTY)
2692 SetLastError(ERROR_INVALID_PARAMETER);
2693 else
2694 {
2695 struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
2696 struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
2697
2698 switch (Property)
2699 {
2700 case SPDRP_CAPABILITIES:
2701 case SPDRP_CLASS:
2702 case SPDRP_CLASSGUID:
2703 case SPDRP_COMPATIBLEIDS:
2704 case SPDRP_CONFIGFLAGS:
2705 case SPDRP_DEVICEDESC:
2706 case SPDRP_DRIVER:
2707 case SPDRP_FRIENDLYNAME:
2708 case SPDRP_HARDWAREID:
2709 case SPDRP_LOCATION_INFORMATION:
2710 case SPDRP_LOWERFILTERS:
2711 case SPDRP_MFG:
2712 case SPDRP_SECURITY:
2713 case SPDRP_SERVICE:
2714 case SPDRP_UI_NUMBER:
2715 case SPDRP_UI_NUMBER_DESC_FORMAT:
2716 case SPDRP_UPPERFILTERS:
2717 {
2718 LPCWSTR RegistryPropertyName;
2719 DWORD BufferSize;
2720
2721 switch (Property)
2722 {
2723 case SPDRP_CAPABILITIES:
2724 RegistryPropertyName = REGSTR_VAL_CAPABILITIES; break;
2725 case SPDRP_CLASS:
2726 RegistryPropertyName = REGSTR_VAL_CLASS; break;
2727 case SPDRP_CLASSGUID:
2728 RegistryPropertyName = REGSTR_VAL_CLASSGUID; break;
2729 case SPDRP_COMPATIBLEIDS:
2730 RegistryPropertyName = REGSTR_VAL_COMPATIBLEIDS; break;
2731 case SPDRP_CONFIGFLAGS:
2732 RegistryPropertyName = REGSTR_VAL_CONFIGFLAGS; break;
2733 case SPDRP_DEVICEDESC:
2734 RegistryPropertyName = REGSTR_VAL_DEVDESC; break;
2735 case SPDRP_DRIVER:
2736 RegistryPropertyName = REGSTR_VAL_DRIVER; break;
2737 case SPDRP_FRIENDLYNAME:
2738 RegistryPropertyName = REGSTR_VAL_FRIENDLYNAME; break;
2739 case SPDRP_HARDWAREID:
2740 RegistryPropertyName = REGSTR_VAL_HARDWAREID; break;
2741 case SPDRP_LOCATION_INFORMATION:
2742 RegistryPropertyName = REGSTR_VAL_LOCATION_INFORMATION; break;
2743 case SPDRP_LOWERFILTERS:
2744 RegistryPropertyName = REGSTR_VAL_LOWERFILTERS; break;
2745 case SPDRP_MFG:
2746 RegistryPropertyName = REGSTR_VAL_MFG; break;
2747 case SPDRP_SECURITY:
2748 RegistryPropertyName = REGSTR_SECURITY; break;
2749 case SPDRP_SERVICE:
2750 RegistryPropertyName = REGSTR_VAL_SERVICE; break;
2751 case SPDRP_UI_NUMBER:
2752 RegistryPropertyName = REGSTR_VAL_UI_NUMBER; break;
2753 case SPDRP_UI_NUMBER_DESC_FORMAT:
2754 RegistryPropertyName = REGSTR_UI_NUMBER_DESC_FORMAT; break;
2755 case SPDRP_UPPERFILTERS:
2756 RegistryPropertyName = REGSTR_VAL_UPPERFILTERS; break;
2757 default:
2758 /* Should not happen */
2759 RegistryPropertyName = NULL; break;
2760 }
2761
2762 /* Open registry key name */
2763 rc = RegOpenKeyExW(
2764 list->HKLM,
2765 REGSTR_PATH_SYSTEMENUM,
2766 0, /* Options */
2767 0,
2768 &hEnumKey);
2769 if (rc != ERROR_SUCCESS)
2770 {
2771 SetLastError(rc);
2772 break;
2773 }
2774 rc = RegOpenKeyExW(
2775 hEnumKey,
2776 DevInfo->Data,
2777 0, /* Options */
2778 KEY_QUERY_VALUE,
2779 &hKey);
2780 RegCloseKey(hEnumKey);
2781 if (rc != ERROR_SUCCESS)
2782 {
2783 SetLastError(rc);
2784 break;
2785 }
2786 /* Read registry entry */
2787 BufferSize = PropertyBufferSize;
2788 rc = RegQueryValueExW(
2789 hKey,
2790 RegistryPropertyName,
2791 NULL, /* Reserved */
2792 PropertyRegDataType,
2793 PropertyBuffer,
2794 &BufferSize);
2795 if (RequiredSize)
2796 *RequiredSize = BufferSize;
2797 switch(rc) {
2798 case ERROR_SUCCESS:
2799 if (PropertyBuffer != NULL || BufferSize == 0)
2800 ret = TRUE;
2801 else
2802 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2803 break;
2804 case ERROR_MORE_DATA:
2805 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2806 break;
2807 default:
2808 SetLastError(rc);
2809 }
2810 RegCloseKey(hKey);
2811 break;
2812 }
2813
2814 case SPDRP_PHYSICAL_DEVICE_OBJECT_NAME:
2815 {
2816 DWORD required = (strlenW(DevInfo->Data) + 1) * sizeof(WCHAR);
2817
2818 if (PropertyRegDataType)
2819 *PropertyRegDataType = REG_SZ;
2820 if (RequiredSize)
2821 *RequiredSize = required;
2822 if (PropertyBufferSize >= required)
2823 {
2824 strcpyW((LPWSTR)PropertyBuffer, DevInfo->Data);
2825 ret = TRUE;
2826 }
2827 else
2828 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2829 break;
2830 }
2831
2832 /*case SPDRP_BUSTYPEGUID:
2833 case SPDRP_LEGACYBUSTYPE:
2834 case SPDRP_BUSNUMBER:
2835 case SPDRP_ENUMERATOR_NAME:
2836 case SPDRP_SECURITY_SDS:
2837 case SPDRP_DEVTYPE:
2838 case SPDRP_EXCLUSIVE:
2839 case SPDRP_CHARACTERISTICS:
2840 case SPDRP_ADDRESS:
2841 case SPDRP_DEVICE_POWER_DATA:*/
2842 #if (WINVER >= 0x501)
2843 /*case SPDRP_REMOVAL_POLICY:
2844 case SPDRP_REMOVAL_POLICY_HW_DEFAULT:
2845 case SPDRP_REMOVAL_POLICY_OVERRIDE:
2846 case SPDRP_INSTALL_STATE:*/
2847 #endif
2848
2849 default:
2850 {
2851 ERR("Property 0x%lx not implemented\n", Property);
2852 SetLastError(ERROR_NOT_SUPPORTED);
2853 }
2854 }
2855 }
2856
2857 TRACE("Returning %d\n", ret);
2858 return ret;
2859 }
2860
2861 /***********************************************************************
2862 * SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
2863 */
2864 BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
2865 IN HDEVINFO DeviceInfoSet,
2866 IN OUT PSP_DEVINFO_DATA DeviceInfoData,
2867 IN DWORD Property,
2868 IN CONST BYTE *PropertyBuffer,
2869 IN DWORD PropertyBufferSize)
2870 {
2871 FIXME("%p %p 0x%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
2872 Property, PropertyBuffer, PropertyBufferSize);
2873 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2874 return FALSE;
2875 }
2876
2877 /***********************************************************************
2878 * SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
2879 */
2880 BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
2881 IN HDEVINFO DeviceInfoSet,
2882 IN OUT PSP_DEVINFO_DATA DeviceInfoData,
2883 IN DWORD Property,
2884 IN const BYTE *PropertyBuffer,
2885 IN DWORD PropertyBufferSize)
2886 {
2887 struct DeviceInfoSet *list;
2888 BOOL ret = FALSE;
2889
2890 TRACE("%p %p 0x%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
2891 Property, PropertyBuffer, PropertyBufferSize);
2892
2893 if (!DeviceInfoSet)
2894 SetLastError(ERROR_INVALID_HANDLE);
2895 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
2896 SetLastError(ERROR_INVALID_HANDLE);
2897 else if (!DeviceInfoData)
2898 SetLastError(ERROR_INVALID_HANDLE);
2899 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
2900 SetLastError(ERROR_INVALID_USER_BUFFER);
2901 else
2902 {
2903 switch (Property)
2904 {
2905 case SPDRP_COMPATIBLEIDS:
2906 case SPDRP_CONFIGFLAGS:
2907 case SPDRP_FRIENDLYNAME:
2908 case SPDRP_HARDWAREID:
2909 case SPDRP_LOCATION_INFORMATION:
2910 case SPDRP_LOWERFILTERS:
2911 case SPDRP_SECURITY:
2912 case SPDRP_SERVICE:
2913 case SPDRP_UI_NUMBER_DESC_FORMAT:
2914 case SPDRP_UPPERFILTERS:
2915 {
2916 LPCWSTR RegistryPropertyName;
2917 DWORD RegistryDataType;
2918 HKEY hKey;
2919 LONG rc;
2920
2921 switch (Property)
2922 {
2923 case SPDRP_COMPATIBLEIDS:
2924 RegistryPropertyName = REGSTR_VAL_COMPATIBLEIDS;
2925 RegistryDataType = REG_MULTI_SZ;
2926 break;
2927 case SPDRP_CONFIGFLAGS:
2928 RegistryPropertyName = REGSTR_VAL_CONFIGFLAGS;
2929 RegistryDataType = REG_DWORD;
2930 break;
2931 case SPDRP_FRIENDLYNAME:
2932 RegistryPropertyName = REGSTR_VAL_FRIENDLYNAME;
2933 RegistryDataType = REG_SZ;
2934 break;
2935 case SPDRP_HARDWAREID:
2936 RegistryPropertyName = REGSTR_VAL_HARDWAREID;
2937 RegistryDataType = REG_MULTI_SZ;
2938 break;
2939 case SPDRP_LOCATION_INFORMATION:
2940 RegistryPropertyName = REGSTR_VAL_LOCATION_INFORMATION;
2941 RegistryDataType = REG_SZ;
2942 break;
2943 case SPDRP_LOWERFILTERS:
2944 RegistryPropertyName = REGSTR_VAL_LOWERFILTERS;
2945 RegistryDataType = REG_MULTI_SZ;
2946 break;
2947 case SPDRP_SECURITY:
2948 RegistryPropertyName = REGSTR_SECURITY;
2949 RegistryDataType = REG_BINARY;
2950 break;
2951 case SPDRP_SERVICE:
2952 RegistryPropertyName = REGSTR_VAL_SERVICE;
2953 RegistryDataType = REG_SZ;
2954 break;
2955 case SPDRP_UI_NUMBER_DESC_FORMAT:
2956 RegistryPropertyName = REGSTR_UI_NUMBER_DESC_FORMAT;
2957 RegistryDataType = REG_SZ;
2958 break;
2959 case SPDRP_UPPERFILTERS:
2960 RegistryPropertyName = REGSTR_VAL_UPPERFILTERS;
2961 RegistryDataType = REG_MULTI_SZ;
2962 break;
2963 default:
2964 /* Should not happen */
2965 RegistryPropertyName = NULL;
2966 RegistryDataType = REG_BINARY;
2967 break;
2968 }
2969 /* Open device registry key */
2970 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_SET_VALUE);
2971 if (hKey != INVALID_HANDLE_VALUE)
2972 {
2973 /* Write new data */
2974 rc = RegSetValueExW(
2975 hKey,
2976 RegistryPropertyName,
2977 0, /* Reserved */
2978 RegistryDataType,
2979 PropertyBuffer,
2980 PropertyBufferSize);
2981 if (rc == ERROR_SUCCESS)
2982 ret = TRUE;
2983 else
2984 SetLastError(rc);
2985 RegCloseKey(hKey);
2986 }
2987 break;
2988 }
2989
2990 /*case SPDRP_CHARACTERISTICS:
2991 case SPDRP_DEVTYPE:
2992 case SPDRP_EXCLUSIVE:*/
2993 #if (WINVER >= 0x501)
2994 //case SPDRP_REMOVAL_POLICY_OVERRIDE:
2995 #endif
2996 //case SPDRP_SECURITY_SDS:
2997
2998 default:
2999 {
3000 ERR("Property 0x%lx not implemented\n", Property);
3001 SetLastError(ERROR_NOT_SUPPORTED);
3002 }
3003 }
3004 }
3005
3006 TRACE("Returning %d\n", ret);
3007 return ret;
3008 }
3009
3010
3011 /***********************************************************************
3012 * SetupDiInstallClassA (SETUPAPI.@)
3013 */
3014 BOOL WINAPI SetupDiInstallClassA(
3015 IN HWND hwndParent OPTIONAL,
3016 IN PCSTR InfFileName,
3017 IN DWORD Flags,
3018 IN HSPFILEQ FileQueue OPTIONAL)
3019 {
3020 return SetupDiInstallClassExA(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
3021 }
3022
3023
3024 /***********************************************************************
3025 * SetupDiInstallClassW (SETUPAPI.@)
3026 */
3027 BOOL WINAPI SetupDiInstallClassW(
3028 IN HWND hwndParent OPTIONAL,
3029 IN PCWSTR InfFileName,
3030 IN DWORD Flags,
3031 IN HSPFILEQ FileQueue OPTIONAL)
3032 {
3033 return SetupDiInstallClassExW(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
3034 }
3035
3036
3037 /***********************************************************************
3038 * SetupDiInstallClassExA (SETUPAPI.@)
3039 */
3040 BOOL WINAPI SetupDiInstallClassExA(
3041 IN HWND hwndParent OPTIONAL,
3042 IN PCSTR InfFileName OPTIONAL,
3043 IN DWORD Flags,
3044 IN HSPFILEQ FileQueue OPTIONAL,
3045 IN const GUID* InterfaceClassGuid OPTIONAL,
3046 IN PVOID Reserved1,
3047 IN PVOID Reserved2)
3048 {
3049 PWSTR InfFileNameW = NULL;
3050 BOOL Result;
3051
3052 if (InfFileName)
3053 {
3054 InfFileNameW = MultiByteToUnicode(InfFileName, CP_ACP);
3055 if (InfFileNameW == NULL)
3056 {
3057 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3058 return FALSE;
3059 }
3060 }
3061
3062 Result = SetupDiInstallClassExW(hwndParent, InfFileNameW, Flags,
3063 FileQueue, InterfaceClassGuid, Reserved1, Reserved2);
3064
3065 MyFree(InfFileNameW);
3066
3067 return Result;
3068 }
3069
3070
3071 static HKEY CreateClassKey(HINF hInf)
3072 {
3073 WCHAR FullBuffer[MAX_PATH];
3074 WCHAR Buffer[MAX_PATH];
3075 DWORD RequiredSize;
3076 HKEY hClassKey;
3077
3078 Buffer[0] = '\\';
3079 if (!SetupGetLineTextW(NULL,
3080 hInf,
3081 Version,
3082 ClassGUID,
3083 &Buffer[1],
3084 MAX_PATH - 1,
3085 &RequiredSize))
3086 {
3087 return INVALID_HANDLE_VALUE;
3088 }
3089
3090 lstrcpyW(FullBuffer, REGSTR_PATH_CLASS_NT);
3091 lstrcatW(FullBuffer, Buffer);
3092
3093
3094 if (!SetupGetLineTextW(NULL,
3095 hInf,
3096 Version,
3097 Class,
3098 Buffer,
3099 MAX_PATH,
3100 &RequiredSize))
3101 {
3102 RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
3103 return INVALID_HANDLE_VALUE;
3104 }
3105
3106 if (ERROR_SUCCESS != RegCreateKeyExW(HKEY_LOCAL_MACHINE,
3107 FullBuffer,
3108 0,
3109 NULL,
3110 REG_OPTION_NON_VOLATILE,
3111 KEY_SET_VALUE,
3112 NULL,
3113 &hClassKey,
3114 NULL))
3115 {
3116 RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
3117 return INVALID_HANDLE_VALUE;
3118 }
3119
3120 if (ERROR_SUCCESS != RegSetValueExW(hClassKey,
3121 Class,
3122 0,
3123 REG_SZ,
3124 (LPBYTE)Buffer,
3125 RequiredSize * sizeof(WCHAR)))
3126 {
3127 RegCloseKey(hClassKey);
3128 RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
3129 return INVALID_HANDLE_VALUE;
3130 }
3131
3132 return hClassKey;
3133 }
3134
3135
3136 /***********************************************************************
3137 * SetupDiInstallClassExW (SETUPAPI.@)
3138 */
3139 BOOL WINAPI SetupDiInstallClassExW(
3140 IN HWND hwndParent OPTIONAL,
3141 IN PCWSTR InfFileName OPTIONAL,
3142 IN DWORD Flags,
3143 IN HSPFILEQ FileQueue OPTIONAL,
3144 IN const GUID* InterfaceClassGuid OPTIONAL,
3145 IN PVOID Reserved1,
3146 IN PVOID Reserved2)
3147 {
3148 BOOL ret = FALSE;
3149
3150 TRACE("%p %s 0x%lx %p %s %p %p\n", hwndParent, debugstr_w(InfFileName), Flags,
3151 FileQueue, debugstr_guid(InterfaceClassGuid), Reserved1, Reserved2);
3152
3153 if (!InfFileName)
3154 {
3155 FIXME("Case not implemented: InfFileName NULL\n");
3156 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3157 }
3158 else if (Flags & ~(DI_NOVCP | DI_NOBROWSE | DI_FORCECOPY))
3159 {
3160 TRACE("Unknown flags: 0x%08lx\n", Flags & ~(DI_NOVCP | DI_NOBROWSE | DI_FORCECOPY));
3161 SetLastError(ERROR_INVALID_FLAGS);
3162 }
3163 else if ((Flags & DI_NOVCP) && FileQueue == NULL)
3164 SetLastError(ERROR_INVALID_PARAMETER);
3165 else if (Reserved1 != NULL)
3166 SetLastError(ERROR_INVALID_PARAMETER);
3167 else if (Reserved2 != NULL)
3168 SetLastError(ERROR_INVALID_PARAMETER);
3169 else
3170 {
3171 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
3172 SP_DEVINSTALL_PARAMS_W InstallParams;
3173 WCHAR SectionName[MAX_PATH];
3174 HINF hInf = INVALID_HANDLE_VALUE;
3175 HKEY hRootKey = INVALID_HANDLE_VALUE;
3176 PVOID callback_context = NULL;
3177
3178 hDeviceInfo = SetupDiCreateDeviceInfoList(NULL, NULL);
3179
3180 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
3181 if (!SetupDiGetDeviceInstallParamsW(hDeviceInfo, NULL, &InstallParams))
3182 goto cleanup;
3183 InstallParams.Flags &= ~(DI_NOVCP | DI_NOBROWSE | DI_QUIETINSTALL);
3184 InstallParams.Flags |= Flags & (DI_NOVCP | DI_NOBROWSE | DI_QUIETINSTALL);
3185 if (Flags & DI_NOVCP)
3186 InstallParams.FileQueue = FileQueue;
3187 if (!SetupDiSetDeviceInstallParamsW(hDeviceInfo, NULL, &InstallParams))
3188 goto cleanup;
3189
3190 /* Open the .inf file */
3191 hInf = SetupOpenInfFileW(
3192 InfFileName,
3193 NULL,
3194 INF_STYLE_WIN4,
3195 NULL);
3196 if (hInf == INVALID_HANDLE_VALUE)
3197 goto cleanup;
3198
3199 /* Try to append a layout file */
3200 ret = SetupOpenAppendInfFileW(NULL, hInf, NULL);
3201 if (!ret)
3202 goto cleanup;
3203
3204 if (InterfaceClassGuid)
3205 {
3206 /* Retrieve the actual section name */
3207 ret = SetupDiGetActualSectionToInstallW(
3208 hInf,
3209 InterfaceInstall32,
3210 SectionName,
3211 MAX_PATH,
3212 NULL,
3213 NULL);
3214 if (!ret)
3215 goto cleanup;
3216
3217 /* Open registry key related to this interface */
3218 /* FIXME: What happens if the key doesn't exist? */
3219 hRootKey = SetupDiOpenClassRegKeyExW(InterfaceClassGuid, KEY_ENUMERATE_SUB_KEYS, DIOCR_INTERFACE, NULL, NULL);
3220 if (hRootKey == INVALID_HANDLE_VALUE)
3221 goto cleanup;
3222
3223 /* SetupDiCreateDeviceInterface??? */
3224 FIXME("Installing an interface is not implemented\n");
3225 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3226 }
3227 else
3228 {
3229 /* Create or open the class registry key 'HKLM\CurrentControlSet\Class\{GUID}' */
3230 hRootKey = CreateClassKey(hInf);
3231 if (hRootKey == INVALID_HANDLE_VALUE)
3232 goto cleanup;
3233
3234 /* Retrieve the actual section name */
3235 ret = SetupDiGetActualSectionToInstallW(
3236 hInf,
3237 ClassInstall32,
3238 SectionName,
3239 MAX_PATH - strlenW(DotServices),
3240 NULL,
3241 NULL);
3242 if (!ret)
3243 goto cleanup;
3244
3245 callback_context = SetupInitDefaultQueueCallback(hwndParent);
3246 if (!callback_context)
3247 goto cleanup;
3248
3249 ret = SetupInstallFromInfSectionW(
3250 hwndParent,
3251 hInf,
3252 SectionName,
3253 SPINST_REGISTRY | SPINST_FILES | SPINST_BITREG | SPINST_INIFILES | SPINST_INI2REG,
3254 hRootKey,
3255 NULL, /* SourceRootPath */
3256 !(Flags & DI_NOVCP) && (Flags & DI_FORCECOPY) ? SP_COPY_FORCE_IN_USE : 0, /* CopyFlags */
3257 SetupDefaultQueueCallbackW,
3258 callback_context,
3259 hDeviceInfo,
3260 NULL);
3261 if (!ret)
3262 goto cleanup;
3263
3264 /* Install .Services section */
3265 lstrcatW(SectionName, DotServices);
3266 ret = SetupInstallServicesFromInfSectionExW(
3267 hInf,
3268 SectionName,
3269 0,
3270 hDeviceInfo,
3271 NULL,
3272 NULL,
3273 NULL);
3274 if (!ret)
3275 goto cleanup;
3276
3277 ret = TRUE;
3278 }
3279
3280 cleanup:
3281 if (hDeviceInfo != INVALID_HANDLE_VALUE)
3282 SetupDiDestroyDeviceInfoList(hDeviceInfo);
3283 if (hInf != INVALID_HANDLE_VALUE)
3284 SetupCloseInfFile(hInf);
3285 if (hRootKey != INVALID_HANDLE_VALUE)
3286 RegCloseKey(hRootKey);
3287 SetupTermDefaultQueueCallback(callback_context);
3288 }
3289
3290 TRACE("Returning %d\n", ret);
3291 return ret;
3292 }
3293
3294
3295 /***********************************************************************
3296 * SetupDiOpenClassRegKey (SETUPAPI.@)
3297 */
3298 HKEY WINAPI SetupDiOpenClassRegKey(
3299 const GUID* ClassGuid,
3300 REGSAM samDesired)
3301 {
3302 return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3303 DIOCR_INSTALLER, NULL, NULL);
3304 }
3305
3306
3307 /***********************************************************************
3308 * SetupDiOpenClassRegKeyExA (SETUPAPI.@)
3309 */
3310 HKEY WINAPI SetupDiOpenClassRegKeyExA(
3311 const GUID* ClassGuid OPTIONAL,
3312 REGSAM samDesired,
3313 DWORD Flags,
3314 PCSTR MachineName OPTIONAL,
3315 PVOID Reserved)
3316 {
3317 PWSTR MachineNameW = NULL;
3318 HKEY hKey;
3319
3320 TRACE("\n");
3321
3322 if (MachineName)
3323 {
3324 MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
3325 if (MachineNameW == NULL)
3326 return INVALID_HANDLE_VALUE;
3327 }
3328
3329 hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
3330 Flags, MachineNameW, Reserved);
3331
3332 if (MachineNameW)
3333 MyFree(MachineNameW);
3334
3335 return hKey;
3336 }
3337
3338
3339 /***********************************************************************
3340 * SetupDiOpenClassRegKeyExW (SETUPAPI.@)
3341 */
3342 HKEY WINAPI SetupDiOpenClassRegKeyExW(
3343 const GUID* ClassGuid OPTIONAL,
3344 REGSAM samDesired,
3345 DWORD Flags,
3346 PCWSTR MachineName OPTIONAL,
3347 PVOID Reserved)
3348 {
3349 LPWSTR lpGuidString;
3350 LPWSTR lpFullGuidString;
3351 DWORD dwLength;
3352 HKEY HKLM;
3353 HKEY hClassesKey;
3354 HKEY hClassKey;
3355 DWORD rc;
3356 LPCWSTR lpKeyName;
3357
3358 TRACE("%s 0x%lx 0x%lx %s %p\n", debugstr_guid(ClassGuid), samDesired,
3359 Flags, debugstr_w(MachineName), Reserved);
3360
3361 if (Flags == DIOCR_INSTALLER)
3362 {
3363 lpKeyName = REGSTR_PATH_CLASS_NT;
3364 }
3365 else if (Flags == DIOCR_INTERFACE)
3366 {
3367 lpKeyName = REGSTR_PATH_DEVICE_CLASSES;
3368 }
3369 else
3370 {
3371 ERR("Invalid Flags parameter!\n");
3372 SetLastError(ERROR_INVALID_FLAGS);
3373 return INVALID_HANDLE_VALUE;
3374 }
3375
3376 if (MachineName != NULL)
3377 {
3378 rc = RegConnectRegistryW(MachineName, HKEY_LOCAL_MACHINE, &HKLM);
3379 if (rc != ERROR_SUCCESS)
3380 {
3381 SetLastError(rc);
3382 return INVALID_HANDLE_VALUE;
3383 }
3384 }
3385 else
3386 HKLM = HKEY_LOCAL_MACHINE;
3387
3388 rc = RegOpenKeyExW(HKLM,
3389 lpKeyName,
3390 0,
3391 ClassGuid ? 0 : samDesired,
3392 &hClassesKey);
3393 if (MachineName != NULL) RegCloseKey(HKLM);
3394 if (rc != ERROR_SUCCESS)
3395 {
3396 SetLastError(rc);
3397 return INVALID_HANDLE_VALUE;
3398 }
3399
3400 if (ClassGuid == NULL)
3401 return hClassesKey;
3402
3403 if (UuidToStringW((UUID*)ClassGuid, &lpGuidString) != RPC_S_OK)
3404 {
3405 SetLastError(ERROR_GEN_FAILURE);
3406 RegCloseKey(hClassesKey);
3407 return INVALID_HANDLE_VALUE;
3408 }
3409
3410 dwLength = lstrlenW(lpGuidString);
3411 lpFullGuidString = HeapAlloc(GetProcessHeap(), 0, (dwLength + 3) * sizeof(WCHAR));
3412 if (!lpFullGuidString)
3413 {
3414 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3415 RpcStringFreeW(&lpGuidString);
3416 return INVALID_HANDLE_VALUE;
3417 }
3418 lpFullGuidString[0] = '{';
3419 memcpy(&lpFullGuidString[1], lpGuidString, dwLength * sizeof(WCHAR));
3420 lpFullGuidString[dwLength + 1] = '}';
3421 lpFullGuidString[dwLength + 2] = '\0';
3422 RpcStringFreeW(&lpGuidString);
3423
3424 rc = RegOpenKeyExW(hClassesKey,
3425 lpFullGuidString,
3426 0,
3427 samDesired,
3428 &hClassKey);
3429 if (rc != ERROR_SUCCESS)
3430 {
3431 SetLastError(rc);
3432 HeapFree(GetProcessHeap(), 0, lpFullGuidString);
3433 RegCloseKey(hClassesKey);
3434 return INVALID_HANDLE_VALUE;
3435 }
3436
3437 HeapFree(GetProcessHeap(), 0, lpFullGuidString);
3438 RegCloseKey(hClassesKey);
3439
3440 return hClassKey;
3441 }
3442
3443 /***********************************************************************
3444 * SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
3445 */
3446 BOOL WINAPI SetupDiOpenDeviceInterfaceW(
3447 HDEVINFO DeviceInfoSet,
3448 PCWSTR DevicePath,
3449 DWORD OpenFlags,
3450 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3451 {
3452 FIXME("%p %s %08lx %p\n",
3453 DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
3454 return FALSE;
3455 }
3456
3457 /***********************************************************************
3458 * SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
3459 */
3460 BOOL WINAPI SetupDiOpenDeviceInterfaceA(
3461 HDEVINFO DeviceInfoSet,
3462 PCSTR DevicePath,
3463 DWORD OpenFlags,
3464 PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
3465 {
3466 LPWSTR DevicePathW = NULL;
3467 BOOL bResult;
3468
3469 TRACE("%p %s %08lx %p\n", DeviceInfoSet, debugstr_a(DevicePath), OpenFlags, DeviceInterfaceData);
3470
3471 DevicePathW = MultiByteToUnicode(DevicePath, CP_ACP);
3472 if (DevicePathW == NULL)
3473 return FALSE;
3474
3475 bResult = SetupDiOpenDeviceInterfaceW(DeviceInfoSet,
3476 DevicePathW, OpenFlags, DeviceInterfaceData);
3477
3478 MyFree(DevicePathW);
3479
3480 return bResult;
3481 }
3482
3483 /***********************************************************************
3484 * SetupDiSetClassInstallParamsA (SETUPAPI.@)
3485 */
3486 BOOL WINAPI SetupDiSetClassInstallParamsA(
3487 HDEVINFO DeviceInfoSet,
3488 PSP_DEVINFO_DATA DeviceInfoData,
3489 PSP_CLASSINSTALL_HEADER ClassInstallParams,
3490 DWORD ClassInstallParamsSize)
3491 {
3492 FIXME("%p %p %x %lu\n",DeviceInfoSet, DeviceInfoData,
3493 ClassInstallParams->InstallFunction, ClassInstallParamsSize);
3494 return FALSE;
3495 }
3496
3497 /***********************************************************************
3498 * SetupDiSetClassInstallParamsW (SETUPAPI.@)
3499 */
3500 BOOL WINAPI SetupDiSetClassInstallParamsW(
3501 IN HDEVINFO DeviceInfoSet,
3502 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
3503 IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
3504 IN DWORD ClassInstallParamsSize)
3505 {
3506 struct DeviceInfoSet *list;
3507 BOOL ret = FALSE;
3508
3509 TRACE("%p %p %p %lu\n", DeviceInfoSet, DeviceInfoData,
3510 ClassInstallParams, ClassInstallParamsSize);
3511
3512 if (!DeviceInfoSet)
3513 SetLastError(ERROR_INVALID_PARAMETER);
3514 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
3515 SetLastError(ERROR_INVALID_HANDLE);
3516 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
3517 SetLastError(ERROR_INVALID_HANDLE);
3518 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
3519 SetLastError(ERROR_INVALID_USER_BUFFER);
3520 else if (ClassInstallParams && ClassInstallParams->cbSize != sizeof(SP_CLASSINSTALL_HEADER))
3521 SetLastError(ERROR_INVALID_USER_BUFFER);
3522 else if (ClassInstallParams && ClassInstallParamsSize < sizeof(SP_CLASSINSTALL_HEADER))
3523 SetLastError(ERROR_INVALID_PARAMETER);
3524 else if (!ClassInstallParams && ClassInstallParamsSize != 0)
3525 SetLastError(ERROR_INVALID_PARAMETER);
3526 else
3527 {
3528 SP_DEVINSTALL_PARAMS_W InstallParams;
3529 BOOL Result;
3530
3531 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
3532 Result = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
3533 if (!Result)
3534 goto done;
3535
3536 if (ClassInstallParams)
3537 {
3538 /* Check parameters in ClassInstallParams */
3539 if (ClassInstallParams->InstallFunction < DIF_SELECTDEVICE
3540 || ClassInstallParams->InstallFunction - DIF_SELECTDEVICE >= sizeof(UpdateClassInstallParamHandlers)/sizeof(UpdateClassInstallParamHandlers[0]))
3541 {
3542 SetLastError(ERROR_INVALID_USER_BUFFER);
3543 goto done;
3544 }
3545 else if (UpdateClassInstallParamHandlers[ClassInstallParams->InstallFunction - DIF_SELECTDEVICE] == NULL)
3546 {
3547 FIXME("InstallFunction %u is valid, but has no associated update handler\n", ClassInstallParams->InstallFunction);
3548 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3549 goto done;
3550 }
3551 ret = UpdateClassInstallParamHandlers[ClassInstallParams->InstallFunction - DIF_SELECTDEVICE](DeviceInfoSet, DeviceInfoData, ClassInstallParams, ClassInstallParamsSize);
3552 if (!ret)
3553 goto done;
3554 InstallParams.Flags |= DI_CLASSINSTALLPARAMS;
3555 }
3556 else
3557 {
3558 InstallParams.Flags &= ~DI_CLASSINSTALLPARAMS;
3559 }
3560
3561 ret = SetupDiSetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
3562 }
3563
3564 done:
3565 TRACE("Returning %d\n", ret);
3566 return ret;
3567 }
3568
3569 static BOOL PropertyChangeHandler(
3570 IN HDEVINFO DeviceInfoSet,
3571 IN PSP_DEVINFO_DATA DeviceInfoData,
3572 IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
3573 IN DWORD ClassInstallParamsSize)
3574 {
3575 PSP_PROPCHANGE_PARAMS PropChangeParams = (PSP_PROPCHANGE_PARAMS)ClassInstallParams;
3576 BOOL ret = FALSE;
3577
3578 if (!DeviceInfoData)
3579 SetLastError(ERROR_INVALID_PARAMETER);
3580 else if (ClassInstallParamsSize != sizeof(SP_PROPCHANGE_PARAMS))
3581 SetLastError(ERROR_INVALID_PARAMETER);
3582 else if (PropChangeParams && PropChangeParams->StateChange != DICS_ENABLE
3583 && PropChangeParams->StateChange != DICS_DISABLE && PropChangeParams->StateChange != DICS_PROPCHANGE
3584 && PropChangeParams->StateChange != DICS_START && PropChangeParams->StateChange != DICS_STOP)
3585 SetLastError(ERROR_INVALID_FLAGS);
3586 else if (PropChangeParams && PropChangeParams->Scope != DICS_FLAG_GLOBAL
3587 && PropChangeParams->Scope != DICS_FLAG_CONFIGSPECIFIC)
3588 SetLastError(ERROR_INVALID_FLAGS);
3589 else if (PropChangeParams
3590 && (PropChangeParams->StateChange == DICS_START || PropChangeParams->StateChange == DICS_STOP)
3591 && PropChangeParams->Scope != DICS_FLAG_CONFIGSPECIFIC)
3592 SetLastError(ERROR_INVALID_USER_BUFFER);
3593 else
3594 {
3595 PSP_PROPCHANGE_PARAMS *CurrentPropChangeParams;
3596 if (!DeviceInfoData)
3597 {
3598 struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
3599 CurrentPropChangeParams = &list->ClassInstallParams.PropChange;
3600 }
3601 else
3602 {
3603 struct DeviceInfoElement *deviceInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
3604 CurrentPropChangeParams = &deviceInfo->ClassInstallParams.PropChange;
3605 }
3606 if (*CurrentPropChangeParams)
3607 {
3608 MyFree(*CurrentPropChangeParams);
3609 *CurrentPropChangeParams = NULL;
3610 }
3611 if (PropChangeParams)
3612 {
3613 *CurrentPropChangeParams = MyMalloc(sizeof(SP_PROPCHANGE_PARAMS));
3614 if (!*CurrentPropChangeParams)
3615 {
3616 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3617 goto done;
3618 }
3619 memcpy(*CurrentPropChangeParams, PropChangeParams, sizeof(SP_PROPCHANGE_PARAMS));
3620 }
3621 ret = TRUE;
3622 }
3623
3624 done:
3625 return ret;
3626 }
3627
3628 static DWORD
3629 GetFunctionPointer(
3630 IN PWSTR InstallerName,
3631 OUT HMODULE* ModulePointer,
3632 OUT PVOID* FunctionPointer)
3633 {
3634 HMODULE hModule = NULL;
3635 LPSTR FunctionNameA = NULL;
3636 PWCHAR Comma;
3637 DWORD rc;
3638
3639 *ModulePointer = NULL;
3640 *FunctionPointer = NULL;
3641
3642 Comma = strchrW(InstallerName, ',');
3643 if (!Comma)
3644 {
3645 rc = ERROR_INVALID_PARAMETER;
3646 goto cleanup;
3647 }
3648
3649 /* Load library */
3650 *Comma = '\0';
3651 hModule = LoadLibraryW(InstallerName);
3652 *Comma = ',';
3653 if (!hModule)
3654 {
3655 rc = GetLastError();
3656 goto cleanup;
3657 }
3658
3659 /* Skip comma spaces */
3660 while (*Comma == ',' || isspaceW(*Comma))
3661 Comma++;
3662
3663 /* W->A conversion for function name */
3664 FunctionNameA = UnicodeToMultiByte(Comma, CP_ACP);
3665 if (!FunctionNameA)
3666 {
3667 rc = GetLastError();
3668 goto cleanup;
3669 }
3670
3671 /* Search function */
3672 *FunctionPointer = GetProcAddress(hModule, FunctionNameA);
3673 if (!*FunctionPointer)
3674 {
3675 rc = GetLastError();
3676 goto cleanup;
3677 }
3678
3679 *ModulePointer = hModule;
3680 rc = ERROR_SUCCESS;
3681
3682 cleanup:
3683 if (rc != ERROR_SUCCESS && hModule)
3684 FreeLibrary(hModule);
3685 MyFree(FunctionNameA);
3686 return rc;
3687 }
3688
3689 static DWORD
3690 FreeFunctionPointer(
3691 IN HMODULE ModulePointer,
3692 IN PVOID FunctionPointer)
3693 {
3694 if (ModulePointer == NULL)
3695 return ERROR_SUCCESS;
3696 if (FreeLibrary(ModulePointer))
3697 return ERROR_SUCCESS;
3698 else
3699 return GetLastError();
3700 }
3701
3702 static BOOL WINAPI
3703 IntSetupDiRegisterDeviceInfo(
3704 IN HDEVINFO DeviceInfoSet,
3705 IN OUT PSP_DEVINFO_DATA DeviceInfoData)
3706 {
3707 return SetupDiRegisterDeviceInfo(DeviceInfoSet, DeviceInfoData, 0, NULL, NULL, NULL);
3708 }
3709
3710 /***********************************************************************
3711 * SetupDiCallClassInstaller (SETUPAPI.@)
3712 */
3713 BOOL WINAPI SetupDiCallClassInstaller(
3714 IN DI_FUNCTION InstallFunction,
3715 IN HDEVINFO DeviceInfoSet,
3716 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
3717 {
3718 BOOL ret = FALSE;
3719
3720 TRACE("%u %p %p\n", InstallFunction, DeviceInfoSet, DeviceInfoData);
3721
3722 if (!DeviceInfoSet)
3723 SetLastError(ERROR_INVALID_PARAMETER);
3724 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
3725 SetLastError(ERROR_INVALID_HANDLE);
3726 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
3727 SetLastError(ERROR_INVALID_HANDLE);
3728 else if (((struct DeviceInfoSet *)DeviceInfoSet)->HKLM != HKEY_LOCAL_MACHINE)
3729 SetLastError(ERROR_INVALID_HANDLE);
3730 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
3731 SetLastError(ERROR_INVALID_USER_BUFFER);
3732 else
3733 {
3734 SP_DEVINSTALL_PARAMS_W InstallParams;
3735 #define CLASS_COINSTALLER 0x1
3736 #define DEVICE_COINSTALLER 0x2
3737 #define CLASS_INSTALLER 0x4
3738 UCHAR CanHandle = 0;
3739 DEFAULT_CLASS_INSTALL_PROC DefaultHandler = NULL;
3740
3741 switch (InstallFunction)
3742 {
3743 case DIF_ADDPROPERTYPAGE_ADVANCED:
3744 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3745 break;
3746 case DIF_ADDREMOTEPROPERTYPAGE_ADVANCED:
3747 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3748 break;
3749 case DIF_ALLOW_INSTALL:
3750 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3751 break;
3752 case DIF_DETECT:
3753 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3754 break;
3755 case DIF_DESTROYPRIVATEDATA:
3756 CanHandle = CLASS_INSTALLER;
3757 break;
3758 case DIF_INSTALLDEVICE:
3759 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3760 DefaultHandler = SetupDiInstallDevice;
3761 break;
3762 case DIF_INSTALLDEVICEFILES:
3763 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3764 DefaultHandler = SetupDiInstallDriverFiles;
3765 break;
3766 case DIF_INSTALLINTERFACES:
3767 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3768 DefaultHandler = SetupDiInstallDeviceInterfaces;
3769 break;
3770 case DIF_NEWDEVICEWIZARD_FINISHINSTALL:
3771 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3772 break;
3773 case DIF_NEWDEVICEWIZARD_POSTANALYZE:
3774 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3775 break;
3776 case DIF_NEWDEVICEWIZARD_PREANALYZE:
3777 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3778 break;
3779 case DIF_NEWDEVICEWIZARD_PRESELECT:
3780 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3781 break;
3782 case DIF_NEWDEVICEWIZARD_SELECT:
3783 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3784 break;
3785 case DIF_POWERMESSAGEWAKE:
3786 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3787 break;
3788 case DIF_PROPERTYCHANGE:
3789 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3790 DefaultHandler = SetupDiChangeState;
3791 break;
3792 case DIF_REGISTER_COINSTALLERS:
3793 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3794 DefaultHandler = SetupDiRegisterCoDeviceInstallers;
3795 break;
3796 case DIF_REGISTERDEVICE:
3797 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3798 DefaultHandler = IntSetupDiRegisterDeviceInfo;
3799 break;
3800 case DIF_REMOVE:
3801 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3802 DefaultHandler = SetupDiRemoveDevice;
3803 break;
3804 case DIF_SELECTBESTCOMPATDRV:
3805 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3806 DefaultHandler = SetupDiSelectBestCompatDrv;
3807 break;
3808 case DIF_SELECTDEVICE:
3809 CanHandle = CLASS_COINSTALLER | CLASS_INSTALLER;
3810 DefaultHandler = SetupDiSelectBestCompatDrv;
3811 break;
3812 case DIF_TROUBLESHOOTER:
3813 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3814 break;
3815 case DIF_UNREMOVE:
3816 CanHandle = CLASS_COINSTALLER | DEVICE_COINSTALLER | CLASS_INSTALLER;
3817 DefaultHandler = SetupDiUnremoveDevice;
3818 break;
3819 default:
3820 ERR("Install function %u not supported\n", InstallFunction);
3821 SetLastError(ERROR_NOT_SUPPORTED);
3822 }
3823
3824 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
3825 if (!SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams))
3826 /* Don't process this call, as a parameter is invalid */
3827 CanHandle = 0;
3828
3829 if (CanHandle != 0)
3830 {
3831 LIST_ENTRY ClassCoInstallersListHead;
3832 LIST_ENTRY DeviceCoInstallersListHead;
3833 HMODULE ClassInstallerLibrary = NULL;
3834 CLASS_INSTALL_PROC ClassInstaller = NULL;
3835 COINSTALLER_CONTEXT_DATA Context;
3836 PLIST_ENTRY ListEntry;
3837 HKEY hKey;
3838 DWORD dwRegType, dwLength;
3839 DWORD rc = NO_ERROR;
3840
3841 InitializeListHead(&ClassCoInstallersListHead);
3842 InitializeListHead(&DeviceCoInstallersListHead);
3843
3844 if (CanHandle & DEVICE_COINSTALLER)
3845 {
3846 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
3847 if (hKey != INVALID_HANDLE_VALUE)
3848 {
3849 rc = RegQueryValueExW(hKey, REGSTR_VAL_COINSTALLERS_32, NULL, &dwRegType, NULL, &dwLength);
3850 if (rc == ERROR_SUCCESS && dwRegType == REG_MULTI_SZ)
3851 {
3852 LPWSTR KeyBuffer = HeapAlloc(GetProcessHeap(), 0, dwLength);
3853 if (KeyBuffer != NULL)
3854 {
3855 rc = RegQueryValueExW(hKey, REGSTR_VAL_COINSTALLERS_32, NULL, NULL, (LPBYTE)KeyBuffer, &dwLength);
3856 if (rc == ERROR_SUCCESS)
3857 {
3858 LPWSTR ptr;
3859 for (ptr = KeyBuffer; *ptr; ptr += strlenW(ptr) + 1)
3860 {
3861 /* Add coinstaller to DeviceCoInstallersListHead list */
3862 struct CoInstallerElement *coinstaller;
3863 TRACE("Got device coinstaller '%s'\n", debugstr_w(ptr));
3864 coinstaller = HeapAlloc(GetProcessHeap(), 0, sizeof(struct CoInstallerElement));
3865 if (!coinstaller)
3866 continue;
3867 memset(coinstaller, 0, sizeof(struct CoInstallerElement));
3868 if (GetFunctionPointer(ptr, &coinstaller->Module, (PVOID*)&coinstaller->Function) == ERROR_SUCCESS)
3869 InsertTailList(&DeviceCoInstallersListHead, &coinstaller->ListEntry);
3870 else
3871 HeapFree(GetProcessHeap(), 0, coinstaller);
3872 }
3873 }
3874 HeapFree(GetProcessHeap(), 0, KeyBuffer);
3875 }
3876 }
3877 RegCloseKey(hKey);
3878 }
3879 }
3880 if (CanHandle & CLASS_COINSTALLER)
3881 {
3882 rc = RegOpenKeyEx(
3883 HKEY_LOCAL_MACHINE,
3884 REGSTR_PATH_CODEVICEINSTALLERS,
3885 0, /* Options */
3886 KEY_QUERY_VALUE,
3887 &hKey);
3888 if (rc == ERROR_SUCCESS)
3889 {
3890 LPWSTR lpGuidString;
3891 if (UuidToStringW((UUID*)&DeviceInfoData->ClassGuid, &lpGuidString) == RPC_S_OK)
3892 {
3893 rc = RegQueryValueExW(hKey, lpGuidString, NULL, &dwRegType, NULL, &dwLength);
3894 if (rc == ERROR_SUCCESS && dwRegType == REG_MULTI_SZ)
3895 {
3896 LPWSTR KeyBuffer = HeapAlloc(GetProcessHeap(), 0, dwLength);
3897 if (KeyBuffer != NULL)
3898 {
3899 rc = RegQueryValueExW(hKey, lpGuidString, NULL, NULL, (LPBYTE)KeyBuffer, &dwLength);
3900 if (rc == ERROR_SUCCESS)
3901 {
3902 LPWSTR ptr;
3903 for (ptr = KeyBuffer; *ptr; ptr += strlenW(ptr) + 1)
3904 {
3905 /* Add coinstaller to ClassCoInstallersListHead list */
3906 struct CoInstallerElement *coinstaller;
3907 TRACE("Got class coinstaller '%s'\n", debugstr_w(ptr));
3908 coinstaller = HeapAlloc(GetProcessHeap(), 0, sizeof(struct CoInstallerElement));
3909 if (!coinstaller)
3910 continue;
3911 memset(coinstaller, 0, sizeof(struct CoInstallerElement));
3912 if (GetFunctionPointer(ptr, &coinstaller->Module, (PVOID*)&coinstaller->Function) == ERROR_SUCCESS)
3913 InsertTailList(&ClassCoInstallersListHead, &coinstaller->ListEntry);
3914 else
3915 HeapFree(GetProcessHeap(), 0, coinstaller);
3916 }
3917 }
3918 HeapFree(GetProcessHeap(), 0, KeyBuffer);
3919 }
3920 }
3921 RpcStringFreeW(&lpGuidString);
3922 }
3923 RegCloseKey(hKey);
3924 }
3925 }
3926 if ((CanHandle & CLASS_INSTALLER) && !(InstallParams.FlagsEx & DI_FLAGSEX_CI_FAILED))
3927 {
3928 hKey = SetupDiOpenClassRegKey(&DeviceInfoData->ClassGuid, KEY_QUERY_VALUE);
3929 if (hKey != INVALID_HANDLE_VALUE)
3930 {
3931 rc = RegQueryValueExW(hKey, REGSTR_VAL_INSTALLER_32, NULL, &dwRegType, NULL, &dwLength);
3932 if (rc == ERROR_SUCCESS && dwRegType == REG_SZ)
3933 {
3934 LPWSTR KeyBuffer = HeapAlloc(GetProcessHeap(), 0, dwLength);
3935 if (KeyBuffer != NULL)
3936 {
3937 rc = RegQueryValueExW(hKey, REGSTR_VAL_INSTALLER_32, NULL, NULL, (LPBYTE)KeyBuffer, &dwLength);
3938 if (rc == ERROR_SUCCESS)
3939 {
3940 /* Get ClassInstaller function pointer */
3941 TRACE("Got class installer '%s'\n", debugstr_w(KeyBuffer));
3942 if (GetFunctionPointer(KeyBuffer, &ClassInstallerLibrary, (PVOID*)&ClassInstaller) != ERROR_SUCCESS)
3943 {
3944 InstallParams.FlagsEx |= DI_FLAGSEX_CI_FAILED;
3945 SetupDiSetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
3946 }
3947 }
3948 HeapFree(GetProcessHeap(), 0, KeyBuffer);
3949 }
3950 }
3951 RegCloseKey(hKey);
3952 }
3953 }
3954
3955 /* Call Class co-installers */
3956 Context.PostProcessing = FALSE;
3957 rc = NO_ERROR;
3958 ListEntry = ClassCoInstallersListHead.Flink;
3959 while (rc == NO_ERROR && ListEntry != &ClassCoInstallersListHead)
3960 {
3961 struct CoInstallerElement *coinstaller = (struct CoInstallerElement *)ListEntry;
3962 rc = (*coinstaller->Function)(InstallFunction, DeviceInfoSet, DeviceInfoData, &Context);
3963 coinstaller->PrivateData = Context.PrivateData;
3964 if (rc == ERROR_DI_POSTPROCESSING_REQUIRED)
3965 {
3966 coinstaller->DoPostProcessing = TRUE;
3967 rc = NO_ERROR;
3968 }
3969 ListEntry = ListEntry->Flink;
3970 }
3971
3972 /* Call Device co-installers */
3973 ListEntry = DeviceCoInstallersListHead.Flink;
3974 while (rc == NO_ERROR && ListEntry != &DeviceCoInstallersListHead)
3975 {
3976 struct CoInstallerElement *coinstaller = (struct CoInstallerElement *)ListEntry;
3977 rc = (*coinstaller->Function)(InstallFunction, DeviceInfoSet, DeviceInfoData, &Context);
3978 coinstaller->PrivateData = Context.PrivateData;
3979 if (rc == ERROR_DI_POSTPROCESSING_REQUIRED)
3980 {
3981 coinstaller->DoPostProcessing = TRUE;
3982 rc = NO_ERROR;
3983 }
3984 ListEntry = ListEntry->Flink;
3985 }
3986
3987 /* Call Class installer */
3988 if (ClassInstaller)
3989 {
3990 rc = (*ClassInstaller)(InstallFunction, DeviceInfoSet, DeviceInfoData);
3991 FreeFunctionPointer(ClassInstallerLibrary, ClassInstaller);
3992 }
3993 else
3994 rc = ERROR_DI_DO_DEFAULT;
3995
3996 /* Call default handler */
3997 if (rc == ERROR_DI_DO_DEFAULT)
3998 {
3999 if (DefaultHandler && !(InstallParams.Flags & DI_NODI_DEFAULTACTION))
4000 {
4001 if ((*DefaultHandler)(DeviceInfoSet, DeviceInfoData))
4002 rc = NO_ERROR;
4003 else
4004 rc = GetLastError();
4005 }
4006 else
4007 rc = NO_ERROR;
4008 }
4009
4010 /* Call Class co-installers that required postprocessing */
4011 Context.PostProcessing = TRUE;
4012 ListEntry = ClassCoInstallersListHead.Flink;
4013 while (ListEntry != &ClassCoInstallersListHead)
4014 {
4015 struct CoInstallerElement *coinstaller = (struct CoInstallerElement *)ListEntry;
4016 if (coinstaller->DoPostProcessing)
4017 {
4018 Context.InstallResult = rc;
4019 Context.PrivateData = coinstaller->PrivateData;
4020 rc = (*coinstaller->Function)(InstallFunction, DeviceInfoSet, DeviceInfoData, &Context);
4021 }
4022 FreeFunctionPointer(coinstaller->Module, coinstaller->Function);
4023 ListEntry = ListEntry->Flink;
4024 }
4025
4026 /* Call Device co-installers that required postprocessing */
4027 ListEntry = DeviceCoInstallersListHead.Flink;
4028 while (ListEntry != &DeviceCoInstallersListHead)
4029 {
4030 struct CoInstallerElement *coinstaller = (struct CoInstallerElement *)ListEntry;
4031 if (coinstaller->DoPostProcessing)
4032 {
4033 Context.InstallResult = rc;
4034 Context.PrivateData = coinstaller->PrivateData;
4035 rc = (*coinstaller->Function)(InstallFunction, DeviceInfoSet, DeviceInfoData, &Context);
4036 }
4037 FreeFunctionPointer(coinstaller->Module, coinstaller->Function);
4038 ListEntry = ListEntry->Flink;
4039 }
4040
4041 /* Free allocated memory */
4042 while (!IsListEmpty(&ClassCoInstallersListHead))
4043 {
4044 ListEntry = RemoveHeadList(&ClassCoInstallersListHead);
4045 HeapFree(GetProcessHeap(), 0, ListEntry);
4046 }
4047 while (!IsListEmpty(&DeviceCoInstallersListHead))
4048 {
4049 ListEntry = RemoveHeadList(&DeviceCoInstallersListHead);
4050 HeapFree(GetProcessHeap(), 0, ListEntry);
4051 }
4052
4053 ret = (rc == NO_ERROR);
4054 }
4055 }
4056
4057 TRACE("Returning %d\n", ret);
4058 return ret;
4059 }
4060
4061 /***********************************************************************
4062 * SetupDiGetDeviceInfoListClass (SETUPAPI.@)
4063 */
4064 BOOL WINAPI SetupDiGetDeviceInfoListClass(
4065 IN HDEVINFO DeviceInfoSet,
4066 OUT LPGUID ClassGuid)
4067 {
4068 struct DeviceInfoSet *list;
4069 BOOL ret = FALSE;
4070
4071 TRACE("%p %p\n", DeviceInfoSet, ClassGuid);
4072
4073 if (!DeviceInfoSet)
4074 SetLastError(ERROR_INVALID_HANDLE);
4075 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4076 SetLastError(ERROR_INVALID_HANDLE);
4077 else if (IsEqualIID(&list->ClassGuid, &GUID_NULL))
4078 SetLastError(ERROR_NO_ASSOCIATED_CLASS);
4079 else
4080 {
4081 memcpy(&ClassGuid, &list->ClassGuid, sizeof(GUID));
4082
4083 ret = TRUE;
4084 }
4085
4086 TRACE("Returning %d\n", ret);
4087 return ret;
4088 }
4089
4090 /***********************************************************************
4091 * SetupDiGetDeviceInfoListDetailW (SETUPAPI.@)
4092 */
4093 BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
4094 IN HDEVINFO DeviceInfoSet,
4095 OUT PSP_DEVINFO_LIST_DETAIL_DATA_W DeviceInfoListDetailData)
4096 {
4097 struct DeviceInfoSet *list;
4098 BOOL ret = FALSE;
4099
4100 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoListDetailData);
4101
4102 if (!DeviceInfoSet)
4103 SetLastError(ERROR_INVALID_HANDLE);
4104 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4105 SetLastError(ERROR_INVALID_HANDLE);
4106 else if (!DeviceInfoListDetailData)
4107 SetLastError(ERROR_INVALID_PARAMETER);
4108 else if (DeviceInfoListDetailData->cbSize != sizeof(SP_DEVINFO_LIST_DETAIL_DATA_W))
4109 SetLastError(ERROR_INVALID_USER_BUFFER);
4110 else
4111 {
4112 memcpy(
4113 &DeviceInfoListDetailData->ClassGuid,
4114 &list->ClassGuid,
4115 sizeof(GUID));
4116 DeviceInfoListDetailData->RemoteMachineHandle = list->hMachine;
4117 if (list->MachineName)
4118 strcpyW(DeviceInfoListDetailData->RemoteMachineName, list->MachineName + 2);
4119 else
4120 DeviceInfoListDetailData->RemoteMachineName[0] = 0;
4121
4122 ret = TRUE;
4123 }
4124
4125 TRACE("Returning %d\n", ret);
4126 return ret;
4127 }
4128
4129 /***********************************************************************
4130 * SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
4131 */
4132 BOOL WINAPI SetupDiGetDeviceInstallParamsA(
4133 IN HDEVINFO DeviceInfoSet,
4134 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
4135 OUT PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
4136 {
4137 SP_DEVINSTALL_PARAMS_W deviceInstallParamsW;
4138 BOOL ret = FALSE;
4139
4140 TRACE("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
4141
4142 if (DeviceInstallParams == NULL)
4143 SetLastError(ERROR_INVALID_PARAMETER);
4144 else if (DeviceInstallParams->cbSize != sizeof(SP_DEVINSTALL_PARAMS_A))
4145 SetLastError(ERROR_INVALID_USER_BUFFER);
4146 else
4147 {
4148 deviceInstallParamsW.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
4149 ret = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &deviceInstallParamsW);
4150
4151 if (ret)
4152 {
4153 /* Do W->A conversion */
4154 memcpy(
4155 DeviceInstallParams,
4156 &deviceInstallParamsW,
4157 FIELD_OFFSET(SP_DEVINSTALL_PARAMS_W, DriverPath));
4158 if (WideCharToMultiByte(CP_ACP, 0, deviceInstallParamsW.DriverPath, -1,
4159 DeviceInstallParams->DriverPath, MAX_PATH, NULL, NULL) == 0)
4160 {
4161 DeviceInstallParams->DriverPath[0] = '\0';
4162 ret = FALSE;
4163 }
4164 }
4165 }
4166
4167 TRACE("Returning %d\n", ret);
4168 return ret;
4169 }
4170
4171 /***********************************************************************
4172 * SetupDiGetDeviceInstallParamsW (SETUPAPI.@)
4173 */
4174 BOOL WINAPI SetupDiGetDeviceInstallParamsW(
4175 IN HDEVINFO DeviceInfoSet,
4176 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
4177 OUT PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
4178 {
4179 struct DeviceInfoSet *list;
4180 BOOL ret = FALSE;
4181
4182 TRACE("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
4183
4184 if (!DeviceInfoSet)
4185 SetLastError(ERROR_INVALID_HANDLE);
4186 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4187 SetLastError(ERROR_INVALID_HANDLE);
4188 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
4189 SetLastError(ERROR_INVALID_USER_BUFFER);
4190 else if (!DeviceInstallParams)
4191 SetLastError(ERROR_INVALID_PARAMETER);
4192 else if (DeviceInstallParams->cbSize != sizeof(SP_DEVINSTALL_PARAMS_W))
4193 SetLastError(ERROR_INVALID_USER_BUFFER);
4194 else
4195 {
4196 PSP_DEVINSTALL_PARAMS_W Source;
4197
4198 if (DeviceInfoData)
4199 Source = &((struct DeviceInfoElement *)DeviceInfoData->Reserved)->InstallParams;
4200 else
4201 Source = &list->InstallParams;
4202 memcpy(DeviceInstallParams, Source, Source->cbSize);
4203 ret = TRUE;
4204 }
4205
4206 TRACE("Returning %d\n", ret);
4207 return ret;
4208 }
4209
4210 static BOOL
4211 CheckDeviceInstallParameters(
4212 IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
4213 {
4214 DWORD SupportedFlags =
4215 DI_NOVCP | /* 0x00000008 */
4216 DI_DIDCOMPAT | /* 0x00000010 */
4217 DI_DIDCLASS | /* 0x00000020 */
4218 DI_NEEDRESTART | /* 0x00000080 */
4219 DI_NEEDREBOOT | /* 0x00000100 */
4220 DI_RESOURCEPAGE_ADDED | /* 0x00002000 */
4221 DI_PROPERTIES_CHANGE | /* 0x00004000 */
4222 DI_ENUMSINGLEINF | /* 0x00010000 */
4223 DI_CLASSINSTALLPARAMS | /* 0x00100000 */
4224 DI_NODI_DEFAULTACTION | /* 0x00200000 */
4225 DI_QUIETINSTALL | /* 0x00800000 */
4226 DI_NOFILECOPY | /* 0x01000000 */
4227 DI_DRIVERPAGE_ADDED; /* 0x04000000 */
4228 DWORD SupportedFlagsEx =
4229 DI_FLAGSEX_CI_FAILED | /* 0x00000004 */
4230 DI_FLAGSEX_DIDINFOLIST | /* 0x00000010 */
4231 DI_FLAGSEX_DIDCOMPATINFO | /* 0x00000020 */
4232 DI_FLAGSEX_ALLOWEXCLUDEDDRVS | /* 0x00000800 */
4233 DI_FLAGSEX_NO_DRVREG_MODIFY; /* 0x00008000 */
4234 BOOL ret = FALSE;
4235
4236 /* FIXME: add support for more flags */
4237
4238 /* FIXME: DI_CLASSINSTALLPARAMS flag is not correctly used.
4239 * It should be checked before accessing to other values
4240 * of the SP_DEVINSTALL_PARAMS structure */
4241
4242 if (DeviceInstallParams->Flags & ~SupportedFlags)
4243 {
4244 FIXME("Unknown Flags: 0x%08lx\n", DeviceInstallParams->Flags & ~SupportedFlags);
4245 SetLastError(ERROR_INVALID_FLAGS);
4246 }
4247 else if (DeviceInstallParams->FlagsEx & ~SupportedFlagsEx)
4248 {
4249 FIXME("Unknown FlagsEx: 0x%08lx\n", DeviceInstallParams->FlagsEx & ~SupportedFlagsEx);
4250 SetLastError(ERROR_INVALID_FLAGS);
4251 }
4252 else if ((DeviceInstallParams->Flags & DI_NOVCP)
4253 && (DeviceInstallParams->FileQueue == NULL || DeviceInstallParams->FileQueue == (HSPFILEQ)INVALID_HANDLE_VALUE))
4254 SetLastError(ERROR_INVALID_USER_BUFFER);
4255 else
4256 {
4257 /* FIXME: check Reserved field */
4258 ret = TRUE;
4259 }
4260
4261 return ret;
4262 }
4263
4264 /***********************************************************************
4265 * SetupDiSetDeviceInstallParamsW (SETUPAPI.@)
4266 */
4267 BOOL WINAPI SetupDiSetDeviceInstallParamsW(
4268 IN HDEVINFO DeviceInfoSet,
4269 IN PSP_DEVINFO_DATA DeviceInfoData,
4270 IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
4271 {
4272 struct DeviceInfoSet *list;
4273 BOOL ret = FALSE;
4274
4275 TRACE("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
4276
4277 if (!DeviceInfoSet)
4278 SetLastError(ERROR_INVALID_HANDLE);
4279 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4280 SetLastError(ERROR_INVALID_HANDLE);
4281 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
4282 SetLastError(ERROR_INVALID_USER_BUFFER);
4283 else if (!DeviceInstallParams)
4284 SetLastError(ERROR_INVALID_PARAMETER);
4285 else if (DeviceInstallParams->cbSize != sizeof(SP_DEVINSTALL_PARAMS_W))
4286 SetLastError(ERROR_INVALID_USER_BUFFER);
4287 else if (CheckDeviceInstallParameters(DeviceInstallParams))
4288 {
4289 PSP_DEVINSTALL_PARAMS_W Destination;
4290
4291 if (DeviceInfoData)
4292 Destination = &((struct DeviceInfoElement *)DeviceInfoData->Reserved)->InstallParams;
4293 else
4294 Destination = &list->InstallParams;
4295 memcpy(Destination, DeviceInstallParams, DeviceInstallParams->cbSize);
4296 ret = TRUE;
4297 }
4298
4299 TRACE("Returning %d\n", ret);
4300 return ret;
4301 }
4302
4303 /***********************************************************************
4304 * SetupDiGetDeviceInstanceIdA(SETUPAPI.@)
4305 */
4306 BOOL WINAPI SetupDiGetDeviceInstanceIdA(
4307 IN HDEVINFO DeviceInfoSet,
4308 IN PSP_DEVINFO_DATA DeviceInfoData,
4309 OUT PSTR DeviceInstanceId OPTIONAL,
4310 IN DWORD DeviceInstanceIdSize,
4311 OUT PDWORD RequiredSize OPTIONAL)
4312 {
4313 PWSTR DeviceInstanceIdW = NULL;
4314 BOOL ret = FALSE;
4315
4316 TRACE("%p %p %p %lu %p\n", DeviceInfoSet, DeviceInfoData,
4317 DeviceInstanceId, DeviceInstanceIdSize, RequiredSize);
4318
4319 if (!DeviceInstanceId && DeviceInstanceIdSize > 0)
4320 SetLastError(ERROR_INVALID_PARAMETER);
4321 else
4322 {
4323 if (DeviceInstanceIdSize != 0)
4324 {
4325 DeviceInstanceIdW = MyMalloc(DeviceInstanceIdSize * sizeof(WCHAR));
4326 if (DeviceInstanceIdW == NULL)
4327 return FALSE;
4328 }
4329
4330 ret = SetupDiGetDeviceInstanceIdW(DeviceInfoSet, DeviceInfoData,
4331 DeviceInstanceIdW, DeviceInstanceIdSize,
4332 RequiredSize);
4333
4334 if (ret && DeviceInstanceIdW != NULL)
4335 {
4336 if (WideCharToMultiByte(CP_ACP, 0, DeviceInstanceIdW, -1,
4337 DeviceInstanceId, DeviceInstanceIdSize, NULL, NULL) == 0)
4338 {
4339 DeviceInstanceId[0] = '\0';
4340 ret = FALSE;
4341 }
4342 }
4343 }
4344
4345 TRACE("Returning %d\n", ret);
4346 return ret;
4347 }
4348
4349 /***********************************************************************
4350 * SetupDiGetDeviceInstanceIdW(SETUPAPI.@)
4351 */
4352 BOOL WINAPI SetupDiGetDeviceInstanceIdW(
4353 IN HDEVINFO DeviceInfoSet,
4354 IN PSP_DEVINFO_DATA DeviceInfoData,
4355 OUT PWSTR DeviceInstanceId OPTIONAL,
4356 IN DWORD DeviceInstanceIdSize,
4357 OUT PDWORD RequiredSize OPTIONAL)
4358 {
4359 BOOL ret = FALSE;
4360
4361 TRACE("%p %p %p %lu %p\n", DeviceInfoSet, DeviceInfoData,
4362 DeviceInstanceId, DeviceInstanceIdSize, RequiredSize);
4363
4364 if (!DeviceInfoSet)
4365 SetLastError(ERROR_INVALID_HANDLE);
4366 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4367 SetLastError(ERROR_INVALID_HANDLE);
4368 else if (!DeviceInfoData)
4369 SetLastError(ERROR_INVALID_PARAMETER);
4370 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
4371 SetLastError(ERROR_INVALID_USER_BUFFER);
4372 else if (!DeviceInstanceId && DeviceInstanceIdSize > 0)
4373 SetLastError(ERROR_INVALID_PARAMETER);
4374 else if (DeviceInstanceId && DeviceInstanceIdSize == 0)
4375 SetLastError(ERROR_INVALID_PARAMETER);
4376 else
4377 {
4378 struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
4379 DWORD required;
4380
4381 required = (strlenW(DevInfo->DeviceName) + 1) * sizeof(WCHAR);
4382 if (RequiredSize)
4383 *RequiredSize = required;
4384
4385 if (required <= DeviceInstanceIdSize)
4386 {
4387 strcpyW(DeviceInstanceId, DevInfo->DeviceName);
4388 ret = TRUE;
4389 }
4390 else
4391 SetLastError(ERROR_INSUFFICIENT_BUFFER);
4392 }
4393
4394 TRACE("Returning %d\n", ret);
4395 return ret;
4396 }
4397
4398 /***********************************************************************
4399 * SetupDiGetClassDevPropertySheetsA(SETUPAPI.@)
4400 */
4401 BOOL WINAPI SetupDiGetClassDevPropertySheetsA(
4402 IN HDEVINFO DeviceInfoSet,
4403 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
4404 IN LPPROPSHEETHEADERA PropertySheetHeader,
4405 IN DWORD PropertySheetHeaderPageListSize,
4406 OUT PDWORD RequiredSize OPTIONAL,
4407 IN DWORD PropertySheetType)
4408 {
4409 PROPSHEETHEADERW psh;
4410 BOOL ret = FALSE;
4411
4412 TRACE("%p %p %p 0%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
4413 PropertySheetHeader, PropertySheetHeaderPageListSize,
4414 RequiredSize, PropertySheetType);
4415
4416 psh.dwFlags = PropertySheetHeader->dwFlags;
4417 psh.phpage = PropertySheetHeader->phpage;
4418 psh.nPages = PropertySheetHeader->nPages;
4419
4420 ret = SetupDiGetClassDevPropertySheetsW(DeviceInfoSet, DeviceInfoData, PropertySheetHeader ? &psh : NULL,
4421 PropertySheetHeaderPageListSize, RequiredSize,
4422 PropertySheetType);
4423 if (ret)
4424 {
4425 PropertySheetHeader->nPages = psh.nPages;
4426 }
4427
4428 TRACE("Returning %d\n", ret);
4429 return ret;
4430 }
4431
4432 struct ClassDevPropertySheetsData
4433 {
4434 HPROPSHEETPAGE *PropertySheetPages;
4435 DWORD MaximumNumberOfPages;
4436 DWORD NumberOfPages;
4437 };
4438
4439 static BOOL WINAPI GetClassDevPropertySheetsCallback(
4440 IN HPROPSHEETPAGE hPropSheetPage,
4441 IN OUT LPARAM lParam)
4442 {
4443 struct ClassDevPropertySheetsData *PropPageData;
4444
4445 PropPageData = (struct ClassDevPropertySheetsData *)lParam;
4446
4447 if (PropPageData->NumberOfPages < PropPageData->MaximumNumberOfPages)
4448 {
4449 *PropPageData->PropertySheetPages = hPropSheetPage;
4450 PropPageData->PropertySheetPages++;
4451 }
4452
4453 PropPageData->NumberOfPages++;
4454 return TRUE;
4455 }
4456
4457 /***********************************************************************
4458 * SetupDiGetClassDevPropertySheetsW(SETUPAPI.@)
4459 */
4460 BOOL WINAPI SetupDiGetClassDevPropertySheetsW(
4461 IN HDEVINFO DeviceInfoSet,
4462 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
4463 IN OUT LPPROPSHEETHEADERW PropertySheetHeader,
4464 IN DWORD PropertySheetHeaderPageListSize,
4465 OUT PDWORD RequiredSize OPTIONAL,
4466 IN DWORD PropertySheetType)
4467 {
4468 struct DeviceInfoSet *list;
4469 BOOL ret = FALSE;
4470
4471 TRACE("%p %p %p 0%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
4472 PropertySheetHeader, PropertySheetHeaderPageListSize,
4473 RequiredSize, PropertySheetType);
4474
4475 if (!DeviceInfoSet)
4476 SetLastError(ERROR_INVALID_HANDLE);
4477 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4478 SetLastError(ERROR_INVALID_HANDLE);
4479 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4480 SetLastError(ERROR_INVALID_HANDLE);
4481 else if (!PropertySheetHeader)
4482 SetLastError(ERROR_INVALID_PARAMETER);
4483 else if (PropertySheetHeader->dwFlags & PSH_PROPSHEETPAGE)
4484 SetLastError(ERROR_INVALID_FLAGS);
4485 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
4486 SetLastError(ERROR_INVALID_USER_BUFFER);
4487 else if (!DeviceInfoData && IsEqualIID(&list->ClassGuid, &GUID_NULL))
4488 SetLastError(ERROR_INVALID_PARAMETER);
4489 else if (!PropertySheetHeader)
4490 SetLastError(ERROR_INVALID_PARAMETER);
4491 else if (PropertySheetType != DIGCDP_FLAG_ADVANCED
4492 && PropertySheetType != DIGCDP_FLAG_BASIC
4493 && PropertySheetType != DIGCDP_FLAG_REMOTE_ADVANCED
4494 && PropertySheetType != DIGCDP_FLAG_REMOTE_BASIC)
4495 SetLastError(ERROR_INVALID_PARAMETER);
4496 else
4497 {
4498 HKEY hKey = INVALID_HANDLE_VALUE;
4499 SP_PROPSHEETPAGE_REQUEST Request;
4500 LPWSTR PropPageProvider = NULL;
4501 HMODULE hModule = NULL;
4502 PROPERTY_PAGE_PROVIDER pPropPageProvider = NULL;
4503 struct ClassDevPropertySheetsData PropPageData;
4504 DWORD dwLength, dwRegType;
4505 DWORD rc;
4506
4507 if (DeviceInfoData)
4508 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
4509 else
4510 {
4511 hKey = SetupDiOpenClassRegKeyExW(&list->ClassGuid, KEY_QUERY_VALUE,
4512 DIOCR_INSTALLER, list->MachineName + 2, NULL);
4513 }
4514 if (hKey == INVALID_HANDLE_VALUE)
4515 goto cleanup;
4516
4517 rc = RegQueryValueExW(hKey, REGSTR_VAL_ENUMPROPPAGES_32, NULL, &dwRegType, NULL, &dwLength);
4518 if (rc == ERROR_FILE_NOT_FOUND)
4519 {
4520 /* No registry key. As it is optional, don't say it's a bad error */
4521 if (RequiredSize)
4522 *RequiredSize = 0;
4523 ret = TRUE;
4524 goto cleanup;
4525 }
4526 else if (rc != ERROR_SUCCESS && dwRegType != REG_SZ)
4527 {
4528 SetLastError(rc);
4529 goto cleanup;
4530 }
4531
4532 PropPageProvider = HeapAlloc(GetProcessHeap(), 0, dwLength + sizeof(WCHAR));
4533 if (!PropPageProvider)
4534 {
4535 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
4536 goto cleanup;
4537 }
4538 rc = RegQueryValueExW(hKey, REGSTR_VAL_ENUMPROPPAGES_32, NULL, NULL, (LPBYTE)PropPageProvider, &dwLength);
4539 if (rc != ERROR_SUCCESS)
4540 {
4541 SetLastError(rc);
4542 goto cleanup;
4543 }
4544 PropPageProvider[dwLength / sizeof(WCHAR)] = 0;
4545
4546 rc = GetFunctionPointer(PropPageProvider, &hModule, (PVOID*)&pPropPageProvider);
4547 if (rc != ERROR_SUCCESS)
4548 {
4549 SetLastError(ERROR_INVALID_PROPPAGE_PROVIDER);
4550 goto cleanup;
4551 }
4552
4553 Request.cbSize = sizeof(SP_PROPSHEETPAGE_REQUEST);
4554 Request.PageRequested = SPPSR_ENUM_ADV_DEVICE_PROPERTIES;
4555 Request.DeviceInfoSet = DeviceInfoSet;
4556 Request.DeviceInfoData = DeviceInfoData;
4557 PropPageData.PropertySheetPages = &PropertySheetHeader->phpage[PropertySheetHeader->nPages];
4558 PropPageData.MaximumNumberOfPages = PropertySheetHeaderPageListSize - PropertySheetHeader->nPages;
4559 PropPageData.NumberOfPages = 0;
4560 ret = pPropPageProvider(&Request, GetClassDevPropertySheetsCallback, (LPARAM)&PropPageData);
4561 if (!ret)
4562 goto cleanup;
4563
4564 if (RequiredSize)
4565 *RequiredSize = PropPageData.NumberOfPages + PropertySheetHeader->nPages;
4566 if (PropPageData.NumberOfPages <= PropPageData.MaximumNumberOfPages)
4567 {
4568 PropertySheetHeader->nPages += PropPageData.NumberOfPages;
4569 ret = TRUE;
4570 }
4571 else
4572 {
4573 PropertySheetHeader->nPages += PropPageData.MaximumNumberOfPages;
4574 SetLastError(ERROR_INSUFFICIENT_BUFFER);
4575 }
4576
4577 cleanup:
4578 if (hKey != INVALID_HANDLE_VALUE)
4579 RegCloseKey(hKey);
4580 HeapFree(GetProcessHeap(), 0, PropPageProvider);
4581 FreeFunctionPointer(hModule, pPropPageProvider);
4582 }
4583
4584 TRACE("Returning %d\n", ret);
4585 return ret;
4586 }
4587
4588 /***********************************************************************
4589 * SetupDiCreateDevRegKeyA (SETUPAPI.@)
4590 */
4591 HKEY WINAPI SetupDiCreateDevRegKeyA(
4592 IN HDEVINFO DeviceInfoSet,
4593 IN PSP_DEVINFO_DATA DeviceInfoData,
4594 IN DWORD Scope,
4595 IN DWORD HwProfile,
4596 IN DWORD KeyType,
4597 IN HINF InfHandle OPTIONAL,
4598 IN PCSTR InfSectionName OPTIONAL)
4599 {
4600 PCWSTR InfSectionNameW = NULL;
4601 HKEY ret = INVALID_HANDLE_VALUE;
4602
4603 if (InfSectionName)
4604 {
4605 InfSectionNameW = MultiByteToUnicode(InfSectionName, CP_ACP);
4606 if (InfSectionNameW == NULL) return INVALID_HANDLE_VALUE;
4607 }
4608
4609 ret = SetupDiCreateDevRegKeyW(DeviceInfoSet,
4610 DeviceInfoData,
4611 Scope,
4612 HwProfile,
4613 KeyType,
4614 InfHandle,
4615 InfSectionNameW);
4616
4617 if (InfSectionNameW != NULL)
4618 MyFree((PVOID)InfSectionNameW);
4619
4620 return ret;
4621 }
4622
4623 static HKEY
4624 OpenHardwareProfileKey(
4625 IN HKEY HKLM,
4626 IN DWORD HwProfile,
4627 IN DWORD samDesired)
4628 {
4629 HKEY hHWProfilesKey = INVALID_HANDLE_VALUE;
4630 HKEY hHWProfileKey = INVALID_HANDLE_VALUE;
4631 HKEY ret = INVALID_HANDLE_VALUE;
4632 LONG rc;
4633
4634 rc = RegOpenKeyExW(HKLM,
4635 REGSTR_PATH_HWPROFILES,
4636 0,
4637 0,
4638 &hHWProfilesKey);
4639 if (rc != ERROR_SUCCESS)
4640 {
4641 SetLastError(rc);
4642 goto cleanup;
4643 }
4644 if (HwProfile == 0)
4645 {
4646 rc = RegOpenKeyExW(
4647 hHWProfilesKey,
4648 REGSTR_KEY_CURRENT,
4649 0,
4650 KEY_CREATE_SUB_KEY,
4651 &hHWProfileKey);
4652 }
4653 else
4654 {
4655 WCHAR subKey[5];
4656 snprintfW(subKey, 4, L"%04lu", HwProfile);
4657 subKey[4] = '\0';
4658 rc = RegOpenKeyExW(
4659 hHWProfilesKey,
4660 subKey,
4661 0,
4662 KEY_CREATE_SUB_KEY,
4663 &hHWProfileKey);
4664 }
4665 if (rc != ERROR_SUCCESS)
4666 {
4667 SetLastError(rc);
4668 goto cleanup;
4669 }
4670 ret = hHWProfileKey;
4671
4672 cleanup:
4673 if (hHWProfilesKey != INVALID_HANDLE_VALUE)
4674 RegCloseKey(hHWProfilesKey);
4675 if (hHWProfileKey != INVALID_HANDLE_VALUE && hHWProfileKey != ret)
4676 RegCloseKey(hHWProfileKey);
4677 return ret;
4678 }
4679
4680 /***********************************************************************
4681 * SetupDiCreateDevRegKeyW (SETUPAPI.@)
4682 */
4683 HKEY WINAPI SetupDiCreateDevRegKeyW(
4684 IN HDEVINFO DeviceInfoSet,
4685 IN PSP_DEVINFO_DATA DeviceInfoData,
4686 IN DWORD Scope,
4687 IN DWORD HwProfile,
4688 IN DWORD KeyType,
4689 IN HINF InfHandle OPTIONAL,
4690 IN PCWSTR InfSectionName OPTIONAL)
4691 {
4692 struct DeviceInfoSet *list;
4693 HKEY ret = INVALID_HANDLE_VALUE;
4694
4695 TRACE("%p %p %lu %lu %lu %p %s\n", DeviceInfoSet, DeviceInfoData,
4696 Scope, HwProfile, KeyType, InfHandle, debugstr_w(InfSectionName));
4697
4698 if (!DeviceInfoSet)
4699 SetLastError(ERROR_INVALID_HANDLE);
4700 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4701 SetLastError(ERROR_INVALID_HANDLE);
4702 else if (!DeviceInfoData)
4703 SetLastError(ERROR_INVALID_PARAMETER);
4704 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
4705 SetLastError(ERROR_INVALID_USER_BUFFER);
4706 else if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
4707 SetLastError(ERROR_INVALID_PARAMETER);
4708 else if (KeyType != DIREG_DEV && KeyType != DIREG_DRV)
4709 SetLastError(ERROR_INVALID_PARAMETER);
4710 else if (InfHandle && !InfSectionName)
4711 SetLastError(ERROR_INVALID_PARAMETER);
4712 else if (!InfHandle && InfSectionName)
4713 SetLastError(ERROR_INVALID_PARAMETER);
4714 else
4715 {
4716 LPWSTR lpGuidString = NULL;
4717 LPWSTR DriverKey = NULL; /* {GUID}\Index */
4718 LPWSTR pDeviceInstance; /* Points into DriverKey, on the Index field */
4719 DWORD Index; /* Index used in the DriverKey name */
4720 DWORD rc;
4721 HKEY hHWProfileKey = INVALID_HANDLE_VALUE;
4722 HKEY hEnumKey = INVALID_HANDLE_VALUE;
4723 HKEY hClassKey = INVALID_HANDLE_VALUE;
4724 HKEY hDeviceKey = INVALID_HANDLE_VALUE;
4725 HKEY hKey = INVALID_HANDLE_VALUE;
4726 HKEY RootKey;
4727
4728 if (Scope == DICS_FLAG_GLOBAL)
4729 RootKey = list->HKLM;
4730 else /* Scope == DICS_FLAG_CONFIGSPECIFIC */
4731 {
4732 hHWProfileKey = OpenHardwareProfileKey(list->HKLM, HwProfile, KEY_CREATE_SUB_KEY);
4733 if (hHWProfileKey == INVALID_HANDLE_VALUE)
4734 goto cleanup;
4735 RootKey = hHWProfileKey;
4736 }
4737
4738 if (KeyType == DIREG_DEV)
4739 {
4740 struct DeviceInfoElement *deviceInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
4741
4742 rc = RegCreateKeyExW(
4743 RootKey,
4744 REGSTR_PATH_SYSTEMENUM,
4745 0,
4746 NULL,
4747 REG_OPTION_NON_VOLATILE,
4748 KEY_CREATE_SUB_KEY,
4749 NULL,
4750 &hEnumKey,
4751 NULL);
4752 if (rc != ERROR_SUCCESS)
4753 {
4754 SetLastError(rc);
4755 goto cleanup;
4756 }
4757 rc = RegCreateKeyExW(
4758 hEnumKey,
4759 deviceInfo->DeviceName,
4760 0,
4761 NULL,
4762 REG_OPTION_NON_VOLATILE,
4763 #if _WIN32_WINNT >= 0x502
4764 KEY_READ | KEY_WRITE,
4765 #else
4766 KEY_ALL_ACCESS,
4767 #endif
4768 NULL,
4769 &hKey,
4770 NULL);
4771 if (rc != ERROR_SUCCESS)
4772 {
4773 SetLastError(rc);
4774 goto cleanup;
4775 }
4776 }
4777 else /* KeyType == DIREG_DRV */
4778 {
4779 if (UuidToStringW((UUID*)&DeviceInfoData->ClassGuid, &lpGuidString) != RPC_S_OK)
4780 goto cleanup;
4781 /* The driver key is in \System\CurrentControlSet\Control\Class\{GUID}\Index */
4782 DriverKey = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpGuidString) + 7) * sizeof(WCHAR) + sizeof(UNICODE_STRING));
4783 if (!DriverKey)
4784 {
4785 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
4786 goto cleanup;
4787 }
4788 strcpyW(DriverKey, L"{");
4789 strcatW(DriverKey, lpGuidString);
4790 strcatW(DriverKey, L"}\\");
4791 pDeviceInstance = &DriverKey[strlenW(DriverKey)];
4792 rc = RegOpenKeyExW(RootKey,
4793 REGSTR_PATH_CLASS_NT,
4794 0,
4795 KEY_CREATE_SUB_KEY,
4796 &hClassKey);
4797 if (rc != ERROR_SUCCESS)
4798 {
4799 SetLastError(rc);
4800 goto cleanup;
4801 }
4802
4803 /* Try all values for Index between 0 and 9999 */
4804 Index = 0;
4805 while (Index <= 9999)
4806 {
4807 DWORD Disposition;
4808 wsprintf(pDeviceInstance, L"%04lu", Index);
4809 rc = RegCreateKeyEx(hClassKey,
4810 DriverKey,
4811 0,
4812 NULL,
4813 REG_OPTION_NON_VOLATILE,
4814 #if _WIN32_WINNT >= 0x502
4815 KEY_READ | KEY_WRITE,
4816 #else
4817 KEY_ALL_ACCESS,
4818 #endif
4819 NULL,
4820 &hKey,
4821 &Disposition);
4822 if (rc != ERROR_SUCCESS)
4823 {
4824 SetLastError(rc);
4825 goto cleanup;
4826 }
4827 if (Disposition == REG_CREATED_NEW_KEY)
4828 break;
4829 RegCloseKey(hKey);
4830 hKey = INVALID_HANDLE_VALUE;
4831 Index++;
4832 }
4833 if (Index > 9999)
4834 {
4835 /* Unable to create more than 9999 devices within the same class */
4836 SetLastError(ERROR_GEN_FAILURE);
4837 goto cleanup;
4838 }
4839
4840 /* Open device key, to write Driver value */
4841 hDeviceKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, DIREG_DEV, KEY_SET_VALUE);
4842 if (hDeviceKey == INVALID_HANDLE_VALUE)
4843 goto cleanup;
4844 rc = RegSetValueEx(hDeviceKey, REGSTR_VAL_DRIVER, 0, REG_SZ, (const BYTE *)DriverKey, (strlenW(DriverKey) + 1) * sizeof(WCHAR));
4845 if (rc != ERROR_SUCCESS)
4846 {
4847 SetLastError(rc);
4848 goto cleanup;
4849 }
4850 }
4851
4852 /* Do installation of the specified section */
4853 if (InfHandle)
4854 {
4855 FIXME("Need to install section %s in file %p\n",
4856 debugstr_w(InfSectionName), InfHandle);
4857 }
4858 ret = hKey;
4859
4860 cleanup:
4861 if (lpGuidString)
4862 RpcStringFreeW(&lpGuidString);
4863 HeapFree(GetProcessHeap(), 0, DriverKey);
4864 if (hHWProfileKey != INVALID_HANDLE_VALUE)
4865 RegCloseKey(hHWProfileKey);
4866 if (hEnumKey != INVALID_HANDLE_VALUE)
4867 RegCloseKey(hEnumKey);
4868 if (hClassKey != INVALID_HANDLE_VALUE)
4869 RegCloseKey(hClassKey);
4870 if (hDeviceKey != INVALID_HANDLE_VALUE)
4871 RegCloseKey(hDeviceKey);
4872 if (hKey != INVALID_HANDLE_VALUE && hKey != ret)
4873 RegCloseKey(hKey);
4874 }
4875
4876 TRACE("Returning 0x%p\n", ret);
4877 return ret;
4878 }
4879
4880 /***********************************************************************
4881 * SetupDiOpenDevRegKey (SETUPAPI.@)
4882 */
4883 HKEY WINAPI SetupDiOpenDevRegKey(
4884 HDEVINFO DeviceInfoSet,
4885 PSP_DEVINFO_DATA DeviceInfoData,
4886 DWORD Scope,
4887 DWORD HwProfile,
4888 DWORD KeyType,
4889 REGSAM samDesired)
4890 {
4891 struct DeviceInfoSet *list;
4892 HKEY ret = INVALID_HANDLE_VALUE;
4893
4894 TRACE("%p %p %lu %lu %lu 0x%lx\n", DeviceInfoSet, DeviceInfoData,
4895 Scope, HwProfile, KeyType, samDesired);
4896
4897 if (!DeviceInfoSet)
4898 SetLastError(ERROR_INVALID_HANDLE);
4899 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
4900 SetLastError(ERROR_INVALID_HANDLE);
4901 else if (!DeviceInfoData)
4902 SetLastError(ERROR_INVALID_PARAMETER);
4903 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
4904 SetLastError(ERROR_INVALID_USER_BUFFER);
4905 else if (Scope != DICS_FLAG_GLOBAL && Scope != DICS_FLAG_CONFIGSPECIFIC)
4906 SetLastError(ERROR_INVALID_PARAMETER);
4907 else if (KeyType != DIREG_DEV && KeyType != DIREG_DRV)
4908 SetLastError(ERROR_INVALID_PARAMETER);
4909 else
4910 {
4911 struct DeviceInfoElement *deviceInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
4912 LPWSTR DriverKey = NULL;
4913 DWORD dwLength = 0;
4914 DWORD dwRegType;
4915 DWORD rc;
4916 HKEY hHWProfileKey = INVALID_HANDLE_VALUE;
4917 HKEY hEnumKey = INVALID_HANDLE_VALUE;
4918 HKEY hKey = INVALID_HANDLE_VALUE;
4919 HKEY RootKey;
4920
4921 if (Scope == DICS_FLAG_GLOBAL)
4922 RootKey = list->HKLM;
4923 else /* Scope == DICS_FLAG_CONFIGSPECIFIC */
4924 {
4925 hHWProfileKey = OpenHardwareProfileKey(list->HKLM, HwProfile, 0);
4926 if (hHWProfileKey == INVALID_HANDLE_VALUE)
4927 goto cleanup;
4928 RootKey = hHWProfileKey;
4929 }
4930
4931 rc = RegOpenKeyExW(
4932 RootKey,
4933 REGSTR_PATH_SYSTEMENUM,
4934 0, /* Options */
4935 0,
4936 &hEnumKey);
4937 if (rc != ERROR_SUCCESS)
4938 {
4939 SetLastError(rc);
4940 goto cleanup;
4941 }
4942 rc = RegOpenKeyExW(
4943 hEnumKey,
4944 deviceInfo->DeviceName,
4945 0, /* Options */
4946 KeyType == DIREG_DEV ? samDesired : KEY_QUERY_VALUE,
4947 &hKey);
4948 RegCloseKey(hEnumKey);
4949 hEnumKey = INVALID_HANDLE_VALUE;
4950 if (rc != ERROR_SUCCESS)
4951 {
4952 SetLastError(rc);
4953 goto cleanup;
4954 }
4955 if (KeyType == DIREG_DEV)
4956 {
4957 /* We're done. Just return the hKey handle */
4958 ret = hKey;
4959 goto cleanup;
4960 }
4961 /* Read the 'Driver' key */
4962 rc = RegQueryValueExW(hKey, REGSTR_VAL_DRIVER, NULL, &dwRegType, NULL, &dwLength);
4963 if (rc != ERROR_SUCCESS)
4964 {
4965 SetLastError(rc);
4966 goto cleanup;
4967 }
4968 else if (dwRegType != REG_SZ)
4969 {
4970 SetLastError(ERROR_GEN_FAILURE);
4971 goto cleanup;
4972 }
4973 DriverKey = HeapAlloc(GetProcessHeap(), 0, dwLength);
4974 if (!DriverKey)
4975 {
4976 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
4977 goto cleanup;
4978 }
4979 rc = RegQueryValueExW(hKey, REGSTR_VAL_DRIVER, NULL, &dwRegType, (LPBYTE)DriverKey, &dwLength);
4980 if (rc != ERROR_SUCCESS)
4981 {
4982 SetLastError(rc);
4983 goto cleanup;
4984 }
4985 RegCloseKey(hKey);
4986 hKey = INVALID_HANDLE_VALUE;
4987 /* Need to open the driver key */
4988 rc = RegOpenKeyExW(
4989 RootKey,
4990 REGSTR_PATH_CLASS_NT,
4991 0, /* Options */
4992 0,
4993 &hEnumKey);
4994 if (rc != ERROR_SUCCESS)
4995 {
4996 SetLastError(rc);
4997 goto cleanup;
4998 }
4999 rc = RegOpenKeyExW(
5000 hEnumKey,
5001 DriverKey,
5002 0, /* Options */
5003 samDesired,
5004 &hKey);
5005 if (rc != ERROR_SUCCESS)
5006 {
5007 SetLastError(rc);
5008 goto cleanup;
5009 }
5010 ret = hKey;
5011
5012 cleanup:
5013 if (hHWProfileKey != INVALID_HANDLE_VALUE)
5014 RegCloseKey(hHWProfileKey);
5015 if (hEnumKey != INVALID_HANDLE_VALUE)
5016 RegCloseKey(hEnumKey);
5017 if (hKey != INVALID_HANDLE_VALUE && hKey != ret)
5018 RegCloseKey(hKey);
5019 }
5020
5021 TRACE("Returning 0x%p\n", ret);
5022 return ret;
5023 }
5024
5025 /***********************************************************************
5026 * SetupDiCreateDeviceInfoA (SETUPAPI.@)
5027 */
5028 BOOL WINAPI SetupDiCreateDeviceInfoA(
5029 HDEVINFO DeviceInfoSet,
5030 PCSTR DeviceName,
5031 CONST GUID *ClassGuid,
5032 PCSTR DeviceDescription,
5033 HWND hwndParent,
5034 DWORD CreationFlags,
5035 PSP_DEVINFO_DATA DeviceInfoData)
5036 {
5037 LPWSTR DeviceNameW = NULL;
5038 LPWSTR DeviceDescriptionW = NULL;
5039 BOOL bResult;
5040
5041 TRACE("\n");
5042
5043 if (DeviceName)
5044 {
5045 DeviceNameW = MultiByteToUnicode(DeviceName, CP_ACP);
5046 if (DeviceNameW == NULL) return FALSE;
5047 }
5048 if (DeviceDescription)
5049 {
5050 DeviceDescriptionW = MultiByteToUnicode(DeviceDescription, CP_ACP);
5051 if (DeviceDescriptionW == NULL)
5052 {
5053 if (DeviceNameW) MyFree(DeviceNameW);
5054 return FALSE;
5055 }
5056 }
5057
5058 bResult = SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceNameW,
5059 ClassGuid, DeviceDescriptionW,
5060 hwndParent, CreationFlags,
5061 DeviceInfoData);
5062
5063 if (DeviceNameW) MyFree(DeviceNameW);
5064 if (DeviceDescriptionW) MyFree(DeviceDescriptionW);
5065
5066 return bResult;
5067 }
5068
5069 /***********************************************************************
5070 * SetupDiCreateDeviceInfoW (SETUPAPI.@)
5071 */
5072 BOOL WINAPI SetupDiCreateDeviceInfoW(
5073 HDEVINFO DeviceInfoSet,
5074 PCWSTR DeviceName,
5075 CONST GUID *ClassGuid,
5076 PCWSTR DeviceDescription,
5077 HWND hwndParent,
5078 DWORD CreationFlags,
5079 PSP_DEVINFO_DATA DeviceInfoData)
5080 {
5081 struct DeviceInfoSet *list;
5082 BOOL ret = FALSE;
5083
5084 TRACE("%p %s %s %s %p %lx %p\n", DeviceInfoSet, debugstr_w(DeviceName),
5085 debugstr_guid(ClassGuid), debugstr_w(DeviceDescription),
5086 hwndParent, CreationFlags, DeviceInfoData);
5087
5088 if (!DeviceInfoSet)
5089 SetLastError(ERROR_INVALID_HANDLE);
5090 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
5091 SetLastError(ERROR_INVALID_HANDLE);
5092 else if (!ClassGuid)
5093 SetLastError(ERROR_INVALID_PARAMETER);
5094 else if (!IsEqualIID(&list->ClassGuid, &GUID_NULL) && !IsEqualIID(&list->ClassGuid, ClassGuid))
5095 SetLastError(ERROR_CLASS_MISMATCH);
5096 else if (CreationFlags & ~(DICD_GENERATE_ID | DICD_INHERIT_CLASSDRVS))
5097 {
5098 TRACE("Unknown flags: 0x%08lx\n", CreationFlags & ~(DICD_GENERATE_ID | DICD_INHERIT_CLASSDRVS));
5099 SetLastError(ERROR_INVALID_FLAGS);
5100 }
5101 else
5102 {
5103 SP_DEVINFO_DATA DevInfo;
5104
5105 if (CreationFlags & DICD_GENERATE_ID)
5106 {
5107 /* Generate a new unique ID for this device */
5108 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
5109 FIXME("not implemented\n");
5110 }
5111 else
5112 {
5113 /* Device name is fully qualified. Try to open it */
5114 BOOL rc;
5115
5116 DevInfo.cbSize = sizeof(SP_DEVINFO_DATA);
5117 rc = SetupDiOpenDeviceInfoW(
5118 DeviceInfoSet,
5119 DeviceName,
5120 NULL, /* hwndParent */
5121 CreationFlags & DICD_INHERIT_CLASSDRVS ? DIOD_INHERIT_CLASSDRVS : 0,
5122 &DevInfo);
5123
5124 if (rc)
5125 {
5126 /* SetupDiOpenDeviceInfoW has already added
5127 * the device info to the device info set
5128 */
5129 SetLastError(ERROR_DEVINST_ALREADY_EXISTS);
5130 }
5131 else if (GetLastError() == ERROR_FILE_NOT_FOUND)
5132 {
5133 struct DeviceInfoElement *deviceInfo;
5134
5135 if (CreateDeviceInfoElement(list, DeviceName, ClassGuid, &deviceInfo))
5136 {
5137 InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
5138
5139 if (!DeviceInfoData)
5140 ret = TRUE;
5141 else
5142 {
5143 if (DeviceInfoData->cbSize != sizeof(PSP_DEVINFO_DATA))
5144 {
5145 SetLastError(ERROR_INVALID_USER_BUFFER);
5146 }
5147 else
5148 {
5149 memcpy(&DeviceInfoData->ClassGuid, ClassGuid, sizeof(GUID));
5150 DeviceInfoData->DevInst = deviceInfo->dnDevInst;
5151 DeviceInfoData->Reserved = (ULONG_PTR)deviceInfo;
5152 ret = TRUE;
5153 }
5154 }
5155 }
5156 }
5157 }
5158 }
5159
5160 TRACE("Returning %d\n", ret);
5161 return ret;
5162 }
5163
5164 /***********************************************************************
5165 * Helper functions for SetupDiBuildDriverInfoList
5166 */
5167 static BOOL
5168 AddDriverToList(
5169 IN PLIST_ENTRY DriverListHead,
5170 IN DWORD DriverType, /* SPDIT_CLASSDRIVER or SPDIT_COMPATDRIVER */
5171 IN LPGUID ClassGuid,
5172 IN INFCONTEXT ContextDevice,
5173 IN struct InfFileDetails *InfFileDetails,
5174 IN LPCWSTR InfFile,
5175 IN LPCWSTR ProviderName,
5176 IN LPCWSTR ManufacturerName,
5177 IN LPCWSTR MatchingId,
5178 FILETIME DriverDate,
5179 DWORDLONG DriverVersion,
5180 IN DWORD Rank)
5181 {
5182 struct DriverInfoElement *driverInfo = NULL;
5183 HANDLE hFile = INVALID_HANDLE_VALUE;
5184 DWORD RequiredSize = 128; /* Initial buffer size */
5185 BOOL Result = FALSE;
5186 PLIST_ENTRY PreviousEntry;
5187 LPWSTR InfInstallSection = NULL;
5188 BOOL ret = FALSE;
5189
5190 driverInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(struct DriverInfoElement));
5191 if (!driverInfo)
5192 {
5193 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5194 goto cleanup;
5195 }
5196 memset(driverInfo, 0, sizeof(struct DriverInfoElement));
5197
5198 driverInfo->Details.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
5199 driverInfo->Details.Reserved = (ULONG_PTR)driverInfo;
5200
5201 /* Copy InfFileName field */
5202 strncpyW(driverInfo->Details.InfFileName, InfFile, MAX_PATH - 1);
5203 driverInfo->Details.InfFileName[MAX_PATH - 1] = '\0';
5204
5205 /* Fill InfDate field */
5206 /* FIXME: hFile = CreateFile(driverInfo->Details.InfFileName,
5207 GENERIC_READ, FILE_SHARE_READ,
5208 NULL, OPEN_EXISTING, 0, NULL);
5209 if (hFile == INVALID_HANDLE_VALUE)
5210 goto cleanup;
5211 Result = GetFileTime(hFile, NULL, NULL, &driverInfo->Details.InfDate);
5212 if (!Result)
5213 goto cleanup;*/
5214
5215 /* Fill SectionName field */
5216 Result = SetupGetStringFieldW(
5217 &ContextDevice,
5218 1,
5219 driverInfo->Details.SectionName, LINE_LEN,
5220 NULL);
5221 if (!Result)
5222 goto cleanup;
5223
5224 /* Fill DrvDescription field */
5225 Result = SetupGetStringFieldW(
5226 &ContextDevice,
5227 0, /* Field index */
5228 driverInfo->Details.DrvDescription, LINE_LEN,
5229 NULL);
5230
5231 /* Copy MatchingId information */
5232 if (MatchingId)
5233 {
5234 driverInfo->MatchingId = HeapAlloc(GetProcessHeap(), 0, (strlenW(MatchingId) + 1) * sizeof(WCHAR));
5235 if (!driverInfo->MatchingId)
5236 {
5237 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5238 goto cleanup;
5239 }
5240 RtlCopyMemory(driverInfo->MatchingId, MatchingId, (strlenW(MatchingId) + 1) * sizeof(WCHAR));
5241 }
5242 else
5243 driverInfo->MatchingId = NULL;
5244
5245 /* Get inf install section */
5246 Result = FALSE;
5247 RequiredSize = 128; /* Initial buffer size */
5248 SetLastError(ERROR_INSUFFICIENT_BUFFER);
5249 while (!Result && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
5250 {
5251 HeapFree(GetProcessHeap(), 0, InfInstallSection);
5252 InfInstallSection = HeapAlloc(GetProcessHeap(), 0, RequiredSize * sizeof(WCHAR));
5253 if (!InfInstallSection)
5254 goto cleanup;
5255 Result = SetupGetStringFieldW(
5256 &ContextDevice,
5257 1, /* Field index */
5258 InfInstallSection, RequiredSize,
5259 &RequiredSize);
5260 }
5261 if (!Result)
5262 goto cleanup;
5263
5264 TRACE("Adding driver '%s' [%s/%s] (Rank 0x%lx)\n",
5265 debugstr_w(driverInfo->Details.DrvDescription), debugstr_w(InfFile),
5266 debugstr_w(InfInstallSection), Rank);
5267
5268 driverInfo->DriverRank = Rank;
5269 memcpy(&driverInfo->DriverDate, &DriverDate, sizeof(FILETIME));
5270 memcpy(&driverInfo->ClassGuid, ClassGuid, sizeof(GUID));
5271 driverInfo->Info.DriverType = DriverType;
5272 driverInfo->Info.Reserved = (ULONG_PTR)driverInfo;
5273 strncpyW(driverInfo->Info.Description, driverInfo->Details.DrvDescription, LINE_LEN - 1);
5274 driverInfo->Info.Description[LINE_LEN - 1] = '\0';
5275 strncpyW(driverInfo->Info.MfgName, ManufacturerName, LINE_LEN - 1);
5276 driverInfo->Info.MfgName[LINE_LEN - 1] = '\0';
5277 if (ProviderName)
5278 {
5279 strncpyW(driverInfo->Info.ProviderName, ProviderName, LINE_LEN - 1);
5280 driverInfo->Info.ProviderName[LINE_LEN - 1] = '\0';
5281 }
5282 else
5283 driverInfo->Info.ProviderName[0] = '\0';
5284 driverInfo->Info.DriverDate = DriverDate;
5285 driverInfo->Info.DriverVersion = DriverVersion;
5286 ReferenceInfFile(InfFileDetails);
5287 driverInfo->InfFileDetails = InfFileDetails;
5288
5289 /* Insert current driver in driver list, according to its rank */
5290 PreviousEntry = DriverListHead->Flink;
5291 while (PreviousEntry != DriverListHead)
5292 {
5293 struct DriverInfoElement *CurrentDriver;
5294 CurrentDriver = CONTAINING_RECORD(PreviousEntry, struct DriverInfoElement, ListEntry);
5295 if (CurrentDriver->DriverRank > Rank ||
5296 (CurrentDriver->DriverRank == Rank && CurrentDriver->DriverDate.QuadPart > driverInfo->DriverDate.QuadPart))
5297 {
5298 /* Insert before the current item */
5299 InsertHeadList(PreviousEntry, &driverInfo->ListEntry);
5300 break;
5301 }
5302 PreviousEntry = PreviousEntry->Flink;
5303 }
5304 if (PreviousEntry == DriverListHead)
5305 {
5306 /* Insert at the end of the list */
5307 InsertTailList(DriverListHead, &driverInfo->ListEntry);
5308 }
5309
5310 ret = TRUE;
5311
5312 cleanup:
5313 if (!ret)
5314 {
5315 if (driverInfo)
5316 HeapFree(GetProcessHeap(), 0, driverInfo->MatchingId);
5317 HeapFree(GetProcessHeap(), 0, driverInfo);
5318 }
5319 if (hFile != INVALID_HANDLE_VALUE)
5320 CloseHandle(hFile);
5321 HeapFree(GetProcessHeap(), 0, InfInstallSection);
5322
5323 return ret;
5324 }
5325
5326 static BOOL
5327 GetVersionInformationFromInfFile(
5328 IN HINF hInf,
5329 OUT LPGUID ClassGuid,
5330 OUT LPWSTR* pProviderName,
5331 OUT FILETIME* DriverDate,
5332 OUT DWORDLONG* DriverVersion)
5333 {
5334 DWORD RequiredSize;
5335 WCHAR guidW[MAX_GUID_STRING_LEN + 1];
5336 LPWSTR DriverVer = NULL;
5337 LPWSTR ProviderName = NULL;
5338 LPWSTR pComma; /* Points into DriverVer */
5339 LPWSTR pVersion = NULL; /* Points into DriverVer */
5340 SYSTEMTIME SystemTime;
5341 BOOL Result;
5342 BOOL ret = FALSE; /* Final result */
5343
5344 /* Get class Guid */
5345 if (!SetupGetLineTextW(
5346 NULL, /* Context */
5347 hInf,
5348 Version, ClassGUID,
5349 guidW, sizeof(guidW),
5350 NULL /* Required size */))
5351 {
5352 goto cleanup;
5353 }
5354 guidW[37] = '\0'; /* Replace the } by a NULL character */
5355 if (UuidFromStringW(&guidW[1], ClassGuid) != RPC_S_OK)
5356 {
5357 SetLastError(ERROR_GEN_FAILURE);
5358 goto cleanup;
5359 }
5360
5361 /* Get provider name */
5362 Result = SetupGetLineTextW(
5363 NULL, /* Context */
5364 hInf, Version, INF_PROVIDER,
5365 NULL, 0,
5366 &RequiredSize);
5367 if (Result)
5368 {
5369 /* We know know the needed buffer size */
5370 ProviderName = HeapAlloc(GetProcessHeap(), 0, RequiredSize * sizeof(WCHAR));
5371 if (!ProviderName)
5372 {
5373 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5374 goto cleanup;
5375 }
5376 Result = SetupGetLineTextW(
5377 NULL, /* Context */
5378 hInf, Version, INF_PROVIDER,
5379 ProviderName, RequiredSize,
5380 &RequiredSize);
5381 }
5382 if (!Result)
5383 goto cleanup;
5384 *pProviderName = ProviderName;
5385
5386 /* Read the "DriverVer" value */
5387 Result = SetupGetLineTextW(
5388 NULL, /* Context */
5389 hInf, Version, INF_DRIVER_VER,
5390 NULL, 0,
5391 &RequiredSize);
5392 if (Result)
5393 {
5394 /* We know know the needed buffer size */
5395 DriverVer = HeapAlloc(GetProcessHeap(), 0, RequiredSize * sizeof(WCHAR));
5396 if (!DriverVer)
5397 {
5398 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5399 goto cleanup;
5400 }
5401 Result = SetupGetLineTextW(
5402 NULL, /* Context */
5403 hInf, Version, INF_DRIVER_VER,
5404 DriverVer, RequiredSize,
5405 &RequiredSize);
5406 }
5407 if (!Result)
5408 goto cleanup;
5409
5410 /* Get driver date and driver version, by analyzing the "DriverVer" value */
5411 pComma = strchrW(DriverVer, ',');
5412 if (pComma != NULL)
5413 {
5414 *pComma = UNICODE_NULL;
5415 pVersion = pComma + 1;
5416 }
5417 /* Get driver date version. Invalid date = 00/00/00 */
5418 memset(DriverDate, 0, sizeof(FILETIME));
5419 if (strlenW(DriverVer) == 10
5420 && (DriverVer[2] == '-' || DriverVer[2] == '/')
5421 && (DriverVer[5] == '-' || DriverVer[5] == '/'))
5422 {
5423 memset(&SystemTime, 0, sizeof(SYSTEMTIME));
5424 DriverVer[2] = DriverVer[5] = UNICODE_NULL;
5425 SystemTime.wMonth = ((DriverVer[0] - '0') * 10) + DriverVer[1] - '0';
5426 SystemTime.wDay = ((DriverVer[3] - '0') * 10) + DriverVer[4] - '0';
5427 SystemTime.wYear = ((DriverVer[6] - '0') * 1000) + ((DriverVer[7] - '0') * 100) + ((DriverVer[8] - '0') * 10) + DriverVer[9] - '0';
5428 SystemTimeToFileTime(&SystemTime, DriverDate);
5429 }
5430 /* Get driver version. Invalid version = 0.0.0.0 */
5431 *DriverVersion = 0;
5432 if (pVersion)
5433 {
5434 WORD Major, Minor = 0, Revision = 0, Build = 0;
5435 LPWSTR pMinor = NULL, pRevision = NULL, pBuild = NULL;
5436 LARGE_INTEGER fullVersion;
5437
5438 pMinor = strchrW(pVersion, '.');
5439 if (pMinor)
5440 {
5441 *pMinor = 0;
5442 pRevision = strchrW(++pMinor, '.');
5443 Minor = atoiW(pMinor);
5444 }
5445 if (pRevision)
5446 {
5447 *pRevision = 0;
5448 pBuild = strchrW(++pRevision, '.');
5449 Revision = atoiW(pRevision);
5450 }
5451 if (pBuild)
5452 {
5453 *pBuild = 0;
5454 pBuild++;
5455 Build = atoiW(pBuild);
5456 }
5457 Major = atoiW(pVersion);
5458 fullVersion.u.HighPart = Major << 16 | Minor;
5459 fullVersion.u.LowPart = Revision << 16 | Build;
5460 memcpy(DriverVersion, &fullVersion, sizeof(LARGE_INTEGER));
5461 }
5462
5463 ret = TRUE;
5464
5465 cleanup:
5466 if (!ret)
5467 HeapFree(GetProcessHeap(), 0, ProviderName);
5468 HeapFree(GetProcessHeap(), 0, DriverVer);
5469
5470 return ret;
5471 }
5472
5473 /***********************************************************************
5474 * SetupDiBuildDriverInfoList (SETUPAPI.@)
5475 */
5476 BOOL WINAPI
5477 SetupDiBuildDriverInfoList(
5478 IN HDEVINFO DeviceInfoSet,
5479 IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
5480 IN DWORD DriverType)
5481 {
5482 struct DeviceInfoSet *list;
5483 SP_DEVINSTALL_PARAMS_W InstallParams;
5484 PVOID Buffer = NULL;
5485 struct InfFileDetails *currentInfFileDetails = NULL;
5486 LPWSTR ProviderName = NULL;
5487 LPWSTR ManufacturerName = NULL;
5488 WCHAR ManufacturerSection[LINE_LEN + 1];
5489 LPWSTR HardwareIDs = NULL;
5490 LPWSTR CompatibleIDs = NULL;
5491 LPWSTR FullInfFileName = NULL;
5492 LPWSTR ExcludeFromSelect = NULL;
5493 FILETIME DriverDate;
5494 DWORDLONG DriverVersion = 0;
5495 DWORD RequiredSize;
5496 BOOL ret = FALSE;
5497
5498 TRACE("%p %p %ld\n", DeviceInfoSet, DeviceInfoData, DriverType);
5499
5500 if (!DeviceInfoSet)
5501 SetLastError(ERROR_INVALID_HANDLE);
5502 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
5503 SetLastError(ERROR_INVALID_HANDLE);
5504 else if (list->HKLM != HKEY_LOCAL_MACHINE)
5505 SetLastError(ERROR_INVALID_HANDLE);
5506 else if (DriverType != SPDIT_CLASSDRIVER && DriverType != SPDIT_COMPATDRIVER)
5507 SetLastError(ERROR_INVALID_PARAMETER);
5508 else if (DriverType == SPDIT_COMPATDRIVER && !DeviceInfoData)
5509 SetLastError(ERROR_INVALID_PARAMETER);
5510 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
5511 SetLastError(ERROR_INVALID_USER_BUFFER);
5512 else
5513 {
5514 PLIST_ENTRY pDriverListHead = &list->DriverListHead;
5515 BOOL Result;
5516
5517 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
5518 Result = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
5519 if (!Result)
5520 goto done;
5521
5522 if (DeviceInfoData)
5523 {
5524 struct DeviceInfoElement *devInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
5525 if (!(devInfo->CreationFlags & DICD_INHERIT_CLASSDRVS))
5526 pDriverListHead = &devInfo->DriverListHead;
5527 }
5528
5529 if (DriverType == SPDIT_COMPATDRIVER)
5530 {
5531 /* Get hardware IDs list */
5532 Result = FALSE;
5533 RequiredSize = 512; /* Initial buffer size */
5534 SetLastError(ERROR_INSUFFICIENT_BUFFER);
5535 while (!Result && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
5536 {
5537 HeapFree(GetProcessHeap(), 0, HardwareIDs);
5538 HardwareIDs = HeapAlloc(GetProcessHeap(), 0, RequiredSize);
5539 if (!HardwareIDs)
5540 {
5541 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5542 goto done;
5543 }
5544 Result = SetupDiGetDeviceRegistryPropertyW(
5545 DeviceInfoSet,
5546 DeviceInfoData,
5547 SPDRP_HARDWAREID,
5548 NULL,
5549 (PBYTE)HardwareIDs,
5550 RequiredSize,
5551 &RequiredSize);
5552 }
5553 if (!Result)
5554 goto done;
5555
5556 /* Get compatible IDs list */
5557 Result = FALSE;
5558 RequiredSize = 512; /* Initial buffer size */
5559 SetLastError(ERROR_INSUFFICIENT_BUFFER);
5560 while (!Result && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
5561 {
5562 HeapFree(GetProcessHeap(), 0, CompatibleIDs);
5563 CompatibleIDs = HeapAlloc(GetProcessHeap(), 0, RequiredSize);
5564 if (!CompatibleIDs)
5565 {
5566 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5567 goto done;
5568 }
5569 Result = SetupDiGetDeviceRegistryPropertyW(
5570 DeviceInfoSet,
5571 DeviceInfoData,
5572 SPDRP_COMPATIBLEIDS,
5573 NULL,
5574 (PBYTE)CompatibleIDs,
5575 RequiredSize,
5576 &RequiredSize);
5577 if (!Result && GetLastError() == ERROR_FILE_NOT_FOUND)
5578 {
5579 /* No compatible ID for this device */
5580 HeapFree(GetProcessHeap(), 0, CompatibleIDs);
5581 CompatibleIDs = NULL;
5582 Result = TRUE;
5583 }
5584 }
5585 if (!Result)
5586 goto done;
5587 }
5588
5589 if (InstallParams.Flags & DI_ENUMSINGLEINF)
5590 {
5591 /* InstallParams.DriverPath contains the name of a .inf file */
5592 RequiredSize = strlenW(InstallParams.DriverPath) + 2;
5593 Buffer = HeapAlloc(GetProcessHeap(), 0, RequiredSize * sizeof(WCHAR));
5594 if (!Buffer)
5595 {
5596 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5597 goto done;
5598 }
5599 strcpyW(Buffer, InstallParams.DriverPath);
5600 ((LPWSTR)Buffer)[RequiredSize - 1] = 0;
5601 Result = TRUE;
5602 }
5603 else
5604 {
5605 /* Enumerate .inf files */
5606 Result = FALSE;
5607 RequiredSize = 32768; /* Initial buffer size */
5608 SetLastError(ERROR_INSUFFICIENT_BUFFER);
5609 while (!Result && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
5610 {
5611 HeapFree(GetProcessHeap(), 0, Buffer);
5612 Buffer = HeapAlloc(GetProcessHeap(), 0, RequiredSize * sizeof(WCHAR));
5613 if (!Buffer)
5614 {
5615 Result = FALSE;
5616 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5617 break;
5618 }
5619 Result = SetupGetInfFileListW(
5620 *InstallParams.DriverPath ? InstallParams.DriverPath : NULL,
5621 INF_STYLE_WIN4,
5622 Buffer, RequiredSize,
5623 &RequiredSize);
5624 }
5625 if (!Result && GetLastError() == ERROR_FILE_NOT_FOUND)
5626 {
5627 /* No .inf file in specified directory. So, we should
5628 * success as we created an empty driver info list.
5629 */
5630 ret = TRUE;
5631 goto done;
5632 }
5633 }
5634 if (Result)
5635 {
5636 LPCWSTR filename;
5637 LPWSTR pFullFilename;
5638
5639 if (InstallParams.Flags & DI_ENUMSINGLEINF)
5640 {
5641 FullInfFileName = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
5642 if (!FullInfFileName)
5643 goto done;
5644 pFullFilename = &FullInfFileName[0];
5645 }
5646 else if (*InstallParams.DriverPath)
5647 {
5648 DWORD len;
5649 len = GetFullPathNameW(InstallParams.DriverPath, 0, NULL, NULL);
5650 if (len == 0)
5651 goto done;
5652 FullInfFileName = HeapAlloc(GetProcessHeap(), 0, len + MAX_PATH);
5653 if (!FullInfFileName)
5654 goto done;
5655 len = GetFullPathNameW(InstallParams.DriverPath, len, FullInfFileName, NULL);
5656 if (len == 0)
5657 goto done;
5658 if (*FullInfFileName && FullInfFileName[strlenW(FullInfFileName) - 1] != '\\')
5659 strcatW(FullInfFileName, L"\\");
5660 pFullFilename = &FullInfFileName[strlenW(FullInfFileName)];
5661 }
5662 else
5663 {
5664 FullInfFileName = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
5665 if (!FullInfFileName)
5666 goto done;
5667 pFullFilename = &FullInfFileName[0];
5668 }
5669
5670 for (filename = (LPCWSTR)Buffer; *filename; filename += strlenW(filename) + 1)
5671 {
5672 INFCONTEXT ContextManufacturer, ContextDevice;
5673 GUID ClassGuid;
5674
5675 strcpyW(pFullFilename, filename);
5676 TRACE("Opening file %s\n", debugstr_w(FullInfFileName));
5677
5678 currentInfFileDetails = HeapAlloc(
5679 GetProcessHeap(),
5680 0,
5681 FIELD_OFFSET(struct InfFileDetails, FullInfFileName) + strlenW(FullInfFileName) * sizeof(WCHAR) + sizeof(UNICODE_NULL));
5682 if (!currentInfFileDetails)
5683 continue;
5684 memset(currentInfFileDetails, 0, sizeof(struct InfFileDetails));
5685 strcpyW(currentInfFileDetails->FullInfFileName, FullInfFileName);
5686
5687 currentInfFileDetails->hInf = SetupOpenInfFileW(FullInfFileName, NULL, INF_STYLE_WIN4, NULL);
5688 ReferenceInfFile(currentInfFileDetails);
5689 if (currentInfFileDetails->hInf == INVALID_HANDLE_VALUE)
5690 {
5691 HeapFree(GetProcessHeap(), 0, currentInfFileDetails);
5692 currentInfFileDetails = NULL;
5693 continue;
5694 }
5695
5696 if (!GetVersionInformationFromInfFile(
5697 currentInfFileDetails->hInf,
5698 &ClassGuid,
5699 &ProviderName,
5700 &DriverDate,
5701 &DriverVersion))
5702 {
5703 SetupCloseInfFile(currentInfFileDetails->hInf);
5704 HeapFree(GetProcessHeap(), 0, currentInfFileDetails->hInf);
5705 currentInfFileDetails = NULL;
5706 continue;
5707 }
5708
5709 if (DriverType == SPDIT_CLASSDRIVER)
5710 {
5711 /* Check if the ClassGuid in this .inf file is corresponding with our needs */
5712 if (!IsEqualIID(&list->ClassGuid, &GUID_NULL) && !IsEqualIID(&list->ClassGuid, &ClassGuid))
5713 {
5714 goto next;
5715 }
5716 }
5717
5718 if (InstallParams.FlagsEx & DI_FLAGSEX_ALLOWEXCLUDEDDRVS)
5719 {
5720 /* Read ExcludeFromSelect control flags */
5721 /* FIXME */
5722 }
5723 else
5724 FIXME("ExcludeFromSelect list ignored\n");
5725
5726 /* Get the manufacturers list */
5727 Result = SetupFindFirstLineW(currentInfFileDetails->hInf, INF_MANUFACTURER, NULL, &ContextManufacturer);
5728 while (Result)
5729 {
5730 Result = SetupGetStringFieldW(
5731 &ContextManufacturer,
5732 0, /* Field index */
5733 NULL, 0,
5734 &RequiredSize);
5735 if (Result)
5736 {
5737 /* We got the needed size for the buffer */
5738 ManufacturerName = HeapAlloc(GetProcessHeap(), 0, RequiredSize * sizeof(WCHAR));
5739 if (!ManufacturerName)
5740 {
5741 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5742 goto done;
5743 }
5744 Result = SetupGetStringFieldW(
5745 &ContextManufacturer,
5746 0, /* Field index */
5747 ManufacturerName, RequiredSize,
5748 &RequiredSize);
5749 }
5750 /* Get manufacturer section name */
5751 Result = SetupGetStringFieldW(
5752 &ContextManufacturer,
5753 1, /* Field index */
5754 ManufacturerSection, LINE_LEN,
5755 &RequiredSize);
5756 if (Result)
5757 {
5758 ManufacturerSection[RequiredSize] = 0; /* Final NULL char */
5759 /* Add (possible) extension to manufacturer section name */
5760 Result = SetupDiGetActualSectionToInstallW(
5761 currentInfFileDetails->hInf, ManufacturerSection, ManufacturerSection, LINE_LEN, NULL, NULL);
5762 if (Result)
5763 {
5764 TRACE("Enumerating devices in manufacturer %s\n", debugstr_w(ManufacturerSection));
5765 Result = SetupFindFirstLineW(currentInfFileDetails->hInf, ManufacturerSection, NULL, &ContextDevice);
5766 }
5767 }
5768 while (Result)
5769 {
5770 if (DriverType == SPDIT_CLASSDRIVER)
5771 {
5772 /* FIXME: Check ExcludeFromSelect list */
5773 if (!AddDriverToList(
5774 pDriverListHead,
5775 DriverType,
5776 &ClassGuid,
5777 ContextDevice,
5778 currentInfFileDetails,
5779 filename,
5780 ProviderName,
5781 ManufacturerName,
5782 NULL,
5783 DriverDate, DriverVersion,
5784 0))
5785 {
5786 break;
5787 }
5788 }
5789 else /* DriverType = SPDIT_COMPATDRIVER */
5790 {
5791 /* 1. Get all fields */
5792 DWORD FieldCount = SetupGetFieldCount(&ContextDevice);
5793 DWORD DriverRank;
5794 DWORD i;
5795 LPCWSTR currentId;
5796 BOOL DriverAlreadyAdded;
5797
5798 for (i = 2; i <= FieldCount; i++)
5799 {
5800 LPWSTR DeviceId = NULL;
5801 Result = FALSE;
5802 RequiredSize = 128; /* Initial buffer size */
5803 SetLastError(ERROR_INSUFFICIENT_BUFFER);
5804 while (!Result && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
5805 {
5806 HeapFree(GetProcessHeap(), 0, DeviceId);
5807 DeviceId = HeapAlloc(GetProcessHeap(), 0, RequiredSize * sizeof(WCHAR));
5808 if (!DeviceId)
5809 {
5810 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
5811 goto done;
5812 }
5813 Result = SetupGetStringFieldW(
5814 &ContextDevice,
5815 i,
5816 DeviceId, RequiredSize,
5817 &RequiredSize);
5818 }
5819 if (!Result)
5820 {
5821 HeapFree(GetProcessHeap(), 0, DeviceId);
5822 goto done;
5823 }
5824 /* FIXME: Check ExcludeFromSelect list */
5825 DriverAlreadyAdded = FALSE;
5826 for (DriverRank = 0, currentId = (LPCWSTR)HardwareIDs; !DriverAlreadyAdded && *currentId; currentId += strlenW(currentId) + 1, DriverRank++)
5827 {
5828 if (wcsicmp(DeviceId, currentId) == 0)
5829 {
5830 AddDriverToList(
5831 pDriverListHead,
5832 DriverType,
5833 &ClassGuid,
5834 ContextDevice,
5835 currentInfFileDetails,
5836 filename,
5837 ProviderName,
5838 ManufacturerName,
5839 currentId,
5840 DriverDate, DriverVersion,
5841 DriverRank + (i == 2 ? 0 : 0x1000 + i - 3));
5842 DriverAlreadyAdded = TRUE;
5843 }
5844 }
5845 if (CompatibleIDs)
5846 {
5847 for (DriverRank = 0, currentId = (LPCWSTR)CompatibleIDs; !DriverAlreadyAdded && *currentId; currentId += strlenW(currentId) + 1, DriverRank++)
5848 {
5849 if (wcsicmp(DeviceId, currentId) == 0)
5850 {
5851 AddDriverToList(
5852 pDriverListHead,
5853 DriverType,
5854 &ClassGuid,
5855 ContextDevice,
5856 currentInfFileDetails,
5857 filename,
5858 ProviderName,
5859 ManufacturerName,
5860 currentId,
5861 DriverDate, DriverVersion,
5862 DriverRank + (i == 2 ? 0x2000 : 0x3000 + i - 3));
5863 DriverAlreadyAdded = TRUE;
5864 }
5865 }
5866 }
5867 HeapFree(GetProcessHeap(), 0, DeviceId);
5868 }
5869 }
5870 Result = SetupFindNextLine(&ContextDevice, &ContextDevice);
5871 }
5872
5873 HeapFree(GetProcessHeap(), 0, ManufacturerName);
5874 ManufacturerName = NULL;
5875 Result = SetupFindNextLine(&ContextManufacturer, &ContextManufacturer);
5876 }
5877
5878 ret = TRUE;
5879 next:
5880 HeapFree(GetProcessHeap(), 0, ProviderName);
5881 HeapFree(GetProcessHeap(), 0, ExcludeFromSelect);
5882 ProviderName = ExcludeFromSelect = NULL;
5883
5884 DereferenceInfFile(currentInfFileDetails);
5885 currentInfFileDetails = NULL;
5886 }
5887 ret = TRUE;
5888 }
5889 }
5890
5891 done:
5892 if (ret)
5893 {
5894 if (DeviceInfoData)
5895 {
5896 InstallParams.Flags |= DI_DIDCOMPAT;
5897 InstallParams.FlagsEx |= DI_FLAGSEX_DIDCOMPATINFO;
5898 }
5899 else
5900 {
5901 InstallParams.Flags |= DI_DIDCLASS;
5902 InstallParams.FlagsEx |= DI_FLAGSEX_DIDINFOLIST;
5903 }
5904 ret = SetupDiSetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
5905 }
5906
5907 HeapFree(GetProcessHeap(), 0, ProviderName);
5908 HeapFree(GetProcessHeap(), 0, ManufacturerName);
5909 HeapFree(GetProcessHeap(), 0, HardwareIDs);
5910 HeapFree(GetProcessHeap(), 0, CompatibleIDs);
5911 HeapFree(GetProcessHeap(), 0, FullInfFileName);
5912 HeapFree(GetProcessHeap(), 0, ExcludeFromSelect);
5913 if (currentInfFileDetails)
5914 DereferenceInfFile(currentInfFileDetails);
5915 HeapFree(GetProcessHeap(), 0, Buffer);
5916
5917 TRACE("Returning %d\n", ret);
5918 return ret;
5919 }
5920
5921 /***********************************************************************
5922 * SetupDiDeleteDeviceInfo (SETUPAPI.@)
5923 */
5924 BOOL WINAPI
5925 SetupDiDeleteDeviceInfo(
5926 IN HDEVINFO DeviceInfoSet,
5927 IN PSP_DEVINFO_DATA DeviceInfoData)
5928 {
5929 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
5930
5931 FIXME("not implemented\n");
5932 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
5933 return FALSE;
5934 }
5935
5936
5937 /***********************************************************************
5938 * SetupDiDestroyDriverInfoList (SETUPAPI.@)
5939 */
5940 BOOL WINAPI
5941 SetupDiDestroyDriverInfoList(
5942 IN HDEVINFO DeviceInfoSet,
5943 IN PSP_DEVINFO_DATA DeviceInfoData,
5944 IN DWORD DriverType)
5945 {
5946 struct DeviceInfoSet *list;
5947 BOOL ret = FALSE;
5948
5949 TRACE("%p %p 0x%lx\n", DeviceInfoSet, DeviceInfoData, DriverType);
5950
5951 if (!DeviceInfoSet)
5952 SetLastError(ERROR_INVALID_HANDLE);
5953 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
5954 SetLastError(ERROR_INVALID_HANDLE);
5955 else if (DriverType != SPDIT_CLASSDRIVER && DriverType != SPDIT_COMPATDRIVER)
5956 SetLastError(ERROR_INVALID_PARAMETER);
5957 else if (DriverType == SPDIT_COMPATDRIVER && !DeviceInfoData)
5958 SetLastError(ERROR_INVALID_PARAMETER);
5959 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
5960 SetLastError(ERROR_INVALID_USER_BUFFER);
5961 else
5962 {
5963 PLIST_ENTRY ListEntry;
5964 struct DriverInfoElement *driverInfo;
5965 SP_DEVINSTALL_PARAMS_W InstallParams;
5966
5967 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
5968 if (!SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams))
5969 goto done;
5970
5971 if (!DeviceInfoData)
5972 /* Fall back to destroying class driver list */
5973 DriverType = SPDIT_CLASSDRIVER;
5974
5975 if (DriverType == SPDIT_CLASSDRIVER)
5976 {
5977 while (!IsListEmpty(&list->DriverListHead))
5978 {
5979 ListEntry = RemoveHeadList(&list->DriverListHead);
5980 driverInfo = (struct DriverInfoElement *)ListEntry;
5981 DestroyDriverInfoElement(driverInfo);
5982 }
5983 InstallParams.Reserved = 0;
5984 InstallParams.Flags &= ~(DI_DIDCLASS | DI_MULTMFGS);
5985 InstallParams.FlagsEx &= ~DI_FLAGSEX_DIDINFOLIST;
5986 ret = SetupDiSetDeviceInstallParamsW(DeviceInfoSet, NULL, &InstallParams);
5987 }
5988 else
5989 {
5990 SP_DEVINSTALL_PARAMS_W InstallParamsSet;
5991 struct DeviceInfoElement *deviceInfo;
5992
5993 InstallParamsSet.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
5994 if (!SetupDiGetDeviceInstallParamsW(DeviceInfoSet, NULL, &InstallParamsSet))
5995 goto done;
5996 deviceInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
5997 while (!IsListEmpty(&deviceInfo->DriverListHead))
5998 {
5999 ListEntry = RemoveHeadList(&deviceInfo->DriverListHead);
6000 driverInfo = (struct DriverInfoElement *)ListEntry;
6001 if ((PVOID)InstallParamsSet.Reserved == driverInfo)
6002 {
6003 InstallParamsSet.Reserved = 0;
6004 SetupDiSetDeviceInstallParamsW(DeviceInfoSet, NULL, &InstallParamsSet);
6005 }
6006 DestroyDriverInfoElement(driverInfo);
6007 }
6008 InstallParams.Reserved = 0;
6009 InstallParams.Flags &= ~DI_DIDCOMPAT;
6010 InstallParams.FlagsEx &= ~DI_FLAGSEX_DIDCOMPATINFO;
6011 ret = SetupDiSetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
6012 }
6013 }
6014
6015 done:
6016 TRACE("Returning %d\n", ret);
6017 return ret;
6018 }
6019
6020
6021 /***********************************************************************
6022 * SetupDiOpenDeviceInfoA (SETUPAPI.@)
6023 */
6024 BOOL WINAPI
6025 SetupDiOpenDeviceInfoA(
6026 IN HDEVINFO DeviceInfoSet,
6027 IN PCSTR DeviceInstanceId,
6028 IN HWND hwndParent OPTIONAL,
6029 IN DWORD OpenFlags,
6030 OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
6031 {
6032 LPWSTR DeviceInstanceIdW = NULL;
6033 BOOL bResult;
6034
6035 TRACE("%p %s %p %lx %p\n", DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData);
6036
6037 DeviceInstanceIdW = MultiByteToUnicode(DeviceInstanceId, CP_ACP);
6038 if (DeviceInstanceIdW == NULL)
6039 return FALSE;
6040
6041 bResult = SetupDiOpenDeviceInfoW(DeviceInfoSet,
6042 DeviceInstanceIdW, hwndParent, OpenFlags, DeviceInfoData);
6043
6044 MyFree(DeviceInstanceIdW);
6045
6046 return bResult;
6047 }
6048
6049
6050 /***********************************************************************
6051 * SetupDiOpenDeviceInfoW (SETUPAPI.@)
6052 */
6053 BOOL WINAPI
6054 SetupDiOpenDeviceInfoW(
6055 IN HDEVINFO DeviceInfoSet,
6056 IN PCWSTR DeviceInstanceId,
6057 IN HWND hwndParent OPTIONAL,
6058 IN DWORD OpenFlags,
6059 OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
6060 {
6061 struct DeviceInfoSet *list;
6062 HKEY hEnumKey, hKey;
6063 DWORD rc;
6064 BOOL ret = FALSE;
6065
6066 TRACE("%p %s %p %lx %p\n",
6067 DeviceInfoSet, debugstr_w(DeviceInstanceId),
6068 hwndParent, OpenFlags, DeviceInfoData);
6069
6070 if (OpenFlags & DIOD_CANCEL_REMOVE)
6071 FIXME("DIOD_CANCEL_REMOVE flag not implemented\n");
6072
6073 if (!DeviceInfoSet)
6074 SetLastError(ERROR_INVALID_HANDLE);
6075 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
6076 SetLastError(ERROR_INVALID_HANDLE);
6077 else if (!DeviceInstanceId)
6078 SetLastError(ERROR_INVALID_PARAMETER);
6079 else if (OpenFlags & ~(DIOD_CANCEL_REMOVE | DIOD_INHERIT_CLASSDRVS))
6080 {
6081 TRACE("Unknown flags: 0x%08lx\n", OpenFlags & ~(DIOD_CANCEL_REMOVE | DIOD_INHERIT_CLASSDRVS));
6082 SetLastError(ERROR_INVALID_FLAGS);
6083 }
6084 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
6085 SetLastError(ERROR_INVALID_USER_BUFFER);
6086 else
6087 {
6088 struct DeviceInfoElement *deviceInfo = NULL;
6089 /* Search if device already exists in DeviceInfoSet.
6090 * If yes, return the existing element
6091 * If no, create a new element using informations in registry
6092 */
6093 PLIST_ENTRY ItemList = list->ListHead.Flink;
6094 while (ItemList != &list->ListHead)
6095 {
6096 // TODO
6097 //if (good one)
6098 // break;
6099 FIXME("not implemented\n");
6100 ItemList = ItemList->Flink;
6101 }
6102
6103 if (deviceInfo)
6104 {
6105 /* good one found */
6106 ret = TRUE;
6107 }
6108 else
6109 {
6110 /* Open supposed registry key */
6111 rc = RegOpenKeyExW(
6112 list->HKLM,
6113 REGSTR_PATH_SYSTEMENUM,
6114 0, /* Options */
6115 0,
6116 &hEnumKey);
6117 if (rc != ERROR_SUCCESS)
6118 {
6119 SetLastError(rc);
6120 return FALSE;
6121 }
6122 rc = RegOpenKeyExW(
6123 hEnumKey,
6124 DeviceInstanceId,
6125 0, /* Options */
6126 KEY_QUERY_VALUE,
6127 &hKey);
6128 RegCloseKey(hEnumKey);
6129 if (rc != ERROR_SUCCESS)
6130 {
6131 if (rc == ERROR_FILE_NOT_FOUND)
6132 rc = ERROR_NO_SUCH_DEVINST;
6133 SetLastError(rc);
6134 return FALSE;
6135 }
6136
6137 /* FIXME: try to get ClassGUID from registry, instead of
6138 * sending GUID_NULL to CreateDeviceInfoElement
6139 */
6140 if (!CreateDeviceInfoElement(list, DeviceInstanceId, &GUID_NULL, &deviceInfo))
6141 {
6142 RegCloseKey(hKey);
6143 return FALSE;
6144 }
6145 InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
6146
6147 RegCloseKey(hKey);
6148 ret = TRUE;
6149 }
6150
6151 if (ret && deviceInfo && DeviceInfoData)
6152 {
6153 memcpy(&DeviceInfoData->ClassGuid, &deviceInfo->ClassGuid, sizeof(GUID));
6154 DeviceInfoData->DevInst = deviceInfo->dnDevInst;
6155 DeviceInfoData->Reserved = (ULONG_PTR)deviceInfo;
6156 }
6157 }
6158
6159 return ret;
6160 }
6161
6162
6163 /***********************************************************************
6164 * SetupDiEnumDriverInfoA (SETUPAPI.@)
6165 */
6166 BOOL WINAPI
6167 SetupDiEnumDriverInfoA(
6168 IN HDEVINFO DeviceInfoSet,
6169 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
6170 IN DWORD DriverType,
6171 IN DWORD MemberIndex,
6172 OUT PSP_DRVINFO_DATA_A DriverInfoData)
6173 {
6174 SP_DRVINFO_DATA_V2_W driverInfoData2W;
6175 BOOL ret = FALSE;
6176
6177 TRACE("%p %p 0x%lx %ld %p\n", DeviceInfoSet, DeviceInfoData,
6178 DriverType, MemberIndex, DriverInfoData);
6179
6180 if (DriverInfoData == NULL)
6181 SetLastError(ERROR_INVALID_PARAMETER);
6182 else if (DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V1_A) && DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V2_A))
6183 SetLastError(ERROR_INVALID_USER_BUFFER);
6184 else
6185 {
6186 driverInfoData2W.cbSize = sizeof(SP_DRVINFO_DATA_V2_W);
6187 ret = SetupDiEnumDriverInfoW(DeviceInfoSet, DeviceInfoData,
6188 DriverType, MemberIndex, &driverInfoData2W);
6189
6190 if (ret)
6191 {
6192 /* Do W->A conversion */
6193 DriverInfoData->DriverType = driverInfoData2W.DriverType;
6194 DriverInfoData->Reserved = driverInfoData2W.Reserved;
6195 if (WideCharToMultiByte(CP_ACP, 0, driverInfoData2W.Description, -1,
6196 DriverInfoData->Description, LINE_LEN, NULL, NULL) == 0)
6197 {
6198 DriverInfoData->Description[0] = '\0';
6199 ret = FALSE;
6200 }
6201 if (WideCharToMultiByte(CP_ACP, 0, driverInfoData2W.MfgName, -1,
6202 DriverInfoData->MfgName, LINE_LEN, NULL, NULL) == 0)
6203 {
6204 DriverInfoData->MfgName[0] = '\0';
6205 ret = FALSE;
6206 }
6207 if (WideCharToMultiByte(CP_ACP, 0, driverInfoData2W.ProviderName, -1,
6208 DriverInfoData->ProviderName, LINE_LEN, NULL, NULL) == 0)
6209 {
6210 DriverInfoData->ProviderName[0] = '\0';
6211 ret = FALSE;
6212 }
6213 if (DriverInfoData->cbSize == sizeof(SP_DRVINFO_DATA_V2_A))
6214 {
6215 /* Copy more fields */
6216 DriverInfoData->DriverDate = driverInfoData2W.DriverDate;
6217 DriverInfoData->DriverVersion = driverInfoData2W.DriverVersion;
6218 }
6219 }
6220 }
6221
6222 TRACE("Returning %d\n", ret);
6223 return ret;
6224 }
6225
6226
6227 /***********************************************************************
6228 * SetupDiEnumDriverInfoW (SETUPAPI.@)
6229 */
6230 BOOL WINAPI
6231 SetupDiEnumDriverInfoW(
6232 IN HDEVINFO DeviceInfoSet,
6233 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
6234 IN DWORD DriverType,
6235 IN DWORD MemberIndex,
6236 OUT PSP_DRVINFO_DATA_W DriverInfoData)
6237 {
6238 PLIST_ENTRY ListHead;
6239 BOOL ret = FALSE;
6240
6241 TRACE("%p %p 0x%lx %ld %p\n", DeviceInfoSet, DeviceInfoData,
6242 DriverType, MemberIndex, DriverInfoData);
6243
6244 if (!DeviceInfoSet || !DriverInfoData)
6245 SetLastError(ERROR_INVALID_PARAMETER);
6246 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
6247 SetLastError(ERROR_INVALID_HANDLE);
6248 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
6249 SetLastError(ERROR_INVALID_HANDLE);
6250 else if (DriverType != SPDIT_CLASSDRIVER && DriverType != SPDIT_COMPATDRIVER)
6251 SetLastError(ERROR_INVALID_PARAMETER);
6252 else if (DriverType == SPDIT_COMPATDRIVER && !DeviceInfoData)
6253 SetLastError(ERROR_INVALID_PARAMETER);
6254 else if (DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V1_W) && DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V2_W))
6255 SetLastError(ERROR_INVALID_USER_BUFFER);
6256 else
6257 {
6258 struct DeviceInfoElement *devInfo = NULL;
6259 PLIST_ENTRY ItemList;
6260 if (DeviceInfoData)
6261 devInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
6262 if (!devInfo || (devInfo->CreationFlags & DICD_INHERIT_CLASSDRVS))
6263 {
6264 ListHead = &((struct DeviceInfoSet *)DeviceInfoSet)->DriverListHead;
6265 }
6266 else
6267 {
6268 ListHead = &devInfo->DriverListHead;
6269 }
6270
6271 ItemList = ListHead->Flink;
6272 while (ItemList != ListHead && MemberIndex-- > 0)
6273 ItemList = ItemList->Flink;
6274 if (ItemList == ListHead)
6275 SetLastError(ERROR_NO_MORE_ITEMS);
6276 else
6277 {
6278 struct DriverInfoElement *DrvInfo = (struct DriverInfoElement *)ItemList;
6279
6280 memcpy(
6281 &DriverInfoData->DriverType,
6282 &DrvInfo->Info.DriverType,
6283 DriverInfoData->cbSize - FIELD_OFFSET(SP_DRVINFO_DATA_W, DriverType));
6284 ret = TRUE;
6285 }
6286 }
6287
6288 TRACE("Returning %d\n", ret);
6289 return ret;
6290 }
6291
6292
6293 /***********************************************************************
6294 * SetupDiGetSelectedDevice (SETUPAPI.@)
6295 */
6296 BOOL WINAPI
6297 SetupDiGetSelectedDevice(
6298 IN HDEVINFO DeviceInfoSet,
6299 OUT PSP_DEVINFO_DATA DeviceInfoData)
6300 {
6301 struct DeviceInfoSet *list;
6302 BOOL ret = FALSE;
6303
6304 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
6305
6306 if (!DeviceInfoSet)
6307 SetLastError(ERROR_INVALID_HANDLE);
6308 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
6309 SetLastError(ERROR_INVALID_HANDLE);
6310 else if (list->SelectedDevice == NULL)
6311 SetLastError(ERROR_NO_DEVICE_SELECTED);
6312 else if (!DeviceInfoData)
6313 SetLastError(ERROR_INVALID_PARAMETER);
6314 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
6315 SetLastError(ERROR_INVALID_USER_BUFFER);
6316 else
6317 {
6318 memcpy(&DeviceInfoData->ClassGuid,
6319 &list->SelectedDevice->ClassGuid,
6320 sizeof(GUID));
6321 DeviceInfoData->DevInst = list->SelectedDevice->dnDevInst;
6322 DeviceInfoData->Reserved = (ULONG_PTR)list->SelectedDevice;
6323 ret = TRUE;
6324 }
6325
6326 TRACE("Returning %d\n", ret);
6327 return ret;
6328 }
6329
6330
6331 /***********************************************************************
6332 * SetupDiGetSelectedDriverA (SETUPAPI.@)
6333 */
6334 BOOL WINAPI
6335 SetupDiGetSelectedDriverA(
6336 IN HDEVINFO DeviceInfoSet,
6337 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
6338 OUT PSP_DRVINFO_DATA_A DriverInfoData)
6339 {
6340 SP_DRVINFO_DATA_V2_W driverInfoData2W;
6341 BOOL ret = FALSE;
6342
6343 if (DriverInfoData == NULL)
6344 SetLastError(ERROR_INVALID_PARAMETER);
6345 else if (DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V1_A) && DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V2_A))
6346 SetLastError(ERROR_INVALID_USER_BUFFER);
6347 else
6348 {
6349 driverInfoData2W.cbSize = sizeof(SP_DRVINFO_DATA_V2_W);
6350
6351 ret = SetupDiGetSelectedDriverW(DeviceInfoSet,
6352 DeviceInfoData,
6353 &driverInfoData2W);
6354
6355 if (ret)
6356 {
6357 /* Do W->A conversion */
6358 DriverInfoData->DriverType = driverInfoData2W.DriverType;
6359 DriverInfoData->Reserved = driverInfoData2W.Reserved;
6360 if (WideCharToMultiByte(CP_ACP, 0, driverInfoData2W.Description, -1,
6361 DriverInfoData->Description, LINE_LEN, NULL, NULL) == 0)
6362 {
6363 DriverInfoData->Description[0] = '\0';
6364 ret = FALSE;
6365 }
6366 if (WideCharToMultiByte(CP_ACP, 0, driverInfoData2W.MfgName, -1,
6367 DriverInfoData->MfgName, LINE_LEN, NULL, NULL) == 0)
6368 {
6369 DriverInfoData->MfgName[0] = '\0';
6370 ret = FALSE;
6371 }
6372 if (WideCharToMultiByte(CP_ACP, 0, driverInfoData2W.ProviderName, -1,
6373 DriverInfoData->ProviderName, LINE_LEN, NULL, NULL) == 0)
6374 {
6375 DriverInfoData->ProviderName[0] = '\0';
6376 ret = FALSE;
6377 }
6378 if (DriverInfoData->cbSize == sizeof(SP_DRVINFO_DATA_V2_A))
6379 {
6380 /* Copy more fields */
6381 DriverInfoData->DriverDate = driverInfoData2W.DriverDate;
6382 DriverInfoData->DriverVersion = driverInfoData2W.DriverVersion;
6383 }
6384 }
6385 }
6386
6387 return ret;
6388 }
6389
6390
6391 /***********************************************************************
6392 * SetupDiGetSelectedDriverW (SETUPAPI.@)
6393 */
6394 BOOL WINAPI
6395 SetupDiGetSelectedDriverW(
6396 IN HDEVINFO DeviceInfoSet,
6397 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
6398 OUT PSP_DRVINFO_DATA_W DriverInfoData)
6399 {
6400 BOOL ret = FALSE;
6401
6402 TRACE("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DriverInfoData);
6403
6404 if (!DeviceInfoSet || !DriverInfoData)
6405 SetLastError(ERROR_INVALID_PARAMETER);
6406 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
6407 SetLastError(ERROR_INVALID_HANDLE);
6408 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
6409 SetLastError(ERROR_INVALID_HANDLE);
6410 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
6411 SetLastError(ERROR_INVALID_USER_BUFFER);
6412 else if (DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V1_W) && DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V2_W))
6413 SetLastError(ERROR_INVALID_USER_BUFFER);
6414 else
6415 {
6416 SP_DEVINSTALL_PARAMS InstallParams;
6417
6418 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
6419 if (SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams))
6420 {
6421 struct DriverInfoElement *driverInfo;
6422 driverInfo = (struct DriverInfoElement *)InstallParams.Reserved;
6423 if (driverInfo == NULL)
6424 SetLastError(ERROR_NO_DRIVER_SELECTED);
6425 else
6426 {
6427 memcpy(
6428 &DriverInfoData->DriverType,
6429 &driverInfo->Info.DriverType,
6430 DriverInfoData->cbSize - FIELD_OFFSET(SP_DRVINFO_DATA_W, DriverType));
6431 ret = TRUE;
6432 }
6433 }
6434 }
6435
6436 TRACE("Returning %d\n", ret);
6437 return ret;
6438 }
6439
6440
6441 /***********************************************************************
6442 * SetupDiSetSelectedDevice (SETUPAPI.@)
6443 */
6444 BOOL WINAPI
6445 SetupDiSetSelectedDevice(
6446 IN HDEVINFO DeviceInfoSet,
6447 IN PSP_DEVINFO_DATA DeviceInfoData)
6448 {
6449 struct DeviceInfoSet *list;
6450 BOOL ret = FALSE;
6451
6452 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
6453
6454 if (!DeviceInfoSet)
6455 SetLastError(ERROR_INVALID_HANDLE);
6456 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
6457 SetLastError(ERROR_INVALID_HANDLE);
6458 else if (!DeviceInfoData)
6459 SetLastError(ERROR_INVALID_PARAMETER);
6460 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
6461 SetLastError(ERROR_INVALID_USER_BUFFER);
6462 else if (DeviceInfoData->Reserved == 0)
6463 SetLastError(ERROR_INVALID_USER_BUFFER);
6464 else
6465 {
6466 list->SelectedDevice = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
6467 ret = TRUE;
6468 }
6469
6470 TRACE("Returning %d\n", ret);
6471 return ret;
6472 }
6473
6474
6475 /***********************************************************************
6476 * SetupDiSetSelectedDriverA (SETUPAPI.@)
6477 */
6478 BOOL WINAPI
6479 SetupDiSetSelectedDriverA(
6480 IN HDEVINFO DeviceInfoSet,
6481 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
6482 IN OUT PSP_DRVINFO_DATA_A DriverInfoData OPTIONAL)
6483 {
6484 SP_DRVINFO_DATA_V1_W DriverInfoDataW;
6485 PSP_DRVINFO_DATA_W pDriverInfoDataW = NULL;
6486 BOOL ret = FALSE;
6487
6488 if (DriverInfoData != NULL)
6489 {
6490 if (DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V2_A) &&
6491 DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V1_A));
6492 {
6493 SetLastError(ERROR_INVALID_PARAMETER);
6494 return FALSE;
6495 }
6496
6497 DriverInfoDataW.cbSize = sizeof(SP_DRVINFO_DATA_V1_W);
6498 DriverInfoDataW.Reserved = DriverInfoData->Reserved;
6499
6500 if (DriverInfoDataW.Reserved == 0)
6501 {
6502 DriverInfoDataW.DriverType = DriverInfoData->DriverType;
6503
6504 /* convert the strings to unicode */
6505 if (!MultiByteToWideChar(CP_ACP,
6506 0,
6507 DriverInfoData->Description,
6508 LINE_LEN,
6509 DriverInfoDataW.Description,
6510 LINE_LEN) ||
6511 !MultiByteToWideChar(CP_ACP,
6512 0,
6513 DriverInfoData->ProviderName,
6514 LINE_LEN,
6515 DriverInfoDataW.ProviderName,
6516 LINE_LEN))
6517 {
6518 return FALSE;
6519 }
6520 }
6521
6522 pDriverInfoDataW = (PSP_DRVINFO_DATA_W)&DriverInfoDataW;
6523 }
6524
6525 ret = SetupDiSetSelectedDriverW(DeviceInfoSet,
6526 DeviceInfoData,
6527 pDriverInfoDataW);
6528
6529 if (ret && pDriverInfoDataW != NULL)
6530 {
6531 DriverInfoData->Reserved = DriverInfoDataW.Reserved;
6532 }
6533
6534 return ret;
6535 }
6536
6537
6538 /***********************************************************************
6539 * SetupDiSetSelectedDriverW (SETUPAPI.@)
6540 */
6541 BOOL WINAPI
6542 SetupDiSetSelectedDriverW(
6543 IN HDEVINFO DeviceInfoSet,
6544 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
6545 IN OUT PSP_DRVINFO_DATA_W DriverInfoData OPTIONAL)
6546 {
6547 BOOL ret = FALSE;
6548
6549 TRACE("%p %p %p\n", DeviceInfoSet, DeviceInfoData, DriverInfoData);
6550
6551 if (!DeviceInfoSet)
6552 SetLastError(ERROR_INVALID_PARAMETER);
6553 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
6554 SetLastError(ERROR_INVALID_HANDLE);
6555 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
6556 SetLastError(ERROR_INVALID_HANDLE);
6557 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
6558 SetLastError(ERROR_INVALID_USER_BUFFER);
6559 else if (DriverInfoData && DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V1_W) && DriverInfoData->cbSize != sizeof(SP_DRVINFO_DATA_V2_W))
6560 SetLastError(ERROR_INVALID_USER_BUFFER);
6561 else
6562 {
6563 struct DriverInfoElement **pDriverInfo;
6564 PLIST_ENTRY ListHead, ItemList;
6565
6566 if (DeviceInfoData)
6567 {
6568 pDriverInfo = (struct DriverInfoElement **)&((struct DeviceInfoElement *)DeviceInfoData->Reserved)->InstallParams.Reserved;
6569 ListHead = &((struct DeviceInfoElement *)DeviceInfoData->Reserved)->DriverListHead;
6570 }
6571 else
6572 {
6573 pDriverInfo = (struct DriverInfoElement **)&((struct DeviceInfoSet *)DeviceInfoSet)->InstallParams.Reserved;
6574 ListHead = &((struct DeviceInfoSet *)DeviceInfoSet)->DriverListHead;
6575 }
6576
6577 if (!DriverInfoData)
6578 {
6579 *pDriverInfo = NULL;
6580 ret = TRUE;
6581 }
6582 else
6583 {
6584 /* Search selected driver in list */
6585 ItemList = ListHead->Flink;
6586 while (ItemList != ListHead)
6587 {
6588 if (DriverInfoData->Reserved != 0)
6589 {
6590 if (DriverInfoData->Reserved == (ULONG_PTR)ItemList)
6591 break;
6592 }
6593 else
6594 {
6595 /* The caller wants to compare only DriverType, Description and ProviderName fields */
6596 struct DriverInfoElement *driverInfo = CONTAINING_RECORD(ItemList, struct DriverInfoElement, ListEntry);
6597 if (driverInfo->Info.DriverType == DriverInfoData->DriverType
6598 && strcmpW(driverInfo->Info.Description, DriverInfoData->Description) == 0
6599 && strcmpW(driverInfo->Info.ProviderName, DriverInfoData->ProviderName) == 0)
6600 {
6601 break;
6602 }
6603 }
6604 ItemList = ItemList->Flink;
6605 }
6606 if (ItemList == ListHead)
6607 SetLastError(ERROR_INVALID_PARAMETER);
6608 else
6609 {
6610 *pDriverInfo = (struct DriverInfoElement *)ItemList;
6611 DriverInfoData->Reserved = (ULONG_PTR)ItemList;
6612 ret = TRUE;
6613 TRACE("Choosing driver whose rank is 0x%lx\n",
6614 ((struct DriverInfoElement *)ItemList)->DriverRank);
6615 if (DeviceInfoData)
6616 memcpy(&DeviceInfoData->ClassGuid, &(*pDriverInfo)->ClassGuid, sizeof(GUID));
6617 }
6618 }
6619 }
6620
6621 TRACE("Returning %d\n", ret);
6622 return ret;
6623 }
6624
6625 /***********************************************************************
6626 * SetupDiGetDriverInfoDetailA (SETUPAPI.@)
6627 */
6628 BOOL WINAPI
6629 SetupDiGetDriverInfoDetailA(
6630 IN HDEVINFO DeviceInfoSet,
6631 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
6632 IN PSP_DRVINFO_DATA_A DriverInfoData,
6633 OUT PSP_DRVINFO_DETAIL_DATA_A DriverInfoDetailData OPTIONAL,
6634 IN DWORD DriverInfoDetailDataSize,
6635 OUT PDWORD RequiredSize OPTIONAL)
6636 {
6637 SP_DRVINFO_DATA_V2_W DriverInfoDataW;
6638 PSP_DRVINFO_DETAIL_DATA_W DriverInfoDetailDataW = NULL;
6639 DWORD BufSize = 0;
6640 DWORD HardwareIDLen = 0;
6641 BOOL ret = FALSE;
6642
6643 /* do some sanity checks, the unicode version might do more thorough checks */
6644 if (DriverInfoData == NULL ||
6645 (DriverInfoDetailData == NULL && DriverInfoDetailDataSize != 0) ||
6646 (DriverInfoDetailData != NULL &&
6647 (DriverInfoDetailDataSize < FIELD_OFFSET(SP_DRVINFO_DETAIL_DATA_A, HardwareID) + sizeof(CHAR) ||
6648 DriverInfoDetailData->cbSize != sizeof(SP_DRVINFO_DETAIL_DATA_A))))
6649 {
6650 SetLastError(ERROR_INVALID_PARAMETER);
6651 goto Cleanup;
6652 }
6653
6654 /* make sure we support both versions of the SP_DRVINFO_DATA structure */
6655 if (DriverInfoData->cbSize == sizeof(SP_DRVINFO_DATA_V1_A))
6656 {
6657 DriverInfoDataW.cbSize = sizeof(SP_DRVINFO_DATA_V1_W);
6658 }
6659 else if (DriverInfoData->cbSize == sizeof(SP_DRVINFO_DATA_V2_A))
6660 {
6661 DriverInfoDataW.cbSize = sizeof(SP_DRVINFO_DATA_V2_W);
6662 }
6663 else
6664 {
6665 SetLastError(ERROR_INVALID_PARAMETER);
6666 goto Cleanup;
6667 }
6668 DriverInfoDataW.DriverType = DriverInfoData->DriverType;
6669 DriverInfoDataW.Reserved = DriverInfoData->Reserved;
6670
6671 /* convert the strings to unicode */
6672 if (MultiByteToWideChar(CP_ACP,
6673 0,
6674 DriverInfoData->Description,
6675 LINE_LEN,
6676 DriverInfoDataW.Description,
6677 LINE_LEN) &&
6678 MultiByteToWideChar(CP_ACP,
6679 0,
6680 DriverInfoData->MfgName,
6681 LINE_LEN,
6682 DriverInfoDataW.MfgName,
6683 LINE_LEN) &&
6684 MultiByteToWideChar(CP_ACP,
6685 0,
6686 DriverInfoData->ProviderName,
6687 LINE_LEN,
6688 DriverInfoDataW.ProviderName,
6689 LINE_LEN))
6690 {
6691 if (DriverInfoDataW.cbSize == sizeof(SP_DRVINFO_DATA_V2_W))
6692 {
6693 DriverInfoDataW.DriverDate = ((PSP_DRVINFO_DATA_V2_A)DriverInfoData)->DriverDate;
6694 DriverInfoDataW.DriverVersion = ((PSP_DRVINFO_DATA_V2_A)DriverInfoData)->DriverVersion;
6695 }
6696
6697 if (DriverInfoDetailData != NULL)
6698 {
6699 /* calculate the unicode buffer size from the ansi buffer size */
6700 HardwareIDLen = DriverInfoDetailDataSize - FIELD_OFFSET(SP_DRVINFO_DETAIL_DATA_A, HardwareID);
6701 BufSize = FIELD_OFFSET(SP_DRVINFO_DETAIL_DATA_W, HardwareID) +
6702 (HardwareIDLen * sizeof(WCHAR));
6703
6704 DriverInfoDetailDataW = MyMalloc(BufSize);
6705 if (DriverInfoDetailDataW == NULL)
6706 {
6707 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
6708 goto Cleanup;
6709 }
6710
6711 /* initialize the buffer */
6712 ZeroMemory(DriverInfoDetailDataW,
6713 BufSize);
6714 DriverInfoDetailDataW->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W);
6715 }
6716
6717 /* call the unicode version */
6718 ret = SetupDiGetDriverInfoDetailW(DeviceInfoSet,
6719 DeviceInfoData,
6720 &DriverInfoDataW,
6721 DriverInfoDetailDataW,
6722 BufSize,
6723 RequiredSize);
6724
6725 if (ret)
6726 {
6727 if (DriverInfoDetailDataW != NULL)
6728 {
6729 /* convert the SP_DRVINFO_DETAIL_DATA_W structure to ansi */
6730 DriverInfoDetailData->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_A);
6731 DriverInfoDetailData->InfDate = DriverInfoDetailDataW->InfDate;
6732 DriverInfoDetailData->Reserved = DriverInfoDetailDataW->Reserved;
6733 if (WideCharToMultiByte(CP_ACP,
6734 0,
6735 DriverInfoDetailDataW->SectionName,
6736 LINE_LEN,
6737 DriverInfoDetailData->SectionName,
6738 LINE_LEN,
6739 NULL,
6740 NULL) &&
6741 WideCharToMultiByte(CP_ACP,
6742 0,
6743 DriverInfoDetailDataW->InfFileName,
6744 MAX_PATH,
6745 DriverInfoDetailData->InfFileName,
6746 MAX_PATH,
6747 NULL,
6748 NULL) &&
6749 WideCharToMultiByte(CP_ACP,
6750 0,
6751 DriverInfoDetailDataW->DrvDescription,
6752 LINE_LEN,
6753 DriverInfoDetailData->DrvDescription,
6754 LINE_LEN,
6755 NULL,
6756 NULL) &&
6757 WideCharToMultiByte(CP_ACP,
6758 0,
6759 DriverInfoDetailDataW->HardwareID,
6760 HardwareIDLen,
6761 DriverInfoDetailData->HardwareID,
6762 HardwareIDLen,
6763 NULL,
6764 NULL))
6765 {
6766 DWORD len, cnt = 0;
6767 DWORD hwidlen = HardwareIDLen;
6768 CHAR *s = DriverInfoDetailData->HardwareID;
6769
6770 /* count the strings in the list */
6771 while (*s != '\0')
6772 {
6773 len = lstrlenA(s) + 1;
6774 if (hwidlen > len)
6775 {
6776 cnt++;
6777 s += len;
6778 hwidlen -= len;
6779 }
6780 else
6781 {
6782 /* looks like the string list wasn't terminated... */
6783 SetLastError(ERROR_INVALID_USER_BUFFER);
6784 ret = FALSE;
6785 break;
6786 }
6787 }
6788
6789 /* make sure CompatIDsOffset points to the second string in the
6790 list, if present */
6791 if (cnt > 1)
6792 {
6793 DriverInfoDetailData->CompatIDsOffset = lstrlenA(DriverInfoDetailData->HardwareID) + 1;
6794 DriverInfoDetailData->CompatIDsLength = (DWORD)(s - DriverInfoDetailData->HardwareID) -
6795 DriverInfoDetailData->CompatIDsOffset + 1;
6796 }
6797 else
6798 {
6799 DriverInfoDetailData->CompatIDsOffset = 0;
6800 DriverInfoDetailData->CompatIDsLength = 0;
6801 }
6802 }
6803 else
6804 {
6805 ret = FALSE;
6806 }
6807 }
6808
6809 if (RequiredSize != NULL)
6810 {
6811 *RequiredSize = FIELD_OFFSET(SP_DRVINFO_DETAIL_DATA_A, HardwareID) +
6812 (((*RequiredSize) - FIELD_OFFSET(SP_DRVINFO_DETAIL_DATA_W, HardwareID)) / sizeof(WCHAR));
6813 }
6814 }
6815 }
6816
6817 Cleanup:
6818 if (DriverInfoDetailDataW != NULL)
6819 {
6820 MyFree(DriverInfoDetailDataW);
6821 }
6822
6823 return ret;
6824 }
6825
6826 /***********************************************************************
6827 * SetupDiGetDriverInfoDetailW (SETUPAPI.@)
6828 */
6829 BOOL WINAPI
6830 SetupDiGetDriverInfoDetailW(
6831 IN HDEVINFO DeviceInfoSet,
6832 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
6833 IN PSP_DRVINFO_DATA_W DriverInfoData,
6834 OUT PSP_DRVINFO_DETAIL_DATA_W DriverInfoDetailData OPTIONAL,
6835 IN DWORD DriverInfoDetailDataSize,
6836 OUT PDWORD RequiredSize OPTIONAL)
6837 {
6838 BOOL ret = FALSE;
6839
6840 TRACE("%p %p %p %p %lu %p\n", DeviceInfoSet, DeviceInfoData,
6841 DriverInfoData, DriverInfoDetailData,
6842 DriverInfoDetailDataSize, RequiredSize);
6843
6844 if (!DeviceInfoSet)
6845 SetLastError(ERROR_INVALID_PARAMETER);
6846 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
6847 SetLastError(ERROR_INVALID_HANDLE);
6848 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
6849 SetLastError(ERROR_INVALID_HANDLE);
6850 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
6851 SetLastError(ERROR_INVALID_USER_BUFFER);
6852 else if (!DriverInfoData)
6853 SetLastError(ERROR_INVALID_PARAMETER);
6854 else if (!DriverInfoDetailData && DriverInfoDetailDataSize != 0)
6855 SetLastError(ERROR_INVALID_PARAMETER);
6856 else if (DriverInfoDetailData && DriverInfoDetailDataSize < sizeof(SP_DRVINFO_DETAIL_DATA_W))
6857 SetLastError(ERROR_INVALID_PARAMETER);
6858 else if (DriverInfoDetailData && DriverInfoDetailData->cbSize != sizeof(SP_DRVINFO_DETAIL_DATA_W))
6859 SetLastError(ERROR_INVALID_USER_BUFFER);
6860 else if (DriverInfoData->Reserved == 0)
6861 SetLastError(ERROR_NO_DRIVER_SELECTED);
6862 else
6863 {
6864 struct DriverInfoElement *driverInfoElement;
6865 driverInfoElement = (struct DriverInfoElement *)DriverInfoData->Reserved;
6866
6867 memcpy(
6868 DriverInfoDetailData,
6869 &driverInfoElement->Details,
6870 driverInfoElement->Details.cbSize);
6871 /* FIXME: copy HardwareIDs/CompatibleIDs if buffer is big enough
6872 * Don't forget to set CompatIDsOffset and CompatIDsLength fields.
6873 */
6874 ret = TRUE;
6875 }
6876
6877 TRACE("Returning %d\n", ret);
6878 return ret;
6879 }
6880
6881 /* Return the current hardware profile id, or -1 if error */
6882 static DWORD
6883 GetCurrentHwProfile(
6884 IN HDEVINFO DeviceInfoSet)
6885 {
6886 HKEY hKey = INVALID_HANDLE_VALUE;
6887 DWORD dwRegType, dwLength;
6888 DWORD hwProfile;
6889 LONG rc;
6890 DWORD ret = (DWORD)-1;
6891
6892 rc = RegOpenKeyExW(
6893 ((struct DeviceInfoSet *)DeviceInfoSet)->HKLM,
6894 REGSTR_PATH_IDCONFIGDB,
6895 0, /* Options */
6896 KEY_QUERY_VALUE,
6897 &hKey);
6898 if (rc != ERROR_SUCCESS)
6899 {
6900 SetLastError(rc);
6901 goto cleanup;
6902 }
6903
6904 dwLength = sizeof(DWORD);
6905 rc = RegQueryValueExW(
6906 hKey,
6907 REGSTR_VAL_CURRENTCONFIG,
6908 NULL,
6909 &dwRegType,
6910 (LPBYTE)&hwProfile, &dwLength);
6911 if (rc != ERROR_SUCCESS)
6912 {
6913 SetLastError(rc);
6914 goto cleanup;
6915 }
6916 else if (dwRegType != REG_DWORD || dwLength != sizeof(DWORD))
6917 {
6918 SetLastError(ERROR_GEN_FAILURE);
6919 goto cleanup;
6920 }
6921
6922 ret = hwProfile;
6923
6924 cleanup:
6925 if (hKey != INVALID_HANDLE_VALUE)
6926 RegCloseKey(hKey);
6927
6928 return hwProfile;
6929 }
6930
6931 static BOOL
6932 ResetDevice(
6933 IN HDEVINFO DeviceInfoSet,
6934 IN PSP_DEVINFO_DATA DeviceInfoData)
6935 {
6936 #ifndef __WINESRC__
6937 PLUGPLAY_CONTROL_RESET_DEVICE_DATA ResetDeviceData;
6938 struct DeviceInfoElement *deviceInfo = (struct DeviceInfoElement *)DeviceInfoData->Reserved;
6939 NTSTATUS Status;
6940
6941 if (((struct DeviceInfoSet *)DeviceInfoSet)->HKLM != HKEY_LOCAL_MACHINE)
6942 {
6943 /* At the moment, I only know how to start local devices */
6944 SetLastError(ERROR_INVALID_COMPUTERNAME);
6945 return FALSE;
6946 }
6947
6948 RtlInitUnicodeString(&ResetDeviceData.DeviceInstance, deviceInfo->DeviceName);
6949 Status = NtPlugPlayControl(PlugPlayControlResetDevice, &ResetDeviceData, sizeof(PLUGPLAY_CONTROL_RESET_DEVICE_DATA));
6950 SetLastError(RtlNtStatusToDosError(Status));
6951 return NT_SUCCESS(Status);
6952 #else
6953 FIXME("Stub: ResetDevice(%p %p)\n", DeviceInfoSet, DeviceInfoData);
6954 return TRUE;
6955 #endif
6956 }
6957
6958 static BOOL StopDevice(
6959 IN HDEVINFO DeviceInfoSet,
6960 IN PSP_DEVINFO_DATA DeviceInfoData)
6961 {
6962 FIXME("Stub: StopDevice(%p %p)\n", DeviceInfoSet, DeviceInfoData);
6963 return TRUE;
6964 }
6965
6966 /***********************************************************************
6967 * SetupDiChangeState (SETUPAPI.@)
6968 */
6969 BOOL WINAPI
6970 SetupDiChangeState(
6971 IN HDEVINFO DeviceInfoSet,
6972 IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
6973 {
6974 PSP_PROPCHANGE_PARAMS PropChange;
6975 HKEY hKey = INVALID_HANDLE_VALUE;
6976 LPCWSTR RegistryValueName;
6977 DWORD dwConfigFlags, dwLength, dwRegType;
6978 LONG rc;
6979 BOOL ret = FALSE;
6980
6981 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
6982
6983 if (!DeviceInfoData)
6984 PropChange = ((struct DeviceInfoSet *)DeviceInfoSet)->ClassInstallParams.PropChange;
6985 else
6986 PropChange = ((struct DeviceInfoElement *)DeviceInfoData->Reserved)->ClassInstallParams.PropChange;
6987 if (!PropChange)
6988 {
6989 SetLastError(ERROR_INVALID_PARAMETER);
6990 goto cleanup;
6991 }
6992
6993 if (PropChange->Scope == DICS_FLAG_GLOBAL)
6994 RegistryValueName = REGSTR_VAL_CONFIGFLAGS;
6995 else
6996 RegistryValueName = REGSTR_VAL_CSCONFIGFLAGS;
6997
6998 switch (PropChange->StateChange)
6999 {
7000 case DICS_ENABLE:
7001 case DICS_DISABLE:
7002 {
7003 /* Enable/disable device in registry */
7004 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, PropChange->Scope, PropChange->HwProfile, DIREG_DEV, KEY_QUERY_VALUE | KEY_SET_VALUE);
7005 if (hKey == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND)
7006 hKey = SetupDiCreateDevRegKeyW(DeviceInfoSet, DeviceInfoData, PropChange->Scope, PropChange->HwProfile, DIREG_DEV, NULL, NULL);
7007 if (hKey == INVALID_HANDLE_VALUE)
7008 break;
7009 dwLength = sizeof(DWORD);
7010 rc = RegQueryValueExW(
7011 hKey,
7012 RegistryValueName,
7013 NULL,
7014 &dwRegType,
7015 (LPBYTE)&dwConfigFlags, &dwLength);
7016 if (rc == ERROR_FILE_NOT_FOUND)
7017 dwConfigFlags = 0;
7018 else if (rc != ERROR_SUCCESS)
7019 {
7020 SetLastError(rc);
7021 goto cleanup;
7022 }
7023 else if (dwRegType != REG_DWORD || dwLength != sizeof(DWORD))
7024 {
7025 SetLastError(ERROR_GEN_FAILURE);
7026 goto cleanup;
7027 }
7028 if (PropChange->StateChange == DICS_ENABLE)
7029 dwConfigFlags &= ~(PropChange->Scope == DICS_FLAG_GLOBAL ? CONFIGFLAG_DISABLED : CSCONFIGFLAG_DISABLED);
7030 else
7031 dwConfigFlags |= (PropChange->Scope == DICS_FLAG_GLOBAL ? CONFIGFLAG_DISABLED : CSCONFIGFLAG_DISABLED);
7032 rc = RegSetValueEx(
7033 hKey,
7034 RegistryValueName,
7035 0,
7036 REG_DWORD,
7037 (LPBYTE)&dwConfigFlags, sizeof(DWORD));
7038 if (rc != ERROR_SUCCESS)
7039 {
7040 SetLastError(rc);
7041 goto cleanup;
7042 }
7043
7044 /* Enable/disable device if needed */
7045 if (PropChange->Scope == DICS_FLAG_GLOBAL
7046 || PropChange->HwProfile == 0
7047 || PropChange->HwProfile == GetCurrentHwProfile(DeviceInfoSet))
7048 {
7049 if (PropChange->StateChange == DICS_ENABLE)
7050 ret = ResetDevice(DeviceInfoSet, DeviceInfoData);
7051 else
7052 ret = StopDevice(DeviceInfoSet, DeviceInfoData);
7053 }
7054 else
7055 ret = TRUE;
7056 break;
7057 }
7058 case DICS_PROPCHANGE:
7059 {
7060 ret = ResetDevice(DeviceInfoSet, DeviceInfoData);
7061 break;
7062 }
7063 default:
7064 {
7065 ERR("Unknown StateChange 0x%lx\n", PropChange->StateChange);
7066 SetLastError(ERROR_NOT_SUPPORTED);
7067 }
7068 }
7069
7070 cleanup:
7071 if (hKey != INVALID_HANDLE_VALUE)
7072 RegCloseKey(hKey);
7073
7074 TRACE("Returning %d\n", ret);
7075 return ret;
7076 }
7077
7078 /***********************************************************************
7079 * SetupDiSelectBestCompatDrv (SETUPAPI.@)
7080 */
7081 BOOL WINAPI
7082 SetupDiSelectBestCompatDrv(
7083 IN HDEVINFO DeviceInfoSet,
7084 IN PSP_DEVINFO_DATA DeviceInfoData)
7085 {
7086 SP_DRVINFO_DATA_W drvInfoData;
7087 BOOL ret;
7088
7089 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
7090
7091 /* Drivers are sorted by rank in the driver list, so
7092 * the first driver in the list is the best one.
7093 */
7094 drvInfoData.cbSize = sizeof(SP_DRVINFO_DATA_W);
7095 ret = SetupDiEnumDriverInfoW(
7096 DeviceInfoSet,
7097 DeviceInfoData,
7098 SPDIT_COMPATDRIVER,
7099 0, /* Member index */
7100 &drvInfoData);
7101
7102 if (ret)
7103 {
7104 ret = SetupDiSetSelectedDriverW(
7105 DeviceInfoSet,
7106 DeviceInfoData,
7107 &drvInfoData);
7108 }
7109
7110 TRACE("Returning %d\n", ret);
7111 return ret;
7112 }
7113
7114 /***********************************************************************
7115 * SetupDiInstallDriverFiles (SETUPAPI.@)
7116 */
7117 BOOL WINAPI
7118 SetupDiInstallDriverFiles(
7119 IN HDEVINFO DeviceInfoSet,
7120 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
7121 {
7122 BOOL ret = FALSE;
7123
7124 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
7125
7126 if (!DeviceInfoSet)
7127 SetLastError(ERROR_INVALID_PARAMETER);
7128 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
7129 SetLastError(ERROR_INVALID_HANDLE);
7130 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
7131 SetLastError(ERROR_INVALID_HANDLE);
7132 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
7133 SetLastError(ERROR_INVALID_USER_BUFFER);
7134 else if (DeviceInfoData && ((struct DeviceInfoElement *)DeviceInfoData->Reserved)->InstallParams.Reserved == 0)
7135 SetLastError(ERROR_NO_DRIVER_SELECTED);
7136 else if (!DeviceInfoData && ((struct DeviceInfoSet *)DeviceInfoSet)->InstallParams.Reserved == 0)
7137 SetLastError(ERROR_NO_DRIVER_SELECTED);
7138 else
7139 {
7140 SP_DEVINSTALL_PARAMS_W InstallParams;
7141 struct DriverInfoElement *SelectedDriver;
7142 WCHAR SectionName[MAX_PATH];
7143 DWORD SectionNameLength = 0;
7144 PVOID InstallMsgHandler;
7145 PVOID InstallMsgHandlerContext;
7146 PVOID Context = NULL;
7147
7148 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
7149 ret = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
7150 if (!ret)
7151 goto done;
7152
7153 SelectedDriver = (struct DriverInfoElement *)InstallParams.Reserved;
7154 if (!SelectedDriver)
7155 {
7156 SetLastError(ERROR_NO_DRIVER_SELECTED);
7157 goto done;
7158 }
7159
7160 ret = SetupDiGetActualSectionToInstallW(
7161 SelectedDriver->InfFileDetails->hInf,
7162 SelectedDriver->Details.SectionName,
7163 SectionName, MAX_PATH - strlenW(DotCoInstallers), &SectionNameLength, NULL);
7164 if (!ret)
7165 goto done;
7166
7167 if (InstallParams.InstallMsgHandler)
7168 {
7169 InstallMsgHandler = InstallParams.InstallMsgHandler;
7170 InstallMsgHandlerContext = InstallParams.InstallMsgHandlerContext;
7171 }
7172 else
7173 {
7174 Context = SetupInitDefaultQueueCallback(InstallParams.hwndParent);
7175 if (!Context)
7176 goto cleanup;
7177 InstallMsgHandler = SetupDefaultQueueCallback;
7178 InstallMsgHandlerContext = Context;
7179 }
7180 ret = SetupInstallFromInfSectionW(InstallParams.hwndParent,
7181 SelectedDriver->InfFileDetails->hInf, SectionName,
7182 SPINST_FILES, NULL, NULL, SP_COPY_NEWER,
7183 InstallMsgHandler, InstallMsgHandlerContext,
7184 DeviceInfoSet, DeviceInfoData);
7185 if (!ret)
7186 goto done;
7187
7188 /* Install files from .CoInstallers section */
7189 lstrcatW(SectionName, DotCoInstallers);
7190 ret = SetupInstallFromInfSectionW(InstallParams.hwndParent,
7191 SelectedDriver->InfFileDetails->hInf, SectionName,
7192 SPINST_FILES, NULL, NULL, SP_COPY_NEWER,
7193 InstallMsgHandler, InstallMsgHandlerContext,
7194 DeviceInfoSet, DeviceInfoData);
7195 if (!ret)
7196 goto done;
7197
7198 /* Set the DI_NOFILECOPY flag to prevent another
7199 * installation during SetupDiInstallDevice */
7200 InstallParams.Flags |= DI_NOFILECOPY;
7201 ret = SetupDiSetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
7202
7203 cleanup:
7204 if (Context)
7205 SetupTermDefaultQueueCallback(Context);
7206 }
7207
7208 done:
7209 TRACE("Returning %d\n", ret);
7210 return ret;
7211 }
7212
7213 /***********************************************************************
7214 * SetupDiRegisterCoDeviceInstallers (SETUPAPI.@)
7215 */
7216 BOOL WINAPI
7217 SetupDiRegisterCoDeviceInstallers(
7218 IN HDEVINFO DeviceInfoSet,
7219 IN PSP_DEVINFO_DATA DeviceInfoData)
7220 {
7221 BOOL ret = FALSE; /* Return value */
7222
7223 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
7224
7225 if (!DeviceInfoSet)
7226 SetLastError(ERROR_INVALID_PARAMETER);
7227 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
7228 SetLastError(ERROR_INVALID_HANDLE);
7229 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
7230 SetLastError(ERROR_INVALID_HANDLE);
7231 else if (!DeviceInfoData)
7232 SetLastError(ERROR_INVALID_PARAMETER);
7233 else if (DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
7234 SetLastError(ERROR_INVALID_USER_BUFFER);
7235 else
7236 {
7237 SP_DEVINSTALL_PARAMS_W InstallParams;
7238 struct DriverInfoElement *SelectedDriver;
7239 BOOL Result;
7240 DWORD DoAction;
7241 WCHAR SectionName[MAX_PATH];
7242 DWORD SectionNameLength = 0;
7243 HKEY hKey = INVALID_HANDLE_VALUE;
7244 PVOID Context = NULL;
7245
7246 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
7247 Result = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
7248 if (!Result)
7249 goto cleanup;
7250
7251 SelectedDriver = (struct DriverInfoElement *)InstallParams.Reserved;
7252 if (SelectedDriver == NULL)
7253 {
7254 SetLastError(ERROR_NO_DRIVER_SELECTED);
7255 goto cleanup;
7256 }
7257
7258 /* Get .CoInstallers section name */
7259 Result = SetupDiGetActualSectionToInstallW(
7260 SelectedDriver->InfFileDetails->hInf,
7261 SelectedDriver->Details.SectionName,
7262 SectionName, MAX_PATH, &SectionNameLength, NULL);
7263 if (!Result || SectionNameLength > MAX_PATH - strlenW(DotCoInstallers) - 1)
7264 goto cleanup;
7265 lstrcatW(SectionName, DotCoInstallers);
7266
7267 /* Open/Create driver key information */
7268 #if _WIN32_WINNT >= 0x502
7269 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_READ | KEY_WRITE);
7270 #else
7271 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_ALL_ACCESS);
7272 #endif
7273 if (hKey == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND)
7274 hKey = SetupDiCreateDevRegKeyW(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, NULL, NULL);
7275 if (hKey == INVALID_HANDLE_VALUE)
7276 goto cleanup;
7277
7278 /* Install .CoInstallers section */
7279 DoAction = SPINST_REGISTRY;
7280 if (!(InstallParams.Flags & DI_NOFILECOPY))
7281 {
7282 DoAction |= SPINST_FILES;
7283 Context = SetupInitDefaultQueueCallback(InstallParams.hwndParent);
7284 if (!Context)
7285 goto cleanup;
7286 }
7287 Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
7288 SelectedDriver->InfFileDetails->hInf, SectionName,
7289 DoAction, hKey, NULL, SP_COPY_NEWER,
7290 SetupDefaultQueueCallback, Context,
7291 DeviceInfoSet, DeviceInfoData);
7292 if (!Result)
7293 goto cleanup;
7294
7295 ret = TRUE;
7296
7297 cleanup:
7298 if (Context)
7299 SetupTermDefaultQueueCallback(Context);
7300 if (hKey != INVALID_HANDLE_VALUE)
7301 RegCloseKey(hKey);
7302 }
7303
7304 TRACE("Returning %d\n", ret);
7305 return ret;
7306 }
7307
7308 static BOOL
7309 InstallOneInterface(
7310 IN LPGUID InterfaceGuid,
7311 IN LPCWSTR ReferenceString,
7312 IN LPCWSTR InterfaceSection,
7313 IN UINT InterfaceFlags)
7314 {
7315 if (InterfaceFlags != 0)
7316 {
7317 SetLastError(ERROR_INVALID_PARAMETER);
7318 return FALSE;
7319 }
7320
7321 FIXME("Need to InstallOneInterface(%s %s %s %u)\n", debugstr_guid(InterfaceGuid),
7322 debugstr_w(ReferenceString), debugstr_w(InterfaceSection), InterfaceFlags);
7323 return TRUE;
7324 }
7325
7326 /***********************************************************************
7327 * SetupDiInstallDeviceInterfaces (SETUPAPI.@)
7328 */
7329 BOOL WINAPI
7330 SetupDiInstallDeviceInterfaces(
7331 IN HDEVINFO DeviceInfoSet,
7332 IN PSP_DEVINFO_DATA DeviceInfoData)
7333 {
7334 struct DeviceInfoSet *list = NULL;
7335 BOOL ret = FALSE;
7336
7337 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
7338
7339 if (!DeviceInfoSet)
7340 SetLastError(ERROR_INVALID_PARAMETER);
7341 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
7342 SetLastError(ERROR_INVALID_HANDLE);
7343 else if ((list = (struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
7344 SetLastError(ERROR_INVALID_HANDLE);
7345 else if (!DeviceInfoData)
7346 SetLastError(ERROR_INVALID_PARAMETER);
7347 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
7348 SetLastError(ERROR_INVALID_USER_BUFFER);
7349 else
7350 {
7351 struct DriverInfoElement *SelectedDriver;
7352 SP_DEVINSTALL_PARAMS_W InstallParams;
7353 WCHAR SectionName[MAX_PATH];
7354 DWORD SectionNameLength = 0;
7355 INFCONTEXT ContextInterface;
7356 LPWSTR InterfaceGuidString = NULL;
7357 LPWSTR ReferenceString = NULL;
7358 LPWSTR InterfaceSection = NULL;
7359 INT InterfaceFlags;
7360 GUID InterfaceGuid;
7361 BOOL Result;
7362
7363 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
7364 Result = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
7365 if (!Result)
7366 goto cleanup;
7367
7368 SelectedDriver = (struct DriverInfoElement *)InstallParams.Reserved;
7369 if (SelectedDriver == NULL)
7370 {
7371 SetLastError(ERROR_NO_DRIVER_SELECTED);
7372 ret = FALSE;
7373 goto cleanup;
7374 }
7375
7376 /* Get .Interfaces section name */
7377 Result = SetupDiGetActualSectionToInstallW(
7378 SelectedDriver->InfFileDetails->hInf,
7379 SelectedDriver->Details.SectionName,
7380 SectionName, MAX_PATH, &SectionNameLength, NULL);
7381 if (!Result || SectionNameLength > MAX_PATH - strlenW(DotInterfaces) - 1)
7382 goto cleanup;
7383 strcatW(SectionName, DotInterfaces);
7384
7385 ret = TRUE;
7386 Result = SetupFindFirstLineW(
7387 SelectedDriver->InfFileDetails->hInf,
7388 SectionName,
7389 AddInterface,
7390 &ContextInterface);
7391 while (ret && Result)
7392 {
7393 ret = GetStringField(&ContextInterface, 1, &InterfaceGuidString);
7394 if (!ret)
7395 goto cleanup;
7396 else if (strlenW(InterfaceGuidString) != MAX_GUID_STRING_LEN - 1)
7397 {
7398 SetLastError(ERROR_INVALID_PARAMETER);
7399 ret = FALSE;
7400 goto cleanup;
7401 }
7402
7403 InterfaceGuidString[MAX_GUID_STRING_LEN - 2] = '\0'; /* Replace the } by a NULL character */
7404 if (UuidFromStringW(&InterfaceGuidString[1], &InterfaceGuid) != RPC_S_OK)
7405 {
7406 /* Bad GUID, skip the entry */
7407 SetLastError(ERROR_INVALID_PARAMETER);
7408 ret = FALSE;
7409 goto cleanup;
7410 }
7411
7412 ret = GetStringField(&ContextInterface, 2, &ReferenceString);
7413 if (!ret)
7414 goto cleanup;
7415
7416 ret = GetStringField(&ContextInterface, 3, &InterfaceSection);
7417 if (!ret)
7418 goto cleanup;
7419
7420 ret = SetupGetIntField(
7421 &ContextInterface,
7422 4, /* Field index */
7423 &InterfaceFlags);
7424 if (!ret)
7425 {
7426 if (GetLastError() == ERROR_INVALID_PARAMETER)
7427 {
7428 /* The field may be empty. Ignore the error */
7429 InterfaceFlags = 0;
7430 ret = TRUE;
7431 }
7432 else
7433 goto cleanup;
7434 }
7435
7436 /* Install Interface */
7437 ret = InstallOneInterface(&InterfaceGuid, ReferenceString, InterfaceSection, InterfaceFlags);
7438
7439 cleanup:
7440 MyFree(InterfaceGuidString);
7441 MyFree(ReferenceString);
7442 MyFree(InterfaceSection);
7443 InterfaceGuidString = ReferenceString = InterfaceSection = NULL;
7444 Result = SetupFindNextMatchLineW(&ContextInterface, AddInterface, &ContextInterface);
7445 }
7446 }
7447
7448 TRACE("Returning %d\n", ret);
7449 return ret;
7450 }
7451
7452 BOOL
7453 InfIsFromOEMLocation(
7454 IN PCWSTR FullName,
7455 OUT LPBOOL IsOEMLocation)
7456 {
7457 PWCHAR last;
7458
7459 last = strrchrW(FullName, '\\');
7460 if (!last)
7461 {
7462 /* No directory specified */
7463 *IsOEMLocation = FALSE;
7464 }
7465 else
7466 {
7467 WCHAR Windir[MAX_PATH];
7468 UINT ret;
7469
7470 ret = GetWindowsDirectory(Windir, MAX_PATH);
7471 if (ret == 0 || ret >= MAX_PATH)
7472 {
7473 SetLastError(ERROR_GEN_FAILURE);
7474 return FALSE;
7475 }
7476
7477 if (strncmpW(FullName, Windir, last - FullName) == 0)
7478 {
7479 /* The path is %WINDIR%\Inf */
7480 *IsOEMLocation = FALSE;
7481 }
7482 else
7483 {
7484 /* The file is in another place */
7485 *IsOEMLocation = TRUE;
7486 }
7487 }
7488 return TRUE;
7489 }
7490
7491 /***********************************************************************
7492 * SetupDiInstallDevice (SETUPAPI.@)
7493 */
7494 BOOL WINAPI
7495 SetupDiInstallDevice(
7496 IN HDEVINFO DeviceInfoSet,
7497 IN PSP_DEVINFO_DATA DeviceInfoData)
7498 {
7499 SP_DEVINSTALL_PARAMS_W InstallParams;
7500 struct DriverInfoElement *SelectedDriver;
7501 SYSTEMTIME DriverDate;
7502 WCHAR SectionName[MAX_PATH];
7503 WCHAR Buffer[32];
7504 DWORD SectionNameLength = 0;
7505 BOOL Result = FALSE;
7506 ULONG DoAction;
7507 DWORD RequiredSize;
7508 LPWSTR pSectionName = NULL;
7509 WCHAR ClassName[MAX_CLASS_NAME_LEN];
7510 GUID ClassGuid;
7511 LPWSTR lpGuidString = NULL, lpFullGuidString = NULL;
7512 BOOL RebootRequired = FALSE;
7513 HKEY hKey = INVALID_HANDLE_VALUE;
7514 HKEY hClassKey = INVALID_HANDLE_VALUE;
7515 BOOL NeedtoCopyFile;
7516 LARGE_INTEGER fullVersion;
7517 LONG rc;
7518 PVOID Context = NULL;
7519 BOOL ret = FALSE; /* Return value */
7520
7521 TRACE("%p %p\n", DeviceInfoSet, DeviceInfoData);
7522
7523 if (!DeviceInfoSet)
7524 SetLastError(ERROR_INVALID_PARAMETER);
7525 else if (DeviceInfoSet == (HDEVINFO)INVALID_HANDLE_VALUE)
7526 SetLastError(ERROR_INVALID_HANDLE);
7527 else if (((struct DeviceInfoSet *)DeviceInfoSet)->magic != SETUP_DEV_INFO_SET_MAGIC)
7528 SetLastError(ERROR_INVALID_HANDLE);
7529 else if (DeviceInfoData && DeviceInfoData->cbSize != sizeof(SP_DEVINFO_DATA))
7530 SetLastError(ERROR_INVALID_USER_BUFFER);
7531 else
7532 Result = TRUE;
7533
7534 if (!Result)
7535 {
7536 /* One parameter is bad */
7537 goto cleanup;
7538 }
7539
7540 InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS_W);
7541 Result = SetupDiGetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
7542 if (!Result)
7543 goto cleanup;
7544
7545 if (InstallParams.FlagsEx & DI_FLAGSEX_SETFAILEDINSTALL)
7546 {
7547 /* Set FAILEDINSTALL in ConfigFlags registry value */
7548 DWORD ConfigFlags, regType;
7549 Result = SetupDiGetDeviceRegistryPropertyW(
7550 DeviceInfoSet,
7551 DeviceInfoData,
7552 SPDRP_CONFIGFLAGS,
7553 &regType,
7554 (PBYTE)&ConfigFlags,
7555 sizeof(ConfigFlags),
7556 NULL);
7557 if (!Result || regType != REG_DWORD)
7558 {
7559 SetLastError(ERROR_GEN_FAILURE);
7560 goto cleanup;
7561 }
7562 ConfigFlags |= DNF_DISABLED;
7563 Result = SetupDiSetDeviceRegistryPropertyW(
7564 DeviceInfoSet,
7565 DeviceInfoData,
7566 SPDRP_CONFIGFLAGS,
7567 (PBYTE)&ConfigFlags,
7568 sizeof(ConfigFlags));
7569 if (!Result)
7570 {
7571 SetLastError(ERROR_GEN_FAILURE);
7572 goto cleanup;
7573 }
7574
7575 ret = TRUE;
7576 goto cleanup;
7577 }
7578
7579 SelectedDriver = (struct DriverInfoElement *)InstallParams.Reserved;
7580 if (SelectedDriver == NULL)
7581 {
7582 SetLastError(ERROR_NO_DRIVER_SELECTED);
7583 goto cleanup;
7584 }
7585
7586 FileTimeToSystemTime(&SelectedDriver->Info.DriverDate, &DriverDate);
7587
7588 Result = SetupDiGetActualSectionToInstallW(
7589 SelectedDriver->InfFileDetails->hInf,
7590 SelectedDriver->Details.SectionName,
7591 SectionName, MAX_PATH, &SectionNameLength, NULL);
7592 if (!Result || SectionNameLength > MAX_PATH - strlenW(DotServices))
7593 goto cleanup;
7594 pSectionName = &SectionName[strlenW(SectionName)];
7595
7596 /* Get information from [Version] section */
7597 if (!SetupDiGetINFClassW(SelectedDriver->Details.InfFileName, &ClassGuid, ClassName, MAX_CLASS_NAME_LEN, &RequiredSize))
7598 goto cleanup;
7599 /* Format ClassGuid to a string */
7600 if (UuidToStringW((UUID*)&ClassGuid, &lpGuidString) != RPC_S_OK)
7601 goto cleanup;
7602 RequiredSize = lstrlenW(lpGuidString);
7603 lpFullGuidString = HeapAlloc(GetProcessHeap(), 0, (RequiredSize + 3) * sizeof(WCHAR));
7604 if (!lpFullGuidString)
7605 {
7606 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
7607 goto cleanup;
7608 }
7609 lpFullGuidString[0] = '{';
7610 memcpy(&lpFullGuidString[1], lpGuidString, RequiredSize * sizeof(WCHAR));
7611 lpFullGuidString[RequiredSize + 1] = '}';
7612 lpFullGuidString[RequiredSize + 2] = '\0';
7613
7614 /* Open/Create driver key information */
7615 #if _WIN32_WINNT >= 0x502
7616 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_READ | KEY_WRITE);
7617 #else
7618 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_ALL_ACCESS);
7619 #endif
7620 if (hKey == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND)
7621 hKey = SetupDiCreateDevRegKeyW(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, NULL, NULL);
7622 if (hKey == INVALID_HANDLE_VALUE)
7623 goto cleanup;
7624
7625 /* Install main section */
7626 DoAction = 0;
7627 if (!(InstallParams.FlagsEx & DI_FLAGSEX_NO_DRVREG_MODIFY))
7628 DoAction |= SPINST_REGISTRY;
7629 if (!(InstallParams.Flags & DI_NOFILECOPY))
7630 {
7631 DoAction |= SPINST_FILES;
7632 Context = SetupInitDefaultQueueCallback(InstallParams.hwndParent);
7633 if (!Context)
7634 goto cleanup;
7635 }
7636 *pSectionName = '\0';
7637 Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
7638 SelectedDriver->InfFileDetails->hInf, SectionName,
7639 DoAction, hKey, NULL, SP_COPY_NEWER,
7640 SetupDefaultQueueCallback, Context,
7641 DeviceInfoSet, DeviceInfoData);
7642 if (!Result)
7643 goto cleanup;
7644 InstallParams.Flags |= DI_NOFILECOPY;
7645 SetupDiSetDeviceInstallParamsW(DeviceInfoSet, DeviceInfoData, &InstallParams);
7646
7647 /* Write information to driver key */
7648 *pSectionName = UNICODE_NULL;
7649 memcpy(&fullVersion, &SelectedDriver->Info.DriverVersion, sizeof(LARGE_INTEGER));
7650 TRACE("Write information to driver key\n");
7651 TRACE("DriverDate : '%u-%u-%u'\n", DriverDate.wMonth, DriverDate.wDay, DriverDate.wYear);
7652 TRACE("DriverDesc : '%s'\n", debugstr_w(SelectedDriver->Info.Description));
7653 TRACE("DriverVersion : '%u.%u.%u.%u'\n", fullVersion.HighPart >> 16, fullVersion.HighPart & 0xffff, fullVersion.LowPart >> 16, fullVersion.LowPart & 0xffff);
7654 TRACE("InfPath : '%s'\n", debugstr_w(SelectedDriver->Details.InfFileName));
7655 TRACE("InfSection : '%s'\n", debugstr_w(SelectedDriver->Details.SectionName));
7656 TRACE("InfSectionExt : '%s'\n", debugstr_w(&SectionName[strlenW(SelectedDriver->Details.SectionName)]));
7657 TRACE("MatchingDeviceId: '%s'\n", debugstr_w(SelectedDriver->MatchingId));
7658 TRACE("ProviderName : '%s'\n", debugstr_w(SelectedDriver->Info.ProviderName));
7659 swprintf(Buffer, L"%u-%u-%u", DriverDate.wMonth, DriverDate.wDay, DriverDate.wYear);
7660 rc = RegSetValueEx(hKey, REGSTR_DRIVER_DATE, 0, REG_SZ, (const BYTE *)Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR));
7661 if (rc == ERROR_SUCCESS)
7662 rc = RegSetValueEx(hKey, REGSTR_DRIVER_DATE_DATA, 0, REG_BINARY, (const BYTE *)&SelectedDriver->Info.DriverDate, sizeof(FILETIME));
7663 if (rc == ERROR_SUCCESS)
7664 rc = RegSetValueEx(hKey, REGSTR_VAL_DRVDESC, 0, REG_SZ, (const BYTE *)SelectedDriver->Info.Description, (strlenW(SelectedDriver->Info.Description) + 1) * sizeof(WCHAR));
7665 if (rc == ERROR_SUCCESS)
7666 {
7667 swprintf(Buffer, L"%u.%u.%u.%u", fullVersion.HighPart >> 16, fullVersion.HighPart & 0xffff, fullVersion.LowPart >> 16, fullVersion.LowPart & 0xffff);
7668 rc = RegSetValueEx(hKey, REGSTR_DRIVER_VERSION, 0, REG_SZ, (const BYTE *)Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR));
7669 }
7670 if (rc == ERROR_SUCCESS)
7671 rc = RegSetValueEx(hKey, REGSTR_VAL_INFPATH, 0, REG_SZ, (const BYTE *)SelectedDriver->Details.InfFileName, (strlenW(SelectedDriver->Details.InfFileName) + 1) * sizeof(WCHAR));
7672 if (rc == ERROR_SUCCESS)
7673 rc = RegSetValueEx(hKey, REGSTR_VAL_INFSECTION, 0, REG_SZ, (const BYTE *)SelectedDriver->Details.SectionName, (strlenW(SelectedDriver->Details.SectionName) + 1) * sizeof(WCHAR));
7674 if (rc == ERROR_SUCCESS)
7675 rc = RegSetValueEx(hKey, REGSTR_VAL_INFSECTIONEXT, 0, REG_SZ, (const BYTE *)&SectionName[strlenW(SelectedDriver->Details.SectionName)], (strlenW(SectionName) - strlenW(SelectedDriver->Details.SectionName) + 1) * sizeof(WCHAR));
7676 if (rc == ERROR_SUCCESS)
7677 rc = RegSetValueEx(hKey, REGSTR_VAL_MATCHINGDEVID, 0, REG_SZ, (const BYTE *)SelectedDriver->MatchingId, (strlenW(SelectedDriver->MatchingId) + 1) * sizeof(WCHAR));
7678 if (rc == ERROR_SUCCESS)
7679 rc = RegSetValueEx(hKey, REGSTR_VAL_PROVIDER_NAME, 0, REG_SZ, (const BYTE *)SelectedDriver->Info.ProviderName, (strlenW(SelectedDriver->Info.ProviderName) + 1) * sizeof(WCHAR));
7680 if (rc != ERROR_SUCCESS)
7681 {
7682 SetLastError(rc);
7683 goto cleanup;
7684 }
7685 RegCloseKey(hKey);
7686 hKey = INVALID_HANDLE_VALUE;
7687
7688 /* FIXME: Process .LogConfigOverride section */
7689
7690 /* Install .Services section */
7691 strcpyW(pSectionName, DotServices);
7692 Result = SetupInstallServicesFromInfSectionExW(
7693 SelectedDriver->InfFileDetails->hInf,
7694 SectionName,
7695 0,
7696 DeviceInfoSet,
7697 DeviceInfoData,
7698 NULL,
7699 NULL);
7700 if (!Result)
7701 goto cleanup;
7702 if (GetLastError() == ERROR_SUCCESS_REBOOT_REQUIRED)
7703 RebootRequired = TRUE;
7704
7705 /* Copy .inf file to Inf\ directory (if needed) */
7706 Result = InfIsFromOEMLocation(SelectedDriver->InfFileDetails->FullInfFileName, &NeedtoCopyFile);
7707 if (!Result)
7708 goto cleanup;
7709 if (NeedtoCopyFile)
7710 {
7711 Result = SetupCopyOEMInfW(
7712 SelectedDriver->InfFileDetails->FullInfFileName,
7713 NULL,
7714 SPOST_NONE,
7715 SP_COPY_NOOVERWRITE,
7716 NULL, 0,
7717 NULL,
7718 NULL);
7719 if (!Result)
7720 goto cleanup;
7721 /* FIXME: create a new struct InfFileDetails, and set it to SelectedDriver->InfFileDetails,
7722 * to release use of current InfFile */
7723 }
7724
7725 /* Open device registry key */
7726 hKey = SetupDiOpenDevRegKey(DeviceInfoSet, DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_SET_VALUE);
7727 if (hKey == INVALID_HANDLE_VALUE)
7728 goto cleanup;
7729
7730 /* Install .HW section */
7731 DoAction = 0;
7732 if (!(InstallParams.FlagsEx & DI_FLAGSEX_NO_DRVREG_MODIFY))
7733 DoAction |= SPINST_REGISTRY;
7734 strcpyW(pSectionName, DotHW);
7735 Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
7736 SelectedDriver->InfFileDetails->hInf, SectionName,
7737 DoAction, hKey, NULL, 0,
7738 NULL, NULL,
7739 DeviceInfoSet, DeviceInfoData);
7740 if (!Result)
7741 goto cleanup;
7742
7743 /* Write information to enum key */
7744 TRACE("Write information to enum key\n");
7745 TRACE("Class : '%s'\n", debugstr_w(ClassName));
7746 TRACE("ClassGUID : '%s'\n", debugstr_w(lpFullGuidString));
7747 TRACE("DeviceDesc : '%s'\n", debugstr_w(SelectedDriver->Info.Description));
7748 TRACE("Mfg : '%s'\n", debugstr_w(SelectedDriver->Info.MfgName));
7749 rc = RegSetValueEx(hKey, REGSTR_VAL_CLASS, 0, REG_SZ, (const BYTE *)ClassName, (strlenW(ClassName) + 1) * sizeof(WCHAR));
7750 if (rc == ERROR_SUCCESS)
7751 rc = RegSetValueEx(hKey, REGSTR_VAL_CLASSGUID, 0, REG_SZ, (const BYTE *)lpFullGuidString, (strlenW(lpFullGuidString) + 1) * sizeof(WCHAR));
7752 if (rc == ERROR_SUCCESS)
7753 rc = RegSetValueEx(hKey, REGSTR_VAL_DEVDESC, 0, REG_SZ, (const BYTE *)SelectedDriver->Info.Description, (strlenW(SelectedDriver->Info.Description) + 1) * sizeof(WCHAR));
7754 if (rc == ERROR_SUCCESS)
7755 rc = RegSetValueEx(hKey, REGSTR_VAL_MFG, 0, REG_SZ, (const BYTE *)SelectedDriver->Info.MfgName, (strlenW(SelectedDriver->Info.MfgName) + 1) * sizeof(WCHAR));
7756 if (rc != ERROR_SUCCESS)
7757 {
7758 SetLastError(rc);
7759 goto cleanup;
7760 }
7761
7762 /* Start the device */
7763 if (!RebootRequired && !(InstallParams.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT | DI_DONOTCALLCONFIGMG)))
7764 ret = ResetDevice(DeviceInfoSet, DeviceInfoData);
7765 else
7766 ret = TRUE;
7767
7768 cleanup:
7769 /* End of installation */
7770 if (hClassKey != INVALID_HANDLE_VALUE)
7771 RegCloseKey(hClassKey);
7772 if (hKey != INVALID_HANDLE_VALUE)
7773 RegCloseKey(hKey);
7774 if (lpGuidString)
7775 RpcStringFreeW(&lpGuidString);
7776 HeapFree(GetProcessHeap(), 0, lpFullGuidString);
7777 if (Context)
7778 SetupTermDefaultQueueCallback(Context);
7779 TRACE("Returning %d\n", ret);
7780 return ret;
7781 }