summaryrefslogtreecommitdiff
path: root/src/md/winmd/winmdimport.cpp
blob: fe80bf0b04e45bc9537414a69cbd0693d9d86e47 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

 

#include "stdafx.h"
#include "winmdinterfaces.h"
#include "metadataexports.h"
#include "inc/adapter.h"
#include "strsafe.h"

//========================================================================================
// This metadata importer is exposed publically via CoCreateInstance(CLSID_CorMetaDataDispenser...).
// when the target is a .winmd file. It applies a small number of on-the-fly
// conversions to make the .winmd file look like a regular .NET assembly.
//
// Despite what the MSDN docs say, this importer only supports the reader interfaces and
// a subset of them at that. Supporting the whole set would not be practical with an
// on-the-fly translation strategy.
//========================================================================================
class WinMDImport : public IMetaDataImport2
                         , IMetaDataAssemblyImport
                         , IMetaDataWinMDImport
                         , IMetaDataValidate
                         , IMDCommon
                         , IMetaModelCommon
                         , IWinMDImport
#ifdef FEATURE_METADATA_INTERNAL_APIS
                         , IGetIMDInternalImport
#endif //FEATURE_METADATA_INTERNAL_APIS 
{
  public:
    //=========================================================
    // Factory
    //=========================================================
    static HRESULT Create(IMDCommon *pRawMDCommon, WinMDImport **ppWinMDImport)
    {
        HRESULT hr;
        *ppWinMDImport = NULL;

        WinMDImport *pNewWinMDImport = new (nothrow) WinMDImport(pRawMDCommon);

        IfFailGo(pNewWinMDImport->m_pRawMDCommon->QueryInterface(IID_IMetaDataImport2, (void**)&pNewWinMDImport->m_pRawImport));
        IfFailGo(pNewWinMDImport->m_pRawImport->QueryInterface(IID_IMetaDataAssemblyImport, (void**)&pNewWinMDImport->m_pRawAssemblyImport));
        
        if (FAILED(pNewWinMDImport->m_pRawImport->QueryInterface(IID_IMetaDataValidate, (void**)&pNewWinMDImport->m_pRawValidate)))
        {
            pNewWinMDImport->m_pRawValidate = nullptr;
        }
  
        pNewWinMDImport->m_pRawMetaModelCommonRO = pRawMDCommon->GetMetaModelCommonRO();
  
        IfFailGo(WinMDAdapter::Create(pRawMDCommon, &pNewWinMDImport->m_pWinMDAdapter));

        (*ppWinMDImport = pNewWinMDImport)->AddRef();
        hr = S_OK;

      ErrExit:
        if (pNewWinMDImport)
            pNewWinMDImport->Release();
        return hr;
    }
    

  private:
    //=========================================================
    // Ctors, Dtors
    //=========================================================
    WinMDImport(IMDCommon * pRawMDCommon)
    {
        m_cRef = 1;
        m_pRawImport = NULL;
        m_pWinMDAdapter = NULL;
        m_pRawAssemblyImport = NULL;
        m_pFreeThreadedMarshaler = NULL;
        (m_pRawMDCommon = pRawMDCommon)->AddRef();
    }

    //---------------------------------------------------------
    ~WinMDImport()
    {
        if (m_pRawMDCommon)
            m_pRawMDCommon->Release();
        if (m_pRawImport)
            m_pRawImport->Release();
        if (m_pRawAssemblyImport)
            m_pRawAssemblyImport->Release();
        if (m_pRawValidate)
            m_pRawValidate->Release();
        if (m_pFreeThreadedMarshaler)
            m_pFreeThreadedMarshaler->Release();
        delete m_pWinMDAdapter;
    }

    //---------------------------------------------------------
    BOOL IsValidNonNilToken(mdToken token, DWORD tkKind)
    {
        DWORD tkKindActual = TypeFromToken(token);
        DWORD rid          = RidFromToken(token);

        if (tkKindActual != tkKind)
            return FALSE;

        if (rid == 0)
            return FALSE;

        ULONG ulRowCount = m_pRawMetaModelCommonRO->CommonGetRowCount(tkKind);
        if (tkKind == mdtAssemblyRef)
            return rid <= ulRowCount + m_pWinMDAdapter->GetExtraAssemblyRefCount();
        else
            return rid <= ulRowCount;
    }

  public:
    //=========================================================
    // IUnknown methods
    //=========================================================
    STDMETHODIMP QueryInterface(REFIID riid, void** ppUnk)
    {
        HRESULT hr;
        
        *ppUnk = 0;
        if (riid == IID_IUnknown || riid == IID_IWinMDImport)
            *ppUnk = (IWinMDImport*)this;
        else if (riid == IID_IMetaDataImport || riid == IID_IMetaDataImport2)
            *ppUnk = (IMetaDataImport*)this;
        else if (riid == IID_IMetaDataWinMDImport)
            *ppUnk = (IMetaDataWinMDImport*)this;
        else if (riid == IID_IMetaDataAssemblyImport)
            *ppUnk = (IMetaDataAssemblyImport*)this;
        else if (riid == IID_IMDCommon)
            *ppUnk = (IMDCommon*)this;
        else if (riid == IID_IMetaDataValidate)
        {
            if (m_pRawValidate == NULL)
            {
                return E_NOINTERFACE;
            }

            *ppUnk = (IMetaDataValidate*)this;
        }
#ifdef FEATURE_METADATA_INTERNAL_APIS
        else if (riid == IID_IGetIMDInternalImport)
            *ppUnk = (IGetIMDInternalImport*)this;
#endif // FEATURE_METADATA_INTERNAL_APIS
        else if (riid == IID_IMarshal)
        {
            if (m_pFreeThreadedMarshaler == NULL)
            {
                ReleaseHolder<IUnknown> pFreeThreadedMarshaler = NULL;
                IfFailRet(CoCreateFreeThreadedMarshaler((IUnknown *)(IMetaDataImport *)this, &pFreeThreadedMarshaler));
                if (InterlockedCompareExchangeT<IUnknown *>(&m_pFreeThreadedMarshaler, pFreeThreadedMarshaler, NULL) == NULL)
                {   // We won the initialization race
                    pFreeThreadedMarshaler.SuppressRelease();
                }
            }
            return m_pFreeThreadedMarshaler->QueryInterface(riid, ppUnk); 
        }
        else
        {
#ifndef DACCESS_COMPILE
#ifdef _DEBUG
            if (CLRConfig::GetConfigValue(CLRConfig::INTERNAL_MD_WinMD_AssertOnIllegalUsage))
            {
                if (riid == IID_IMetaDataTables)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataTables) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataTables2)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataTables2) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataInfo)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataInfo) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataEmit)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataEmit) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataEmit2)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataEmit2) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataAssemblyEmit)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataAssemblyEmit) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataValidate)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataValidate) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataFilter)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataFilter) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataHelper)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataHelper) returning E_NOINTERFACE");
                else if (riid == IID_IMDInternalEmit)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMDInternalEmit) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataEmitHelper)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataEmitHelper) returning E_NOINTERFACE");
                else if (riid == IID_IMetaDataCorProfileData)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMetaDataCorProfileData) returning E_NOINTERFACE");
                else if (riid == IID_IMDInternalMetadataReorderingOptions)
                    _ASSERTE(!"WinMDImport::QueryInterface(IID_IMDInternalMetadataReorderingOptions) returning E_NOINTERFACE");
                else
                    _ASSERTE(!"WinMDImport::QueryInterface() returning E_NOINTERFACE");
            }
#endif //_DEBUG
#endif
            return E_NOINTERFACE;
        }
        AddRef();
        return S_OK;
    }
    //---------------------------------------------------------
    STDMETHODIMP_(ULONG) AddRef(void)
    {
        return InterlockedIncrement(&m_cRef);
    }
    //---------------------------------------------------------
    STDMETHODIMP_(ULONG) Release(void)
    {
        ULONG   cRef = InterlockedDecrement(&m_cRef);
        if (!cRef)
            delete this;
        return cRef;
    }

  public:
    //=========================================================
    // IWinMDImport methods
    //=========================================================
    STDMETHODIMP IsScenarioWinMDExp(BOOL *pbResult)
    {
        if (pbResult == NULL)
            return E_POINTER;

        *pbResult = m_pWinMDAdapter->IsScenarioWinMDExp();
        return S_OK;
    }

    STDMETHODIMP IsRuntimeClassImplementation(mdTypeDef tkTypeDef, BOOL *pbResult)
    {
        if (pbResult == NULL)
            return E_POINTER;

        return m_pWinMDAdapter->IsRuntimeClassImplementation(tkTypeDef, pbResult);
    }

    //=========================================================
    // IMetaDataImport methods
    //=========================================================
    STDMETHODIMP_(void) CloseEnum(HCORENUM hEnum)
    {
        m_pRawImport->CloseEnum(hEnum);
    }

    STDMETHODIMP CountEnum(HCORENUM hEnum, ULONG *pulCount)      
    {
        _ASSERTE(pulCount != NULL); // Crash on NULL pulCount (just like real RegMeta)
        if (hEnum != NULL)
        {
            HENUMInternal *henuminternal = static_cast<HENUMInternal*>(hEnum);
            // Special case: We hijack MethodImpl enum's 
            if (henuminternal->m_tkKind == (TBL_MethodImpl << 24))
            {                                              
                *pulCount = henuminternal->m_ulCount / 2;  // MethodImpl enum's are weird - their entries are body/decl pairs so the internal count is twice the number the caller wants to see.
                return S_OK;
            }
        }

        return m_pRawImport->CountEnum(hEnum, pulCount);
    }

    STDMETHODIMP ResetEnum(HCORENUM hEnum, ULONG ulPos)      
    {
        return m_pRawImport->ResetEnum(hEnum, ulPos);
    }

    STDMETHODIMP EnumTypeDefs(HCORENUM *phEnum, mdTypeDef rTypeDefs[],
                            ULONG cMax, ULONG *pcTypeDefs)      
    {
        return m_pRawImport->EnumTypeDefs(phEnum, rTypeDefs, cMax, pcTypeDefs);
    }

    STDMETHODIMP EnumInterfaceImpls(HCORENUM *phEnum, mdTypeDef td,
                            mdInterfaceImpl rImpls[], ULONG cMax,
                            ULONG* pcImpls)      
    {
        return m_pRawImport->EnumInterfaceImpls(phEnum, td, rImpls, cMax, pcImpls);
    }

    STDMETHODIMP EnumTypeRefs(HCORENUM *phEnum, mdTypeRef rTypeRefs[],
                            ULONG cMax, ULONG* pcTypeRefs)      
    {
            // No trick needed: a previous call to EnumTypeRefs must have already taken care of the
            // extra type refs.
            return m_pRawImport->EnumTypeRefs(phEnum, rTypeRefs, cMax, pcTypeRefs);
    }

    STDMETHODIMP FindTypeDefByName(         // S_OK or error.
        LPCWSTR     wszTypeDef,             // [IN] Name of the Type.
        mdToken     tkEnclosingClass,       // [IN] TypeDef/TypeRef for Enclosing class.
        mdTypeDef   *ptd)                   // [OUT] Put the TypeDef token here.
    {
        if (wszTypeDef == NULL)          // E_INVALIDARG on NULL szTypeDef (just like real RegMeta)
            return E_INVALIDARG;
        _ASSERTE(ptd != NULL);          // AV on NULL ptd (just like real RegMeta)
        *ptd = mdTypeDefNil; 

        LPUTF8      szFullName;
        LPCUTF8     szNamespace;
        LPCUTF8     szName;
        // Convert the  name to UTF8.
        UTF8STR(wszTypeDef, szFullName);
        ns::SplitInline(szFullName, szNamespace, szName);

        return m_pWinMDAdapter->FindTypeDef(szNamespace, szName, tkEnclosingClass, ptd);
    }


    STDMETHODIMP GetScopeProps(               // S_OK or error.
      __out_ecount_part_opt(cchName, *pchName)
        LPWSTR      szName,                 // [OUT] Put the name here.
        ULONG       cchName,                // [IN] Size of name buffer in wide chars.
        ULONG       *pchName,               // [OUT] Put size of name (wide chars) here.
        GUID        *pmvid)                 // [OUT, OPTIONAL] Put MVID here.
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawImport->GetScopeProps(szName, cchName, pchName, pmvid);
    }


    STDMETHODIMP GetModuleFromScope(          // S_OK.
        mdModule    *pmd)                   // [OUT] Put mdModule token here.
    {
        return m_pRawImport->GetModuleFromScope(pmd);
    }


    STDMETHODIMP GetTypeDefProps(             // S_OK or error.
        mdTypeDef   td,                     // [IN] TypeDef token for inquiry.
      __out_ecount_part_opt(cchTypeDef, *pchTypeDef)
        LPWSTR      szTypeDef,              // [OUT] Put name here.
        ULONG       cchTypeDef,             // [IN] size of name buffer in wide chars.
        ULONG       *pchTypeDef,            // [OUT] put size of name (wide chars) here.
        DWORD       *pdwTypeDefFlags,       // [OUT] Put flags here.
        mdToken     *ptkExtends)            // [OUT] Put base class TypeDef/TypeRef here.
    {
        HRESULT hr;
        LPCSTR szNamespace;
        LPCSTR szName;
        if (!IsValidNonNilToken(td, mdtTypeDef))
        {
            // Idiosyncractic edge cases - delegate straight through to inherit the correct idiosyncractic edge results 
            return m_pRawImport->GetTypeDefProps(td, szTypeDef, cchTypeDef, pchTypeDef, pdwTypeDefFlags, ptkExtends);
        }
        IfFailRet(m_pWinMDAdapter->GetTypeDefProps(td, &szNamespace, &szName, pdwTypeDefFlags, ptkExtends));
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return DeliverUtf8NamespaceAndName(szNamespace, szName, szTypeDef, cchTypeDef, pchTypeDef);
    }


    STDMETHODIMP GetInterfaceImplProps(       // S_OK or error.
        mdInterfaceImpl iiImpl,             // [IN] InterfaceImpl token.
        mdTypeDef   *pClass,                // [OUT] Put implementing class token here.
        mdToken     *ptkIface)              // [OUT] Put implemented interface token here.
    {
        return m_pRawImport->GetInterfaceImplProps(iiImpl, pClass, ptkIface);
    }

            
    STDMETHODIMP GetTypeRefProps(             // S_OK or error.
        mdTypeRef   tr,                     // [IN] TypeRef token.
        mdToken     *ptkResolutionScope,    // [OUT] Resolution scope, ModuleRef or AssemblyRef.
      __out_ecount_part_opt(cchName, *pchName)
        LPWSTR      szName,                 // [OUT] Name of the TypeRef.
        ULONG       cchName,                // [IN] Size of buffer.
        ULONG       *pchName)               // [OUT] Size of Name.
    {
        HRESULT hr;
        if (!IsValidNonNilToken(tr, mdtTypeRef))
        {
            // Idiosyncratic edge cases - delegate straight through to inherit the correct idiosyncratic edge result
            return m_pRawImport->GetTypeRefProps(tr, ptkResolutionScope, szName, cchName, pchName);
        }

        LPCUTF8 szUtf8Namespace;
        LPCUTF8 szUtf8Name;
        mdToken tkResolutionScope;
        IfFailRet(CommonGetTypeRefProps(tr, &szUtf8Namespace, &szUtf8Name, &tkResolutionScope));
        if (ptkResolutionScope != NULL)
        {
            *ptkResolutionScope = tkResolutionScope;
        }
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return DeliverUtf8NamespaceAndName(szUtf8Namespace, szUtf8Name, szName, cchName, pchName);
    }


    STDMETHODIMP ResolveTypeRef(mdTypeRef tr, REFIID riid, IUnknown **ppIScope, mdTypeDef *ptd)      
    {
        WINMD_COMPAT_ASSERT("IMetaDataImport::ResolveTypeRef() not supported on .winmd files.");
        return E_NOTIMPL;
    }


    STDMETHODIMP EnumMembers(                 // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   cl,                     // [IN] TypeDef to scope the enumeration.
        mdToken     rMembers[],             // [OUT] Put MemberDefs here.
        ULONG       cMax,                   // [IN] Max MemberDefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumMembers(phEnum, cl, rMembers, cMax, pcTokens);
    }


    STDMETHODIMP EnumMembersWithName(         // S_OK, S_FALSE, or error.
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   cl,                     // [IN] TypeDef to scope the enumeration.
        LPCWSTR     szName,                 // [IN] Limit results to those with this name.
        mdToken     rMembers[],             // [OUT] Put MemberDefs here.
        ULONG       cMax,                   // [IN] Max MemberDefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumMembersWithName(phEnum, cl, szName, rMembers, cMax, pcTokens);
    }


    STDMETHODIMP EnumMethods(                 // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   cl,                     // [IN] TypeDef to scope the enumeration.
        mdMethodDef rMethods[],             // [OUT] Put MethodDefs here.
        ULONG       cMax,                   // [IN] Max MethodDefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumMethods(phEnum, cl, rMethods, cMax, pcTokens);
    }


    STDMETHODIMP EnumMethodsWithName(         // S_OK, S_FALSE, or error.
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   cl,                     // [IN] TypeDef to scope the enumeration.
        LPCWSTR     szName,                 // [IN] Limit results to those with this name.
        mdMethodDef rMethods[],             // [OU] Put MethodDefs here.
        ULONG       cMax,                   // [IN] Max MethodDefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumMethodsWithName(phEnum, cl, szName, rMethods, cMax, pcTokens);
    }


    STDMETHODIMP EnumFields(                  // S_OK, S_FALSE, or error.
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   cl,                     // [IN] TypeDef to scope the enumeration.
        mdFieldDef  rFields[],              // [OUT] Put FieldDefs here.
        ULONG       cMax,                   // [IN] Max FieldDefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumFields(phEnum, cl, rFields, cMax, pcTokens);
    }


    STDMETHODIMP EnumFieldsWithName(          // S_OK, S_FALSE, or error.
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   cl,                     // [IN] TypeDef to scope the enumeration.
        LPCWSTR     szName,                 // [IN] Limit results to those with this name.
        mdFieldDef  rFields[],              // [OUT] Put MemberDefs here.
        ULONG       cMax,                   // [IN] Max MemberDefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumFieldsWithName(phEnum, cl, szName, rFields, cMax, pcTokens);
    }



    STDMETHODIMP EnumParams(                  // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdMethodDef mb,                     // [IN] MethodDef to scope the enumeration. 
        mdParamDef  rParams[],              // [OUT] Put ParamDefs here.
        ULONG       cMax,                   // [IN] Max ParamDefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumParams(phEnum, mb, rParams, cMax, pcTokens);
    }


    STDMETHODIMP EnumMemberRefs(              // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdToken     tkParent,               // [IN] Parent token to scope the enumeration.
        mdMemberRef rMemberRefs[],          // [OUT] Put MemberRefs here.
        ULONG       cMax,                   // [IN] Max MemberRefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumMemberRefs(phEnum, tkParent, rMemberRefs, cMax, pcTokens);
    }


    STDMETHODIMP EnumMethodImpls(             // S_OK, S_FALSE, or error
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   td,                     // [IN] TypeDef to scope the enumeration.
        mdToken     rMethodBody[],          // [OUT] Put Method Body tokens here.
        mdToken     rMethodDecl[],          // [OUT] Put Method Declaration tokens here.
        ULONG       cMax,                   // [IN] Max tokens to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        // Note: This wrapper emulates the underlying RegMeta's parameter validation semantics:
        //   Pass a bad phEnum: Instant AV
        //   Pass out of range typedef: returns S_FALSE (i.e. generates empty list)
        //   Pass non-typedef: returns S_FALSE with assert
        //   Pass bad output ptr for bodies or decls, AV 
        //   Pass bad output ptr for pcTokens, AV (but NULL is allowed.)

        HRESULT         hr;
        HENUMInternal **ppmdEnum = reinterpret_cast<HENUMInternal **> (phEnum);
        HENUMInternal  *pEnum = *ppmdEnum;
        
        if ( pEnum == 0 )
        {
            // Create the enumerator, DynamicArrayEnum does not use the token type.
            IfFailGo( HENUMInternal::CreateDynamicArrayEnum( (TBL_MethodImpl << 24), &pEnum) );
            IfFailGo( m_pWinMDAdapter->AddMethodImplsToEnum(td, pEnum));
            // set the output parameter
            *ppmdEnum = pEnum;
        }
    
        // fill the output token buffer
        hr = HENUMInternal::EnumWithCount(pEnum, cMax, rMethodBody, rMethodDecl, pcTokens);
    
    ErrExit:
        HENUMInternal::DestroyEnumIfEmpty(ppmdEnum);
        return hr;
    }


    STDMETHODIMP EnumPermissionSets(          // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdToken     tk,                     // [IN] if !NIL, token to scope the enumeration.
        DWORD       dwActions,              // [IN] if !0, return only these actions.
        mdPermission rPermission[],         // [OUT] Put Permissions here.
        ULONG       cMax,                   // [IN] Max Permissions to put. 
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumPermissionSets(phEnum, tk, dwActions, rPermission, cMax, pcTokens);
    }


    STDMETHODIMP FindMember(
        mdTypeDef   td,                     // [IN] given typedef
        LPCWSTR     szName,                 // [IN] member name 
        PCCOR_SIGNATURE pvSigBlob,          // [IN] point to a blob value of CLR signature 
        ULONG       cbSigBlob,              // [IN] count of bytes in the signature blob
        mdToken     *pmb)                   // [OUT] matching memberdef 
    {
        HRESULT hr = FindMethod(
            td,
            szName,
            pvSigBlob,
            cbSigBlob,
            pmb);

        if (hr == CLDB_E_RECORD_NOTFOUND)
        {
            // now try field table
            hr = FindField(
                td,
                szName,
                pvSigBlob,
                cbSigBlob,
                pmb);
        }

        return hr;
    }


    STDMETHODIMP FindMethod(
        mdTypeDef   td,                     // [IN] given typedef
        LPCWSTR     szName,                 // [IN] member name 
        PCCOR_SIGNATURE pvSigBlob,          // [IN] point to a blob value of CLR signature 
        ULONG       cbSigBlob,              // [IN] count of bytes in the signature blob
        mdMethodDef *pmb)                   // [OUT] matching memberdef 
    {
        if (pvSigBlob == NULL || cbSigBlob == 0)
        {
            // if signature matching is not needed, we can delegate to the underlying implementation
            return m_pRawImport->FindMethod(td, szName, pvSigBlob, cbSigBlob, pmb);
        }

        // The following code emulates RegMeta::FindMethod. We cannot call the underlying
        // implementation because we need to compare pvSigBlob to reinterpreted signatures.
        HRESULT hr = S_OK;
        HCORENUM hEnum = NULL;

        CQuickBytes qbSig; // holds non-varargs signature
        CQuickArray<WCHAR> rName;

        if (szName == NULL || pmb == NULL)
            IfFailGo(E_INVALIDARG);

        *pmb = mdMethodDefNil;

        // check to see if this is a vararg signature
        PCCOR_SIGNATURE pvSigTemp = pvSigBlob;
        if (isCallConv(CorSigUncompressCallingConv(pvSigTemp), IMAGE_CEE_CS_CALLCONV_VARARG))
        {
            // Get the fixed part of VARARG signature
            IfFailGo(_GetFixedSigOfVarArg(pvSigBlob, cbSigBlob, &qbSig, &cbSigBlob));
            pvSigBlob = (PCCOR_SIGNATURE) qbSig.Ptr();
        }

        // now iterate all methods in td and compare name and signature
        IfNullGo(rName.AllocNoThrow(wcslen(szName) + 1));

        mdMethodDef md;
        ULONG count;
        while ((hr = EnumMethods(&hEnum, td, &md, 1, &count)) == S_OK)
        {
            PCCOR_SIGNATURE pvMethodSigBlob;
            ULONG cbMethodSigBlob;
            ULONG chMethodName;
            DWORD dwMethodAttr;

            IfFailGo(GetMethodProps(md, NULL, rName.Ptr(), (ULONG)rName.Size(), &chMethodName, &dwMethodAttr, &pvMethodSigBlob, &cbMethodSigBlob, NULL, NULL));

            if (chMethodName == rName.Size() && wcscmp(szName, rName.Ptr()) == 0)
            {
                // we have a name match, check signature
                if (cbSigBlob != cbMethodSigBlob || memcmp(pvSigBlob, pvMethodSigBlob, cbSigBlob) != 0)
                    continue;

                // ignore PrivateScope methods
                if (IsMdPrivateScope(dwMethodAttr))
                    continue;

                // we found the method
                *pmb = md;
                goto ErrExit;
            }
        }
        IfFailGo(hr);
        hr = CLDB_E_RECORD_NOTFOUND;

    ErrExit:
        if (hEnum != NULL)
            CloseEnum(hEnum);

        return hr;
    }


    STDMETHODIMP FindField(
        mdTypeDef   td,                     // [IN] given typedef
        LPCWSTR     szName,                 // [IN] member name 
        PCCOR_SIGNATURE pvSigBlob,          // [IN] point to a blob value of CLR signature 
        ULONG       cbSigBlob,              // [IN] count of bytes in the signature blob
        mdFieldDef  *pmb)                   // [OUT] matching memberdef 
    {
        if (pvSigBlob == NULL || cbSigBlob == 0)
        {
            // if signature matching is not needed, we can delegate to the underlying implementation
            return m_pRawImport->FindField(td, szName, pvSigBlob, cbSigBlob, pmb);
        }

        // The following code emulates RegMeta::FindField. We cannot call the underlying
        // implementation because we need to compare pvSigBlob to reinterpreted signatures.
        HRESULT hr = S_OK;
        HCORENUM hEnum = NULL;

        CQuickArray<WCHAR> rName;

        if (szName == NULL || pmb == NULL)
            IfFailGo(E_INVALIDARG);

        *pmb = mdFieldDefNil;

        // now iterate all fields in td and compare name and signature
        IfNullGo(rName.AllocNoThrow(wcslen(szName) + 1));

        mdFieldDef fd;
        ULONG count;
        while ((hr = EnumFields(&hEnum, td, &fd, 1, &count)) == S_OK)
        {
            PCCOR_SIGNATURE pvFieldSigBlob;
            ULONG cbFieldSigBlob;
            ULONG chFieldName;
            DWORD dwFieldAttr;

            IfFailGo(GetFieldProps(fd, NULL, rName.Ptr(), (ULONG)rName.Size(), &chFieldName, &dwFieldAttr, &pvFieldSigBlob, &cbFieldSigBlob, NULL, NULL, NULL));

            if (chFieldName == rName.Size() && wcscmp(szName, rName.Ptr()) == 0)
            {
                // we have a name match, check signature
                if (cbSigBlob != cbFieldSigBlob || memcmp(pvSigBlob, pvFieldSigBlob, cbSigBlob) != 0)
                    continue;

                // ignore PrivateScope fields
                if (IsFdPrivateScope(dwFieldAttr))
                    continue;

                // we found the field
                *pmb = fd;
                goto ErrExit;
            }
        }
        IfFailGo(hr);
        hr = CLDB_E_RECORD_NOTFOUND;

    ErrExit:
        if (hEnum != NULL)
            CloseEnum(hEnum);

        return hr;
    }

   
   STDMETHODIMP FindMemberRef(
		   mdTypeRef   td,					   // [IN] given typeRef
		   LPCWSTR	   szName,				   // [IN] member name 
		   PCCOR_SIGNATURE pvSigBlob,		   // [IN] point to a blob value of CLR signature 
		   ULONG	   cbSigBlob,			   // [IN] count of bytes in the signature blob
		   mdMemberRef *pmr)				   // [OUT] matching memberref 
	   {
		   if (pvSigBlob == NULL || cbSigBlob == 0)
		   {
			   // if signature matching is not needed, we can delegate to the underlying implementation
			   return m_pRawImport->FindMemberRef(td, szName, pvSigBlob, cbSigBlob, pmr);
		   }
   
		   // The following code emulates RegMeta::FindMemberRef. We cannot call the underlying
		   // implementation because we need to compare pvSigBlob to reinterpreted signatures.
		   HRESULT hr = S_OK;
		   HCORENUM hEnum = NULL;
   
		   CQuickArray<WCHAR> rName;
   
		   if (szName == NULL || pmr == NULL)
			   IfFailGo(CLDB_E_RECORD_NOTFOUND);

		   *pmr = mdMemberRefNil;
   
		   // now iterate all members in td and compare name and signature
           IfNullGo(rName.AllocNoThrow(wcslen(szName) + 1));
   
		   mdMemberRef rd;
		   ULONG count;
		   while ((hr = EnumMemberRefs(&hEnum, td, &rd, 1, &count)) == S_OK)
		   {
			   PCCOR_SIGNATURE pvMemberSigBlob;
			   ULONG cbMemberSigBlob;
			   ULONG chMemberName;
   
			   IfFailGo(GetMemberRefProps(rd, NULL, rName.Ptr(), (ULONG)rName.Size(), &chMemberName, &pvMemberSigBlob, &cbMemberSigBlob));
   
			   if (chMemberName == rName.Size() && wcscmp(szName, rName.Ptr()) == 0)
			   {
				   // we have a name match, check signature
				   if (cbSigBlob != cbMemberSigBlob || memcmp(pvSigBlob, pvMemberSigBlob, cbSigBlob) != 0)
					   continue;
   
				   // we found the member
				   *pmr = rd;
				   goto ErrExit;
			   }
		   }
		   IfFailGo(hr);
		   hr = CLDB_E_RECORD_NOTFOUND;
   
	   ErrExit:
		   if (hEnum != NULL)
			   CloseEnum(hEnum);
   
		   return hr;
	   }   


    STDMETHODIMP GetMethodProps( 
        mdMethodDef mb,                     // The method for which to get props.
        mdTypeDef   *pClass,                // Put method's class here. 
      __out_ecount_part_opt(cchMethod, *pchMethod)
        LPWSTR      szMethod,               // Put method's name here.
        ULONG       cchMethod,              // Size of szMethod buffer in wide chars.
        ULONG       *pchMethod,             // Put actual size here 
        DWORD       *pdwAttr,               // Put flags here.
        PCCOR_SIGNATURE *ppvSigBlob,        // [OUT] point to the blob value of meta data
        ULONG       *pcbSigBlob,            // [OUT] actual size of signature blob
        ULONG       *pulCodeRVA,            // [OUT] codeRVA
        DWORD       *pdwImplFlags)          // [OUT] Impl. Flags
    {
        HRESULT hr;
        HRESULT hrNameTruncation;
        if (!IsValidNonNilToken(mb, mdtMethodDef))
        {
            // Idiosyncratic edge cases - delegate straight through to inherit the correct idiosyncratic edge result
            return m_pRawImport->GetMethodProps(mb, pClass, szMethod, cchMethod, pchMethod, pdwAttr, ppvSigBlob, pcbSigBlob, pulCodeRVA, pdwImplFlags);
        }

        ULONG cbOrigSigBlob = (ULONG)(-1);
        PCCOR_SIGNATURE pOrigSig = NULL;
        IfFailRet(hrNameTruncation = m_pRawImport->GetMethodProps(mb, pClass, szMethod, cchMethod, pchMethod, pdwAttr, &pOrigSig, &cbOrigSigBlob, pulCodeRVA, pdwImplFlags));

        IfFailRet((m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtMethodDef>(
            mb, 
            &pOrigSig,          // ppOrigSig
            &cbOrigSigBlob,     // pcbOrigSig
            ppvSigBlob, 
            pcbSigBlob, 
            m_pRawImport)));

        LPCSTR szNewName = NULL;
        IfFailRet(m_pWinMDAdapter->ModifyMethodProps(mb, pdwAttr, pdwImplFlags, pulCodeRVA, &szNewName));
        if (szNewName != NULL)
        {
            // We want to return name truncation status from the method that really fills the output buffer, rewrite the previous value
            IfFailRet(hrNameTruncation = DeliverUtf8String(szNewName, szMethod, cchMethod, pchMethod));
        }
        
        // Return the success code from name filling (S_OK or CLDB_S_TRUNCATION)
        return hrNameTruncation;
    }


    STDMETHODIMP GetMemberRefProps(           // S_OK or error.
        mdMemberRef mr,                     // [IN] given memberref 
        mdToken     *ptk,                   // [OUT] Put classref or classdef here. 
      __out_ecount_part_opt(cchMember, *pchMember)
        LPWSTR      szMember,               // [OUT] buffer to fill for member's name
        ULONG       cchMember,              // [IN] the count of char of szMember
        ULONG       *pchMember,             // [OUT] actual count of char in member name
        PCCOR_SIGNATURE *ppvSigBlob,        // [OUT] point to meta data blob value
        ULONG       *pbSig)                 // [OUT] actual size of signature blob
    {
        HRESULT hr = S_OK;
        HRESULT hrNameTruncation;

        ULONG cbOrigSigBlob = (ULONG)(-1);
        PCCOR_SIGNATURE pOrigSig = NULL;
        IfFailRet(hrNameTruncation = m_pRawImport->GetMemberRefProps(mr, ptk, szMember, cchMember, pchMember, &pOrigSig, &cbOrigSigBlob));
        LPCSTR szNewName = NULL;
        IfFailRet(m_pWinMDAdapter->ModifyMemberProps(mr, NULL, NULL, NULL, &szNewName));
        IfFailRet((m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtMemberRef>(
            mr, 
            &pOrigSig,          // ppOrigSig
            &cbOrigSigBlob,     // pcbOrigSig
            ppvSigBlob, 
            pbSig, 
            m_pRawImport)));
        if (szNewName != NULL)
        {
            // We want to return name truncation status from the method that really fills the output buffer, rewrite the previous value
            IfFailRet(hrNameTruncation = DeliverUtf8String(szNewName, szMember, cchMember, pchMember));
        }
        // Return the success code from name filling (S_OK or CLDB_S_TRUNCATION)
        return hrNameTruncation;
    }


    STDMETHODIMP EnumProperties(              // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   td,                     // [IN] TypeDef to scope the enumeration.
        mdProperty  rProperties[],          // [OUT] Put Properties here.
        ULONG       cMax,                   // [IN] Max properties to put.
        ULONG       *pcProperties)          // [OUT] Put # put here.
    {
        return m_pRawImport->EnumProperties(phEnum, td, rProperties, cMax, pcProperties);
    }


    STDMETHODIMP EnumEvents(                  // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdTypeDef   td,                     // [IN] TypeDef to scope the enumeration.
        mdEvent     rEvents[],              // [OUT] Put events here.
        ULONG       cMax,                   // [IN] Max events to put.
        ULONG       *pcEvents)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumEvents(phEnum, td, rEvents, cMax, pcEvents);
    }


    STDMETHODIMP GetEventProps(               // S_OK, S_FALSE, or error. 
        mdEvent     ev,                     // [IN] event token 
        mdTypeDef   *pClass,                // [OUT] typedef containing the event declarion.
        LPCWSTR     szEvent,                // [OUT] Event name 
        ULONG       cchEvent,               // [IN] the count of wchar of szEvent
        ULONG       *pchEvent,              // [OUT] actual count of wchar for event's name 
        DWORD       *pdwEventFlags,         // [OUT] Event flags.
        mdToken     *ptkEventType,          // [OUT] EventType class
        mdMethodDef *pmdAddOn,              // [OUT] AddOn method of the event
        mdMethodDef *pmdRemoveOn,           // [OUT] RemoveOn method of the event
        mdMethodDef *pmdFire,               // [OUT] Fire method of the event
        mdMethodDef rmdOtherMethod[],       // [OUT] other method of the event
        ULONG       cMax,                   // [IN] size of rmdOtherMethod
        ULONG       *pcOtherMethod)         // [OUT] total number of other method of this event 
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawImport->GetEventProps(ev, pClass, szEvent, cchEvent, pchEvent, pdwEventFlags, ptkEventType, pmdAddOn, pmdRemoveOn, pmdFire, rmdOtherMethod, cMax, pcOtherMethod);
    }


    STDMETHODIMP EnumMethodSemantics(         // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdMethodDef mb,                     // [IN] MethodDef to scope the enumeration. 
        mdToken     rEventProp[],           // [OUT] Put Event/Property here.
        ULONG       cMax,                   // [IN] Max properties to put.
        ULONG       *pcEventProp)           // [OUT] Put # put here.
    {
        return m_pRawImport->EnumMethodSemantics(phEnum, mb, rEventProp, cMax, pcEventProp);
    }

    STDMETHODIMP GetMethodSemantics(          // S_OK, S_FALSE, or error. 
        mdMethodDef mb,                     // [IN] method token
        mdToken     tkEventProp,            // [IN] event/property token.
        DWORD       *pdwSemanticsFlags)       // [OUT] the role flags for the method/propevent pair 
    {
        return m_pRawImport->GetMethodSemantics(mb, tkEventProp, pdwSemanticsFlags);
    }


    STDMETHODIMP GetClassLayout( 
        mdTypeDef   td,                     // [IN] give typedef
        DWORD       *pdwPackSize,           // [OUT] 1, 2, 4, 8, or 16
        COR_FIELD_OFFSET rFieldOffset[],    // [OUT] field offset array 
        ULONG       cMax,                   // [IN] size of the array
        ULONG       *pcFieldOffset,         // [OUT] needed array size
        ULONG       *pulClassSize)              // [OUT] the size of the class
    {
        return m_pRawImport->GetClassLayout(td, pdwPackSize, rFieldOffset, cMax, pcFieldOffset, pulClassSize);
    }


    STDMETHODIMP GetFieldMarshal(
        mdToken     tk,                     // [IN] given a field's memberdef
        PCCOR_SIGNATURE *ppvNativeType,     // [OUT] native type of this field
        ULONG       *pcbNativeType)         // [OUT] the count of bytes of *ppvNativeType
    {
        return m_pRawImport->GetFieldMarshal(tk, ppvNativeType, pcbNativeType);
    }


    STDMETHODIMP GetRVA(                      // S_OK or error.
        mdToken     tk,                     // Member for which to set offset
        ULONG       *pulCodeRVA,            // The offset
        DWORD       *pdwImplFlags)          // the implementation flags 
    {
        HRESULT hr;
        if (!IsValidNonNilToken(tk, mdtMethodDef))
        {
            // Idiosyncratic edge cases - delegate straight through to inherit the correct idiosyncratic edge result
            return m_pRawImport->GetRVA(tk, pulCodeRVA, pdwImplFlags);
        }

        IfFailRet(m_pRawImport->GetRVA(tk, pulCodeRVA, pdwImplFlags));
        IfFailRet(m_pWinMDAdapter->ModifyMethodProps(tk, NULL, pdwImplFlags, pulCodeRVA, NULL));
        return hr;
    }


    STDMETHODIMP GetPermissionSetProps(
        mdPermission pm,                    // [IN] the permission token.
        DWORD       *pdwAction,             // [OUT] CorDeclSecurity.
        void const  **ppvPermission,        // [OUT] permission blob.
        ULONG       *pcbPermission)         // [OUT] count of bytes of pvPermission.
    {
        return m_pRawImport->GetPermissionSetProps(pm, pdwAction, ppvPermission, pcbPermission);
    }


    STDMETHODIMP GetSigFromToken(           // S_OK or error.
        mdSignature mdSig,                  // [IN] Signature token.
        PCCOR_SIGNATURE *ppvSig,            // [OUT] return pointer to token.
        ULONG       *pcbSig)                // [OUT] return size of signature.
    {
        // mdSignature is not part of public WinMD surface, so it does not need signature rewriting
        return m_pRawImport->GetSigFromToken(mdSig, ppvSig, pcbSig);
    }

    STDMETHODIMP GetModuleRefProps(           // S_OK or error.
        mdModuleRef mur,                    // [IN] moduleref token.
      __out_ecount_part_opt(cchName, *pchName)
        LPWSTR      szName,                 // [OUT] buffer to fill with the moduleref name.
        ULONG       cchName,                // [IN] size of szName in wide characters.
        ULONG       *pchName)               // [OUT] actual count of characters in the name.
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawImport->GetModuleRefProps(mur, szName, cchName, pchName);
    }


    STDMETHODIMP EnumModuleRefs(              // S_OK or error.
        HCORENUM    *phEnum,                // [IN|OUT] pointer to the enum.
        mdModuleRef rModuleRefs[],          // [OUT] put modulerefs here.
        ULONG       cmax,                   // [IN] max memberrefs to put.
        ULONG       *pcModuleRefs)          // [OUT] put # put here.
    {
        return m_pRawImport->EnumModuleRefs(phEnum, rModuleRefs, cmax, pcModuleRefs);
    }


    STDMETHODIMP GetTypeSpecFromToken(        // S_OK or error.
        mdTypeSpec typespec,                // [IN] TypeSpec token.
        PCCOR_SIGNATURE *ppvSig,            // [OUT] return pointer to TypeSpec signature
        ULONG       *pcbSig)                // [OUT] return size of signature.
    {
        return m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtTypeSpec>(
            typespec, 
            NULL,     // ppOrigSig
            NULL,     // pcbOrigSig
            ppvSig, 
            pcbSig, 
            m_pRawImport);
    }


    STDMETHODIMP GetNameFromToken(            // Not Recommended! May be removed!
        mdToken     tk,                     // [IN] Token to get name from.  Must have a name.
        MDUTF8CSTR  *pszUtf8NamePtr)        // [OUT] Return pointer to UTF8 name in heap.
    {
        HRESULT hr;
        
        if (!IsValidNonNilToken(tk, mdtTypeRef))
        {
            // Handle corner error case
            IfFailGo(m_pRawImport->GetNameFromToken(tk, pszUtf8NamePtr));        
        }
        else
        {
            IfFailGo(m_pWinMDAdapter->GetTypeRefProps(tk, NULL, pszUtf8NamePtr, NULL));
        }
        
      ErrExit:
        return hr;
    }


    STDMETHODIMP EnumUnresolvedMethods(       // S_OK, S_FALSE, or error. 
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdToken     rMethods[],             // [OUT] Put MemberDefs here.
        ULONG       cMax,                   // [IN] Max MemberDefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawImport->EnumUnresolvedMethods(phEnum, rMethods, cMax, pcTokens);
    }


    STDMETHODIMP GetUserString(               // S_OK or error.
        mdString    stk,                    // [IN] String token.
      __out_ecount_part_opt(cchString, *pchString)
        LPWSTR      szString,               // [OUT] Copy of string.
        ULONG       cchString,              // [IN] Max chars of room in szString.
        ULONG       *pchString)             // [OUT] How many chars in actual string.
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawImport->GetUserString(stk, szString, cchString, pchString);
    }


    STDMETHODIMP GetPinvokeMap(               // S_OK or error.
        mdToken     tk,                     // [IN] FieldDef or MethodDef.
        DWORD       *pdwMappingFlags,       // [OUT] Flags used for mapping.
      __out_ecount_part_opt(cchImportName, *pchImportName)
        LPWSTR      szImportName,           // [OUT] Import name.
        ULONG       cchImportName,          // [IN] Size of the name buffer.
        ULONG       *pchImportName,         // [OUT] Actual number of characters stored.
        mdModuleRef *pmrImportDLL)          // [OUT] ModuleRef token for the target DLL.
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawImport->GetPinvokeMap(tk, pdwMappingFlags, szImportName, cchImportName, pchImportName, pmrImportDLL);
    }


    STDMETHODIMP EnumSignatures(              // S_OK or error.
        HCORENUM    *phEnum,                // [IN|OUT] pointer to the enum.
        mdSignature rSignatures[],          // [OUT] put signatures here.
        ULONG       cmax,                   // [IN] max signatures to put.
        ULONG       *pcSignatures)          // [OUT] put # put here.
    {
        return m_pRawImport->EnumSignatures(phEnum, rSignatures, cmax, pcSignatures);
    }


    STDMETHODIMP EnumTypeSpecs(               // S_OK or error.
        HCORENUM    *phEnum,                // [IN|OUT] pointer to the enum.
        mdTypeSpec  rTypeSpecs[],           // [OUT] put TypeSpecs here.
        ULONG       cmax,                   // [IN] max TypeSpecs to put.
        ULONG       *pcTypeSpecs)           // [OUT] put # put here.
    {
        return m_pRawImport->EnumTypeSpecs(phEnum, rTypeSpecs, cmax, pcTypeSpecs);
    }


    STDMETHODIMP EnumUserStrings(             // S_OK or error.
        HCORENUM    *phEnum,                // [IN/OUT] pointer to the enum.
        mdString    rStrings[],             // [OUT] put Strings here.
        ULONG       cmax,                   // [IN] max Strings to put.
        ULONG       *pcStrings)             // [OUT] put # put here.
    {
        return m_pRawImport->EnumUserStrings(phEnum, rStrings, cmax, pcStrings);
    }


    STDMETHODIMP GetParamForMethodIndex(      // S_OK or error.
        mdMethodDef md,                     // [IN] Method token.
        ULONG       ulParamSeq,             // [IN] Parameter sequence.
        mdParamDef  *ppd)                   // [IN] Put Param token here.
    {
        return m_pRawImport->GetParamForMethodIndex(md, ulParamSeq, ppd);
    }


    STDMETHODIMP EnumCustomAttributes(        // S_OK or error.
        HCORENUM    *phEnum,                // [IN, OUT] COR enumerator.
        mdToken     tk,                     // [IN] Token to scope the enumeration, 0 for all.
        mdToken     tkType,                 // [IN] Type of interest, 0 for all.
        mdCustomAttribute rCustomAttributes[], // [OUT] Put custom attribute tokens here.
        ULONG       cMax,                   // [IN] Size of rCustomAttributes.
        ULONG       *pcCustomAttributes)        // [OUT, OPTIONAL] Put count of token values here.
    {
        return m_pRawImport->EnumCustomAttributes(phEnum, tk, tkType, rCustomAttributes, cMax, pcCustomAttributes);
    }


    STDMETHODIMP GetCustomAttributeProps(     // S_OK or error.
        mdCustomAttribute cv,               // [IN] CustomAttribute token.
        mdToken     *ptkObj,                // [OUT, OPTIONAL] Put object token here.
        mdToken     *ptkType,               // [OUT, OPTIONAL] Put AttrType token here.
        void const  **ppBlob,               // [OUT, OPTIONAL] Put pointer to data here.
        ULONG       *pcbSize)               // [OUT, OPTIONAL] Put size of date here.
    {
        HRESULT hr;
        if (!IsValidNonNilToken(cv, mdtCustomAttribute))
        {
            // Idiosyncratic edge cases - delegate straight through to inherit the correct idiosyncratic edge result
            return m_pRawImport->GetCustomAttributeProps(cv, ptkObj, ptkType, ppBlob, pcbSize);
        }
        IfFailRet(m_pRawImport->GetCustomAttributeProps(cv, ptkObj, ptkType, NULL, NULL));
        IfFailRet(m_pWinMDAdapter->GetCustomAttributeBlob(cv, ppBlob, pcbSize));
        return hr;
    }


    STDMETHODIMP FindTypeRef(
        mdToken     tkResolutionScope,      // [IN] ModuleRef, AssemblyRef or TypeRef.
        LPCWSTR     wzTypeName,             // [IN] TypeRef Name.
        mdTypeRef   *ptr)                   // [OUT] matching TypeRef.
    {

        HRESULT hr;
        LPUTF8      szFullName;
        LPCUTF8     szNamespace;
        LPCUTF8     szName;

        _ASSERTE(wzTypeName && ptr);
        *ptr = mdTypeRefNil;   // AV if caller passes NULL: just like the real RegMeta.

        // Convert the  name to UTF8.
        PREFIX_ASSUME(wzTypeName != NULL); // caller might pass NULL, but they'll AV (just like the real RegMeta)
        UTF8STR(wzTypeName, szFullName);
        ns::SplitInline(szFullName, szNamespace, szName);
        hr = m_pWinMDAdapter->FindTypeRef(szNamespace, szName, tkResolutionScope, ptr);

        return hr;
    }


    STDMETHODIMP GetMemberProps(
        mdToken     mb,                     // The member for which to get props.
        mdTypeDef   *pClass,                // Put member's class here. 
      __out_ecount_part_opt(cchMember, *pchMember)
        LPWSTR      szMember,               // Put member's name here.
        ULONG       cchMember,              // Size of szMember buffer in wide chars.
        ULONG       *pchMember,             // Put actual size here 
        DWORD       *pdwAttr,               // Put flags here.
        PCCOR_SIGNATURE *ppvSigBlob,        // [OUT] point to the blob value of meta data
        ULONG       *pcbSigBlob,            // [OUT] actual size of signature blob
        ULONG       *pulCodeRVA,            // [OUT] codeRVA
        DWORD       *pdwImplFlags,          // [OUT] Impl. Flags
        DWORD       *pdwCPlusTypeFlag,      // [OUT] flag for value type. selected ELEMENT_TYPE_*
        UVCP_CONSTANT *ppValue,             // [OUT] constant value 
        ULONG       *pcchValue)             // [OUT] size of constant string in chars, 0 for non-strings.
    {
        HRESULT hr;
        HRESULT hrNameTruncation;
        if (!IsValidNonNilToken(mb, mdtMethodDef) && !IsValidNonNilToken(mb, mdtFieldDef))
        {
            // Idiosyncratic edge cases - delegate straight through to inherit the correct idiosyncratic edge result
            return m_pRawImport->GetMemberProps(mb, pClass, szMember, cchMember, pchMember, pdwAttr, ppvSigBlob, pcbSigBlob, pulCodeRVA, pdwImplFlags, pdwCPlusTypeFlag, ppValue, pcchValue);
        }

        PCCOR_SIGNATURE pOrigSig;
        ULONG cbOrigSig;
        
        IfFailRet(hrNameTruncation = m_pRawImport->GetMemberProps(mb, pClass, szMember, cchMember, pchMember, pdwAttr, &pOrigSig, &cbOrigSig, pulCodeRVA, pdwImplFlags, pdwCPlusTypeFlag, ppValue, pcchValue));

        LPCSTR szNewName = NULL;
        IfFailRet(m_pWinMDAdapter->ModifyMemberProps(mb, pdwAttr, pdwImplFlags, pulCodeRVA, &szNewName));
        
        if (IsValidNonNilToken(mb, mdtMethodDef))
        {        
            IfFailRet((m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtMethodDef>(
                mb, 
                &pOrigSig,          // ppOrigSig
                &cbOrigSig,         // pcbOrigSig
                ppvSigBlob, 
                pcbSigBlob, 
                m_pRawImport)));
        }
        else if (IsValidNonNilToken(mb, mdtFieldDef))
        {
            IfFailRet((m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtFieldDef>(
                mb, 
                &pOrigSig,          // ppOrigSig
                &cbOrigSig,         // pcbOrigSig
                ppvSigBlob, 
                pcbSigBlob, 
                m_pRawImport)));
        }
        else
        {
            if (ppvSigBlob != NULL)
                *ppvSigBlob = pOrigSig;

            if (pcbSigBlob != NULL)
                *pcbSigBlob = cbOrigSig;
        } 

        if (szNewName != NULL)
        {
            // We want to return name truncation status from the method that really fills the output buffer, rewrite the previous value
            IfFailRet(hrNameTruncation = DeliverUtf8String(szNewName, szMember, cchMember, pchMember));
        }
        
        // Return the success code from name filling (S_OK or CLDB_S_TRUNCATION)
        return hrNameTruncation;
    }


    STDMETHODIMP GetFieldProps(
        mdFieldDef  mb,                     // The field for which to get props.
        mdTypeDef   *pClass,                // Put field's class here.
      __out_ecount_part_opt(cchField, *pchField)
        LPWSTR      szField,                // Put field's name here.
        ULONG       cchField,               // Size of szField buffer in wide chars.
        ULONG       *pchField,              // Put actual size here 
        DWORD       *pdwAttr,               // Put flags here.
        PCCOR_SIGNATURE *ppvSigBlob,        // [OUT] point to the blob value of meta data
        ULONG       *pcbSigBlob,            // [OUT] actual size of signature blob
        DWORD       *pdwCPlusTypeFlag,      // [OUT] flag for value type. selected ELEMENT_TYPE_*
        UVCP_CONSTANT *ppValue,             // [OUT] constant value 
        ULONG       *pcchValue)             // [OUT] size of constant string in chars, 0 for non-strings.
    {
        HRESULT hr;
        HRESULT hrNameTruncation;

        PCCOR_SIGNATURE pOrigSig;
        ULONG cbOrigSig;
        IfFailRet(hrNameTruncation = m_pRawImport->GetFieldProps(mb, pClass, szField, cchField, pchField, pdwAttr, &pOrigSig, &cbOrigSig, pdwCPlusTypeFlag, ppValue, pcchValue));

        IfFailRet(m_pWinMDAdapter->ModifyFieldDefProps(mb, pdwAttr));
        IfFailRet((m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtFieldDef>(
            mb,
            &pOrigSig, 
            &cbOrigSig, 
            ppvSigBlob, 
            pcbSigBlob, 
            m_pRawImport)));
        
        // Return the success code from name filling (S_OK or CLDB_S_TRUNCATION)
        return hrNameTruncation;
    }


    STDMETHODIMP GetPropertyProps(            // S_OK, S_FALSE, or error. 
        mdProperty  prop,                   // [IN] property token
        mdTypeDef   *pClass,                // [OUT] typedef containing the property declarion. 
        LPCWSTR     szProperty,             // [OUT] Property name
        ULONG       cchProperty,            // [IN] the count of wchar of szProperty
        ULONG       *pchProperty,           // [OUT] actual count of wchar for property name
        DWORD       *pdwPropFlags,          // [OUT] property flags.
        PCCOR_SIGNATURE *ppvSig,            // [OUT] property type. pointing to meta data internal blob 
        ULONG       *pbSig,                 // [OUT] count of bytes in *ppvSig
        DWORD       *pdwCPlusTypeFlag,      // [OUT] flag for value type. selected ELEMENT_TYPE_*
        UVCP_CONSTANT *ppDefaultValue,      // [OUT] constant value 
        ULONG       *pcchDefaultValue,      // [OUT] size of constant string in chars, 0 for non-strings.
        mdMethodDef *pmdSetter,             // [OUT] setter method of the property
        mdMethodDef *pmdGetter,             // [OUT] getter method of the property
        mdMethodDef rmdOtherMethod[],       // [OUT] other method of the property
        ULONG       cMax,                   // [IN] size of rmdOtherMethod
        ULONG       *pcOtherMethod)         // [OUT] total number of other method of this property
    {
        HRESULT hr = S_OK;
        HRESULT hrNameTruncation;

        ULONG cbOrigSigBlob = (ULONG)(-1);
        PCCOR_SIGNATURE pOrigSig = NULL;
        IfFailRet(hrNameTruncation = m_pRawImport->GetPropertyProps(prop, pClass, szProperty, cchProperty, pchProperty, pdwPropFlags, &pOrigSig, &cbOrigSigBlob, pdwCPlusTypeFlag, ppDefaultValue, pcchDefaultValue, pmdSetter, pmdGetter, rmdOtherMethod, cMax, pcOtherMethod));

        IfFailRet((m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtProperty>(
            prop, 
            &pOrigSig,          // ppOrigSig
            &cbOrigSigBlob,     // pcbOrigSig
            ppvSig, 
            pbSig, 
            m_pRawImport)));
        // Return the success code from name filling (S_OK or CLDB_S_TRUNCATION)
        return hrNameTruncation;
    }


    STDMETHODIMP GetParamProps(               // S_OK or error.
        mdParamDef  tk,                     // [IN]The Parameter.
        mdMethodDef *pmd,                   // [OUT] Parent Method token.
        ULONG       *pulSequence,           // [OUT] Parameter sequence.
      __out_ecount_part_opt(cchName, *pchName)
        LPWSTR      szName,                 // [OUT] Put name here.
        ULONG       cchName,                // [OUT] Size of name buffer.
        ULONG       *pchName,               // [OUT] Put actual size of name here.
        DWORD       *pdwAttr,               // [OUT] Put flags here.
        DWORD       *pdwCPlusTypeFlag,      // [OUT] Flag for value type. selected ELEMENT_TYPE_*.
        UVCP_CONSTANT *ppValue,             // [OUT] Constant value.
        ULONG       *pcchValue)             // [OUT] size of constant string in chars, 0 for non-strings.
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawImport->GetParamProps(tk, pmd, pulSequence, szName, cchName, pchName, pdwAttr, pdwCPlusTypeFlag, ppValue, pcchValue);
    }


    STDMETHODIMP GetCustomAttributeByName(    // S_OK or error.
        mdToken     tkObj,                  // [IN] Object with Custom Attribute.
        LPCWSTR     wszName,                // [IN] Name of desired Custom Attribute.
        const void  **ppData,               // [OUT] Put pointer to data here.
        ULONG       *pcbData)               // [OUT] Put size of data here.
    {
        HRESULT hr;
        if (wszName == NULL)
        {
            hr = S_FALSE;   // Questionable response but maintains compatibility with RegMeta
        }
        else
        {
            MAKE_UTF8PTR_FROMWIDE_NOTHROW(szName, wszName);
            IfNullGo(szName);
            IfFailGo(CommonGetCustomAttributeByName(tkObj, szName, ppData, pcbData));
        }
      ErrExit:
        return hr;
    }


    STDMETHODIMP_(BOOL) IsValidToken(         // True or False.
        mdToken     tk)                     // [IN] Given token.
    {
        mdToken tokenType = TypeFromToken(tk);
        if (tokenType == mdtAssemblyRef)
            return m_pWinMDAdapter->IsValidAssemblyRefToken(tk);
        
        return m_pRawImport->IsValidToken(tk);
    }


    STDMETHODIMP GetNestedClassProps(         // S_OK or error.
        mdTypeDef   tdNestedClass,          // [IN] NestedClass token.
        mdTypeDef   *ptdEnclosingClass)       // [OUT] EnclosingClass token.
    {
        return m_pRawImport->GetNestedClassProps(tdNestedClass, ptdEnclosingClass);
    }


    STDMETHODIMP GetNativeCallConvFromSig(    // S_OK or error.
        void const  *pvSig,                 // [IN] Pointer to signature.
        ULONG       cbSig,                  // [IN] Count of signature bytes.
        ULONG       *pCallConv)             // [OUT] Put calling conv here (see CorPinvokemap).
    {
        return m_pRawImport->GetNativeCallConvFromSig(pvSig, cbSig, pCallConv);
    }


    STDMETHODIMP IsGlobal(                    // S_OK or error.
        mdToken     pd,                     // [IN] Type, Field, or Method token.
        int         *pbGlobal)              // [OUT] Put 1 if global, 0 otherwise.
    {
        return m_pRawImport->IsGlobal(pd, pbGlobal);
    }

  public:

      // ========================================================
      // IMetaDataWinMDImport methods
      // ========================================================

      // This method returns the RAW view of the metadata. Essentially removing the Adapter's projection support for the typeRef.
      STDMETHODIMP GetUntransformedTypeRefProps(
          mdTypeRef   tr,                     // [IN] TypeRef token.
          mdToken     *ptkResolutionScope,    // [OUT] Resolution scope, ModuleRef or AssemblyRef.
          __out_ecount_part_opt(cchName, *pchName)
          LPWSTR      szName,                 // [OUT] Unprojected name of the TypeRef.
          ULONG       cchName,                // [IN] Size of buffer.
          ULONG       *pchName)               // [OUT] Size of Name.
      {
          // By-pass the call to the raw importer, removing the adapter layer.
          return m_pRawImport->GetTypeRefProps(tr, ptkResolutionScope, szName, cchName, pchName);
      }

    //=========================================================
    // IMetaDataImport2 methods
    //=========================================================
    STDMETHODIMP EnumGenericParams(
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdToken      tk,                    // [IN] TypeDef or MethodDef whose generic parameters are requested
        mdGenericParam rGenericParams[],    // [OUT] Put GenericParams here.
        ULONG       cMax,                   // [IN] Max GenericParams to put.
        ULONG       *pcGenericParams)       // [OUT] Put # put here.
    {
        return m_pRawImport->EnumGenericParams(phEnum, tk, rGenericParams, cMax, pcGenericParams);
    }

    
    STDMETHODIMP GetGenericParamProps(        // S_OK or error.
        mdGenericParam gp,                  // [IN] GenericParam
        ULONG        *pulParamSeq,          // [OUT] Index of the type parameter
        DWORD        *pdwParamFlags,        // [OUT] Flags, for future use (e.g. variance)
        mdToken      *ptOwner,              // [OUT] Owner (TypeDef or MethodDef)
        DWORD       *reserved,              // [OUT] For future use (e.g. non-type parameters)
      __out_ecount_part_opt(cchName, *pchName)
        LPWSTR       wzname,                // [OUT] Put name here
        ULONG        cchName,               // [IN] Size of buffer
        ULONG        *pchName)              // [OUT] Put size of name (wide chars) here.
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawImport->GetGenericParamProps(gp, pulParamSeq, pdwParamFlags, ptOwner, reserved, wzname, cchName, pchName);
    }

    
    STDMETHODIMP GetMethodSpecProps(
        mdMethodSpec mi,                    // [IN] The method instantiation
        mdToken *tkParent,                  // [OUT] MethodDef or MemberRef
        PCCOR_SIGNATURE *ppvSigBlob,        // [OUT] point to the blob value of meta data
        ULONG       *pcbSigBlob)            // [OUT] actual size of signature blob
    {
        HRESULT hr = S_OK;

        ULONG cbOrigSigBlob = (ULONG)(-1);
        PCCOR_SIGNATURE pOrigSig = NULL;
        IfFailRet(m_pRawImport->GetMethodSpecProps(mi, tkParent, &pOrigSig, &cbOrigSigBlob));

        return m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtMethodSpec>(
            mi, 
            &pOrigSig,          // ppOrigSig
            &cbOrigSigBlob,     // pcbOrigSig
            ppvSigBlob, 
            pcbSigBlob, 
            m_pRawImport);
    }

    
    STDMETHODIMP EnumGenericParamConstraints(
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdGenericParam tk,                  // [IN] GenericParam whose constraints are requested
        mdGenericParamConstraint rGenericParamConstraints[],    // [OUT] Put GenericParamConstraints here.
        ULONG       cMax,                   // [IN] Max GenericParamConstraints to put.
        ULONG       *pcGenericParamConstraints)       // [OUT] Put # put here.
    {
        return m_pRawImport->EnumGenericParamConstraints(phEnum, tk, rGenericParamConstraints, cMax, pcGenericParamConstraints);
    }

    
    STDMETHODIMP GetGenericParamConstraintProps( // S_OK or error.
        mdGenericParamConstraint gpc,       // [IN] GenericParamConstraint
        mdGenericParam *ptGenericParam,     // [OUT] GenericParam that is constrained
        mdToken      *ptkConstraintType)       // [OUT] TypeDef/Ref/Spec constraint
    {
        return m_pRawImport->GetGenericParamConstraintProps(gpc, ptGenericParam, ptkConstraintType);
    }

    
    STDMETHODIMP GetPEKind(                   // S_OK or error.
        DWORD* pdwPEKind,                   // [OUT] The kind of PE (0 - not a PE)
        DWORD* pdwMachine)                  // [OUT] Machine as defined in NT header
    {
        return m_pRawImport->GetPEKind(pdwPEKind, pdwMachine);
    }

    
    STDMETHODIMP GetVersionString(            // S_OK or error.
      __out_ecount_part_opt(ccBufSize, *pccBufSize)
        LPWSTR      pwzBuf,                 // [OUT] Put version string here.
        DWORD       ccBufSize,              // [IN] size of the buffer, in wide chars
        DWORD       *pccBufSize)            // [OUT] Size of the version string, wide chars, including terminating nul.
    {
        HRESULT hr;
        LPCSTR szVersion;
        IfFailRet(GetVersionString(&szVersion));
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return DeliverUtf8String(szVersion, pwzBuf, ccBufSize, pccBufSize);
    }

    
    STDMETHODIMP EnumMethodSpecs(
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdToken      tk,                    // [IN] MethodDef or MemberRef whose MethodSpecs are requested
        mdMethodSpec rMethodSpecs[],        // [OUT] Put MethodSpecs here.
        ULONG       cMax,                   // [IN] Max tokens to put.
        ULONG       *pcMethodSpecs)         // [OUT] Put actual count here.
    {
        return m_pRawImport->EnumMethodSpecs(phEnum, tk, rMethodSpecs, cMax, pcMethodSpecs);
    }


  public:
    //=========================================================
    // IMetaDataAssemblyImport methods
    //=========================================================

    STDMETHODIMP GetAssemblyProps(            // S_OK or error.
        mdAssembly  mda,                    // [IN] The Assembly for which to get the properties.
        const void  **ppbPublicKey,         // [OUT] Pointer to the public key.
        ULONG       *pcbPublicKey,          // [OUT] Count of bytes in the public key.
        ULONG       *pulHashAlgId,          // [OUT] Hash Algorithm.
        __out_ecount_part_opt(cchName, *pchName) LPWSTR  szName, // [OUT] Buffer to fill with assembly's simply name.
        ULONG       cchName,                // [IN] Size of buffer in wide chars.
        ULONG       *pchName,               // [OUT] Actual # of wide chars in name.
        ASSEMBLYMETADATA *pMetaData,        // [OUT] Assembly MetaData.
        DWORD       *pdwAssemblyFlags)          // [OUT] Flags.
    {
        return m_pRawAssemblyImport->GetAssemblyProps(mda, ppbPublicKey, pcbPublicKey, pulHashAlgId, szName, cchName, pchName, pMetaData, pdwAssemblyFlags);
    }


    STDMETHODIMP GetAssemblyRefProps(         // S_OK or error.
        mdAssemblyRef mdar,                 // [IN] The AssemblyRef for which to get the properties.
        const void  **ppbPublicKeyOrToken,  // [OUT] Pointer to the public key or token.
        ULONG       *pcbPublicKeyOrToken,   // [OUT] Count of bytes in the public key or token.
        __out_ecount_part_opt(cchName, *pchName)LPWSTR szName, // [OUT] Buffer to fill with name.
        ULONG       cchName,                // [IN] Size of buffer in wide chars.
        ULONG       *pchName,               // [OUT] Actual # of wide chars in name.
        ASSEMBLYMETADATA *pMetaData,        // [OUT] Assembly MetaData.
        const void  **ppbHashValue,         // [OUT] Hash blob.
        ULONG       *pcbHashValue,          // [OUT] Count of bytes in the hash blob.
        DWORD       *pdwAssemblyRefFlags)       // [OUT] Flags.
    {
        HRESULT hr;
        HRESULT hrNameTruncation;

        if (!IsValidNonNilToken(mdar, mdtAssemblyRef))
        {
            // Idiosyncratic edge cases - delegate straight through to inherit the correct idiosyncratic edge result
            return m_pRawAssemblyImport->GetAssemblyRefProps(mdar, ppbPublicKeyOrToken, pcbPublicKeyOrToken, szName, cchName, pchName, pMetaData, ppbHashValue, pcbHashValue, pdwAssemblyRefFlags);
        }

        mdAssemblyRef md = mdar;
        if (RidFromToken(md) > m_pWinMDAdapter->GetRawAssemblyRefCount())
        {
            // The extra framework assemblies we add references to should all have the
            // same verion, key, culture, etc as those of mscorlib.
            // So we retrieve the mscorlib properties and change the name.
            md = m_pWinMDAdapter->GetAssemblyRefMscorlib();
        }

        IfFailRet(hrNameTruncation = m_pRawAssemblyImport->GetAssemblyRefProps(md, ppbPublicKeyOrToken, pcbPublicKeyOrToken, szName, cchName, pchName, pMetaData, ppbHashValue, pcbHashValue, pdwAssemblyRefFlags));

        LPCSTR szNewName = nullptr;
        USHORT *pusMajorVersion = nullptr;
        USHORT *pusMinorVersion = nullptr;
        USHORT *pusBuildNumber = nullptr;
        USHORT *pusRevisionNumber = nullptr;

        if (pMetaData != nullptr)
        {
            pusMajorVersion = &pMetaData->usMajorVersion;
            pusMinorVersion = &pMetaData->usMinorVersion;
            pusBuildNumber = &pMetaData->usBuildNumber;
            pusRevisionNumber = &pMetaData->usRevisionNumber;
        }

        m_pWinMDAdapter->ModifyAssemblyRefProps(
            mdar,
            ppbPublicKeyOrToken,
            pcbPublicKeyOrToken,
            &szNewName,
            pusMajorVersion,
            pusMinorVersion,
            pusBuildNumber,
            pusRevisionNumber,
            ppbHashValue,
            pcbHashValue);
        
        if (szNewName != nullptr)
        {
            IfFailRet(hrNameTruncation = DeliverUtf8String(szNewName, szName, cchName, pchName));
        }

        // Return the success code from name filling (S_OK or CLDB_S_TRUNCATION)
        return hrNameTruncation;
    }


    STDMETHODIMP GetFileProps(                // S_OK or error.
        mdFile      mdf,                    // [IN] The File for which to get the properties.
        __out_ecount_part_opt(cchName, *pchName) LPWSTR      szName, // [OUT] Buffer to fill with name.
        ULONG       cchName,                // [IN] Size of buffer in wide chars.
        ULONG       *pchName,               // [OUT] Actual # of wide chars in name.
        const void  **ppbHashValue,         // [OUT] Pointer to the Hash Value Blob.
        ULONG       *pcbHashValue,          // [OUT] Count of bytes in the Hash Value Blob.
        DWORD       *pdwFileFlags)          // [OUT] Flags.
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawAssemblyImport->GetFileProps(mdf, szName, cchName, pchName, ppbHashValue, pcbHashValue, pdwFileFlags);
    }


    STDMETHODIMP GetExportedTypeProps(        // S_OK or error.
        mdExportedType   mdct,              // [IN] The ExportedType for which to get the properties.
        __out_ecount_part_opt(cchName, *pchName) LPWSTR      szName, // [OUT] Buffer to fill with name.
        ULONG       cchName,                // [IN] Size of buffer in wide chars.
        ULONG       *pchName,               // [OUT] Actual # of wide chars in name.
        mdToken     *ptkImplementation,     // [OUT] mdFile or mdAssemblyRef or mdExportedType.
        mdTypeDef   *ptkTypeDef,            // [OUT] TypeDef token within the file.
        DWORD       *pdwExportedTypeFlags)       // [OUT] Flags.
    {
        HRESULT hr;
        LPCSTR szUtf8Namespace;
        LPCSTR szUtf8Name;
        if (!IsValidNonNilToken(mdct, mdtExportedType))
        {
            // Idiosyncractic edge cases - delegate straight through to inherit the correct idiosyncractic edge results 
            return m_pRawAssemblyImport->GetExportedTypeProps(mdct, szName, cchName, pchName, ptkImplementation, ptkTypeDef, pdwExportedTypeFlags);
        }
        IfFailRet(m_pRawAssemblyImport->GetExportedTypeProps(mdct, NULL, 0, NULL, NULL, ptkTypeDef, pdwExportedTypeFlags));
        IfFailRet(this->CommonGetExportedTypeProps(mdct, &szUtf8Namespace, &szUtf8Name, ptkImplementation));
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return DeliverUtf8NamespaceAndName(szUtf8Namespace, szUtf8Name, szName, cchName, pchName);
    }


    STDMETHODIMP GetManifestResourceProps(    // S_OK or error.
        mdManifestResource  mdmr,           // [IN] The ManifestResource for which to get the properties.
        __out_ecount_part_opt(cchName, *pchName)LPWSTR      szName,  // [OUT] Buffer to fill with name.
        ULONG       cchName,                // [IN] Size of buffer in wide chars.
        ULONG       *pchName,               // [OUT] Actual # of wide chars in name.
        mdToken     *ptkImplementation,     // [OUT] mdFile or mdAssemblyRef that provides the ManifestResource.
        DWORD       *pdwOffset,             // [OUT] Offset to the beginning of the resource within the file.
        DWORD       *pdwResourceFlags)      // [OUT] Flags.
    {
        // Returns error code from name filling (may be CLDB_S_TRUNCTATION)
        return m_pRawAssemblyImport->GetManifestResourceProps(mdmr, szName, cchName, pchName, ptkImplementation, pdwOffset, pdwResourceFlags);
    }


    STDMETHODIMP EnumAssemblyRefs(            // S_OK or error
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdAssemblyRef rAssemblyRefs[],      // [OUT] Put AssemblyRefs here.
        ULONG       cMax,                   // [IN] Max AssemblyRefs to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        if (*phEnum != NULL)
        {
            // No trick needed: a previous call to EnumAssemblyRefs must have already taken care of the
            // extra assembly refs.
            return m_pRawAssemblyImport->EnumAssemblyRefs(phEnum, rAssemblyRefs, cMax, pcTokens);
        }
        else
        {
            // if *phEnum is NULL, we need create the HENUMInternal, adjust the assembly ref count, 
            // and enumerate the number of assembly refs requested. This is done in three steps:
            HRESULT hr;

            // Step 1: Call EnumAssemblyRefs with an empty buffer to create the HENUMInternal
            IfFailGo(m_pRawAssemblyImport->EnumAssemblyRefs(phEnum, NULL, 0, NULL));

            // Step 2: Increment the cound to include the extra assembly refs
            HENUMInternal *phInternalEnum = static_cast<HENUMInternal*>(*phEnum);

            _ASSERTE(phInternalEnum->m_EnumType == MDSimpleEnum);

            _ASSERTE( phInternalEnum->m_ulCount == m_pWinMDAdapter->GetRawAssemblyRefCount());
            int n = m_pWinMDAdapter->GetExtraAssemblyRefCount();
            phInternalEnum->m_ulCount += n;
            phInternalEnum->u.m_ulEnd += n;

            // Step 3: Call EnumAssemblyRefs again and pass in the modifed HENUMInternal and the real buffer
            IfFailGo(m_pRawAssemblyImport->EnumAssemblyRefs(phEnum, rAssemblyRefs, cMax, pcTokens));

ErrExit:
            return hr;
        }
    }


    STDMETHODIMP EnumFiles(                   // S_OK or error
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdFile      rFiles[],               // [OUT] Put Files here.
        ULONG       cMax,                   // [IN] Max Files to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawAssemblyImport->EnumFiles(phEnum, rFiles, cMax, pcTokens);
    }


    STDMETHODIMP EnumExportedTypes(           // S_OK or error
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdExportedType   rExportedTypes[],  // [OUT] Put ExportedTypes here.
        ULONG       cMax,                   // [IN] Max ExportedTypes to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawAssemblyImport->EnumExportedTypes(phEnum, rExportedTypes, cMax, pcTokens);
    }


    STDMETHODIMP EnumManifestResources(       // S_OK or error
        HCORENUM    *phEnum,                // [IN|OUT] Pointer to the enum.
        mdManifestResource  rManifestResources[],   // [OUT] Put ManifestResources here.
        ULONG       cMax,                   // [IN] Max Resources to put.
        ULONG       *pcTokens)              // [OUT] Put # put here.
    {
        return m_pRawAssemblyImport->EnumManifestResources(phEnum, rManifestResources, cMax, pcTokens);
    }


    STDMETHODIMP GetAssemblyFromScope(        // S_OK or error
        mdAssembly  *ptkAssembly)           // [OUT] Put token here.
    {
        return m_pRawAssemblyImport->GetAssemblyFromScope(ptkAssembly);
    }


    STDMETHODIMP FindExportedTypeByName(     // S_OK or error
        LPCWSTR     wszName,                 // [IN] Name of the ExportedType.
        mdToken     mdtExportedType,         // [IN] ExportedType for the enclosing class.
        mdExportedType   *ptkExportedType)   // [OUT] Put the ExportedType token here.
    {
        if (wszName == NULL)
            return E_INVALIDARG;

        LPUTF8      szFullName;
        LPCUTF8     szNamespace;
        LPCUTF8     szName;
        // Convert the  name to UTF8.
        UTF8STR(wszName, szFullName);
        ns::SplitInline(szFullName, szNamespace, szName);
        return this->CommonFindExportedType(szNamespace, szName, mdtExportedType, ptkExportedType);
    }


    STDMETHODIMP FindManifestResourceByName(  // S_OK or error
        LPCWSTR     szName,                 // [IN] Name of the ManifestResource.
        mdManifestResource *ptkManifestResource)        // [OUT] Put the ManifestResource token here.
    {
        return m_pRawAssemblyImport->FindManifestResourceByName(szName, ptkManifestResource);
    }



    STDMETHODIMP FindAssembliesByName(        // S_OK or error
        LPCWSTR  szAppBase,                 // [IN] optional - can be NULL
        LPCWSTR  szPrivateBin,              // [IN] optional - can be NULL
        LPCWSTR  szAssemblyName,            // [IN] required - this is the assembly you are requesting
        IUnknown *ppIUnk[],                 // [OUT] put IMetaDataAssemblyImport pointers here
        ULONG    cMax,                      // [IN] The max number to put
        ULONG    *pcAssemblies)             // [OUT] The number of assemblies returned.
    {
        return m_pRawAssemblyImport->FindAssembliesByName(szAppBase, szPrivateBin, szAssemblyName, ppIUnk, cMax, pcAssemblies);
    }

    //=========================================================
    // IMetaDataValidate
    //=========================================================
    STDMETHODIMP ValidatorInit(               // S_OK or error.
        DWORD       dwModuleType,           // [IN] Specifies the type of the module.
        IUnknown    *pUnk)                  // [IN] Validation error handler.
    {
        if (m_pRawValidate == nullptr)
            return E_NOTIMPL;

        return m_pRawValidate->ValidatorInit(dwModuleType, pUnk);
    }

    STDMETHODIMP ValidateMetaData()            // S_OK or error.
    {
        if (m_pRawValidate == nullptr)
            return E_NOTIMPL;

        return m_pRawValidate->ValidateMetaData();
    }

    //=========================================================
    // IMDCommon methods
    //=========================================================
    STDMETHODIMP_(IMetaModelCommon*) GetMetaModelCommon()
    {
        return this;
    }

    STDMETHODIMP_(IMetaModelCommonRO*) GetMetaModelCommonRO()
    {
        _ASSERTE(!"WinMDImport does not support GetMetaModelCommonRO(). The most likely cause of this assert is that you're trying to wrap a WinMD adapter around another WinMD adapter.");
        return NULL;
    }

    STDMETHODIMP GetVersionString(LPCSTR *pszVersionString)
    {
        return m_pWinMDAdapter->GetVersionString(pszVersionString);
    }


    //=========================================================
    // IMetaModelCommon methods
    //=========================================================
    __checkReturn 
    virtual HRESULT CommonGetScopeProps(
        LPCUTF8     *pszName,
        GUID        *pMvid)
    {
        return m_pRawMetaModelCommonRO->CommonGetScopeProps(pszName, pMvid);
    }

    __checkReturn 
    virtual HRESULT CommonGetTypeRefProps(
        mdTypeRef tr,
        LPCUTF8     *pszNamespace,
        LPCUTF8     *pszName,
        mdToken     *ptkResolution)
    {
        return m_pWinMDAdapter->GetTypeRefProps(tr, pszNamespace, pszName, ptkResolution);
    }
    
    
    __checkReturn 
    virtual HRESULT CommonGetTypeDefProps(
        mdTypeDef td,
        LPCUTF8     *pszNameSpace,
        LPCUTF8     *pszName,
        DWORD       *pdwFlags,
        mdToken     *pdwExtends,
        ULONG       *pMethodList)
    {
        HRESULT hr;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetTypeDefProps(td, NULL, NULL, NULL, NULL, pMethodList));
        IfFailGo(m_pWinMDAdapter->GetTypeDefProps(td, pszNameSpace, pszName, pdwFlags, pdwExtends));
      ErrExit:
        return hr;
    }
    
    
    __checkReturn 
    virtual HRESULT CommonGetTypeSpecProps(
        mdTypeSpec ts,
        PCCOR_SIGNATURE *ppvSig,
        ULONG       *pcbSig)
    {
        return m_pWinMDAdapter->GetSignatureForToken<IMetaDataImport2, mdtTypeSpec>(
            ts, 
            NULL,     // ppOrigSig
            NULL,     // pcbOrigSig
            ppvSig, 
            pcbSig, 
            m_pRawImport);
    }
    
    
    __checkReturn 
    virtual HRESULT CommonGetEnclosingClassOfTypeDef(
        mdTypeDef  td, 
        mdTypeDef *ptkEnclosingTypeDef)
    {
        return m_pRawMetaModelCommonRO->CommonGetEnclosingClassOfTypeDef(td, ptkEnclosingTypeDef);
    }
    
    
    __checkReturn 
    virtual HRESULT CommonGetAssemblyProps(
        USHORT      *pusMajorVersion,
        USHORT      *pusMinorVersion,
        USHORT      *pusBuildNumber,
        USHORT      *pusRevisionNumber,
        DWORD       *pdwFlags,
        const void  **ppbPublicKey,
        ULONG       *pcbPublicKey,
        LPCUTF8     *pszName,
        LPCUTF8     *pszLocale)
    {
        return m_pRawMetaModelCommonRO->CommonGetAssemblyProps(pusMajorVersion, pusMinorVersion, pusBuildNumber, pusRevisionNumber, pdwFlags, ppbPublicKey, pcbPublicKey, pszName, pszLocale);
    }
    
    
    __checkReturn 
    virtual HRESULT CommonGetAssemblyRefProps(
        mdAssemblyRef tkAssemRef,
        USHORT      *pusMajorVersion,
        USHORT      *pusMinorVersion,
        USHORT      *pusBuildNumber,
        USHORT      *pusRevisionNumber,
        DWORD       *pdwFlags,
        const void  **ppbPublicKeyOrToken,
        ULONG       *pcbPublicKeyOrToken,
        LPCUTF8     *pszName,
        LPCUTF8     *pszLocale,
        const void  **ppbHashValue,
        ULONG       *pcbHashValue)
    {
        HRESULT hr;

        mdAssemblyRef md = tkAssemRef;
        if (RidFromToken(md) > m_pWinMDAdapter->GetRawAssemblyRefCount())
        {
            // The extra framework assemblies we add references to should all have the
            // same verion, key, culture, etc as those of mscorlib.
            // So we retrieve the mscorlib properties and change the name.
            md = m_pWinMDAdapter->GetAssemblyRefMscorlib();
        }

        IfFailRet(m_pRawMetaModelCommonRO->CommonGetAssemblyRefProps(
            md, 
            pusMajorVersion, 
            pusMinorVersion, 
            pusBuildNumber, 
            pusRevisionNumber, 
            pdwFlags, 
            ppbPublicKeyOrToken, 
            pcbPublicKeyOrToken, 
            pszName, 
            pszLocale, 
            ppbHashValue, 
            pcbHashValue));

        m_pWinMDAdapter->ModifyAssemblyRefProps(
            tkAssemRef,
            ppbPublicKeyOrToken,
            pcbPublicKeyOrToken,
            pszName,
            pusMajorVersion,
            pusMinorVersion,
            pusBuildNumber,
            pusRevisionNumber,
            ppbHashValue,
            pcbHashValue);

        return hr;
    }
    
    
    __checkReturn 
    virtual HRESULT CommonGetModuleRefProps(
        mdModuleRef tkModuleRef,
        LPCUTF8     *pszName)
    {
        return m_pRawMetaModelCommonRO->CommonGetModuleRefProps(tkModuleRef, pszName);
    }
    
    
    __checkReturn 
    virtual HRESULT CommonFindExportedType(
        LPCUTF8     szNamespace,
        LPCUTF8     szName,
        mdToken     tkEnclosingType,
        mdExportedType   *ptkExportedType)
    {
        return m_pWinMDAdapter->FindExportedType(szNamespace, szName, tkEnclosingType, ptkExportedType);
    }
    
    
    __checkReturn 
    virtual HRESULT CommonGetExportedTypeProps(
        mdToken     tkExportedType,
        LPCUTF8     *pszNamespace,
        LPCUTF8     *pszName,
        mdToken     *ptkImpl)
    {
        HRESULT hr;
        IfFailRet(m_pRawMetaModelCommonRO->CommonGetExportedTypeProps(tkExportedType, pszNamespace, pszName, ptkImpl));
        IfFailRet(m_pWinMDAdapter->ModifyExportedTypeName(tkExportedType, pszNamespace, pszName));
        return hr;
    }
    
    
    virtual int CommonIsRo()
    {
        return m_pRawMetaModelCommonRO->CommonIsRo();
    }
    
    
    __checkReturn 
    virtual HRESULT CommonGetCustomAttributeByNameEx( // S_OK or error.
        mdToken            tkObj,            // [IN] Object with Custom Attribute.
        LPCUTF8            szName,           // [IN] Name of desired Custom Attribute.
        mdCustomAttribute *ptkCA,            // [OUT] put custom attribute token here
        const void       **ppData,           // [OUT] Put pointer to data here.
        ULONG             *pcbData)          // [OUT] Put size of data here.
    {
        return m_pWinMDAdapter->GetCustomAttributeByName(tkObj, szName, ptkCA, ppData, pcbData);
    }
    

    __checkReturn 
    virtual HRESULT FindParentOfMethodHelper(mdMethodDef md, mdTypeDef *ptd)
    {
        return m_pRawMetaModelCommonRO->FindParentOfMethodHelper(md, ptd);
    }



 public:
    //=========================================================
    // IGetIMDInternalImport methods
    //=========================================================
    STDMETHODIMP GetIMDInternalImport(IMDInternalImport ** ppIMDInternalImport)
    {
        HRESULT hr = S_OK;
        ReleaseHolder<IMDCommon> pMDCommon;
        ReleaseHolder<IUnknown> pRawMDInternalImport;

        *ppIMDInternalImport = NULL;
        // Get the raw IMDInternalImport
        IfFailGo(GetMDInternalInterfaceFromPublic(m_pRawImport, IID_IMDInternalImport, (LPVOID*)(&pRawMDInternalImport)));
    
        // Create an adapter around the internal raw interface
        IfFailGo(pRawMDInternalImport->QueryInterface(IID_IMDCommon, (LPVOID*)(&pMDCommon)));
        IfFailGo(CreateWinMDInternalImportRO(pMDCommon, IID_IMDInternalImport, (void**)ppIMDInternalImport));
    
      ErrExit:
        return hr;
    }


    //=========================================================
    // Private methods
    //=========================================================

    //------------------------------------------------------------------------------------------------------
    // Deliver a result string (Unicode) to a caller's sized output buffer using the standard convention
    // followed by all metadata api.
    //------------------------------------------------------------------------------------------------------
    static HRESULT DeliverUnicodeString(LPCWSTR wszResult, __out_ecount_part(cchCallerBuffer, *pchSizeNeeded) LPWSTR wszCallerBuffer, ULONG cchCallerBuffer, ULONG *pchSizeNeeded)
    {
        ULONG cchActual = (ULONG)(wcslen(wszResult) + 1);
        if (pchSizeNeeded)
        {
            *pchSizeNeeded = cchActual;
        }
        if (wszCallerBuffer == NULL || cchCallerBuffer < cchActual)
        {
            if (wszCallerBuffer != NULL)
            {
                memcpy(wszCallerBuffer, wszResult, cchCallerBuffer * sizeof(WCHAR)); // If buffer too small, return truncated result to be compatible with metadata api conventions
                if (cchCallerBuffer > 0)
                {   // null-terminate the truncated output string
                    wszCallerBuffer[cchCallerBuffer - 1] = W('\0');
                }
            }
            return CLDB_S_TRUNCATION;
        }
        else
        {
            memcpy(wszCallerBuffer, wszResult, cchActual * sizeof(WCHAR));
            return S_OK;
        }
    }

    //------------------------------------------------------------------------------------------------------
    // Deliver a result string (Utf8) to a caller's sized output buffer using the standard convention
    // followed by all metadata api.
    //------------------------------------------------------------------------------------------------------
    static HRESULT DeliverUtf8String(LPCSTR szUtf8Result, __out_ecount_part(cchCallerBuffer, *pchSizeNeeded) LPWSTR wszCallerBuffer, ULONG cchCallerBuffer, ULONG *pchSizeNeeded)
    {
        MAKE_WIDEPTR_FROMUTF8_NOTHROW(wzResult, szUtf8Result);
        if (wzResult == NULL)
            return E_OUTOFMEMORY;
        return DeliverUnicodeString(wzResult, wszCallerBuffer, cchCallerBuffer, pchSizeNeeded);
    }

    //------------------------------------------------------------------------------------------------------
    // Combine a result namespace/name string pair (Utf8) to a Unicode fullname and deliver to a caller's
    // sized output buffer using the standard convention followed by all metadata api.
    //------------------------------------------------------------------------------------------------------
    static HRESULT DeliverUtf8NamespaceAndName(LPCSTR szUtf8Namespace, LPCSTR szUtf8Name, __out_ecount_part(cchCallerBuffer, *pchSizeNeeded) LPWSTR wszCallerBuffer, ULONG cchCallerBuffer, ULONG *pchSizeNeeded)
    {
        HRESULT hr;
        if (wszCallerBuffer != NULL || pchSizeNeeded != NULL)
        {
            MAKE_WIDEPTR_FROMUTF8_NOTHROW(wzNamespace, szUtf8Namespace);
            IfNullRet(wzNamespace);
            MAKE_WIDEPTR_FROMUTF8_NOTHROW(wzName, szUtf8Name);
            IfNullRet(wzName);

            BOOL fTruncation = FALSE;
            if (wszCallerBuffer != NULL)
            {
                fTruncation = !(ns::MakePath(wszCallerBuffer, cchCallerBuffer, wzNamespace, wzName));
                if (fTruncation && (cchCallerBuffer > 0))
                {   // null-terminate the truncated output string
                    wszCallerBuffer[cchCallerBuffer - 1] = W('\0');
                }
            }
            if (pchSizeNeeded != NULL)
            {
                if (fTruncation || (wszCallerBuffer == NULL))
                {
                    *pchSizeNeeded = ns::GetFullLength(wzNamespace, wzName);
                }
                else
                {
                    *pchSizeNeeded = (ULONG)(wcslen(wszCallerBuffer) + 1);
                }
            }
            hr = fTruncation ? CLDB_S_TRUNCATION : S_OK;
        }
        else
        {
            hr = S_OK; // Caller did not request name back.
        }
        return hr;
    }

  private:
    //=========================================================
    // Private instance data
    //=========================================================
    IMDCommon               *m_pRawMDCommon;
    IMetaDataImport2        *m_pRawImport;
    IMetaDataAssemblyImport *m_pRawAssemblyImport;
    IMetaDataValidate       *m_pRawValidate;
    IMetaModelCommonRO      *m_pRawMetaModelCommonRO;
    IUnknown                *m_pFreeThreadedMarshaler;
    WinMDAdapter            *m_pWinMDAdapter;
    LONG                     m_cRef;

};  // class WinMDImport



//========================================================================================
// Entrypoint called by IMetaDataDispenser::OpenScope()
//========================================================================================
HRESULT CreateWinMDImport(IMDCommon * pRawMDCommon, REFIID riid, /*[out]*/ void **ppWinMDImport)
{
    HRESULT hr;
    *ppWinMDImport = NULL;
    WinMDImport *pWinMDImport = NULL;
    IfFailGo(WinMDImport::Create(pRawMDCommon, &pWinMDImport));
    IfFailGo(pWinMDImport->QueryInterface(riid, ppWinMDImport));
    hr = S_OK;
  ErrExit:
    if (pWinMDImport)
        pWinMDImport->Release();
    return hr;
}