summaryrefslogblamecommitdiffstats
path: root/private/windows/spooler/splsetup/utildi.c
blob: df5a53bfd9ab564a6af9c67bae1dedd3b0a9b101 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023






























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                 
/*++

Copyright (c) 1995 Microsoft Corporation
All rights reserved.

Module Name:

    Utildi.c

Abstract:

    Driver Setup DeviceInstaller Utility functions

Author:

    Muhunthan Sivapragasam (MuhuntS) 06-Sep-1995

Revision History:

--*/

#include "precomp.h"
#include <devguid.h>


TCHAR   cszOEMInfGen[]                  = TEXT("%s\\inf\\OEM%d.INF");


BOOL
SetSelectDevParams(
    IN  HDEVINFO    hDevInfo,
    IN  BOOL        bWin95,
    IN  LPCTSTR     pszModel    OPTIONAL
    )
/*++

Routine Description:
    Sets the select device parameters by calling setup apis

Arguments:
    hDevInfo    : Handle to the printer class device information list
    bWin95      : TRUE if selecting Win95 driver, else WinNT driver
    pszModel    : Printer model we are looking for -- only for Win95 case
    
Return Value:
    TRUE on success
    FALSE else

--*/
{
    SP_SELECTDEVICE_PARAMS  SelectDevParams;
    LPTSTR                  pszWin95Instn;
    
    SelectDevParams.ClassInstallHeader.cbSize
                                 = sizeof(SelectDevParams.ClassInstallHeader);
    SelectDevParams.ClassInstallHeader.InstallFunction
                                 = DIF_SELECTDEVICE;

    //
    // Get current SelectDevice parameters, and then set the fields
    // we want to be different from default
    //
    if ( !SetupDiGetClassInstallParams(
                        hDevInfo,
                        NULL,
                        &SelectDevParams.ClassInstallHeader,
                        sizeof(SelectDevParams),
                        NULL) ) {

        if ( GetLastError() != ERROR_NO_CLASSINSTALL_PARAMS )
            return FALSE;
        
        ZeroMemory(&SelectDevParams, sizeof(SelectDevParams));
        SelectDevParams.ClassInstallHeader.cbSize
                                 = sizeof(SelectDevParams.ClassInstallHeader);
        SelectDevParams.ClassInstallHeader.InstallFunction
                                 = DIF_SELECTDEVICE;
    }

    //
    // Set the strings to use on the select driver page ..
    //
    LoadString(ghInst,
               IDS_PRINTERWIZARD,
               SelectDevParams.Title,
               sizeof(SelectDevParams.Title)/sizeof(SelectDevParams.Title[0]));

    //
    // For Win95 drivers instructions are different than NT drivers
    //
    if ( bWin95 ) {

        pszWin95Instn = GetStringFromRcFile(IDS_WIN95DEV_INSTRUCT);
        if ( !pszWin95Instn )
            return FALSE;

        if ( lstrlen(pszWin95Instn) + lstrlen(pszModel) + 1 
                            > sizeof(SelectDevParams.Instructions) ) {

            FreeStr(pszWin95Instn);
            return FALSE;
        }

        wsprintf(SelectDevParams.Instructions, pszWin95Instn, pszModel);
        FreeStr(pszWin95Instn);
    } else {

        LoadString(ghInst,
                   IDS_WINNTDEV_INSTRUCT,
                   SelectDevParams.Instructions,
                   sizeof(SelectDevParams.Instructions)/sizeof(SelectDevParams.Instructions[0]));
    }

    LoadString(ghInst,
               IDS_SELECTDEV_LABEL,
               SelectDevParams.ListLabel,
               sizeof(SelectDevParams.ListLabel)/sizeof(SelectDevParams.ListLabel[0]));

    return SetupDiSetClassInstallParams(
                                hDevInfo,
                                NULL,
                                &SelectDevParams.ClassInstallHeader,
                                sizeof(SelectDevParams));

}


BOOL
SetDevInstallParams(
    IN  HDEVINFO    hDevInfo,
    IN  HWND        hwnd,
    IN  LPCTSTR     pszDriverPath   OPTIONAL
    )
/*++

Routine Description:
    Sets the device installation parameters by calling setup apis

Arguments:
    hDevInfo        : Handle to the printer class device information list
    hwnd            : Window handle that owns the UI
    pszDriverPath   : Path where INF file should be searched
    
Return Value:
    TRUE on success
    FALSE else

--*/
{
    SP_DEVINSTALL_PARAMS    DevInstallParams;

    //
    // Get current SelectDevice parameters, and then set the fields
    // we wanted changed from default
    //
    DevInstallParams.cbSize = sizeof(DevInstallParams);
    if ( !SetupDiGetDeviceInstallParams(hDevInfo,
                                        NULL,
                                        &DevInstallParams) ) {

        return FALSE;
    }

    //
    // Drivers are class drivers,
    // ntprint.inf is sorted do not waste time sorting,
    // show Have Disk button,
    // use our strings on the select driver page
    //
    DevInstallParams.Flags     |= DI_SHOWCLASS | DI_INF_IS_SORTED
                                               | DI_SHOWOEM
                                               | DI_USECI_SELECTSTRINGS;

    if ( pszDriverPath && *pszDriverPath ) {

        lstrcpy(DevInstallParams.DriverPath, pszDriverPath);
    }

    DevInstallParams.hwndParent = hwnd;

    return SetupDiSetDeviceInstallParams(hDevInfo,
                                         NULL,
                                         &DevInstallParams);
}


BOOL
PSetupBuildDriversFromPath(
    IN  HANDLE      h,
    IN  LPCTSTR     pszDriverPath,
    IN  BOOL        bEnumSingleInf
    )
/*++

Routine Description:
    Sets the device installation path (INF and driver dir) by calling setup apis

Arguments:
    h               : Handle from PSetupCreateDrvSetupParams
    pszDriverPath   : Path where INF file should be searched
    bEnumSingleInf  : Tells if pszDriverPath is a filename instead of path
    
Return Value:
    TRUE on success
    FALSE else

--*/
{
    HDEVINFO                hDevInfo = (HDEVINFO)h;
    SP_DEVINSTALL_PARAMS    DevInstallParams;

    //
    // Get current SelectDevice parameters, and then set the fields
    // we wanted changed from default
    //
    DevInstallParams.cbSize = sizeof(DevInstallParams);
    if ( !SetupDiGetDeviceInstallParams(hDevInfo,
                                        NULL,
                                        &DevInstallParams) ) {

        return FALSE;
    }

    DevInstallParams.Flags  |= DI_INF_IS_SORTED;

    if ( bEnumSingleInf )
        DevInstallParams.Flags  |= DI_ENUMSINGLEINF;

    lstrcpy(DevInstallParams.DriverPath, pszDriverPath);
    
    SetupDiDestroyDriverInfoList(hDevInfo,
                                 NULL,
                                 SPDIT_CLASSDRIVER);

    return SetupDiSetDeviceInstallParams(hDevInfo,
                                         NULL,
                                         &DevInstallParams) &&
           SetupDiBuildDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER);
}


VOID
PSetupDestroyDrvSetupParams(
    IN  HANDLE h
    )
/*++

Routine Description:
    This routine should be called at the end to destroy the driver setup
    information

Arguments:
    h   : Handle which was created by calling PSetupCreateDrvSetupParams

Return Value:
    Nothing

--*/
{

    if ( h ) {

        SetupDiDestroyDeviceInfoList((HDEVINFO) h);
    }
}


HDEVINFO
CreatePrinterDevInfo(
    VOID
    )
/*++

Routine Description:
    Creates a printer HDEVINFO by calling setup apis/

Arguments:
    None

Return Value:
    Valid HDEVINFO on success, NULL on error

--*/
{
    HDEVINFO    hDevInfo;

    hDevInfo = SetupDiCreateDeviceInfoList((LPGUID)&GUID_DEVCLASS_PRINTER, NULL);

    return hDevInfo != INVALID_HANDLE_VALUE ? hDevInfo : NULL;
}


HANDLE
PSetupCreateDrvSetupParams(
    VOID
    )
/*++

Routine Description:
    This routine should be called at the beginning to do the initialization

Arguments:
    None

Return Value:
    A handle which will be used on any subsequent calls to the driver setup
    routines. Handle will be NULL on error

--*/
{
    HDEVINFO                hDevInfo;

    hDevInfo = CreatePrinterDevInfo();
    if ( !hDevInfo )
        return NULL;

    if ( !SetSelectDevParams(hDevInfo, FALSE, NULL) ) {

        PSetupDestroyDrvSetupParams(hDevInfo);
        return NULL;
    }

    return (HANDLE) hDevInfo;
}


HPROPSHEETPAGE
PSetupCreateDrvSetupPage(
    IN  HANDLE  h,
    IN  HWND    hwnd
    )
/*++

Routine Description:
    Returns the print driver selection property page

Arguments:
    h               : Handle from PSetupCreateDrvSetupParams
    hwnd            : Window handle that owns the UI
    pszManufacturer : Manufacturer to preselect
    pszModel        : Model to preselect
    
Return Value:
    Handle to the property page, NULL on failure -- use GetLastError()

--*/
{
    HDEVINFO                hDevInfo = (HDEVINFO) h;
    SP_INSTALLWIZARD_DATA   InstallWizardData;

    if ( !SetDevInstallParams(hDevInfo, hwnd, NULL) ) {

        return NULL;
    }

    ZeroMemory(&InstallWizardData, sizeof(InstallWizardData));
    InstallWizardData.ClassInstallHeader.cbSize
                            = sizeof(InstallWizardData.ClassInstallHeader);
    InstallWizardData.ClassInstallHeader.InstallFunction
                            = DIF_INSTALLWIZARD;

    InstallWizardData.DynamicPageFlags  = DYNAWIZ_FLAG_PAGESADDED;
    InstallWizardData.hwndWizardDlg     = hwnd;

    return SetupDiGetWizardPage(hDevInfo,
                                NULL,
                                &InstallWizardData,
                                SPWPT_SELECTDEVICE,
                                0);

}


VOID
PSetupDestroySelectedDriverInfo(
    IN  PSELECTED_DRV_INFO      pSelectedDrvInfo
    )
/*++

Routine Description:
    Frees all memory allocated for the SELECTED_DRV_INFO structure and the
    fields in it

Arguments:
    pSelectedDrvInfo    : Pointer to the structure to destory information
    
Return Value:
    Nothing

--*/
{
    if ( pSelectedDrvInfo ) {

        FreeStr(pSelectedDrvInfo->pszModelName);
        FreeStr(pSelectedDrvInfo->pszDriverSection);
        FreeStr(pSelectedDrvInfo->pszInfFile);
        FreeMem(pSelectedDrvInfo);
    }
}


PSELECTED_DRV_INFO
SelectedDriverInfoFromDrvInfo(
    IN  HDEVINFO    hDevInfo,
    IN  PSP_DRVINFO_DATA    pDrvInfoData
    )
/*++

Routine Description:
    Gets the INF file, driver section name for a give driver info and builds
    a SELECTED_DRV_INFO

Arguments:
    hDevInfo    - Handle to the printer class device information list
    pDrvInfoData    - Points to a SP_DRVINFO_DATA giving the driver model
                      to retrive information
    
Return Value:
    On success -- A non null pointer value to SELECTED_DRV_INFO
    On failure -- NULL, GetLastError to get error code

--*/
{
    PSP_DRVINFO_DETAIL_DATA     pDrvInfoDetailData;
    PSELECTED_DRV_INFO          pSelectedDrvInfo;
    BOOL                        bRet = FALSE;
    DWORD                       dwNeeded;

    pSelectedDrvInfo    = (PSELECTED_DRV_INFO)
                                AllocMem(sizeof(*pSelectedDrvInfo));
    pDrvInfoDetailData  = (PSP_DRVINFO_DETAIL_DATA)
                                AllocMem(sizeof(*pDrvInfoDetailData));

    if ( !pSelectedDrvInfo || !pDrvInfoDetailData )
        goto Cleanup;

    ZeroMemory(pSelectedDrvInfo, sizeof(*pSelectedDrvInfo));
    pDrvInfoDetailData->cbSize = sizeof(*pDrvInfoDetailData);

    if ( !SetupDiGetDriverInfoDetail(hDevInfo,
                                     NULL,
                                     pDrvInfoData,
                                     pDrvInfoDetailData,
                                     sizeof(*pDrvInfoDetailData),
                                     &dwNeeded) ) {

        if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER ) {

            goto Cleanup;
        }

        FreeMem(pDrvInfoDetailData);
        pDrvInfoDetailData = (PSP_DRVINFO_DETAIL_DATA) AllocMem(dwNeeded);

        if ( !pDrvInfoDetailData )
            goto Cleanup;
                
        pDrvInfoDetailData->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);

        if ( !SetupDiGetDriverInfoDetail(hDevInfo,
                                         NULL,
                                         pDrvInfoData,
                                         pDrvInfoDetailData,
                                         dwNeeded,
                                         &dwNeeded) ) {

            goto Cleanup;
        }
    }

    //
    // Get modelname, driversection, and inffile
    //
    pSelectedDrvInfo->pszModelName      = AllocStr(pDrvInfoData->Description);
    pSelectedDrvInfo->pszDriverSection  = AllocStr(pDrvInfoDetailData->SectionName);
    pSelectedDrvInfo->pszInfFile        = AllocStr(pDrvInfoDetailData->InfFileName);

    if ( pSelectedDrvInfo->pszModelName &&
         pSelectedDrvInfo->pszDriverSection &&
         pSelectedDrvInfo->pszInfFile ) {

        bRet = TRUE;
    }

Cleanup:

    if ( pDrvInfoDetailData ) {

        FreeMem(pDrvInfoDetailData);
    }

    if ( !bRet ) {

        PSetupDestroySelectedDriverInfo(pSelectedDrvInfo);
        return NULL;
    }

    return pSelectedDrvInfo;
}


BOOL
CopyOEMInfFileAndGiveUniqueName(
    IN  HANDLE  h,
    IN  LPTSTR  pszInfFile
    )
/*++

Routine Description:

    This routine checks if an OEM driver list is being used and if so
    copies the OEM printer inf file to "<systemroot>\Inf\OEM<n>.INF".
    Where n is the first unused file number.

Arguments:

    h   : Handle got by calling PSetupCreateDrvSetupParams
    pszInfFile  : Fully qualified path of OEM inf file

Return Value:
    TRUE if no error, FALSE else

--*/
{
    SP_DEVINSTALL_PARAMS    DevInstallParams;
    HANDLE                  hFile;
    TCHAR                   szNewFileName[MAX_PATH], szSystemDir[MAX_PATH];
    DWORD                   i;

    DevInstallParams.cbSize         = sizeof(DevInstallParams);
    DevInstallParams.DriverPath[0]  = 0;

    //
    // Check DeviceInstallParams to see if OEM driver list is built
    //
    if ( !SetupDiGetDeviceInstallParams((HDEVINFO)h,
                                       NULL,
                                       &DevInstallParams) ) {

        return FALSE;
    }

    //
    // If DriverPath is clear then not an OEM driver
    if ( !DevInstallParams.DriverPath[0] ) {

        return TRUE;
    }

    if ( !GetWindowsDirectory(szSystemDir,
                              sizeof(szSystemDir)/sizeof(szSystemDir[0])) ) {

        return FALSE;
    }

    if ( lstrlen(szSystemDir) + lstrlen(cszOEMInfGen) + 5
                            > sizeof(szNewFileName)/sizeof(szNewFileName[0]) ) {

        return FALSE;
    }

    for ( i = 0 ; i < 10000 ; ++i ) {

        wsprintf(szNewFileName, cszOEMInfGen, szSystemDir, i);

        //
        // By using the CREATE_NEW flag we reserve the file name and
        // will not end up overwriting another file which gets created
        // by another setup (some inf) thread
        //
        h = CreateFile(szNewFileName,
                       0,
                       FILE_SHARE_READ | FILE_SHARE_WRITE,
                       NULL,
                       CREATE_NEW,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);

        if ( h != INVALID_HANDLE_VALUE ) {

            CloseHandle(h);
            return CopyFile(pszInfFile,
                            szNewFileName,
                            FALSE);
        } else if ( GetLastError() != ERROR_FILE_EXISTS ) {

            return FALSE;
        }
    }

    return FALSE;
}


PSELECTED_DRV_INFO
GetSelectedDriverInfo(
    IN  HDEVINFO    hDevInfo
    )
/*++

Routine Description:
    Gets the selected diver information by calling the setupapi routines

Arguments:
    hDevInfo    - Handle to the printer class device information list
    
Return Value:
    On success -- A non null pointer value to SELECTED_DRV_INFO
    On failure -- NULL, GetLastError to get error code

--*/
{
    SP_DRVINFO_DATA             DrvInfoData;


    DrvInfoData.cbSize = sizeof(DrvInfoData);

    if ( SetupDiGetSelectedDriver(hDevInfo, NULL, &DrvInfoData) ) {


        return SelectedDriverInfoFromDrvInfo(hDevInfo, &DrvInfoData);
    }

    return NULL;
}


PSELECTED_DRV_INFO
PSetupGetSelectedDriverInfo(
    IN HANDLE   h
    )
/*++

Routine Description:
    Gets the selected diver information by calling the setupapi routines

Arguments:
    h   : Handle got by calling PSetupCreateDrvSetupParams
    
Return Value:
    On success -- A non null pointer value to SELECTED_DRV_INFO
    On failure -- NULL, GetLastError to get error code

--*/
{
    return GetSelectedDriverInfo((HDEVINFO)h);
}


BOOL
SelectDriver(
    IN  HDEVINFO    hDevInfo
    )
/*++

Routine Description:
    Display manufacturer/model information and have the user select a
    printer driver. GetSelectedDriver will give the selected driver.

Arguments:
    hDevInfo    - Handle to the printer class device information list
    
Return Value:
    TRUE on success, FALSE on error

--*/
{

    return SetupDiSelectDevice(hDevInfo, NULL);
}


BOOL
PSetupSelectDriver(
    IN HANDLE   h,
    IN HWND     hwnd
    )
/*++

Routine Description:
    Display manufacturer/model information and have the user select a
    printer driver. Use PSetupGetSelectedDriverInfo

Arguments:
    h       : Handle got by call to PSetupCreateDrvSetupParams
    hwnd    : Window handle
    
Return Value:
    TRUE on success, FALSE on error

--*/
{
    HDEVINFO    hDevInfo = (HDEVINFO)h;

    return  SetDevInstallParams(hDevInfo,
                                hwnd,
                                NULL)       &&
            SelectDriver(hDevInfo);
}


VOID
GetDriverPath(
    IN  HANDLE      h,
    OUT LPTSTR      pszDriverPath
    )
/*++

Routine Description:
    Gets the path where driver files should be searched first to copy from

Arguments:
    h               : Handle got from call to PSetupCreateDrvSetupParams
    pszDriverPath   : buffer to copy the driver path to

Return Value:
    Nothing

--*/
{
    LPTSTR                 *List;
    DWORD                   dwCount;
    SP_DEVINSTALL_PARAMS    DevInstallParams;

    //
    // If we have hDevInfo for printer check if DeviceInstallParams have
    // been set
    //
    DevInstallParams.cbSize         = sizeof(DevInstallParams);
    DevInstallParams.DriverPath[0]  = 0;

    //
    // First call DeviceInstaller API to findout if we should be looking
    // in a specific place for drivers (ex. HaveDisk case)
    // Second try to get from mru
    //
    //
    if ( SetupDiGetDeviceInstallParams((HDEVINFO)h,
                                       NULL,
                                       &DevInstallParams) &&
         DevInstallParams.DriverPath[0] ) {

        lstrcpy(pszDriverPath, DevInstallParams.DriverPath);
        return;
    }

    if ( SetupQuerySourceList(SRCLIST_USER | SRCLIST_SYSTEM,
                              &List, &dwCount) ) {

        lstrcpy(pszDriverPath, List[0]);
        SetupFreeSourceList(&List, dwCount);
    } else {

        //
        // Default put A:\ since we have to give something to setup
        //
        lstrcpy(pszDriverPath, TEXT("A:\\"));
    }
}


BOOL
BuildClassDriverList(
    IN HDEVINFO    hDevInfo
    )
/*++

Routine Description:
    Build the class driver list.

    Note: If driver list is already built this comes back immediately

Arguments:
    hDevInfo    : Handle to the printer class device information list
    
Return Value:
    TRUE on success, FALSE on error

--*/
{
    return SetupDiBuildDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER);
}


BOOL
PSetupRefreshDriverList(
    IN HANDLE   h
    )
/*++

Routine Description:
    Destroy current driver list and build new one

Arguments:
    h   : Handle got by calling PSetupCreateDrvSetupParams
    
Return Value:
    TRUE on success, FALSE on error

--*/
{
    HDEVINFO                hDevInfo = (HDEVINFO) h;
    SP_DEVINSTALL_PARAMS    DevInstallParams;

    DevInstallParams.cbSize         = sizeof(DevInstallParams);
    DevInstallParams.DriverPath[0]  = 0;

    //
    // Check DeviceInstallParams to see if OEM driver list is built
    //
    if ( SetupDiGetDeviceInstallParams(hDevInfo,
                                       NULL,
                                       &DevInstallParams) &&
         !DevInstallParams.DriverPath[0] ) {

        return TRUE;
    }

    //
    // Destroy current list and build another one
    //
    SetupDiDestroyDriverInfoList(hDevInfo,
                                 NULL,
                                 SPDIT_CLASSDRIVER);

    DevInstallParams.DriverPath[0] = sZero;

    return SetupDiSetDeviceInstallParams(hDevInfo,
                                         NULL,
                                         &DevInstallParams) &&

           BuildClassDriverList(hDevInfo);
}


PSELECTED_DRV_INFO
DriverInfoFromName(
    IN HDEVINFO     hDevInfo,
    IN LPCTSTR      pszModel
    )
/*++

Routine Description:
    Given a printer driver model name get the SELECTED_DRV_INFO for it
    for a given device info set

Arguments:
    hDeVInfo    : Handle to the device information set
    pszModel    : Driver model name
    
Return Value:
    Valid pointer to a SELECTED_DRV_INFO on success, NULL on error

--*/
{
    SP_DRVINFO_DATA     DrvInfoData;
    DWORD               dwIndex;

    dwIndex = 0;
    DrvInfoData.cbSize = sizeof(DrvInfoData);

    while ( SetupDiEnumDriverInfo(hDevInfo, NULL, SPDIT_CLASSDRIVER,
                                  dwIndex, &DrvInfoData) ) {

        if ( !lstrcmp(pszModel, DrvInfoData.Description) ) {

            return SelectedDriverInfoFromDrvInfo(hDevInfo, &DrvInfoData);
        }

        DrvInfoData.cbSize = sizeof(DrvInfoData);
        ++dwIndex;
    }

    SetLastError(ERROR_UNKNOWN_PRINTER_DRIVER);
    return NULL;
}


PSELECTED_DRV_INFO
PSetupDriverInfoFromName(
    IN  HANDLE      h,
    IN  LPCTSTR     pszModel
    )
/*++

Routine Description:
    Given a printer driver model name get the SELECTED_DRV_INFO for it

Arguments:
    h           : Handle got by calling PSetupCreateDrvSetupParams
    pszModel    : Driver model name

Return Value:
    Valid pointer to SELECTED_DRV_INFO on success (finding model in an INF)
    NULL else (do GetLastError() to get the last error)

--*/
{
    HDEVINFO    hDevInfo = (HDEVINFO)h;

    if ( BuildClassDriverList(hDevInfo) )
        return DriverInfoFromName(hDevInfo, pszModel);
    else
        return NULL;
}


BOOL
PreSelectDriver(
    IN  HDEVINFO    hDevInfo,
    IN  LPCTSTR     pszManufacturer,
    IN  LPCTSTR     pszModel
    )
/*++

Routine Description:
    Preselect a manufacturer and model for the driver dialog

    If same model is found select it, else if a match in manufacturer is
    found select first driver for the manufacturer.

Arguments:
    hDevInfo        : Handle to printer DeviceInfoSet
    pszManufacturer : Manufacterer name to preselect
    pszModel        : Model name to preselect

Return Value:
    TRUE on a model or manufacturer match
    FALSE else

--*/
{
    SP_DRVINFO_DATA     DrvInfoData;
    DWORD               dwIndex, dwManf, dwMod;

    if ( !BuildClassDriverList(hDevInfo) ) {

        return FALSE;
    }

    dwIndex = 0;

    //
    // If no model/manf given select first driver
    //
    if ( pszManufacturer && *pszManufacturer && pszModel && *pszModel ) {

        dwManf = dwMod = MAX_DWORD;
        DrvInfoData.cbSize = sizeof(DrvInfoData);

        while ( SetupDiEnumDriverInfo(hDevInfo, NULL, SPDIT_CLASSDRIVER,
                                      dwIndex, &DrvInfoData) ) {

            if ( dwManf == MAX_DWORD &&
                 !lstrcmp(pszManufacturer, DrvInfoData.MfgName) ) {

                dwManf = dwIndex;
            }

            if ( !lstrcmp(pszModel, DrvInfoData.Description) ) {

                dwMod = dwIndex;
                break; // the for loop
            }

            DrvInfoData.cbSize = sizeof(DrvInfoData);
            ++dwIndex;
        }

        if ( dwMod != MAX_DWORD ) {

            dwIndex = dwMod;
        } else if ( dwManf != MAX_DWORD ) {

            dwIndex = dwManf;
        } else {

            SetLastError(ERROR_UNKNOWN_PRINTER_DRIVER);
            return FALSE;
        }
    }

    DrvInfoData.cbSize = sizeof(DrvInfoData);
    if ( SetupDiEnumDriverInfo(hDevInfo, NULL, SPDIT_CLASSDRIVER,
                               dwIndex, &DrvInfoData)   &&
         SetupDiSetSelectedDriver(hDevInfo, NULL, &DrvInfoData) ) {

        return TRUE;
    }

    return FALSE;
}


BOOL
PSetupPreSelectDriver(
    IN  HANDLE      h,
    IN  LPCTSTR     pszManufacturer,
    IN  LPCTSTR     pszModel
    )
/*++

Routine Description:
    Preselect a manufacturer and model for the driver dialog

    If same model is found select it, else if a match in manufacturer is
    found select first driver for the manufacturer.

Arguments:
    hDevInfo        : Handle to printer DeviceInfoSet
    pszManufacturer : Manufacterer name to preselect
    pszModel        : Model name to preselect

Return Value:
    TRUE on a model or manufacturer match
    FALSE else

--*/
{
    HDEVINFO    hDevInfo = (HDEVINFO)h;

    return PreSelectDriver(hDevInfo, pszManufacturer, pszModel);
}