Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StandardInterfaces.cs « Shared « src « System.Private.Interop « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d21279cc6c748a77df28762fd7e5e7ba7acfe234 (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
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
// 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.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Text;
using System.Runtime;
using Internal.NativeFormat;

namespace System.Runtime.InteropServices
{
    [CLSCompliant(false)]
    public unsafe struct __com_IUnknown
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IUnknown* pVtable;
#pragma warning restore 649
    }

#pragma warning disable 414 // The field 'blah' is assigned but its value is never used

    [EditorBrowsable(EditorBrowsableState.Never)]
    [CLSCompliant(false)]
    public unsafe struct __vtable_IUnknown
    {
        // IUnknown
        internal IntPtr pfnQueryInterface;
        internal IntPtr pfnAddRef;
        internal IntPtr pfnRelease;

        public static IntPtr pNativeVtable;

        private static __vtable_IUnknown s_theCcwVtable;

        /// <summary>
        /// Eager library initializer called from LibraryInitializer.cs for the module
        /// </summary>
        internal static void Initialize()
        {
            s_theCcwVtable = new __vtable_IUnknown
            {
                pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(QueryInterface),
                pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(AddRef),
                pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(Release),
            };
        }

        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IUnknown);
        }
        internal static unsafe IntPtr GetCcwvtable_IUnknown()
        {
            if (__vtable_IUnknown.pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IUnknown.pNativeVtable,sizeof(__vtable_IUnknown));
                }
            }
            return __vtable_IUnknown.pNativeVtable;
        }

        [NativeCallable]
        public static int QueryInterface(
                    IntPtr __IntPtr__pComThis,
                    IntPtr __IntPtr__pIID,
                    IntPtr __IntPtr__ppvObject)
        {
            // Prevent reentrancy due to message pumping in blocking operations.
            InteropExtensions.SuppressReentrantWaits();

            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;
            Guid* pIID = (Guid*)__IntPtr__pIID;
            void** ppvObject = (void**)__IntPtr__ppvObject;

            // @TODO: does not distinguish between E_NOINTERFACE and E_OUTOFMEMORY
            var cco = pComThis->ComCallableObject;
            IntPtr result = cco.GetComInterfaceForIID(ref *pIID);
            *ppvObject = (void*)result;

            int hr;
            if (result == default(IntPtr))
                hr = Interop.COM.E_NOINTERFACE;
            else
                hr = Interop.COM.S_OK;

            // Restore reentrant waits.  No need for a 'finally' because exceptions in NativeCallable methods will FailFast.
            InteropExtensions.RestoreReentrantWaits();
            return hr;
        }

        /// <summary>
        /// Special NO GC region support helpers
        ///
        /// Declare the GC trigger helpers here because we want to call them directly to avoid
        /// GC stress hijacking InteropExtensions.RhpSet/ClearThreadDoNoTriggerGC
        ///
        /// WARNING: Please avoid using this API whenver possible. If you find yourself must call this
        /// API, please make sure you follow these rules:
        /// 1. The block where GC is disabled need to be small and fast
        /// 2. You can't allocate or call GC.Collect (you'll be surprised how many code actually do)
        /// 3. You can't throw out exception (which allocates)
        /// 4. You can't P/invoke
        /// 5. others?
        /// </summary>

#if CORECLR

        [NativeCallable]
        public static int AddRef(IntPtr __IntPtr__pComThis)
        {
            int newRefCount;

            //
            // NO GC REGION
            //
            // Jupiter assumes AddRef doesn't block on GC and that is indeed the case in desktop CLR
            // We need to match desktop CLR behavior, otherwise Jupiter would be taking a lock and then
            // calling AddRef which triggers another GC and calls out to Jupiter OnGCStart code, which
            // attempts to take the same lock.
            //
            // NOTE 1: We don't call SetDoNotTriggerGC in Release because we do allocate memory in Release()
            // and Jupiter doesn't call it inside their lock for now
            //
            // NOTE 2: Typically you should put those calls in a try/finally, but since you can't throw new
            // exception and even if we throw an existing exception we would failfast inside AddRef anyway,
            // there is no need to put it in a finally
            //

            // RhpSetThreadDoNotTriggerGC are nop on CoreCLR
            GCHelpers.RhpSetThreadDoNotTriggerGC();
            {

                __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;

                //
                // Use ->NativeCCW to make sure we failfast when calling AddRef/Release for neutered CCWs
                //
                newRefCount = pComThis->NativeCCW->AddCOMRef();
            }

            GCHelpers.RhpClearThreadDoNotTriggerGC();

            return newRefCount;
        }

        class GCHelpers
        {
            internal  static void RhpClearThreadDoNotTriggerGC(){}
            internal  static void RhpSetThreadDoNotTriggerGC(){}
        }
#else
        //
        // AddRef is implemented in native code, via Interop.Native.lib.  Here we link in the native code;
        // a DllImport from "*" means "import the named method from a linked .lib file."
        // NOTE:
        // Having McgGeneratedNativeCallCodeAttribute is *REQUIRED* because this is not really a p/invoke but
        // a reverse p/invoke in disguise. It is just convenient to use DllImport as a way to indicate a call
        // into a externally linked module. Without this attribute, MCG will generate code for this as a
        // p/invoke and disaster happens
        //
#if X86
        [DllImport("*", EntryPoint = "_CCWAddRef@4")]
#else
        [DllImport("*", EntryPoint = "CCWAddRef")]
#endif
        [McgGeneratedNativeCallCodeAttribute]
        public static extern int AddRef(IntPtr __IntPtr__pComThis);

        class GCHelpers
        {
            private const string RuntimeLibrary = "[MRT]";
            private const MethodImplOptions InternalCall = (MethodImplOptions)0x1000;

            [MethodImplAttribute(InternalCall)]
            [RuntimeImport(RuntimeLibrary, "RhpClearThreadDoNotTriggerGC")]
            internal extern static void RhpClearThreadDoNotTriggerGC();

            [MethodImplAttribute(InternalCall)]
            [RuntimeImport(RuntimeLibrary, "RhpSetThreadDoNotTriggerGC")]
            internal extern static void RhpSetThreadDoNotTriggerGC();
        }
#endif //CORECLR

        static __vtable_IUnknown()
        {
            ComCallableObject.InitRefCountedHandleCallback();
        }

        [NativeCallable]
        public static int Release(IntPtr __IntPtr__pComThis)
        {
            // Prevent reentrancy due to message pumping in blocking operations.
            InteropExtensions.SuppressReentrantWaits();

            int hr = __interface_ccw.DirectRelease(__IntPtr__pComThis);

            // Restore reentrant waits.  No need for a 'finally' because exceptions in NativeCallable methods will FailFast.
            InteropExtensions.RestoreReentrantWaits();
            return hr;
        }
    }

    [CLSCompliant(false)]
    public unsafe struct __com_IInspectable
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        public __vtable_IInspectable* pVtable;
#pragma warning restore 649
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    [CLSCompliant(false)]
    public unsafe struct __vtable_IInspectable
    {
        // IUnknown
        internal IntPtr pfnQueryInterface;
        internal IntPtr pfnAddRef;
        internal IntPtr pfnRelease;

        // IInspectable
        internal IntPtr pfnGetIids;
        internal IntPtr pfnGetRuntimeClassName;
        internal IntPtr pfnGetTrustLevel;

        public static IntPtr pNativeVtable;
        private static __vtable_IInspectable s_theCcwVtable = new __vtable_IInspectable
        {
            // IUnknown
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),
            // IInspectable
            pfnGetIids = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetIID>(GetIIDs),
            pfnGetRuntimeClassName = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(GetRuntimeClassName),
            pfnGetTrustLevel = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(GetTrustLevel),
        };
        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IInspectable);
        }
        internal static unsafe IntPtr GetCcwvtable_IInspectable()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IInspectable.pNativeVtable,sizeof(__vtable_IInspectable));
                }
            }
            return __vtable_IInspectable.pNativeVtable;
        }

        const int S_OK = 0;
        const int E_NOTIMPL = unchecked((int)0x80000001);

        [NativeCallable]
        public static int GetIIDs(
                    IntPtr __IntPtr__pComThis,
                    IntPtr __IntPtr__iidCount,
                    IntPtr __IntPtr__iids)
        {
            // Prevent reentrancy due to message pumping in blocking operations.
            InteropExtensions.SuppressReentrantWaits();
            try
            {
                __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;
                int* iidCount = (int*)__IntPtr__iidCount;
                Guid** iids = (Guid**)__IntPtr__iids;

                if (iidCount == null || iids == null)
                    return Interop.COM.E_POINTER;

                *iidCount = 0;
                *iids = null;

                Object target = pComThis->ComCallableObject.TargetObject;

                System.Collections.Generic.Internal.List<Guid> guidList = target.GetTypeHandle().GetIIDs();

                int totalGuids = guidList.Count;

                if (totalGuids > 0)
                {
                    int guidArraySize = totalGuids * sizeof(Guid);
                    if (guidArraySize < 0)
                        return Interop.COM.E_OUTOFMEMORY;

                    *iids = (Guid*)PInvokeMarshal.CoTaskMemAlloc(new UIntPtr((uint)guidArraySize));

                    if (*iids == null)
                    {
                        return Interop.COM.E_OUTOFMEMORY;
                    }

                    for (int i = 0; i < totalGuids; ++i)
                        (*iids)[i] = guidList[i];

                    *iidCount = totalGuids;

                    return Interop.COM.S_OK;
                }


                return Interop.COM.S_OK;
            }
            finally
            {
                InteropExtensions.RestoreReentrantWaits();
            }
        }

        [NativeCallable]
        public static int GetRuntimeClassName(
                    IntPtr __IntPtr__pComThis,
                    IntPtr __IntPtr__className)
        {
#if  ENABLE_WINRT            
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;
            HSTRING* pClassName = (HSTRING*)__IntPtr__className;

            if (pClassName == null)
            {
                return Interop.COM.E_POINTER;
            }

            Object target = pComThis->ComCallableObject.TargetObject;

            string runtimeClassName = target.GetTypeHandle().GetCCWRuntimeClassName();

            if (!string.IsNullOrEmpty(runtimeClassName))
            {
                if (InteropEventProvider.IsEnabled())
                    InteropEventProvider.Log.TaskCCWQueryRuntimeClassName((long)pComThis->NativeCCW, runtimeClassName);
                return McgMarshal.StringToHStringNoNullCheck(runtimeClassName, pClassName);
            }

            *pClassName = default(HSTRING);

            if (InteropEventProvider.IsEnabled())
                InteropEventProvider.Log.TaskCCWQueryRuntimeClassName((long)pComThis->NativeCCW, String.Empty);

            return Interop.COM.S_OK;
#else
            throw new PlatformNotSupportedException("GetRuntimeClassName");
#endif
        }

        const int BaseTrust = 0;

        [NativeCallable]
        public static int GetTrustLevel(
                        IntPtr __IntPtr__pComThis,
                        IntPtr __IntPtr__pTrustLevel)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;
            int* pTrustLevel = (int*)__IntPtr__pTrustLevel;

            if (pTrustLevel == null)
            {
                return Interop.COM.E_POINTER;
            }

            *pTrustLevel = BaseTrust;

            return S_OK;
        }
    }

    [CLSCompliant(false)]
    public unsafe struct __com_IDispatch
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IDispatch* pVtable;
#pragma warning restore 649
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    [CLSCompliant(false)]
    public unsafe struct __vtable_IDispatch
    {
        // IUnknown
        internal IntPtr pfnQueryInterface;
        internal IntPtr pfnAddRef;
        internal IntPtr pfnRelease;

        // IDispatch
        internal IntPtr pfnGetTypeInfoCount;
        internal IntPtr pfnGetTypeInfo;
        internal IntPtr pfnGetIDsOfNames;
        internal IntPtr pfnInvoke;

        public static IntPtr pNativeVtable;
        private static __vtable_IDispatch s_theCcwVtable = new __vtable_IDispatch
        {
            // IUnknown
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),
            // IDispatch
            pfnGetTypeInfoCount = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(GetTypeInfoCount),
            pfnGetTypeInfo = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetTypeInfo>(GetTypeInfo),
            pfnGetIDsOfNames = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetIDsOfNames>(GetIDsOfNames),
            pfnInvoke = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfInvoke>(Invoke),
        };
        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IDispatch);
        }
        internal static unsafe IntPtr GetCcwvtable_IDispatch()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IDispatch.pNativeVtable, sizeof(__vtable_IDispatch));
                }
            }
            return __vtable_IDispatch.pNativeVtable;
        }

        const int E_NOTIMPL = unchecked((int)0x80000001);

        [NativeCallable]
        public static int GetTypeInfoCount(
            IntPtr pComThis, 
            IntPtr pctinfo)
        {
            return E_NOTIMPL;
        }

        [NativeCallable]
        public static int GetTypeInfo(
            IntPtr pComThis,
            uint iTInfo,
            uint lcid,
            IntPtr ppTInfo)
        {
            return E_NOTIMPL;
        }

        [NativeCallable]
        public static int GetIDsOfNames(
            IntPtr pComThis,
            IntPtr riid,
            IntPtr rgszNames,
            uint cNames,
            uint lcid,
            IntPtr rgDispId)
        {
            return E_NOTIMPL;
        }

        [NativeCallable]
        public static int Invoke(
            IntPtr pComThis,
            int dispIdMember,
            IntPtr riid,
            uint lcid,
            ushort wFlags,
            IntPtr pDispParams,
            IntPtr pVarResult,
            IntPtr pExcepInfo,
            IntPtr puArgErr)
        {
            return E_NOTIMPL;
        }
    }

#if ENABLE_WINRT
    internal unsafe struct __com_ICustomPropertyProvider
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        public __vtable_ICustomPropertyProvider* pVtable;
#pragma warning restore 649
    }

    internal unsafe struct __vtable_ICustomPropertyProvider
    {
        // The ICustomProperty interop implementation is generated by MCG so we need the
        // IID to convert it between a managed object and COM interface
        //30da92c0-23e8-42a0-ae7c-734a0e5d2782
        static Guid ICustomPropertyIID = new Guid(0x30da92c0, 0x23e8, 0x42a0, 0xae, 0x7c, 0x73, 0x4a, 0x0e, 0x5d, 0x27, 0x82);

        // IUnknown
        IntPtr pfnQueryInterface;
        IntPtr pfnAddRef;
        IntPtr pfnRelease;
        // IInspectable
        IntPtr pfnGetIids;
        IntPtr pfnGetRuntimeClassName;
        IntPtr pfnGetTrustLevel;
        // ICustomPropertyProvider
        IntPtr pfnGetCustomProperty;
        IntPtr pfnGetIndexedProperty;
        IntPtr pfnGetStringRepresentation;
        IntPtr pfnget_Type;

        public static IntPtr pNativeVtable;
        static __vtable_ICustomPropertyProvider s_theCcwVtable = new __vtable_ICustomPropertyProvider
        {
            // IUnknown
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),
            // IInspectable
            pfnGetIids = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetIID>(__vtable_IInspectable.GetIIDs),
            pfnGetRuntimeClassName = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IInspectable.GetRuntimeClassName),
            pfnGetTrustLevel = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IInspectable.GetTrustLevel),
            // ICustomPropertyProvider
            pfnGetCustomProperty = AddrOfIntrinsics.AddrOf<WinRTAddrOfIntrinsics.AddrOfGetCustomProperty>(GetCustomProperty__STUB),
            pfnGetIndexedProperty = AddrOfIntrinsics.AddrOf<WinRTAddrOfIntrinsics.AddrOfGetIndexedProperty>(GetIndexedProperty__STUB),
            pfnGetStringRepresentation = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(GetStringRepresentation__STUB),
            pfnget_Type = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(get_Type__STUB),
        };
        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_ICustomPropertyProvider);
        }
        internal static unsafe IntPtr GetCcwvtable_ICustomPropertyProvider()
        {
            if(pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_ICustomPropertyProvider.pNativeVtable,sizeof(__vtable_ICustomPropertyProvider));
                }
            }
            return __vtable_ICustomPropertyProvider.pNativeVtable;
        }

        [NativeCallable]
        static int GetCustomProperty__STUB(
                    System.IntPtr pComThis,
                    HSTRING unsafe_name,
                    IntPtr __IntPtr__unsafe_customProperty)
        {
            void** unsafe_customProperty = (void**)__IntPtr__unsafe_customProperty;
            // Initialize [out] parameters
            *unsafe_customProperty = default(void*);

            object target = ComCallableObject.FromThisPointer(pComThis).TargetObject;
            string propertyName = McgMarshal.HStringToString(unsafe_name);
            try
            {
                global::Windows.UI.Xaml.Data.ICustomProperty property = ManagedGetCustomProperty(target, propertyName);
                    *unsafe_customProperty = (void*)McgComHelpers.ManagedObjectToComInterface(
                        property,
                        typeof(global::Windows.UI.Xaml.Data.ICustomProperty).TypeHandle);
            }
            catch (Exception)
            {
                // Don't fail if property can't be found
            }
            return Interop.COM.S_OK;
        }

        /// <summary>
        /// Safe Implementation of ICustomPropertyProvider.GetCustomProperty
        /// </summary>
        /// <param name="target">Object to find a property on</param>
        /// <param name="propertyName">Name of the property to find</param>
        /// <returns>ICustomProperty representing the property</returns>
        private static global::Windows.UI.Xaml.Data.ICustomProperty ManagedGetCustomProperty(object target, string propertyName)
        {
            try
            {
                target = CustomPropertyImpl.UnwrapTarget(target);

                PropertyInfo propertyInfo = target.GetType().GetProperty(propertyName);

                if (propertyInfo != null)
                    return new CustomPropertyImpl(propertyInfo, supportIndexerWithoutMetadata : false);

                // Weakly-Typed RCW scenario
                // Check cached interface to see whether it supports propertyName Property
                if (McgMarshal.IsOfType(target, typeof(__ComObject).TypeHandle))
                {
                    __ComObject comObj = (__ComObject)target;
                    propertyInfo = comObj.GetMatchingProperty(
                        (PropertyInfo p) => { if (p.Name == propertyName) return true; return false; }
                    );
                    if (propertyInfo != null)
                        return new CustomPropertyImpl(propertyInfo, supportIndexerWithoutMetadata : false);
                }
            }
            catch (MissingMetadataException ex)
            {
                CustomPropertyImpl.LogDataBindingError(propertyName, ex);
            }

            return null;
        }

        [NativeCallable]
        static int GetIndexedProperty__STUB(
                    System.IntPtr pComThis,
                    HSTRING unsafe_name,
                    TypeName unsafe_type,
                    IntPtr __IntPtr__unsafe_customProperty)
        {
            void** unsafe_customProperty = (void**)__IntPtr__unsafe_customProperty;
            
            // Initialize [out] parameters
            *unsafe_customProperty = default(void*);
            try
            {
                object target = ComCallableObject.FromThisPointer(pComThis).TargetObject;
                string propertyName = McgMarshal.HStringToString(unsafe_name);
                Type indexerType = McgMarshal.TypeNameToType(unsafe_type.Name, (int)unsafe_type.Kind);

                global::Windows.UI.Xaml.Data.ICustomProperty property = ManagedGetIndexedProperty(target, propertyName, indexerType);
                *unsafe_customProperty = (void*)McgComHelpers.ManagedObjectToComInterface(
                    property,
                    typeof(global::Windows.UI.Xaml.Data.ICustomProperty).TypeHandle);
            }
            catch (Exception)
            {
                // Don't fail if property can't be found - just return S_OK and NULL property
            }

            return Interop.COM.S_OK;
        }


        /// <summary>
        /// Determine whether specifed property is the matching one based on propertyName and indexerType
        /// </summary>
        /// <param name="property"></param>
        /// <param name="propertyName"></param>
        /// <param name="indexerType"></param>
        /// <returns></returns>
        private static bool IsMatchingIndexedProperty(PropertyInfo property, string propertyName, Type indexerType)
        {
            if (property.Name != propertyName)
            {
                return false;
            }

            ParameterInfo[] indexParameters = property.GetIndexParameters();
            if (indexParameters.Length != 1)
            {
                return false;
            }

            if (indexParameters[0].ParameterType != indexerType)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Safe Implementation of ICustomPropertyProvider.GetIndexedProperty
        /// </summary>
        /// <param name="target">Object to find a property on</param>
        /// <param name="propertyName">Name of the property to find</param>
        /// <param name="indexerType">Type of indexer used on the indexed property to distinguish overloads</param>
        /// <returns>ICustomProperty representing the property</returns>
        private static global::Windows.UI.Xaml.Data.ICustomProperty ManagedGetIndexedProperty(object target, string propertyName, Type indexerType)
        {
            target = CustomPropertyImpl.UnwrapTarget(target);

            // We can do indexing on lists and dictionaries without metadata as a fallback
            bool supportIndexerWithoutMetadata = (target is IList || target is IDictionary);

            try
            {
                BindingFlags Everything = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
                foreach (PropertyInfo property in target.GetType().GetProperties(Everything))
                {
                    if (IsMatchingIndexedProperty(property, propertyName, indexerType))
                    {
                        return new CustomPropertyImpl(property, supportIndexerWithoutMetadata);
                    }
                }

                // Weakly-Typed RCW
                // Check cached interface to see whether it supports propertyName Property
                if (McgMarshal.IsOfType(target, typeof(__ComObject).TypeHandle))
                {
                    __ComObject comObj = (__ComObject)target;
                    PropertyInfo property = comObj.GetMatchingProperty(
                        (PropertyInfo p) => { return IsMatchingIndexedProperty(p, propertyName, indexerType); }
                    );

                    if (property != null)
                    {
                        return new CustomPropertyImpl(property, supportIndexerWithoutMetadata);
                    }
                }
            }
            catch (MissingMetadataException ex)
            {
                CustomPropertyImpl.LogDataBindingError(propertyName, ex);
            }
           
            if (supportIndexerWithoutMetadata)
            {
                return new CustomPropertyImpl(null, supportIndexerWithoutMetadata, target.GetType());
            }
            
            return null;
        }

        [NativeCallable]
        static int GetStringRepresentation__STUB(
                    System.IntPtr pComThis,
                    IntPtr __IntPtr__unsafe_stringRepresentation)
        {
            HSTRING* unsafe_stringRepresentation = (HSTRING*)__IntPtr__unsafe_stringRepresentation;

            try
            {
                // Check whether the ICustomPropertyProvider implements IStringable.
                // If so, we prefer IStringable implementation over object.ToString().
                object target = ComCallableObject.FromThisPointer(pComThis).TargetObject;

                string stringRepresentation;
                if (!IStringableHelper.TryGetIStringableToString(target, out stringRepresentation))
                {
                    stringRepresentation = ManagedGetStringRepresentation(target);
                }

                //
                // To align with desktop behavior: this is the only place we convert null string to
                // NULL HSTRING
                //
                if (stringRepresentation == null)
                    *unsafe_stringRepresentation = default(HSTRING);
                else
                    *unsafe_stringRepresentation = McgMarshal.StringToHString(stringRepresentation);
            }
            catch (Exception ex) when (McgMarshal.PropagateException(ex))
            {
                return McgMarshal.GetHRForExceptionWinRT(ex);
            }

            return Interop.COM.S_OK;
        }


        /// <summary>
        /// Safe Implementation of ICustomPropertyProvider.GetStringRepresentation.
        /// Just calls .ToString()
        /// </summary>
        /// <param name="target">Object to get a string representation for</param>
        /// <returns>String representation of the object</returns>
        private static string ManagedGetStringRepresentation(object target)
        {
            return CustomPropertyImpl.UnwrapTarget(target).ToString();
        }

        [NativeCallable]
        static int get_Type__STUB(
                    System.IntPtr pComThis,
                    IntPtr __IntPtr__unsafe_value)
        {

            TypeName* unsafe_value = (TypeName*)__IntPtr__unsafe_value;
            *unsafe_value = default(TypeName);
            int kind;
            try
            {
                // Since this is only used for databinding, we want to round trip to a McgFakeType if
                // the type is unreflectable. This prevents MissingMetadataExceptions in XAML-generated code.
                Type realType = CustomPropertyImpl.UnwrapTarget(ComCallableObject.FromThisPointer(pComThis).TargetObject).GetType();
                Type xamlSafeType = McgTypeHelpers.GetReflectableOrFakeType(realType);

                McgTypeHelpers.TypeToTypeName(
                    xamlSafeType,
                    out unsafe_value->Name,
                    out kind);

                unsafe_value->Kind = (TypeKind)kind;
            }
            catch (Exception)
            {
                // Don't fail---Align with desktop behavior
            }
            return Interop.COM.S_OK;
        }
    }
#endif //ENABLE_WINRT

    /// <summary>
    /// This is a special type we'll create CCW for but does not implement any WinRT interfaces
    /// We need to ask MCG to generate templates for it explicitly by marking with McgComCallableAttribute
    /// </summary>
    [McgComCallableAttribute]
    internal class StandardCustomPropertyProviderProxy : IManagedWrapper
    {
        Object m_target;

        internal StandardCustomPropertyProviderProxy(object target)
        {
            m_target = target;
        }

        public object GetTarget()
        {
            return m_target;
        }
    }

    internal class EnumerableCustomPropertyProviderProxy : IManagedWrapper, System.Collections.IEnumerable
    {
        System.Collections.IEnumerable m_target;

        internal EnumerableCustomPropertyProviderProxy(System.Collections.IEnumerable target)
        {
            m_target = target;
        }

        public object GetTarget()
        {
            return m_target;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return m_target.GetEnumerator();
        }
    }

    internal class ListCustomPropertyProviderProxy : IManagedWrapper, System.Collections.IList
    {
        System.Collections.IList m_target;

        internal ListCustomPropertyProviderProxy(System.Collections.IList target)
        {
            m_target = target;
        }

        public object GetTarget()
        {
            return m_target;
        }

        int System.Collections.IList.Add(object value)
        {
            return m_target.Add(value);
        }

        void System.Collections.IList.Clear()
        {
            m_target.Clear();
        }

        bool System.Collections.IList.Contains(object value)
        {
            return m_target.Contains(value);
        }

        int System.Collections.IList.IndexOf(object value)
        {
            return m_target.IndexOf(value);
        }

        void System.Collections.IList.Insert(int index, object value)
        {
            m_target.Insert(index, value);
        }

        bool System.Collections.IList.IsFixedSize
        {
            get { return m_target.IsFixedSize; }
        }

        bool System.Collections.IList.IsReadOnly
        {
            get { return m_target.IsReadOnly; }
        }

        void System.Collections.IList.Remove(object value)
        {
            m_target.Remove(value);
        }

        void System.Collections.IList.RemoveAt(int index)
        {
            m_target.RemoveAt(index);
        }

        object System.Collections.IList.this[int index]
        {
            get
            {
                return m_target[index];
            }
            set
            {
                m_target[index] = value;
            }
        }

        void System.Collections.ICollection.CopyTo(Array array, int index)
        {
            m_target.CopyTo(array, index);
        }

        int System.Collections.ICollection.Count
        {
            get { return m_target.Count; }
        }

        bool System.Collections.ICollection.IsSynchronized
        {
            get { return m_target.IsSynchronized; }
        }

        object System.Collections.ICollection.SyncRoot
        {
            get { return m_target.SyncRoot; }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return m_target.GetEnumerator();
        }
    }

#if  ENABLE_WINRT
    /// <summary>
    /// IWeakReferenceSource implementation
    /// Because it doesn't actually implement IWeakReferenceSource, we'll ask MCG to import this as a
    /// CCWTemplate explicitly to avoid global interface lookup.
    /// </summary>
    [McgComCallableAttribute]
    internal class WeakReferenceSource
    {
        // Here we use WeakReference<Object> instead of WeakReference<ComCallableObject>
        // The reason is:
        // .if we use WeakReference<ComCallableObject>, then we will create a relationship: IWeakReference-->Managed Object CCW-->Managed Object,
        // during this relationship, none will keep this Managed Object CCW alive(the link between IWeakReference to Managed Object CCW is WeakReference<T>).
        // So if GC occurs and Managed Object CCW will be GCed, The call IWeakReference.Resolve will fail even managed object is still alive.
        // .if use WeakReference<Object>, then we create a relationship:IWeakReference---> Managed Object, in this way, we don't care
        // whether the managed object CCW is alive or not as long as managed object is alive.
        // But there is a drawback for using WeakReference<Object>: The IUknown pointer of CCW will change.
        // since we will create new CCW if the old one is Gced and the mangaed object still is alive.
        // So if user try to cache this IUnknown pointer of CCW instead of IWeakReference Pointer, he/she may encounter problem during Resolve
        // Workaround for this drawback is to cache IWeakReference instead of IUnknown pointer of CCW if user need to cache.
        private WeakReference<Object> weakRef;

        internal WeakReferenceSource(Object obj)
        {
            weakRef = new WeakReference<Object>(obj);
        }

        internal object Resolve()
        {
            Object targetObject;

            if (weakRef.TryGetTarget(out targetObject))
            {
                return targetObject;
            }

            return null;
        }
    }

    internal unsafe struct __com_IWeakReferenceSource
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IWeakReferenceSource* pVtable;
#pragma warning restore 649
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    internal unsafe struct __vtable_IWeakReferenceSource
    {
        // IUnknown
        IntPtr pfnQueryInterface;
        IntPtr pfnAddRef;
        IntPtr pfnRelease;
        // IWeakReferenceSource
        internal IntPtr pfnGetWeakReference;

        public static IntPtr pNativeVtable;
        private static __vtable_IWeakReferenceSource s_theCcwVtable = new __vtable_IWeakReferenceSource
        {
            // IUnknown
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),
            // IWeakReferenceSource
            pfnGetWeakReference = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(GetWeakReference),
        };

        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IWeakReferenceSource);
        }
        internal static unsafe IntPtr GetCcwvtable_IWeakReferenceSource()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IWeakReferenceSource.pNativeVtable,sizeof(__vtable_IWeakReferenceSource));
                }
            }
            return __vtable_IWeakReferenceSource.pNativeVtable;
        }

        [NativeCallable]
        private static unsafe int GetWeakReference(
                                    IntPtr __IntPtr__pComThis,
                                    IntPtr __IntPtr__ppWeakReference)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;
            __com_IWeakReference** ppWeakReference = (__com_IWeakReference**)__IntPtr__ppWeakReference;

            if (ppWeakReference == null)
            {
                return Interop.COM.E_INVALIDARG;
            }

            var cco = pComThis->ComCallableObject;
            WeakReferenceSource source = new WeakReferenceSource(cco.TargetObject);
            (*ppWeakReference) = (__com_IWeakReference*)McgMarshal.ManagedObjectToComInterface(source, InternalTypes.IWeakReference);

            return Interop.COM.S_OK;
        }
    }

    internal unsafe struct __com_IWeakReference
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IWeakReference* pVtable;
#pragma warning restore 649
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    internal unsafe struct __vtable_IWeakReference
    {
        // IUnknown
        IntPtr pfnQueryInterface;
        IntPtr pfnAddRef;
        IntPtr pfnRelease;
        // IWeakReference
        internal IntPtr pfnResolve;

        public static IntPtr pNativeVtable;
        private static __vtable_IWeakReference s_theCcwVtable = new __vtable_IWeakReference
        {
            // IUnknown
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),
            // IWeakReference
            pfnResolve = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfResolve>(Resolve),
        };
        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IWeakReference);
        }
        internal static unsafe IntPtr GetCcwvtable_IWeakReference()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IWeakReference.pNativeVtable,sizeof(__vtable_IWeakReference));
                }
            }
            return __vtable_IWeakReference.pNativeVtable;
        }

        [NativeCallable]
        private static unsafe int Resolve(
                                    IntPtr __IntPtr__pComThis,
                                    IntPtr __IntPtr__piid,
                                    IntPtr __IntPtr__ppWeakReference)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;
            Guid* piid = (Guid*)__IntPtr__piid;
            __com_IInspectable** ppWeakReference = (__com_IInspectable**)__IntPtr__ppWeakReference;

            if (ppWeakReference == null)
            {
                return Interop.COM.E_INVALIDARG;
            }

            int hr;
            var cco = pComThis->ComCallableObject;
            WeakReferenceSource source = (WeakReferenceSource)cco.TargetObject;
            object targetObject = source.Resolve();

            if (targetObject == null)
            {
                hr = Interop.COM.S_OK;
                *ppWeakReference = null;
            }
            else
            {
                (*ppWeakReference) = (__com_IInspectable*)
                    McgComHelpers.ManagedObjectToComInterface(
                        targetObject,
                        ref *piid
                    );

                hr = (*ppWeakReference) != null ? Interop.COM.S_OK : Interop.COM.E_NOINTERFACE;
            }

            if ((hr < 0) && (InteropEventProvider.IsEnabled()))
                InteropEventProvider.Log.TaskCCWResolveFailure((long)pComThis->NativeCCW, (long)pComThis, *piid, hr);

            return hr;
        }
    }


    unsafe struct __com_IStringable
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IStringable* pVtable;
#pragma warning restore 649
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    internal unsafe struct __vtable_IStringable
    {
        // IUnknown
        IntPtr pfnQueryInterface;
        IntPtr pfnAddRef;
        IntPtr pfnRelease;

        // IInspectable
        IntPtr pfnGetIids;
        IntPtr pfnGetRuntimeClassName;
        IntPtr pfnGetTrustLevel;

        // IStringable
        internal IntPtr pfnToString;

        public static IntPtr pNativeVtable;
        private static __vtable_IStringable s_theCcwVtable = new __vtable_IStringable
        {
            // IUnknown
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),

            // IInspectable
            pfnGetIids = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetIID>(__vtable_IInspectable.GetIIDs),
            pfnGetRuntimeClassName = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IInspectable.GetRuntimeClassName),
            pfnGetTrustLevel = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IInspectable.GetTrustLevel),

            // IStringable
            pfnToString = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(ToString),
        };

        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IStringable);
        }
        internal static unsafe IntPtr GetCcwvtable_IStringable()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IStringable.pNativeVtable,sizeof(__vtable_IStringable));
                }
            }
            return __vtable_IStringable.pNativeVtable;
        }

        [NativeCallable]
        private static unsafe int ToString(
                                    IntPtr pComThis,
                                    IntPtr __IntPtr__pResult)
        {
            // If we have reached here then the managed object does not directly implement
            // IStringable and hence we need to rely on the default Object.ToString() behavior.

            // Since the default implementation is only a best effort scenario where we may or may
            // not have the metadata to give the Type.FullName, this method call might not be very useful
            HSTRING* pResult = (HSTRING*)__IntPtr__pResult;
            int hr;

            if (pResult == null)
                return Interop.COM.E_POINTER;

            object target = ComCallableObject.FromThisPointer(pComThis).TargetObject;
            Debug.Assert(target != null);

            try
            {
                string pResult_String = target.ToString();

                if (pResult_String == null)
                {
                    *pResult = default(HSTRING);
                    hr = Interop.COM.S_OK;
                }
                else
                {
                    hr = McgMarshal.StringToHStringNoNullCheck(pResult_String, pResult);
                }

                return hr;
            }
            catch (Exception ex) when (McgMarshal.PropagateException(ex))
            {
                return McgMarshal.GetHRForExceptionWinRT(ex);
            }
        }
    }
#endif
    /// <summary>
    /// Implementation of ICCW for Jupiter Lifetime Feature
    /// Currently this implementation doesn't do anything - it only delegates the Jupiter AddRef/Release
    /// calls to real COM ref. This is done to avoid CCW gets destroyed when Jupiter does the following:
    /// 1. COM Add Ref
    /// 2. Jupiter Add Ref
    /// 3. COM release
    /// </summary>
    internal unsafe struct __vtable_ICCW
    {
        // IUnknown
        IntPtr pfnQueryInterface;
        IntPtr pfnAddRef;
        IntPtr pfnRelease;

        // ICCW
        IntPtr pfnAddRefFromJupiter;
        IntPtr pfnReleaseFromJupiter;
        IntPtr pfnPeg;
        IntPtr pfnUnpeg;

        public static IntPtr pNativeVtable;
        static __vtable_ICCW s_theCcwVtable = new __vtable_ICCW
        {
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(QueryInterface__STUB),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(AddRef__STUB),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(Release__STUB),
            pfnAddRefFromJupiter = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget1>(AddRefFromJupiter__STUB),
            pfnReleaseFromJupiter = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget1>(ReleaseFromJupiter__STUB),
            pfnPeg = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget1>(Peg__STUB),
            pfnUnpeg = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget1>(Unpeg__STUB),
        };
        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_ICCW);
        }
        internal static unsafe IntPtr GetCcwvtable_ICCW()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_ICCW.pNativeVtable,sizeof(__vtable_ICCW));
                }
            }
            return __vtable_ICCW.pNativeVtable;
        }

        /// <remarks>
        /// This is safe to mark as [NativeCallable] because Jupiter should never call it inside GC callout
        /// in the first place
        /// <remarks>
        [NativeCallable]
        static int QueryInterface__STUB(
                    IntPtr __IntPtr__pComThis,
                    IntPtr __IntPtr__pIID,
                    IntPtr __IntPtr__ppvObject)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;
            Guid* pIID = (Guid*)__IntPtr__pIID;
            void** ppvObject = (void**)__IntPtr__ppvObject;

            //
            // Check for neutered CCWs
            // This is the only QI that can be called on a neutered CCW and we fail with a well defined
            // COR_E_ACCESSING_CCW error code back to Jupiter
            // NOTE: We should not use IsNeutered check here, instead, we have to keep a reference to CCW
            // from here to avoid CCW getting neutered after the check.
            //
            var cco = pComThis->NativeCCW->ComCallableObjectUnsafe;

            if (cco == null)
                return Interop.COM.COR_E_ACCESSING_CCW;

            // @TODO: does not distinguish between E_NOINTERFACE and E_OUTOFMEMORY
            IntPtr result = cco.GetComInterfaceForIID(ref *pIID);
            *ppvObject = (void*)result;

            if (result == default(IntPtr))
                return Interop.COM.E_NOINTERFACE;

            return Interop.COM.S_OK;
        }

        [NativeCallable]
        static int AddRef__STUB(IntPtr __IntPtr__pComThis)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;

            //
            // Use ->NativeCCW to support accessing neutered CCWs
            //
            return pComThis->NativeCCW->AddCOMRef();
        }

        [NativeCallable]
        static int Release__STUB(IntPtr __IntPtr__pComThis)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;

            //
            // Use ->NativeCCW to support accessing neutered CCWs
            //
            return pComThis->NativeCCW->ReleaseCOMRef();
        }

        [NativeCallable]
        static int AddRefFromJupiter__STUB(System.IntPtr __IntPtr__pComThis)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;

            //
            // Use ->NativeCCW to support accessing neutered CCWs
            //
            return pComThis->NativeCCW->AddJupiterRef();
        }

        [NativeCallable]
        static int ReleaseFromJupiter__STUB(System.IntPtr __IntPtr__pComThis)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;

            //
            // Use ->NativeCCW to support accessing neutered CCWs
            //
            return pComThis->NativeCCW->ReleaseJupiterRef();
        }

        [NativeCallable]
        static int Peg__STUB(System.IntPtr __IntPtr__pComThis)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;

            //
            // Use ->NativeCCW to support accessing neutered CCWs
            //
            pComThis->NativeCCW->IsPegged = true;

            return 0;
        }

        [NativeCallable]
        static int Unpeg__STUB(System.IntPtr __IntPtr__pComThis)
        {
            __interface_ccw* pComThis = (__interface_ccw*)__IntPtr__pComThis;

            //
            // Use ->NativeCCW to support accessing neutered CCWs
            //
            pComThis->NativeCCW->IsPegged = false;

            return 0;
        }
    }

#if ENABLE_MIN_WINRT
    unsafe struct __com_IActivationFactoryInternal
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IActivationFactoryInternal* pVtable;
#pragma warning restore 649
    }

    internal unsafe struct __vtable_IActivationFactoryInternal
    {
        // IUnknown
        IntPtr pfnQueryInterface;
        IntPtr pfnAddRef;
        IntPtr pfnRelease;
        // IInspectable
        IntPtr pfnGetIids;
        IntPtr pfnGetRuntimeClassName;
        IntPtr pfnGetTrustLevel;
        // IActivationFactoryInternal
        internal IntPtr pfnActivateInstance;

        static IntPtr pNativeVtable;
        private static __vtable_IActivationFactoryInternal s_theCcwVtable = new __vtable_IActivationFactoryInternal
        {
            // IUnknown
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),
            // IInspectable
            pfnGetIids = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetIID>(__vtable_IInspectable.GetIIDs),
            pfnGetRuntimeClassName = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IInspectable.GetRuntimeClassName),
            pfnGetTrustLevel = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IInspectable.GetTrustLevel),
            // IActivationFactoryInternal
            pfnActivateInstance = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(ActivateInstance)
        };
        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IActivationFactoryInternal);
        }
        internal static unsafe IntPtr GetCcwvtable_IActivationFactoryInternal()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IActivationFactoryInternal.pNativeVtable,sizeof(__vtable_IActivationFactoryInternal));
                    return __vtable_IActivationFactoryInternal.pNativeVtable;
                }
            }
            return __vtable_IActivationFactoryInternal.pNativeVtable;
        }

        [NativeCallable]
        internal static int ActivateInstance(
            IntPtr pComThis,
            IntPtr ppResult)
        {
            IntPtr* pResult = (IntPtr*)ppResult;
            if (pResult == null)
                return Interop.COM.E_POINTER;

            try
            {
                object target = ComCallableObject.FromThisPointer(pComThis).TargetObject;
                *pResult = (IntPtr)((IActivationFactoryInternal)target).ActivateInstance();
                return Interop.COM.S_OK;
            }
            catch (System.Exception hrExcep) when (McgMarshal.PropagateException(hrExcep))
            {
                *pResult = default(IntPtr);
                return McgMarshal.GetHRForExceptionWinRT(hrExcep);
            }
        }
    }
#endif // ENABLE_MIN_WINRT


    unsafe struct __com_IManagedActivationFactory
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IManagedActivationFactory* pVtable;
#pragma warning restore 649
    }

    internal unsafe struct __vtable_IManagedActivationFactory
    {
        // IUnknown
        IntPtr pfnQueryInterface;
        IntPtr pfnAddRef;
        IntPtr pfnRelease;
        // IInspectable
        IntPtr pfnGetIids;
        IntPtr pfnGetRuntimeClassName;
        IntPtr pfnGetTrustLevel;
        // IManagedActivationFactory
        IntPtr pfnRunClassConstructor;

        public static IntPtr pNativeVtable;
        private static __vtable_IManagedActivationFactory s_theCcwVtable = new __vtable_IManagedActivationFactory
        {
            // IUnknown
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),
            // IInspectable
            pfnGetIids = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetIID>(__vtable_IInspectable.GetIIDs),
            pfnGetRuntimeClassName = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IInspectable.GetRuntimeClassName),
            pfnGetTrustLevel = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IInspectable.GetTrustLevel),
            // IManagedActivationFactory
            pfnRunClassConstructor = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget1>(RunClassConstructor),
        };
        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IManagedActivationFactory);
        }
        internal static unsafe IntPtr GetCcwvtable_IManagedActivationFactory()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IManagedActivationFactory.pNativeVtable,sizeof(__vtable_IManagedActivationFactory));

                }
            }
            return __vtable_IManagedActivationFactory.pNativeVtable;
        }

        [NativeCallable]
        static int RunClassConstructor(
            IntPtr pComThis)
        {
            try
            {
                ((IManagedActivationFactory)ComCallableObject.GetTarget(pComThis)).RunClassConstructor();
                return Interop.COM.S_OK;
            }
            catch (System.Exception hrExcep) when (McgMarshal.PropagateException(hrExcep))
            {
                return McgMarshal.GetHRForExceptionWinRT(hrExcep);
            }
        }
    }

    unsafe struct __com_IMarshal
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IMarshal* pVtable;
#pragma warning restore 649
    }

    /// <summary>
    /// IMarshal implementation that delegates to FTM
    /// </summary>
    internal unsafe struct __vtable_IMarshal
    {
        // IUnknown
        IntPtr pfnQueryInterface;
        IntPtr pfnAddRef;
        IntPtr pfnRelease;
        // IMarshal
        IntPtr pfnGetUnmarshalClass;
        IntPtr pfnGetMarshalSizeMax;
        IntPtr pfnMarshalInterface;
        IntPtr pfnUnmarshalInterface;
        IntPtr pfnReleaseMarshalData;
        IntPtr pfnDisconnectObject;

        public static IntPtr pNativeVtable;
        private static __vtable_IMarshal s_theCcwVtable = new __vtable_IMarshal
        {
            // IUnknown
            pfnQueryInterface       = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(__vtable_IUnknown.QueryInterface),
            pfnAddRef               = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(__vtable_IUnknown.AddRef),
            pfnRelease              = AddrOfIntrinsics.AddrOf<AddrOfRelease>(__vtable_IUnknown.Release),
            // IMarshal
            pfnGetUnmarshalClass    = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetMarshalUnMarshal>(__vtable_IMarshal.GetUnmarshalClass),
            pfnGetMarshalSizeMax    = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfGetMarshalUnMarshal>(__vtable_IMarshal.GetMarshalSizeMax),
            pfnMarshalInterface     = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfMarshalInterface>(__vtable_IMarshal.MarshalInterface),
            pfnUnmarshalInterface   = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget5>(__vtable_IMarshal.UnmarshalInterface),
            pfnReleaseMarshalData   = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget3>(__vtable_IMarshal.ReleaseMarshalData),
            pfnDisconnectObject     = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget2>(__vtable_IMarshal.DisconnectObject)
        };

        internal static IntPtr GetVtableFuncPtr()
        {
            return AddrOfIntrinsics.AddrOf<AddrOfGetCCWVtable>(GetCcwvtable_IMarshal);
        }

        internal static unsafe IntPtr GetCcwvtable_IMarshal()
        {
            if (pNativeVtable == default(IntPtr))
            {
                fixed (void* pVtbl = &s_theCcwVtable)
                {
                    McgMarshal.GetCCWVTableCopy(pVtbl, ref __vtable_IMarshal.pNativeVtable,sizeof(__vtable_IMarshal));
                }
            }
            return __vtable_IMarshal.pNativeVtable;
        }

        static unsafe int GetIMarshal(void **ppIMarshal)
        {
#if ENABLE_MIN_WINRT
            void *pUnk = null;
            int hr = ExternalInterop.CoCreateFreeThreadedMarshaler(null, (void **)&pUnk);
            if (hr < 0) return hr;

            *ppIMarshal = (void *)McgMarshal.ComQueryInterfaceNoThrow(
                new IntPtr(pUnk), ref Interop.COM.IID_IMarshal, out hr);

            McgMarshal.ComSafeRelease(new IntPtr(pUnk));

            return hr;
#else
            throw new PlatformNotSupportedException("GetIMarshal");
#endif
        }

        [NativeCallable]
        static int GetUnmarshalClass(
            IntPtr pComThis,
            IntPtr piid,
            IntPtr pv,
            int dwDestContext,
            IntPtr pvDestContext,
            int mshlflags,
            IntPtr pclsid
            )
        {
            __vtable_IMarshal **pIMarshal = null;
            try
            {
                int hr = GetIMarshal((void **)&pIMarshal);
                if (hr < 0) return hr;

                return CalliIntrinsics.StdCall__int(
                    (*pIMarshal)->pfnGetUnmarshalClass,
                    pIMarshal,
                    piid,
                    pv,
                    dwDestContext,
                    pvDestContext,
                    mshlflags,
                    pclsid);
            }
            finally
            {
                McgMarshal.ComSafeRelease(new IntPtr(pIMarshal));
            }
        }

        [NativeCallable]
        static int GetMarshalSizeMax(
            IntPtr pComThis,
            IntPtr piid,
            IntPtr pv,
            int dwDestContext,
            IntPtr pvDestContext,
            int mshlflags,
            IntPtr pSize
            )
        {
            __vtable_IMarshal **pIMarshal = null;
            try
            {
                int hr = GetIMarshal((void**)&pIMarshal); ;
                if (hr < 0) return hr;

                return CalliIntrinsics.StdCall__int(
                    (*pIMarshal)->pfnGetMarshalSizeMax,
                    pIMarshal,
                    piid,
                    pv,
                    dwDestContext,
                    pvDestContext,
                    mshlflags,
                    pSize);
            }
            finally
            {
                McgMarshal.ComSafeRelease(new IntPtr(pIMarshal));
            }
        }

        [NativeCallable]
        static int MarshalInterface(
            IntPtr pComThis,
            IntPtr pStm,
            IntPtr piid,
            IntPtr pv,
            int dwDestContext,
            IntPtr pvDestContext,
            int mshlflags
            )
        {
            __vtable_IMarshal **pIMarshal = null;
            try
            {
                int hr = GetIMarshal((void**)&pIMarshal);
                if (hr < 0) return hr;

                return CalliIntrinsics.StdCall__int(
                    (*pIMarshal)->pfnMarshalInterface,
                    pIMarshal,
                    pStm,
                    piid,
                    pv,
                    dwDestContext,
                    pvDestContext,
                    mshlflags);
            }
            finally
            {
                McgMarshal.ComSafeRelease(new IntPtr(pIMarshal));
            }
        }

        [NativeCallable]
        static int UnmarshalInterface(
            IntPtr pComThis,
            IntPtr pStm,
            IntPtr piid,
            IntPtr ppvObj
            )
        {
            __vtable_IMarshal **pIMarshal = null;
            try
            {
                int hr = GetIMarshal((void**)&pIMarshal);
                if (hr < 0) return hr;

                return CalliIntrinsics.StdCall__int(
                    (*pIMarshal)->pfnUnmarshalInterface,
                    pIMarshal,
                    pStm,
                    piid,
                    ppvObj);
            }
            finally
            {
                McgMarshal.ComSafeRelease(new IntPtr(pIMarshal));
            }
        }

        [NativeCallable]
        static int ReleaseMarshalData(
            IntPtr pComThis,
            IntPtr pStm
            )
        {
            __vtable_IMarshal **pIMarshal = null;
            try
            {
                int hr = GetIMarshal((void**)&pIMarshal);
                if (hr < 0) return hr;

                return CalliIntrinsics.StdCall__int(
                    (*pIMarshal)->pfnReleaseMarshalData,
                    pIMarshal,
                    pStm);
            }
            finally
            {
                McgMarshal.ComSafeRelease(new IntPtr(pIMarshal));
            }
        }

        [NativeCallable]
        static int DisconnectObject(
            IntPtr pComThis,
            int dwReserved
            )
        {
            __vtable_IMarshal **pIMarshal = null;
            try
            {
                int hr = GetIMarshal((void**)&pIMarshal);
                if (hr < 0) return hr;

                return CalliIntrinsics.StdCall__int(
                    (*pIMarshal)->pfnDisconnectObject,
                    pIMarshal,
                    dwReserved);
            }
            finally
            {
                McgMarshal.ComSafeRelease(new IntPtr(pIMarshal));
            }
        }
    }

    /// <summary>
    /// Windows.UI.Xaml.Hosting.IReferenceTrackerTarget
    /// Jupiter use this interface to coordinate with us
    /// </summary>
    internal unsafe struct __com_ICCW
    {
#pragma warning disable 0649
        internal __vtable_ICCW* pVtable;
#pragma warning restore 0649
    }


    unsafe struct __com_IStream
    {
#pragma warning disable 649 // Field 'blah' is never assigned to, and will always have its default value null
        internal __vtable_IStream* pVtable;
        public byte* m_pMem;            // Memory for the read.
        public int m_cbSize;            // Size of the memory.
#pragma warning restore 649

        public int m_cbCurrent;         // Current offset.
        public int m_cRef;              // Ref count.

        /// <summary>
        /// Create a predefined size of IStream*
        /// </summary>
        /// <remarks>
        /// NOTE: The reason to create own IStream is that the API SHCreateMemStream doesn't belong to UWP API Sets
        /// </remarks>
        /// <param name="lSize">Requested size</param>
        /// <returns>The IStream*</returns>
        internal static unsafe IntPtr CreateMemStm(ulong lSize)
        {
#if ENABLE_MIN_WINRT
            __com_IStream* pIStream = (__com_IStream*)PInvokeMarshal.CoTaskMemAlloc(new UIntPtr((uint)sizeof(__com_IStream)));
            pIStream->pVtable = (__vtable_IStream*)__vtable_IStream.GetVtable();
            pIStream->m_cbCurrent = 0;
            pIStream->m_cRef = 1;
            pIStream->m_cbSize = (int)lSize;
            pIStream->m_pMem = (byte*)PInvokeMarshal.CoTaskMemAlloc(new UIntPtr((uint)lSize));

            return new IntPtr(pIStream);
#else
            throw new PlatformNotSupportedException("CreateMemStm");
#endif
        }

        internal static unsafe void DestroyMemStm(__com_IStream* pIStream)
        {
            // Release memory allocated by CreateMemStm
            if (pIStream->m_pMem != null)
                PInvokeMarshal.CoTaskMemFree(new IntPtr(pIStream->m_pMem));

            PInvokeMarshal.CoTaskMemFree(new IntPtr(pIStream));
        }
    }

    /// <summary>
    /// Native Value for STATSTG
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    struct STATSTG_UnsafeType
    {
        public IntPtr pwcsName;
        public int type;
        public long cbSize;
        public ComTypes.FILETIME mtime;
        public ComTypes.FILETIME ctime;
        public ComTypes.FILETIME atime;
        public int grfMode;
        public int grfLocksSupported;
        public Guid clsid;
        public int grfStateBits;
        public int reserved;
    }

    unsafe internal struct __vtable_IStream
    {
        // IUnknown
        internal IntPtr pfnQueryInterface;
        internal IntPtr pfnAddRef;
        internal IntPtr pfnRelease;

        // ISequentialStream
        internal IntPtr pfnRead;
        internal IntPtr pfnWrite;

        // IStream 
        internal IntPtr pfnSeek;
        internal IntPtr pfnSetSize;
        internal IntPtr pfnCopyTo;
        internal IntPtr pfnCommit;
        internal IntPtr pfnRevert;
        internal IntPtr pfnLockRegion;
        internal IntPtr pfnUnlockRegion;
        internal IntPtr pfnStat;
        internal IntPtr pfnClone;

        public static IntPtr pNativeVtable;

        private static __vtable_IStream s_theCcwVtable = new __vtable_IStream
        {
            pfnQueryInterface = AddrOfIntrinsics.AddrOf<AddrOfQueryInterface>(QueryInterface),
            pfnAddRef = AddrOfIntrinsics.AddrOf<AddrOfAddRef>(AddRef),
            pfnRelease = AddrOfIntrinsics.AddrOf<AddrOfRelease>(Release),

            pfnRead = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamRead>(Read),
            pfnWrite = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamWrite>(Write),

            pfnSeek = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamSeek>(Seek),
            pfnSetSize = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamSetSize>(SetSize),
            pfnCopyTo = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamCopyTo>(CopyTo),
            pfnCommit = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget2>(Commit),
            pfnRevert = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfTarget1>(Revert),
            pfnLockRegion = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamLockRegion>(LockRegion),
            pfnUnlockRegion = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamUnlockRegion>(UnlockRegion),
            pfnStat = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamStat>(Stat),
            pfnClone = AddrOfIntrinsics.AddrOf<AddrOfIntrinsics.AddrOfIStreamClone>(Clone),
        };

        internal static IntPtr GetVtable()
        {
            fixed (void* pVtable = &s_theCcwVtable)
            {
                McgMarshal.GetCCWVTableCopy(pVtable, ref __vtable_IStream.pNativeVtable, sizeof(__vtable_IStream));
            }

            return __vtable_IStream.pNativeVtable;
        }

        [NativeCallable]
        internal static int QueryInterface(
            IntPtr __IntPtr__pComThis,
            IntPtr __IntPtr__pIID,
            IntPtr __IntPtr__ppvObject)
        {
            __com_IStream* pIStream = (__com_IStream*)__IntPtr__pComThis;
            void** ppvObject = (void**)__IntPtr__ppvObject;
            Guid* pIID = (Guid*)__IntPtr__pIID;

            if (ppvObject == null)
                return Interop.COM.E_POINTER;

            if (pIID->Equals(Interop.COM.IID_IUnknown) ||
                pIID->Equals(Interop.COM.IID_IStream) ||
                pIID->Equals(Interop.COM.IID_ISequentialStream))
            {
                CalliIntrinsics.StdCall__int(pIStream->pVtable->pfnAddRef, __IntPtr__pComThis);
                *ppvObject = pIStream;
                return Interop.COM.S_OK;
            }
            return Interop.COM.E_NOINTERFACE;
        }

        [NativeCallable]
        internal static int AddRef(System.IntPtr pComThis)
        {
            // This never gets released from native
            __com_IStream* pIStream = (__com_IStream*)pComThis;
            int cRef = System.Threading.Interlocked.Increment(ref pIStream->m_cRef);
            return cRef;
        }

        [NativeCallable]
        internal static int Release(IntPtr pComThis)
        {
            __com_IStream* pIStream = (__com_IStream*)pComThis;
            int cRef = System.Threading.Interlocked.Decrement(ref pIStream->m_cRef);
            if (cRef == 0)
            {
                __com_IStream.DestroyMemStm(pIStream);
            }
            return cRef;
        }

        [NativeCallable]
        internal static int CopyTo(System.IntPtr pComThis, IntPtr pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
            // We don't handle pcbRead or pcbWritten.
            System.Diagnostics.Debug.Assert(pcbRead == IntPtr.Zero);
            System.Diagnostics.Debug.Assert(pcbWritten == IntPtr.Zero);
            System.Diagnostics.Debug.Assert(cb >= 0);

            try
            {
                __com_IStream* pIStream = (__com_IStream*)pComThis;
                __com_IStream* pToIStream = (__com_IStream*)pstm;
                long cbTotal = Math.Min(cb, pIStream->m_cbSize - pIStream->m_cbCurrent);
                long cbRead = Math.Min(1024, cbTotal);
                byte[] buffer = new byte[cbRead];

                while (cbTotal > 0)
                {
                    if (cbRead > cbTotal)
                        cbRead = cbTotal;
                    fixed (byte* pBuf = buffer)
                    {
                        int hr;

                        hr = CalliIntrinsics.StdCall__int(pIStream->pVtable->pfnRead, pComThis, pBuf, (int)cbRead, IntPtr.Zero);
                        if (hr < 0)
                            return hr;
                        hr = CalliIntrinsics.StdCall__int(pToIStream->pVtable->pfnWrite, pstm, pBuf, (int)cbRead, IntPtr.Zero);
                        if (hr < 0)
                            return hr;
                    }
                    cbTotal -= cbRead;
                }

                // Adjust seek pointer to the end.
                pIStream->m_cbCurrent = pIStream->m_cbSize;
                return Interop.COM.S_OK;
            }
            catch (System.OutOfMemoryException ex)
            {
                return ex.HResult;
            }
        }

        [NativeCallable]
        internal static unsafe int Read(System.IntPtr pComThis, IntPtr pv, int cb, IntPtr pcb)
        {
            __com_IStream* pIStream = (__com_IStream*)pComThis;
            byte* pByte = (byte*)pv;
            int* pcbRead = (int*)pcb;

            int cbRead = Math.Min(cb, pIStream->m_cbSize - pIStream->m_cbCurrent);
            if (cbRead <= 0)
                return Interop.COM.S_FALSE;

            Buffer.MemoryCopy(
                &pIStream->m_pMem[pIStream->m_cbCurrent],
                (void*)pv,
                cb,
                cb);
            if (pcbRead != null)
                *pcbRead  = cbRead;
            pIStream->m_cbCurrent += cbRead;

            return Interop.COM.S_OK;
        }

        [NativeCallable]
        internal static unsafe int Write(System.IntPtr pComThis, IntPtr pv, int cb, IntPtr pcbWritten)
        {
            __com_IStream* pIStream = (__com_IStream*)pComThis;
            byte* pByte = (byte*)pv;
            int* cbWritten = (int*)pcbWritten;
            if (cb + pIStream->m_cbCurrent > pIStream->m_cbSize)
                return Interop.COM.E_OUTOFMEMORY;

            Buffer.MemoryCopy(
                (void*)pv,
                &pIStream->m_pMem[pIStream->m_cbCurrent],
                cb,
                cb);
            pIStream->m_cbCurrent += cb;
            if (cbWritten != null)
                *cbWritten = cb;

            return Interop.COM.S_OK;
        }

        [NativeCallable]
        internal static unsafe int Seek(System.IntPtr pComThis, long dlibMove, int dwOrigin, IntPtr plib)
        {
            __com_IStream* pIStream = (__com_IStream*)pComThis;
            long* plibNewPosition = (long*)plib;
            Debug.Assert(dwOrigin == (int)Interop.COM.STREAM_SEEK.STREAM_SEEK_SET ||
                dwOrigin == (int)Interop.COM.STREAM_SEEK.STREAM_SEEK_CUR);
            Debug.Assert(dlibMove >= 0);

            if (dwOrigin == (int)Interop.COM.STREAM_SEEK.STREAM_SEEK_SET)
            {
                pIStream->m_cbCurrent = (int)dlibMove;
            }
            else if (dwOrigin == (int)Interop.COM.STREAM_SEEK.STREAM_SEEK_CUR)
            {
                pIStream->m_cbCurrent += (int)dlibMove;
            }

            if (plibNewPosition != null)
            {
                *plibNewPosition = pIStream->m_cbCurrent;
            }

            if (pIStream->m_cbCurrent > pIStream->m_cbSize)
                return Interop.COM.E_FAIL;

            return Interop.COM.S_OK;
        }

        const int STG_E_INVALIDPOINTER = unchecked((int)0x80030009);

        [NativeCallable]
        internal static unsafe int Stat(System.IntPtr pComThis, IntPtr pstatstg, int grfStatFlag)
        {
            __com_IStream* pIStream = (__com_IStream*)pComThis;
            STATSTG_UnsafeType* pUnsafeStatstg = (STATSTG_UnsafeType*)pstatstg;
            if (pUnsafeStatstg == null)
                return STG_E_INVALIDPOINTER;
            pUnsafeStatstg->pwcsName = IntPtr.Zero;
            pUnsafeStatstg->type = 2; // STGTY_STREAM
            pUnsafeStatstg->cbSize = pIStream->m_cbSize;
            return Interop.COM.S_OK;
        }

        #region Rest of IStream overrides that are not implemented
        [NativeCallable]
        internal static int Clone(System.IntPtr pComThis, IntPtr ppstm)
        {
            ppstm = default(IntPtr);
            return Interop.COM.E_NOTIMPL;
        }

        [NativeCallable]
        internal static int Commit(System.IntPtr pComThis, int grfCommitFlags)
        {
            return Interop.COM.E_NOTIMPL;
        }

        [NativeCallable]
        internal static int LockRegion(System.IntPtr pComThis, long libOffset, long cb, int dwLockType)
        {
            return Interop.COM.E_NOTIMPL;
        }

        [NativeCallable]
        internal static int Revert(System.IntPtr pComThis)
        {
            return Interop.COM.E_NOTIMPL;
        }

        [NativeCallable]
        internal static int SetSize(System.IntPtr pComThis, long libNewSize)
        {
            return Interop.COM.E_NOTIMPL;
        }

        [NativeCallable]
        internal static int UnlockRegion(System.IntPtr pComThis, long libOffset, long cb, int dwLockType)
        {
            return Interop.COM.E_NOTIMPL;
        }
        #endregion
    }

    /// The main puropse of this type is making McgIR happy
    /// The problem to solve is to generate "IUriRuntimeClass" McgData into shared assembly in production build.
    /// In Mcg, there is a place to determine which CompilationUnit(assembly) for a type should go to.
    /// Related Code: MCG.CompilationUnitCollection[ReportInteropTypeAndComputeDestination(typeDef)].IRCollection.Add(type);
    /// Solution 1: Use Windows.Foundation.IUriRuntimeClass type as typedef
    /// By default, the destination for a Windows.* WinRT type will be App CompilationUnit unless Windows.* WinRT type is shared type.
    /// Since marking Windows.Foundation.IUriRuntimeClass as shared type, it will introduce Windows.Foundation.WwwFormUrlDecoder as shared type and
    /// in reality, we don't care Windows.Foundation.WwwFormUrlDecoder.
    /// 
    /// [Current]Solution2: Use a WellKnown type(System.Runtime.InteropServices.IUriRuntimeClass) as typedef
    /// By default, the destination for a type in system.private.interop will be shared CompilationUnit.
    [Guid("9e365e57-48b2-4160-956f-c7385120bbfc")]
    public interface IUriRuntimeClass
    {
    }

    // The main puropse of this type is making McgIR happy during TypeImporter.ImporWinRTUri
    /// The problem to solve is to generate "IUriRuntimeClassFactory" McgData into shared assembly in production build
    /// In Mcg, there is a place to determine which CompilationUnit(assembly) for a type should go to.
    /// Related Code: MCG.CompilationUnitCollection[ReportInteropTypeAndComputeDestination(typeDef)].IRCollection.Add(type);
    /// Solution 1: Use Windows.Foundation.IUriRuntimeClassFactory type as typedef
    /// By default, the destination for a Windows.* WinRT type will be App CompilationUnit unless Windows.* WinRT type is shared type.
    /// Since marking Windows.Foundation.IUriRuntimeClassFactory as shared type, it will introduce Windows.Foundation.Uri as shared type 
    /// 
    /// [Current]Solution2: Use a WellKnown type(System.Runtime.InteropServices.IUriRuntimeClassFactory) as typedef
    /// By default, the destination for a type in system.private.interop will be shared CompilationUnit.
    [Guid("44a9796f-723e-4fdf-a218-033e75b0c084")]
    public interface IUriRuntimeClassFactory
    {
    }
}