Sample Code

Windows Driver Samples/ OEM Printer Customization Plug-in Samples/ C++/ PSUIRep/ Features.cpp/

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
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
//  Copyright  2001 - 2003  Microsoft Corporation.  All Rights Reserved.
//
//  FILE:    Features.cpp
//
//  PURPOSE:  Implementation wrapper class for PScript Driver Features and Options.
//
 
#include "precomp.h"
#include "resource.h"
#include "debug.h"
#include "globals.h"
#include "devmode.h"
#include "stringutils.h"
#include "helper.h"
#include "features.h"
#include "oemui.h"
 
// This indicates to Prefast that this is a usermode driver file.
_Analysis_mode_(_Analysis_code_type_user_driver_);
 
 
////////////////////////////////////////////////////////
//      Internal Defines and Macros
////////////////////////////////////////////////////////
 
#define INITIAL_ENUM_FEATURES_SIZE          1024
#define INITIAL_ENUM_OPTIONS_SIZE           64
#define INITIAL_FEATURE_DISPLAY_NAME_SIZE   64
#define INITIAL_OPTION_DISPLAY_NAME_SIZE    32
#define INITIAL_GET_OPTION_SIZE             64
#define INITIAL_GET_REASON_SIZE             1024
 
#define DRIVER_FEATURE_PREFIX               '%'
#define IS_DRIVER_FEATURE(f)                (DRIVER_FEATURE_PREFIX == (f)[0])
 
// Flags that the uDisplayNameID should be returned as
// MAKEINTRESOURCE() instead of loading the string resource.
#define RETURN_INT_RESOURCE     1
 
// Macros to test for conditions of KEYWORDMAP entry.
#define IS_MAPPING_INT_RESOURCE(p)  ((p)->dwFlags & RETURN_INT_RESOURCE)
 
// TAG the identifies feature OPTITEM data stuct.
#define FEATURE_OPTITEM_TAG     'FETR'
 
 
////////////////////////////////////////////////////////
//      Type Definitions
////////////////////////////////////////////////////////
 
// Struct used to identify OPTITEM as
// feature OPTITEM and to map back from
// an OPTITEM to the feature.
typedef struct _tagFeatureOptitemData
{
    DWORD       dwSize;
    DWORD       dwTag;
    PCSTR       pszFeatureKeyword;
    COptions    *pOptions;
 
} FEATUREOPTITEMDATA, *PFEATUREOPTITEMDATA;
 
 
 
////////////////////////////////////////////////////////
//      Internal Constants
////////////////////////////////////////////////////////
 
static KEYWORDMAP gkmFeatureMap[] =
{
    "%AddEuro",                 NULL,                   IDS_ADD_EURO,           OEMCUIP_PRNPROP,    0,
    "%CtrlDAfter",              NULL,                   IDS_CTRLD_AFTER,        OEMCUIP_PRNPROP,    0,
    "%CtrlDBefore",             NULL,                   IDS_CTRLD_BEFORE,       OEMCUIP_PRNPROP,    0,
    //"%CustomPageSize",          NULL,                   IDS_PSCRIPT_CUSTOMSIZE, OEMCUIP_DOCPROP,    0,
    "%GraphicsTrueGray",        NULL,                   IDS_TRUE_GRAY_GRAPH,    OEMCUIP_PRNPROP,    0,
    "%JobTimeout",              NULL,                   IDS_JOBTIMEOUT,         OEMCUIP_PRNPROP,    0,
    "%MaxFontSizeAsBitmap",     NULL,                   IDS_PSMAXBITMAP,        OEMCUIP_PRNPROP,    0,
    "%MetafileSpooling",        NULL,                   IDS_METAFILE_SPOOLING,  OEMCUIP_DOCPROP,    0,
    "%MinFontSizeAsOutline",    NULL,                   IDS_PSMINOUTLINE,       OEMCUIP_PRNPROP,    0,
    "%Mirroring",               NULL,                   IDS_MIRROR,             OEMCUIP_DOCPROP,    0,
    "%Negative",                NULL,                   IDS_NEGATIVE_PRINT,     OEMCUIP_DOCPROP,    0,
    "%Orientation",             TEXT("COMPSTUI.DLL"),   IDS_CPSUI_ORIENTATION,  OEMCUIP_DOCPROP,    RETURN_INT_RESOURCE,
    "%OutputFormat",            NULL,                   IDS_PSOUTPUT_OPTION,    OEMCUIP_DOCPROP,    0,
    "%OutputProtocol",          NULL,                   IDS_PSPROTOCOL,         OEMCUIP_PRNPROP,    0,
    "%OutputPSLevel",           NULL,                   IDS_PSLEVEL,            OEMCUIP_DOCPROP,    0,
    "%PageOrder",               TEXT("COMPSTUI.DLL"),   IDS_CPSUI_PAGEORDER,    OEMCUIP_DOCPROP,    RETURN_INT_RESOURCE,
    "%PagePerSheet",            TEXT("COMPSTUI.DLL"),   IDS_CPSUI_NUP,          OEMCUIP_DOCPROP,    RETURN_INT_RESOURCE,
    "%PSErrorHandler",          NULL,                   IDS_PSERROR_HANDLER,    OEMCUIP_DOCPROP,    0,
    "%PSMemory",                NULL,                   IDS_POSTSCRIPT_VM,      OEMCUIP_PRNPROP,    0,
    "%TextTrueGray",            NULL,                   IDS_TRUE_GRAY_TEXT,     OEMCUIP_PRNPROP,    0,
    "%TTDownloadFormat",        NULL,                   IDS_PSTT_DLFORMAT,      OEMCUIP_DOCPROP,    0,
    "%WaitTimeout",             NULL,                   IDS_WAITTIMEOUT,        OEMCUIP_PRNPROP,    0,
};
static const NUM_FEATURE_MAP    = (sizeof(gkmFeatureMap)/sizeof(gkmFeatureMap[0]));
 
 
static KEYWORDMAP gkmOptionMap[] =
{
    "True",             TEXT("COMPSTUI.DLL"),       IDS_CPSUI_TRUE,             0,  RETURN_INT_RESOURCE,
    "False",            TEXT("COMPSTUI.DLL"),       IDS_CPSUI_FALSE,            0,  RETURN_INT_RESOURCE,
    "Portrait",         TEXT("COMPSTUI.DLL"),       IDS_CPSUI_PORTRAIT,         0,  RETURN_INT_RESOURCE,
    "Landscape",        TEXT("COMPSTUI.DLL"),       IDS_CPSUI_LANDSCAPE,        0,  RETURN_INT_RESOURCE,
    "RotatedLandscape", TEXT("COMPSTUI.DLL"),       IDS_CPSUI_ROT_LAND,         0,  RETURN_INT_RESOURCE,
    "Speed",            NULL,                       IDS_PSOPT_SPEED,            0,  RETURN_INT_RESOURCE,
    "Portability",      NULL,                       IDS_PSOPT_PORTABILITY,      0,  0,
    "EPS",              NULL,                       IDS_PSOPT_EPS,              0,  0,
    "Archive",          NULL,                       IDS_PSOPT_ARCHIVE,          0,  0,
    "ASCII",            NULL,                       IDS_PSPROTOCOL_ASCII,       0,  0,
    "BCP",              NULL,                       IDS_PSPROTOCOL_BCP,         0,  0,
    "TBCP",             NULL,                       IDS_PSPROTOCOL_TBCP,        0,  0,
    "Binary",           NULL,                       IDS_PSPROTOCOL_BINARY,      0,  0,
    "FrontToBack",      TEXT("COMPSTUI.DLL"),       IDS_CPSUI_FRONTTOBACK,      0,  RETURN_INT_RESOURCE,
    "BackToFront",      TEXT("COMPSTUI.DLL"),       IDS_CPSUI_BACKTOFRONT,      0,  RETURN_INT_RESOURCE,
    "1",                TEXT("COMPSTUI.DLL"),       IDS_CPSUI_NUP_NORMAL,       0,  RETURN_INT_RESOURCE,
    "2",                TEXT("COMPSTUI.DLL"),       IDS_CPSUI_NUP_TWOUP,        0,  RETURN_INT_RESOURCE,
    "4",                TEXT("COMPSTUI.DLL"),       IDS_CPSUI_NUP_FOURUP,       0,  RETURN_INT_RESOURCE,
    "6",                TEXT("COMPSTUI.DLL"),       IDS_CPSUI_NUP_SIXUP,        0,  RETURN_INT_RESOURCE,
    "9",                TEXT("COMPSTUI.DLL"),       IDS_CPSUI_NUP_NINEUP,       0,  RETURN_INT_RESOURCE,
    "16",               TEXT("COMPSTUI.DLL"),       IDS_CPSUI_NUP_SIXTEENUP,    0,  RETURN_INT_RESOURCE,
    "Booklet",          TEXT("COMPSTUI.DLL"),       IDS_CPSUI_BOOKLET,          0,  RETURN_INT_RESOURCE,
    "Automatic",        NULL,                       IDS_TTDL_DEFAULT,           0,  0,
    "Outline",          NULL,                       IDS_TTDL_TYPE1,             0,  0,
    "Bitmap",           NULL,                       IDS_TTDL_TYPE3,             0,  0,
    "NativeTrueType",   NULL,                       IDS_TTDL_TYPE42,            0,  0,
};
static const NUM_OPTION_MAP     = (sizeof(gkmOptionMap)/sizeof(gkmOptionMap[0]));
 
 
 
 
////////////////////////////////////////////
//
//  COptions Methods
//
 
//
//  Private Methods
//
 
// Initializes class data members.
void COptions::Init()
{
    m_wOptions      = 0;
    m_cType         = TVOT_COMBOBOX;
    m_pmszRaw       = NULL;
    m_pszFeature    = NULL;
    m_ppszOptions   = NULL;
    m_ptRange.x     = 0;
    m_ptRange.y     = 0;
    m_dwSize        = 0;
    m_pszUnits      = NULL;
    m_hHeap         = NULL;
    m_pInfo         = NULL;
}
 
void COptions::Clear()
{
    // Free memory associated with data members.
    if(NULL != m_pmszRaw)       HeapFree(m_hHeap, 0, m_pmszRaw);
    if(NULL != m_ppszOptions)   HeapFree(m_hHeap, 0, m_ppszOptions);
    if( (NULL != m_pszUnits) && !IS_INTRESOURCE(m_pszUnits))    HeapFree(m_hHeap, 0, m_pszUnits);
 
    // Free option info.
    FreeOptionInfo();
 
    // Initialize data members.
    Init();
}
 
// Frees memory associated with Option Info array.
void COptions::FreeOptionInfo()
{
    // Validate parameters.
    if( (NULL == m_hHeap)
        ||
        (NULL == m_pInfo)
      )
    {
        return;
    }
 
    // Free strings in the array.
    for(WORD wIndex = 0; wIndex < m_wOptions; ++wIndex)
    {
        PWSTR   pszDisplay = m_pInfo[wIndex].pszDisplayName;
 
        if( (NULL != pszDisplay)
            &&
            !(IS_INTRESOURCE(pszDisplay))
          )
        {
            HeapFree(m_hHeap, 0, pszDisplay);
        }
    }
 
    // Free the array.
    HeapFree(m_hHeap, 0, m_pInfo);
    m_pInfo = NULL;
}
 
// Will do init for features that need special handling.
HRESULT COptions::GetOptionsForSpecialFeatures(CUIHelper &Helper, POEMUIOBJ poemuiobj)
{
    HRESULT hrResult    = E_NOTIMPL;
 
 
    // See if this is a special feature.
    // If it is, then call the special option init for the
    // feature.
    if(!lstrcmpA(m_pszFeature, "%CustomPageSize"))
    {
        // There is no option init for CustomPageSize.
        // The item itself must be handled specially.
        hrResult = S_OK;
    }
    else if( !lstrcmpA(m_pszFeature, "%JobTimeout")
             ||
             !lstrcmpA(m_pszFeature, "%WaitTimeout")
           )
    {
        // JobTimeout and WaitTimeout feature options are string representations
        // of an integer that represents number of seconds in the range 0 through
        // 2,147,483,647 (i.e. LONG_MAX).  However, COMPSTUI limits us to
        // WORD size, which has range of 0 to 32,767 (i.e. SHRT_MAX).
        m_cType     = TVOT_UDARROW;
        m_ptRange.x = 0;
        m_ptRange.y = SHRT_MAX;
        hrResult    = GetOptionSelectionShort(Helper, poemuiobj);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("COptions::GetOptionsForSpecialFeatures() failed to get current selection for feature %hs. (hrResult = 0x%x)\r\n"),
                m_pszFeature,
                hrResult);
 
            goto Exit;
        }
    }
    else if( !lstrcmpA(m_pszFeature, "%MaxFontSizeAsBitmap")
             ||
             !lstrcmpA(m_pszFeature, "%MinFontSizeAsOutline")
           )
    {
        // MaxFontSizeAsBitmap, and MinFontSizeAsOutline feature options
        // are string representations of an integer that represents number
        // of pixels in the range 0 through 32,767 (i.e. SHRT_MAX).
        m_cType     = TVOT_UDARROW;
        m_ptRange.x = 0;
        m_ptRange.y = SHRT_MAX;
        hrResult    = GetOptionSelectionShort(Helper, poemuiobj);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("COptions::GetOptionsForSpecialFeatures() failed to get current selection for feature %hs. (hrResult = 0x%x)\r\n"),
                m_pszFeature,
                hrResult);
 
            goto Exit;
        }
    }
    else if( !lstrcmpA(m_pszFeature, "%PSMemory") )
    {
        DWORD   dwType;
        DWORD   dwLevel;
        DWORD   dwNeeded;
 
 
        // PSMemory option is string representation of an integer
        // that represents number of seconds in the range 0
        // through 32,767 (i.e. SHRT_MAX).
        // However, the minimum is 172 KB for Level 1 and 249 KB for level 2
        m_cType     = TVOT_UDARROW;
        m_ptRange.y = SHRT_MAX;
        hrResult    = GetOptionSelectionShort(Helper, poemuiobj);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("COptions::GetOptionsForSpecialFeatures() failed to get current selection for feature %hs. (hrResult = 0x%x)\r\n"),
                m_pszFeature,
                hrResult);
 
            goto Exit;
        }
 
        // Get the global attribute for max language level.
        hrResult = Helper.GetGlobalAttribute(poemuiobj,
                                             0,
                                             "LanguageLevel",
                                             &dwType,
                                             (PBYTE) &dwLevel,
                                             sizeof(dwLevel),
                                             &dwNeeded);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("COptions::GetOptionsForSpecialFeatures() failed to get global attribute \"LanguageLevel\". (hrResult = 0x%x)\r\n"),
                hrResult);
 
            goto Exit;
        }
 
        // Set minimum range based on PS Max Language level.
        switch(dwLevel)
        {
            case 1:
                m_ptRange.x     = 172;
                break;
 
            default:
            case 2:
            case 3:
                m_ptRange.x     = 249;
                break;
        }
 
        // Get Units string.
        //GetStringResource(m_pszUnits
    }
    else if(!lstrcmpA(m_pszFeature, "%OutputPSLevel"))
    {
        hrResult = GetOptionsForOutputPSLevel(Helper, poemuiobj);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("COptions::GetOptionsForSpecialFeatures() failed to get current selection for feature %hs. (hrResult = 0x%x)\r\n"),
                m_pszFeature,
                hrResult);
 
            goto Exit;
        }
    }
 
 
Exit:
 
    return hrResult;
}
 
// Do init for PS Level options.
HRESULT COptions::GetOptionsForOutputPSLevel(CUIHelper &Helper, POEMUIOBJ poemuiobj)
{
    WORD    wCount      = 0;
    DWORD   dwLevel     = 0;
    DWORD   dwType      = 0;
    DWORD   dwNeeded    = 0;
    HRESULT hrResult    = E_NOTIMPL;
 
 
    // PS Level is integers from 1 to "LanguageLevel"
    // global atribute.
    m_cType = TVOT_COMBOBOX;
 
    // Get the global attribute for max language level.
    hrResult = Helper.GetGlobalAttribute(poemuiobj,
                                         0,
                                         "LanguageLevel",
                                         &dwType,
                                         (PBYTE) &dwLevel,
                                         sizeof(dwLevel),
                                         &dwNeeded);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("COptions::GetOptionsForOutputPSLevel() failed to get global attribute \"LanguageLevel\". (hrResult = 0x%x)\r\n"),
            hrResult);
 
        goto Exit;
    }
 
    //
    // Create Options for PS Level
    //
 
    // Set the number of options to the PS Level supported.
    m_wOptions = (WORD) dwLevel;
 
    // Allocate keyword list.
    // Allocate memory for pointer to keyword and the keyword itself, so that
    // the memory for the keyword strings will get de-allocated with the keyword list
    // on object destruction, just like regular features for which EnumOptions works.
    // User the size of a pointer (4 bytes on x86, and 8 on IA64) so that
    // it begining of the keyword strings will be DWORD or QUADWORD aligned
    // for x86 and IA64 respectively.  Keyword strings aren't required to
    // be DWORD or QUADWORD aligned, but it is more optimal.  Also, this gives
    // some additional space for the case of %OutputPSLevel keywords, which are
    // in the range of 1 through the max PostScript level supported, and only
    // require 2 CHARs (1 for the digit and one for the NULL terminator).
    m_ppszOptions = (PCSTR *) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, m_wOptions * ( sizeof(PSTR) + sizeof(PCSTR *) ) );
    if(NULL == m_ppszOptions)
    {
        ERR(ERRORTEXT("COptions::GetOptionsForOutputPSLevel() failed to allocate option keyword array for PS Level.\r\n"));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
 
    // Allocate option info array.
    m_pInfo = (POPTION_INFO) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, m_wOptions * sizeof(OPTION_INFO));
    if(NULL == m_pInfo)
    {
        ERR(ERRORTEXT("COptions::GetOptionsForOutputPSLevel() failed to allocate info array for PS Level.\r\n"));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
    // Init the option info.
    for(wCount = 0; wCount < m_wOptions; ++wCount)
    {
        // Init keyword.
        // The memory for both the keyword list and the keyword strings was allocated above.
        m_ppszOptions[wCount] = (PSTR)(m_ppszOptions + m_wOptions) + (sizeof(PSTR) * wCount);
        hrResult = StringCbPrintfA(const_cast<PSTR>(m_ppszOptions[wCount]), sizeof(PSTR), "%d", wCount + 1);
        if(FAILED(hrResult))
        {
            ERR(ERRORTEXT("COptions::GetOptionsForOutputPSLevel() failed to string representation of item %d.\r\n"),
                          wCount + 1);
 
            goto Exit;
        }
 
        // Init option display name.
        m_pInfo[wCount].pszDisplayName = (PWSTR) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, 2 * sizeof(WCHAR));
        if(NULL == m_pInfo[wCount].pszDisplayName)
        {
            ERR(ERRORTEXT("COptions::GetOptionsForOutputPSLevel() failed to allocate display string for Level %d.\r\n"),
                          wCount);
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
 
        // Init option display name.
        hrResult = StringCchPrintfW(m_pInfo[wCount].pszDisplayName, 2, TEXT("%d"), wCount + 1);
        if(FAILED(hrResult))
        {
            ERR(ERRORTEXT("COptions::GetOptionsForOutputPSLevel() failed to create display name for item %d.\r\n"),
                          wCount + 1);
 
            goto Exit;
        }
    }
 
    //
    // Get Current Selection
    //
 
    hrResult = GetOptionSelectionIndex(Helper, poemuiobj);
 
 
Exit:
 
    // Don't need to clean up memory allocation on error, since
    // all memory allocated are assigned to data members, which
    // will be cleaned up in the object destructor.
 
    return hrResult;
}
 
HRESULT COptions::GetOptionSelectionString(CUIHelper &Helper, POEMUIOBJ poemuiobj, _Outptr_result_maybenull_ PSTR *ppszSel)
{
    PSTR    pmszFeature     = NULL;
    PSTR    pmszBuf         = NULL;
    WORD    wCount          = 0;
    PCSTR  *ppszList        = NULL;
    DWORD   dwFeatureSize   = 0;
    DWORD   dwNeeded        = 0;
    DWORD   dwSize          = INITIAL_GET_OPTION_SIZE;
    HRESULT hrResult        = S_OK;
 
    *ppszSel = NULL;
 
 
    //
    // Make single feature multi-sz.
    //
 
    // Allocate single feature multi-sz buffer that's the size of the Feature name, plus two bytes for multi-sz termination
    if (FAILED(hrResult = SizeTToDWord(strlen(m_pszFeature) + 2, &dwFeatureSize)))
    {
        goto Exit;
    }
 
    pmszFeature = (PSTR) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, dwFeatureSize);
    if(NULL == pmszFeature)
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionString() failed to allocate buffer for single feature multi-sz for feature %hs.\r\n"),
                      m_pszFeature);
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
    // Just need to do a regular string copy, since the buffer is already zero filled.
    hrResult = StringCbCopyA(pmszFeature, dwFeatureSize, m_pszFeature);
    if(FAILED(hrResult))
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionString() failed to copy feature string %hs.\r\n"), m_pszFeature);
    }
 
    // Allocated initial buffer of reasonible size.
    pmszBuf = (PSTR) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, dwSize);
    if(NULL == pmszBuf)
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionString() failed to allocate buffer to get current setting for feature %hs.\r\n"),
                      m_pszFeature);
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
    // Get current option selection.
    hrResult = Helper.GetOptions(poemuiobj,
                                 0,
                                 pmszFeature,
                                 dwFeatureSize,
                                 pmszBuf,
                                 dwSize,
                                 &dwNeeded);
    if( (E_OUTOFMEMORY == hrResult) && (dwSize < dwNeeded) )
    {
        PSTR    pTemp   = NULL;
 
 
        // INVARIANT:  initial buffer not large enough.
 
        // Re-alloc buffer and try again.
        pTemp = (PSTR) HeapReAlloc(m_hHeap, HEAP_ZERO_MEMORY, pmszBuf, dwNeeded);
        if(NULL == pTemp)
        {
            ERR(ERRORTEXT("COptions::GetOptionSelectionString() failed to re-allocate buffer to get current setting for feature %hs.\r\n"),
                          m_pszFeature);
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
        pmszBuf = pTemp;
 
 
        // Try to get current option selection again.
        hrResult = Helper.GetOptions(poemuiobj,
                                     0,
                                     pmszFeature,
                                     dwFeatureSize,
                                     pmszBuf,
                                     dwNeeded,
                                     &dwNeeded);
    }
 
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionString() failed to get current setting for feature %hs. (hrResult = 0x%x)\r\n"),
            m_pszFeature,
            hrResult);
 
        goto Exit;
    }
 
    // NOTE: The return string from GetOptions() may return
    //       contain no strings and not return a HRESULT error
    //       when the feature isn't supported in the current document or
    //       printer sticky mode.
    if('\0' == pmszBuf[0])
    {
        // Feature not supported for this sticky mode.
        goto Exit;
    }
 
    // Parse the results buffer to see what the current setting is.
    hrResult = MakeStrPtrList(m_hHeap, pmszBuf, &ppszList, &wCount);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionString() failed to make string list for GetOptions() return for feature %hs. (hrResult = 0x%x)\r\n"),
            m_pszFeature,
            hrResult);
 
        goto Exit;
    }
 
    // Check that we got 2 strings back.
    if(2 != wCount)
    {
        WARNING(DLLTEXT("COptions::GetOptionSelectionString() the GetOption() return string list for \r\n\tfeature %hs is not of size 2.\r\n\tNumber of string is %d\r\n"),
                        m_pszFeature,
                        wCount);
 
        // Bail if we don't have at least 2 strings.
        if(2 > wCount)
        {
            goto Exit;
        }
    }
 
    // Return copy of just the GetOption() result.
    *ppszSel = MakeStringCopy(m_hHeap, ppszList[1]);
    if(NULL == *ppszSel)
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionString() failed to duplicate string GetOptions() return for feature %hs. (hrResult = 0x%x)\r\n"),
            m_pszFeature,
            hrResult);
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
 
Exit:
 
    // Free local buffers.
    if(NULL != pmszFeature) HeapFree(m_hHeap, 0, pmszFeature);
    if(NULL != pmszBuf)     HeapFree(m_hHeap, 0, pmszBuf);
    if(NULL != ppszList)    HeapFree(m_hHeap, 0, ppszList);
 
    return hrResult;
}
 
// Gets current Options selection for LONG value.
HRESULT COptions::GetOptionSelectionLong(CUIHelper &Helper, POEMUIOBJ poemuiobj)
{
    PSTR    pszSel      = NULL;
    HRESULT hrResult    = S_OK;
 
 
    // Get option selection string.
    hrResult = GetOptionSelectionString(Helper, poemuiobj, &pszSel);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionLong() failed to get string for GetOptions() return for feature %hs. (hrResult = 0x%x)\r\n"),
            m_pszFeature,
            hrResult);
 
        goto Exit;
    }
 
    // Convert string option to LONG and uses that as the selection.
    if(NULL != pszSel) m_Sel = atol(pszSel);
 
 
Exit:
 
    // Free local buffers.
    if(NULL != pszSel)  HeapFree(m_hHeap, 0, pszSel);
 
    return hrResult;
}
 
// Gets current Options selection for SHORT value.
HRESULT COptions::GetOptionSelectionShort(CUIHelper &Helper, POEMUIOBJ poemuiobj)
{
    PSTR    pszSel      = NULL;
    HRESULT hrResult    = S_OK;
 
 
    // Get option selection string.
    hrResult = GetOptionSelectionString(Helper, poemuiobj, &pszSel);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionLong() failed to get string for GetOptions() return for feature %hs. (hrResult = 0x%x)\r\n"),
            m_pszFeature,
            hrResult);
 
        goto Exit;
    }
 
    // Convert string option to LONG and uses that as the selection.
    if(NULL != pszSel) m_Sel = atoi(pszSel) & 0x00ffff;
 
 
Exit:
 
    // Free local buffers.
    if(NULL != pszSel)  HeapFree(m_hHeap, 0, pszSel);
 
    return hrResult;
}
 
// Gets current option selection for feature.
HRESULT COptions::GetOptionSelectionIndex(CUIHelper &Helper, POEMUIOBJ poemuiobj)
{
    PSTR    pszSel      = NULL;
    HRESULT hrResult    = S_OK;
 
 
    // Get option selection string.
    hrResult = GetOptionSelectionString(Helper, poemuiobj, &pszSel);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("COptions::GetOptionSelectionIndex() failed to get string for GetOptions() return for feature %hs. (hrResult = 0x%x)\r\n"),
            m_pszFeature,
            hrResult);
 
        goto Exit;
    }
 
    // Find the matching option for the string returned from GetOption.
    m_Sel = FindOption(pszSel, m_wOptions - 1);
 
 
Exit:
 
    // Free local buffers.
    if(NULL != pszSel)  HeapFree(m_hHeap, 0, pszSel);
 
    return hrResult;
}
 
 
 
//
//  Public Methods
//
 
// Default constructor
COptions::COptions()
{
    Init();
}
 
// Destructor
COptions::~COptions()
{
    Clear();
}
 
// Get the option list for a feature
HRESULT COptions::Acquire(_In_ HANDLE hHeap,
                          CUIHelper &Helper,
                          _In_ POEMUIOBJ poemuiobj,
                          _In_ PCSTR pszFeature)
{
    DWORD   dwNeeded    = 0;
    HRESULT hrResult    = S_OK;
 
 
    VERBOSE(DLLTEXT("COptions::Acquire(0x%p, Helper, 0x%p, %hs) entered.\r\n"),
            hHeap,
            poemuiobj,
            pszFeature ? pszFeature : "NULL");
 
    // Don't retreive the Options again if we already got them.
    if( (0 < m_wOptions)
        &&
        (NULL != m_pszFeature)
        &&
        !lstrcmpA(m_pszFeature, pszFeature)
      )
    {
        VERBOSE(DLLTEXT("COptions::Acquire() already have options for feature %hs.\r\n"), m_pszFeature);
        VERBOSE(DLLTEXT("COptions::Acquire() returning with HRESULT of S_OK\r\n"));
 
        return S_OK;
    }
 
    // Save the heap handle for use later, such as freeing memory at destruction.
    m_hHeap = hHeap;
 
    // Store Keyword string.
    m_pszFeature = pszFeature;
 
 
    //
    // Enumerate Options.
    //
 
 
    // Some features require special handling for initializing their options.
    // EnumOpionts isn't implemented for these features.
    // Return of E_NOTIMPL indicates it isn't the feature doesn't
    // need special handling.
    hrResult = GetOptionsForSpecialFeatures(Helper, poemuiobj);
    if( SUCCEEDED(hrResult)
        ||
        (!SUCCEEDED(hrResult) && (E_NOTIMPL != hrResult) )
      )
    {
        // We either dealt with the special feature or incounter an error
        // trying to deal with the special feature.
 
        goto Exit;
    }
 
    // To try to cut down on having to call EnumOptions more than once,
    // pre-allocate a buffer of reasonable size.
    m_dwSize = INITIAL_ENUM_OPTIONS_SIZE;
    m_pmszRaw = (PSTR) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, m_dwSize);
    if(NULL == m_pmszRaw)
    {
        ERR(ERRORTEXT("COptions::Acquire() alloc for options for feature %hs failed.\r\n"), m_pszFeature);
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
 
    // Try to get options list with initial buffer.
    hrResult = Helper.EnumOptions(poemuiobj, 0, m_pszFeature, m_pmszRaw, m_dwSize, &dwNeeded);
    if( (E_OUTOFMEMORY == hrResult) && (m_dwSize < dwNeeded))
    {
        PSTR    pTemp;
 
 
        // INVARIANT:  options list multi-sz wasn't large enough.
 
        // Re-allocate the buffer and try again.
        pTemp = (PSTR) HeapReAlloc(m_hHeap, HEAP_ZERO_MEMORY, m_pmszRaw, dwNeeded);
        if(NULL == pTemp)
        {
            ERR(ERRORTEXT("COptions::Acquire() re-alloc for options list for feature %hs failed.\r\n"), m_pszFeature);
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
        m_pmszRaw = pTemp;
        m_dwSize = dwNeeded;
 
        // Try again to get the options list.
        hrResult = Helper.EnumOptions(poemuiobj, 0, m_pszFeature, m_pmszRaw, m_dwSize, &dwNeeded);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("COptions::Acquire() failed to EnumOptions() for feature %hs after re-allocating buffer.\r\n"), m_pszFeature);
 
            goto Exit;
        }
    }
 
    // Make sure we got the option list.
    // Can't do anything more with out it.
    if(!SUCCEEDED(hrResult))
    {
        if(E_NOTIMPL != hrResult) ERR(ERRORTEXT("COptions::Acquire() failed to enumerate options for feature %hs. (hrResult = 0x%x)\r\n"), m_pszFeature, hrResult);
 
        goto Exit;
    }
 
    // INVARIANT:  successfully got option keyword list.
 
    // Create array of string pointers to the Option names
    // in the multi-sz we got from EnumOptions().
    hrResult = MakeStrPtrList(m_hHeap, m_pmszRaw, &m_ppszOptions, &m_wOptions);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("COptions::Acquire() failed to create pointer list to options. (hrResult = 0x%x)\r\n"), hrResult);
 
        goto Exit;
    }
 
    //
    //  Build Option Information
    //
 
    // Allocate array to hold feature info
    m_pInfo = (POPTION_INFO) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, m_wOptions * sizeof(OPTION_INFO));
    if(NULL == m_pInfo)
    {
        ERR(ERRORTEXT("COptions::Acquire() failed to alloc feature info array.\r\n"));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
    // For each option, build or get useful information, such as display name.
    for(WORD wIndex = 0; wIndex < m_wOptions; ++wIndex)
    {
        POPTION_INFO    pCurrent    = m_pInfo + wIndex;
 
 
        // Get or build a keyword mapping entry
        // that maps from keyword to usefully where to get info, such as
        // display name, icon, option type, for keywords that may not be
        // able to get info for from Helper.
        pCurrent->pMapping = FindKeywordMapping(gkmOptionMap, NUM_OPTION_MAP, m_ppszOptions[wIndex]);
 
        // Get display names for each of the Options.
        // The function implements a heuristic for detemining the display name,
        // since can't get the display name from the UI Helper for all Options.
        hrResult = DetermineOptionDisplayName(m_hHeap,
                                              Helper,
                                              poemuiobj,
                                              m_pszFeature,
                                              m_ppszOptions[wIndex],
                                              pCurrent->pMapping,
                                              &pCurrent->pszDisplayName);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("COptions::Acquire() failed to get display name for %hs of feature %hs. (hrResult = 0x%x)\r\n"),
                          m_ppszOptions[wIndex],
                          m_pszFeature,
                          hrResult);
 
            goto Exit;
        }
    }
 
    //
    // Get current option selection.
    //
 
    hrResult = GetOptionSelectionIndex(Helper, poemuiobj);
 
 
Exit:
 
    // Clean up if weren't successful.
    if(!SUCCEEDED(hrResult))
    {
        Clear();
    }
 
    VERBOSE(DLLTEXT("COptions::Acquire() returning with HRESULT of 0x%x\r\n"), hrResult);
 
    return hrResult;
}
 
// Return nth options keyword.
PCSTR COptions::GetKeyword(WORD wIndex) const
{
    // Validate parameters.
    if( (wIndex >= m_wOptions)
        ||
        (NULL == m_ppszOptions)
      )
    {
        return NULL;
    }
 
    return m_ppszOptions[wIndex];
}
 
// Return nth options display name.
PCWSTR COptions::GetName(WORD wIndex) const
{
    // Validate parameters.
    if( (wIndex >= m_wOptions)
        ||
        (NULL == m_pInfo)
      )
    {
        ERR(ERRORTEXT("COptions::GetName() invalid parameters.\r\n"));
 
        return NULL;
    }
 
    if(NULL == m_pInfo[wIndex].pszDisplayName) ERR(ERRORTEXT("COptions::GetName() returning NULL option display name.\r\n"));
 
    return m_pInfo[wIndex].pszDisplayName;
}
 
// Find option with matching keyword string.
WORD COptions::FindOption(_In_opt_ PCSTR pszOption, WORD wDefault) const
{
    BOOL    bFound  = FALSE;
    WORD    wMatch  = wDefault;
 
 
    // Validate parameters.
    if( (NULL == pszOption)
        ||
        (NULL == m_ppszOptions)
      )
    {
        return wDefault;
    }
 
    // Walk the option keyword looking for a match.
    for(WORD wIndex = 0; !bFound && (wIndex < m_wOptions); ++wIndex)
    {
        bFound = !lstrcmpA(pszOption, m_ppszOptions[wIndex]);
        if(bFound)
        {
            wMatch = wIndex;
        }
    }
 
    return wMatch;
}
 
// Initializes OptItem with options for a feature.
HRESULT COptions::InitOptItem(_In_ HANDLE hHeap, POPTITEM pOptItem)
{
    WORD    wParams     = 0;
    WORD    wOptions    = 0;
    HRESULT hrResult    = S_OK;
 
 
    // Set option selection.
    pOptItem->pSel = m_pSel;
 
    // Get count of number of options.
    // NOTE: Some feature options have no counts.
    wOptions = GetCount();
 
    // Different OPTTYPE types require different number of OPTPARAMs.
    switch(m_cType)
    {
        // For up down arrow control, the OPTPARAMs need is 2.
        case TVOT_UDARROW:
            wParams = 2;
            break;
 
        // For combobox, the OPTPARAMs needed is on per options.
        case TVOT_COMBOBOX:
            wParams = wOptions;
            break;
 
        // The default is the option count.
        default:
            WARNING(DLLTEXT("COptions::InitOptItem() OPTTYPE type %d num of OPTPARAMs not handled. Default to option count of %d.\r\n"),
                            m_cType,
                            wOptions);
            wParams = wOptions;
            break;
    }
 
    // Only do OPTTYPEs if we have non-Zero number of OPTPARAMs.
    // Every OPTTYPE has at leas one OPTPARAM.
    if(0 < wParams)
    {
        // Allocate memory for feature options.
        pOptItem->pOptType = CreateOptType(hHeap, wParams);
        if(NULL == pOptItem->pOptType)
        {
            ERR(ERRORTEXT("COptions::InitOptItem() failed to allocate OPTTYPEs for OPTITEM %hs.\r\n"),
                           m_pszFeature);
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
 
        // Set OPTTYPE.
        pOptItem->pOptType->Type = m_cType;
 
        // Different OPTTYPE types require different initialization.
        switch(m_cType)
        {
            // For up down arrow control, OPTPARAM[0] is used by the contrl.
            // pOptParam[0]->pData is the Units description string.
            // pOptParam[1].IconID is the min limit.
            // pOptParam[1].lParam is the max limit.
            case TVOT_UDARROW:
                assert(2 == wParams);
                pOptItem->pOptType->pOptParam[0].pData  = m_pszUnits;
                pOptItem->pOptType->pOptParam[1].IconID = m_ptRange.x;
                pOptItem->pOptType->pOptParam[1].lParam = m_ptRange.y;
                break;
 
            // For combobox, the pOptParam[n].pData is the display name of the option.
            case TVOT_COMBOBOX:
                for(WORD wIndex = 0; wIndex < wParams; ++wIndex)
                {
                    pOptItem->pOptType->pOptParam[wIndex].pData = const_cast<PWSTR>(GetName(wIndex));
                }
                break;
 
            default:
                ERR(ERRORTEXT("COptions::InitOptItem() OPTTYPE type %d OPTTYPE init not handled.\r\n"),
                              m_cType);
                break;
        }
    }
 
 
Exit:
 
    return hrResult;
}
 
// Refresh option selection.
HRESULT COptions::RefreshSelection(CUIHelper &Helper, POEMUIOBJ poemuiobj)
{
    HRESULT     hrResult = S_OK;
 
 
    // Method for getting option selection is based
    // on OPTTYPE type.
    switch(m_cType)
    {
 
        case TVOT_UDARROW:
            hrResult = GetOptionSelectionShort(Helper, poemuiobj);
            break;
 
        case TVOT_COMBOBOX:
            hrResult = GetOptionSelectionIndex(Helper, poemuiobj);
            break;
 
        default:
            ERR(ERRORTEXT("COptions::RefreshSelection() not handled for type %d OPTTYPE.\r\n"),
                           m_cType);
            break;
    }
 
    return hrResult;
}
 
 
 
////////////////////////////////////////////
//
//  CFeatures Methods
//
 
//
//  Private Methods
//
 
// Initializes class
void CFeatures::Init()
{
    // Initialize data members.
    m_wFeatures         = 0;
    m_wDocFeatures      = 0;
    m_wPrintFeatures    = 0;
    m_pmszRaw           = NULL;
    m_ppszKeywords      = NULL;
    m_dwSize            = 0;
    m_hHeap             = NULL;
    m_pInfo             = NULL;
}
 
// Cleans up class and re-initialize it.
void CFeatures::Clear()
{
    // Free memory associated with data members.
    if(NULL != m_pmszRaw)       HeapFree(m_hHeap, 0, m_pmszRaw);
    if(NULL != m_ppszKeywords)  HeapFree(m_hHeap, 0, m_ppszKeywords);
 
    // Free feature info
    FreeFeatureInfo();
 
    // Re-initialize
    Init();
}
 
// Free feature info
void CFeatures::FreeFeatureInfo()
{
    // Validate parameters.
    if( (NULL == m_hHeap)
        ||
        (NULL == m_pInfo)
      )
    {
        return;
    }
 
    // Free memory associated with feature info.
    for(WORD wIndex = 0; wIndex < m_wFeatures; ++wIndex)
    {
        PWSTR   pszDisplay =  m_pInfo[wIndex].pszDisplayName;
 
 
        // Free display name.
        if( (NULL != pszDisplay)
            &&
            !IS_INTRESOURCE(pszDisplay)
          )
        {
            HeapFree(m_hHeap, 0, pszDisplay);
        }
    }
 
    // Free feature info array.
    // Feature Info array allocated with new so
    // that each of the constructors for COptions
    // in fhte Feature Info array will be called.
    delete[] m_pInfo;
}
 
// Turns index for mode to modeless index, which
// is the real index to the feature.
WORD CFeatures::GetModelessIndex(WORD wIndex, DWORD dwMode) const
{
    WORD    wCount = 0;
 
 
    switch(dwMode)
    {
        // Number of features, all modes
        case 0:
            wCount = wIndex;
            break;
 
        // Find the nth feature that matches the mode
        case OEMCUIP_DOCPROP:
        case OEMCUIP_PRNPROP:
            // Walk the feature list looking for nth feature
            // with matching mode.
            for(wCount = 0; wCount < m_wFeatures; ++wCount)
            {
                // Count down to the feature we want.
                // Only count down for matching modes.
                if(dwMode == m_pInfo[wCount].dwMode)
                {
                    if(0 == wIndex)
                    {
                        break;
                    }
                    else
                    {
                        --wIndex;
                    }
                }
            }
            break;
    }
 
    return wCount;
}
 
 
//
//  Public Methods
//
 
// Default constructor
CFeatures::CFeatures()
{
    Init();
}
 
// Destructor
CFeatures::~CFeatures()
{
    // Clean up class.
    Clear();
}
 
// Gets Core Driver Features, if not already retrieved.
HRESULT CFeatures::Acquire(_In_ HANDLE hHeap,
                           CUIHelper &Helper,
                           _In_ POEMUIOBJ poemuiobj
                          )
{
    WORD    wIndex      = 0;
    DWORD   dwNeeded    = 0;
    HRESULT hrResult    = S_OK;
 
 
    VERBOSE(DLLTEXT("CFeatures::Acquire(0x%p, Helper, 0x%p) entered.\r\n"),
            hHeap,
            poemuiobj);
 
    // Don't retreive the Features again if we already got them.
    if(0 < m_wFeatures)
    {
        VERBOSE(DLLTEXT("CFeatures::Acquire() features already enumerated.\r\n"));
        VERBOSE(DLLTEXT("CFeatures::Acquire() returning S_OK.\r\n"));
 
        return S_OK;
    }
 
    // Save the heap handle for use later, such as freeing memory at destruction.
    m_hHeap = hHeap;
 
    //
    // Enumerate features.
    //
 
    // To try to cut down on having to call EnumFeatures more than once,
    // pre-allocate a buffer of reasonable size.
    m_dwSize = INITIAL_ENUM_FEATURES_SIZE;
    m_pmszRaw = (PSTR) HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, m_dwSize);
    if(NULL == m_pmszRaw)
    {
        ERR(ERRORTEXT("CFeatures::Acquire() alloc for feature list failed.\r\n"));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
 
    // Try to get feature list with initial buffer.
    hrResult = Helper.EnumFeatures(poemuiobj, 0, m_pmszRaw, m_dwSize, &dwNeeded);
    if( (E_OUTOFMEMORY == hrResult) && (m_dwSize < dwNeeded))
    {
        PSTR    pTemp;
 
 
        // INVARIANT:  feature list multi-sz wasn't large enough.
 
 
        // Re-allocate the buffer and try again.
        pTemp = (PSTR) HeapReAlloc(m_hHeap, HEAP_ZERO_MEMORY, m_pmszRaw, dwNeeded);
        if(NULL == pTemp)
        {
            ERR(ERRORTEXT("CFeatures::Acquire() re-alloc for feature list failed.\r\n"));
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
        m_pmszRaw = pTemp;
        m_dwSize = dwNeeded;
 
        // Try again to get the feature list.
        hrResult = Helper.EnumFeatures(poemuiobj, 0, m_pmszRaw, m_dwSize, &dwNeeded);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("CFeatures::Acquire() failed to EnumFeatures() after re-allocating buffer.\r\n"));
 
            goto Exit;
        }
    }
 
    // Make sure we got the feature list.
    // Can't do anything more with out it.
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("CFeatures::Acquire() failed to enumerate features. (hrResult = 0x%x)\r\n"), hrResult);
 
        goto Exit;
    }
 
    // INVARIANT:  successfully got feature keyword list.
 
    // Create array of string pointers to the feature keywords
    // in the multi-sz we got from EnumFreatures().
    hrResult = MakeStrPtrList(m_hHeap, m_pmszRaw, &m_ppszKeywords, &m_wFeatures);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("CFeatures::Acquire() failed to create pointer list to keywords. (hrResult = 0x%x)\r\n"), hrResult);
 
        goto Exit;
    }
 
 
    //
    //  Build Feature Information
    //
 
 
    // Allocate array to hold feature info
    // Use new for allocation so class
    // constructors/destructors get called.
    m_pInfo = new FEATURE_INFO[m_wFeatures];
    if(NULL == m_pInfo)
    {
        ERR(ERRORTEXT("CFeatures::Acquire() failed to alloc feature info array.\r\n"));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
    // For each feature, build/get feature info....
    for(wIndex = 0; wIndex < m_wFeatures; ++wIndex)
    {
        PFEATURE_INFO   pCurrent    = m_pInfo + wIndex;
 
 
        // Get or build a keyword mapping entry
        // that maps from keyword to usefully where to get info, such as
        // display name, icon, option type, for keywords that may not be
        // able to get info for from Helper.
        pCurrent->pMapping = FindKeywordMapping(gkmFeatureMap, NUM_FEATURE_MAP, m_ppszKeywords[wIndex]);
 
        // Get display names for each of the featurs.
        // The function implements a heuristic for detemining the display name,
        // since can't get the display name from the UI Helper for all features.
        hrResult = DetermineFeatureDisplayName(m_hHeap,
                                               Helper,
                                               poemuiobj,
                                               m_ppszKeywords[wIndex],
                                               pCurrent->pMapping,
                                               &pCurrent->pszDisplayName);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("CFeatures::Acquire() failed to get display name for feature %hs. (hrResult = 0x%x)\r\n"),
                          m_ppszKeywords[wIndex],
                          hrResult);
 
            goto Exit;
        }
 
        // Get options for each feature.
        // NOTE: some features don't have options; the HRESULT will be E_NOTIMPL for these.
        hrResult = pCurrent->Options.Acquire(hHeap,
                                             Helper,
                                             poemuiobj,
                                             m_ppszKeywords[wIndex]);
        if(!SUCCEEDED(hrResult) && (E_NOTIMPL != hrResult))
        {
            ERR(ERRORTEXT("CFeatures::Acquire() failed to get options for feature %hs. (hrResult = 0x%x)\r\n"),
                          m_ppszKeywords[wIndex],
                          hrResult);
 
            goto Exit;
        }
 
        // Determine if feature is Printer or Document sticky.
        hrResult = DetermineStickiness(Helper,
                                       poemuiobj,
                                       m_ppszKeywords[wIndex],
                                       pCurrent->pMapping,
                                       &pCurrent->dwMode);
        // Don't propagate error if failure from unhandled driver feature.
        if( !SUCCEEDED(hrResult)
            &&
            !IS_DRIVER_FEATURE(m_ppszKeywords[wIndex])
          )
        {
            ERR(ERRORTEXT("CFeatures::Acquire() failed to determine stickiness for feature %hs. (hrResult = 0x%x)\r\n"),
                          m_ppszKeywords[wIndex],
                          hrResult);
 
            goto Exit;
        }
 
        // Keep track of mode counts.
        switch(pCurrent->dwMode)
        {
            case OEMCUIP_DOCPROP:
                ++m_wDocFeatures;
                break;
 
            case OEMCUIP_PRNPROP:
                ++m_wPrintFeatures;
                break;
 
            default:
                ERR(ERRORTEXT("CFeatures::Acquire() unknown stickiness for feature %hs.\r\n"),
                              m_ppszKeywords[wIndex]);
                break;
        }
 
    }
 
    // INVARIANT:  successfully build feature list.
 
    // Make sure that we always return success if we reach this point.
    hrResult = S_OK;
 
 
Exit:
 
    // Clean up if weren't successful.
    if(!SUCCEEDED(hrResult))
    {
        Clear();
    }
 
 
    VERBOSE(DLLTEXT("CFeatures::Acquire() returning HRESULT of 0x%x.\r\n"), hrResult);
 
    return hrResult;
}
 
// Returns number of features contained in class instance.
WORD CFeatures::GetCount(DWORD dwMode) const
{
    WORD    wCount = 0;
 
 
    switch(dwMode)
    {
        // Number of features, all modes
        case 0:
            wCount = m_wFeatures;
            break;
 
        case OEMCUIP_DOCPROP:
            wCount = m_wDocFeatures;
            break;
 
        case OEMCUIP_PRNPROP:
            wCount = m_wPrintFeatures;
            break;
 
    }
 
    VERBOSE(DLLTEXT("CFeatures::GetCount() returning %d\r\n"), wCount);
 
    return wCount;
}
 
// Returns nth feature's keyword
PCSTR CFeatures::GetKeyword(WORD wIndex, DWORD dwMode) const
{
    // Validate parameters.
    if( (wIndex >= GetCount(dwMode))
        ||
        (NULL == m_ppszKeywords)
      )
    {
        return NULL;
    }
 
    // Get internal index.
    wIndex = GetModelessIndex(wIndex, dwMode);
 
    // Return keyword
    return m_ppszKeywords[wIndex];
}
 
// Return nth feature's Display Name.
PCWSTR CFeatures::GetName(WORD wIndex, DWORD dwMode) const
{
    // Validate parameters.
    if( (wIndex >= GetCount(dwMode))
        ||
        (NULL == m_pInfo)
      )
    {
        return NULL;
    }
 
    // Get internal index.
    wIndex = GetModelessIndex(wIndex, dwMode);
 
    // Return display name.
    return m_pInfo[wIndex].pszDisplayName;
}
 
// Returns pointer to option class for nth feature.
COptions* CFeatures::GetOptions(WORD wIndex, DWORD dwMode) const
{
    // Validate parameters.
    if( (wIndex >= GetCount(dwMode))
        ||
        (NULL == m_pInfo)
      )
    {
        return NULL;
    }
 
    // Get internal index.
    wIndex = GetModelessIndex(wIndex, dwMode);
 
    // Return options pointer.
    return &m_pInfo[wIndex].Options;
}
 
// Formats OPTITEM for specied feature.
HRESULT CFeatures::InitOptItem(_In_ HANDLE hHeap, POPTITEM pOptItem, WORD wIndex, DWORD dwMode)
{
    COptions            *pOptions   = NULL;
    HRESULT             hrResult    = S_OK;
    PFEATUREOPTITEMDATA pData       = NULL;
 
 
    // Validate parameters.
    if( (wIndex >= GetCount(dwMode))
        ||
        (NULL == m_pInfo)
        ||
        (NULL == pOptItem)
      )
    {
        return E_INVALIDARG;
    }
 
    // Map mode index to internal index.
    wIndex = GetModelessIndex(wIndex, dwMode);
 
    // Get name of feature.
    pOptItem->pName = const_cast<PWSTR>(GetName(wIndex));
 
    // Add feature OPTITEM data to OPTITEM to facilitate saving
    // selection changes.
    pData = (PFEATUREOPTITEMDATA) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(FEATUREOPTITEMDATA));
    if(NULL == pData)
    {
        ERR(ERRORTEXT("CFeatures::InitOptItem() failed to allocated memory for feature OPTITEM data."));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
    pData->dwSize               = sizeof(FEATUREOPTITEMDATA);
    pData->dwTag                = FEATURE_OPTITEM_TAG;
    pData->pszFeatureKeyword    = GetKeyword(wIndex);
    pData->pOptions             = GetOptions(wIndex);
    pOptItem->UserData          = (ULONG_PTR) pData;
 
 
    // Get pointer to options for this feature.
    // NOTE: some features do not have option list for various reasons.
    pOptions = GetOptions(wIndex);
    if(NULL != pOptions)
    {
        // Initialize COption parts of the OPTITEM
        hrResult = pOptions->InitOptItem(hHeap, pOptItem);
    }
 
Exit:
 
    Dump(pOptItem);
 
    return hrResult;
}
 
//////////////////////////////////////////////////
//
//  Regular functions not part of class
//
//////////////////////////////////////////////////
 
 
// Maps feature keywords to display names for the features.
HRESULT DetermineFeatureDisplayName(_In_ HANDLE hHeap,
                                    CUIHelper &Helper,
                                    POEMUIOBJ poemuiobj,
                                    _In_ PCSTR pszKeyword,
                                    const PKEYWORDMAP pMapping,
                                    _Outptr_result_maybenull_ PWSTR *ppszDisplayName)
{
    DWORD   dwDataType  = 0;
    DWORD   dwSize      = INITIAL_FEATURE_DISPLAY_NAME_SIZE;
    DWORD   dwNeeded    = 0;
    HRESULT hrResult    = S_OK;
 
 
    // Validate parameters.
    if( (NULL == hHeap)
        ||
        (NULL == pszKeyword)
        ||
        (NULL == ppszDisplayName)
      )
    {
        ERR(ERRORTEXT("DetermineFeatureDisplayName() invalid arguement.\r\n"));
 
        hrResult = E_INVALIDARG;
        goto Exit;
    }
 
    //
    // Call the Helper function.
    //
 
    // Helper will return Display Names for PPD Features, but
    // not for Driver Synthisized features (i.e. features prefixed with %).
    // Do it for Driver Synthisized features, just in case the helper
    // interface changes to support it.
 
    // Pre-allocate a buffer of reasonable size to try
    // to one have to call GetFeatureAttribute() once.
    *ppszDisplayName = (PWSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwSize);
    if(NULL == *ppszDisplayName)
    {
        ERR(ERRORTEXT("DetermineFeatureDisplayName() alloc for feature display name failed.\r\n"));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
    // Try to get diplay name for feature from Helper.
    hrResult = Helper.GetFeatureAttribute(poemuiobj,
                                          0,
                                          pszKeyword,
                                          "DisplayName",
                                          &dwDataType,
                                          (PBYTE) *ppszDisplayName,
                                          dwSize,
                                          &dwNeeded);
    if( (E_OUTOFMEMORY == hrResult) && (dwSize < dwNeeded))
    {
        PWSTR   pTemp;
 
 
        // INVARIANT: initial buffer wasn't large enough.
 
        // Re-alloc buffer and try again.
        pTemp = (PWSTR) HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, *ppszDisplayName, dwNeeded);
        if(NULL == pTemp)
        {
            ERR(ERRORTEXT("DetermineFeatureDisplayName() re-alloc for feature display name failed.\r\n"));
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
        *ppszDisplayName = pTemp;
 
        // Try to get the display name from Helper, again.
        hrResult = Helper.GetFeatureAttribute(poemuiobj,
                                              0,
                                              pszKeyword,
                                              "DisplayName",
                                              &dwDataType,
                                              (PBYTE) *ppszDisplayName,
                                              dwNeeded,
                                              &dwNeeded);
    }
 
    if(SUCCEEDED(hrResult))
    {
        // INVARIANT:  Successfully got display name from Helper for feature.
        //             Don't need to do anything more.
 
        // Check the data type, it should be kADT_UNICODE.
        if(kADT_UNICODE != dwDataType) WARNING(DLLTEXT("DetermineFeatureDisplayName() feature attribute type not kADT_UNICODE. (dwDataType = %d)\r\n"), dwDataType);
 
        goto Exit;
    }
 
    // INVARIANT:  Did not get the display name from the Helper.
 
    // Free memory allocated for call to Helper.
    if(NULL != *ppszDisplayName)
    {
        HeapFree(hHeap, 0, *ppszDisplayName);
        *ppszDisplayName = NULL;
    }
 
    // Try alternative methods for getting the display name other
    // than from the Helper function.
    // If we have a mapping entry, then try to get resource string
    // for the display name.
    // Otherwise, covert the keyword to UNICODE and use that.
    if(NULL != pMapping)
    {
        //
        // Try mapping the keyword to resource string.
        //
 
        hrResult = GetDisplayNameFromMapping(hHeap, pMapping, ppszDisplayName);
    }
    else
    {
        //
        // Convert the keyword to UNICODE and use that.
        //
 
        // Convert ANSI keyword to Unicode string for display name.
        // Need to remove the % for Driver Synthisized features.
        // For debug version, add marker that shows that the display name was faked.
        PCSTR   pConvert = IS_DRIVER_FEATURE(pszKeyword) ? pszKeyword + 1 : pszKeyword;
    #if DBG
        CHAR    szTemp[256];
        if(FAILED(StringCbPrintfA(szTemp, sizeof(szTemp), "%s (Keyword)", pConvert)))
        {
            ERR(ERRORTEXT("DetermineFeatureDisplayName() StringCbPrintfA() called failed.\r\n"));
        }
        pConvert = szTemp;
    #endif
        *ppszDisplayName = MakeUnicodeString(hHeap, pConvert);
        if(NULL == *ppszDisplayName)
        {
            ERR(ERRORTEXT("DetermineFeatureDisplayName() alloc for feature display name failed.\r\n"));
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
 
        // Return success even though we faked a display name.
        hrResult = S_OK;
    }
 
 
Exit:
 
    // If failed, then return no string.
    if(!SUCCEEDED(hrResult))
    {
        if(ppszDisplayName && NULL != *ppszDisplayName)
        {
            HeapFree(hHeap, 0, *ppszDisplayName);
            *ppszDisplayName = NULL;
        }
    }
 
    return hrResult;
}
 
// Maps option keywords to display names for the option.
HRESULT DetermineOptionDisplayName(_In_ HANDLE hHeap,
                                   CUIHelper &Helper,
                                   POEMUIOBJ poemuiobj,
                                   _In_ PCSTR pszFeature,
                                   _In_ PCSTR pszOption,
                                   const PKEYWORDMAP pMapping,
                                   _Outptr_result_maybenull_ PWSTR *ppszDisplayName)
{
    DWORD   dwDataType  = 0;
    DWORD   dwSize      = INITIAL_OPTION_DISPLAY_NAME_SIZE;
    DWORD   dwNeeded    = 0;
    HRESULT hrResult    = S_OK;
 
 
    // Validate parameters.
    if( (NULL == hHeap)
        ||
        (NULL == pszFeature)
        ||
        (NULL == pszOption)
        ||
        (NULL == ppszDisplayName)
      )
    {
        ERR(ERRORTEXT("DetermineOptionDisplayName() invalid arguement.\r\n"));
 
        hrResult = E_INVALIDARG;
        goto Exit;
    }
 
    //
    // Call the Helper function.
    //
 
    // Helper will return Display Names for PPD Feature Options, but
    // not for Driver Synthisized features options (i.e. features prefixed with %).
    // Do it for all options, just in case the helper interface changes to support it.
 
    // Pre-allocate a buffer of reasonable size to try
    // to one have to call GetOptionAttribute() once.
    *ppszDisplayName = (PWSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwSize);
    if(NULL == *ppszDisplayName)
    {
        ERR(ERRORTEXT("DetermineOptionDisplayName() alloc for feature display name failed.\r\n"));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
    // Try to get diplay name for feature from Helper.
    hrResult = Helper.GetOptionAttribute(poemuiobj,
                                         0,
                                         pszFeature,
                                         pszOption,
                                         "DisplayName",
                                         &dwDataType,
                                         (PBYTE) *ppszDisplayName,
                                         dwSize,
                                         &dwNeeded);
    if( (E_OUTOFMEMORY == hrResult) && (dwSize < dwNeeded))
    {
        PWSTR   pTemp;
 
 
        // INVARIANT: initial buffer wasn't large enough.
 
        // Re-alloc buffer and try again.
        pTemp = (PWSTR) HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, *ppszDisplayName, dwNeeded);
        if(NULL == pTemp)
        {
            ERR(ERRORTEXT("GetOptionAttribute() re-alloc for feature display name failed.\r\n"));
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
        *ppszDisplayName = pTemp;
 
        // Try to get the display name from Helper, again.
        hrResult = Helper.GetOptionAttribute(poemuiobj,
                                             0,
                                             pszFeature,
                                             pszOption,
                                             "DisplayName",
                                             &dwDataType,
                                             (PBYTE) *ppszDisplayName,
                                             dwNeeded,
                                             &dwNeeded);
    }
 
    if(SUCCEEDED(hrResult))
    {
        // INVARIANT:  Successfully got display name from Helper for feature.
        //             Don't need to do anything more.
 
        // Check the data type, it should be kADT_UNICODE.
        if(kADT_UNICODE != dwDataType) WARNING(DLLTEXT("DetermineOptionDisplayName() feature attribute type not kADT_UNICODE. (dwDataType = %d)\r\n"), dwDataType);
 
        goto Exit;
    }
 
    // INVARIANT:  Did not get the display name from the Helper.
 
    // Free memory allocated for call to Helper.
    if(NULL != *ppszDisplayName)
    {
        HeapFree(hHeap, 0, *ppszDisplayName);
        *ppszDisplayName = NULL;
    }
 
    // Try alternative methods for getting the display name other
    // than from the Helper function.
    // If we have a mapping entry, then try to get resource string
    // for the display name.
    // Otherwise, covert the keyword to UNICODE and use that.
    if(NULL != pMapping)
    {
        //
        // Try mapping the keyword to resource string.
        //
 
        hrResult = GetDisplayNameFromMapping(hHeap, pMapping, ppszDisplayName);
    }
    else
    {
        //
        // Convert the keyword to UNICODE and use that.
        //
 
        // Convert ANSI keyword to Unicode string for display name.
        // For debug version, add marker that shows that the display name was faked.
        PCSTR   pConvert    = pszOption;
    #if DBG
        CHAR    szTemp[256];
        if(FAILED(StringCbPrintfA(szTemp, sizeof(szTemp), "%s (Keyword)", pConvert)))
        {
            ERR(ERRORTEXT("DetermineOptionDisplayName() StringCbPrintfA() called failed.\r\n"));
        }
        pConvert = szTemp;
    #endif
        *ppszDisplayName = MakeUnicodeString(hHeap, pConvert);
        if(NULL == *ppszDisplayName)
        {
            ERR(ERRORTEXT("DetermineOptionDisplayName() alloc for feature display name failed.\r\n"));
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
 
        // Return success even though we faked a display name.
        hrResult = S_OK;
    }
 
 
Exit:
 
    // If failed, then return no string.
    if(!SUCCEEDED(hrResult))
    {
        if(ppszDisplayName && NULL != *ppszDisplayName)
        {
            HeapFree(hHeap, 0, *ppszDisplayName);
            *ppszDisplayName = NULL;
        }
    }
 
    return hrResult;
}
 
// Determines sticky mode for the feature.
HRESULT DetermineStickiness(CUIHelper &Helper,
                            POEMUIOBJ poemuiobj,
                            PCSTR pszKeyword,
                            const PKEYWORDMAP pMapping,
                            PDWORD pdwMode)
{
    CHAR    szGroupType[32]     = {0};
    DWORD   dwType              = 0;
    DWORD   dwNeeded            = 0;
    HRESULT hrResult            = S_OK;
 
 
    // Use mapping to see what stickiness of the feature is.
    if(NULL != pMapping)
    {
        *pdwMode = pMapping->dwMode;
        goto Exit;
    }
 
    // By default make feature Document sticky, if we don't have mapping.
    *pdwMode = OEMCUIP_DOCPROP;
 
    // Try to use Helper to determine stickiness.
    hrResult = Helper.GetFeatureAttribute(poemuiobj,
                                          0,
                                          pszKeyword,
                                          "OpenGroupType",
                                          &dwType,
                                          (PBYTE) szGroupType,
                                          sizeof(szGroupType),
                                          &dwNeeded);
    if(SUCCEEDED(hrResult))
    {
        // INVARIANT:  found out if feature is an installable option.
        //             Installable options are the only PPD features
        //             that are Printer sticky.
 
        if(!lstrcmpA(szGroupType, "InstallableOptions"))
        {
            *pdwMode = OEMCUIP_PRNPROP;
        }
        goto Exit;
    }
 
 
Exit:
 
    return hrResult;
}
 
// Find the mapping entry from the keyword.
PKEYWORDMAP FindKeywordMapping(PKEYWORDMAP pKeywordMap, WORD wMapSize, PCSTR pszKeyword)
{
    BOOL        bFound      = FALSE;
    PKEYWORDMAP pMapping    = NULL;
 
 
    // Walk mapping array for matching keyword.
    for(WORD wIndex = 0; !bFound && (wIndex < wMapSize); ++wIndex)
    {
        bFound = !lstrcmpA(pszKeyword, pKeywordMap[wIndex].pszKeyword);
        if(bFound)
        {
            pMapping = pKeywordMap + wIndex;
        }
    }
 
    return pMapping;
}
 
// Get display name from mapping entry.
HRESULT GetDisplayNameFromMapping(_In_ HANDLE hHeap, PKEYWORDMAP pMapping, _Outptr_result_maybenull_ PWSTR *ppszDisplayName)
{
    HMODULE hModule     = NULL;
    HRESULT hrResult    = S_OK;
 
 
    // Validate parameters.
    if( (NULL == hHeap)
        ||
        (NULL == pMapping)
        ||
        (NULL == ppszDisplayName)
      )
    {
        hrResult = E_INVALIDARG;
        goto Exit;
    }
 
    // Check for simple case of returning INT resource.
    if( (NULL == pMapping->pwszModule)
        ||
        IS_MAPPING_INT_RESOURCE(pMapping)
      )
    {
        // Just need to do MAKEINTRESOURCE on the resource ID and return.
        *ppszDisplayName = MAKEINTRESOURCE(pMapping->uDisplayNameID);
        goto Exit;
    }
 
    // We only need to get the module if we aren't loading the resource from
    // this module (i.e. if module name isn't NULL).
    // Also, as an optimization, we assume that the module has already been loaded,
    // since the only cases currently are this module, driver ui, and Compstui.dll.
    hModule = GetModuleHandle(pMapping->pwszModule);
    if(NULL == hModule)
    {
        DWORD   dwError = GetLastError();
 
 
        ERR(ERRORTEXT("GetDisplayNameFromMapping() for failed to load module %s. (Error %d)\r\n"),
                      pMapping->pwszModule,
                      dwError);
 
        hrResult = HRESULT_FROM_WIN32(dwError);
        goto Exit;
    }
 
    // INVARIANT:  have handle to module to load resource from or the
    //             resource is being loaded from this module.
 
    // Get the string resouce.
    hrResult = GetStringResource(hHeap, hModule, pMapping->uDisplayNameID, ppszDisplayName);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("GetDisplayNameFromMapping() failed to load string. (hrResult = 0x%x)\r\n"),
                      hrResult);
        goto Exit;
    }
 
 
Exit:
 
    return hrResult;
}
 
// Test if an OPTITEM is an OPTITEM for a feature.
// Macro for testing if OPTITEM is feature OPTITEM
BOOL IsFeatureOptitem(POPTITEM pOptItem)
{
    BOOL                bRet    = FALSE;
    PFEATUREOPTITEMDATA pData   = NULL;
 
 
    // Make sure pointers are NULL.
    if( (NULL == pOptItem)
        ||
        (NULL == pOptItem->UserData)
       )
    {
        // INVARIANT:  can't be feature OPTITEM, since one of
        //             the necessary pointer are NULL.
 
        return FALSE;
    }
 
    // For convienience, assign to pointer to feature OPTITEM data.
    pData = (PFEATUREOPTITEMDATA)(pOptItem->UserData);
 
    // Check size and tag.
    bRet = (sizeof(FEATUREOPTITEMDATA) == pData->dwSize)
           &&
           (FEATURE_OPTITEM_TAG == pData->dwTag);
 
    return bRet;
}
 
 
 
// Walks array of OPTITEMs saving each feature OPTITEM
// that has changed.
HRESULT SaveFeatureOptItems(_In_ HANDLE hHeap,
                            CUIHelper *pHelper,
                            POEMUIOBJ poemuiobj,
                            HWND hWnd,
                            POPTITEM pOptItem,
                            WORD wItems)
{
    PSTR        pmszPairs           = NULL;
    WORD        wPairs              = 0;
    WORD        wReasons            = 0;
    DWORD       dwSize              = 0;
    DWORD       dwResult            = 0;
    PCSTR       *ppszReasons        = NULL;
    PWSTR       pszConfilictFeature = NULL;
    PWSTR       pszConfilictOption  = NULL;
    PWSTR       pszCaption          = NULL;
    PWSTR       pszFormat           = NULL;
    PWSTR       pszMessage          = NULL;
    HRESULT     hrResult            = S_OK;
 
 
    // Validate parameters
    if( (NULL == hHeap)
        ||
        (NULL == pHelper)
        ||
        (NULL == pOptItem)
      )
    {
        ERR(ERRORTEXT("SaveFeatureOptItems() called with invalid parameters.\r\n"));
 
        hrResult = E_INVALIDARG;
        goto Exit;
    }
 
    // Get feature option pairs to save.
    hrResult = GetChangedFeatureOptions(hHeap, pOptItem, wItems, &pmszPairs, &wPairs, &dwSize);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("SaveFeatureOptItems() failed to get changed feature option pairs. (hrResult = 0x%x)\r\n"),
                       hrResult);
 
        goto Exit;
    }
 
    // Don't need to do anything more if no feature options changed.
    if(0 == wPairs || !pmszPairs)
    {
        VERBOSE(DLLTEXT("SaveFeatureOptItems() no feature options that need to be set.\r\n"));
 
        goto Exit;
    }
 
    // Set the change feature options.
    // For the first SetOptions() call, don't have the
    // core driver UI resolve conflicts, so we can
    // prompt user for automatic resolution or let
    // them do the conflict resolving.
    hrResult = pHelper->SetOptions(poemuiobj,
                                   SETOPTIONS_FLAG_KEEP_CONFLICT,
                                   pmszPairs,
                                   dwSize,
                                   &dwResult);
    if(!SUCCEEDED(hrResult))
    {
        ERR(ERRORTEXT("SaveFeatureOptItems() call to SetOptions() failed. (hrResult = 0x%x, dwResult = %d\r\n"),
                       hrResult,
                       dwResult);
 
        goto Exit;
    }
 
    // Check to see if we were able to save changed feature options,
    // or if there is a conflict that needs resolution.
    if(SETOPTIONS_RESULT_CONFLICT_REMAINED == dwResult)
    {
        int         nRet;
        DWORD       dwRet;
        CONFLICT    Conflict;
        PKEYWORDMAP pMapping    = NULL;
 
 
        // INVARIANT:  constraint conflict, options weren't saved.
 
        // Get list of all features that have conflict.
        hrResult = GetFirstConflictingFeature(hHeap,
                                              pHelper,
                                              poemuiobj,
                                              pOptItem,
                                              wItems,
                                              &Conflict);
        if(!SUCCEEDED(hrResult))
        {
            goto Exit;
        }
 
        // Create string pointer list in to multi-sz of first conflict.
        hrResult = MakeStrPtrList(hHeap, Conflict.pmszReasons, &ppszReasons, &wReasons);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("SaveFeatureOptItems() failed to make string list for conflict reasons. (hrResult = 0x%x)\r\n"),
                           hrResult);
 
            goto Exit;
        }
 
        // Ensure that we have at least 2 reasons. Later code assumes that we do.
        if (wReasons < 2)
        {
            hrResult = E_UNEXPECTED;
 
            goto Exit;
        }
 
        //
        // Get display versions of feature/option conflict reason.
        //
 
        // Get or build a keyword mapping entry
        // that maps from keyword to usefully where to get info, such as
        // display name, icon, option type, for keywords that may not be
        // able to get info for from Helper.
        pMapping = FindKeywordMapping(gkmFeatureMap, NUM_FEATURE_MAP, ppszReasons[0]);
 
        // Get display names for each of the featurs.
        // The function implements a heuristic for detemining the display name,
        // since can't get the display name from the UI Helper for all features.
        hrResult = DetermineFeatureDisplayName(hHeap,
                                               *pHelper,
                                               poemuiobj,
                                               ppszReasons[0],
                                               pMapping,
                                               &pszConfilictFeature);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("SaveFeatureOptItems failed to get display name for feature %hs. (hrResult = 0x%x)\r\n"),
                          ppszReasons[0],
                          hrResult);
 
            goto Exit;
        }
 
        // Get or build a keyword mapping entry
        // that maps from keyword to usefully where to get info, such as
        // display name, icon, option type, for keywords that may not be
        // able to get info for from Helper.
        pMapping = FindKeywordMapping(gkmOptionMap, NUM_OPTION_MAP, ppszReasons[1]);
 
        // Get option display name.
        hrResult = DetermineOptionDisplayName(hHeap,
                                              *pHelper,
                                              poemuiobj,
                                              ppszReasons[0],
                                              ppszReasons[1],
                                              pMapping,
                                              &pszConfilictOption);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("SaveFeatureOptItems() failed to get display name for %hs of feature %hs. (hrResult = 0x%x)\r\n"),
                          ppszReasons[1],
                          ppszReasons[0],
                          hrResult);
 
            goto Exit;
        }
 
        //
        // Prompt user about how to resolve conflict.
        //
 
        // Get caption name.
        hrResult = GetStringResource(hHeap, ghInstance, IDS_NAME, &pszCaption);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("SaveFeatureOptItems() failed to get caption name. (hrResult = 0x%x)\r\n"),
                          hrResult);
            goto Exit;
        }
 
        // Get message body format string.
        hrResult = GetStringResource(hHeap, ghInstance, IDS_CONSTRAINT_CONFLICT, &pszFormat);
        if(!SUCCEEDED(hrResult))
        {
            ERR(ERRORTEXT("SaveFeatureOptItems() failed to get constraint conflict format. (hrResult = 0x%x)\r\n"),
                          hrResult);
            goto Exit;
        }
 
 
        // Get messsage body.
        PVOID   paArgs[4] = {pszConfilictFeature,
                             pszConfilictOption,
                             const_cast<PWSTR>(Conflict.pszFeature),
                             Conflict.pszOption
                            };
        dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
                              pszFormat,
                              0,
                              0,
                              (PWSTR) &pszMessage,
                              0,
                              (va_list *) paArgs);
        if(0 == dwRet)
        {
            DWORD   dwError = GetLastError();
 
            ERR(ERRORTEXT("SaveFeatureOptItems() failed to FormatMessage() for constraint conflict of feature %hs option %hs. (Last Error %d)\r\n"),
                          Conflict.pszFeatureKeyword,
                          Conflict.pszOptionKeyword,
                          dwError);
 
            hrResult = HRESULT_FROM_WIN32(dwError);
            goto Exit;
        }
 
        // Display simple message box with prompt.
        nRet = MessageBox(hWnd, pszMessage, pszCaption, MB_YESNO | MB_ICONWARNING);
 
        // Check to see how user wants to resolve conflict.
        if(IDYES == nRet)
        {
            // Let core driver resolve conflict resolution.
            hrResult = pHelper->SetOptions(poemuiobj,
                                           SETOPTIONS_FLAG_RESOLVE_CONFLICT,
                                           pmszPairs,
                                           dwSize,
                                           &dwResult);
 
            // Conflict resolution requires refreshing current option
            // selection for each feature, since selection may have
            // changed because of conflict resolution.
            RefreshOptItemSelection(pHelper, poemuiobj, pOptItem, wItems);
        }
 
        // Return failure if there are still conflictts.
        if(SETOPTIONS_RESULT_CONFLICT_REMAINED == dwResult)
        {
            hrResult = E_FAIL;
        }
    }
 
 
Exit:
 
    // Clean up...
 
    // cleanup heap allocs.
    if(NULL != pmszPairs)       HeapFree(hHeap, 0, pmszPairs);
    if(NULL != ppszReasons)         HeapFree(hHeap, 0, ppszReasons);
    if(NULL != pszConfilictFeature) HeapFree(hHeap, 0, pszConfilictFeature);
    if(NULL != pszConfilictOption)  HeapFree(hHeap, 0, pszConfilictOption);
    if(NULL != pszCaption)          HeapFree(hHeap, 0, pszCaption);
    if(NULL != pszFormat)           HeapFree(hHeap, 0, pszFormat);
    if(NULL != pszMessage)          LocalFree(pszMessage);
 
    return hrResult;
}
 
// Allocates buffer, if needed, and calls IPrintCoreUI2::WhyConsrained
// to get reason for constraint.
HRESULT GetWhyConstrained(_In_ HANDLE hHeap,
                          CUIHelper *pHelper,
                          POEMUIOBJ poemuiobj,
                          _In_ PCSTR     pszFeature,
                          _In_ PCSTR     pszOption,
                          _Inout_ PSTR   *ppmszReason,
                          _Inout_ PDWORD pdwSize)
{
    PSTR    pmszReasonList  = *ppmszReason;
    DWORD   dwNeeded        = 0;
    HRESULT hrResult        = S_OK;
 
 
    // If buffer wasn't passed in, then allocate one.
    if( (NULL == pmszReasonList) || (0 == *pdwSize) )
    {
        // If no size or size is smaller than default, then change to default
        // size.  We want to try to only allocate and call once.
        if(*pdwSize < INITIAL_GET_REASON_SIZE)
        {
            *pdwSize = INITIAL_GET_REASON_SIZE;
        }
 
        // Alloc initial buffer for reason constrained.
        pmszReasonList = (PSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, *pdwSize);
        if(NULL == pmszReasonList)
        {
            ERR(ERRORTEXT("GetWhyConstrained() failed to alloc buffer for constraint reasons for feature %hs and option %hs.\r\n"),
                           pszFeature,
                           pszOption);
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
    }
 
    // Get reason for constraint.
    hrResult = pHelper->WhyConstrained(poemuiobj,
                                       0,
                                       pszFeature,
                                       pszOption,
                                       pmszReasonList,
                                       *pdwSize,
                                       &dwNeeded);
    if( (E_OUTOFMEMORY == hrResult)
        &&
        (*pdwSize < dwNeeded)
      )
    {
        PSTR    pTemp;
 
 
        // INVARIANT:  initial buffer not large enough.
 
        // Re-alloc buffer to needed size.
        pTemp = (PSTR) HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, pmszReasonList, dwNeeded);
        if(NULL == pTemp)
        {
            ERR(ERRORTEXT("GetWhyConstrained() failed to re-allocate buffer for constraint reason for feature %hs and option %hs.\r\n"),
                           pszFeature,
                           pszOption);
 
            hrResult = E_OUTOFMEMORY;
            goto Exit;
        }
        pmszReasonList  = pTemp;
        *pdwSize        = dwNeeded;
 
        // Retry getting constaint reason.
        hrResult = pHelper->WhyConstrained(poemuiobj,
                                           0,
                                           pszFeature,
                                           pszOption,
                                           pmszReasonList,
                                           *pdwSize,
                                           &dwNeeded);
    }
 
 
Exit:
 
    // On error, do clean up.
    if(!SUCCEEDED(hrResult))
    {
        // Free reason buffer.
        if(NULL != pmszReasonList) HeapFree(hHeap, 0, pmszReasonList);
 
        // Return NULL and zero size.
        *ppmszReason    = NULL;
        *pdwSize        = 0;
    }
    else
    {
        *ppmszReason = pmszReasonList;
    }
 
    return hrResult;
}
 
// Creates multi-sz list of feature option pairs that have changed.
HRESULT GetChangedFeatureOptions(_In_ HANDLE           hHeap,
                                 POPTITEM              pOptItem,
                                 WORD                  wItems,
                                 _Outptr_result_maybenull_ PSTR  *ppmszPairs,
                                 _Out_ PWORD           pwPairs,
                                 _Out_ PDWORD          pdwSize)
{
    WORD                wCount      = 0;
    WORD                wChanged    = 0;
    WORD                wPairs      = 0;
    PSTR                pmszPairs   = NULL;
    DWORD               dwNeeded    = 2;
    DWORD               dwOffset    = 0;
    HRESULT             hrResult    = S_OK;
    PFEATUREOPTITEMDATA pData       = NULL;
 
 
    // Walk OPTITEM array looking or changed options,
    // and calculating size needed for multi-sz buffer.
    for(wCount = 0; wCount < wItems; ++wCount)
    {
        PSTR    pszOption   = NULL;
 
 
        // Just go to next item if this OPTITEM hasn't
        // changed or isn't a feature OPTITEM.
        if( !(OPTIF_CHANGEONCE & pOptItem[wCount].Flags)
            ||
            !IsFeatureOptitem(pOptItem + wCount)
          )
        {
            continue;
        }
 
        // For convienience, assign to pointer to feature OPTITEM data.
        pData = (PFEATUREOPTITEMDATA)(pOptItem[wCount].UserData);
 
        // Increment options changed and size needed.
        pszOption = GetOptionKeywordFromOptItem(hHeap, pOptItem + wCount);
        if(NULL != pszOption)
        {
            ++wChanged;
 
            DWORD dwTmp = 0;
 
            if (FAILED(hrResult = SizeTToDWord(strlen(pData->pszFeatureKeyword) + strlen(pszOption) + 2, &dwTmp)))
            {
                goto Exit;
            }
 
            dwNeeded += dwTmp;
 
            // Need to free option keyword string copy
            // allocated in GetOptionKeywordFromOptItem().
            HeapFree(hHeap, 0, pszOption);
            pszOption = NULL;
        }
    }
 
    // Don't need to do anything more if no feature options changed.
    if(0 == wChanged)
    {
        goto Exit;
    }
 
    // Allocate multi-sz buffer.
    pmszPairs = (PSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwNeeded);
    if(NULL == pmszPairs)
    {
        ERR(ERRORTEXT("GetChangedFeatureOptions() failed to allocate multi-sz.\r\n"));
 
        hrResult = E_OUTOFMEMORY;
        goto Exit;
    }
 
    // Build mult-sz list of feature option pairs
    // that changed.
    for(wCount = 0, wPairs = 0; (wCount < wItems) && (wPairs < wChanged); ++wCount)
    {
        PSTR    pszOption   = NULL;
 
 
        // Just go to next item if this OPTITEM hasn't
        // changed or isn't a feature OPTITEM.
        if( !(OPTIF_CHANGEONCE & pOptItem[wCount].Flags)
            ||
            !IsFeatureOptitem(pOptItem + wCount)
          )
        {
            continue;
        }
 
        // For convienience, assign to pointer to feature OPTITEM data.
        pData = (PFEATUREOPTITEMDATA)(pOptItem[wCount].UserData);
 
        // Add feature option pair.
        pszOption = GetOptionKeywordFromOptItem(hHeap, pOptItem + wCount);
        if(NULL != pszOption)
        {
            if (FAILED(hrResult = StringCbCopyA(pmszPairs + dwOffset, dwNeeded - dwOffset, pData->pszFeatureKeyword)))
            {
                goto Exit;
            }
 
            DWORD dwTmp = 0;
 
            if (FAILED(hrResult = SizeTToDWord(strlen(pData->pszFeatureKeyword) + 1, &dwTmp)))
            {
                goto Exit;
            }
 
            dwOffset += dwTmp;
 
            if (FAILED(hrResult = StringCbCopyA(pmszPairs + dwOffset, dwNeeded - dwOffset, pszOption)))
            {
                goto Exit;
            }
 
            if (FAILED(hrResult = SizeTToDWord(strlen(pszOption) + 1, &dwTmp)))
            {
                goto Exit;
            }
 
            dwOffset += dwTmp;
 
            // Keep track of number of pairs added, so
            // we are able to exit loop as soon as
            // we added all changed feature options.
            ++wPairs;
 
            // Need to free option keyword string copy
            // allocated in GetOptionKeywordFromOptItem().
            HeapFree(hHeap, 0, pszOption);
            pszOption = NULL;
        }
    }
 
 
Exit:
 
    if(SUCCEEDED(hrResult))
    {
        // INVARIANT: either successfully build mutli-sz of changed
        //            feature option pairs, or there are no feature
        //            that options changed.
 
        // Return pairs and number of pairs.
        *pwPairs    = wPairs;
        *pdwSize    = dwNeeded;
        *ppmszPairs = pmszPairs;
    }
    else
    {
        // INVARINAT:   error.
 
        // Clean up.
        if(NULL == pmszPairs)    HeapFree(hHeap, 0, pmszPairs);
 
        // Return NULL and zero count.
        *pwPairs    = 0;
        *pdwSize    = 0;
        *ppmszPairs = NULL;
    }
 
    return hrResult;
}
 
// Returns pointer to option keyword for a feature OPTITEM.
PSTR GetOptionKeywordFromOptItem(_In_ HANDLE hHeap, POPTITEM pOptItem)
{
    char                szNumber[16]    = {0};
    PSTR                pszOption       = NULL;
    PFEATUREOPTITEMDATA pData           = NULL;
 
 
    // Validate parameter.
    if(!IsFeatureOptitem(pOptItem))
    {
        ERR(ERRORTEXT("GetOptionKeywordFromOptItem() invalid parameter.\r\n"));
 
        goto Exit;
    }
 
    // For convienience, assign to pointer to feature OPTITEM data.
    pData = (PFEATUREOPTITEMDATA)(pOptItem->UserData);
 
    // Option selection is based on type of OPTTYPE.
    switch(pOptItem->pOptType->Type)
    {
        // For up down arrow control, selection is just pOptItem->Sel
        // converted to string.
        case TVOT_UDARROW:
            if(FAILED(StringCbPrintfA(szNumber, sizeof(szNumber)/sizeof(szNumber[0]), "%u", pOptItem->Sel)))
            {
                ERR(ERRORTEXT("GetOptionKeywordFromOptItem() failed to convert number to string.\r\n"));
            }
            szNumber[sizeof(szNumber)/sizeof(szNumber[0]) - 1] = '\0';
            pszOption = MakeStringCopy(hHeap, szNumber);
            break;
 
        // For combobox, pOptItem->Sel is the index in to the
        // option array.
        case TVOT_COMBOBOX:
            pszOption = MakeStringCopy(hHeap, pData->pOptions->GetKeyword((WORD)pOptItem->Sel));
            break;
 
        // The default is the option count.
        default:
            ERR(ERRORTEXT("GetOptionKeywordFromOptItem() OPTTYPE type %d num of OPTPARAMs not handled.\r\n"),
                            pOptItem->pOptType->Type);
 
            goto Exit;
            break;
    }
 
 
Exit:
 
    return pszOption;
}
 
// Returns pointer to option display name for a feature OPTITEM.
PWSTR GetOptionDisplayNameFromOptItem(_In_ HANDLE hHeap, POPTITEM pOptItem)
{
    WCHAR               szNumber[16]    = {0};
    PWSTR               pszOption       = NULL;
    PFEATUREOPTITEMDATA pData           = NULL;
 
 
    // Validate parameter.
    if(!IsFeatureOptitem(pOptItem))
    {
        ERR(ERRORTEXT("GetOptionDisplayNameFromOptItem() invalid parameter.\r\n"));
 
        goto Exit;
    }
 
    // For convienience, assign to pointer to feature OPTITEM data.
    pData = (PFEATUREOPTITEMDATA)(pOptItem->UserData);
 
    // Option selection is based on type of OPTTYPE.
    switch(pOptItem->pOptType->Type)
    {
        // For up down arrow control, selection is just pOptItem->Sel
        // converted to string.
        case TVOT_UDARROW:
            if(FAILED(StringCbPrintfW(szNumber, sizeof(szNumber)/sizeof(szNumber[0]), L"%u", pOptItem->Sel)))
            {
                ERR(ERRORTEXT("GetOptionDisplayNameFromOptItem() failed to convert number to string.\r\n"));
            }
            szNumber[sizeof(szNumber)/sizeof(szNumber[0]) - 1] = L'\0';
            pszOption = MakeStringCopy(hHeap, szNumber);
            break;
 
        // For combobox, pOptItem->Sel is the index in to the
        // option array.
        case TVOT_COMBOBOX:
            pszOption = MakeStringCopy(hHeap, pOptItem->pOptType->pOptParam[pOptItem->Sel].pData);
            break;
 
        // The default is the option count.
        default:
            ERR(ERRORTEXT("GetOptionDisplayNameFromOptItem() OPTTYPE type %d num of OPTPARAMs not handled.\r\n"),
                            pOptItem->pOptType->Type);
 
            goto Exit;
            break;
    }
 
 
Exit:
 
    return pszOption;
}
 
// Refreshes option selection for each feature OPTITEM
HRESULT RefreshOptItemSelection(CUIHelper *pHelper,
                                POEMUIOBJ poemuiobj,
                                POPTITEM pOptItems,
                                WORD wItems)
{
    HRESULT             hrResult    = S_OK;
    PFEATUREOPTITEMDATA pData       = NULL;
 
 
    // Walk OPTITEM array refreshing feature OPTITEMs.
    for(WORD wCount = 0; wCount < wItems; ++wCount)
    {
        // Just go to next item if this OPTITEM isn't a feature OPTITEM.
        if(!IsFeatureOptitem(pOptItems + wCount))
        {
            continue;
        }
 
        // For convienience, assign to pointer to feature OPTITEM data.
        pData = (PFEATUREOPTITEMDATA)(pOptItems[wCount].UserData);
 
        // Refresh COption selection.
        pData->pOptions->RefreshSelection(*pHelper, poemuiobj);
 
        // Assign COption selection to OPTITEM selection.
        pOptItems[wCount].pSel = pData->pOptions->GetSelection();
 
    }
 
    return hrResult;
}
 
// Creates array of conflict features.
HRESULT GetFirstConflictingFeature(_In_ HANDLE hHeap,
                                   CUIHelper *pHelper,
                                   POEMUIOBJ poemuiobj,
                                   POPTITEM pOptItem,
                                   WORD wItems,
                                   PCONFLICT pConflict)
{
    WORD                wCount      = 0;
    HRESULT             hrResult    = S_OK;
    PFEATUREOPTITEMDATA pData       = NULL;
 
 
    // Walk OPTITEM array looking or changed options that are in conflict.
    for(wCount = 0; wCount < wItems; ++wCount)
    {
        // Just go to next item if this OPTITEM hasn't
        // changed or isn't a feature OPTITEM.
        if( !(OPTIF_CHANGEONCE & pOptItem[wCount].Flags)
            ||
            !IsFeatureOptitem(pOptItem + wCount)
          )
        {
            continue;
        }
 
        // For convienience, assign to pointer to feature OPTITEM data.
        pData = (PFEATUREOPTITEMDATA)(pOptItem[wCount].UserData);
 
        // Init conflict record if this feature is in conflict.
        pConflict->pszOptionKeyword = GetOptionKeywordFromOptItem(hHeap, pOptItem + wCount);
        if(NULL != pConflict->pszOptionKeyword)
        {
            // Get reason for conflict
            // If the feature isn't in conflict,
            // then the pmszReasonList will start
            // with NULL terminator.
            hrResult = GetWhyConstrained(hHeap,
                                         pHelper,
                                         poemuiobj,
                                         pData->pszFeatureKeyword,
                                         pConflict->pszOptionKeyword,
                                         &pConflict->pmszReasons,
                                         &pConflict->dwReasonsSize);
            if(!SUCCEEDED(hrResult))
            {
                // NOTE: driver features aren't supported by WhyConstrained.
                if((E_INVALIDARG == hrResult) && IS_DRIVER_FEATURE(pData->pszFeatureKeyword))
                {
                    // Need to reset the result in case it is the last
                    // feature OPTITEM.
                    hrResult = S_OK;
                    continue;
                }
 
                ERR(ERRORTEXT("GetConflictingFeatures() failed to get reason for feature %hs option %hs constraint. (hrResult = 0x%x)\r\n"),
                               pData->pszFeatureKeyword,
                               pConflict->pszOptionKeyword,
                               hrResult);
 
                goto Exit;
            }
 
            // Record conflict if feature is in conflict.
            if( (NULL != pConflict->pmszReasons)
                &&
                (pConflict->pmszReasons[0] != '\0')
              )
            {
                // Save pointer to feature keyword.
                pConflict->pszFeatureKeyword = pData->pszFeatureKeyword;
                pConflict->pszFeature        = pOptItem[wCount].pName;
                pConflict->pszOption         = GetOptionDisplayNameFromOptItem(hHeap, pOptItem + wCount);
 
                // Found first conflict.
                break;
            }
        }
    }
 
 
Exit:
 
    return hrResult;
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS