libusbk-sys 0.2.0

Rust Windows library for accessing USB devices via libusbK
Documentation
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
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
#Region "Copyright (c) Travis Robinson"

' Copyright (c) 2011-2021 Travis Robinson <libusbdotnet@gmail.com>
' All rights reserved.
'
' C# libusbK Bindings
' Auto-generated on: 07.08.2021
'
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions are met:
'
'     * Redistributions of source code must retain the above copyright
'       notice, this list of conditions and the following disclaimer.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
' IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
' TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
' PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TRAVIS LEE ROBINSON
' BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
' CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
' SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
' INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
' CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
' ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
' THE POSSIBILITY OF SUCH DAMAGE.

#End Region

Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices

' ReSharper disable InconsistentNaming
' ReSharper disable CheckNamespace
' ReSharper disable UnassignedReadonlyField

Namespace libusbK
    Public Module AllKOptions
        ''' <summary>
        '''     Alternate libusbK library to use.  This must be assigned before any libusbK functions are called and it must be the
        '''     full path and file name to a libusbK.dll.
        ''' </summary>
        Public LIBUSBK_FULLPATH_TO_ALTERNATE_DLL As String
    End Module

    Public Module AllKConstants
        ''' <summary>
        '''     Allocated length for all strings in a \ref KLST_DEVINFO structure.
        ''' </summary>
        Public Const KLST_STRING_MAX_LEN As Integer = 256

        ''' <summary>
        '''     libusbK library
        ''' </summary>
        Public Const LIBUSBK_DLL As String = "libusbK.dll"

        ''' <summary>
        '''     Config power mask for the \c bmAttributes field of a \ref USB_CONFIGURATION_DESCRIPTOR
        ''' </summary>
        Public Const USB_CONFIG_POWERED_MASK As Byte = &HC0

        ''' <summary>
        '''     Endpoint direction mask for the \c bEndpointAddress field of a \ref USB_ENDPOINT_DESCRIPTOR
        ''' </summary>
        Public Const USB_ENDPOINT_DIRECTION_MASK As Byte = &H80

        ''' <summary>
        '''     Endpoint address mask for the \c bEndpointAddress field of a \ref USB_ENDPOINT_DESCRIPTOR
        ''' </summary>
        Public Const USB_ENDPOINT_ADDRESS_MASK As Byte = &H0F
    End Module

    Public Enum PipePolicyType
        SHORT_PACKET_TERMINATE = &H01
        AUTO_CLEAR_STALL = &H02
        PIPE_TRANSFER_TIMEOUT = &H03
        IGNORE_SHORT_PACKETS = &H04
        ALLOW_PARTIAL_READS = &H05
        AUTO_FLUSH = &H06
        RAW_IO = &H07
        MAXIMUM_TRANSFER_SIZE = &H08
        RESET_PIPE_ON_RESUME = &H09
        ISO_START_LATENCY = &H20
        ISO_ALWAYS_START_ASAP = &H21
        ISO_NUM_FIXED_PACKETS = &H22
        SIMUL_PARALLEL_REQUESTS = &H30
    End Enum

    Public Enum PowerPolicyType
        AUTO_SUSPEND = &H81
        SUSPEND_DELAY = &H83
    End Enum

    Public Enum DeviceInformationType
        DEVICE_SPEED = &H01
    End Enum

    Public Enum EndpointType
        ''' <summary>
        '''     Indicates a control endpoint
        ''' </summary>
        CONTROL = &H00

        ''' <summary>
        '''     Indicates an isochronous endpoint
        ''' </summary>
        ISOCHRONOUS = &H01

        ''' <summary>
        '''     Indicates a bulk endpoint
        ''' </summary>
        BULK = &H02

        ''' <summary>
        '''     Indicates an interrupt endpoint
        ''' </summary>
        INTERRUPT = &H03

        ''' <summary>
        '''     Endpoint type mask for the \c bmAttributes field of a \ref USB_ENDPOINT_DESCRIPTOR
        ''' </summary>
        MASK = &H03
    End Enum

    Public Module ErrorCodes
        ''' <summary>
        '''     The operation completed successfully.
        ''' </summary>
        Public Const Success As Integer = 0

        ''' <summary>
        '''     Access is denied.
        ''' </summary>
        Public Const AccessDenied As Integer = 5

        ''' <summary>
        '''     The handle is invalid.
        ''' </summary>
        Public Const InvalidHandle As Integer = 6

        ''' <summary>
        '''     Not enough storage is available to process this command.
        ''' </summary>
        Public Const NotEnoughMemory As Integer = 8

        ''' <summary>
        '''     The request is not supported.
        ''' </summary>
        Public Const NotSupported As Integer = 50

        ''' <summary>
        '''     The parameter is incorrect.
        ''' </summary>
        Public Const InvalidParameter As Integer = 87

        ''' <summary>
        '''     The semaphore timeout period has expired.
        ''' </summary>
        Public Const SemTimeout As Integer = 121

        ''' <summary>
        '''     The requested resource is in use.
        ''' </summary>
        Public Const Busy As Integer = 170

        ''' <summary>
        '''     Too many dynamic-link modules are attached to this program or dynamic-link module.
        ''' </summary>
        Public Const TooManyModules As Integer = 214

        ''' <summary>
        '''     More data is available.
        ''' </summary>
        Public Const MoreData As Integer = 234

        ''' <summary>
        '''     No more data is available.
        ''' </summary>
        Public Const NoMoreItems As Integer = 259

        ''' <summary>
        '''     An attempt was made to operate on a thread within a specific process, but the thread specified is not in the
        '''     process specified.
        ''' </summary>
        Public Const ThreadNotInProcess As Integer = 566

        ''' <summary>
        '''     A thread termination occurred while the thread was suspended. The thread was resumed, and termination proceeded.
        ''' </summary>
        Public Const ThreadWasSuspended As Integer = 699

        ''' <summary>
        '''     The I/O operation has been aborted because of either a thread exit or an application request.
        ''' </summary>
        Public Const OperationAborted As Integer = 995

        ''' <summary>
        '''     Overlapped I/O event is not in a signaled state.
        ''' </summary>
        Public Const IoIncomplete As Integer = 996

        ''' <summary>
        '''     Overlapped I/O operation is in progress.
        ''' </summary>
        Public Const IoPending As Integer = 997

        ''' <summary>
        '''     Element not found.
        ''' </summary>
        Public Const NotFound As Integer = 1168

        ''' <summary>
        '''     The operation was canceled by the user.
        ''' </summary>
        Public Const Cancelled As Integer = 1223

        ''' <summary>
        '''     The library, drive, or media pool is empty.
        ''' </summary>
        Public Const Empty As Integer = 4306

        ''' <summary>
        '''     The cluster resource is not available.
        ''' </summary>
        Public Const ResourceNotAvailable As Integer = 5006

        ''' <summary>
        '''     The cluster resource could not be found.
        ''' </summary>
        Public Const ResourceNotFound As Integer = 5007
    End Module

    Public Interface IKLIB_HANDLE
        ReadOnly Property HandleType As KLIB_HANDLE_TYPE
        ReadOnly Property Pointer As IntPtr
        Function GetContext() As IntPtr
        Function SetContext(ByVal UserContext As IntPtr) As Boolean
        Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean
    End Interface

    ''' <Summary>
    '''     A structure representing additional information about super speed (or higher) endpoints.
    ''' </Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Structure USB_SUPERSPEED_ENDPOINT_COMPANION_DESCRIPTOR
        ''' <Summary>Size of this descriptor (in bytes)</Summary>
        Public bLength As Byte

        ''' <Summary>Descriptor type</Summary>
        Public bDescriptorType As Byte

        ''' <Summary>Specifies the maximum number of packets that the endpoint can send or receive as a part of a burst.</Summary>
        Public bMaxBurst As Byte
        Public AsUchar As Byte

        ''' <Summary>Number of bytes per interval</Summary>
        Public wBytesPerInterval As UShort

        ''' <Summary>Specifies the maximum number of streams supported by the bulk endpoint.</Summary>
        Public ReadOnly Property BulkMaxStreams As Byte
            Get
                Return AsUchar And &H1F
            End Get
        End Property

        ''' <Summary>
        '''     Specifies a zero-based number that determines the maximum number of packets (bMaxBurst * (Mult + 1)) that can
        '''     be sent to the endpoint within a service interval.
        ''' </Summary>
        Public ReadOnly Property IsoMult As Byte
            Get
                Return AsUchar And &H3
            End Get
        End Property

        Public ReadOnly Property SspCompanion As Byte
            Get
                Return AsUchar >> 7
            End Get
        End Property
    End Structure

#Region "Opaque library handles"

    Public Structure KLST_HANDLE
        Implements IKLIB_HANDLE

        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr Implements IKLIB_HANDLE.Pointer
            Get
                Return mHandlePtr
            End Get
        End Property

        Public ReadOnly Property HandleType As KLIB_HANDLE_TYPE Implements IKLIB_HANDLE.HandleType
            Get
                Return KLIB_HANDLE_TYPE.LSTK
            End Get
        End Property

        Public Function GetContext() As IntPtr Implements IKLIB_HANDLE.GetContext
            Return LibK_GetContext(mHandlePtr, HandleType)
        End Function

        Public Function SetContext(ByVal UserContext As IntPtr) As Boolean Implements IKLIB_HANDLE.SetContext
            Return LibK_SetContext(mHandlePtr, HandleType, UserContext)
        End Function

        Public Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean Implements IKLIB_HANDLE.SetCleanupCallback
            Return LibK_SetCleanupCallback(mHandlePtr, HandleType, CleanupCallback)
        End Function
    End Structure

    Public Structure KHOT_HANDLE
        Implements IKLIB_HANDLE

        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr Implements IKLIB_HANDLE.Pointer
            Get
                Return mHandlePtr
            End Get
        End Property

        Public ReadOnly Property HandleType As KLIB_HANDLE_TYPE Implements IKLIB_HANDLE.HandleType
            Get
                Return KLIB_HANDLE_TYPE.HOTK
            End Get
        End Property

        Public Function GetContext() As IntPtr Implements IKLIB_HANDLE.GetContext
            Return LibK_GetContext(mHandlePtr, HandleType)
        End Function

        Public Function SetContext(ByVal UserContext As IntPtr) As Boolean Implements IKLIB_HANDLE.SetContext
            Return LibK_SetContext(mHandlePtr, HandleType, UserContext)
        End Function

        Public Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean Implements IKLIB_HANDLE.SetCleanupCallback
            Return LibK_SetCleanupCallback(mHandlePtr, HandleType, CleanupCallback)
        End Function
    End Structure

    Public Structure KUSB_HANDLE
        Implements IKLIB_HANDLE

        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr Implements IKLIB_HANDLE.Pointer
            Get
                Return mHandlePtr
            End Get
        End Property

        Public ReadOnly Property HandleType As KLIB_HANDLE_TYPE Implements IKLIB_HANDLE.HandleType
            Get
                Return KLIB_HANDLE_TYPE.USBK
            End Get
        End Property

        Public Function GetContext() As IntPtr Implements IKLIB_HANDLE.GetContext
            Return LibK_GetContext(mHandlePtr, HandleType)
        End Function

        Public Function SetContext(ByVal UserContext As IntPtr) As Boolean Implements IKLIB_HANDLE.SetContext
            Return LibK_SetContext(mHandlePtr, HandleType, UserContext)
        End Function

        Public Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean Implements IKLIB_HANDLE.SetCleanupCallback
            Return LibK_SetCleanupCallback(mHandlePtr, HandleType, CleanupCallback)
        End Function

#Region "USB Shared Device Context"

        Public Function GetSharedContext() As IntPtr
            Return LibK_GetContext(mHandlePtr, KLIB_HANDLE_TYPE.USBSHAREDK)
        End Function

        Public Function SetSharedContext(ByVal UserContext As IntPtr) As Boolean
            Return LibK_SetContext(mHandlePtr, KLIB_HANDLE_TYPE.USBSHAREDK, UserContext)
        End Function

        Public Function SetSharedCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean
            Return LibK_SetCleanupCallback(mHandlePtr, KLIB_HANDLE_TYPE.USBSHAREDK, CleanupCallback)
        End Function

#End Region
    End Structure

    Public Structure KOVL_POOL_HANDLE
        Implements IKLIB_HANDLE

        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr Implements IKLIB_HANDLE.Pointer
            Get
                Return mHandlePtr
            End Get
        End Property

        Public ReadOnly Property HandleType As KLIB_HANDLE_TYPE Implements IKLIB_HANDLE.HandleType
            Get
                Return KLIB_HANDLE_TYPE.OVLPOOLK
            End Get
        End Property

        Public Function GetContext() As IntPtr Implements IKLIB_HANDLE.GetContext
            Return LibK_GetContext(mHandlePtr, HandleType)
        End Function

        Public Function SetContext(ByVal UserContext As IntPtr) As Boolean Implements IKLIB_HANDLE.SetContext
            Return LibK_SetContext(mHandlePtr, HandleType, UserContext)
        End Function

        Public Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean Implements IKLIB_HANDLE.SetCleanupCallback
            Return LibK_SetCleanupCallback(mHandlePtr, HandleType, CleanupCallback)
        End Function
    End Structure

    Public Structure KOVL_HANDLE
        Implements IKLIB_HANDLE

        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr Implements IKLIB_HANDLE.Pointer
            Get
                Return mHandlePtr
            End Get
        End Property

        Public ReadOnly Property HandleType As KLIB_HANDLE_TYPE Implements IKLIB_HANDLE.HandleType
            Get
                Return KLIB_HANDLE_TYPE.OVLK
            End Get
        End Property

        Public Function GetContext() As IntPtr Implements IKLIB_HANDLE.GetContext
            Return LibK_GetContext(mHandlePtr, HandleType)
        End Function

        Public Function SetContext(ByVal UserContext As IntPtr) As Boolean Implements IKLIB_HANDLE.SetContext
            Return LibK_SetContext(mHandlePtr, HandleType, UserContext)
        End Function

        Public Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean Implements IKLIB_HANDLE.SetCleanupCallback
            Return LibK_SetCleanupCallback(mHandlePtr, HandleType, CleanupCallback)
        End Function
    End Structure

    Public Structure KSTM_HANDLE
        Implements IKLIB_HANDLE

        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr Implements IKLIB_HANDLE.Pointer
            Get
                Return mHandlePtr
            End Get
        End Property

        Public ReadOnly Property HandleType As KLIB_HANDLE_TYPE Implements IKLIB_HANDLE.HandleType
            Get
                Return KLIB_HANDLE_TYPE.STMK
            End Get
        End Property

        Public Function GetContext() As IntPtr Implements IKLIB_HANDLE.GetContext
            Return LibK_GetContext(mHandlePtr, HandleType)
        End Function

        Public Function SetContext(ByVal UserContext As IntPtr) As Boolean Implements IKLIB_HANDLE.SetContext
            Return LibK_SetContext(mHandlePtr, HandleType, UserContext)
        End Function

        Public Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean Implements IKLIB_HANDLE.SetCleanupCallback
            Return LibK_SetCleanupCallback(mHandlePtr, HandleType, CleanupCallback)
        End Function
    End Structure

    Public Structure KISOCH_HANDLE
        Implements IKLIB_HANDLE

        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr Implements IKLIB_HANDLE.Pointer
            Get
                Return mHandlePtr
            End Get
        End Property

        Public ReadOnly Property HandleType As KLIB_HANDLE_TYPE Implements IKLIB_HANDLE.HandleType
            Get
                Return KLIB_HANDLE_TYPE.ISOCHK
            End Get
        End Property

        Public Function GetContext() As IntPtr Implements IKLIB_HANDLE.GetContext
            Return LibK_GetContext(mHandlePtr, HandleType)
        End Function

        Public Function SetContext(ByVal UserContext As IntPtr) As Boolean Implements IKLIB_HANDLE.SetContext
            Return LibK_SetContext(mHandlePtr, HandleType, UserContext)
        End Function

        Public Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean Implements IKLIB_HANDLE.SetCleanupCallback
            Return LibK_SetCleanupCallback(mHandlePtr, HandleType, CleanupCallback)
        End Function
    End Structure

#End Region

#Region "Internal Function Imports"

    Friend Module AllKFunctions
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Sub HotK_FreeAllDelegate()
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function HotK_FreeDelegate(
        <[In]> ByVal Handle As KHOT_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function HotK_InitDelegate(<Out> ByRef Handle As KHOT_HANDLE,
        <[In]>
        <Out> ByRef InitParams As KHOT_PARAMS) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_CalcPacketInformationDelegate(ByVal IsHighSpeed As Boolean,
        <[In]> ByRef PipeInformationEx As WINUSB_PIPE_INFORMATION_EX, <Out> ByRef PacketInformation As KISOCH_PACKET_INFORMATION) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_EnumPacketsDelegate(
        <[In]> ByVal IsochHandle As KISOCH_HANDLE, ByVal EnumPackets As KISOCH_ENUM_PACKETS_CB, ByVal StartPacketIndex As UInteger, ByVal UserState As IntPtr) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_FreeDelegate(
        <[In]> ByVal IsochHandle As KISOCH_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_GetNumberOfPacketsDelegate(
        <[In]> ByVal IsochHandle As KISOCH_HANDLE, <Out> ByRef NumberOfPackets As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_GetPacketDelegate(
        <[In]> ByVal IsochHandle As KISOCH_HANDLE, ByVal PacketIndex As UInteger, <Out> ByRef Offset As UInteger, <Out> ByRef Length As UInteger, <Out> ByRef Status As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_InitDelegate(<Out> ByRef IsochHandle As KISOCH_HANDLE,
        <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeId As Byte, ByVal MaxNumberOfPackets As UInteger, ByVal TransferBuffer As IntPtr, ByVal TransferBufferSize As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_SetNumberOfPacketsDelegate(
        <[In]> ByVal IsochHandle As KISOCH_HANDLE, ByVal NumberOfPackets As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_SetPacketDelegate(
        <[In]> ByVal IsochHandle As KISOCH_HANDLE, ByVal PacketIndex As UInteger, ByVal Offset As UInteger, ByVal Length As UInteger, ByVal Status As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsochK_SetPacketOffsetsDelegate(
        <[In]> ByVal IsochHandle As KISOCH_HANDLE, ByVal PacketSize As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsoK_EnumPacketsDelegate(
        <[In]> ByVal IsoContext As KISO_CONTEXT, ByVal EnumPackets As KISO_ENUM_PACKETS_CB, ByVal StartPacketIndex As Integer, ByVal UserState As IntPtr) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsoK_FreeDelegate(
        <[In]> ByVal IsoContext As KISO_CONTEXT) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsoK_GetPacketDelegate(
        <[In]> ByVal IsoContext As KISO_CONTEXT, ByVal PacketIndex As Integer, <Out> ByRef IsoPacket As KISO_PACKET) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsoK_InitDelegate(<Out> ByRef IsoContext As KISO_CONTEXT, ByVal NumberOfPackets As Integer, ByVal StartFrame As Integer) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsoK_ReUseDelegate(
        <[In]> ByVal IsoContext As KISO_CONTEXT) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsoK_SetPacketDelegate(
        <[In]> ByVal IsoContext As KISO_CONTEXT, ByVal PacketIndex As Integer,
        <[In]> ByRef IsoPacket As KISO_PACKET) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function IsoK_SetPacketsDelegate(
        <[In]> ByVal IsoContext As KISO_CONTEXT, ByVal PacketSize As Integer) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Sub LibK_Context_FreeDelegate()
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_Context_InitDelegate(ByVal Heap As IntPtr, ByVal Reserved As IntPtr) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_CopyDriverAPIDelegate(<Out> ByRef DriverAPI As KUSB_DRIVER_API,
        <[In]> ByVal UsbHandle As KUSB_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_GetContextDelegate(
        <[In]> ByVal Handle As IntPtr, ByVal HandleType As KLIB_HANDLE_TYPE) As IntPtr
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_GetDefaultContextDelegate(ByVal HandleType As KLIB_HANDLE_TYPE) As IntPtr
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_GetProcAddressDelegate(ByVal ProcAddress As IntPtr, ByVal DriverID As Integer, ByVal FunctionID As Integer) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Sub LibK_GetVersionDelegate(<Out> ByRef Version As KLIB_VERSION)
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_IsFunctionSupportedDelegate(
        <[In]> ByRef DriverAPI As KUSB_DRIVER_API, ByVal FunctionID As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_LoadDriverAPIDelegate(<Out> ByRef DriverAPI As KUSB_DRIVER_API, ByVal DriverID As Integer) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_SetCleanupCallbackDelegate(
        <[In]> ByVal Handle As IntPtr, ByVal HandleType As KLIB_HANDLE_TYPE, ByVal CleanupCB As KLIB_HANDLE_CLEANUP_CB) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_SetContextDelegate(
        <[In]> ByVal Handle As IntPtr, ByVal HandleType As KLIB_HANDLE_TYPE, ByVal ContextValue As IntPtr) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LibK_SetDefaultContextDelegate(ByVal HandleType As KLIB_HANDLE_TYPE, ByVal ContextValue As IntPtr) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LstK_CountDelegate(
        <[In]> ByVal DeviceList As KLST_HANDLE, ByRef Count As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LstK_CurrentDelegate(
        <[In]> ByVal DeviceList As KLST_HANDLE, <Out> ByRef DeviceInfo As KLST_DEVINFO_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LstK_EnumerateDelegate(
        <[In]> ByVal DeviceList As KLST_HANDLE, ByVal EnumDevListCB As KLST_ENUM_DEVINFO_CB, ByVal Context As IntPtr) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LstK_FindByVidPidDelegate(
        <[In]> ByVal DeviceList As KLST_HANDLE, ByVal Vid As Integer, ByVal Pid As Integer, <Out> ByRef DeviceInfo As KLST_DEVINFO_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LstK_FreeDelegate(
        <[In]> ByVal DeviceList As KLST_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LstK_InitDelegate(<Out> ByRef DeviceList As KLST_HANDLE, ByVal Flags As KLST_FLAG) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LstK_InitExDelegate(<Out> ByRef DeviceList As KLST_HANDLE, ByVal Flags As KLST_FLAG,
        <[In]> ByRef PatternMatch As KLST_PATTERN_MATCH) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LstK_MoveNextDelegate(
        <[In]> ByVal DeviceList As KLST_HANDLE, <Out> ByRef DeviceInfo As KLST_DEVINFO_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Sub LstK_MoveResetDelegate(
        <[In]> ByVal DeviceList As KLST_HANDLE)
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LUsb0_ControlTransferDelegate(
        <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal SetupPacket As WINUSB_SETUP_PACKET, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function LUsb0_SetConfigurationDelegate(
        <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal ConfigurationNumber As Byte) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_AcquireDelegate(<Out> ByRef OverlappedK As KOVL_HANDLE,
        <[In]> ByVal PoolHandle As KOVL_POOL_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_FreeDelegate(
        <[In]> ByVal PoolHandle As KOVL_POOL_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_GetEventHandleDelegate(
        <[In]> ByVal OverlappedK As KOVL_HANDLE) As IntPtr
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_InitDelegate(<Out> ByRef PoolHandle As KOVL_POOL_HANDLE,
        <[In]> ByVal UsbHandle As KUSB_HANDLE, ByVal MaxOverlappedCount As Integer, ByVal Flags As KOVL_POOL_FLAG) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_IsCompleteDelegate(
        <[In]> ByVal OverlappedK As KOVL_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_ReleaseDelegate(
        <[In]> ByVal OverlappedK As KOVL_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_ReUseDelegate(
        <[In]> ByVal OverlappedK As KOVL_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_WaitAndReleaseDelegate(
        <[In]> ByVal OverlappedK As KOVL_HANDLE, ByVal TimeoutMS As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_WaitDelegate(
        <[In]> ByVal OverlappedK As KOVL_HANDLE, ByVal TimeoutMS As Integer, ByVal WaitFlags As KOVL_WAIT_FLAG, <Out> ByRef TransferredLength As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_WaitOldestDelegate(
        <[In]> ByVal PoolHandle As KOVL_POOL_HANDLE, <Out> ByRef OverlappedK As KOVL_HANDLE, ByVal TimeoutMS As Integer, ByVal WaitFlags As KOVL_WAIT_FLAG, <Out> ByRef TransferredLength As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function OvlK_WaitOrCancelDelegate(
        <[In]> ByVal OverlappedK As KOVL_HANDLE, ByVal TimeoutMS As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function StmK_FreeDelegate(
        <[In]> ByVal StreamHandle As KSTM_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function StmK_InitDelegate(<Out> ByRef StreamHandle As KSTM_HANDLE,
        <[In]> ByVal UsbHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal MaxTransferSize As Integer, ByVal MaxPendingTransfers As Integer, ByVal MaxPendingIO As Integer,
        <[In]> ByRef Callbacks As KSTM_CALLBACK, ByVal Flags As KSTM_FLAG) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function StmK_ReadDelegate(
        <[In]> ByVal StreamHandle As KSTM_HANDLE, ByVal Buffer As IntPtr, ByVal Offset As Integer, ByVal Length As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function StmK_StartDelegate(
        <[In]> ByVal StreamHandle As KSTM_HANDLE) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function StmK_StopDelegate(
        <[In]> ByVal StreamHandle As KSTM_HANDLE, ByVal TimeoutCancelMS As Integer) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function StmK_WriteDelegate(
        <[In]> ByVal StreamHandle As KSTM_HANDLE, ByVal Buffer As IntPtr, ByVal Offset As Integer, ByVal Length As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
        <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
        Public Delegate Function UsbK_FreeDelegate(
        <[In]> ByVal InterfaceHandle As KUSB_HANDLE) As Boolean
        Private ReadOnly mModuleLibusbK As IntPtr = IntPtr.Zero
        Public LibK_GetVersion As LibK_GetVersionDelegate
        Public LibK_GetContext As LibK_GetContextDelegate
        Public LibK_SetContext As LibK_SetContextDelegate
        Public LibK_SetCleanupCallback As LibK_SetCleanupCallbackDelegate
        Public LibK_LoadDriverAPI As LibK_LoadDriverAPIDelegate
        Public LibK_IsFunctionSupported As LibK_IsFunctionSupportedDelegate
        Public LibK_CopyDriverAPI As LibK_CopyDriverAPIDelegate
        Public LibK_GetProcAddress As LibK_GetProcAddressDelegate
        Public LibK_SetDefaultContext As LibK_SetDefaultContextDelegate
        Public LibK_GetDefaultContext As LibK_GetDefaultContextDelegate
        Public LibK_Context_Init As LibK_Context_InitDelegate
        Public LibK_Context_Free As LibK_Context_FreeDelegate
        Public UsbK_Free As UsbK_FreeDelegate
        Public LstK_Init As LstK_InitDelegate
        Public LstK_InitEx As LstK_InitExDelegate
        Public LstK_Free As LstK_FreeDelegate
        Public LstK_Enumerate As LstK_EnumerateDelegate
        Public LstK_Current As LstK_CurrentDelegate
        Public LstK_MoveNext As LstK_MoveNextDelegate
        Public LstK_MoveReset As LstK_MoveResetDelegate
        Public LstK_FindByVidPid As LstK_FindByVidPidDelegate
        Public LstK_Count As LstK_CountDelegate
        Public HotK_Init As HotK_InitDelegate
        Public HotK_Free As HotK_FreeDelegate
        Public HotK_FreeAll As HotK_FreeAllDelegate
        Public OvlK_Acquire As OvlK_AcquireDelegate
        Public OvlK_Release As OvlK_ReleaseDelegate
        Public OvlK_Init As OvlK_InitDelegate
        Public OvlK_Free As OvlK_FreeDelegate
        Public OvlK_GetEventHandle As OvlK_GetEventHandleDelegate
        Public OvlK_Wait As OvlK_WaitDelegate
        Public OvlK_WaitOldest As OvlK_WaitOldestDelegate
        Public OvlK_WaitOrCancel As OvlK_WaitOrCancelDelegate
        Public OvlK_WaitAndRelease As OvlK_WaitAndReleaseDelegate
        Public OvlK_IsComplete As OvlK_IsCompleteDelegate
        Public OvlK_ReUse As OvlK_ReUseDelegate
        Public StmK_Init As StmK_InitDelegate
        Public StmK_Free As StmK_FreeDelegate
        Public StmK_Start As StmK_StartDelegate
        Public StmK_Stop As StmK_StopDelegate
        Public StmK_Read As StmK_ReadDelegate
        Public StmK_Write As StmK_WriteDelegate
        Public IsoK_Init As IsoK_InitDelegate
        Public IsoK_Free As IsoK_FreeDelegate
        Public IsoK_SetPackets As IsoK_SetPacketsDelegate
        Public IsoK_SetPacket As IsoK_SetPacketDelegate
        Public IsoK_GetPacket As IsoK_GetPacketDelegate
        Public IsoK_EnumPackets As IsoK_EnumPacketsDelegate
        Public IsoK_ReUse As IsoK_ReUseDelegate
        Public IsochK_Init As IsochK_InitDelegate
        Public IsochK_Free As IsochK_FreeDelegate
        Public IsochK_SetPacketOffsets As IsochK_SetPacketOffsetsDelegate
        Public IsochK_SetPacket As IsochK_SetPacketDelegate
        Public IsochK_GetPacket As IsochK_GetPacketDelegate
        Public IsochK_EnumPackets As IsochK_EnumPacketsDelegate
        Public IsochK_CalcPacketInformation As IsochK_CalcPacketInformationDelegate
        Public IsochK_GetNumberOfPackets As IsochK_GetNumberOfPacketsDelegate
        Public IsochK_SetNumberOfPackets As IsochK_SetNumberOfPacketsDelegate
        Public LUsb0_ControlTransfer As LUsb0_ControlTransferDelegate
        Public LUsb0_SetConfiguration As LUsb0_SetConfigurationDelegate

        Sub New()
            If String.IsNullOrEmpty(LIBUSBK_FULLPATH_TO_ALTERNATE_DLL) Then
                mModuleLibusbK = LoadLibraryEx(LIBUSBK_DLL, IntPtr.Zero, LoadLibraryFlags.NONE)
            Else
                mModuleLibusbK = LoadLibraryEx(LIBUSBK_FULLPATH_TO_ALTERNATE_DLL, IntPtr.Zero, LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH)
            End If

            If mModuleLibusbK = IntPtr.Zero Then Throw New DllNotFoundException("libusbK.dll not found.  Please install drivers/applications and retry.")
            Call LoadDynamicFunctions()
        End Sub

        <DllImport("kernel32.dll")>
        Private Function LoadLibraryEx(ByVal lpFileName As String, ByVal hReservedNull As IntPtr, ByVal dwFlags As LoadLibraryFlags) As IntPtr
        End Function

        <DllImport("kernel32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True, SetLastError:=True)>
        Private Function GetProcAddress(ByVal hModule As IntPtr, ByVal procName As String) As IntPtr
        End Function

        Private Sub LoadDynamicFunctions()
            LibK_GetVersion = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_GetVersion"), GetType(LibK_GetVersionDelegate)), LibK_GetVersionDelegate)
            LibK_GetContext = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_GetContext"), GetType(LibK_GetContextDelegate)), LibK_GetContextDelegate)
            LibK_SetContext = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_SetContext"), GetType(LibK_SetContextDelegate)), LibK_SetContextDelegate)
            LibK_SetCleanupCallback = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_SetCleanupCallback"), GetType(LibK_SetCleanupCallbackDelegate)), LibK_SetCleanupCallbackDelegate)
            LibK_LoadDriverAPI = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_LoadDriverAPI"), GetType(LibK_LoadDriverAPIDelegate)), LibK_LoadDriverAPIDelegate)
            LibK_IsFunctionSupported = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_IsFunctionSupported"), GetType(LibK_IsFunctionSupportedDelegate)), LibK_IsFunctionSupportedDelegate)
            LibK_CopyDriverAPI = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_CopyDriverAPI"), GetType(LibK_CopyDriverAPIDelegate)), LibK_CopyDriverAPIDelegate)
            LibK_GetProcAddress = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_GetProcAddress"), GetType(LibK_GetProcAddressDelegate)), LibK_GetProcAddressDelegate)
            LibK_SetDefaultContext = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_SetDefaultContext"), GetType(LibK_SetDefaultContextDelegate)), LibK_SetDefaultContextDelegate)
            LibK_GetDefaultContext = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_GetDefaultContext"), GetType(LibK_GetDefaultContextDelegate)), LibK_GetDefaultContextDelegate)
            LibK_Context_Init = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_Context_Init"), GetType(LibK_Context_InitDelegate)), LibK_Context_InitDelegate)
            LibK_Context_Free = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LibK_Context_Free"), GetType(LibK_Context_FreeDelegate)), LibK_Context_FreeDelegate)
            UsbK_Free = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "UsbK_Free"), GetType(UsbK_FreeDelegate)), UsbK_FreeDelegate)
            LstK_Init = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_Init"), GetType(LstK_InitDelegate)), LstK_InitDelegate)
            LstK_InitEx = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_InitEx"), GetType(LstK_InitExDelegate)), LstK_InitExDelegate)
            LstK_Free = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_Free"), GetType(LstK_FreeDelegate)), LstK_FreeDelegate)
            LstK_Enumerate = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_Enumerate"), GetType(LstK_EnumerateDelegate)), LstK_EnumerateDelegate)
            LstK_Current = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_Current"), GetType(LstK_CurrentDelegate)), LstK_CurrentDelegate)
            LstK_MoveNext = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_MoveNext"), GetType(LstK_MoveNextDelegate)), LstK_MoveNextDelegate)
            LstK_MoveReset = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_MoveReset"), GetType(LstK_MoveResetDelegate)), LstK_MoveResetDelegate)
            LstK_FindByVidPid = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_FindByVidPid"), GetType(LstK_FindByVidPidDelegate)), LstK_FindByVidPidDelegate)
            LstK_Count = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LstK_Count"), GetType(LstK_CountDelegate)), LstK_CountDelegate)
            HotK_Init = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "HotK_Init"), GetType(HotK_InitDelegate)), HotK_InitDelegate)
            HotK_Free = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "HotK_Free"), GetType(HotK_FreeDelegate)), HotK_FreeDelegate)
            HotK_FreeAll = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "HotK_FreeAll"), GetType(HotK_FreeAllDelegate)), HotK_FreeAllDelegate)
            OvlK_Acquire = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_Acquire"), GetType(OvlK_AcquireDelegate)), OvlK_AcquireDelegate)
            OvlK_Release = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_Release"), GetType(OvlK_ReleaseDelegate)), OvlK_ReleaseDelegate)
            OvlK_Init = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_Init"), GetType(OvlK_InitDelegate)), OvlK_InitDelegate)
            OvlK_Free = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_Free"), GetType(OvlK_FreeDelegate)), OvlK_FreeDelegate)
            OvlK_GetEventHandle = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_GetEventHandle"), GetType(OvlK_GetEventHandleDelegate)), OvlK_GetEventHandleDelegate)
            OvlK_Wait = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_Wait"), GetType(OvlK_WaitDelegate)), OvlK_WaitDelegate)
            OvlK_WaitOldest = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_WaitOldest"), GetType(OvlK_WaitOldestDelegate)), OvlK_WaitOldestDelegate)
            OvlK_WaitOrCancel = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_WaitOrCancel"), GetType(OvlK_WaitOrCancelDelegate)), OvlK_WaitOrCancelDelegate)
            OvlK_WaitAndRelease = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_WaitAndRelease"), GetType(OvlK_WaitAndReleaseDelegate)), OvlK_WaitAndReleaseDelegate)
            OvlK_IsComplete = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_IsComplete"), GetType(OvlK_IsCompleteDelegate)), OvlK_IsCompleteDelegate)
            OvlK_ReUse = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "OvlK_ReUse"), GetType(OvlK_ReUseDelegate)), OvlK_ReUseDelegate)
            StmK_Init = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "StmK_Init"), GetType(StmK_InitDelegate)), StmK_InitDelegate)
            StmK_Free = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "StmK_Free"), GetType(StmK_FreeDelegate)), StmK_FreeDelegate)
            StmK_Start = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "StmK_Start"), GetType(StmK_StartDelegate)), StmK_StartDelegate)
            StmK_Stop = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "StmK_Stop"), GetType(StmK_StopDelegate)), StmK_StopDelegate)
            StmK_Read = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "StmK_Read"), GetType(StmK_ReadDelegate)), StmK_ReadDelegate)
            StmK_Write = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "StmK_Write"), GetType(StmK_WriteDelegate)), StmK_WriteDelegate)
            IsoK_Init = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsoK_Init"), GetType(IsoK_InitDelegate)), IsoK_InitDelegate)
            IsoK_Free = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsoK_Free"), GetType(IsoK_FreeDelegate)), IsoK_FreeDelegate)
            IsoK_SetPackets = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsoK_SetPackets"), GetType(IsoK_SetPacketsDelegate)), IsoK_SetPacketsDelegate)
            IsoK_SetPacket = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsoK_SetPacket"), GetType(IsoK_SetPacketDelegate)), IsoK_SetPacketDelegate)
            IsoK_GetPacket = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsoK_GetPacket"), GetType(IsoK_GetPacketDelegate)), IsoK_GetPacketDelegate)
            IsoK_EnumPackets = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsoK_EnumPackets"), GetType(IsoK_EnumPacketsDelegate)), IsoK_EnumPacketsDelegate)
            IsoK_ReUse = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsoK_ReUse"), GetType(IsoK_ReUseDelegate)), IsoK_ReUseDelegate)
            IsochK_Init = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_Init"), GetType(IsochK_InitDelegate)), IsochK_InitDelegate)
            IsochK_Free = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_Free"), GetType(IsochK_FreeDelegate)), IsochK_FreeDelegate)
            IsochK_SetPacketOffsets = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_SetPacketOffsets"), GetType(IsochK_SetPacketOffsetsDelegate)), IsochK_SetPacketOffsetsDelegate)
            IsochK_SetPacket = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_SetPacket"), GetType(IsochK_SetPacketDelegate)), IsochK_SetPacketDelegate)
            IsochK_GetPacket = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_GetPacket"), GetType(IsochK_GetPacketDelegate)), IsochK_GetPacketDelegate)
            IsochK_EnumPackets = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_EnumPackets"), GetType(IsochK_EnumPacketsDelegate)), IsochK_EnumPacketsDelegate)
            IsochK_CalcPacketInformation = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_CalcPacketInformation"), GetType(IsochK_CalcPacketInformationDelegate)), IsochK_CalcPacketInformationDelegate)
            IsochK_GetNumberOfPackets = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_GetNumberOfPackets"), GetType(IsochK_GetNumberOfPacketsDelegate)), IsochK_GetNumberOfPacketsDelegate)
            IsochK_SetNumberOfPackets = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "IsochK_SetNumberOfPackets"), GetType(IsochK_SetNumberOfPacketsDelegate)), IsochK_SetNumberOfPacketsDelegate)
            LUsb0_ControlTransfer = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LUsb0_ControlTransfer"), GetType(LUsb0_ControlTransferDelegate)), LUsb0_ControlTransferDelegate)
            LUsb0_SetConfiguration = CType(Marshal.GetDelegateForFunctionPointer(GetProcAddress(mModuleLibusbK, "LUsb0_SetConfiguration"), GetType(LUsb0_SetConfigurationDelegate)), LUsb0_SetConfigurationDelegate)
        End Sub

        <Flags>
        Friend Enum LoadLibraryFlags
            NONE = 0
            DONT_RESOLVE_DLL_REFERENCES = &H00000001
            LOAD_IGNORE_CODE_AUTHZ_LEVEL = &H00000010
            LOAD_LIBRARY_AS_DATAFILE = &H00000002
            LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = &H00000040
            LOAD_LIBRARY_AS_IMAGE_RESOURCE = &H00000020
            LOAD_WITH_ALTERED_SEARCH_PATH = &H00000008
        End Enum
    End Module

#End Region

#Region "Enumerations"

    ''' <Summary>Values used in the \c bmAttributes field of a \ref USB_ENDPOINT_DESCRIPTOR</Summary>
    Public Enum USBD_PIPE_TYPE
        ''' <Summary>Indicates a control endpoint</Summary>
        UsbdPipeTypeControl

        ''' <Summary>Indicates an isochronous endpoint</Summary>
        UsbdPipeTypeIsochronous

        ''' <Summary>Indicates a bulk endpoint</Summary>
        UsbdPipeTypeBulk

        ''' <Summary>Indicates an interrupt endpoint</Summary>
        UsbdPipeTypeInterrupt
    End Enum

    ''' <Summary>Additional ISO transfer flags.</Summary>
    <Flags>
    Public Enum KISO_FLAG
        NONE = 0

        ''' <Summary>Do not start the transfer immediately, instead use \ref KISO_CONTEXT::StartFrame.</Summary>
        SET_START_FRAME = &H00000001
    End Enum

    ''' <Summary>Handle type enumeration.</Summary>
    Public Enum KLIB_HANDLE_TYPE
        ''' <Summary>Hot plug handle. \ref KHOT_HANDLE</Summary>
        HOTK

        ''' <Summary>USB handle. \ref KUSB_HANDLE</Summary>
        USBK

        ''' <Summary>Shared USB handle. \ref KUSB_HANDLE</Summary>
        USBSHAREDK

        ''' <Summary>Device list handle. \ref KLST_HANDLE</Summary>
        LSTK

        ''' <Summary>Device info handle. \ref KLST_DEVINFO_HANDLE</Summary>
        LSTINFOK

        ''' <Summary>Overlapped handle. \ref KOVL_HANDLE</Summary>
        OVLK

        ''' <Summary>Overlapped pool handle. \ref KOVL_POOL_HANDLE</Summary>
        OVLPOOLK

        ''' <Summary>Pipe stream handle. \ref KSTM_HANDLE</Summary>
        STMK

        ''' <Summary>Pipe stream handle. \ref KSTM_HANDLE</Summary>
        ISOCHK

        ''' <Summary>Max handle type count.</Summary>
        COUNT
    End Enum

    ''' <Summary>Device list sync flags.</Summary>
    <Flags>
    Public Enum KLST_SYNC_FLAG
        ''' <Summary>Cleared/invalid state.</Summary>
        NONE = 0

        ''' <Summary>Unchanged state,</Summary>
        UNCHANGED = &H0001

        ''' <Summary>Added (Arrival) state,</Summary>
        ADDED = &H0002

        ''' <Summary>Removed (Unplugged) state,</Summary>
        REMOVED = &H0004

        ''' <Summary>Connect changed state.</Summary>
        CONNECT_CHANGE = &H0008

        ''' <Summary>All states.</Summary>
        MASK = &H000F
    End Enum

    ''' <Summary>Device list initialization flags.</Summary>
    <Flags>
    Public Enum KLST_FLAG
        ''' <Summary>No flags (or 0)</Summary>
        NONE = 0

        ''' <Summary>Enable listings for the raw device interface GUID \b only. {A5DCBF10-6530-11D2-901F-00C04FB951ED}</Summary>
        INCLUDE_RAWGUID = &H0001

        ''' <Summary>List all libusbK devices including those not currently connected.</Summary>
        INCLUDE_DISCONNECT = &H0002
    End Enum

    ''' <Summary>bmRequest.Dir</Summary>
    Public Enum BMREQUEST_DIR
        HOST_TO_DEVICE = 0
        DEVICE_TO_HOST = 1
    End Enum

    ''' <Summary>bmRequest.Type</Summary>
    Public Enum BMREQUEST_TYPE
        ''' <Summary>Standard request. See \ref USB_REQUEST_ENUM</Summary>
        STANDARD = 0

        ''' <Summary>Class-specific request.</Summary>
        [CLASS] = 1

        ''' <Summary>Vendor-specific request</Summary>
        VENDOR = 2
    End Enum

    ''' <Summary>bmRequest.Recipient</Summary>
    Public Enum BMREQUEST_RECIPIENT
        ''' <Summary>Request is for a device.</Summary>
        DEVICE = 0

        ''' <Summary>Request is for an interface of a device.</Summary>
        [INTERFACE] = 1

        ''' <Summary>Request is for an endpoint of a device.</Summary>
        ENDPOINT = 2

        ''' <Summary>Request is for a vendor-specific purpose.</Summary>
        OTHER = 3
    End Enum

    ''' <Summary>Values for the bits returned by the \ref USB_REQUEST_GET_STATUS request.</Summary>
    Public Enum USB_GETSTATUS
        ''' <Summary>Device is self powered</Summary>
        SELF_POWERED = &H01

        ''' <Summary>Device can wake the system from a low power/sleeping state.</Summary>
        REMOTE_WAKEUP_ENABLED = &H02
    End Enum

    ''' <Summary>Standard USB descriptor types. For more information, see section 9-5 of the USB 3.0 specifications.</Summary>
    Public Enum USB_DESCRIPTOR_TYPE
        ''' <Summary>Device descriptor type.</Summary>
        DEVICE = &H01

        ''' <Summary>Configuration descriptor type.</Summary>
        CONFIGURATION = &H02

        ''' <Summary>String descriptor type.</Summary>
        [STRING] = &H03

        ''' <Summary>Interface descriptor type.</Summary>
        [INTERFACE] = &H04

        ''' <Summary>Endpoint descriptor type.</Summary>
        ENDPOINT = &H05

        ''' <Summary>Device qualifier descriptor type.</Summary>
        DEVICE_QUALIFIER = &H06

        ''' <Summary>Config power descriptor type.</Summary>
        CONFIG_POWER = &H07

        ''' <Summary>Interface power descriptor type.</Summary>
        INTERFACE_POWER = &H08

        ''' <Summary>Interface association descriptor type.</Summary>
        INTERFACE_ASSOCIATION = &H0B
        USB_SUPERSPEED_ENDPOINT_COMPANION = &H30
    End Enum

    ''' <Summary>Usb handle specific properties that can be retrieved with \ref UsbK_GetProperty.</Summary>
    Public Enum KUSB_PROPERTY
        ''' <Summary>Get the internal device file handle used for operations such as GetOverlappedResult or DeviceIoControl.</Summary>
        DEVICE_FILE_HANDLE
        COUNT
    End Enum

    ''' <Summary>Supported driver id enumeration.</Summary>
    Public Enum KUSB_DRVID
        ''' <Summary>libusbK.sys driver ID</Summary>
        LIBUSBK

        ''' <Summary>libusb0.sys driver ID</Summary>
        LIBUSB0

        ''' <Summary>WinUSB.sys driver ID</Summary>
        WINUSB

        ''' <Summary>libusb0.sys filter driver ID</Summary>
        LIBUSB0_FILTER

        ''' <Summary>Supported driver count</Summary>
        COUNT
    End Enum

    ''' <Summary>Supported function id enumeration.</Summary>
    Public Enum KUSB_FNID
        ''' <Summary>\ref UsbK_Init dynamic driver function id.</Summary>
        Init

        ''' <Summary>\ref UsbK_Free dynamic driver function id.</Summary>
        Free

        ''' <Summary>\ref UsbK_ClaimInterface dynamic driver function id.</Summary>
        ClaimInterface

        ''' <Summary>\ref UsbK_ReleaseInterface dynamic driver function id.</Summary>
        ReleaseInterface

        ''' <Summary>\ref UsbK_SetAltInterface dynamic driver function id.</Summary>
        SetAltInterface

        ''' <Summary>\ref UsbK_GetAltInterface dynamic driver function id.</Summary>
        GetAltInterface

        ''' <Summary>\ref UsbK_GetDescriptor dynamic driver function id.</Summary>
        GetDescriptor

        ''' <Summary>\ref UsbK_ControlTransfer dynamic driver function id.</Summary>
        ControlTransfer

        ''' <Summary>\ref UsbK_SetPowerPolicy dynamic driver function id.</Summary>
        SetPowerPolicy

        ''' <Summary>\ref UsbK_GetPowerPolicy dynamic driver function id.</Summary>
        GetPowerPolicy

        ''' <Summary>\ref UsbK_SetConfiguration dynamic driver function id.</Summary>
        SetConfiguration

        ''' <Summary>\ref UsbK_GetConfiguration dynamic driver function id.</Summary>
        GetConfiguration

        ''' <Summary>\ref UsbK_ResetDevice dynamic driver function id.</Summary>
        ResetDevice

        ''' <Summary>\ref UsbK_Initialize dynamic driver function id.</Summary>
        Initialize

        ''' <Summary>\ref UsbK_SelectInterface dynamic driver function id.</Summary>
        SelectInterface

        ''' <Summary>\ref UsbK_GetAssociatedInterface dynamic driver function id.</Summary>
        GetAssociatedInterface

        ''' <Summary>\ref UsbK_Clone dynamic driver function id.</Summary>
        Clone

        ''' <Summary>\ref UsbK_QueryInterfaceSettings dynamic driver function id.</Summary>
        QueryInterfaceSettings

        ''' <Summary>\ref UsbK_QueryDeviceInformation dynamic driver function id.</Summary>
        QueryDeviceInformation

        ''' <Summary>\ref UsbK_SetCurrentAlternateSetting dynamic driver function id.</Summary>
        SetCurrentAlternateSetting

        ''' <Summary>\ref UsbK_GetCurrentAlternateSetting dynamic driver function id.</Summary>
        GetCurrentAlternateSetting

        ''' <Summary>\ref UsbK_QueryPipe dynamic driver function id.</Summary>
        QueryPipe

        ''' <Summary>\ref UsbK_SetPipePolicy dynamic driver function id.</Summary>
        SetPipePolicy

        ''' <Summary>\ref UsbK_GetPipePolicy dynamic driver function id.</Summary>
        GetPipePolicy

        ''' <Summary>\ref UsbK_ReadPipe dynamic driver function id.</Summary>
        ReadPipe

        ''' <Summary>\ref UsbK_WritePipe dynamic driver function id.</Summary>
        WritePipe

        ''' <Summary>\ref UsbK_ResetPipe dynamic driver function id.</Summary>
        ResetPipe

        ''' <Summary>\ref UsbK_AbortPipe dynamic driver function id.</Summary>
        AbortPipe

        ''' <Summary>\ref UsbK_FlushPipe dynamic driver function id.</Summary>
        FlushPipe

        ''' <Summary>\ref UsbK_IsoReadPipe dynamic driver function id.</Summary>
        IsoReadPipe

        ''' <Summary>\ref UsbK_IsoWritePipe dynamic driver function id.</Summary>
        IsoWritePipe

        ''' <Summary>\ref UsbK_GetCurrentFrameNumber dynamic driver function id.</Summary>
        GetCurrentFrameNumber

        ''' <Summary>\ref UsbK_GetOverlappedResult dynamic driver function id.</Summary>
        GetOverlappedResult

        ''' <Summary>\ref UsbK_GetProperty dynamic driver function id.</Summary>
        GetProperty

        ''' <Summary>\ref UsbK_IsochReadPipe dynamic driver function id.</Summary>
        IsochReadPipe

        ''' <Summary>\ref UsbK_IsochWritePipe dynamic driver function id.</Summary>
        IsochWritePipe

        ''' <Summary>\ref UsbK_QueryPipeEx dynamic driver function id.</Summary>
        QueryPipeEx

        ''' <Summary>\ref UsbK_GetSuperSpeedPipeCompanionDescriptor dynamic driver function id.</Summary>
        GetSuperSpeedPipeCompanionDescriptor

        ''' <Summary>Supported function count</Summary>
        COUNT
    End Enum

    ''' <Summary>Hot plug config flags.</Summary>
    <Flags>
    Public Enum KHOT_FLAG
        ''' <Summary>No flags (or 0)</Summary>
        NONE

        ''' <Summary>Notify all devices which match upon a succuessful call to \ref HotK_Init.</Summary>
        PLUG_ALL_ON_INIT = &H0001

        ''' <Summary>Allow other \ref KHOT_HANDLE instances to consume this match.</Summary>
        PASS_DUPE_INSTANCE = &H0002

        ''' <Summary>If a \c UserHwnd is specified, use \c PostMessage instead of \c SendMessage.</Summary>
        POST_USER_MESSAGE = &H0004
    End Enum

    ''' <Summary>\c WaitFlags used by \ref OvlK_Wait.</Summary>
    <Flags>
    Public Enum KOVL_WAIT_FLAG
        ''' <Summary>Do not perform any additional actions upon exiting \ref OvlK_Wait.</Summary>
        NONE = 0

        ''' <Summary>If the i/o operation completes successfully, release the OverlappedK back to it's pool.</Summary>
        RELEASE_ON_SUCCESS = &H0001

        ''' <Summary>If the i/o operation fails, release the OverlappedK back to it's pool.</Summary>
        RELEASE_ON_FAIL = &H0002

        ''' <Summary>
        '''     If the i/o operation fails or completes successfully, release the OverlappedK back to its pool. Perform no
        '''     actions if it times-out.
        ''' </Summary>
        RELEASE_ON_SUCCESS_FAIL = &H0003

        ''' <Summary>If the i/o operation times-out cancel it, but do not release the OverlappedK back to its pool.</Summary>
        CANCEL_ON_TIMEOUT = &H0004

        ''' <Summary>If the i/o operation times-out, cancel it and release the OverlappedK back to its pool.</Summary>
        RELEASE_ON_TIMEOUT = &H000C

        ''' <Summary>
        '''     Always release the OverlappedK back to its pool.  If the operation timed-out, cancel it before releasing back
        '''     to its pool.
        ''' </Summary>
        RELEASE_ALWAYS = &H000F

        ''' <Summary>
        '''     Uses alterable wait functions.  See
        '''     http://msdn.microsoft.com/en-us/library/windows/desktop/ms687036%28v=vs.85%29.aspx
        ''' </Summary>
        ALERTABLE = &H0010
    End Enum

    ''' <Summary>\c Overlapped pool config flags.</Summary>
    <Flags>
    Public Enum KOVL_POOL_FLAG
        NONE = 0
    End Enum

    ''' <Summary>Stream config flags.</Summary>
    <Flags>
    Public Enum KSTM_FLAG As UInteger
        ''' <Summary>None</Summary>
        NONE = 0
        NO_PARTIAL_XFERS = &H00100000
        USE_TIMEOUT = &H80000000UI
        TIMEOUT_MASK = &H0001FFFF
    End Enum

    ''' <Summary>Stream config flags.</Summary>
    Public Enum KSTM_COMPLETE_RESULT
        ''' <Summary>Valid</Summary>
        VALID = 0

        ''' <Summary>Invalid</Summary>
        INVALID
    End Enum

#End Region

#Region "Structs"

    ''' <Summary>
    '''     The \c WINUSB_PIPE_INFORMATION structure contains pipe information that the \ref UsbK_QueryPipe routine
    '''     retrieves.
    ''' </Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Structure WINUSB_PIPE_INFORMATION
        ''' <Summary>A \c USBD_PIPE_TYPE enumeration value that specifies the pipe type</Summary>
        Public PipeType As USBD_PIPE_TYPE

        ''' <Summary>The pipe identifier (ID)</Summary>
        Public PipeId As Byte

        ''' <Summary>The maximum size, in bytes, of the packets that are transmitted on the pipe</Summary>
        Public MaximumPacketSize As UShort

        ''' <Summary>The pipe interval</Summary>
        Public Interval As Byte

        Public Overrides Function ToString() As String
            Return String.Format("PipeType: {0}" & Microsoft.VisualBasic.Constants.vbLf & "PipeId: {1}" & Microsoft.VisualBasic.Constants.vbLf & "MaximumPacketSize: {2}" & Microsoft.VisualBasic.Constants.vbLf & "Interval: {3}" & Microsoft.VisualBasic.Constants.vbLf, PipeType, PipeId.ToString("X2") & "h", MaximumPacketSize, Interval.ToString("X2") & "h")
        End Function
    End Structure

    ''' <Summary>
    '''     The \c WINUSB_PIPE_INFORMATION_EX structure contains pipe information that the \ref UsbK_QueryPipeEx routine
    '''     retrieves.
    ''' </Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Structure WINUSB_PIPE_INFORMATION_EX
        ''' <Summary>A \c USBD_PIPE_TYPE enumeration value that specifies the pipe type</Summary>
        Public PipeType As USBD_PIPE_TYPE

        ''' <Summary>The pipe identifier (ID)</Summary>
        Public PipeId As Byte

        ''' <Summary>The maximum size, in bytes, of the packets that are transmitted on the pipe</Summary>
        Public MaximumPacketSize As UShort

        ''' <Summary>The pipe interval</Summary>
        Public Interval As Byte

        ''' <Summary>The maximum number of bytes that can be transmitted in single interval.</Summary>
        Public MaximumBytesPerInterval As UInteger

        Public Overrides Function ToString() As String
            Return String.Format("PipeType: {0}" & Microsoft.VisualBasic.Constants.vbLf & "PipeId: {1}" & Microsoft.VisualBasic.Constants.vbLf & "MaximumPacketSize: {2}" & Microsoft.VisualBasic.Constants.vbLf & "Interval: {3}" & Microsoft.VisualBasic.Constants.vbLf & "MaximumBytesPerInterval: {4}" & Microsoft.VisualBasic.Constants.vbLf, PipeType, PipeId.ToString("X2") & "h", MaximumPacketSize, Interval.ToString("X2") & "h", MaximumBytesPerInterval)
        End Function
    End Structure

    ''' <Summary>The \c WINUSB_SETUP_PACKET structure describes a USB setup packet.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure WINUSB_SETUP_PACKET
        ''' <Summary>
        '''     The request type. The values that are assigned to this member are defined in Table 9.2 of section 9.3 of the
        '''     Universal Serial Bus (USB) specification (www.usb.org).
        ''' </Summary>
        Public RequestType As Byte

        ''' <Summary>
        '''     The device request. The values that are assigned to this member are defined in Table 9.3 of section 9.4 of the
        '''     Universal Serial Bus (USB) specification.
        ''' </Summary>
        Public Request As Byte

        ''' <Summary>
        '''     The meaning of this member varies according to the request. For an explanation of this member, see the
        '''     Universal Serial Bus (USB) specification.
        ''' </Summary>
        Public Value As UShort

        ''' <Summary>
        '''     The meaning of this member varies according to the request. For an explanation of this member, see the
        '''     Universal Serial Bus (USB) specification.
        ''' </Summary>
        Public Index As UShort

        ''' <Summary>The number of bytes to transfer. (not including the \c WINUSB_SETUP_PACKET itself)</Summary>
        Public Length As UShort

        Public Overrides Function ToString() As String
            Return String.Format("RequestType: {0}" & Microsoft.VisualBasic.Constants.vbLf & "Request: {1}" & Microsoft.VisualBasic.Constants.vbLf & "Value: {2}" & Microsoft.VisualBasic.Constants.vbLf & "Index: {3}" & Microsoft.VisualBasic.Constants.vbLf & "Length: {4}" & Microsoft.VisualBasic.Constants.vbLf, RequestType.ToString("X2") & "h", Request.ToString("X2") & "h", Value.ToString("X4") & "h", Index.ToString("X4") & "h", Length)
        End Function
    End Structure

    ''' <Summary>Structure describing an isochronous transfer packet for libusbK.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure KISO_PACKET
        ''' <Summary>
        '''     Specifies the offset, in bytes, of the buffer for this packet from the beginning of the entire isochronous
        '''     transfer data buffer.
        ''' </Summary>
        Public Offset As UInteger

        ''' <Summary>
        '''     Set by the host controller to indicate the actual number of bytes received by the device for isochronous IN
        '''     transfers. Length not used for isochronous OUT transfers.
        ''' </Summary>
        Public Length As UShort

        ''' <Summary>
        '''     Contains the 16 least significant USBD status bits, on return from the host controller driver, of this
        '''     transfer packet.
        ''' </Summary>
        Public Status As UShort

        Public Overrides Function ToString() As String
            Return String.Format("Offset: {0}" & Microsoft.VisualBasic.Constants.vbLf & "Length: {1}" & Microsoft.VisualBasic.Constants.vbLf & "Status: {2}" & Microsoft.VisualBasic.Constants.vbLf, Offset, Length, Status.ToString("X4") & "h")
        End Function
    End Structure

    ''' <Summary>Structure describing an isochronous transfer packet for winusb.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure KISO_WUSB_PACKET
        ''' <Summary>
        '''     Specifies the offset, in bytes, of the buffer for this packet from the beginning of the entire isochronous
        '''     transfer data buffer.
        ''' </Summary>
        Public Offset As UInteger

        ''' <Summary>
        '''     Set by the host controller to indicate the actual number of bytes received by the device for isochronous IN
        '''     transfers. Length not used for isochronous OUT transfers.
        ''' </Summary>
        Public Length As UInteger

        ''' <Summary>
        '''     Contains the 16 least significant USBD status bits, on return from the host controller driver, of this
        '''     transfer packet.
        ''' </Summary>
        Public Status As UInteger

        Public Overrides Function ToString() As String
            Return String.Format("Offset: {0}" & Microsoft.VisualBasic.Constants.vbLf & "Length: {1}" & Microsoft.VisualBasic.Constants.vbLf & "Status: {2}" & Microsoft.VisualBasic.Constants.vbLf, Offset, Length, Status)
        End Function
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure KISO_CONTEXT
        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr
            Get
                Return mHandlePtr
            End Get
        End Property

        ''' <summary>KISO_CONTEXT_MAP is used for calculating field offsets only</summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
        Private Structure KISO_CONTEXT_MAP
            ''' <Summary>Additional ISO transfer flags. See \ref KISO_FLAG.</Summary>
            Private ReadOnly Flags As KISO_FLAG

            ''' <Summary>Specifies the frame number that the transfer should begin on (0 for ASAP).</Summary>
            Private ReadOnly StartFrame As UInteger

            ''' <Summary>
            '''     Contains the number of packets that completed with an error condition on return from the host controller
            '''     driver.
            ''' </Summary>
            Private ReadOnly ErrorCount As Short

            ''' <Summary>Specifies the number of packets that are described by the variable-length array member \c IsoPacket.</Summary>
            Private ReadOnly NumberOfPackets As Short

            ''' <Summary>Contains the URB Hdr.Status value on return from the host controller driver.</Summary>
            Private ReadOnly UrbHdrStatus As UInteger
        End Structure

        Private Shared ReadOnly ofsFlags As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "Flags").ToInt32()
        Private Shared ReadOnly ofsStartFrame As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "StartFrame").ToInt32()
        Private Shared ReadOnly ofsErrorCount As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "ErrorCount").ToInt32()
        Private Shared ReadOnly ofsNumberOfPackets As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "NumberOfPackets").ToInt32()
        Private Shared ReadOnly ofsUrbHdrStatus As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "UrbHdrStatus").ToInt32()


        ''' <Summary>Additional ISO transfer flags. See \ref KISO_FLAG.</Summary>
        Public ReadOnly Property Flags As KISO_FLAG
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsFlags)
            End Get
        End Property


        ''' <Summary>Specifies the frame number that the transfer should begin on (0 for ASAP).</Summary>

        Public ReadOnly Property StartFrame As UInteger
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsStartFrame)
            End Get
        End Property


        ''' <Summary>
        '''     Contains the number of packets that completed with an error condition on return from the host controller
        '''     driver.
        ''' </Summary>

        Public ReadOnly Property ErrorCount As Short
            Get
                Return Marshal.ReadInt16(mHandlePtr, ofsErrorCount)
            End Get
        End Property


        ''' <Summary>Specifies the number of packets that are described by the variable-length array member \c IsoPacket.</Summary>

        Public ReadOnly Property NumberOfPackets As Short
            Get
                Return Marshal.ReadInt16(mHandlePtr, ofsNumberOfPackets)
            End Get
        End Property


        ''' <Summary>Contains the URB Hdr.Status value on return from the host controller driver.</Summary>

        Public ReadOnly Property UrbHdrStatus As UInteger
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsUrbHdrStatus)
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return String.Format("Flags: {0}" & Microsoft.VisualBasic.Constants.vbLf & "StartFrame: {1}" & Microsoft.VisualBasic.Constants.vbLf & "ErrorCount: {2}" & Microsoft.VisualBasic.Constants.vbLf & "NumberOfPackets: {3}" & Microsoft.VisualBasic.Constants.vbLf & "UrbHdrStatus: {4}" & Microsoft.VisualBasic.Constants.vbLf, Flags.ToString(), StartFrame, ErrorCount, NumberOfPackets, UrbHdrStatus.ToString("X8") & "h")
        End Function
    End Structure

    ''' <Summary>Structure describing additional information about how an isochronous pipe transfers data.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure KISOCH_PACKET_INFORMATION
        ''' <Summary>Number of ISO packets transferred per whole USB frame (1 millisecond).</Summary>
        Public PacketsPerFrame As UInteger

        ''' <Summary>How often a pipe transfers data.</Summary>
        Public PollingPeriodMicroseconds As UInteger


        ''' <Summary>Number of bytes transferred per millisecond (or whole frame).</Summary>
        Public BytesPerMillisecond As UInteger

        Public Overrides Function ToString() As String
            Return String.Format("PacketsPerFrame: {0}" & Microsoft.VisualBasic.Constants.vbLf & "PollingPeriodMicroseconds: {1}" & Microsoft.VisualBasic.Constants.vbLf & "BytesPerMillisecond: {2}" & Microsoft.VisualBasic.Constants.vbLf, PacketsPerFrame, PollingPeriodMicroseconds, BytesPerMillisecond)
        End Function
    End Structure

    ''' <Summary>libusbK verson information structure.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Structure KLIB_VERSION
        ''' <Summary>Major version number.</Summary>
        Public Major As Integer

        ''' <Summary>Minor version number.</Summary>
        Public Minor As Integer

        ''' <Summary>Micro version number.</Summary>
        Public Micro As Integer

        ''' <Summary>Nano version number.</Summary>
        Public Nano As Integer

        Public Overrides Function ToString() As String
            Return String.Format("Major: {0}" & Microsoft.VisualBasic.Constants.vbLf & "Minor: {1}" & Microsoft.VisualBasic.Constants.vbLf & "Micro: {2}" & Microsoft.VisualBasic.Constants.vbLf & "Nano: {3}" & Microsoft.VisualBasic.Constants.vbLf, Major, Minor, Micro, Nano)
        End Function
    End Structure

    ''' <Summary>Common usb device information structure</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Structure KLST_DEV_COMMON_INFO
        ''' <Summary>VendorID parsed from \ref KLST_DEVINFO::DeviceID</Summary>
        Public Vid As Integer

        ''' <Summary>ProductID parsed from \ref KLST_DEVINFO::DeviceID</Summary>
        Public Pid As Integer

        ''' <Summary>
        '''     Composite interface number parsed from \ref KLST_DEVINFO::DeviceID.  Set to \b -1 for devices that do not have
        '''     the composite parent driver.
        ''' </Summary>
        Public MI As Integer

        ' An ID that uniquely identifies a USB device.
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
        Public InstanceID As String

        Public Overrides Function ToString() As String
            Return String.Format("Vid: {0}" & Microsoft.VisualBasic.Constants.vbLf & "Pid: {1}" & Microsoft.VisualBasic.Constants.vbLf & "MI: {2}" & Microsoft.VisualBasic.Constants.vbLf & "InstanceID: {3}" & Microsoft.VisualBasic.Constants.vbLf, Vid.ToString("X4") & "h", Pid.ToString("X4") & "h", MI.ToString("X2") & "h", InstanceID)
        End Function
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure KLST_DEVINFO_HANDLE
        Implements IKLIB_HANDLE

        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr Implements IKLIB_HANDLE.Pointer
            Get
                Return mHandlePtr
            End Get
        End Property

        Public ReadOnly Property HandleType As KLIB_HANDLE_TYPE Implements IKLIB_HANDLE.HandleType
            Get
                Return KLIB_HANDLE_TYPE.LSTINFOK
            End Get
        End Property

        Public Function GetContext() As IntPtr Implements IKLIB_HANDLE.GetContext
            Return LibK_GetContext(mHandlePtr, HandleType)
        End Function

        Public Function SetContext(ByVal UserContext As IntPtr) As Boolean Implements IKLIB_HANDLE.SetContext
            Return LibK_SetContext(mHandlePtr, HandleType, UserContext)
        End Function

        Public Function SetCleanupCallback(ByVal CleanupCallback As KLIB_HANDLE_CLEANUP_CB) As Boolean Implements IKLIB_HANDLE.SetCleanupCallback
            Return LibK_SetCleanupCallback(mHandlePtr, HandleType, CleanupCallback)
        End Function

        ''' <summary>KLST_DEVINFO_MAP is used for calculating field offsets only</summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
        Private Structure KLST_DEVINFO_MAP
            ''' <Summary>Common usb device information</Summary>
            Private ReadOnly Common As KLST_DEV_COMMON_INFO

            ''' <Summary>Driver id this device element is using</Summary>
            Private ReadOnly DriverID As Integer

            ''' <Summary>Device interface GUID</Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly DeviceInterfaceGUID As String

            ''' <Summary>Device instance ID.</Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly DeviceID As String

            ''' <Summary>Class GUID.</Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly ClassGUID As String

            ''' <Summary>Manufacturer name as specified in the INF file.</Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly Mfg As String

            ''' <Summary>Device description as specified in the INF file.</Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly DeviceDesc As String

            ''' <Summary>Driver service name.</Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly Service As String

            ''' <Summary>Unique identifier.</Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly SymbolicLink As String

            ''' <Summary>physical device filename used with the Windows \c CreateFile()</Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly DevicePath As String

            ''' <Summary>libusb-win32 filter index id.</Summary>
            Private ReadOnly LUsb0FilterIndex As Integer

            ''' <Summary>Indicates the devices connection state.</Summary>
            Private ReadOnly Connected As Boolean

            ''' <Summary>Synchronization flags. (internal use only)</Summary>
            Private ReadOnly SyncFlags As KLST_SYNC_FLAG
            Private ReadOnly BusNumber As Integer
            Private ReadOnly DeviceAddress As Integer

            ''' <Summary>
            '''     If the the device is serialized, represents the string value of \ref USB_DEVICE_DESCRIPTOR::iSerialNumber. For
            '''     Devices without a \b iSerialNumber, represents the unique \b InstanceID assigned by \b Windows.
            ''' </Summary>
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
            Private ReadOnly SerialNumber As String
        End Structure

        Private Shared ReadOnly ofsCommon As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "Common").ToInt32()
        Private Shared ReadOnly ofsDriverID As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "DriverID").ToInt32()
        Private Shared ReadOnly ofsDeviceInterfaceGUID As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "DeviceInterfaceGUID").ToInt32()
        Private Shared ReadOnly ofsDeviceID As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "DeviceID").ToInt32()
        Private Shared ReadOnly ofsClassGUID As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "ClassGUID").ToInt32()
        Private Shared ReadOnly ofsMfg As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "Mfg").ToInt32()
        Private Shared ReadOnly ofsDeviceDesc As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "DeviceDesc").ToInt32()
        Private Shared ReadOnly ofsService As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "Service").ToInt32()
        Private Shared ReadOnly ofsSymbolicLink As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "SymbolicLink").ToInt32()
        Private Shared ReadOnly ofsDevicePath As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "DevicePath").ToInt32()
        Private Shared ReadOnly ofsLUsb0FilterIndex As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "LUsb0FilterIndex").ToInt32()
        Private Shared ReadOnly ofsConnected As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "Connected").ToInt32()
        Private Shared ReadOnly ofsSyncFlags As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "SyncFlags").ToInt32()
        Private Shared ReadOnly ofsBusNumber As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "BusNumber").ToInt32()
        Private Shared ReadOnly ofsDeviceAddress As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "DeviceAddress").ToInt32()
        Private Shared ReadOnly ofsSerialNumber As Integer = Marshal.OffsetOf(GetType(KLST_DEVINFO_MAP), "SerialNumber").ToInt32()


        ''' <Summary>Common usb device information</Summary>
        Public ReadOnly Property Common As KLST_DEV_COMMON_INFO
            Get
                Return CType(Marshal.PtrToStructure(New IntPtr(mHandlePtr.ToInt64() + ofsCommon), GetType(KLST_DEV_COMMON_INFO)), KLST_DEV_COMMON_INFO)
            End Get
        End Property


        ''' <Summary>Driver id this device element is using</Summary>
        Public ReadOnly Property DriverID As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsDriverID)
            End Get
        End Property


        ''' <Summary>Device interface GUID</Summary>
        Public ReadOnly Property DeviceInterfaceGUID As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsDeviceInterfaceGUID))
            End Get
        End Property


        ''' <Summary>Device instance ID.</Summary>

        Public ReadOnly Property DeviceID As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsDeviceID))
            End Get
        End Property


        ''' <Summary>Class GUID.</Summary>
        Public ReadOnly Property ClassGUID As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsClassGUID))
            End Get
        End Property


        ''' <Summary>Manufacturer name as specified in the INF file.</Summary>
        Public ReadOnly Property Mfg As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsMfg))
            End Get
        End Property


        ''' <Summary>Device description as specified in the INF file.</Summary>
        Public ReadOnly Property DeviceDesc As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsDeviceDesc))
            End Get
        End Property


        ''' <Summary>Driver service name.</Summary>
        Public ReadOnly Property Service As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsService))
            End Get
        End Property


        ''' <Summary>Unique identifier.</Summary>
        Public ReadOnly Property SymbolicLink As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsSymbolicLink))
            End Get
        End Property


        ''' <Summary>physical device filename used with the Windows \c CreateFile()</Summary>
        Public ReadOnly Property DevicePath As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsDevicePath))
            End Get
        End Property


        ''' <Summary>libusb-win32 filter index id.</Summary>
        Public ReadOnly Property LUsb0FilterIndex As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsLUsb0FilterIndex)
            End Get
        End Property


        ''' <Summary>Indicates the devices connection state.</Summary>
        Public ReadOnly Property Connected As Boolean
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsConnected) <> 0
            End Get
        End Property


        ''' <Summary>Synchronization flags. (internal use only)</Summary>
        Public ReadOnly Property SyncFlags As KLST_SYNC_FLAG
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsSyncFlags)
            End Get
        End Property

        Public ReadOnly Property BusNumber As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsBusNumber)
            End Get
        End Property

        Public ReadOnly Property DeviceAddress As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsDeviceAddress)
            End Get
        End Property


        ''' <Summary>
        '''     If the the device is serialized, represents the string value of \ref USB_DEVICE_DESCRIPTOR::iSerialNumber. For
        '''     Devices without a \b iSerialNumber, represents the unique \b InstanceID assigned by \b Windows.
        ''' </Summary>
        Public ReadOnly Property SerialNumber As String
            Get
                Return Marshal.PtrToStringAnsi(New IntPtr(mHandlePtr.ToInt64() + ofsSerialNumber))
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return String.Format("DriverID: {0}" & Microsoft.VisualBasic.Constants.vbLf & "DeviceInterfaceGUID: {1}" & Microsoft.VisualBasic.Constants.vbLf & "DeviceID: {2}" & Microsoft.VisualBasic.Constants.vbLf & "ClassGUID: {3}" & Microsoft.VisualBasic.Constants.vbLf & "Mfg: {4}" & Microsoft.VisualBasic.Constants.vbLf & "DeviceDesc: {5}" & Microsoft.VisualBasic.Constants.vbLf & "Service: {6}" & Microsoft.VisualBasic.Constants.vbLf & "SymbolicLink: {7}" & Microsoft.VisualBasic.Constants.vbLf & "DevicePath: {8}" & Microsoft.VisualBasic.Constants.vbLf & "LUsb0FilterIndex: {9}" & Microsoft.VisualBasic.Constants.vbLf & "Connected: {10}" & Microsoft.VisualBasic.Constants.vbLf & "SyncFlags: {11}" & Microsoft.VisualBasic.Constants.vbLf & "BusNumber: {12}" & Microsoft.VisualBasic.Constants.vbLf & "DeviceAddress: {13}" & Microsoft.VisualBasic.Constants.vbLf & "SerialNumber: {14}" & Microsoft.VisualBasic.Constants.vbLf, DriverID, DeviceInterfaceGUID, DeviceID, ClassGUID, Mfg, DeviceDesc, Service, SymbolicLink, DevicePath, LUsb0FilterIndex, Connected, SyncFlags.ToString(), BusNumber, DeviceAddress, SerialNumber)
        End Function
    End Structure

    ''' <Summary>Device list/hot-plug pattern match structure.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Size:=1024)>
    Public Structure KLST_PATTERN_MATCH
        ''' <Summary>Pattern match a device instance id.</Summary>
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
        Public DeviceID As String

        ''' <Summary>Pattern match a device interface guid.</Summary>
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
        Public DeviceInterfaceGUID As String

        ''' <Summary>Pattern match a symbolic link.</Summary>
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
        Public ClassGUID As String

        Public Overrides Function ToString() As String
            Return String.Format("DeviceID: {0}" & Microsoft.VisualBasic.Constants.vbLf & "DeviceInterfaceGUID: {1}" & Microsoft.VisualBasic.Constants.vbLf & "ClassGUID: {2}" & Microsoft.VisualBasic.Constants.vbLf, DeviceID, DeviceInterfaceGUID, ClassGUID)
        End Function
    End Structure

    ''' <Summary>A structure representing the standard USB device descriptor.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure USB_DEVICE_DESCRIPTOR
        ''' <Summary>Size of this descriptor (in bytes)</Summary>
        Public bLength As Byte

        ''' <Summary>Descriptor type</Summary>
        Public bDescriptorType As Byte

        ''' <Summary>USB specification release number in binary-coded decimal.</Summary>
        Public bcdUSB As UShort

        ''' <Summary>USB-IF class code for the device</Summary>
        Public bDeviceClass As Byte

        ''' <Summary>USB-IF subclass code for the device</Summary>
        Public bDeviceSubClass As Byte

        ''' <Summary>USB-IF protocol code for the device</Summary>
        Public bDeviceProtocol As Byte

        ''' <Summary>Maximum packet size for control endpoint 0</Summary>
        Public bMaxPacketSize0 As Byte

        ''' <Summary>USB-IF vendor ID</Summary>
        Public idVendor As UShort

        ''' <Summary>USB-IF product ID</Summary>
        Public idProduct As UShort

        ''' <Summary>Device release number in binary-coded decimal</Summary>
        Public bcdDevice As UShort

        ''' <Summary>Index of string descriptor describing manufacturer</Summary>
        Public iManufacturer As Byte

        ''' <Summary>Index of string descriptor describing product</Summary>
        Public iProduct As Byte

        ''' <Summary>Index of string descriptor containing device serial number</Summary>
        Public iSerialNumber As Byte

        ''' <Summary>Number of possible configurations</Summary>
        Public bNumConfigurations As Byte

        Public Overrides Function ToString() As String
            Return String.Format("bLength: {0}" & Microsoft.VisualBasic.Constants.vbLf & "bDescriptorType: {1}" & Microsoft.VisualBasic.Constants.vbLf & "bcdUSB: {2}" & Microsoft.VisualBasic.Constants.vbLf & "bDeviceClass: {3}" & Microsoft.VisualBasic.Constants.vbLf & "bDeviceSubClass: {4}" & Microsoft.VisualBasic.Constants.vbLf & "bDeviceProtocol: {5}" & Microsoft.VisualBasic.Constants.vbLf & "bMaxPacketSize0: {6}" & Microsoft.VisualBasic.Constants.vbLf & "idVendor: {7}" & Microsoft.VisualBasic.Constants.vbLf & "idProduct: {8}" & Microsoft.VisualBasic.Constants.vbLf & "bcdDevice: {9}" & Microsoft.VisualBasic.Constants.vbLf & "iManufacturer: {10}" & Microsoft.VisualBasic.Constants.vbLf & "iProduct: {11}" & Microsoft.VisualBasic.Constants.vbLf & "iSerialNumber: {12}" & Microsoft.VisualBasic.Constants.vbLf & "bNumConfigurations: {13}" & Microsoft.VisualBasic.Constants.vbLf, bLength, bDescriptorType.ToString("X2") & "h", bcdUSB.ToString("X4") & "h", bDeviceClass.ToString("X2") & "h", bDeviceSubClass.ToString("X2") & "h", bDeviceProtocol.ToString("X2") & "h", bMaxPacketSize0, idVendor.ToString("X4") & "h", idProduct.ToString("X4") & "h", bcdDevice.ToString("X4") & "h", iManufacturer, iProduct, iSerialNumber, bNumConfigurations)
        End Function
    End Structure

    ''' <Summary>A structure representing the standard USB endpoint descriptor.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure USB_ENDPOINT_DESCRIPTOR
        ''' <Summary>Size of this descriptor (in bytes)</Summary>
        Public bLength As Byte

        ''' <Summary>Descriptor type</Summary>
        Public bDescriptorType As Byte

        ''' <Summary>The address of the endpoint described by this descriptor.</Summary>
        Public bEndpointAddress As Byte

        ''' <Summary>Attributes which apply to the endpoint when it is configured using the bConfigurationValue.</Summary>
        Public bmAttributes As Byte

        ''' <Summary>Maximum packet size this endpoint is capable of sending/receiving.</Summary>
        Public wMaxPacketSize As UShort

        ''' <Summary>Interval for polling endpoint for data transfers.</Summary>
        Public bInterval As Byte

        Public Overrides Function ToString() As String
            Return String.Format("bLength: {0}" & Microsoft.VisualBasic.Constants.vbLf & "bDescriptorType: {1}" & Microsoft.VisualBasic.Constants.vbLf & "bEndpointAddress: {2}" & Microsoft.VisualBasic.Constants.vbLf & "bmAttributes: {3}" & Microsoft.VisualBasic.Constants.vbLf & "wMaxPacketSize: {4}" & Microsoft.VisualBasic.Constants.vbLf & "bInterval: {5}" & Microsoft.VisualBasic.Constants.vbLf, bLength, bDescriptorType.ToString("X2") & "h", bEndpointAddress.ToString("X2") & "h", bmAttributes.ToString("X2") & "h", wMaxPacketSize, bInterval)
        End Function
    End Structure

    ''' <Summary>A structure representing the standard USB configuration descriptor.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure USB_CONFIGURATION_DESCRIPTOR
        ''' <Summary>Size of this descriptor (in bytes)</Summary>
        Public bLength As Byte

        ''' <Summary>Descriptor type</Summary>
        Public bDescriptorType As Byte

        ''' <Summary>Total length of data returned for this configuration</Summary>
        Public wTotalLength As UShort

        ''' <Summary>Number of interfaces supported by this configuration</Summary>
        Public bNumInterfaces As Byte

        ''' <Summary>Identifier value for this configuration</Summary>
        Public bConfigurationValue As Byte

        ''' <Summary>Index of string descriptor describing this configuration</Summary>
        Public iConfiguration As Byte

        ''' <Summary>Configuration characteristics</Summary>
        Public bmAttributes As Byte

        ''' <Summary>
        '''     Maximum power consumption of the USB device from this bus in this configuration when the device is fully
        '''     operation.
        ''' </Summary>
        Public MaxPower As Byte

        Public Overrides Function ToString() As String
            Return String.Format("bLength: {0}" & Microsoft.VisualBasic.Constants.vbLf & "bDescriptorType: {1}" & Microsoft.VisualBasic.Constants.vbLf & "wTotalLength: {2}" & Microsoft.VisualBasic.Constants.vbLf & "bNumInterfaces: {3}" & Microsoft.VisualBasic.Constants.vbLf & "bConfigurationValue: {4}" & Microsoft.VisualBasic.Constants.vbLf & "iConfiguration: {5}" & Microsoft.VisualBasic.Constants.vbLf & "bmAttributes: {6}" & Microsoft.VisualBasic.Constants.vbLf & "MaxPower: {7}" & Microsoft.VisualBasic.Constants.vbLf, bLength, bDescriptorType.ToString("X2") & "h", wTotalLength, bNumInterfaces, bConfigurationValue, iConfiguration, bmAttributes.ToString("X2") & "h", MaxPower)
        End Function
    End Structure

    ''' <Summary>A structure representing the standard USB interface descriptor.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure USB_INTERFACE_DESCRIPTOR
        ''' <Summary>Size of this descriptor (in bytes)</Summary>
        Public bLength As Byte

        ''' <Summary>Descriptor type</Summary>
        Public bDescriptorType As Byte

        ''' <Summary>Number of this interface</Summary>
        Public bInterfaceNumber As Byte

        ''' <Summary>Value used to select this alternate setting for this interface</Summary>
        Public bAlternateSetting As Byte

        ''' <Summary>Number of endpoints used by this interface (excluding the control endpoint)</Summary>
        Public bNumEndpoints As Byte

        ''' <Summary>USB-IF class code for this interface</Summary>
        Public bInterfaceClass As Byte

        ''' <Summary>USB-IF subclass code for this interface</Summary>
        Public bInterfaceSubClass As Byte

        ''' <Summary>USB-IF protocol code for this interface</Summary>
        Public bInterfaceProtocol As Byte

        ''' <Summary>Index of string descriptor describing this interface</Summary>
        Public iInterface As Byte

        Public Overrides Function ToString() As String
            Return String.Format("bLength: {0}" & Microsoft.VisualBasic.Constants.vbLf & "bDescriptorType: {1}" & Microsoft.VisualBasic.Constants.vbLf & "bInterfaceNumber: {2}" & Microsoft.VisualBasic.Constants.vbLf & "bAlternateSetting: {3}" & Microsoft.VisualBasic.Constants.vbLf & "bNumEndpoints: {4}" & Microsoft.VisualBasic.Constants.vbLf & "bInterfaceClass: {5}" & Microsoft.VisualBasic.Constants.vbLf & "bInterfaceSubClass: {6}" & Microsoft.VisualBasic.Constants.vbLf & "bInterfaceProtocol: {7}" & Microsoft.VisualBasic.Constants.vbLf & "iInterface: {8}" & Microsoft.VisualBasic.Constants.vbLf, bLength, bDescriptorType.ToString("X2") & "h", bInterfaceNumber, bAlternateSetting, bNumEndpoints, bInterfaceClass.ToString("X2") & "h", bInterfaceSubClass.ToString("X2") & "h", bInterfaceProtocol.ToString("X2") & "h", iInterface)
        End Function
    End Structure

    ''' <Summary>A structure representing the standard USB string descriptor.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode, Pack:=1)>
    Public Structure USB_STRING_DESCRIPTOR
        ''' <Summary>Size of this descriptor (in bytes)</Summary>
        Public bLength As Byte

        ''' <Summary>Descriptor type</Summary>
        Public bDescriptorType As Byte

        ''' <Summary>Content of the string</Summary>
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=KLST_STRING_MAX_LEN)>
        Public bString As String

        Public Overrides Function ToString() As String
            Return String.Format("bLength: {0}" & Microsoft.VisualBasic.Constants.vbLf & "bDescriptorType: {1}" & Microsoft.VisualBasic.Constants.vbLf & "bString: {2}" & Microsoft.VisualBasic.Constants.vbLf, bLength, bDescriptorType.ToString("X2") & "h", bString)
        End Function
    End Structure

    ''' <Summary>A structure representing the common USB descriptor.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure USB_COMMON_DESCRIPTOR
        ''' <Summary>Size of this descriptor (in bytes)</Summary>
        Public bLength As Byte

        ''' <Summary>Descriptor type</Summary>
        Public bDescriptorType As Byte

        Public Overrides Function ToString() As String
            Return String.Format("bLength: {0}" & Microsoft.VisualBasic.Constants.vbLf & "bDescriptorType: {1}" & Microsoft.VisualBasic.Constants.vbLf, bLength, bDescriptorType.ToString("X2") & "h")
        End Function
    End Structure

    ''' <Summary>Allows hardware manufacturers to define groupings of interfaces.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
    Public Structure USB_INTERFACE_ASSOCIATION_DESCRIPTOR
        ''' <Summary>Size of this descriptor (in bytes)</Summary>
        Public bLength As Byte

        ''' <Summary>Descriptor type</Summary>
        Public bDescriptorType As Byte

        ''' <Summary>First interface number of the set of interfaces that follow this descriptor</Summary>
        Public bFirstInterface As Byte

        ''' <Summary>The Number of interfaces follow this descriptor that are considered "associated"</Summary>
        Public bInterfaceCount As Byte

        ''' <Summary>\c bInterfaceClass used for this associated interfaces</Summary>
        Public bFunctionClass As Byte

        ''' <Summary>\c bInterfaceSubClass used for the associated interfaces</Summary>
        Public bFunctionSubClass As Byte

        ''' <Summary>\c bInterfaceProtocol used for the associated interfaces</Summary>
        Public bFunctionProtocol As Byte

        ''' <Summary>Index of string descriptor describing the associated interfaces</Summary>
        Public iFunction As Byte

        Public Overrides Function ToString() As String
            Return String.Format("bLength: {0}" & Microsoft.VisualBasic.Constants.vbLf & "bDescriptorType: {1}" & Microsoft.VisualBasic.Constants.vbLf & "bFirstInterface: {2}" & Microsoft.VisualBasic.Constants.vbLf & "bInterfaceCount: {3}" & Microsoft.VisualBasic.Constants.vbLf & "bFunctionClass: {4}" & Microsoft.VisualBasic.Constants.vbLf & "bFunctionSubClass: {5}" & Microsoft.VisualBasic.Constants.vbLf & "bFunctionProtocol: {6}" & Microsoft.VisualBasic.Constants.vbLf & "iFunction: {7}" & Microsoft.VisualBasic.Constants.vbLf, bLength, bDescriptorType.ToString("X2") & "h", bFirstInterface, bInterfaceCount, bFunctionClass.ToString("X2") & "h", bFunctionSubClass.ToString("X2") & "h", bFunctionProtocol.ToString("X2") & "h", iFunction)
        End Function
    End Structure

    ''' <Summary>USB core driver API information structure.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Structure KUSB_DRIVER_API_INFO
        ''' <Summary>\readonly Driver id of the driver api.</Summary>
        Public DriverID As Integer

        ''' <Summary>\readonly Number of valid functions contained in the driver API.</Summary>
        Public FunctionCount As Integer

        Public Overrides Function ToString() As String
            Return String.Format("DriverID: {0}" & Microsoft.VisualBasic.Constants.vbLf & "FunctionCount: {1}" & Microsoft.VisualBasic.Constants.vbLf, DriverID, FunctionCount)
        End Function
    End Structure

    ''' <Summary>Driver API function set structure.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Size:=512)>
    Public Structure KUSB_DRIVER_API
        ''' <Summary>Driver API information.</Summary>
        Public Info As KUSB_DRIVER_API_INFO
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public Init As KUSB_InitDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public Free As KUSB_FreeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public ClaimInterface As KUSB_ClaimInterfaceDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public ReleaseInterface As KUSB_ReleaseInterfaceDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public SetAltInterface As KUSB_SetAltInterfaceDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetAltInterface As KUSB_GetAltInterfaceDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetDescriptor As KUSB_GetDescriptorDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public ControlTransfer As KUSB_ControlTransferDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public SetPowerPolicy As KUSB_SetPowerPolicyDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetPowerPolicy As KUSB_GetPowerPolicyDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public SetConfiguration As KUSB_SetConfigurationDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetConfiguration As KUSB_GetConfigurationDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public ResetDevice As KUSB_ResetDeviceDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public Initialize As KUSB_InitializeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public SelectInterface As KUSB_SelectInterfaceDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetAssociatedInterface As KUSB_GetAssociatedInterfaceDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public Clone As KUSB_CloneDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public QueryInterfaceSettings As KUSB_QueryInterfaceSettingsDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public QueryDeviceInformation As KUSB_QueryDeviceInformationDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public SetCurrentAlternateSetting As KUSB_SetCurrentAlternateSettingDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetCurrentAlternateSetting As KUSB_GetCurrentAlternateSettingDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public QueryPipe As KUSB_QueryPipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public SetPipePolicy As KUSB_SetPipePolicyDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetPipePolicy As KUSB_GetPipePolicyDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public ReadPipe As KUSB_ReadPipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public WritePipe As KUSB_WritePipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public ResetPipe As KUSB_ResetPipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public AbortPipe As KUSB_AbortPipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public FlushPipe As KUSB_FlushPipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public IsoReadPipe As KUSB_IsoReadPipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public IsoWritePipe As KUSB_IsoWritePipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetCurrentFrameNumber As KUSB_GetCurrentFrameNumberDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetOverlappedResult As KUSB_GetOverlappedResultDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetProperty As KUSB_GetPropertyDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public IsochReadPipe As KUSB_IsochReadPipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public IsochWritePipe As KUSB_IsochWritePipeDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public QueryPipeEx As KUSB_QueryPipeExDelegate
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public GetSuperSpeedPipeCompanionDescriptor As KUSB_GetSuperSpeedPipeCompanionDescriptorDelegate
    End Structure

    ''' <Summary>Hot plug parameter structure.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Size:=2048)>
    Public Structure KHOT_PARAMS
        ''' <Summary>Hot plug event window handle to send/post messages when notifications occur.</Summary>
        Public UserHwnd As IntPtr

        ''' <Summary>WM_USER message start offset used when sending/posting messages, See details.</Summary>
        Public UserMessage As UInteger

        ''' <Summary>Additional init/config parameters</Summary>
        Public Flags As KHOT_FLAG

        ''' <Summary>File pattern matches for restricting notifcations to a single/group or all supported usb devices.</Summary>
        Public PatternMatch As KLST_PATTERN_MATCH

        ''' <Summary>Hot plug event callback function invoked when notifications occur.</Summary>
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public OnHotPlug As KHOT_PLUG_CB

        ''' <Summary>\b WM_POWERBROADCAST event callback function invoked when a power-management event has occurred.</Summary>
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public OnPowerBroadcast As KHOT_POWER_BROADCAST_CB

        Public Overrides Function ToString() As String
            Return String.Format("UserHwnd: {0}" & Microsoft.VisualBasic.Constants.vbLf & "UserMessage: {1}" & Microsoft.VisualBasic.Constants.vbLf & "Flags: {2}" & Microsoft.VisualBasic.Constants.vbLf, UserHwnd.ToString("X16") & "h", UserMessage.ToString("X8") & "h", Flags.ToString())
        End Function
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure KSTM_XFER_CONTEXT
        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr
            Get
                Return mHandlePtr
            End Get
        End Property

        ''' <summary>KSTM_XFER_CONTEXT_MAP is used for calculating field offsets only</summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
        Private Structure KSTM_XFER_CONTEXT_MAP
            ''' <Summary>Internal stream buffer.</Summary>
            Private ReadOnly Buffer As IntPtr

            ''' <Summary>Size of internal stream buffer.</Summary>
            Private ReadOnly BufferSize As Integer

            ''' <Summary>Number of bytes to write or number of bytes read.</Summary>
            Private ReadOnly TransferLength As Integer

            ''' <Summary>User defined state.</Summary>
            Private ReadOnly UserState As IntPtr
        End Structure

        Private Shared ReadOnly ofsBuffer As Integer = Marshal.OffsetOf(GetType(KSTM_XFER_CONTEXT_MAP), "Buffer").ToInt32()
        Private Shared ReadOnly ofsBufferSize As Integer = Marshal.OffsetOf(GetType(KSTM_XFER_CONTEXT_MAP), "BufferSize").ToInt32()
        Private Shared ReadOnly ofsTransferLength As Integer = Marshal.OffsetOf(GetType(KSTM_XFER_CONTEXT_MAP), "TransferLength").ToInt32()
        Private Shared ReadOnly ofsUserState As Integer = Marshal.OffsetOf(GetType(KSTM_XFER_CONTEXT_MAP), "UserState").ToInt32()


        ''' <Summary>Internal stream buffer.</Summary>
        Public ReadOnly Property Buffer As IntPtr
            Get
                Return Marshal.ReadIntPtr(mHandlePtr, ofsBuffer)
            End Get
        End Property


        ''' <Summary>Size of internal stream buffer.</Summary>
        Public ReadOnly Property BufferSize As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsBufferSize)
            End Get
        End Property


        ''' <Summary>Number of bytes to write or number of bytes read.</Summary>
        Public ReadOnly Property TransferLength As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsTransferLength)
            End Get
        End Property


        ''' <Summary>User defined state.</Summary>
        Public Property UserState As IntPtr
            Get
                Return Marshal.ReadIntPtr(mHandlePtr, ofsUserState)
            End Get
            Set(ByVal value As IntPtr)
                Marshal.WriteIntPtr(mHandlePtr, ofsUserState, value)
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return String.Format("Buffer: {0}" & Microsoft.VisualBasic.Constants.vbLf & "BufferSize: {1}" & Microsoft.VisualBasic.Constants.vbLf & "TransferLength: {2}" & Microsoft.VisualBasic.Constants.vbLf & "UserState: {3}" & Microsoft.VisualBasic.Constants.vbLf, Buffer.ToString("X16") & "h", BufferSize, TransferLength, UserState.ToString("X16") & "h")
        End Function
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure KSTM_INFO
        Private ReadOnly mHandlePtr As IntPtr

        Public Sub New(ByVal Handle As IntPtr)
            mHandlePtr = Handle
        End Sub

        Public ReadOnly Property Pointer As IntPtr
            Get
                Return mHandlePtr
            End Get
        End Property

        ''' <summary>KSTM_INFO_MAP is used for calculating field offsets only</summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
        Private Structure KSTM_INFO_MAP
            ''' <Summary>\ref KUSB_HANDLE this stream uses.</Summary>
            Private ReadOnly UsbHandle As IntPtr

            ''' <Summary>This parameter corresponds to the bEndpointAddress field in the endpoint descriptor.</Summary>
            Private ReadOnly PipeID As Byte

            ''' <Summary>Maximum transfer read/write request allowed pending.</Summary>
            Private ReadOnly MaxPendingTransfers As Integer

            ''' <Summary>Maximum transfer sage size.</Summary>
            Private ReadOnly MaxTransferSize As Integer

            ''' <Summary>Maximum number of I/O request allowed pending.</Summary>
            Private ReadOnly MaxPendingIO As Integer

            ''' <Summary>Populated with the endpoint descriptor for the specified \c PipeID.</Summary>
            Private ReadOnly EndpointDescriptor As USB_ENDPOINT_DESCRIPTOR

            ''' <Summary>Populated with the driver api for the specified \c UsbHandle.</Summary>
            Private ReadOnly DriverAPI As KUSB_DRIVER_API

            ''' <Summary>Populated with the device file handle for the specified \c UsbHandle.</Summary>
            Private ReadOnly DeviceHandle As IntPtr

            ''' <Summary>Stream handle.</Summary>
            Private ReadOnly StreamHandle As IntPtr

            ''' <Summary>Stream info user defined state.</Summary>
            Private ReadOnly UserState As IntPtr
        End Structure

        Private Shared ReadOnly ofsUsbHandle As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "UsbHandle").ToInt32()
        Private Shared ReadOnly ofsPipeID As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "PipeID").ToInt32()
        Private Shared ReadOnly ofsMaxPendingTransfers As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "MaxPendingTransfers").ToInt32()
        Private Shared ReadOnly ofsMaxTransferSize As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "MaxTransferSize").ToInt32()
        Private Shared ReadOnly ofsMaxPendingIO As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "MaxPendingIO").ToInt32()
        Private Shared ReadOnly ofsEndpointDescriptor As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "EndpointDescriptor").ToInt32()
        Private Shared ReadOnly ofsDriverAPI As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "DriverAPI").ToInt32()
        Private Shared ReadOnly ofsDeviceHandle As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "DeviceHandle").ToInt32()
        Private Shared ReadOnly ofsStreamHandle As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "StreamHandle").ToInt32()
        Private Shared ReadOnly ofsUserState As Integer = Marshal.OffsetOf(GetType(KSTM_INFO_MAP), "UserState").ToInt32()


        ''' <Summary>\ref KUSB_HANDLE this stream uses.</Summary>
        Public ReadOnly Property UsbHandle As IntPtr
            Get
                Return Marshal.ReadIntPtr(mHandlePtr, ofsUsbHandle)
            End Get
        End Property


        ''' <Summary>This parameter corresponds to the bEndpointAddress field in the endpoint descriptor.</Summary>
        Public ReadOnly Property PipeID As Byte
            Get
                Return Marshal.ReadByte(mHandlePtr, ofsPipeID)
            End Get
        End Property


        ''' <Summary>Maximum transfer read/write request allowed pending.</Summary>
        Public ReadOnly Property MaxPendingTransfers As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsMaxPendingTransfers)
            End Get
        End Property


        ''' <Summary>Maximum transfer sage size.</Summary>
        Public ReadOnly Property MaxTransferSize As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsMaxTransferSize)
            End Get
        End Property


        ''' <Summary>Maximum number of I/O request allowed pending.</Summary>
        Public ReadOnly Property MaxPendingIO As Integer
            Get
                Return Marshal.ReadInt32(mHandlePtr, ofsMaxPendingIO)
            End Get
        End Property


        ''' <Summary>Populated with the endpoint descriptor for the specified \c PipeID.</Summary>
        Public ReadOnly Property EndpointDescriptor As USB_ENDPOINT_DESCRIPTOR
            Get
                Return CType(Marshal.PtrToStructure(New IntPtr(mHandlePtr.ToInt64() + ofsEndpointDescriptor), GetType(USB_ENDPOINT_DESCRIPTOR)), USB_ENDPOINT_DESCRIPTOR)
            End Get
        End Property


        ''' <Summary>Populated with the driver api for the specified \c UsbHandle.</Summary>
        Public ReadOnly Property DriverAPI As KUSB_DRIVER_API
            Get
                Return CType(Marshal.PtrToStructure(New IntPtr(mHandlePtr.ToInt64() + ofsDriverAPI), GetType(KUSB_DRIVER_API)), KUSB_DRIVER_API)
            End Get
        End Property


        ''' <Summary>Populated with the device file handle for the specified \c UsbHandle.</Summary>
        Public ReadOnly Property DeviceHandle As IntPtr
            Get
                Return Marshal.ReadIntPtr(mHandlePtr, ofsDeviceHandle)
            End Get
        End Property


        ''' <Summary>Stream handle.</Summary>
        Public ReadOnly Property StreamHandle As IntPtr
            Get
                Return Marshal.ReadIntPtr(mHandlePtr, ofsStreamHandle)
            End Get
        End Property


        ''' <Summary>Stream info user defined state.</Summary>
        Public Property UserState As IntPtr
            Get
                Return Marshal.ReadIntPtr(mHandlePtr, ofsUserState)
            End Get
            Set(ByVal value As IntPtr)
                Marshal.WriteIntPtr(mHandlePtr, ofsUserState, value)
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return String.Format("UsbHandle: {0}" & Microsoft.VisualBasic.Constants.vbLf & "PipeID: {1}" & Microsoft.VisualBasic.Constants.vbLf & "MaxPendingTransfers: {2}" & Microsoft.VisualBasic.Constants.vbLf & "MaxTransferSize: {3}" & Microsoft.VisualBasic.Constants.vbLf & "MaxPendingIO: {4}" & Microsoft.VisualBasic.Constants.vbLf & "DeviceHandle: {5}" & Microsoft.VisualBasic.Constants.vbLf & "StreamHandle: {6}" & Microsoft.VisualBasic.Constants.vbLf & "UserState: {7}" & Microsoft.VisualBasic.Constants.vbLf, UsbHandle.ToString("X16") & "h", PipeID.ToString("X2") & "h", MaxPendingTransfers, MaxTransferSize, MaxPendingIO, DeviceHandle.ToString("X16") & "h", StreamHandle.ToString("X16") & "h", UserState.ToString("X16") & "h")
        End Function
    End Structure

    ''' <Summary>Stream callback structure.</Summary>
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Size:=64)>
    Public Structure KSTM_CALLBACK
        ''' <Summary>Executed when a transfer error occurs.</Summary>
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public [Error] As KSTM_ERROR_CB

        ''' <Summary>Executed to submit a transfer.</Summary>
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public Submit As KSTM_SUBMIT_CB

        ''' <Summary>Executed when a valid transfer completes.</Summary>
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public Complete As KSTM_COMPLETE_CB

        ''' <Summary>Executed for every transfer context when the stream is started with \ref StmK_Start.</Summary>
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public Started As KSTM_STARTED_CB

        ''' <Summary>Executed for every transfer context when the stream is stopped with \ref StmK_Stop.</Summary>
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public Stopped As KSTM_STOPPED_CB

        ''' <Summary>Executed immediately after a transfer completes.</Summary>
        <MarshalAs(UnmanagedType.FunctionPtr)>
        Public BeforeComplete As KSTM_BEFORE_COMPLETE_CB
    End Structure

#End Region

#Region "Delegates"

    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KLIB_HANDLE_CLEANUP_CB(
    <[In]> ByVal Handle As IntPtr, ByVal HandleType As KLIB_HANDLE_TYPE, ByVal UserContext As IntPtr) As Integer
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KISO_ENUM_PACKETS_CB(ByVal PacketIndex As UInteger,
    <[In]> ByRef IsoPacket As KISO_PACKET, ByVal UserState As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KISOCH_ENUM_PACKETS_CB(ByVal PacketIndex As UInteger, ByRef Offset As UInteger, ByRef Length As UInteger, ByRef Status As UInteger, ByVal UserState As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KLST_ENUM_DEVINFO_CB(
    <[In]> ByVal DeviceList As KLST_HANDLE,
    <[In]> ByVal DeviceInfo As KLST_DEVINFO_HANDLE, ByVal Context As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_InitDelegate(<Out> ByRef InterfaceHandle As KUSB_HANDLE,
    <[In]> ByVal DevInfo As KLST_DEVINFO_HANDLE) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_FreeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_ClaimInterfaceDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_ReleaseInterfaceDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_SetAltInterfaceDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean, ByVal AltSettingNumber As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetAltInterfaceDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean, <Out> ByRef AltSettingNumber As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetDescriptorDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal DescriptorType As Byte, ByVal Index As Byte, ByVal LanguageID As UShort, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_ControlTransferDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal SetupPacket As WINUSB_SETUP_PACKET, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_SetPowerPolicyDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PolicyType As UInteger, ByVal ValueLength As UInteger, ByVal Value As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetPowerPolicyDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PolicyType As UInteger, ByRef ValueLength As UInteger, ByVal Value As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_SetConfigurationDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal ConfigurationNumber As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetConfigurationDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, <Out> ByRef ConfigurationNumber As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_ResetDeviceDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_InitializeDelegate(ByVal DeviceHandle As IntPtr, <Out> ByRef InterfaceHandle As KUSB_HANDLE) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_SelectInterfaceDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetAssociatedInterfaceDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal AssociatedInterfaceIndex As Byte, <Out> ByRef AssociatedInterfaceHandle As KUSB_HANDLE) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_CloneDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, <Out> ByRef DstInterfaceHandle As KUSB_HANDLE) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_QueryInterfaceSettingsDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal AltSettingIndex As Byte, <Out> ByRef UsbAltInterfaceDescriptor As USB_INTERFACE_DESCRIPTOR) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_QueryDeviceInformationDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal InformationType As UInteger, ByRef BufferLength As UInteger, ByVal Buffer As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_SetCurrentAlternateSettingDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal AltSettingNumber As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetCurrentAlternateSettingDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, <Out> ByRef AltSettingNumber As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_QueryPipeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal AltSettingNumber As Byte, ByVal PipeIndex As Byte, <Out> ByRef PipeInformation As WINUSB_PIPE_INFORMATION) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_QueryPipeExDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal AlternateSettingNumber As Byte, ByVal PipeIndex As Byte, <Out> ByRef PipeInformationEx As WINUSB_PIPE_INFORMATION_EX) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetSuperSpeedPipeCompanionDescriptorDelegate(
    <[In]> ByVal Handle As KUSB_HANDLE, ByVal AltSettingNumber As Byte, ByVal PipeIndex As Byte, <Out> ByRef PipeCompanionDescriptor As USB_SUPERSPEED_ENDPOINT_COMPANION_DESCRIPTOR) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_SetPipePolicyDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal PolicyType As UInteger, ByVal ValueLength As UInteger, ByVal Value As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetPipePolicyDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal PolicyType As UInteger, ByRef ValueLength As UInteger, ByVal Value As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_ReadPipeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_WritePipeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_ResetPipeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_AbortPipeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_FlushPipeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_IsoReadPipeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, ByVal Overlapped As IntPtr,
    <[In]> ByVal IsoContext As KISO_CONTEXT) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_IsoWritePipeDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, ByVal Overlapped As IntPtr,
    <[In]> ByVal IsoContext As KISO_CONTEXT) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetCurrentFrameNumberDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, <Out> ByRef FrameNumber As UInteger) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetOverlappedResultDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal Overlapped As IntPtr, <Out> ByRef lpNumberOfBytesTransferred As UInteger, ByVal bWait As Boolean) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_GetPropertyDelegate(
    <[In]> ByVal InterfaceHandle As KUSB_HANDLE, ByVal PropertyType As KUSB_PROPERTY, ByRef PropertySize As UInteger, ByVal Value As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_IsochReadPipeDelegate(
    <[In]> ByVal IsochHandle As KISOCH_HANDLE, ByVal DataLength As UInteger, ByRef FrameNumber As UInteger, ByVal NumberOfPackets As UInteger, ByVal Overlapped As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KUSB_IsochWritePipeDelegate(
    <[In]> ByVal IsochHandle As KISOCH_HANDLE, ByVal DataLength As UInteger, ByRef FrameNumber As UInteger, ByVal NumberOfPackets As UInteger, ByVal Overlapped As IntPtr) As Boolean
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Sub KHOT_PLUG_CB(
    <[In]> ByVal HotHandle As KHOT_HANDLE,
    <[In]> ByVal DeviceInfo As KLST_DEVINFO_HANDLE, ByVal PlugType As KLST_SYNC_FLAG)
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Sub KHOT_POWER_BROADCAST_CB(
    <[In]> ByVal HotHandle As KHOT_HANDLE,
    <[In]> ByVal DeviceInfo As KLST_DEVINFO_HANDLE, ByVal PbtEvent As UInteger)
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KSTM_ERROR_CB(
    <[In]> ByVal StreamInfo As KSTM_INFO,
    <[In]> ByVal XferContext As KSTM_XFER_CONTEXT, ByVal XferContextIndex As Integer, ByVal ErrorCode As Integer) As Integer
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KSTM_SUBMIT_CB(
    <[In]> ByVal StreamInfo As KSTM_INFO,
    <[In]> ByVal XferContext As KSTM_XFER_CONTEXT, ByVal XferContextIndex As Integer, ByVal Overlapped As IntPtr) As Integer
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KSTM_STARTED_CB(
    <[In]> ByVal StreamInfo As KSTM_INFO,
    <[In]> ByVal XferContext As KSTM_XFER_CONTEXT, ByVal XferContextIndex As Integer) As Integer
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KSTM_STOPPED_CB(
    <[In]> ByVal StreamInfo As KSTM_INFO,
    <[In]> ByVal XferContext As KSTM_XFER_CONTEXT, ByVal XferContextIndex As Integer) As Integer
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KSTM_COMPLETE_CB(
    <[In]> ByVal StreamInfo As KSTM_INFO,
    <[In]> ByVal XferContext As KSTM_XFER_CONTEXT, ByVal XferContextIndex As Integer, ByVal ErrorCode As Integer) As Integer
    <UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet:=CharSet.Ansi, SetLastError:=True)>
    Public Delegate Function KSTM_BEFORE_COMPLETE_CB(
    <[In]> ByVal StreamInfo As KSTM_INFO,
    <[In]> ByVal XferContext As KSTM_XFER_CONTEXT, ByVal XferContextIndex As Integer, ByRef ErrorCode As Integer) As KSTM_COMPLETE_RESULT

#End Region

    Public Class LstK
        Implements IDisposable

        Protected mbDisposed As Boolean
        Protected mHandleStruct As KLST_HANDLE

        Protected Sub New()
        End Sub

        ''' <Summary>Initializes a new usb device list containing all supported devices.</Summary>
        Public Sub New(ByVal Flags As KLST_FLAG)
            Dim success = LstK_Init(mHandleStruct, Flags)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <Summary>Initializes a new usb device list containing only devices matching a specific class GUID.</Summary>
        Public Sub New(ByVal Flags As KLST_FLAG, ByRef PatternMatch As KLST_PATTERN_MATCH)
            Dim success = LstK_InitEx(mHandleStruct, Flags, PatternMatch)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <summary>Gets the handle class structure.</summary>
        Public ReadOnly Property Handle As KLST_HANDLE
            Get
                Return mHandleStruct
            End Get
        End Property

        ''' <summary>Explicitly closes and frees the handle.</summary>
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overrides Sub Finalize()
            Dispose(False)
        End Sub

        ''' <summary>Calls the dispose method.</summary>
        Public Overridable Sub Free()
            Dispose()
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not mbDisposed Then
                If mHandleStruct.Pointer <> IntPtr.Zero Then
                    LstK_Free(mHandleStruct)
                    Call Debug.Print("{0} Dispose: Freed Handle:{1:X16}h Explicit:{2}", [GetType]().Name, mHandleStruct.Pointer.ToInt64(), disposing)
                Else
                    Call Debug.Print("{0} Dispose: [WARNING] Handle is null", [GetType]().Name)
                End If

                mHandleStruct = New KLST_HANDLE(IntPtr.Zero)
                mbDisposed = True
            End If
        End Sub

        ''' <Summary>Initializes a new usb device list containing all supported devices.</Summary>
        Protected Function Init(ByVal Flags As KLST_FLAG) As Boolean
            Dim success = LstK_Init(mHandleStruct, Flags)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Initializes a new usb device list containing only devices matching a specific class GUID.</Summary>
        Protected Function InitEx(ByVal Flags As KLST_FLAG, ByRef PatternMatch As KLST_PATTERN_MATCH) As Boolean
            Dim success = LstK_InitEx(mHandleStruct, Flags, PatternMatch)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Enumerates \ref KLST_DEVINFO elements of a \ref KLST_HANDLE.</Summary>
        Public Overridable Function Enumerate(ByVal EnumDevListCB As KLST_ENUM_DEVINFO_CB, ByVal Context As IntPtr) As Boolean
            Return LstK_Enumerate(mHandleStruct, EnumDevListCB, Context)
        End Function

        ''' <Summary>Gets the \ref KLST_DEVINFO element for the current position.</Summary>
        Public Overridable Function Current(<Out> ByRef DeviceInfo As KLST_DEVINFO_HANDLE) As Boolean
            Return LstK_Current(mHandleStruct, DeviceInfo)
        End Function

        ''' <Summary>Advances the device list current \ref KLST_DEVINFO position.</Summary>
        Public Overridable Function MoveNext(<Out> ByRef DeviceInfo As KLST_DEVINFO_HANDLE) As Boolean
            Return LstK_MoveNext(mHandleStruct, DeviceInfo)
        End Function

        ''' <Summary>Sets the device list to its initial position, which is before the first element in the list.</Summary>
        Public Overridable Sub MoveReset()
            LstK_MoveReset(mHandleStruct)
        End Sub

        ''' <Summary>Find a device by vendor and product id</Summary>
        Public Overridable Function FindByVidPid(ByVal Vid As Integer, ByVal Pid As Integer, <Out> ByRef DeviceInfo As KLST_DEVINFO_HANDLE) As Boolean
            Return LstK_FindByVidPid(mHandleStruct, Vid, Pid, DeviceInfo)
        End Function

        ''' <Summary>Counts the number of device info elements in a device list.</Summary>
        Public Overridable Function Count(ByRef pCount As UInteger) As Boolean
            Return LstK_Count(mHandleStruct, pCount)
        End Function
    End Class

    Public Class HotK
        Implements IDisposable

        Protected mbDisposed As Boolean
        Protected mHandleStruct As KHOT_HANDLE

        Protected Sub New()
        End Sub

        ''' <Summary>Creates a new hot-plug handle for USB device arrival/removal event monitoring.</Summary>
        Public Sub New(ByRef InitParams As KHOT_PARAMS)
            Dim success = HotK_Init(mHandleStruct, InitParams)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <summary>Gets the handle class structure.</summary>
        Public ReadOnly Property Handle As KHOT_HANDLE
            Get
                Return mHandleStruct
            End Get
        End Property

        ''' <summary>Explicitly closes and frees the handle.</summary>
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overrides Sub Finalize()
            Dispose(False)
        End Sub

        ''' <summary>Calls the dispose method.</summary>
        Public Overridable Sub Free()
            Dispose()
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not mbDisposed Then
                If mHandleStruct.Pointer <> IntPtr.Zero Then
                    HotK_Free(mHandleStruct)
                    Call Debug.Print("{0} Dispose: Freed Handle:{1:X16}h Explicit:{2}", [GetType]().Name, mHandleStruct.Pointer.ToInt64(), disposing)
                Else
                    Call Debug.Print("{0} Dispose: [WARNING] Handle is null", [GetType]().Name)
                End If

                mHandleStruct = New KHOT_HANDLE(IntPtr.Zero)
                mbDisposed = True
            End If
        End Sub

        ''' <Summary>Creates a new hot-plug handle for USB device arrival/removal event monitoring.</Summary>
        Protected Function Init(ByRef InitParams As KHOT_PARAMS) As Boolean
            Dim success = HotK_Init(mHandleStruct, InitParams)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Frees all hot-plug handles initialized with \ref HotK_Init.</Summary>
        Public Shared Sub FreeAll()
            HotK_FreeAll()
        End Sub
    End Class

    Public Class UsbK
        Implements IDisposable

        Protected driverAPI As KUSB_DRIVER_API
        Protected mbDisposed As Boolean
        Protected mHandleStruct As KUSB_HANDLE

        Protected Sub New()
        End Sub

        ''' <Summary>Creates/opens a libusbK interface handle from the device list. This is a preferred method.</Summary>
        Public Sub New(ByVal DevInfo As KLST_DEVINFO_HANDLE)
            Dim success = LibK_LoadDriverAPI(driverAPI, DevInfo.DriverID)
            If Not success Then Throw New Exception(String.Format("{0} failed loading Driver API. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            success = driverAPI.Init(mHandleStruct, DevInfo)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing usb device. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <Summary>Creates a libusbK handle for the device specified by a file handle.</Summary>
        Public Sub New(ByVal DeviceHandle As IntPtr, ByVal driverID As KUSB_DRVID)
            Dim success = LibK_LoadDriverAPI(driverAPI, driverID)
            If Not success Then Throw New Exception(String.Format("{0} failed loading Driver API. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            success = driverAPI.Initialize(DeviceHandle, mHandleStruct)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing usb device. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <summary>Gets the handle class structure.</summary>
        Public ReadOnly Property Handle As KUSB_HANDLE
            Get
                Return mHandleStruct
            End Get
        End Property

        ''' <summary>Explicitly closes and frees the handle.</summary>
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overrides Sub Finalize()
            Dispose(False)
        End Sub

        ''' <summary>Calls the dispose method.</summary>
        Public Overridable Sub Free()
            Dispose()
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not mbDisposed Then
                If mHandleStruct.Pointer <> IntPtr.Zero Then
                    UsbK_Free(mHandleStruct)
                    Call Debug.Print("{0} Dispose: Freed Handle:{1:X16}h Explicit:{2}", [GetType]().Name, mHandleStruct.Pointer.ToInt64(), disposing)
                Else
                    Call Debug.Print("{0} Dispose: [WARNING] Handle is null", [GetType]().Name)
                End If

                mHandleStruct = New KUSB_HANDLE(IntPtr.Zero)
                mbDisposed = True
            End If
        End Sub

        ''' <Summary>Creates/opens a libusbK interface handle from the device list. This is a preferred method.</Summary>
        Protected Function Init(ByVal DevInfo As KLST_DEVINFO_HANDLE) As Boolean
            Dim success = LibK_LoadDriverAPI(driverAPI, DevInfo.DriverID)
            If Not success Then Throw New Exception(String.Format("{0} failed loading Driver API. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            success = driverAPI.Init(mHandleStruct, DevInfo)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing usb device. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Claims the specified interface by number or index.</Summary>
        Public Overridable Function ClaimInterface(ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean) As Boolean
            Return driverAPI.ClaimInterface(mHandleStruct, NumberOrIndex, IsIndex)
        End Function

        ''' <Summary>Releases the specified interface by number or index.</Summary>
        Public Overridable Function ReleaseInterface(ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean) As Boolean
            Return driverAPI.ReleaseInterface(mHandleStruct, NumberOrIndex, IsIndex)
        End Function

        ''' <Summary>Sets the alternate setting of the specified interface.</Summary>
        Public Overridable Function SetAltInterface(ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean, ByVal AltSettingNumber As Byte) As Boolean
            Return driverAPI.SetAltInterface(mHandleStruct, NumberOrIndex, IsIndex, AltSettingNumber)
        End Function

        ''' <Summary>Gets the alternate setting for the specified interface.</Summary>
        Public Overridable Function GetAltInterface(ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean, <Out> ByRef AltSettingNumber As Byte) As Boolean
            Return driverAPI.GetAltInterface(mHandleStruct, NumberOrIndex, IsIndex, AltSettingNumber)
        End Function

        ''' <Summary>Gets the requested descriptor. This is a synchronous operation.</Summary>
        Public Overridable Function GetDescriptor(ByVal DescriptorType As Byte, ByVal Index As Byte, ByVal LanguageID As Integer, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger) As Boolean
            Return driverAPI.GetDescriptor(mHandleStruct, DescriptorType, Index, LanguageID, Buffer, BufferLength, LengthTransferred)
        End Function

        ''' <Summary>Gets the requested descriptor. This is a synchronous operation.</Summary>
        Public Overridable Function GetDescriptor(ByVal DescriptorType As Byte, ByVal Index As Byte, ByVal LanguageID As Integer, ByVal Buffer As Array, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger) As Boolean
            Return driverAPI.GetDescriptor(mHandleStruct, DescriptorType, Index, LanguageID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, LengthTransferred)
        End Function

        ''' <Summary>Transmits control data over a default control endpoint.</Summary>
        Public Overridable Function ControlTransfer(ByVal SetupPacket As WINUSB_SETUP_PACKET, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
            Return driverAPI.ControlTransfer(mHandleStruct, SetupPacket, Buffer, BufferLength, LengthTransferred, Overlapped)
        End Function

        ''' <Summary>Transmits control data over a default control endpoint.</Summary>
        Public Overridable Function ControlTransfer(ByVal SetupPacket As WINUSB_SETUP_PACKET, ByVal Buffer As Array, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
            Return driverAPI.ControlTransfer(mHandleStruct, SetupPacket, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, LengthTransferred, Overlapped)
        End Function

        ''' <Summary>Transmits control data over a default control endpoint.</Summary>
        Public Overridable Function ControlTransfer(ByVal SetupPacket As WINUSB_SETUP_PACKET, ByVal Buffer As Array, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As KOVL_HANDLE) As Boolean
            Return driverAPI.ControlTransfer(mHandleStruct, SetupPacket, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, LengthTransferred, Overlapped.Pointer)
        End Function

        ''' <Summary>Transmits control data over a default control endpoint.</Summary>
        Public Overridable Function ControlTransfer(ByVal SetupPacket As WINUSB_SETUP_PACKET, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As KOVL_HANDLE) As Boolean
            Return driverAPI.ControlTransfer(mHandleStruct, SetupPacket, Buffer, BufferLength, LengthTransferred, Overlapped.Pointer)
        End Function

        ''' <Summary>Sets the power policy for a device.</Summary>
        Public Overridable Function SetPowerPolicy(ByVal PolicyType As UInteger, ByVal ValueLength As UInteger, ByVal Value As IntPtr) As Boolean
            Return driverAPI.SetPowerPolicy(mHandleStruct, PolicyType, ValueLength, Value)
        End Function

        ''' <Summary>Sets the power policy for a device.</Summary>
        Public Overridable Function SetPowerPolicy(ByVal PolicyType As UInteger, ByVal ValueLength As UInteger, ByVal Value As Array) As Boolean
            Return driverAPI.SetPowerPolicy(mHandleStruct, PolicyType, ValueLength, Marshal.UnsafeAddrOfPinnedArrayElement(Value, 0))
        End Function

        ''' <Summary>Gets the power policy for a device.</Summary>
        Public Overridable Function GetPowerPolicy(ByVal PolicyType As UInteger, ByRef ValueLength As UInteger, ByVal Value As IntPtr) As Boolean
            Return driverAPI.GetPowerPolicy(mHandleStruct, PolicyType, ValueLength, Value)
        End Function

        ''' <Summary>Gets the power policy for a device.</Summary>
        Public Overridable Function GetPowerPolicy(ByVal PolicyType As UInteger, ByRef ValueLength As UInteger, ByVal Value As Array) As Boolean
            Return driverAPI.GetPowerPolicy(mHandleStruct, PolicyType, ValueLength, Marshal.UnsafeAddrOfPinnedArrayElement(Value, 0))
        End Function

        ''' <Summary>Sets the device configuration number.</Summary>
        Public Overridable Function SetConfiguration(ByVal ConfigurationNumber As Byte) As Boolean
            Return driverAPI.SetConfiguration(mHandleStruct, ConfigurationNumber)
        End Function

        ''' <Summary>Gets the device current configuration number.</Summary>
        Public Overridable Function GetConfiguration(<Out> ByRef ConfigurationNumber As Byte) As Boolean
            Return driverAPI.GetConfiguration(mHandleStruct, ConfigurationNumber)
        End Function

        ''' <Summary>Resets the usb device of the specified interface handle. (port cycle).</Summary>
        Public Overridable Function ResetDevice() As Boolean
            Return driverAPI.ResetDevice(mHandleStruct)
        End Function

        ''' <Summary>Creates a libusbK handle for the device specified by a file handle.</Summary>
        Protected Function Initialize(ByVal DeviceHandle As IntPtr, ByVal driverID As KUSB_DRVID) As Boolean
            Dim success = LibK_LoadDriverAPI(driverAPI, driverID)
            If Not success Then Throw New Exception(String.Format("{0} failed loading Driver API. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            success = driverAPI.Initialize(DeviceHandle, mHandleStruct)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing usb device. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Selects the specified interface by number or index as the current interface.</Summary>
        Public Overridable Function SelectInterface(ByVal NumberOrIndex As Byte, ByVal IsIndex As Boolean) As Boolean
            Return driverAPI.SelectInterface(mHandleStruct, NumberOrIndex, IsIndex)
        End Function

        ''' <Summary>Retrieves a handle for an associated interface.</Summary>
        Public Overridable Function GetAssociatedInterface(ByVal AssociatedInterfaceIndex As Byte, <Out> ByRef AssociatedInterfaceHandle As KUSB_HANDLE) As Boolean
            Return driverAPI.GetAssociatedInterface(mHandleStruct, AssociatedInterfaceIndex, AssociatedInterfaceHandle)
        End Function

        ''' <Summary>Clones the specified interface handle.</Summary>
        Public Overridable Function Clone(<Out> ByRef DstInterfaceHandle As KUSB_HANDLE) As Boolean
            Return driverAPI.Clone(mHandleStruct, DstInterfaceHandle)
        End Function

        ''' <Summary>
        '''     Retrieves the interface descriptor for the specified alternate interface settings for a particular interface
        '''     handle.
        ''' </Summary>
        Public Overridable Function QueryInterfaceSettings(ByVal AltSettingIndex As Byte, <Out> ByRef UsbAltInterfaceDescriptor As USB_INTERFACE_DESCRIPTOR) As Boolean
            Return driverAPI.QueryInterfaceSettings(mHandleStruct, AltSettingIndex, UsbAltInterfaceDescriptor)
        End Function

        ''' <Summary>Retrieves information about the physical device that is associated with a libusbK handle.</Summary>
        Public Overridable Function QueryDeviceInformation(ByVal InformationType As UInteger, ByRef BufferLength As UInteger, ByVal Buffer As IntPtr) As Boolean
            Return driverAPI.QueryDeviceInformation(mHandleStruct, InformationType, BufferLength, Buffer)
        End Function

        ''' <Summary>Sets the alternate setting of an interface.</Summary>
        Public Overridable Function SetCurrentAlternateSetting(ByVal AltSettingNumber As Byte) As Boolean
            Return driverAPI.SetCurrentAlternateSetting(mHandleStruct, AltSettingNumber)
        End Function

        ''' <Summary>Gets the current alternate interface setting for an interface.</Summary>
        Public Overridable Function GetCurrentAlternateSetting(<Out> ByRef AltSettingNumber As Byte) As Boolean
            Return driverAPI.GetCurrentAlternateSetting(mHandleStruct, AltSettingNumber)
        End Function

        ''' <Summary>Retrieves information about a pipe that is associated with an interface.</Summary>
        Public Overridable Function QueryPipe(ByVal AltSettingNumber As Byte, ByVal PipeIndex As Byte, <Out> ByRef PipeInformation As WINUSB_PIPE_INFORMATION) As Boolean
            Return driverAPI.QueryPipe(mHandleStruct, AltSettingNumber, PipeIndex, PipeInformation)
        End Function

        ''' <Summary>Retrieves information about a pipe that is associated with an interface.</Summary>
        Public Overridable Function QueryPipeEx(ByVal AltSettingNumber As Byte, ByVal PipeIndex As Byte, <Out> ByRef PipeInformationEx As WINUSB_PIPE_INFORMATION_EX) As Boolean
            Return driverAPI.QueryPipeEx(mHandleStruct, AltSettingNumber, PipeIndex, PipeInformationEx)
        End Function

        ''' <Summary>Retrieves a pipes super speed endpoint companion descriptor associated with an interface.</Summary>
        Public Overridable Function GetSuperSpeedPipeCompanionDescriptor(ByVal AltSettingNumber As Byte, ByVal PipeIndex As Byte, <Out> ByRef PipeCompanionDescriptor As USB_SUPERSPEED_ENDPOINT_COMPANION_DESCRIPTOR) As Boolean
            Return driverAPI.GetSuperSpeedPipeCompanionDescriptor(mHandleStruct, AltSettingNumber, PipeIndex, PipeCompanionDescriptor)
        End Function

        ''' <Summary>
        '''     Sets the policy for a specific pipe associated with an endpoint on the device. This is a synchronous
        '''     operation.
        ''' </Summary>
        Public Overridable Function SetPipePolicy(ByVal PipeID As Byte, ByVal PolicyType As UInteger, ByVal ValueLength As UInteger, ByVal Value As IntPtr) As Boolean
            Return driverAPI.SetPipePolicy(mHandleStruct, PipeID, PolicyType, ValueLength, Value)
        End Function

        ''' <Summary>
        '''     Sets the policy for a specific pipe associated with an endpoint on the device. This is a synchronous
        '''     operation.
        ''' </Summary>
        Public Overridable Function SetPipePolicy(ByVal PipeID As Byte, ByVal PolicyType As UInteger, ByVal ValueLength As UInteger, ByVal Value As Array) As Boolean
            Return driverAPI.SetPipePolicy(mHandleStruct, PipeID, PolicyType, ValueLength, Marshal.UnsafeAddrOfPinnedArrayElement(Value, 0))
        End Function

        ''' <Summary>Gets the policy for a specific pipe (endpoint).</Summary>
        Public Overridable Function GetPipePolicy(ByVal PipeID As Byte, ByVal PolicyType As UInteger, ByRef ValueLength As UInteger, ByVal Value As IntPtr) As Boolean
            Return driverAPI.GetPipePolicy(mHandleStruct, PipeID, PolicyType, ValueLength, Value)
        End Function

        ''' <Summary>Gets the policy for a specific pipe (endpoint).</Summary>
        Public Overridable Function GetPipePolicy(ByVal PipeID As Byte, ByVal PolicyType As UInteger, ByRef ValueLength As UInteger, ByVal Value As Array) As Boolean
            Return driverAPI.GetPipePolicy(mHandleStruct, PipeID, PolicyType, ValueLength, Marshal.UnsafeAddrOfPinnedArrayElement(Value, 0))
        End Function

        ''' <Summary>Reads data from the specified pipe.</Summary>
        Public Overridable Function ReadPipe(ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
            Return driverAPI.ReadPipe(mHandleStruct, PipeID, Buffer, BufferLength, LengthTransferred, Overlapped)
        End Function

        ''' <Summary>Reads data from the specified pipe.</Summary>
        Public Overridable Function ReadPipe(ByVal PipeID As Byte, ByVal Buffer As Array, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
            Return driverAPI.ReadPipe(mHandleStruct, PipeID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, LengthTransferred, Overlapped)
        End Function

        ''' <Summary>Reads data from the specified pipe.</Summary>
        Public Overridable Function ReadPipe(ByVal PipeID As Byte, ByVal Buffer As Array, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As KOVL_HANDLE) As Boolean
            Return driverAPI.ReadPipe(mHandleStruct, PipeID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, LengthTransferred, Overlapped.Pointer)
        End Function

        ''' <Summary>Reads data from the specified pipe.</Summary>
        Public Overridable Function ReadPipe(ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As KOVL_HANDLE) As Boolean
            Return driverAPI.ReadPipe(mHandleStruct, PipeID, Buffer, BufferLength, LengthTransferred, Overlapped.Pointer)
        End Function

        ''' <Summary>Writes data to a pipe.</Summary>
        Public Overridable Function WritePipe(ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
            Return driverAPI.WritePipe(mHandleStruct, PipeID, Buffer, BufferLength, LengthTransferred, Overlapped)
        End Function

        ''' <Summary>Writes data to a pipe.</Summary>
        Public Overridable Function WritePipe(ByVal PipeID As Byte, ByVal Buffer As Array, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As IntPtr) As Boolean
            Return driverAPI.WritePipe(mHandleStruct, PipeID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, LengthTransferred, Overlapped)
        End Function

        ''' <Summary>Writes data to a pipe.</Summary>
        Public Overridable Function WritePipe(ByVal PipeID As Byte, ByVal Buffer As Array, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As KOVL_HANDLE) As Boolean
            Return driverAPI.WritePipe(mHandleStruct, PipeID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, LengthTransferred, Overlapped.Pointer)
        End Function

        ''' <Summary>Writes data to a pipe.</Summary>
        Public Overridable Function WritePipe(ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, <Out> ByRef LengthTransferred As UInteger, ByVal Overlapped As KOVL_HANDLE) As Boolean
            Return driverAPI.WritePipe(mHandleStruct, PipeID, Buffer, BufferLength, LengthTransferred, Overlapped.Pointer)
        End Function

        ''' <Summary>Resets the data toggle and clears the stall condition on a pipe.</Summary>
        Public Overridable Function ResetPipe(ByVal PipeID As Byte) As Boolean
            Return driverAPI.ResetPipe(mHandleStruct, PipeID)
        End Function

        ''' <Summary>Aborts all of the pending transfers for a pipe.</Summary>
        Public Overridable Function AbortPipe(ByVal PipeID As Byte) As Boolean
            Return driverAPI.AbortPipe(mHandleStruct, PipeID)
        End Function

        ''' <Summary>Discards any data that is cached in a pipe.</Summary>
        Public Overridable Function FlushPipe(ByVal PipeID As Byte) As Boolean
            Return driverAPI.FlushPipe(mHandleStruct, PipeID)
        End Function

        ''' <Summary>Reads from an isochronous pipe.</Summary>
        Public Overridable Function IsoReadPipe(ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, ByVal Overlapped As IntPtr, ByVal IsoContext As KISO_CONTEXT) As Boolean
            Return driverAPI.IsoReadPipe(mHandleStruct, PipeID, Buffer, BufferLength, Overlapped, IsoContext)
        End Function

        ''' <Summary>Reads from an isochronous pipe.</Summary>
        Public Overridable Function IsoReadPipe(ByVal PipeID As Byte, ByVal Buffer As Array, ByVal BufferLength As UInteger, ByVal Overlapped As IntPtr, ByVal IsoContext As KISO_CONTEXT) As Boolean
            Return driverAPI.IsoReadPipe(mHandleStruct, PipeID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, Overlapped, IsoContext)
        End Function

        ''' <Summary>Reads from an isochronous pipe.</Summary>
        Public Overridable Function IsoReadPipe(ByVal PipeID As Byte, ByVal Buffer As Array, ByVal BufferLength As UInteger, ByVal Overlapped As KOVL_HANDLE, ByVal IsoContext As KISO_CONTEXT) As Boolean
            Return driverAPI.IsoReadPipe(mHandleStruct, PipeID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, Overlapped.Pointer, IsoContext)
        End Function

        ''' <Summary>Reads from an isochronous pipe.</Summary>
        Public Overridable Function IsoReadPipe(ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, ByVal Overlapped As KOVL_HANDLE, ByVal IsoContext As KISO_CONTEXT) As Boolean
            Return driverAPI.IsoReadPipe(mHandleStruct, PipeID, Buffer, BufferLength, Overlapped.Pointer, IsoContext)
        End Function

        ''' <Summary>Writes to an isochronous pipe.</Summary>
        Public Overridable Function IsoWritePipe(ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, ByVal Overlapped As IntPtr, ByVal IsoContext As KISO_CONTEXT) As Boolean
            Return driverAPI.IsoWritePipe(mHandleStruct, PipeID, Buffer, BufferLength, Overlapped, IsoContext)
        End Function

        ''' <Summary>Writes to an isochronous pipe.</Summary>
        Public Overridable Function IsoWritePipe(ByVal PipeID As Byte, ByVal Buffer As Array, ByVal BufferLength As UInteger, ByVal Overlapped As IntPtr, ByVal IsoContext As KISO_CONTEXT) As Boolean
            Return driverAPI.IsoWritePipe(mHandleStruct, PipeID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, Overlapped, IsoContext)
        End Function

        ''' <Summary>Writes to an isochronous pipe.</Summary>
        Public Overridable Function IsoWritePipe(ByVal PipeID As Byte, ByVal Buffer As Array, ByVal BufferLength As UInteger, ByVal Overlapped As KOVL_HANDLE, ByVal IsoContext As KISO_CONTEXT) As Boolean
            Return driverAPI.IsoWritePipe(mHandleStruct, PipeID, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), BufferLength, Overlapped.Pointer, IsoContext)
        End Function

        ''' <Summary>Writes to an isochronous pipe.</Summary>
        Public Overridable Function IsoWritePipe(ByVal PipeID As Byte, ByVal Buffer As IntPtr, ByVal BufferLength As UInteger, ByVal Overlapped As KOVL_HANDLE, ByVal IsoContext As KISO_CONTEXT) As Boolean
            Return driverAPI.IsoWritePipe(mHandleStruct, PipeID, Buffer, BufferLength, Overlapped.Pointer, IsoContext)
        End Function

        ''' <Summary>Retrieves the current USB frame number.</Summary>
        Public Overridable Function GetCurrentFrameNumber(<Out> ByRef FrameNumber As UInteger) As Boolean
            Return driverAPI.GetCurrentFrameNumber(mHandleStruct, FrameNumber)
        End Function

        ''' <Summary>Reads from an isochronous pipe. Supports LibusbK or WinUsb</Summary>
        Public Overridable Function IsochReadPipe(ByVal IsochHandle As KISOCH_HANDLE, ByVal DataLength As UInteger, ByRef FrameNumber As UInteger, ByVal NumberOfPackets As UInteger, ByVal Overlapped As IntPtr) As Boolean
            Return driverAPI.IsochReadPipe(IsochHandle, DataLength, FrameNumber, NumberOfPackets, Overlapped)
        End Function

        ''' <Summary>Reads from an isochronous pipe. Supports LibusbK or WinUsb</Summary>
        Public Overridable Function IsochReadPipe(ByVal IsochHandle As KISOCH_HANDLE, ByVal DataLength As UInteger, ByRef FrameNumber As UInteger, ByVal NumberOfPackets As UInteger, ByVal Overlapped As KOVL_HANDLE) As Boolean
            Return driverAPI.IsochReadPipe(IsochHandle, DataLength, FrameNumber, NumberOfPackets, Overlapped.Pointer)
        End Function

        ''' <Summary>Writes to an isochronous pipe. Supports LibusbK or WinUsb</Summary>
        Public Overridable Function IsochWritePipe(ByVal IsochHandle As KISOCH_HANDLE, ByVal DataLength As UInteger, ByRef FrameNumber As UInteger, ByVal NumberOfPackets As UInteger, ByVal Overlapped As IntPtr) As Boolean
            Return driverAPI.IsochWritePipe(IsochHandle, DataLength, FrameNumber, NumberOfPackets, Overlapped)
        End Function

        ''' <Summary>Writes to an isochronous pipe. Supports LibusbK or WinUsb</Summary>
        Public Overridable Function IsochWritePipe(ByVal IsochHandle As KISOCH_HANDLE, ByVal DataLength As UInteger, ByRef FrameNumber As UInteger, ByVal NumberOfPackets As UInteger, ByVal Overlapped As KOVL_HANDLE) As Boolean
            Return driverAPI.IsochWritePipe(IsochHandle, DataLength, FrameNumber, NumberOfPackets, Overlapped.Pointer)
        End Function

        ''' <Summary>Retrieves the results of an overlapped operation on the specified libusbK handle.</Summary>
        Public Overridable Function GetOverlappedResult(ByVal Overlapped As IntPtr, <Out> ByRef lpNumberOfBytesTransferred As UInteger, ByVal bWait As Boolean) As Boolean
            Return driverAPI.GetOverlappedResult(mHandleStruct, Overlapped, lpNumberOfBytesTransferred, bWait)
        End Function

        ''' <Summary>Retrieves the results of an overlapped operation on the specified libusbK handle.</Summary>
        Public Overridable Function GetOverlappedResult(ByVal Overlapped As KOVL_HANDLE, <Out> ByRef lpNumberOfBytesTransferred As UInteger, ByVal bWait As Boolean) As Boolean
            Return driverAPI.GetOverlappedResult(mHandleStruct, Overlapped.Pointer, lpNumberOfBytesTransferred, bWait)
        End Function

        ''' <Summary>Gets a USB device (driver specific) property from usb handle.</Summary>
        Public Overridable Function GetProperty(ByVal PropertyType As KUSB_PROPERTY, ByRef PropertySize As UInteger, ByVal Value As IntPtr) As Boolean
            Return driverAPI.GetProperty(mHandleStruct, PropertyType, PropertySize, Value)
        End Function

        ''' <Summary>Gets a USB device (driver specific) property from usb handle.</Summary>
        Public Overridable Function GetProperty(ByVal PropertyType As KUSB_PROPERTY, ByRef PropertySize As UInteger, ByVal Value As Array) As Boolean
            Return driverAPI.GetProperty(mHandleStruct, PropertyType, PropertySize, Marshal.UnsafeAddrOfPinnedArrayElement(Value, 0))
        End Function
    End Class

    Public Class OvlK
        Implements IDisposable

        Protected mbDisposed As Boolean
        Protected mHandleStruct As KOVL_POOL_HANDLE

        Protected Sub New()
        End Sub

        ''' <Summary>Creates a new overlapped pool.</Summary>
        Public Sub New(ByVal UsbHandle As KUSB_HANDLE, ByVal MaxOverlappedCount As Integer, ByVal Flags As KOVL_POOL_FLAG)
            Dim success = OvlK_Init(mHandleStruct, UsbHandle, MaxOverlappedCount, Flags)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <summary>Gets the handle class structure.</summary>
        Public ReadOnly Property Handle As KOVL_POOL_HANDLE
            Get
                Return mHandleStruct
            End Get
        End Property

        ''' <summary>Explicitly closes and frees the handle.</summary>
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overrides Sub Finalize()
            Dispose(False)
        End Sub

        ''' <summary>Calls the dispose method.</summary>
        Public Overridable Sub Free()
            Dispose()
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not mbDisposed Then
                If mHandleStruct.Pointer <> IntPtr.Zero Then
                    OvlK_Free(mHandleStruct)
                    Call Debug.Print("{0} Dispose: Freed Handle:{1:X16}h Explicit:{2}", [GetType]().Name, mHandleStruct.Pointer.ToInt64(), disposing)
                Else
                    Call Debug.Print("{0} Dispose: [WARNING] Handle is null", [GetType]().Name)
                End If

                mHandleStruct = New KOVL_POOL_HANDLE(IntPtr.Zero)
                mbDisposed = True
            End If
        End Sub

        ''' <Summary>Gets a preallocated \c OverlappedK structure from the specified/default pool.</Summary>
        Public Overridable Function Acquire(<Out> ByRef OverlappedK As KOVL_HANDLE) As Boolean
            Return OvlK_Acquire(OverlappedK, mHandleStruct)
        End Function

        ''' <Summary>Returns an \c OverlappedK structure to it's pool.</Summary>
        Public Shared Function Release(ByVal OverlappedK As KOVL_HANDLE) As Boolean
            Return OvlK_Release(OverlappedK)
        End Function

        ''' <Summary>Creates a new overlapped pool.</Summary>
        Protected Function Init(ByVal UsbHandle As KUSB_HANDLE, ByVal MaxOverlappedCount As Integer, ByVal Flags As KOVL_POOL_FLAG) As Boolean
            Dim success = OvlK_Init(mHandleStruct, UsbHandle, MaxOverlappedCount, Flags)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Returns the internal event handle used to signal IO operations.</Summary>
        Public Shared Function GetEventHandle(ByVal OverlappedK As KOVL_HANDLE) As IntPtr
            Return OvlK_GetEventHandle(OverlappedK)
        End Function

        ''' <Summary>Waits for overlapped I/O completion, and performs actions specified in \c WaitFlags.</Summary>
        Public Shared Function Wait(ByVal OverlappedK As KOVL_HANDLE, ByVal TimeoutMS As Integer, ByVal WaitFlags As KOVL_WAIT_FLAG, <Out> ByRef TransferredLength As UInteger) As Boolean
            Return OvlK_Wait(OverlappedK, TimeoutMS, WaitFlags, TransferredLength)
        End Function

        ''' <Summary>
        '''     Waits for overlapped I/O completion on the oldest acquired OverlappedK handle and performs actions specified
        '''     in \c WaitFlags.
        ''' </Summary>
        Public Overridable Function WaitOldest(<Out> ByRef OverlappedK As KOVL_HANDLE, ByVal TimeoutMS As Integer, ByVal WaitFlags As KOVL_WAIT_FLAG, <Out> ByRef TransferredLength As UInteger) As Boolean
            Return OvlK_WaitOldest(mHandleStruct, OverlappedK, TimeoutMS, WaitFlags, TransferredLength)
        End Function

        ''' <Summary>Waits for overlapped I/O completion, cancels on a timeout error.</Summary>
        Public Shared Function WaitOrCancel(ByVal OverlappedK As KOVL_HANDLE, ByVal TimeoutMS As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
            Return OvlK_WaitOrCancel(OverlappedK, TimeoutMS, TransferredLength)
        End Function

        ''' <Summary>
        '''     Waits for overlapped I/O completion, cancels on a timeout error and always releases the OvlK handle back to
        '''     its pool.
        ''' </Summary>
        Public Shared Function WaitAndRelease(ByVal OverlappedK As KOVL_HANDLE, ByVal TimeoutMS As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
            Return OvlK_WaitAndRelease(OverlappedK, TimeoutMS, TransferredLength)
        End Function

        ''' <Summary>Checks for i/o completion; returns immediately. (polling)</Summary>
        Public Shared Function IsComplete(ByVal OverlappedK As KOVL_HANDLE) As Boolean
            Return OvlK_IsComplete(OverlappedK)
        End Function

        ''' <Summary>Initializes an overlappedK for re-use. The overlappedK is not return to its pool.</Summary>
        Public Shared Function ReUse(ByVal OverlappedK As KOVL_HANDLE) As Boolean
            Return OvlK_ReUse(OverlappedK)
        End Function
    End Class

    Public Class StmK
        Implements IDisposable

        Protected mbDisposed As Boolean
        Protected mHandleStruct As KSTM_HANDLE

        Protected Sub New()
        End Sub

        ''' <Summary>Initializes a new uni-directional pipe stream.</Summary>
        Public Sub New(ByVal UsbHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal MaxTransferSize As Integer, ByVal MaxPendingTransfers As Integer, ByVal MaxPendingIO As Integer, ByRef Callbacks As KSTM_CALLBACK, ByVal Flags As KSTM_FLAG)
            Dim success = StmK_Init(mHandleStruct, UsbHandle, PipeID, MaxTransferSize, MaxPendingTransfers, MaxPendingIO, Callbacks, Flags)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <summary>Gets the handle class structure.</summary>
        Public ReadOnly Property Handle As KSTM_HANDLE
            Get
                Return mHandleStruct
            End Get
        End Property

        ''' <summary>Explicitly closes and frees the handle.</summary>
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overrides Sub Finalize()
            Dispose(False)
        End Sub

        ''' <summary>Calls the dispose method.</summary>
        Public Overridable Sub Free()
            Dispose()
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not mbDisposed Then
                If mHandleStruct.Pointer <> IntPtr.Zero Then
                    StmK_Free(mHandleStruct)
                    Call Debug.Print("{0} Dispose: Freed Handle:{1:X16}h Explicit:{2}", [GetType]().Name, mHandleStruct.Pointer.ToInt64(), disposing)
                Else
                    Call Debug.Print("{0} Dispose: [WARNING] Handle is null", [GetType]().Name)
                End If

                mHandleStruct = New KSTM_HANDLE(IntPtr.Zero)
                mbDisposed = True
            End If
        End Sub

        ''' <Summary>Initializes a new uni-directional pipe stream.</Summary>
        Protected Function Init(ByVal UsbHandle As KUSB_HANDLE, ByVal PipeID As Byte, ByVal MaxTransferSize As Integer, ByVal MaxPendingTransfers As Integer, ByVal MaxPendingIO As Integer, ByRef Callbacks As KSTM_CALLBACK, ByVal Flags As KSTM_FLAG) As Boolean
            Dim success = StmK_Init(mHandleStruct, UsbHandle, PipeID, MaxTransferSize, MaxPendingTransfers, MaxPendingIO, Callbacks, Flags)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Starts the internal stream thread.</Summary>
        Public Overridable Function Start() As Boolean
            Return StmK_Start(mHandleStruct)
        End Function

        ''' <Summary>Stops the internal stream thread.</Summary>
        Public Overridable Function [Stop](ByVal TimeoutCancelMS As Integer) As Boolean
            Return StmK_Stop(mHandleStruct, TimeoutCancelMS)
        End Function

        ''' <Summary>Reads data from the stream buffer.</Summary>
        Public Overridable Function Read(ByVal Buffer As IntPtr, ByVal Offset As Integer, ByVal Length As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
            Return StmK_Read(mHandleStruct, Buffer, Offset, Length, TransferredLength)
        End Function

        ''' <Summary>Reads data from the stream buffer.</Summary>
        Public Overridable Function Read(ByVal Buffer As Array, ByVal Offset As Integer, ByVal Length As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
            Return StmK_Read(mHandleStruct, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), Offset, Length, TransferredLength)
        End Function

        ''' <Summary>Writes data to the stream buffer.</Summary>
        Public Overridable Function Write(ByVal Buffer As IntPtr, ByVal Offset As Integer, ByVal Length As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
            Return StmK_Write(mHandleStruct, Buffer, Offset, Length, TransferredLength)
        End Function

        ''' <Summary>Writes data to the stream buffer.</Summary>
        Public Overridable Function Write(ByVal Buffer As Array, ByVal Offset As Integer, ByVal Length As Integer, <Out> ByRef TransferredLength As UInteger) As Boolean
            Return StmK_Write(mHandleStruct, Marshal.UnsafeAddrOfPinnedArrayElement(Buffer, 0), Offset, Length, TransferredLength)
        End Function
    End Class

    Public Class IsoK
        Implements IDisposable

        Private Shared ReadOnly ofsFlags As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "Flags").ToInt32()
        Private Shared ReadOnly ofsStartFrame As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "StartFrame").ToInt32()
        Private Shared ReadOnly ofsErrorCount As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "ErrorCount").ToInt32()
        Private Shared ReadOnly ofsNumberOfPackets As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "NumberOfPackets").ToInt32()
        Private Shared ReadOnly ofsUrbHdrStatus As Integer = Marshal.OffsetOf(GetType(KISO_CONTEXT_MAP), "UrbHdrStatus").ToInt32()
        Protected mbDisposed As Boolean
        Protected mHandleStruct As KISO_CONTEXT

        Protected Sub New()
        End Sub


        ''' <Summary>Creates a new isochronous transfer context for libusbK only.</Summary>
        Public Sub New(ByVal NumberOfPackets As Integer, ByVal StartFrame As Integer)
            Dim success = IsoK_Init(mHandleStruct, NumberOfPackets, StartFrame)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <summary>Gets the handle class structure.</summary>
        Public ReadOnly Property Handle As KISO_CONTEXT
            Get
                Return mHandleStruct
            End Get
        End Property


        ''' <Summary>Additional ISO transfer flags. See \ref KISO_FLAG.</Summary>
        Public Property Flags As KISO_FLAG
            Get
                Return Marshal.ReadInt32(mHandleStruct.Pointer, ofsFlags)
            End Get
            Set(ByVal value As KISO_FLAG)
                Marshal.WriteInt32(mHandleStruct.Pointer, ofsFlags, value)
            End Set
        End Property


        ''' <Summary>Specifies the frame number that the transfer should begin on (0 for ASAP).</Summary>

        Public Property StartFrame As UInteger
            Get
                Return Marshal.ReadInt32(mHandleStruct.Pointer, ofsStartFrame)
            End Get
            Set(ByVal value As UInteger)
                Marshal.WriteInt32(mHandleStruct.Pointer, ofsStartFrame, CInt(value))
            End Set
        End Property


        ''' <Summary>
        '''     Contains the number of packets that completed with an error condition on return from the host controller
        '''     driver.
        ''' </Summary>

        Public Property ErrorCount As Short
            Get
                Return Marshal.ReadInt16(mHandleStruct.Pointer, ofsErrorCount)
            End Get
            Set(ByVal value As Short)
                Marshal.WriteInt16(mHandleStruct.Pointer, ofsErrorCount, value)
            End Set
        End Property


        ''' <Summary>Specifies the number of packets that are described by the variable-length array member \c IsoPacket.</Summary>

        Public Property NumberOfPackets As Short
            Get
                Return Marshal.ReadInt16(mHandleStruct.Pointer, ofsNumberOfPackets)
            End Get
            Set(ByVal value As Short)
                Marshal.WriteInt16(mHandleStruct.Pointer, ofsNumberOfPackets, value)
            End Set
        End Property


        ''' <Summary>Contains the URB Hdr.Status value on return from the host controller driver.</Summary>

        Public Property UrbHdrStatus As UInteger
            Get
                Return Marshal.ReadInt32(mHandleStruct.Pointer, ofsUrbHdrStatus)
            End Get
            Set(ByVal value As UInteger)
                Marshal.WriteInt32(mHandleStruct.Pointer, ofsUrbHdrStatus, CInt(value))
            End Set
        End Property

        ''' <summary>Explicitly closes and frees the handle.</summary>
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overrides Sub Finalize()
            Dispose(False)
        End Sub

        ''' <summary>Calls the dispose method.</summary>
        Public Overridable Sub Free()
            Dispose()
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not mbDisposed Then
                If mHandleStruct.Pointer <> IntPtr.Zero Then
                    IsoK_Free(mHandleStruct)
                    Call Debug.Print("{0} Dispose: Freed Handle:{1:X16}h Explicit:{2}", [GetType]().Name, mHandleStruct.Pointer.ToInt64(), disposing)
                Else
                    Call Debug.Print("{0} Dispose: [WARNING] Handle is null", [GetType]().Name)
                End If

                mHandleStruct = New KISO_CONTEXT(IntPtr.Zero)
                mbDisposed = True
            End If
        End Sub

        ''' <Summary>Creates a new isochronous transfer context for libusbK only.</Summary>
        Protected Function Init(ByVal NumberOfPackets As Integer, ByVal StartFrame As Integer) As Boolean
            Dim success = IsoK_Init(mHandleStruct, NumberOfPackets, StartFrame)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Convenience function for setting the offset of all ISO packets of an isochronous transfer context.</Summary>
        Public Overridable Function SetPackets(ByVal PacketSize As Integer) As Boolean
            Return IsoK_SetPackets(mHandleStruct, PacketSize)
        End Function

        ''' <Summary>Convenience function for setting all fields of a \ref KISO_PACKET.</Summary>
        Public Overridable Function SetPacket(ByVal PacketIndex As Integer, ByRef IsoPacket As KISO_PACKET) As Boolean
            Return IsoK_SetPacket(mHandleStruct, PacketIndex, IsoPacket)
        End Function

        ''' <Summary>Convenience function for getting all fields of a \ref KISO_PACKET.</Summary>
        Public Overridable Function GetPacket(ByVal PacketIndex As Integer, <Out> ByRef IsoPacket As KISO_PACKET) As Boolean
            Return IsoK_GetPacket(mHandleStruct, PacketIndex, IsoPacket)
        End Function

        ''' <Summary>Convenience function for enumerating ISO packets of an isochronous transfer context.</Summary>
        Public Overridable Function EnumPackets(ByVal pEnumPackets As KISO_ENUM_PACKETS_CB, ByVal StartPacketIndex As Integer, ByVal UserState As IntPtr) As Boolean
            Return IsoK_EnumPackets(mHandleStruct, pEnumPackets, StartPacketIndex, UserState)
        End Function

        ''' <Summary>Convenience function for re-using an isochronous transfer context in a subsequent request.</Summary>
        Public Overridable Function ReUse() As Boolean
            Return IsoK_ReUse(mHandleStruct)
        End Function

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
        Private Structure KISO_CONTEXT_MAP
            ''' <Summary>Additional ISO transfer flags. See \ref KISO_FLAG.</Summary>
            Private ReadOnly Flags As KISO_FLAG

            ''' <Summary>Specifies the frame number that the transfer should begin on (0 for ASAP).</Summary>
            Private ReadOnly StartFrame As UInteger

            ''' <Summary>
            '''     Contains the number of packets that completed with an error condition on return from the host controller
            '''     driver.
            ''' </Summary>
            Private ReadOnly ErrorCount As Short

            ''' <Summary>Specifies the number of packets that are described by the variable-length array member \c IsoPacket.</Summary>
            Private ReadOnly NumberOfPackets As Short

            ''' <Summary>Contains the URB Hdr.Status value on return from the host controller driver.</Summary>
            Private ReadOnly UrbHdrStatus As UInteger
        End Structure
    End Class

    Public Class IsochK
        Implements IDisposable

        Protected mbDisposed As Boolean
        Protected mHandleStruct As KISOCH_HANDLE

        Protected Sub New()
        End Sub

        ''' <Summary>Creates a new isochronous transfer handle for libusbK or WinUSB.</Summary>
        Public Sub New(ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeId As Byte, ByVal MaxNumberOfPackets As UInteger, ByVal TransferBuffer As IntPtr, ByVal TransferBufferSize As UInteger)
            Dim success = IsochK_Init(mHandleStruct, InterfaceHandle, PipeId, MaxNumberOfPackets, TransferBuffer, TransferBufferSize)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
        End Sub

        ''' <summary>Gets the handle class structure.</summary>
        Public ReadOnly Property Handle As KISOCH_HANDLE
            Get
                Return mHandleStruct
            End Get
        End Property

        ''' <summary>Explicitly closes and frees the handle.</summary>
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overrides Sub Finalize()
            Dispose(False)
        End Sub

        ''' <summary>Calls the dispose method.</summary>
        Public Overridable Sub Free()
            Dispose()
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not mbDisposed Then
                If mHandleStruct.Pointer <> IntPtr.Zero Then
                    IsochK_Free(mHandleStruct)
                    Call Debug.Print("{0} Dispose: Freed Handle:{1:X16}h Explicit:{2}", [GetType]().Name, mHandleStruct.Pointer.ToInt64(), disposing)
                Else
                    Call Debug.Print("{0} Dispose: [WARNING] Handle is null", [GetType]().Name)
                End If

                mHandleStruct = New KISOCH_HANDLE(IntPtr.Zero)
                mbDisposed = True
            End If
        End Sub

        ''' <Summary>Creates a new isochronous transfer handle for libusbK or WinUSB.</Summary>
        Protected Function Init(ByVal InterfaceHandle As KUSB_HANDLE, ByVal PipeId As Byte, ByVal MaxNumberOfPackets As UInteger, ByVal TransferBuffer As IntPtr, ByVal TransferBufferSize As UInteger) As Boolean
            Dim success = IsochK_Init(mHandleStruct, InterfaceHandle, PipeId, MaxNumberOfPackets, TransferBuffer, TransferBufferSize)
            If Not success Then Throw New Exception(String.Format("{0} failed initializing. ErrorCode={1:X8}h", [GetType]().Name, Marshal.GetLastWin32Error()))
            Call Debug.Print("{0} Init: handle 0x{1:X16}", [GetType]().Name, mHandleStruct.Pointer.ToInt64())
            Return True
        End Function

        ''' <Summary>Convenience function for setting the offsets and lengths of all ISO packets of an isochronous transfer handle.</Summary>
        Public Overridable Function SetPacketOffsets(ByVal PacketSize As UInteger) As Boolean
            Return IsochK_SetPacketOffsets(mHandleStruct, PacketSize)
        End Function

        ''' <Summary>Convenience function for setting all fields in an isochronous transfer packet.</Summary>
        Public Overridable Function SetPacket(ByVal PacketIndex As UInteger, ByVal Offset As UInteger, ByVal Length As UInteger, ByVal Status As UInteger) As Boolean
            Return IsochK_SetPacket(mHandleStruct, PacketIndex, Offset, Length, Status)
        End Function

        ''' <Summary>Convenience function for getting all fields in an isochronous transfer packet.</Summary>
        Public Overridable Function GetPacket(ByVal PacketIndex As UInteger, <Out> ByRef Offset As UInteger, <Out> ByRef Length As UInteger, <Out> ByRef Status As UInteger) As Boolean
            Return IsochK_GetPacket(mHandleStruct, PacketIndex, Offset, Length, Status)
        End Function

        ''' <Summary>Convenience function for enumerating ISO packets of an isochronous transfer context.</Summary>
        Public Overridable Function EnumPackets(ByVal pEnumPackets As KISOCH_ENUM_PACKETS_CB, ByVal StartPacketIndex As UInteger, ByVal UserState As IntPtr) As Boolean
            Return IsochK_EnumPackets(mHandleStruct, pEnumPackets, StartPacketIndex, UserState)
        End Function

        ''' <Summary>Helper function for isochronous packet/transfer calculations.</Summary>
        Public Shared Function CalcPacketInformation(ByVal IsHighSpeed As Boolean, ByRef PipeInformationEx As WINUSB_PIPE_INFORMATION_EX, <Out> ByRef PacketInformation As KISOCH_PACKET_INFORMATION) As Boolean
            Return IsochK_CalcPacketInformation(IsHighSpeed, PipeInformationEx, PacketInformation)
        End Function

        ''' <Summary>Gets the number of iso packets that will be used.</Summary>
        Public Overridable Function GetNumberOfPackets(<Out> ByRef NumberOfPackets As UInteger) As Boolean
            Return IsochK_GetNumberOfPackets(mHandleStruct, NumberOfPackets)
        End Function

        ''' <Summary>Sets the number of iso packets that will be used.</Summary>
        Public Overridable Function SetNumberOfPackets(ByVal NumberOfPackets As UInteger) As Boolean
            Return IsochK_SetNumberOfPackets(mHandleStruct, NumberOfPackets)
        End Function
    End Class
End Namespace