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
|
/**************************************************************************
*
* Copyright � Microsoft Corporation
*
* Title: ScanJobs.cpp
*
* Description: This file contains the implementation of the IWiaMiniDrv::
* drvAcquireItemData method and its helper methods used to
* execute scan job requests and transfer image and metadata
* files from or to the Production Scanner Driver Sample.
*
***************************************************************************/
#include "stdafx.h"
/**************************************************************************\
*
* Implements IWiaMiniDrv::drvAcquireItemData. This method is called by the
* WIA service when the driver must transfer image data to the application.
* This driver implements the Stream based WIA 2.0 transfer model introduced
* in Windows Vista. This call correponds to one scan job executed at the device
* (scaner starts, scan documents and transfers data, scanner stops).
*
* Parameters:
*
* pWiasContext - pointer to the item context
* lFlags - reserved (set to 0)
* pmdtc - pointer to a MINIDRV_TRANSFER_CONTEXT structure
* containing the device transfer context
* plDevErrVal - unused (drvGetDeviceErrorStr unsupported)
*
* Return Value:
*
* S_OK if successful, S_FALSE if the transfer is canceled
* or an error HRESULT if an error occurrs
*
\**************************************************************************/
HRESULT CWiaDriver::drvAcquireItemData(
_In_ BYTE* pWiasContext,
LONG lFlags,
_In_ PMINIDRV_TRANSFER_CONTEXT pmdtc,
_Out_ LONG* plDevErrVal)
{
HRESULT hr = S_OK;
GUID guidItemCategory = GUID_NULL;
BSTR bstrItemName = NULL;
BSTR bstrFullItemName = NULL;
WIA_DRIVER_ITEM_CONTEXT *pWiaDriverItemContext = NULL;
IWiaMiniDrvTransferCallback *pTransferCallback = NULL;
WiaTransferParams *pCallbackTransferParams = NULL;
BYTE *pTransferBuffer = NULL;
ULONG ulBufferSize = 0;
BOOL bImageTransfer = TRUE;
WIAEX_TRACE_BEGIN;
//
// Validate parameters:
//
if ((!pWiasContext) || (!pmdtc) || (!plDevErrVal) || (!pmdtc->pIWiaMiniDrvCallBack))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
//
// Identify the item to execute the acquisition for checking WIA_IPA_ITEM_CATEGORY:
//
if (SUCCEEDED(hr))
{
hr = wiasReadPropGuid(pWiasContext, WIA_IPA_ITEM_CATEGORY, &guidItemCategory, NULL, TRUE);
if (SUCCEEDED(hr))
{
bImageTransfer = (IsEqualGUID(WIA_CATEGORY_FLATBED, guidItemCategory) ||
IsEqualGUID(WIA_CATEGORY_FEEDER, guidItemCategory) || IsEqualGUID(WIA_CATEGORY_AUTO, guidItemCategory));
}
else
{
WIAEX_ERROR((g_hInst, "Failed to read WIA_IPA_ITEM_CATEGORY property, hr = 0x%08X", hr));
}
}
//
// Get the item names, we will need them when requesting a new WIA transfer stream:
//
if (SUCCEEDED(hr))
{
hr = wiasReadPropStr(pWiasContext, WIA_IPA_ITEM_NAME, &bstrItemName, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed reading WIA_IPA_ITEM_NAME, hr = 0x%08X", hr));
}
}
if (SUCCEEDED(hr))
{
hr = wiasReadPropStr(pWiasContext, WIA_IPA_FULL_ITEM_NAME, &bstrFullItemName, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed reading WIA_IPA_FULL_ITEM_NAME, hr = 0x%08X", hr));
}
}
//
// Transfers from the Root item are not supported:
//
if (SUCCEEDED(hr) && (IsEqualGUID(WIA_CATEGORY_ROOT, guidItemCategory)))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Acquisition not supported from the Root item, hr = 0x%08X", hr));
}
//
// Get the private context data for this item. This sample driver stores in this data
// an image that the WIA application uploads in this session to the Imprinter or the
// Endorser and that should be used in subsequent downloads from the respective item:
//
if (SUCCEEDED(hr))
{
hr = wiasGetDriverItemPrivateContext(pWiasContext, (BYTE**)&pWiaDriverItemContext);
if (SUCCEEDED(hr) && (!pWiaDriverItemContext))
{
hr = E_POINTER;
}
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to retrieve the driver item context data, hr = 0x%08X", hr));
}
}
//
// Check the requested direction for this data transfer. Download means from the driver to the
// application (from the scanner device to the PC). Upload means from the application to the
// driver (from the PC to the scanner device). For a stream upload the flag value is
// WIA_MINIDRV_TRANSFER_UPLOAD while for a WIA 1.0 tymed-style transfer the flag value is 0:
//
if (SUCCEEDED(hr) && (bImageTransfer || IsEqualGUID(WIA_CATEGORY_BARCODE_READER, guidItemCategory) ||
IsEqualGUID(WIA_CATEGORY_PATCH_CODE_READER, guidItemCategory) ||
IsEqualGUID(WIA_CATEGORY_MICR_READER, guidItemCategory)) && (!(lFlags & WIA_MINIDRV_TRANSFER_DOWNLOAD)))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst,
"Invalid transfer flag parameter requested, this driver supports only download transfer direction for image data, barcode, patch code and MICR metadata, hr = 0x%08X",
hr));
}
if (SUCCEEDED(hr))
{
*plDevErrVal = 0;
}
//
// Get the WIA transfer callback interface to use for these transfers:
//
if (SUCCEEDED(hr))
{
hr = pmdtc->pIWiaMiniDrvCallBack->QueryInterface(IID_IWiaMiniDrvTransferCallback, (void**)&pTransferCallback);
if (SUCCEEDED(hr) && (!pTransferCallback))
{
hr = E_POINTER;
}
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to retrieve IID_IWiaMiniDrvTransferCallback interface, scan failed, hr = 0x%08X", hr));
}
}
//
// Allocate memory for the WIA transfer parameters structure and the transfer buffer:
//
if (SUCCEEDED(hr))
{
pCallbackTransferParams = (WiaTransferParams*)CoTaskMemAlloc(sizeof(WiaTransferParams));
if (!pCallbackTransferParams)
{
hr = E_OUTOFMEMORY;
WIAEX_ERROR((g_hInst, "Memory allocation for transfer parameters failed, scan failed, hr = 0x%08X", hr));
}
else
{
memset(pCallbackTransferParams, 0, sizeof(WiaTransferParams));
}
}
if (SUCCEEDED(hr))
{
hr = AllocateTransferBuffer(&pTransferBuffer, &ulBufferSize);
if (!pTransferBuffer)
{
hr = E_OUTOFMEMORY;
WIAEX_ERROR((g_hInst, "Memory allocation for transfer buffer failed, scan failed, hr = 0x%08X", hr));
}
}
if (SUCCEEDED(hr))
{
//
// The CWiaDriver::Download and CWiaDriver::Upload methods called below
// fully trace their execution, no need to trace additionally here.
// Also note that the pInputStrem stream is released by these functions:
//
if (lFlags & WIA_MINIDRV_TRANSFER_DOWNLOAD)
{
//
// 'Download' transfer direction (driver to application)
//
hr = Download(pWiasContext, guidItemCategory, bstrItemName, bstrFullItemName, pmdtc, pTransferBuffer,
ulBufferSize, pTransferCallback, pCallbackTransferParams, pWiaDriverItemContext);
}
else if (lFlags & WIA_MINIDRV_TRANSFER_UPLOAD)
{
//
// 'Upload' transfer direction (application to driver)
//
hr = Upload(pWiasContext, guidItemCategory, bstrItemName, bstrFullItemName, pTransferBuffer,
ulBufferSize, pTransferCallback, pCallbackTransferParams, pWiaDriverItemContext);
}
}
//
// Note that WIA_TRANSFER_MSG_END_OF_STREAM and WIA_TRANSFER_MSG_END_OF_TRANSFER
// are sent to the WIA application by the WIA service itself (first when the driver
// asks for a new stream), the driver should not send these itself.
//
//
// Clean-up for this scan job:
//
if (bstrItemName)
{
SysFreeString(bstrItemName);
}
if (bstrFullItemName)
{
SysFreeString(bstrFullItemName);
}
if (pTransferBuffer)
{
FreeTransferBuffer(pTransferBuffer);
}
if (pCallbackTransferParams)
{
CoTaskMemFree(pCallbackTransferParams);
}
if (pTransferCallback)
{
pTransferCallback->Release();
}
if (FAILED(hr))
{
m_hrLastEdviceError = hr;
}
WIAEX_TRACE((g_hInst, "IWiaMiniDrv::drvAcquireItemData 0x%08X", hr));
return hr;
}
/**************************************************************************\
*
* Helper for CWiaDriver::drvAcquireItemData. Executes the download data
* transfer sequence for the current scan job,
*
* Parameters:
*
* pWiasContext - item context
* guidItemCategory - item category
* bstrItemName - item name
* bstrFullItemName - full item name
* pmdc - driver transfer context data
* pTransferBuffer - pre-allocated transfer buffer
* ulBufferSize - size of the pre-allocated transfer buffer, in bytes
* pTransferCallback - IWiaMiniDrvTransferCallback* for WIA status
* pCallbackTransferParams - WiaTransferParams* for WIA status callbacks
* ulEstimatedFileSize - estimated file size, in bytes (0 if unknown)
*
* Return Value:
*
* S_OK if successful, S_FALSE if the transfer is canceled
* or an error HRESULT if an error occurrs
*
\**************************************************************************/
HRESULT
CWiaDriver::Download(
_In_ BYTE *pWiasContext,
GUID guidItemCategory,
_In_ BSTR bstrItemName,
_In_ BSTR bstrFullItemName,
_In_ PMINIDRV_TRANSFER_CONTEXT pmdtc,
_In_reads_bytes_(ulBufferSize) BYTE *pTransferBuffer,
ULONG ulBufferSize,
_In_ IWiaMiniDrvTransferCallback *pTransferCallback,
_In_ WiaTransferParams *pCallbackTransferParams,
_In_ WIA_DRIVER_ITEM_CONTEXT *pWiaDriverItemContext)
{
HRESULT hr = S_OK;
IStream *pInputStream = NULL;
IStream *pOutputStream = NULL;
LONG lPagesToScan = 1;
LONG lJobSeparators = WIA_SEPARATOR_DISABLED;
LONG lMultiFeed = WIA_MULTI_FEED_DETECT_DISABLED;
LONG lDataType = WIA_DATA_GRAYSCALE;
LONG lCompression = WIA_COMPRESSION_NONE;
LONG lPixelsPerLine = 0;
LONG lNumberOfLines = 0;
LONG lBytesPerLine = 0;
LONG lFilesToProcess = 1;
LONG lFilesProcessed = 0;
ULONG ulEstimatedFileSize = 0;
ULONG ulFileSize = 0;
ULONG_PTR pGDIPlusToken = NULL;
LONG lGDIPlus_PixelsPerLine = 0;
LONG lGDIPlus_NumberOfLines = 0;
LONG lGDIPlus_BytesPerLine = 0;
DWORD dwJobStart = 0;
IStream *pWiaStream = NULL;
BOOL bCancelTransfer = FALSE;
BOOL bSkipTransfer = FALSE;
BOOL bImageTransfer = (IsEqualGUID(WIA_CATEGORY_FLATBED, guidItemCategory) ||
IsEqualGUID(WIA_CATEGORY_FEEDER, guidItemCategory) || IsEqualGUID(WIA_CATEGORY_AUTO, guidItemCategory));
WIAEX_TRACE_BEGIN;
if ((!pWiasContext) || (!bstrItemName) || (!bstrFullItemName) || (!pmdtc) || (!pTransferBuffer) || (!pTransferCallback) || (!pCallbackTransferParams))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
//
// Reset the recorded scan available input source, if any, ignoring failures:
//
if (SUCCEEDED(hr) && bImageTransfer)
{
UpdateScanAvailableItemName(NULL);
}
//
// For image transfers from feeder, use WIA_IPS_PAGES to know the number of images expected to transfer in this job:
//
if (SUCCEEDED(hr))
{
if (IsEqualGUID(guidItemCategory, WIA_CATEGORY_FEEDER))
{
//
// When WIA_IPS_PAGES is set to 0 (ALL_PAGES) meaning "scan as
// many documents as there may be loaded into the feeder" this
// sample driver will transfer up to MAX_SCAN_PAGES:
//
hr = wiasReadPropLong(pWiasContext, WIA_IPS_PAGES, &lPagesToScan, NULL, TRUE);
if (SUCCEEDED(hr))
{
if (ALL_PAGES == lPagesToScan)
{
lPagesToScan = MAX_SCAN_PAGES;
}
}
else
{
lPagesToScan = 1;
WIAEX_ERROR((g_hInst, "Failed reading the WIA_IPS_PAGES property, hr = 0x%08X", hr));
}
}
else if (IsEqualGUID(WIA_CATEGORY_AUTO, guidItemCategory))
{
//
// In full automatic mode scan as many pages as the device decides to allow:
//
lPagesToScan = MAX_SCAN_PAGES;
}
else
{
//
// One single document page to scan from the flatbed or one single metadata file:
//
lPagesToScan = 1;
}
}
if (SUCCEEDED(hr) && bImageTransfer)
{
WIAS_TRACE((g_hInst, "Pages to scan: %u (0 means all)", lPagesToScan));
}
//
// For feeder acquisitions, this sample driver simulates a job separator every
// JOB_SEPARATOR_AT_PAGE page and a multi-feed every MULTI_FEED_AT_PAGE pages:
//
if (SUCCEEDED(hr) && IsEqualGUID(guidItemCategory, WIA_CATEGORY_FEEDER))
{
hr = wiasReadPropLong(pWiasContext, WIA_IPS_JOB_SEPARATORS, &lJobSeparators, NULL, TRUE);
if (SUCCEEDED(hr))
{
hr = wiasReadPropLong(pWiasContext, WIA_IPS_MULTI_FEED, &lMultiFeed, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed reading the WIA_IPS_MULTI_FEED property, hr = 0x%08X", hr));
}
}
else
{
WIAEX_ERROR((g_hInst, "Failed reading the WIA_IPS_JOB_SEPARATORS property, hr = 0x%08X", hr));
}
}
//
// For feeder acquisitions, if WIA_IPS_FEEDER_CONTROL is set to WIA_FEEDER_CONTROL_MANUAL
// and the feeder is not running, start the feeder and reset the feeder control to automatic
// mode, updating WIA_IPS_FEEDER_CONTROL to WIA_FEEDER_CONTROL_AUTO:
//
if (SUCCEEDED(hr) && IsEqualGUID(guidItemCategory, WIA_CATEGORY_FEEDER))
{
LONG lFeederControl = WIA_FEEDER_CONTROL_AUTO;
hr = wiasReadPropLong(pWiasContext, WIA_IPS_FEEDER_CONTROL, &lFeederControl, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed reading the WIA_IPS_FEEDER_CONTROL property, hr = 0x%08X", hr));
}
if (SUCCEEDED(hr) && (WIA_FEEDER_CONTROL_MANUAL == lFeederControl) && (!m_bFeederStarted))
{
hr = StartFeeder();
if (SUCCEEDED(hr))
{
lFeederControl = WIA_FEEDER_CONTROL_AUTO;
hr = wiasWritePropLong(pWiasContext, WIA_IPS_FEEDER_CONTROL, lFeederControl);
if (SUCCEEDED(hr))
{
WIAS_TRACE((g_hInst, "Reverted to WIA_FEEDER_CONTROL_AUTO"));
}
else
{
WIAEX_ERROR((g_hInst, "Failed to reset the WIA_IPS_FEEDER_CONTROL property, hr = 0x%08X", hr));
}
}
else
{
WIAEX_ERROR((g_hInst, "Cannot start the feeder, hr = 0x%08X", hr));
}
}
}
//
// Read the current data type configured for this image source:
//
if (SUCCEEDED(hr) && bImageTransfer && (!IsEqualGUID(WIA_CATEGORY_AUTO, guidItemCategory)))
{
hr = wiasReadPropLong(pWiasContext, WIA_IPA_DATATYPE, &lDataType, NULL, TRUE);
if (SUCCEEDED(hr))
{
if (WIA_DATA_AUTO == lDataType)
{
//
// When WIA_DATA_AUTO is set the sample driver choses randomly between WIA_DATA_GRAYSCALE
// and WIA_DATA_COLOR. A real driver should base this decision on the actual scan document,
// each document page preferrably. This sample driver uses the same test image to transfer
// all single-file page scans in a job so this initialization is only performed once, for
// the entire job:
//
lDataType = (rand() % 2) ? WIA_DATA_GRAYSCALE : WIA_DATA_COLOR;
WIAS_TRACE((g_hInst, "Detected data type in automatic mode: %ws",
(WIA_DATA_GRAYSCALE == lDataType) ? L"8-bpp grayscale" : L"24-bpp RGB color"));
//
// Updated dependent image information properties:
//
hr = UpdateImageInfoProperties(pWiasContext, lDataType);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to update dependent image information properties, hr = 0x%08X", hr));
}
}
}
else
{
WIAEX_ERROR((g_hInst, "Failed to read WIA_IPA_DATATYPE property, hr = 0x%08X", hr));
}
}
//
// How many files and transfer streams the driver needs for this drvAcquireItemData call:
//
// a) For transfers from Feeder WIA_IPS_PAGES indicates the number of files.
// b) For transfers from Flatbed there is always just one file.
// c) For multi-page transfers (which this sample driver doesn't support) there would be just one file.
//
if (SUCCEEDED(hr))
{
if ((IsEqualGUID(guidItemCategory, WIA_CATEGORY_FEEDER)) || (IsEqualGUID(WIA_CATEGORY_AUTO, guidItemCategory)))
{
lFilesToProcess = lPagesToScan;
}
else
{
lFilesToProcess = 1;
}
WIAS_TRACE((g_hInst, "Files to transfer: %u", lFilesToProcess));
}
if (SUCCEEDED(hr))
{
//
// Check first if we have a (previously uploaded in this session) imprinter/endorser image to use:
//
if (pWiaDriverItemContext->m_pUploadedImage)
{
pInputStream = pWiaDriverItemContext->m_pUploadedImage;
}
else
{
//
// Create a new global memory stream to store the transfer data file:
//
hr = CreateStreamOnHGlobal(NULL, TRUE, &pInputStream);
if (SUCCEEDED(hr) && (!pInputStream))
{
hr = E_FAIL;
}
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "CreateStreamOnHGlobal failed, hr = 0x%08X", hr));
}
//
// Read from resources or generate the test data file (image or metadata) to be downloaded
// to the application, storing this data into the new memory stream:
//
if (SUCCEEDED(hr))
{
ULONG ulResource = 0;
if (bImageTransfer)
{
ulResource = (WIA_DATA_COLOR == lDataType) ? IDB_TESTIMAGE_COLOR : IDB_TESTIMAGE_GRAY;
}
else if ((IsEqualGUID(WIA_CATEGORY_IMPRINTER, guidItemCategory) || IsEqualGUID(WIA_CATEGORY_ENDORSER, guidItemCategory))
&& IsEqualGUID(WiaImgFmt_BMP, pmdtc->guidFormatID))
{
//
// The sample driver will generate itself the WiaImgFmt_CSV and WiaImgFmt_TXT files:
//
ulResource = IDB_TEST_IMPRINTER_IMAGE;
}
else if (IsEqualGUID(WIA_CATEGORY_BARCODE_READER, guidItemCategory) && IsEqualGUID(WiaImgFmt_XMLBAR, pmdtc->guidFormatID))
{
//
// The sample driver hard-codes the sample WiaImgFmt_RAWBAR file:
//
ulResource = IDB_BARCODE_SAMPLE;
}
else if (IsEqualGUID(WIA_CATEGORY_PATCH_CODE_READER, guidItemCategory) && IsEqualGUID(WiaImgFmt_XMLPAT, pmdtc->guidFormatID))
{
//
// The sample driver hard-codes the sample WiaImgFmt_RAWPAT file:
//
ulResource = IDB_PATCH_CODE_SAMPLE;
}
else if (IsEqualGUID(WIA_CATEGORY_MICR_READER, guidItemCategory) && IsEqualGUID(WiaImgFmt_XMLMIC, pmdtc->guidFormatID))
{
//
// The sample driver hard-codes the sample WiaImgFmt_RAWMIC file:
//
ulResource = IDB_MICR_SAMPLE;
}
if (ulResource)
{
hr = LoadTestDataResourceToStream(ulResource, pInputStream, &ulFileSize);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to load the test data file from resource %u, hr = 0x%08X", ulResource, hr));
}
}
else
{
hr = LoadTestDataToStream(pWiasContext, guidItemCategory, pmdtc->guidFormatID, pInputStream, &ulFileSize);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to load the test data file from resource %u, hr = 0x%08X", ulResource, hr));
}
}
}
}
}
//
// For image transfers, if the application asks for DIB (which the driver must always support) or Raw
// this sample driver converts the image from the attached EXIF test image to obtain the uncompressed
// image before the first image transfer takes place. For this the driver first runs the test image
// through the GDI+ DIB encoder to generate the DIB image to transfer to the WIA application.
//
if (S_OK == hr)
{
if (bImageTransfer && IsEqualGUID(pmdtc->guidFormatID, WiaImgFmt_BMP) || IsEqualGUID(pmdtc->guidFormatID, WiaImgFmt_RAW))
{
//
// Initialize GDI+ if the driver must translate scanned images to the DIB format:
//
if (S_OK == hr)
{
WIAS_TRACE((g_hInst, "Initialize GDI++.."));
hr = InitializeGDIPlus(&pGDIPlusToken);
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "Failed to initialize GDI+, hr = 0x%08X", hr));
}
}
//
// Load the image to a GDI+ Image object (GDI+ cannot convert the image directly from the stream):
//
Image *pInputImage = NULL;
if (S_OK == hr)
{
pInputImage = Image::FromStream(pInputStream);
if (!pInputImage)
{
hr = E_FAIL;
WIAEX_ERROR((g_hInst, "Image::FromStream failed, hr = 0x%08X", hr));
}
}
//
// Release the input stream now, no longer needed, the same image is held by the Image object:
//
pInputStream->Release();
pInputStream = NULL;
//
// Hand the Image object to GDI+ to generate the DIB image:
//
if (S_OK == hr)
{
hr = ConvertImageToDIB(pInputImage, &pOutputStream,
&lGDIPlus_PixelsPerLine, &lGDIPlus_NumberOfLines, &lGDIPlus_BytesPerLine);
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "Failed to convert scanned image file to DIB, hr = 0x%08X", hr));
}
}
//
// Free the GDI+ Image copy:
//
if (pInputImage)
{
delete pInputImage;
}
//
// Done with file format conversions, if any. Shutdown GDI+, no longer needed:
//
if (pGDIPlusToken)
{
WIAS_TRACE((g_hInst, "Shutdown GDI++.."));
ShutdownGDIPlus(pGDIPlusToken);
pGDIPlusToken = NULL;
}
//
// For a Raw image transfer, further convert the DIB to an uncompressed Raw image file:
//
if ((S_OK == hr) && IsEqualGUID(pmdtc->guidFormatID, WiaImgFmt_RAW))
{
IStream *pTempOutputStream = NULL;
hr = ConvertDibToRaw(pOutputStream, &pTempOutputStream);
if (S_OK == hr)
{
//
// Release pOutputStream then reset its pointer to the new stream:
//
pOutputStream->Release();
pOutputStream = pTempOutputStream;
pTempOutputStream = NULL;
}
}
//
// If not in auto-config mode check the actual image dimensions reported by GDI+.
// In both programmed and auto-config mode update the estimated file size from
// the image dimensions and depth reported by GDI+ during the conversion process:
//
if (S_OK == hr)
{
if (!IsEqualGUID(WIA_CATEGORY_AUTO, guidItemCategory))
{
HRESULT hrTemp = S_OK;
//
// Update WIA_IPA_PIXELS_PER_LINE, WIA_IPA_NUMBER_OF_LINES and WIA_IPA_BYTES_PER_LINE:
//
hrTemp = wiasWritePropLong(pWiasContext, WIA_IPA_PIXELS_PER_LINE, lGDIPlus_PixelsPerLine);
if (FAILED(hrTemp))
{
WIAEX_ERROR((g_hInst, "Failed to update WIA_IPA_PIXELS_PER_LINE to %u, hr = 0x%08X",
lGDIPlus_PixelsPerLine, hrTemp));
}
if (SUCCEEDED(hrTemp))
{
hrTemp = wiasWritePropLong(pWiasContext, WIA_IPA_NUMBER_OF_LINES, lGDIPlus_NumberOfLines);
if (FAILED(hrTemp))
{
WIAEX_ERROR((g_hInst, "Failed to update WIA_IPA_NUMBER_OF_LINES to %u, hr = 0x%08X",
lGDIPlus_NumberOfLines, hrTemp));
}
}
if (SUCCEEDED(hrTemp))
{
hrTemp = wiasWritePropLong(pWiasContext, WIA_IPA_BYTES_PER_LINE, lGDIPlus_BytesPerLine);
if (FAILED(hrTemp))
{
WIAEX_ERROR((g_hInst, "Failed to update WIA_IPA_BYTES_PER_LINE to %u, hr = 0x%08X",
lGDIPlus_BytesPerLine, hrTemp));
}
}
}
}
}
}
//
// Estimate the size of uncompressed data we need to transfer for each file, assuming
// all images in the current acquisition sequence will be scanned using the same scan
// parameters. The estimate is relatively accurate for uncompressed data. Cannot use
// this kind of estimate for compressed data because of the unknown compression ratio:
//
if ((S_OK == hr) && bImageTransfer && (!IsEqualGUID(WIA_CATEGORY_AUTO, guidItemCategory)))
{
//
// Read the image information properties:
//
hr = wiasReadPropLong(pWiasContext, WIA_IPA_NUMBER_OF_LINES, &lNumberOfLines, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed reading WIA_IPA_NUMBER_OF_LINES, hr = 0x%08X", hr));
}
if (S_OK == hr)
{
hr = wiasReadPropLong(pWiasContext, WIA_IPA_BYTES_PER_LINE, &lBytesPerLine, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed reading WIA_IPA_BYTES_PER_LINE, hr = 0x%08X", hr));
}
}
if (S_OK == hr)
{
hr = wiasReadPropLong(pWiasContext, WIA_IPA_PIXELS_PER_LINE, &lPixelsPerLine, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed reading WIA_IPA_PIXELS_PER_LINE, hr = 0x%08X", hr));
}
}
if (S_OK == hr)
{
hr = wiasReadPropLong(pWiasContext, WIA_IPA_COMPRESSION, &lCompression, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed reading WIA_IPA_COMPRESSION, hr = 0x%08X", hr));
}
}
if ((S_OK == hr) && (lBytesPerLine > 0) && (WIA_COMPRESSION_NONE == lCompression))
{
ulEstimatedFileSize = lBytesPerLine * lNumberOfLines;
if (IsEqualGUID(pmdtc->guidFormatID, WiaImgFmt_BMP))
{
ulEstimatedFileSize += sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
}
else if (IsEqualGUID(pmdtc->guidFormatID, WiaImgFmt_RAW))
{
ulEstimatedFileSize += sizeof(WIA_RAW_HEADER);
}
WIAS_TRACE((g_hInst, "Transfer file size for uncompressed image data: %02.2f KB (%u BPL, %u lines)",
ulEstimatedFileSize / 1024.0f, lBytesPerLine, lNumberOfLines));
}
else
{
ulEstimatedFileSize = ulFileSize;
WIAS_TRACE((g_hInst, "Transfer file size for compressed image data: %02.2f KB (%u uncompreessed BPL, %u lines)",
ulEstimatedFileSize / 1024.0f, lBytesPerLine, lNumberOfLines));
}
}
if ((S_OK == hr) && (!bImageTransfer))
{
ulEstimatedFileSize = ulFileSize;
WIAS_TRACE((g_hInst, "Transfer file size for metadata: %02.2f bytes", ulEstimatedFileSize));
}
//
// Begin the actual data transfer procedure to the WIA application:
//
//
#pragma prefast(suppress:__WARNING_USE_OTHER_FUNCTION, "A sample scan job duration does not exceed 49 days. If there is risk to exceed, use GetTickCount64 instead:"
dwJobStart = GetTickCount();
lFilesProcessed = 0;
while (S_OK == hr)
{
bSkipTransfer = FALSE;
//
// Request a new WIA stream from the WIA client application:
//
if (S_OK == hr)
{
_Analysis_assume_nullterminated_(bstrItemName);
hr = pTransferCallback->GetNextStream(0, bstrItemName, bstrFullItemName, &pWiaStream);
if (S_FALSE == hr)
{
bCancelTransfer = TRUE;
WIAS_TRACE((g_hInst, "IWiaMiniDrvTransferCallback::GetNextStream returned S_FALSE (0x%08X), transfer must be canceled", hr));
}
else if (WIA_STATUS_SKIP_ITEM == hr)
{
bSkipTransfer = TRUE;
WIAS_TRACE((g_hInst, "IWiaMiniDrvTransferCallback::GetNextStream returned WIA_STATUS_SKIP_ITEM (0x%08X), transfer must be skipped", hr));
hr = S_OK;
}
else if (SUCCEEDED(hr) && (S_OK != hr))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::GetNextStream returned an unknown success value, hr = 0x%08X", hr));
hr = E_UNEXPECTED;
}
else if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::GetNextStream failed, hr = 0x%08X", hr));
}
}
//
// Signal start of data transfer to the WIA application and check
// if the application asks already for the transfers to be cancelled:
//
if ((S_OK == hr) && (!bSkipTransfer))
{
pCallbackTransferParams->lMessage = WIA_TRANSFER_MSG_STATUS;
pCallbackTransferParams->hrErrorStatus = 0;
pCallbackTransferParams->lPercentComplete = 0;
pCallbackTransferParams->ulTransferredBytes = 0;
WIAS_TRACE((g_hInst, "Transfer callback: WIA_TRANSFER_MSG_STATUS, 0 bytes"));
hr = pTransferCallback->SendMessage(0, pCallbackTransferParams);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage failed, hr = 0x%08X", hr));
}
else if (S_FALSE == hr)
{
bCancelTransfer = TRUE;
}
else if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage returned unknown success value, hr = 0x%08X", hr));
bCancelTransfer = TRUE;
hr = S_FALSE;
}
}
if (S_OK == hr)
{
if (bSkipTransfer)
{
WIAS_TRACE((g_hInst, "Discarding data to skip the current file transfer (%u)..", lFilesProcessed + 1));
}
else
{
//
// Read the test file and transfer it to the WIA application:
//
if ((S_OK == hr) && (!bCancelTransfer))
{
hr = TransferFile(pOutputStream ? pOutputStream : pInputStream, pWiaStream, pTransferBuffer, ulBufferSize,
pTransferCallback, pCallbackTransferParams, ulEstimatedFileSize, &bCancelTransfer);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "File transfer to WIA stream failed, hr = 0x%08X", hr));
}
}
}
}
//
// The transfer of one file was successfully completed, reset WIA_STATUS_END_OF_MEDIA:
//
if (WIA_STATUS_END_OF_MEDIA == hr)
{
WIAS_TRACE((g_hInst, "File transfer complete (WIA_STATUS_END_OF_MEDIA)"));
hr = S_OK;
}
//
// Signal 100% transfer complete to the WIA client application (pCallbackTransferParams->ulTransferredBytes
// contains the total number of bytes transferred to this stream):
//
if ((S_OK == hr) && (!bSkipTransfer))
{
pCallbackTransferParams->lMessage = WIA_TRANSFER_MSG_STATUS;
pCallbackTransferParams->hrErrorStatus = 0;
pCallbackTransferParams->lPercentComplete = 100;
WIAS_TRACE((g_hInst, "Transfer callback: WIA_TRANSFER_MSG_STATUS, transfer complete, %02.2f KB total (%u)",
pCallbackTransferParams->ulTransferredBytes / 1024.0f, pCallbackTransferParams->lPercentComplete));
hr = pTransferCallback->SendMessage(0, pCallbackTransferParams);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage(WIA_TRANSFER_MSG_STATUS, transfer complete) failed, hr = 0x%08X", hr));
}
else if (S_FALSE == hr)
{
bCancelTransfer = TRUE;
}
else if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage(WIA_TRANSFER_MSG_STATUS, transfer complete) returned unknown success value, hr = 0x%08X", hr));
bCancelTransfer = TRUE;
hr = S_FALSE;
}
}
//
// If the transfer was canceled from the client application send a CancelJob
// to the scanner device to ensure scanning is stopped and all scanned image
// data (not read yet) is discarded from the scanner internal buffer:
//
if (bCancelTransfer)
{
WIAS_TRACE((g_hInst, "Transfer cancelled from WIA application.."));
//
// Make sure hr is left set to S_FALSE (cancelled):
//
hr = S_FALSE;
}
//
// The current job must be canceled at the device if there is an error during the data
// transfer phase, error which may result in leaving the current job in a processing
// state and risk to block the scanner from accepting new jobs until this job times out:
//
else if (FAILED(hr))
{
WIAS_TRACE((g_hInst, "Transfer failed (hr = 0x%08X), abort job..", hr));
}
//
// Clean-up after finishing each individual WIA file transfer:
//
pCallbackTransferParams->lPercentComplete = 0;
pCallbackTransferParams->ulTransferredBytes = 0;
//
// Release the current WIA transfer stream:
//
if (pWiaStream)
{
pWiaStream->Release();
pWiaStream = NULL;
}
//
// Note the loop ends when hr != S_OK.
//
// Increment the number of streams (files) transferred
// and check if we must end the transfers here:
//
if (S_OK == hr)
{
//
// If a finite number of files must be processed (transferred and/or skipped):
//
lFilesProcessed++;
WIAS_TRACE((g_hInst, "Successfully transferred %u file(s)", lFilesProcessed));
if (lFilesToProcess && (lFilesProcessed >= lFilesToProcess))
{
//
// Note that in general for the Feeder item the driver must signal
// end of paper, not S_OK (as documented by MSDN), to ensure that the
// application does not request another drvAcquireItemData. In this
// case however we transfer multiple files in a single call so we
// won't do this:
//
// if (IsEqualGUID(WIA_CATEGORY_FEEDER, guidItemCategory))
// {
// hr = WIA_ERROR_PAPER_EMPTY;
// }
//
//
// Do not transfer another file for this call:
//
hr = WIA_STATUS_END_OF_MEDIA;
}
//
// For feeder acquisitions, if job separators and/or multi-feed detection is enabled,
// check if the sample driver needs to perform an action here (such as stopping the scan):
//
else if ((!(lFilesProcessed % JOB_SEPARATOR_AT_PAGE)) && (WIA_SEPARATOR_DISABLED != lJobSeparators))
{
WIAS_TRACE((g_hInst, "Job separator detected"));
if ((WIA_SEPARATOR_DETECT_SCAN_STOP == lJobSeparators) || (WIA_SEPARATOR_DETECT_NOSCAN_STOP == lJobSeparators))
{
WIAS_TRACE((g_hInst, "End of scan job due to job separator"));
hr = WIA_STATUS_END_OF_MEDIA;
}
}
else if ((!(lFilesProcessed % MULTI_FEED_AT_PAGE)) && (WIA_MULTI_FEED_DETECT_DISABLED != lMultiFeed))
{
WIAS_TRACE((g_hInst, "Multi-feed detected"));
if (WIA_MULTI_FEED_DETECT_STOP_SUCCESS == lMultiFeed)
{
WIAS_TRACE((g_hInst, "End of scan job due to multi-feed"));
hr = WIA_STATUS_END_OF_MEDIA;
}
else if (WIA_MULTI_FEED_DETECT_STOP_ERROR == lMultiFeed)
{
WIAS_TRACE((g_hInst, "Failed scan job due to multi-feed"));
hr = WIA_ERROR_MULTI_FEED;
}
}
//
// For image transfers, if lFileToTransfer is 0 (ALL_PAGES) the transfers end (with S_OK) when either:
//
// 1) an error occurrs;
// 2) a file transfer is canceled (either from the app or the scanner);
// 3) when RetrieveImage would return WIA_ERROR_PAPER_EMPTY.
//
}
}
//
// Compute the number of pages per minute (PPM) transferred for this job:
//
if ((lFilesProcessed > 0) && bImageTransfer)
{
#pragma prefast(suppress:__WARNING_USE_OTHER_FUNCTION, "A sample scan job duration does not exceed 49 days. If there is risk to exceed, use GetTickCount64 instead:"
DWORD dwJobEnd = GetTickCount();
if (dwJobEnd > dwJobStart)
{
float fSecondsElapsed = (dwJobEnd - dwJobStart) / 1000.0f;
if (lFilesProcessed > 1)
{
DWORD dwPagesPerMinute = (DWORD)((60 * lFilesProcessed) / fSecondsElapsed);
WIAS_TRACE((g_hInst, "Measured scan performance for this job is %u PPM (%u single-page image files transferred in %0.2f seconds)",
dwPagesPerMinute, lFilesProcessed, fSecondsElapsed));
}
else
{
WIAS_TRACE((g_hInst, "1 image file transferred in %0.2f seconds", fSecondsElapsed));
}
}
}
//
// Keep the image in the item context, if there is one:
//
if (pWiaDriverItemContext->m_pUploadedImage)
{
pInputStream = NULL;
}
//
// Clean-up for memory streams (set to automatically free memory on release)
// used for the test image:
//
if (pInputStream)
{
pInputStream->Release();
}
if (pOutputStream)
{
pOutputStream->Release();
}
//
// The transfers (of all files) were successfully completed, reset WIA_STATUS_END_OF_MEDIA:
//
if (WIA_STATUS_END_OF_MEDIA == hr)
{
WIAS_TRACE((g_hInst, "All files successfully transferred"));
hr = S_OK;
}
//
// If this job call is about to return WIA_ERROR_PAPER_EMPTY we have to consider
// first the following conditions:
//
// - if an undefined number of transfers had to be performed and at least
// one file was transferred the return code must be changed to S_OK;
//
// - if a finite number of transfers had to be performed and all these
// transfers have been done the return code must be changed to S_OK;
//
// - if a finite number of transfers had to be performed and at least one
// of these transfers - but not all of them - has been done the return
// code must be changed to WIA_STATUS_END_OF_MEDIA;
//
// - WIA_ERROR_PAPER_EMPTY must be returned only when no transfers could
// be completed in the current drvAcquireItemData call.
//
if (WIA_ERROR_PAPER_EMPTY == hr)
{
if (((!lFilesToProcess) && (lFilesProcessed >= 1)) ||
((lFilesToProcess > 0) && (lFilesProcessed >= lFilesToProcess)))
{
WIAS_TRACE((g_hInst, "Running out of paper, returning S_OK for drvAcquireItemData.."));
hr = S_OK;
}
else if ((lFilesToProcess > 0) && (lFilesProcessed < lFilesToProcess) && (lFilesProcessed >= 1))
{
WIAS_TRACE((g_hInst, "Running out of paper, returning WIA_STATUS_END_OF_MEDIA for drvAcquireItemData.."));
hr = WIA_STATUS_END_OF_MEDIA;
}
else
{
WIAS_TRACE((g_hInst, "Running out of paper, returning WIA_ERROR_PAPER_EMPTY for drvAcquireItemData.."));
}
}
else if (WIA_ERROR_MULTI_FEED == hr)
{
WIAS_TRACE((g_hInst, "Multi-feed error, returning WIA_ERROR_MULTI_FEED for drvAcquireItemData.."));
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
/**************************************************************************\
*
* Helper for CWiaDriver::drvAcquireItemData. Executes the upload data
* transfer sequence for the current scan job,
*
* Parameters:
*
* pWiasContext - item context
* guidItemCategory - item category
* bstrItemName - item name
* bstrFullItemName - full item name
* pmdc - driver transfer context data
* pTransferBuffer - pre-allocated transfer buffer
* ulBufferSize - size of the pre-allocated transfer buffer, in bytes
* pTransferCallback - IWiaMiniDrvTransferCallback* for WIA status
* pCallbackTransferParams - WiaTransferParams* for WIA status callbacks
* ulEstimatedFileSize - estimated file size, in bytes (0 if unknown)
*
* Return Value:
*
* S_OK if successful, S_FALSE if the transfer is canceled
* or an error HRESULT if an error occurrs
*
\**************************************************************************/
HRESULT
CWiaDriver::Upload(
_In_ BYTE *pWiasContext,
GUID guidItemCategory,
_In_ BSTR bstrItemName,
_In_ BSTR bstrFullItemName,
_In_reads_bytes_(ulBufferSize) BYTE *pTransferBuffer,
ULONG ulBufferSize,
_In_ IWiaMiniDrvTransferCallback *pTransferCallback,
_In_ WiaTransferParams *pCallbackTransferParams,
_In_ WIA_DRIVER_ITEM_CONTEXT *pWiaDriverItemContext)
{
HRESULT hr = S_OK;
IStream *pInputStream = NULL;
IStream *pWiaStream = NULL;
BOOL bCancelTransfer = FALSE;
BOOL bSkipTransfer = FALSE;
GUID guidUploadFormat = GUID_NULL;
WIAEX_TRACE_BEGIN;
if ((!pWiasContext) || (!bstrItemName) || (!bstrFullItemName) || (!pTransferBuffer) || (!pTransferCallback) || (!pCallbackTransferParams))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
if ((!IsEqualGUID(WIA_CATEGORY_IMPRINTER, guidItemCategory)) && (!IsEqualGUID(WIA_CATEGORY_ENDORSER, guidItemCategory)))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "This driver does not support upload transfer direction for the requested item, hr = 0x%08X", hr));
}
//
// Create a new global memory stream to store the transfer data file:
//
if (SUCCEEDED(hr))
{
hr = CreateStreamOnHGlobal(NULL, TRUE, &pInputStream);
if (SUCCEEDED(hr) && (!pInputStream))
{
hr = E_FAIL;
}
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "CreateStreamOnHGlobal failed, hr = 0x%08X", hr));
}
}
//
// This sample driver executes a very simple data transfer upload for the imprinter/endorser data.
//
// For WiaImgFmt_BMP transfers the driver validates that the image dimensions match
// WIA_IPS_PRINTER_ENDORSER_GRAPHICS_MIN/MAX_WIDTH/HEIGHT dimensions.
//
// For WiaImgFmt_TXT and WiaImgFmt_CSV transfers the driver loads the transferred text and
// attempts to update the current WIA_IPS_PRINTER_ENDORSER_STRING value with it, performing
// validation as if the application would directly set WIA_IPS_PRINTER_ENDORSER_STRING.
//
// It was already validated that WIA_MINIDRV_TRANSFER_UPLOAD works only for
// WIA_CATEGORY_IMPRINTER and WIA_CATEGORY_ENDORSER.
//
//
// Request the WIA transfer stream from the WIA client application:
//
if (SUCCEEDED(hr))
{
_Analysis_assume_nullterminated_(bstrItemName);
hr = pTransferCallback->GetNextStream(0, bstrItemName, bstrFullItemName, &pWiaStream);
if (S_FALSE == hr)
{
bCancelTransfer = TRUE;
WIAS_TRACE((g_hInst, "IWiaMiniDrvTransferCallback::GetNextStream returned S_FALSE (0x%08X), transfer must be canceled", hr));
}
else if (WIA_STATUS_SKIP_ITEM == hr)
{
bSkipTransfer = TRUE;
WIAS_TRACE((g_hInst, "IWiaMiniDrvTransferCallback::GetNextStream returned WIA_STATUS_SKIP_ITEM (0x%08X), transfer must be skipped", hr));
hr = S_OK;
}
else if (SUCCEEDED(hr) && (S_OK != hr))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::GetNextStream returned an unknown success value, hr = 0x%08X", hr));
hr = E_UNEXPECTED;
}
else if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::GetNextStream failed, hr = 0x%08X", hr));
}
}
//
// Signal start of data transfer from the WIA application and check if the application asks for the transfer to be cancelled:
//
if ((S_OK == hr) && (!bSkipTransfer))
{
pCallbackTransferParams->lMessage = WIA_TRANSFER_MSG_STATUS;
pCallbackTransferParams->hrErrorStatus = 0;
pCallbackTransferParams->lPercentComplete = 0;
pCallbackTransferParams->ulTransferredBytes = 0;
WIAS_TRACE((g_hInst, "Transfer callback: WIA_TRANSFER_MSG_STATUS, 0 bytes"));
hr = pTransferCallback->SendMessage(0, pCallbackTransferParams);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage failed, hr = 0x%08X", hr));
}
else if (S_FALSE == hr)
{
bCancelTransfer = TRUE;
}
else if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage returned unknown success value, hr = 0x%08X", hr));
bCancelTransfer = TRUE;
hr = S_FALSE;
}
}
if ((S_OK == hr) && (!bSkipTransfer))
{
//
// Transfer the file from the WIA application:
//
if ((S_OK == hr) && (!bCancelTransfer))
{
hr = TransferFile(pWiaStream, pInputStream, pTransferBuffer, ulBufferSize,
pTransferCallback, pCallbackTransferParams, 0, &bCancelTransfer);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "File transfer from the WIA stream failed, hr = 0x%08X", hr));
}
}
}
//
// The transfer of one file was successfully completed, reset WIA_STATUS_END_OF_MEDIA:
//
if (WIA_STATUS_END_OF_MEDIA == hr)
{
WIAS_TRACE((g_hInst, "File transfer complete (WIA_STATUS_END_OF_MEDIA)"));
hr = S_OK;
}
//
// Signal 100% transfer complete to the WIA client application (pCallbackTransferParams->ulTransferredBytes
// contains the total number of bytes transferred to this stream):
//
if ((S_OK == hr) && (!bSkipTransfer))
{
pCallbackTransferParams->lMessage = WIA_TRANSFER_MSG_STATUS;
pCallbackTransferParams->hrErrorStatus = 0;
pCallbackTransferParams->lPercentComplete = 100;
WIAS_TRACE((g_hInst, "Transfer callback: WIA_TRANSFER_MSG_STATUS, transfer complete, %02.2f KB total (%u)",
pCallbackTransferParams->ulTransferredBytes / 1024.0f, pCallbackTransferParams->lPercentComplete));
hr = pTransferCallback->SendMessage(0, pCallbackTransferParams);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage(WIA_TRANSFER_MSG_STATUS, transfer complete) failed, hr = 0x%08X", hr));
}
}
//
// Clean-up after finishing each individual WIA file transfer:
//
pCallbackTransferParams->lPercentComplete = 0;
pCallbackTransferParams->ulTransferredBytes = 0;
//
// Release the current WIA transfer stream:
//
if (pWiaStream)
{
pWiaStream->Release();
pWiaStream = NULL;
}
//
// For upload transfers we need to check the current WIA_IPA_FORMAT to see what
// format is the file that was uploaded by the application, the MINIDRV_TRANSFER_CONTEXT
// structure not being initialized with the right file format in this case.
// Remember that the WIA application needs to set WIA_IPA_FORMAT before calling
// IWiaTransfer::Upload:
//
if (S_OK == hr)
{
hr = wiasReadPropGuid(pWiasContext, WIA_IPA_FORMAT, &guidUploadFormat, NULL, TRUE);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to read WIA_IPA_FORMAT, hr = 0x%08X", hr));
}
}
if (S_OK == hr)
{
//
// At this point pInputStream contains the data uploaded by the application
//
if (IsEqualGUID(WiaImgFmt_BMP, guidUploadFormat))
{
if (SUCCEEDED(IsDibValid(pInputStream, 1, IMPRINTER_MAX_WIDTH, IMPRINTER_MAX_HEIGHT)))
{
if (pWiaDriverItemContext->m_pUploadedImage)
{
IStream *pTemp = pWiaDriverItemContext->m_pUploadedImage;
pWiaDriverItemContext->m_pUploadedImage = NULL;
pTemp->Release();
}
pWiaDriverItemContext->m_pUploadedImage = pInputStream;
pInputStream = NULL;
}
else
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst,
"The uploaded WiaImgFmt_BMP (DIB) image is invalid. This item accepts only a 1-bpp %ux%u pixels image, bottom to top line order, hr = 0x%08X",
IMPRINTER_MAX_WIDTH, IMPRINTER_MAX_HEIGHT, hr));
}
}
else if (IsEqualGUID(WiaImgFmt_TXT, guidUploadFormat) || IsEqualGUID(WiaImgFmt_CSV, guidUploadFormat))
{
if (FAILED(IsImprinterEndorserTextValid(pWiasContext, pInputStream, IsEqualGUID(WIA_CATEGORY_IMPRINTER, guidItemCategory) ? IMPRINTER : ENDORSER)))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "The uploaded text data is invalid, hr = 0x%08X", hr));
}
}
else
{
USHORT *pUUID = NULL;
hr = E_INVALIDARG;
if (RPC_S_OK == UuidToStringW(&guidUploadFormat, &pUUID))
{
WIAEX_ERROR((g_hInst, "Unsupported upload transfer file format (%ws), hr = 0x%08X", pUUID, hr));
RpcStringFreeW(&pUUID);
}
else
{
WIAEX_ERROR((g_hInst, "Unsupported upload transfer file format, hr = 0x%08X", hr));
}
}
}
if (pInputStream)
{
pInputStream->Release();
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
/**************************************************************************\
*
* Helper for CWiaDriver::drvAcquireItemData. Transfers one file (image or
* metadata) to the WIA transfer stream provided by the aplication. Note that
* for image file transfers it is not a good solution to let GDI+ to write the
* image directly to the WIA transfer stream as the WIA stream has special
* requirements such as predetermined buffer size, reposition of the write
* cursor at the beginning of the stream between writes and WIA notifications.
*
* Parameters:
*
* pInputStream - IStream object to read the file from
* pDestinationStream - IStream object to write the file to
* pTransferBuffer - pre-allocated transfer buffer
* ulBufferSize - size of the pre-allocated transfer buffer, in bytes
* pTransferCallback - optional IWiaMiniDrvTransferCallback* for WIA status
* pCallbackTransferParams - optional WiaTransferParams* for WIA status callbacks
* ulEstimatedFileSize - estimated file size, in bytes (0 if unknown)
* pbCancelTransfer - on return indicates if the WIA client canceled
* the operation
*
* Remarks:
*
* The caller may specify a non zero pCallbackTransferParams->lPercentComplete
* value (between 0% and 49%) to have this function resume incrementing the
* percent complete starting from this value. An invalid value is set to 0%
* and does not cause the function to fail (still, the caller must not do this).
*
* Return Value:
*
* WIA_STATUS_END_OF_MEDIA if successful, S_FALSE if the transfer
* is canceled or an error HRESULT if an error occurrs
*
\**************************************************************************/
HRESULT
CWiaDriver::TransferFile(
_In_ IStream *pInputStream,
_In_ IStream *pDestinationStream,
_In_reads_bytes_(ulBufferSize)
BYTE *pTransferBuffer,
ULONG ulBufferSize,
_In_opt_ IWiaMiniDrvTransferCallback *pTransferCallback,
_In_opt_ WiaTransferParams *pCallbackTransferParams,
ULONG ulEstimatedFileSize,
_Out_ BOOL *pbCancelTransfer)
{
HRESULT hr = S_OK;
HRESULT hrTemp = S_OK;
ULONG ulBytesToRead = 0;
ULONG ulBytesRead = 0;
ULONG ulBytesToWrite = 0;
ULONG ulBytesWritten = 0;
ULONG ulFileBytesWritten = 0;
ULONG ulPercentComplete = 0;
ULONG ulStartProgressFrom = 0;
const LARGE_INTEGER liZeroOffset = {};
WIAEX_TRACE_BEGIN;
if ((!pInputStream) || (!pDestinationStream) || (!pTransferBuffer) || (!pbCancelTransfer))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
else
{
*pbCancelTransfer = FALSE;
}
if ((S_OK == hr) && pTransferCallback && pCallbackTransferParams && pCallbackTransferParams->lPercentComplete)
{
if ((pCallbackTransferParams->lPercentComplete < 0) || (pCallbackTransferParams->lPercentComplete > 49))
{
WIAEX_ERROR((g_hInst, "Invalid WiaTransferParams::lPercentComplete parameter (%d), reset to 0",
pCallbackTransferParams->lPercentComplete));
ulStartProgressFrom = 0;
}
else
{
ulStartProgressFrom = (ULONG)pCallbackTransferParams->lPercentComplete;
WIAS_TRACE((g_hInst, "Resuming progress indicator from %u", ulStartProgressFrom));
}
}
if (S_OK == hr)
{
//
// Make sure the input stream pointer is at the beginning of the stream before reading from it:
//
hr = pInputStream->Seek(liZeroOffset, STREAM_SEEK_SET, NULL);
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IStream::Seek(0, STREAM_SEEK_SET, NULL) failed, hr = 0x%08X", hr));
}
}
//
// Resume from current pCallbackTransferParams->lPercentComplete,
// do not reset to 0 pCallbackTransferParams->lPercentComplete:
//
if ((S_OK == hr) && pCallbackTransferParams)
{
pCallbackTransferParams->ulTransferredBytes = 0;
}
while (S_OK == hr)
{
ulBytesRead = 0;
ulBytesToRead = ulBufferSize;
ulBytesWritten = 0;
ulBytesToWrite = 0;
*pbCancelTransfer = FALSE;
//
// Read one buffer of data from the input stream. Note the source IStream
// may return S_OK and no data when the transfer is complete:
//
hr = pInputStream->Read(pTransferBuffer, ulBytesToRead, &ulBytesRead);
if ((S_FALSE == hr) || ((S_OK == hr) && (!ulBytesRead)))
{
//
// End of file, transfer of this file should be complete:
//
hr = WIA_STATUS_END_OF_MEDIA;
WIAS_TRACE((g_hInst, "IStream::Read: end of media, data transfer complete"));
}
else if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "IStream::Read failed, hr = 0x%08X", hr));
}
else if (ulBytesRead > ulBytesToRead)
{
hr = E_UNEXPECTED;
WIAEX_ERROR((g_hInst, "IStream::Read caused a possible buffer overflow (%u bytes over %u limit), hr = 0x%08X",
ulBytesRead - ulBytesToRead, ulBytesToRead, hr));
}
//
// Write the the read buffer content to the destination stream:
//
if ((S_OK == hr) || ((WIA_STATUS_END_OF_MEDIA == hr) && (ulBytesRead > 0)))
{
//
// Make sure the write stream pointer is at the end of the stream before writing to it:
//
hrTemp = pDestinationStream->Seek(liZeroOffset, STREAM_SEEK_END, NULL);
if (FAILED(hrTemp))
{
WIAEX_ERROR((g_hInst, "IStream::Seek(0, STREAM_SEEK_END, NULL) failed, hr = 0x%08X", hrTemp));
}
//
// Write the buffer to the destination stream:
//
if (S_OK == hrTemp)
{
ulBytesToWrite = ulBytesRead;
hrTemp = pDestinationStream->Write(pTransferBuffer, ulBytesToWrite, &ulBytesWritten);
if (FAILED(hrTemp))
{
WIAEX_ERROR((g_hInst, "IStream::Write(%u bytes) failed, hr = 0x%08X", ulBytesToWrite, hrTemp));
}
}
//
// Make progress callback to the WIA client application and check for a cancel transfer request:
//
if (S_OK == hrTemp)
{
if (pTransferCallback && pCallbackTransferParams)
{
ulFileBytesWritten += ulBytesWritten;
//
// Indicate to ComputeTransferProgress a direct/full transfer (0% .. 99%):
//
ComputeTransferProgress(&ulPercentComplete, ulEstimatedFileSize, ulFileBytesWritten, TRUE, ulStartProgressFrom);
pCallbackTransferParams->lMessage = WIA_TRANSFER_MSG_STATUS;
pCallbackTransferParams->ulTransferredBytes = ulFileBytesWritten;
pCallbackTransferParams->lPercentComplete = ulPercentComplete;
WIAS_TRACE((g_hInst, "Transfer callback: WIA_TRANSFER_MSG_STATUS, %u bytes, %02.2f KB total (%u)",
ulBytesWritten, pCallbackTransferParams->ulTransferredBytes / 1024.0f, ulPercentComplete));
hrTemp = pTransferCallback->SendMessage(0, pCallbackTransferParams);
if (FAILED(hrTemp))
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage(%u bytes written) failed, hr = 0x%08X",
ulBytesWritten, hrTemp));
}
else if (S_FALSE == hrTemp)
{
*pbCancelTransfer = TRUE;
}
else if (S_OK != hrTemp)
{
WIAEX_ERROR((g_hInst, "IWiaMiniDrvTransferCallback::SendMessage(%u bytes written) returned unknown success value, hr = 0x%08X",
ulBytesWritten, hrTemp));
*pbCancelTransfer = TRUE;
hrTemp = S_FALSE;
}
}
else
{
WIAS_TRACE((g_hInst, "Transferred %u bytes, %02.2f KB total (%u)", ulBytesWritten,
ulFileBytesWritten / 1024.0f, ulPercentComplete));
}
}
//
// Preserve WIA_STATUS_END_OF_MEDIA to allow this loop to normally end, unless a failure was encountered:
//
if ((S_OK != hrTemp) || (WIA_STATUS_END_OF_MEDIA != hr))
{
hr = hrTemp;
}
}
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
/**************************************************************************\
*
* Helper for IWiaMiniDrv::drvAcquireItemData. Executes a simple validation
* for the WiaImgFmt_BMP (DIB) image uploaded by the application and stored
* in the specified stream.
*
* Parameters:
*
* pStream - stream source for the image to be validated
* lBitDepth - expected pixel bit depth (e.g. 24)
* lWidth - expected image width, in pixels
* lHeight - expected image height, in pixels; also describes
* the DIB line order (if negative the image data is
* top to bottom, positive means bottom to top)
*
* Return Value:
*
* S_OK if successful and the image is valid, E_INVALIDARG if the image is
* invalid or another error HRESULT if another error occurrs and the image
* cannot be validated
*
\**************************************************************************/
HRESULT
CWiaDriver::IsDibValid(
_In_ IStream* pStream,
LONG lBitDepth,
LONG lWidth,
LONG lHeight)
{
HRESULT hr = S_OK;
LARGE_INTEGER lStart = {};
BITMAPINFO bi = {};
WIAEX_TRACE_BEGIN;
if (!pStream)
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
if (SUCCEEDED(hr))
{
lStart.LowPart = sizeof(BITMAPFILEHEADER);
hr = pStream->Seek(lStart, STREAM_SEEK_SET, NULL);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to reset the input stream, hr = 0x%08X", hr));
}
}
if (S_OK == hr)
{
hr = pStream->Read(&bi, sizeof(bi), NULL);
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "Failed to read the DIB header from the image, hr = 0x%08X", hr));
if (SUCCEEDED(hr))
{
hr = E_INVALIDARG;
}
}
}
if (SUCCEEDED(hr) && (bi.bmiHeader.biBitCount != lBitDepth))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid DIB pixel bit depth, got %u bits, expected %u bit(s), hr = 0x%08X",
bi.bmiHeader.biBitCount, lBitDepth, hr));
}
if (SUCCEEDED(hr) && (bi.bmiHeader.biWidth != lWidth))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid DIB width, got %u pixels, expected %u pixels, hr = 0x%08X",
bi.bmiHeader.biWidth, lWidth, hr));
}
if (SUCCEEDED(hr) && (bi.bmiHeader.biHeight != lHeight))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid DIB height and/or line order, got %l pixels, expected %l pixels, hr = 0x%08X",
bi.bmiHeader.biHeight, lHeight, hr));
}
if (S_OK == hr)
{
lStart.LowPart = 0;
hr = pStream->Seek(lStart, STREAM_SEEK_SET, NULL);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to reset the input stream, hr = 0x%08X", hr));
}
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
/**************************************************************************\
*
* Helper for IWiaMiniDrv::drvAcquireItemData. Executes a simple validation
* for the WiaImgFmt_TXT and WiaImgFmt_CSV imprinter/endorser text uploaded
* by the application and stored in the specified stream and if the text is
* valid it updates WIA_IPS_PRINTER_ENDORSER_STRING.
*
* Parameters:
*
* pWiasContext - item context
* pStream - stream source for the image to be validated
* nDocumentHandlingSelect - IMPRINTER or ENDORSER (defined in wiadef.h)
*
* Return Value:
*
* S_OK if successful and the image is valid, E_INVALIDARG or another
* error HRESULT if validation fails or if it cannot be performed
*
\**************************************************************************/
HRESULT
CWiaDriver::IsImprinterEndorserTextValid(
_In_ BYTE* pWiasContext,
_In_ IStream* pStream,
LONG nDocumentHandlingSelect)
{
HRESULT hr = S_OK;
LARGE_INTEGER liZero = {};
ULARGE_INTEGER ui = {};
ULONG ulNumChars = 0;
ULONG ulDataSize = 0;
PWCHAR pwData = NULL;
BSTR bstrData = NULL;
WIAEX_TRACE_BEGIN;
if (!pStream)
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
if (SUCCEEDED(hr))
{
hr = IStream_Size(pStream, &ui);
if (SUCCEEDED(hr))
{
WIAS_TRACE((g_hInst, "Stream size, low: %u bytes, high: %u bytes",
ui.LowPart, ui.HighPart));
if (ui.HighPart > 0)
{
WIAS_TRACE((g_hInst, "Stream size exceeeds maximum accepted by this item, last %u bytes will be ignored", ui.HighPart));
}
//
// Data must contain the two bytes BOM and at least one double-byte character:
//
if (ui.LowPart >= (2 * sizeof(WCHAR)))
{
ulDataSize = ui.LowPart;
}
else
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Insufficient data (less than one actual character), hr = 0x%08X", hr));
}
}
else
{
WIAEX_ERROR((g_hInst, "IStream_Size failed, hr = 0x%08X", hr));
}
}
if (SUCCEEDED(hr))
{
hr = pStream->Seek(liZero, STREAM_SEEK_SET, NULL);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to reset memory stream, hr = 0x%08X", hr));
}
}
//
// Read first the BOM bytes and validate:
//
if (S_OK == hr)
{
BYTE bBOM[2] = {};
hr = pStream->Read(bBOM, 2, NULL);
if (S_OK == hr)
{
if ((0xFF != bBOM[0]) || (0xFE != bBOM[1]))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid BOM. Expected 0xFF 0xFE, received 0x%X 0x%X, hr = 0x%08X", bBOM[0], bBOM[1], hr));
}
}
else
{
WIAEX_ERROR((g_hInst, "Failed to read the BOM from the stream, hr = 0x%08X", hr));
if (SUCCEEDED(hr))
{
hr = E_INVALIDARG;
}
}
}
//
// Alocate memory for the NULL terminated WCHAR string. The NULL string terminator is not
// expected in the stream, however the stream must contain the BOM "character" instead,
// thus we need to allocate space for the same number of whole double byte characters
// that are in the stream:
//
if (SUCCEEDED(hr))
{
ulNumChars = ulDataSize / sizeof(WCHAR);
pwData = new WCHAR[ulNumChars];
if (pwData)
{
//
// NULL terminate the string:
//
pwData[ulNumChars - 1] = 0;
}
else
{
hr = E_OUTOFMEMORY;
WIAEX_ERROR((g_hInst, "Out of memory when trying to allocate %u bytes, hr = 0x%08X", ulDataSize, hr));
}
}
//
// Read the actual imprinter/endorser characters:
//
if (S_OK == hr)
{
//
// ulNumChars includes the length of the NULL terminator:
//
ulNumChars -= 1;
hr = pStream->Read((PBYTE)pwData, ulNumChars * sizeof(WCHAR), NULL);
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "Failed to read from the stream, hr = 0x%08X", hr));
if (SUCCEEDED(hr))
{
hr = E_INVALIDARG;
}
}
}
//
// Validate each character against the set of valid characters for the source:
//
if (SUCCEEDED(hr))
{
PWCHAR szValidChars = (IMPRINTER == nDocumentHandlingSelect) ? SAMPLE_IMPRINTER_VALID_CHARS : SAMPLE_ENDORSER_VALID_CHARS;
ULONG ulValidChars = ((IMPRINTER == nDocumentHandlingSelect) ? ARRAYSIZE(SAMPLE_IMPRINTER_VALID_CHARS) : ARRAYSIZE(SAMPLE_ENDORSER_VALID_CHARS)) - 1;
BOOL bFound = FALSE;
for (ULONG i = 0; i < ulNumChars; i++)
{
bFound = FALSE;
for (ULONG j = 0; j < ulValidChars; j++)
{
if (pwData[i] == szValidChars[j])
{
bFound = TRUE;
i++;
break;
}
}
if (!bFound)
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid character for WIA_IPS_PRINTER_ENDORSER_STRING: 0x%X (%wc), hr = 0x%08X", pwData[i], pwData[i], hr));
break;
}
}
}
if (SUCCEEDED(hr))
{
bstrData = SysAllocString(pwData);
if (!bstrData)
{
hr = E_OUTOFMEMORY;
WIAEX_ERROR((g_hInst, "Out of memory when trying to allocate a BSTR of %u characters in length, hr = 0x%08X", ulNumChars, hr));
}
}
if (SUCCEEDED(hr))
{
hr = wiasWritePropStr(pWiasContext, WIA_IPS_PRINTER_ENDORSER_STRING, bstrData);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Failed to set WIA_IPS_PRINTER_ENDORSER_STRING, hr = 0x%08X", hr));
}
}
if (bstrData)
{
SysFreeString(bstrData);
}
if (pwData)
{
delete[] pwData;
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
/**************************************************************************\
*
* Helper for IWiaMiniDrv::drvAcquireItemData. Loads a test data file (image
* or metadata) from resources. A test image is expected to be in EXIF format
* and match the dimensions described by the MIN/MAX_TEST_SCAN_WIDTH/HEIGHT.
* The function does not attempt to validate the contents of data loaded from
* resources, it only copies the whole data file to the specified stream.
*
* Parameters:
*
* ulResourceId - resource identifier
* pStream - stream destination for the test image
* pulDataSize - returns the size in bytes of the data loaded from
* resources and written to the destination stream
*
* Return Value:
*
* S_OK if successful or an error HRESULT if an error occurrs
*
\**************************************************************************/
HRESULT
CWiaDriver::LoadTestDataResourceToStream(
ULONG ulResourceId,
_In_ IStream *pStream,
_Out_ ULONG *pulDataSize)
{
HRESULT hr = S_OK;
WIAEX_TRACE_BEGIN;
if ((!pStream) && (!pulDataSize))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
if (SUCCEEDED(hr))
{
*pulDataSize = 0;
HRSRC hResourceInfo = FindResource(g_hInst, MAKEINTRESOURCE(ulResourceId), RT_RCDATA);
if (hResourceInfo)
{
ULONG ulWrittenData = 0;
ULONG ulDataSize = (ULONG)SizeofResource(g_hInst, hResourceInfo);
if (ulDataSize > 0)
{
HGLOBAL hTestData = LoadResource(g_hInst, hResourceInfo);
if (hTestData)
{
PBYTE pTestData = (PBYTE)LockResource(hTestData);
if (pTestData)
{
LARGE_INTEGER lZero = {};
hr = pStream->Seek(lZero, STREAM_SEEK_SET, NULL);
if (S_OK == hr)
{
hr = pStream->Write(pTestData, ulDataSize, &ulWrittenData);
if (S_OK == hr)
{
if (ulWrittenData != ulDataSize)
{
hr = E_FAIL;
WIAEX_ERROR((g_hInst, "Failed to load data (%u) from resources, expected %u bytes, got %u bytes, hr = 0x%08X",
ulResourceId, ulDataSize, ulWrittenData, hr));
}
}
else
{
WIAEX_ERROR((g_hInst, "Failed to load data (%u) from resources, hr = 0x%08X", ulResourceId, hr));
}
HRESULT hrTemp = pStream->Seek(lZero, STREAM_SEEK_SET, NULL);
if (S_OK != hrTemp)
{
if (S_OK == hr)
{
hr = hrTemp;
}
WIAEX_ERROR((g_hInst, "Failed to reset memory stream, hr = 0x%08X", hrTemp));
}
}
else
{
WIAEX_ERROR((g_hInst, "Failed to reset memory stream, hr = 0x%08X", hr));
}
}
DeleteObject(hTestData);
}
if (S_OK == hr)
{
*pulDataSize = ulDataSize;
}
}
else
{
hr = HRESULT_FROM_WIN32(::GetLastError());
WIAEX_ERROR((g_hInst, "Bad resource (%u), hr = 0x%08X", ulResourceId, hr));
}
}
else
{
hr = HRESULT_FROM_WIN32(::GetLastError());
WIAEX_ERROR((g_hInst, "Cannot find resource (%u), hr = 0x%08X", ulResourceId, hr));
}
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
/**************************************************************************\
*
* Helper for IWiaMiniDrv::drvAcquireItemData. Writes generated or hard-coded
* sample metadata to the specified download stream. This data is to be
* transferred from this sample driver to the WIA application client as an
* imprinter/endorser text/CSV file, or raw barcode/patch code/MICR metadata.
*
* Parameters:
*
* pWiasContext - item context
* guidItemCategory - item category (WIA_CATEGORY_ value)
* guidFormat - trasfer file format (WiaImgFmt_ value)
* pStream - stream destination for the test image
* pulDataSize - returns the size in bytes of the data loaded from
* resources and written to the destination stream
*
* Return Value:
*
* S_OK if successful or an error HRESULT if an error occurrs
*
\**************************************************************************/
HRESULT
CWiaDriver::LoadTestDataToStream(
_In_ BYTE *pWiasContext,
GUID guidItemCategory,
GUID guidFormat,
_In_ IStream *pStream,
_Out_ ULONG *pulDataSize)
{
HRESULT hr = S_OK;
ULONG ulDataSize = 0;
WIAEX_TRACE_BEGIN;
if ((!pWiasContext) || (!pStream) && (!pulDataSize))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
if (SUCCEEDED(hr))
{
if ((IsEqualGUID(WIA_CATEGORY_IMPRINTER, guidItemCategory) || IsEqualGUID(WIA_CATEGORY_ENDORSER, guidItemCategory)) &&
(!IsEqualGUID(WiaImgFmt_BMP, guidFormat)))
{
if (IsEqualGUID(WiaImgFmt_TXT, guidFormat) || IsEqualGUID(WiaImgFmt_CSV, guidFormat))
{
BSTR bstrData = NULL;
//
// Read the current WIA_IPS_PRINTER_ENDORSER_STRING character string value
// and prepare to write it to the destination stream, for simplicity without
// expanding any special formatting sequences. Note the NULL string terminator
// is not to be written to the stream (which for this sample it will end up
// as a TXT or CSV file):
//
hr = wiasReadPropStr(pWiasContext, WIA_IPS_PRINTER_ENDORSER_STRING, &bstrData, NULL, TRUE);
if (SUCCEEDED(hr))
{
hr = pStream->Write(g_bBOM, sizeof(g_bBOM), NULL);
if (SUCCEEDED(hr))
{
ulDataSize = (ULONG)(wcslen((PWCHAR)bstrData) * sizeof(WCHAR));
hr = pStream->Write((PBYTE)bstrData, ulDataSize, NULL);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "IStream::Write(data) failed, hr = 0x%08X", hr));
}
}
else
{
WIAEX_ERROR((g_hInst, "IStream::Write(prefix) failed, hr = 0x%08X", hr));
}
}
else
{
WIAEX_ERROR((g_hInst, "Failed to read the current WIA_IPS_PRINTER_ENDORSER_STRING value, hr = 0x%08X", hr));
}
if (bstrData)
{
SysFreeString(bstrData);
}
}
else
{
hr = E_INVALIDARG;
}
}
else if (IsEqualGUID(WIA_CATEGORY_BARCODE_READER, guidItemCategory) && IsEqualGUID(WiaImgFmt_RAWBAR, guidFormat))
{
WIA_BARCODES bc = {};
WIA_BARCODE_INFO bi = {};
//
// 3 hard-coded sample barcodes to report. These sample barcodes match the XML sample metadata (Barcodes.xml):
//
WCHAR szBarcodeOne[] = L"036000291452";
WCHAR szBarcodeTwo[] = L"3117013206375";
WCHAR szBarcodeThree[] = L"This is a Full ASCII Code 39 example";
//
// When computing the data size take into account the following important details:
//
// WIA_BARCODES includes a WIA_BARCODE_INFO structure element as a placeholder.
// WIA_BARCODE_INFO includes a WCHAR pleceholder for the text.
// Make sure the size of these fields are not counted twice.
//
// Do not include the length of the NULL string terminators for the character sequences,
// they and not NULL terminated.
//
// Do not compute the fixed size (excluding the text) of a WIA_BARCODE_INFO structure
// doing sizeof(WIA_BARCODE_INFO) - sizeof(WCHAR), this will incorrectly add compiler
// padding for the data structure (2 additional bytes by default). Instead compute
// the length of the fixed WIA_BARCODE_INFO size as 8 * sizeof(DWORD).
//
ULONG ulFixedSize = 4 * sizeof(DWORD); //not: sizeof(WIA_BARCODES - sizeof(WIA_BARCODES_INFO)
ULONG ulFixedInfoSize = 8 * sizeof(DWORD); //not: sizeof(WIA_BARCODES_INFO) - sizeof(WCHAR)
ulDataSize = ulFixedSize + (3 * ulFixedInfoSize) + sizeof(szBarcodeOne) + sizeof(szBarcodeTwo) + sizeof(szBarcodeThree) - (3 * sizeof(WCHAR));
const char szSignature[] = "WBAR";
memcpy(&bc.Tag, szSignature, sizeof(DWORD));
bc.Version = 0x00010000;
bc.Size = ulDataSize;
bc.Count = 3;
hr = pStream->Write(&bc, ulFixedSize, NULL);
if (SUCCEEDED(hr))
{
bi.Size = ulFixedInfoSize + sizeof(szBarcodeOne) - sizeof(WCHAR);
bi.Type = 0;
bi.Page = 0;
bi.Confidence = 5;
bi.XOffset = 0;
bi.YOffset = 0;
bi.Rotation = 90;
bi.Length = (DWORD)wcslen(szBarcodeOne);
hr = pStream->Write(&bi, ulFixedInfoSize, NULL);
if (SUCCEEDED(hr))
{
hr = pStream->Write(szBarcodeOne, sizeof(szBarcodeOne) - sizeof(WCHAR), NULL);
}
}
if (SUCCEEDED(hr))
{
bi.Size = ulFixedInfoSize + sizeof(szBarcodeTwo) - sizeof(WCHAR);
bi.Type = 2;
bi.Page = 0;
bi.Confidence = 9;
bi.XOffset = 2;
bi.YOffset = 1000;
bi.Rotation = 0;
bi.Length = (DWORD)wcslen(szBarcodeTwo);
hr = pStream->Write(&bi, ulFixedInfoSize, NULL);
if (SUCCEEDED(hr))
{
hr = pStream->Write(szBarcodeTwo, sizeof(szBarcodeTwo) - sizeof(WCHAR), NULL);
}
}
if (SUCCEEDED(hr))
{
bi.Size = ulFixedInfoSize + sizeof(szBarcodeThree) - sizeof(WCHAR);
bi.Type = 7;
bi.Page = 0;
bi.Confidence = 10;
bi.XOffset = 0;
bi.YOffset = 2000;
bi.Rotation = 0;
bi.Length = (DWORD)wcslen(szBarcodeThree);
hr = pStream->Write(&bi, ulFixedInfoSize, NULL);
if (SUCCEEDED(hr))
{
hr = pStream->Write(szBarcodeThree, sizeof(szBarcodeThree) - sizeof(WCHAR), NULL);
}
}
}
else if (IsEqualGUID(WIA_CATEGORY_PATCH_CODE_READER, guidItemCategory) && IsEqualGUID(WiaImgFmt_RAWPAT, guidFormat))
{
WIA_PATCH_CODES pc = {};
WIA_PATCH_CODE_INFO pi = {};
//
// 2 hard-coded sample patch codes to report. These sample codes match the XML sample metadata (PatchCod.xml):
//
ULONG ulFixedSize = 4 * sizeof(DWORD);
ULONG ulFixedInfoSize = sizeof(DWORD);
ulDataSize = ulFixedSize + (2 * ulFixedInfoSize);
const char szSignature[] = "WPAT";
memcpy(&pc.Tag, szSignature, sizeof(DWORD));
pc.Version = 0x00010000;
pc.Size = ulDataSize;
pc.Count = 2;
hr = pStream->Write(&pc, ulFixedSize, NULL);
if (SUCCEEDED(hr))
{
pi.Type = 2;
hr = pStream->Write(&pi, ulFixedInfoSize, NULL);
}
if (SUCCEEDED(hr))
{
pi.Type = 1;
hr = pStream->Write(&pi, ulFixedInfoSize, NULL);
}
}
else if (IsEqualGUID(WIA_CATEGORY_MICR_READER, guidItemCategory) && IsEqualGUID(WiaImgFmt_RAWMIC, guidFormat))
{
WIA_MICR micr = {};
WIA_MICR_INFO mi = {};
//
// 2 hard-coded sample MICR codes to report. These sample codes match the XML sample metadata (Micr.xml):
//
WCHAR szMicrOne[] = L"1234567890";
WCHAR szMicrTwo[] = L"987?543?10";
//
// When computing the data size take into account the following important details:
//
// WIA_MICR includes a WIA_MICR_INFO structure element as a placeholder.
// WIA_MICR_INFO includes a WCHAR pleceholder for the text.
// Make sure the size of these fields are not counted twice.
//
// Do not include the length of the NULL string terminators for the character sequences,
// they and not NULL terminated.
//
// Do not compute the fixed size (excluding the text) of a WIA_MICR_INFO structure
// doing sizeof(WIA_MICR_INFO) - sizeof(WCHAR), this may add compiler padding for
// the data structure. Instead compute the length of the fixed WIA_MICR_INFO size
// as 3 * sizeof(DWORD).
//
ULONG ulFixedSize = 5 * sizeof(DWORD); //not: sizeof(WIA_MICR - sizeof(WIA_MICR_INFO)
ULONG ulFixedInfoSize = 3 * sizeof(DWORD); //not: sizeof(WIA_MICR_INFO) - sizeof(WCHAR)
ulDataSize = ulFixedSize + (2 * ulFixedInfoSize) + sizeof(szMicrOne) + sizeof(szMicrTwo) - (2 * sizeof(WCHAR));
const char szSignature[] = "WMIC";
memcpy(&micr.Tag, szSignature, sizeof(DWORD));
micr.Version = 0x00010000;
micr.Size = ulDataSize;
micr.Count = 2;
micr.Placeholder = L'?';
hr = pStream->Write(&micr, ulFixedSize, NULL);
if (SUCCEEDED(hr))
{
mi.Size = ulFixedInfoSize + sizeof(szMicrOne) - sizeof(WCHAR);
mi.Page = 0;
mi.Length = (DWORD)wcslen(szMicrOne);
hr = pStream->Write(&mi, ulFixedInfoSize, NULL);
if (SUCCEEDED(hr))
{
hr = pStream->Write(szMicrOne, sizeof(szMicrOne) - sizeof(WCHAR), NULL);
}
}
if (SUCCEEDED(hr))
{
mi.Size = ulFixedInfoSize + sizeof(szMicrTwo) - sizeof(WCHAR);
mi.Page = 1;
mi.Length = (DWORD)wcslen(szMicrTwo);
hr = pStream->Write(&mi, ulFixedInfoSize, NULL);
if (SUCCEEDED(hr))
{
hr = pStream->Write(szMicrTwo, sizeof(szMicrTwo) - sizeof(WCHAR), NULL);
}
}
}
else
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid item - transfer format combination, hr = 0x%08X", hr));
}
}
if (SUCCEEDED(hr))
{
*pulDataSize = ulDataSize;
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
/**************************************************************************\
*
* Helper for TransferFile. Computes the transfer completion relative to the
* specified size of data written and the estimated target file size.
*
* Parameters:
*
* pulPercentComplete - percent complete value to be updated
* ulEstimatedFileSize - estimated file size, in bytes (0 if unknown)
* ulFileBytesWritten - number of bytes of data written
* bDirectWiaTransfer - TRUE if this is a full WIA transfer when the
* percent complete must be incremented from 0%
* to 99%, FALSE if this is a transfer of a file
* already read from the scanner when the percent
* complete is set from ulResumeFrom to 99% -or-
* if this is a transfer from scanner to the driver
* when the percent complete is set from 0% to 49%.
* ulResumeFrom - if bDirectWiaTransfer is FALSE this parameter
* indicates a start value between 0% and 49% for
* a transfer from the driver to the WIA app -or-
* 0 to indicate a transfer from the scanner to driver
* Return Value:
*
* None
*
\**************************************************************************/
void
CWiaDriver::ComputeTransferProgress(
_Inout_ ULONG *pulPercentComplete,
ULONG ulEstimatedFileSize,
ULONG ulFileBytesWritten,
BOOL bDirectWIATransfer,
ULONG ulResumeFrom)
{
ULONG ulStartPos = 0;
double dMax = 100.0;
ULONG ulLimit = 99;
if (!bDirectWIATransfer)
{
if (!ulResumeFrom)
{
//
// If this is an indirect (translated) transfer and the indicated
// start position is 0 we will compute the transfer complete on a
// 0% to 50% scale up to a maximum value of 49%:
//
ulStartPos = 0;
dMax = 50.0;
ulLimit = 49;
}
else
{
//
// If a non-zero value is indicated to start the progress indicator
// from we will resume incrementing the transfer complete from here:
//
if (ulResumeFrom < 50)
{
ulStartPos = ulResumeFrom + 1;
}
else
{
ulStartPos = 50;
}
dMax = 100.0 - (double)ulStartPos;
ulLimit = 99 - ulStartPos;
}
}
if (pulPercentComplete)
{
//
// Note that we do not know in advance the exact size of the file to be transferred.
// In order to allow the WIA client application to see activity (other than the
// amount of data that is being transferred with each write) we will use the estimated
// uncompressed data size (if available) or simply increment the percent complete until
// 99 or end of file transfer (and then set it to 100%):
//
if (ulEstimatedFileSize > 0)
{
*pulPercentComplete = (ULONG)(dMax * (((double)ulFileBytesWritten) / ((double)ulEstimatedFileSize)));
if ((*pulPercentComplete) > ulLimit)
{
*pulPercentComplete = ulLimit;
}
}
else
{
if ((*pulPercentComplete) < ulLimit)
{
*pulPercentComplete += 1;
}
}
if (!bDirectWIATransfer)
{
*pulPercentComplete += ulStartPos;
}
}
}
|