Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 3321 lines (2770 sloc) 97.7 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321
/*
* cominterop.c: COM Interop Support
*
*
* (C) 2002 Ximian, Inc. http://www.ximian.com
*
*/

#include "config.h"
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif

#include "object.h"
#include "loader.h"
#include "cil-coff.h"
#include "metadata/abi-details.h"
#include "metadata/cominterop.h"
#include "metadata/marshal.h"
#include "metadata/method-builder.h"
#include "metadata/tabledefs.h"
#include "metadata/exception.h"
#include "metadata/appdomain.h"
#include "metadata/reflection-internals.h"
#include "mono/metadata/debug-helpers.h"
#include "mono/metadata/threadpool.h"
#include "mono/metadata/threads.h"
#include "mono/metadata/monitor.h"
#include "mono/metadata/metadata-internals.h"
#include "mono/metadata/domain-internals.h"
#include "mono/metadata/gc-internal.h"
#include "mono/metadata/threads-types.h"
#include "mono/metadata/string-icalls.h"
#include "mono/metadata/attrdefs.h"
#include "mono/metadata/gc-internal.h"
#include "mono/utils/mono-counters.h"
#include "mono/utils/strenc.h"
#include "mono/utils/atomic.h"
#include <string.h>
#include <errno.h>

/*
Code shared between the DISABLE_COM and !DISABLE_COM
*/
static void
register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
{
MonoMethodSignature *sig = mono_create_icall_signature (sigstr);

mono_register_jit_icall (func, name, sig, save);
}

#ifndef DISABLE_COM

#define OPDEF(a,b,c,d,e,f,g,h,i,j) \
a = i,

typedef enum {
MONO_MARSHAL_NONE, /* No marshalling needed */
MONO_MARSHAL_COPY, /* Can be copied by value to the new domain */
MONO_MARSHAL_COPY_OUT, /* out parameter that needs to be copied back to the original instance */
MONO_MARSHAL_SERIALIZE /* Value needs to be serialized into the new domain */
} MonoXDomainMarshalType;

typedef enum {
MONO_COM_DEFAULT,
MONO_COM_MS
} MonoCOMProvider;

static MonoCOMProvider com_provider = MONO_COM_DEFAULT;

enum {
#include "mono/cil/opcode.def"
LAST = 0xff
};
#undef OPDEF

/* This mutex protects the various cominterop related caches in MonoImage */
#define mono_cominterop_lock() EnterCriticalSection (&cominterop_mutex)
#define mono_cominterop_unlock() LeaveCriticalSection (&cominterop_mutex)
static CRITICAL_SECTION cominterop_mutex;

/* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
#ifdef HOST_WIN32
#define STDCALL __stdcall
#else
#define STDCALL
#endif

GENERATE_GET_CLASS_WITH_CACHE (interop_proxy, Mono.Interop, ComInteropProxy)
GENERATE_GET_CLASS_WITH_CACHE (idispatch, Mono.Interop, IDispatch)
GENERATE_GET_CLASS_WITH_CACHE (iunknown, Mono.Interop, IUnknown)

GENERATE_GET_CLASS_WITH_CACHE (com_object, System, __ComObject)
GENERATE_GET_CLASS_WITH_CACHE (variant, System, Variant)

/* Upon creation of a CCW, only allocate a weak handle and set the
* reference count to 0. If the unmanaged client code decides to addref and
* hold onto the CCW, I then allocate a strong handle. Once the reference count
* goes back to 0, convert back to a weak handle.
*/
typedef struct {
guint32 ref_count;
guint32 gc_handle;
GHashTable* vtable_hash;
#ifdef HOST_WIN32
gpointer free_marshaler;
#endif
} MonoCCW;

/* This type is the actual pointer passed to unmanaged code
* to represent a COM interface.
*/
typedef struct {
gpointer vtable;
MonoCCW* ccw;
} MonoCCWInterface;

/* IUnknown */
static int STDCALL cominterop_ccw_addref (MonoCCWInterface* ccwe);

static int STDCALL cominterop_ccw_release (MonoCCWInterface* ccwe);

static int STDCALL cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv);

/* IDispatch */
static int STDCALL cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo);

static int STDCALL cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo);

static int STDCALL cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
gunichar2** rgszNames, guint32 cNames,
guint32 lcid, gint32 *rgDispId);

static int STDCALL cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
gpointer riid, guint32 lcid,
guint16 wFlags, gpointer pDispParams,
gpointer pVarResult, gpointer pExcepInfo,
guint32 *puArgErr);

static MonoMethod *
cominterop_get_managed_wrapper_adjusted (MonoMethod *method);

static gpointer
cominterop_get_ccw (MonoObject* object, MonoClass* itf);

static MonoObject*
cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify);

/* SAFEARRAY marshalling */
static gboolean
mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *indices, gpointer empty, gpointer parameter, gboolean allocateNewArray);

static gpointer
mono_marshal_safearray_get_value (gpointer safearray, gpointer indices);

static gboolean
mono_marshal_safearray_next (gpointer safearray, gpointer indices);

static void
mono_marshal_safearray_end (gpointer safearray, gpointer indices);

static gboolean
mono_marshal_safearray_create (MonoArray *input, gpointer *newsafearray, gpointer *indices, gpointer empty);

static void
mono_marshal_safearray_set_value (gpointer safearray, gpointer indices, gpointer value);

static void
mono_marshal_safearray_free_indices (gpointer indices);

/**
* cominterop_method_signature:
* @method: a method
*
* Returns: the corresponding unmanaged method signature for a managed COM
* method.
*/
static MonoMethodSignature*
cominterop_method_signature (MonoMethod* method)
{
MonoMethodSignature *res;
MonoImage *image = method->klass->image;
MonoMethodSignature *sig = mono_method_signature (method);
gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
int sigsize;
int i;
int param_count = sig->param_count + 1; // convert this arg into IntPtr arg

if (!preserve_sig &&!MONO_TYPE_IS_VOID (sig->ret))
param_count++;

res = mono_metadata_signature_alloc (image, param_count);
sigsize = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
memcpy (res, sig, sigsize);

// now move args forward one
for (i = sig->param_count-1; i >= 0; i--)
res->params[i+1] = sig->params[i];

// first arg is interface pointer
res->params[0] = &mono_defaults.int_class->byval_arg;

if (preserve_sig) {
res->ret = sig->ret;
}
else {
// last arg is return type
if (!MONO_TYPE_IS_VOID (sig->ret)) {
res->params[param_count-1] = mono_metadata_type_dup (image, sig->ret);
res->params[param_count-1]->byref = 1;
res->params[param_count-1]->attrs = PARAM_ATTRIBUTE_OUT;
}

// return type is always int32 (HRESULT)
res->ret = &mono_defaults.int32_class->byval_arg;
}

// no pinvoke
res->pinvoke = FALSE;

// no hasthis
res->hasthis = 0;

// set param_count
res->param_count = param_count;

// STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM
#ifdef HOST_WIN32
res->call_convention = MONO_CALL_STDCALL;
#else
res->call_convention = MONO_CALL_C;
#endif

return res;
}

/**
* cominterop_get_function_pointer:
* @itf: a pointer to the COM interface
* @slot: the vtable slot of the method pointer to return
*
* Returns: the unmanaged vtable function pointer from the interface
*/
static gpointer
cominterop_get_function_pointer (gpointer itf, int slot)
{
gpointer func;
func = *((*(gpointer**)itf)+slot);
return func;
}

/**
* cominterop_object_is_com_object:
* @obj: a pointer to the object
*
* Returns: a value indicating if the object is a
* Runtime Callable Wrapper (RCW) for a COM object
*/
static gboolean
cominterop_object_is_rcw (MonoObject *obj)
{
MonoClass *klass = NULL;
MonoRealProxy* real_proxy = NULL;
if (!obj)
return FALSE;
klass = mono_object_class (obj);
if (!mono_class_is_transparent_proxy (klass))
return FALSE;

real_proxy = ((MonoTransparentProxy*)obj)->rp;
if (!real_proxy)
return FALSE;

klass = mono_object_class (real_proxy);
return (klass && klass == mono_class_get_interop_proxy_class ());
}

static int
cominterop_get_com_slot_begin (MonoClass* klass)
{
static MonoClass *interface_type_attribute = NULL;
MonoCustomAttrInfo *cinfo = NULL;
MonoInterfaceTypeAttribute* itf_attr = NULL;

if (!interface_type_attribute)
interface_type_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "InterfaceTypeAttribute");
cinfo = mono_custom_attrs_from_class (klass);
if (cinfo) {
MonoError error;
itf_attr = (MonoInterfaceTypeAttribute*)mono_custom_attrs_get_attr_checked (cinfo, interface_type_attribute, &error);
g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
if (!cinfo->cached)
mono_custom_attrs_free (cinfo);
}

if (itf_attr && itf_attr->intType == 1)
return 3; /* 3 methods in IUnknown*/
else
return 7; /* 7 methods in IDispatch*/
}

/**
* cominterop_get_method_interface:
* @method: method being called
*
* Returns: the MonoClass* representing the interface on which
* the method is defined.
*/
static MonoClass*
cominterop_get_method_interface (MonoMethod* method)
{
MonoError error;
MonoClass *ic = method->klass;

/* if method is on a class, we need to look up interface method exists on */
if (!MONO_CLASS_IS_INTERFACE(method->klass)) {
GPtrArray *ifaces = mono_class_get_implemented_interfaces (method->klass, &error);
g_assert (mono_error_ok (&error));
if (ifaces) {
int i;
mono_class_setup_vtable (method->klass);
for (i = 0; i < ifaces->len; ++i) {
int j, offset;
gboolean found = FALSE;
ic = g_ptr_array_index (ifaces, i);
offset = mono_class_interface_offset (method->klass, ic);
for (j = 0; j < ic->method.count; ++j) {
if (method->klass->vtable [j + offset] == method) {
found = TRUE;
break;
}
}
if (found)
break;
ic = NULL;
}
g_ptr_array_free (ifaces, TRUE);
}
}

if (!ic)
g_assert (ic);
g_assert (MONO_CLASS_IS_INTERFACE (ic));

return ic;
}

/**
* cominterop_get_com_slot_for_method:
* @method: a method
*
* Returns: the method's slot in the COM interface vtable
*/
static int
cominterop_get_com_slot_for_method (MonoMethod* method)
{
guint32 slot = method->slot;
  MonoClass *ic = method->klass;

/* if method is on a class, we need to look up interface method exists on */
if (!MONO_CLASS_IS_INTERFACE(ic)) {
int offset = 0;
int i = 0;
ic = cominterop_get_method_interface (method);
offset = mono_class_interface_offset (method->klass, ic);
g_assert(offset >= 0);
for(i = 0; i < ic->method.count; ++i) {
if (method->klass->vtable [i + offset] == method)
{
slot = ic->methods[i]->slot;
break;
}
}
}

g_assert (ic);
g_assert (MONO_CLASS_IS_INTERFACE (ic));

return slot + cominterop_get_com_slot_begin (ic);
}


static void
cominterop_mono_string_to_guid (MonoString* string, guint8 *guid);

static gboolean
cominterop_class_guid (MonoClass* klass, guint8* guid)
{
static MonoClass *GuidAttribute = NULL;
MonoCustomAttrInfo *cinfo;

/* Handle the GuidAttribute */
if (!GuidAttribute)
GuidAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "GuidAttribute");

cinfo = mono_custom_attrs_from_class (klass);
if (cinfo) {
MonoError error;
MonoReflectionGuidAttribute *attr = (MonoReflectionGuidAttribute*)mono_custom_attrs_get_attr_checked (cinfo, GuidAttribute, &error);
g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/

if (!attr)
return FALSE;
if (!cinfo->cached)
mono_custom_attrs_free (cinfo);

cominterop_mono_string_to_guid (attr->guid, guid);
return TRUE;
}
return FALSE;
}

static gboolean
cominterop_com_visible (MonoClass* klass)
{
static MonoClass *ComVisibleAttribute = NULL;
MonoError error;
MonoCustomAttrInfo *cinfo;
GPtrArray *ifaces;
MonoBoolean visible = 1;

/* Handle the ComVisibleAttribute */
if (!ComVisibleAttribute)
ComVisibleAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "ComVisibleAttribute");

cinfo = mono_custom_attrs_from_class (klass);
if (cinfo) {
MonoError error;
MonoReflectionComVisibleAttribute *attr = (MonoReflectionComVisibleAttribute*)mono_custom_attrs_get_attr_checked (cinfo, ComVisibleAttribute, &error);
g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/

if (attr)
visible = attr->visible;
if (!cinfo->cached)
mono_custom_attrs_free (cinfo);
if (visible)
return TRUE;
}

ifaces = mono_class_get_implemented_interfaces (klass, &error);
g_assert (mono_error_ok (&error));
if (ifaces) {
int i;
for (i = 0; i < ifaces->len; ++i) {
MonoClass *ic = NULL;
ic = g_ptr_array_index (ifaces, i);
if (MONO_CLASS_IS_IMPORT (ic))
visible = TRUE;

}
g_ptr_array_free (ifaces, TRUE);
}
return visible;

}

static void cominterop_raise_hr_exception (int hr)
{
static MonoMethod* throw_exception_for_hr = NULL;
MonoException* ex;
void* params[1] = {&hr};
if (!throw_exception_for_hr)
throw_exception_for_hr = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetExceptionForHR", 1);
ex = (MonoException*)mono_runtime_invoke (throw_exception_for_hr, NULL, params, NULL);
mono_raise_exception (ex);
}

/**
* cominterop_get_interface:
* @obj: managed wrapper object containing COM object
* @ic: interface type to retrieve for COM object
*
* Returns: the COM interface requested
*/
static gpointer
cominterop_get_interface (MonoComObject* obj, MonoClass* ic, gboolean throw_exception)
{
gpointer itf = NULL;

g_assert (ic);
g_assert (MONO_CLASS_IS_INTERFACE (ic));

mono_cominterop_lock ();
if (obj->itf_hash)
itf = g_hash_table_lookup (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id));
mono_cominterop_unlock ();

if (!itf) {
guint8 iid [16];
int found = cominterop_class_guid (ic, iid);
int hr;
g_assert(found);
hr = ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (obj->iunknown, iid, &itf);
if (hr < 0 && throw_exception) {
cominterop_raise_hr_exception (hr);
}

if (hr >= 0 && itf) {
mono_cominterop_lock ();
if (!obj->itf_hash)
obj->itf_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
g_hash_table_insert (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id), itf);
mono_cominterop_unlock ();
}

}
if (throw_exception)
g_assert (itf);

return itf;
}

static int
cominterop_get_hresult_for_exception (MonoException* exc)
{
int hr = 0;
return hr;
}

static MonoReflectionType *
cominterop_type_from_handle (MonoType *handle)
{
MonoDomain *domain = mono_domain_get ();
MonoClass *klass = mono_class_from_mono_type (handle);

MONO_ARCH_SAVE_REGS;

mono_class_init (klass);
return mono_type_get_object (domain, handle);
}

void
mono_cominterop_init (void)
{
const char* com_provider_env;

InitializeCriticalSection (&cominterop_mutex);

com_provider_env = g_getenv ("MONO_COM");
if (com_provider_env && !strcmp(com_provider_env, "MS"))
com_provider = MONO_COM_MS;

register_icall (cominterop_get_method_interface, "cominterop_get_method_interface", "ptr ptr", FALSE);
register_icall (cominterop_get_function_pointer, "cominterop_get_function_pointer", "ptr ptr int32", FALSE);
register_icall (cominterop_object_is_rcw, "cominterop_object_is_rcw", "int32 object", FALSE);
register_icall (cominterop_get_ccw, "cominterop_get_ccw", "ptr object ptr", FALSE);
register_icall (cominterop_get_ccw_object, "cominterop_get_ccw_object", "object ptr int32", FALSE);
register_icall (cominterop_get_hresult_for_exception, "cominterop_get_hresult_for_exception", "int32 object", FALSE);
register_icall (cominterop_get_interface, "cominterop_get_interface", "ptr object ptr int32", FALSE);

register_icall (mono_string_to_bstr, "mono_string_to_bstr", "ptr obj", FALSE);
register_icall (mono_string_from_bstr, "mono_string_from_bstr", "obj ptr", FALSE);
register_icall (mono_free_bstr, "mono_free_bstr", "void ptr", FALSE);
register_icall (cominterop_type_from_handle, "cominterop_type_from_handle", "object ptr", FALSE);

/* SAFEARRAY marshalling */
register_icall (mono_marshal_safearray_begin, "mono_marshal_safearray_begin", "int32 ptr ptr ptr ptr ptr int32", FALSE);
register_icall (mono_marshal_safearray_get_value, "mono_marshal_safearray_get_value", "ptr ptr ptr", FALSE);
register_icall (mono_marshal_safearray_next, "mono_marshal_safearray_next", "int32 ptr ptr", FALSE);
register_icall (mono_marshal_safearray_end, "mono_marshal_safearray_end", "void ptr ptr", FALSE);
register_icall (mono_marshal_safearray_create, "mono_marshal_safearray_create", "int32 object ptr ptr ptr", FALSE);
register_icall (mono_marshal_safearray_set_value, "mono_marshal_safearray_set_value", "void ptr ptr ptr", FALSE);
register_icall (mono_marshal_safearray_free_indices, "mono_marshal_safearray_free_indices", "void ptr", FALSE);
}

void
mono_cominterop_cleanup (void)
{
DeleteCriticalSection (&cominterop_mutex);
}

void
mono_mb_emit_cominterop_call (MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethod* method)
{
// get function pointer from 1st arg, the COM interface pointer
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_icon (mb, cominterop_get_com_slot_for_method (method));
mono_mb_emit_icall (mb, cominterop_get_function_pointer);

mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
mono_mb_emit_byte (mb, CEE_MONO_SAVE_LMF);
mono_mb_emit_calli (mb, sig);
mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
mono_mb_emit_byte (mb, CEE_MONO_RESTORE_LMF);
}

void
mono_cominterop_emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv conv, MonoMarshalSpec *mspec)
{
switch (conv) {
case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN:
case MONO_MARSHAL_CONV_OBJECT_IDISPATCH: {
static MonoClass* com_interop_proxy_class = NULL;
static MonoMethod* com_interop_proxy_get_proxy = NULL;
static MonoMethod* get_transparent_proxy = NULL;
int real_proxy;
guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
MonoClass *klass = NULL;

klass = mono_class_from_mono_type (type);

mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_byte (mb, CEE_LDNULL);
mono_mb_emit_byte (mb, CEE_STIND_REF);

mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_I);
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

/* load dst to store later */
mono_mb_emit_ldloc (mb, 1);

mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_ccw_object);
pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);

if (!com_interop_proxy_class)
com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
if (!com_interop_proxy_get_proxy)
com_interop_proxy_get_proxy = mono_class_get_method_from_name_flags (com_interop_proxy_class, "GetProxy", 2, METHOD_ATTRIBUTE_PRIVATE);
#ifndef DISABLE_REMOTING
if (!get_transparent_proxy)
get_transparent_proxy = mono_class_get_method_from_name (mono_defaults.real_proxy_class, "GetTransparentProxy", 0);
#endif

real_proxy = mono_mb_add_local (mb, &com_interop_proxy_class->byval_arg);

mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_ptr (mb, &mono_class_get_com_object_class ()->byval_arg);
mono_mb_emit_icall (mb, cominterop_type_from_handle);
mono_mb_emit_managed_call (mb, com_interop_proxy_get_proxy, NULL);
mono_mb_emit_managed_call (mb, get_transparent_proxy, NULL);
if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
g_assert (klass);
  mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
}
  mono_mb_emit_byte (mb, CEE_STIND_REF);
pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);

/* is already managed object */
mono_mb_patch_short_branch (mb, pos_ccw);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_ccw_object);

if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
g_assert (klass);
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
}
mono_mb_emit_byte (mb, CEE_STIND_REF);

mono_mb_patch_short_branch (mb, pos_end);
/* case if null */
mono_mb_patch_short_branch (mb, pos_null);
break;
}
default:
g_assert_not_reached ();
}
}

void
mono_cominterop_emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv conv, MonoMarshalSpec *mspec)
{
switch (conv) {
case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
case MONO_MARSHAL_CONV_OBJECT_IDISPATCH:
case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN: {
guint32 pos_null = 0, pos_rcw = 0, pos_end = 0;

mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_icon (mb, 0);
mono_mb_emit_byte (mb, CEE_CONV_U);
mono_mb_emit_byte (mb, CEE_STIND_I);

mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);

// if null just break, dst was already inited to 0
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
mono_mb_emit_icall (mb, cominterop_object_is_rcw);
pos_rcw = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

// load dst to store later
mono_mb_emit_ldloc (mb, 1);

// load src
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, rp));
mono_mb_emit_byte (mb, CEE_LDIND_REF);

/* load the RCW from the ComInteropProxy*/
mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoComInteropProxy, com_object));
mono_mb_emit_byte (mb, CEE_LDIND_REF);

if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
mono_mb_emit_ptr (mb, mono_type_get_class (type));
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_interface);

}
else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN) {
static MonoProperty* iunknown = NULL;

if (!iunknown)
iunknown = mono_class_get_property_from_name (mono_class_get_com_object_class (), "IUnknown");
mono_mb_emit_managed_call (mb, iunknown->get, NULL);
}
else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH) {
static MonoProperty* idispatch = NULL;

if (!idispatch)
idispatch = mono_class_get_property_from_name (mono_class_get_com_object_class (), "IDispatch");
mono_mb_emit_managed_call (mb, idispatch->get, NULL);
}
else {
g_assert_not_reached ();
}
mono_mb_emit_byte (mb, CEE_STIND_I);
pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);

// if not rcw
mono_mb_patch_short_branch (mb, pos_rcw);
/* load dst to store later */
mono_mb_emit_ldloc (mb, 1);
/* load src */
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);

if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE)
mono_mb_emit_ptr (mb, mono_type_get_class (type));
else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN)
mono_mb_emit_ptr (mb, mono_class_get_iunknown_class ());
else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH)
mono_mb_emit_ptr (mb, mono_class_get_idispatch_class ());
else
g_assert_not_reached ();
mono_mb_emit_icall (mb, cominterop_get_ccw);
mono_mb_emit_byte (mb, CEE_STIND_I);

mono_mb_patch_short_branch (mb, pos_end);
mono_mb_patch_short_branch (mb, pos_null);
break;
}
default:
g_assert_not_reached ();
}
}

/**
* cominterop_get_native_wrapper_adjusted:
* @method: managed COM Interop method
*
* Returns: the generated method to call with signature matching
* the unmanaged COM Method signature
*/
static MonoMethod *
cominterop_get_native_wrapper_adjusted (MonoMethod *method)
{
MonoMethod *res;
MonoMethodBuilder *mb_native;
MonoMarshalSpec **mspecs;
MonoMethodSignature *sig, *sig_native;
MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
int i;

sig = mono_method_signature (method);

// create unmanaged wrapper
mb_native = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_NATIVE);
sig_native = cominterop_method_signature (method);

mspecs = g_new (MonoMarshalSpec*, sig_native->param_count+1);
memset (mspecs, 0, sizeof(MonoMarshalSpec*)*(sig_native->param_count+1));

mono_method_get_marshal_info (method, mspecs);

// move managed args up one
for (i = sig->param_count; i >= 1; i--)
mspecs[i+1] = mspecs[i];

// first arg is IntPtr for interface
mspecs[1] = NULL;

if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG)) {
// move return spec to last param
if (!MONO_TYPE_IS_VOID (sig->ret))
mspecs[sig_native->param_count] = mspecs[0];

mspecs[0] = NULL;
}

for (i = 1; i < sig_native->param_count; i++) {
int mspec_index = i + 1;
if (mspecs[mspec_index] == NULL) {
// default object to VARIANT
if (sig_native->params[i]->type == MONO_TYPE_OBJECT) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
}
else if (sig_native->params[i]->type == MONO_TYPE_STRING) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
}
else if (sig_native->params[i]->type == MONO_TYPE_CLASS) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
}
else if (sig_native->params[i]->type == MONO_TYPE_BOOLEAN) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_VARIANTBOOL;
}
}
}

if (method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG) {
// move return spec to last param
if (!MONO_TYPE_IS_VOID (sig->ret) && mspecs[0] == NULL) {
// default object to VARIANT
if (sig->ret->type == MONO_TYPE_OBJECT) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_STRUCT;
}
else if (sig->ret->type == MONO_TYPE_STRING) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_BSTR;
}
else if (sig->ret->type == MONO_TYPE_CLASS) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_INTERFACE;
}
else if (sig->ret->type == MONO_TYPE_BOOLEAN) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_VARIANTBOOL;
}
}
}

mono_marshal_emit_native_wrapper (method->klass->image, mb_native, sig_native, piinfo, mspecs, piinfo->addr, FALSE, TRUE, FALSE);

res = mono_mb_create_method (mb_native, sig_native, sig_native->param_count + 16);

mono_mb_free (mb_native);

for (i = sig_native->param_count; i >= 0; i--)
if (mspecs [i])
mono_metadata_free_marshal_spec (mspecs [i]);
g_free (mspecs);

return res;
}

/**
* mono_cominterop_get_native_wrapper:
* @method: managed method
*
* Returns: the generated method to call
*/
MonoMethod *
mono_cominterop_get_native_wrapper (MonoMethod *method)
{
MonoMethod *res;
GHashTable *cache;
MonoMethodBuilder *mb;
MonoMethodSignature *sig, *csig;

g_assert (method);

cache = mono_marshal_get_cache (&method->klass->image->cominterop_wrapper_cache, mono_aligned_addr_hash, NULL);
if ((res = mono_marshal_find_in_cache (cache, method)))
return res;

if (!method->klass->vtable)
mono_class_setup_vtable (method->klass);

if (!method->klass->methods)
mono_class_setup_methods (method->klass);
g_assert (!method->klass->exception_type); /*FIXME do proper error handling*/

sig = mono_method_signature (method);
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);

/* if method klass is import, that means method
* is really a com call. let interop system emit it.
*/
if (MONO_CLASS_IS_IMPORT(method->klass)) {
/* FIXME: we have to call actual class .ctor
* instead of just __ComObject .ctor.
*/
if (!strcmp(method->name, ".ctor")) {
static MonoMethod *ctor = NULL;

if (!ctor)
ctor = mono_class_get_method_from_name (mono_class_get_com_object_class (), ".ctor", 0);
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_managed_call (mb, ctor, NULL);
mono_mb_emit_byte (mb, CEE_RET);
}
else {
static MonoMethod * ThrowExceptionForHR = NULL;
MonoMethod *adjusted_method;
int retval = 0;
int ptr_this;
int i;
gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;

// add local variables
ptr_this = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
if (!MONO_TYPE_IS_VOID (sig->ret))
retval = mono_mb_add_local (mb, sig->ret);

// get the type for the interface the method is defined on
// and then get the underlying COM interface for that type
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_ptr (mb, method);
mono_mb_emit_icall (mb, cominterop_get_method_interface);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_interface);
mono_mb_emit_stloc (mb, ptr_this);

// arg 1 is unmanaged this pointer
mono_mb_emit_ldloc (mb, ptr_this);

// load args
for (i = 1; i <= sig->param_count; i++)
mono_mb_emit_ldarg (mb, i);

// push managed return value as byref last argument
if (!MONO_TYPE_IS_VOID (sig->ret) && !preserve_sig)
mono_mb_emit_ldloc_addr (mb, retval);

adjusted_method = cominterop_get_native_wrapper_adjusted (method);
mono_mb_emit_managed_call (mb, adjusted_method, NULL);

if (!preserve_sig) {
if (!ThrowExceptionForHR)
ThrowExceptionForHR = mono_class_get_method_from_name (mono_defaults.marshal_class, "ThrowExceptionForHR", 1);
mono_mb_emit_managed_call (mb, ThrowExceptionForHR, NULL);

// load return value managed is expecting
if (!MONO_TYPE_IS_VOID (sig->ret))
mono_mb_emit_ldloc (mb, retval);
}

mono_mb_emit_byte (mb, CEE_RET);
}


}
/* Does this case ever get hit? */
else {
char *msg = g_strdup ("non imported interfaces on \
imported classes is not yet implemented.");
mono_mb_emit_exception (mb, "NotSupportedException", msg);
}
csig = mono_metadata_signature_dup_full (method->klass->image, sig);
csig->pinvoke = 0;
res = mono_mb_create_and_cache (cache, method,
mb, csig, csig->param_count + 16);
mono_mb_free (mb);
return res;
}

/**
* mono_cominterop_get_invoke:
* @method: managed method
*
* Returns: the generated method that calls the underlying __ComObject
* rather than the proxy object.
*/
MonoMethod *
mono_cominterop_get_invoke (MonoMethod *method)
{
MonoMethodSignature *sig;
MonoMethodBuilder *mb;
MonoMethod *res;
int i, temp_obj;
GHashTable* cache = mono_marshal_get_cache (&method->klass->image->cominterop_invoke_cache, mono_aligned_addr_hash, NULL);

g_assert (method);

if ((res = mono_marshal_find_in_cache (cache, method)))
return res;

sig = mono_signature_no_pinvoke (method);

/* we cant remote methods without this pointer */
if (!sig->hasthis)
return method;

mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP_INVOKE);

/* get real proxy object, which is a ComInteropProxy in this case*/
temp_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, rp));
mono_mb_emit_byte (mb, CEE_LDIND_REF);

/* load the RCW from the ComInteropProxy*/
mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoComInteropProxy, com_object));
mono_mb_emit_byte (mb, CEE_LDIND_REF);

/* load args and make the call on the RCW */
for (i = 1; i <= sig->param_count; i++)
mono_mb_emit_ldarg (mb, i);

if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
MonoMethod * native_wrapper = mono_cominterop_get_native_wrapper(method);
mono_mb_emit_managed_call (mb, native_wrapper, NULL);
}
else {
if (method->flags & METHOD_ATTRIBUTE_VIRTUAL)
mono_mb_emit_op (mb, CEE_CALLVIRT, method);
else
mono_mb_emit_op (mb, CEE_CALL, method);
}

if (!strcmp(method->name, ".ctor")) {
static MonoClass *com_interop_proxy_class = NULL;
static MonoMethod *cache_proxy = NULL;

if (!com_interop_proxy_class)
com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
if (!cache_proxy)
cache_proxy = mono_class_get_method_from_name (com_interop_proxy_class, "CacheProxy", 0);

mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, rp));
mono_mb_emit_byte (mb, CEE_LDIND_REF);
mono_mb_emit_managed_call (mb, cache_proxy, NULL);
}

mono_marshal_emit_thread_interrupt_checkpoint (mb);

mono_mb_emit_byte (mb, CEE_RET);

res = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
mono_mb_free (mb);

return res;
}

/* Maps a managed object to its unmanaged representation
* i.e. it's COM Callable Wrapper (CCW).
* Key: MonoObject*
* Value: MonoCCW*
*/
static GHashTable* ccw_hash = NULL;

/* Maps a CCW interface to it's containing CCW.
* Note that a CCW support many interfaces.
* Key: MonoCCW*
* Value: MonoCCWInterface*
*/
static GHashTable* ccw_interface_hash = NULL;

/* Maps the IUnknown value of a RCW to
* it's MonoComInteropProxy*.
* Key: void*
* Value: gchandle
*/
static GHashTable* rcw_hash = NULL;

int
mono_cominterop_emit_marshal_com_interface (EmitMarshalContext *m, int argnum,
MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
MonoMethodBuilder *mb = m->mb;
MonoClass *klass = t->data.klass;
static MonoMethod* get_object_for_iunknown = NULL;
static MonoMethod* get_iunknown_for_object_internal = NULL;
static MonoMethod* get_com_interface_for_object_internal = NULL;
static MonoMethod* get_idispatch_for_object_internal = NULL;
static MonoMethod* marshal_release = NULL;
static MonoMethod* AddRef = NULL;
if (!get_object_for_iunknown)
get_object_for_iunknown = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForIUnknown", 1);
if (!get_iunknown_for_object_internal)
get_iunknown_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetIUnknownForObjectInternal", 1);
if (!get_idispatch_for_object_internal)
get_idispatch_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetIDispatchForObjectInternal", 1);
if (!get_com_interface_for_object_internal)
get_com_interface_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetComInterfaceForObjectInternal", 2);
if (!marshal_release)
marshal_release = mono_class_get_method_from_name (mono_defaults.marshal_class, "Release", 1);

switch (action) {
case MARSHAL_ACTION_CONV_IN: {
guint32 pos_null = 0;

*conv_arg_type = &mono_defaults.int_class->byval_arg;
conv_arg = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);

mono_mb_emit_ptr (mb, NULL);
mono_mb_emit_stloc (mb, conv_arg);

/* we dont need any conversions for out parameters */
if (t->byref && t->attrs & PARAM_ATTRIBUTE_OUT)
break;

mono_mb_emit_ldarg (mb, argnum);
if (t->byref)
mono_mb_emit_byte (mb, CEE_LDIND_REF);
/* if null just break, conv arg was already inited to 0 */
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

mono_mb_emit_ldarg (mb, argnum);
if (t->byref)
mono_mb_emit_byte (mb, CEE_LDIND_REF);

if (klass && klass != mono_defaults.object_class) {
mono_mb_emit_ptr (mb, t);
mono_mb_emit_icall (mb, cominterop_type_from_handle);
mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
}
else if (spec->native == MONO_NATIVE_IUNKNOWN)
mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
else if (spec->native == MONO_NATIVE_IDISPATCH)
mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
else
g_assert_not_reached ();
mono_mb_emit_stloc (mb, conv_arg);
mono_mb_patch_short_branch (mb, pos_null);
break;
}

case MARSHAL_ACTION_CONV_OUT: {
if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
int ccw_obj;
guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);

mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_byte (mb, CEE_LDNULL);
mono_mb_emit_byte (mb, CEE_STIND_REF);

mono_mb_emit_ldloc (mb, conv_arg);
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_ccw_object);
mono_mb_emit_stloc (mb, ccw_obj);
mono_mb_emit_ldloc (mb, ccw_obj);
pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);

mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);

if (klass && klass != mono_defaults.object_class)
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
mono_mb_emit_byte (mb, CEE_STIND_REF);

pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);

/* is already managed object */
mono_mb_patch_short_branch (mb, pos_ccw);
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_ldloc (mb, ccw_obj);

if (klass && klass != mono_defaults.object_class)
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
mono_mb_emit_byte (mb, CEE_STIND_REF);

mono_mb_patch_short_branch (mb, pos_end);

/* need to call Release to follow COM rules of ownership */
mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_managed_call (mb, marshal_release, NULL);
mono_mb_emit_byte (mb, CEE_POP);

/* case if null */
mono_mb_patch_short_branch (mb, pos_null);
}
break;
}
case MARSHAL_ACTION_PUSH:
if (t->byref)
mono_mb_emit_ldloc_addr (mb, conv_arg);
else
mono_mb_emit_ldloc (mb, conv_arg);
break;

case MARSHAL_ACTION_CONV_RESULT: {
int ccw_obj, ret_ptr;
guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
ret_ptr = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);

/* store return value */
mono_mb_emit_stloc (mb, ret_ptr);

mono_mb_emit_ldloc (mb, ret_ptr);
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

mono_mb_emit_ldloc (mb, ret_ptr);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_ccw_object);
mono_mb_emit_stloc (mb, ccw_obj);
mono_mb_emit_ldloc (mb, ccw_obj);
pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);

mono_mb_emit_ldloc (mb, ret_ptr);
mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);

if (klass && klass != mono_defaults.object_class)
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
mono_mb_emit_stloc (mb, 3);

pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);

/* is already managed object */
mono_mb_patch_short_branch (mb, pos_ccw);
mono_mb_emit_ldloc (mb, ccw_obj);

if (klass && klass != mono_defaults.object_class)
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
mono_mb_emit_stloc (mb, 3);

mono_mb_patch_short_branch (mb, pos_end);

/* need to call Release to follow COM rules of ownership */
mono_mb_emit_ldloc (mb, ret_ptr);
mono_mb_emit_managed_call (mb, marshal_release, NULL);
mono_mb_emit_byte (mb, CEE_POP);

/* case if null */
mono_mb_patch_short_branch (mb, pos_null);
break;
}

case MARSHAL_ACTION_MANAGED_CONV_IN: {
int ccw_obj;
guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);

klass = mono_class_from_mono_type (t);
conv_arg = mono_mb_add_local (mb, &klass->byval_arg);
*conv_arg_type = &mono_defaults.int_class->byval_arg;

mono_mb_emit_byte (mb, CEE_LDNULL);
mono_mb_emit_stloc (mb, conv_arg);
if (t->attrs & PARAM_ATTRIBUTE_OUT)
break;

mono_mb_emit_ldarg (mb, argnum);
if (t->byref)
mono_mb_emit_byte (mb, CEE_LDIND_REF);
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

mono_mb_emit_ldarg (mb, argnum);
if (t->byref)
mono_mb_emit_byte (mb, CEE_LDIND_REF);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_ccw_object);
mono_mb_emit_stloc (mb, ccw_obj);
mono_mb_emit_ldloc (mb, ccw_obj);
pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);


mono_mb_emit_ldarg (mb, argnum);
if (t->byref)
mono_mb_emit_byte (mb, CEE_LDIND_REF);
mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);

if (klass && klass != mono_defaults.object_class)
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
mono_mb_emit_stloc (mb, conv_arg);
pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);

/* is already managed object */
mono_mb_patch_short_branch (mb, pos_ccw);
mono_mb_emit_ldloc (mb, ccw_obj);
if (klass && klass != mono_defaults.object_class)
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
mono_mb_emit_stloc (mb, conv_arg);

mono_mb_patch_short_branch (mb, pos_end);
/* case if null */
mono_mb_patch_short_branch (mb, pos_null);
break;
}

case MARSHAL_ACTION_MANAGED_CONV_OUT: {
if (t->byref && t->attrs & PARAM_ATTRIBUTE_OUT) {
guint32 pos_null = 0;

if (!AddRef)
AddRef = mono_class_get_method_from_name (mono_defaults.marshal_class, "AddRef", 1);

mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_byte (mb, CEE_LDC_I4_0);
mono_mb_emit_byte (mb, CEE_STIND_I);

mono_mb_emit_ldloc (mb, conv_arg);
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

/* to store later */
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_ldloc (mb, conv_arg);
if (klass && klass != mono_defaults.object_class) {
mono_mb_emit_ptr (mb, t);
mono_mb_emit_icall (mb, cominterop_type_from_handle);
mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
}
else if (spec->native == MONO_NATIVE_IUNKNOWN)
mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
else if (spec->native == MONO_NATIVE_IDISPATCH)
mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
else
g_assert_not_reached ();
mono_mb_emit_byte (mb, CEE_STIND_I);

mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_managed_call (mb, AddRef, NULL);
mono_mb_emit_byte (mb, CEE_POP);

mono_mb_patch_short_branch (mb, pos_null);
}
break;
}

case MARSHAL_ACTION_MANAGED_CONV_RESULT: {
guint32 pos_null = 0;
int ccw_obj;
ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);

if (!AddRef)
AddRef = mono_class_get_method_from_name (mono_defaults.marshal_class, "AddRef", 1);

/* store return value */
mono_mb_emit_stloc (mb, ccw_obj);

mono_mb_emit_ldloc (mb, ccw_obj);

/* if null just break, conv arg was already inited to 0 */
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

/* to store later */
mono_mb_emit_ldloc (mb, ccw_obj);
if (klass && klass != mono_defaults.object_class) {
mono_mb_emit_ptr (mb, t);
mono_mb_emit_icall (mb, cominterop_type_from_handle);
mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
}
else if (spec->native == MONO_NATIVE_IUNKNOWN)
mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
else if (spec->native == MONO_NATIVE_IDISPATCH)
mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
else
g_assert_not_reached ();
mono_mb_emit_stloc (mb, 3);
mono_mb_emit_ldloc (mb, 3);

mono_mb_emit_managed_call (mb, AddRef, NULL);
mono_mb_emit_byte (mb, CEE_POP);

mono_mb_patch_short_branch (mb, pos_null);
break;
}

default:
g_assert_not_reached ();
}

return conv_arg;
}

typedef struct
{
int (STDCALL *QueryInterface)(gpointer pUnk, gpointer riid, gpointer* ppv);
int (STDCALL *AddRef)(gpointer pUnk);
int (STDCALL *Release)(gpointer pUnk);
} MonoIUnknown;

#define MONO_S_OK 0x00000000L
#define MONO_E_NOINTERFACE 0x80004002L
#define MONO_E_NOTIMPL 0x80004001L
#define MONO_E_INVALIDARG 0x80070057L
#define MONO_E_DISP_E_UNKNOWNNAME 0x80020006L
#define MONO_E_DISPID_UNKNOWN (gint32)-1

int
ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk)
{
g_assert (pUnk);
return (*(MonoIUnknown**)pUnk)->AddRef(pUnk);
}

int
ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk, gpointer riid, gpointer* ppv)
{
g_assert (pUnk);
return (*(MonoIUnknown**)pUnk)->QueryInterface(pUnk, riid, ppv);
}

int
ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk)
{
g_assert (pUnk);
return (*(MonoIUnknown**)pUnk)->Release(pUnk);
}

static gboolean cominterop_can_support_dispatch (MonoClass* klass)
{
if (!(klass->flags & TYPE_ATTRIBUTE_PUBLIC) )
return FALSE;

if (!cominterop_com_visible (klass))
return FALSE;

return TRUE;
}

static void*
cominterop_get_idispatch_for_object (MonoObject* object)
{
if (!object)
return NULL;

if (cominterop_object_is_rcw (object)) {
return cominterop_get_interface (((MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp)->com_object,
mono_class_get_idispatch_class (), TRUE);
}
else {
MonoClass* klass = mono_object_class (object);
if (!cominterop_can_support_dispatch (klass) )
cominterop_raise_hr_exception (MONO_E_NOINTERFACE);
return cominterop_get_ccw (object, mono_class_get_idispatch_class ());
}
}

void*
ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal (MonoObject* object)
{
#ifndef DISABLE_COM
if (!object)
return NULL;

if (cominterop_object_is_rcw (object)) {
MonoClass *klass = NULL;
MonoRealProxy* real_proxy = NULL;
if (!object)
return NULL;
klass = mono_object_class (object);
if (!mono_class_is_transparent_proxy (klass)) {
g_assert_not_reached ();
return NULL;
}

real_proxy = ((MonoTransparentProxy*)object)->rp;
if (!real_proxy) {
g_assert_not_reached ();
return NULL;
}

klass = mono_object_class (real_proxy);
if (klass != mono_class_get_interop_proxy_class ()) {
g_assert_not_reached ();
return NULL;
}

if (!((MonoComInteropProxy*)real_proxy)->com_object) {
g_assert_not_reached ();
return NULL;
}

return ((MonoComInteropProxy*)real_proxy)->com_object->iunknown;
}
else {
return cominterop_get_ccw (object, mono_class_get_iunknown_class ());
}
#else
g_assert_not_reached ();
#endif
}

MonoObject*
ves_icall_System_Runtime_InteropServices_Marshal_GetObjectForCCW (void* pUnk)
{
#ifndef DISABLE_COM
MonoObject* object = NULL;

if (!pUnk)
return NULL;

/* see if it is a CCW */
object = cominterop_get_ccw_object ((MonoCCWInterface*)pUnk, TRUE);

return object;
#else
g_assert_not_reached ();
#endif
}

void*
ves_icall_System_Runtime_InteropServices_Marshal_GetIDispatchForObjectInternal (MonoObject* object)
{
#ifndef DISABLE_COM
return cominterop_get_idispatch_for_object (object);
#else
g_assert_not_reached ();
#endif
}

void*
ves_icall_System_Runtime_InteropServices_Marshal_GetCCW (MonoObject* object, MonoReflectionType* type)
{
#ifndef DISABLE_COM
MonoClass* klass = NULL;
void* itf = NULL;
g_assert (type);
g_assert (type->type);
klass = mono_type_get_class (type->type);
g_assert (klass);
if (!mono_class_init (klass))
mono_raise_exception (mono_class_get_exception_for_failure (klass));

itf = cominterop_get_ccw (object, klass);
g_assert (itf);
return itf;
#else
g_assert_not_reached ();
#endif
}


MonoBoolean
ves_icall_System_Runtime_InteropServices_Marshal_IsComObject (MonoObject* object)
{
#ifndef DISABLE_COM
return (MonoBoolean)cominterop_object_is_rcw (object);
#else
g_assert_not_reached ();
#endif
}

gint32
ves_icall_System_Runtime_InteropServices_Marshal_ReleaseComObjectInternal (MonoObject* object)
{
#ifndef DISABLE_COM
MonoComInteropProxy* proxy = NULL;
gint32 ref_count = 0;

g_assert (object);
g_assert (cominterop_object_is_rcw (object));

proxy = (MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp;
g_assert (proxy);

if (proxy->ref_count == 0)
return -1;

ref_count = InterlockedDecrement (&proxy->ref_count);

g_assert (ref_count >= 0);

if (ref_count == 0)
ves_icall_System_ComObject_ReleaseInterfaces (proxy->com_object);

return ref_count;
#else
g_assert_not_reached ();
#endif
}

guint32
ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal (MonoReflectionMethod *m)
{
MONO_ARCH_SAVE_REGS;

#ifndef DISABLE_COM
return cominterop_get_com_slot_for_method (m->method);
#else
g_assert_not_reached ();
#endif
}

/* Only used for COM RCWs */
MonoObject *
ves_icall_System_ComObject_CreateRCW (MonoReflectionType *type)
{
MonoClass *klass;
MonoDomain *domain;
MonoObject *obj;

MONO_ARCH_SAVE_REGS;

domain = mono_object_domain (type);
klass = mono_class_from_mono_type (type->type);

/* call mono_object_new_alloc_specific instead of mono_object_new
* because we want to actually create object. mono_object_new checks
* to see if type is import and creates transparent proxy. this method
* is called by the corresponding real proxy to create the real RCW.
* Constructor does not need to be called. Will be called later.
*/
obj = mono_object_new_alloc_specific (mono_class_vtable_full (domain, klass, TRUE));
return obj;
}

static gboolean
cominterop_rcw_interface_finalizer (gpointer key, gpointer value, gpointer user_data)
{
ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (value);
return TRUE;
}

void
ves_icall_System_ComObject_ReleaseInterfaces (MonoComObject* obj)
{
g_assert(obj);
if (obj->itf_hash) {
guint32 gchandle = 0;
mono_cominterop_lock ();
gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash, obj->iunknown));
if (gchandle) {
mono_gchandle_free (gchandle);
g_hash_table_remove (rcw_hash, obj->iunknown);
}

g_hash_table_foreach_remove (obj->itf_hash, cominterop_rcw_interface_finalizer, NULL);
g_hash_table_destroy (obj->itf_hash);
ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (obj->iunknown);
obj->itf_hash = obj->iunknown = NULL;
mono_cominterop_unlock ();
}
}

static gboolean
cominterop_rcw_finalizer (gpointer key, gpointer value, gpointer user_data)
{
guint32 gchandle = 0;

gchandle = GPOINTER_TO_UINT (value);
if (gchandle) {
MonoComInteropProxy* proxy = (MonoComInteropProxy*)mono_gchandle_get_target (gchandle);

if (proxy) {
if (proxy->com_object->itf_hash) {
g_hash_table_foreach_remove (proxy->com_object->itf_hash, cominterop_rcw_interface_finalizer, NULL);
g_hash_table_destroy (proxy->com_object->itf_hash);
}
if (proxy->com_object->iunknown)
ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (proxy->com_object->iunknown);
proxy->com_object->itf_hash = proxy->com_object->iunknown = NULL;
}

mono_gchandle_free (gchandle);
}

return TRUE;
}

void
cominterop_release_all_rcws (void)
{
if (!rcw_hash)
return;

mono_cominterop_lock ();

g_hash_table_foreach_remove (rcw_hash, cominterop_rcw_finalizer, NULL);
g_hash_table_destroy (rcw_hash);
rcw_hash = NULL;

mono_cominterop_unlock ();
}

gpointer
ves_icall_System_ComObject_GetInterfaceInternal (MonoComObject* obj, MonoReflectionType* type, MonoBoolean throw_exception)
{
#ifndef DISABLE_COM
MonoClass *class = mono_type_get_class (type->type);
if (!mono_class_init (class))
mono_raise_exception (mono_class_get_exception_for_failure (class));

return cominterop_get_interface (obj, class, (gboolean)throw_exception);
#else
g_assert_not_reached ();
#endif
}

void
ves_icall_Mono_Interop_ComInteropProxy_AddProxy (gpointer pUnk, MonoComInteropProxy* proxy)
{
#ifndef DISABLE_COM
guint32 gchandle = 0;
if (!rcw_hash) {
mono_cominterop_lock ();
rcw_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
mono_cominterop_unlock ();
}

gchandle = mono_gchandle_new_weakref ((MonoObject*)proxy, FALSE);

mono_cominterop_lock ();
g_hash_table_insert (rcw_hash, pUnk, GUINT_TO_POINTER (gchandle));
mono_cominterop_unlock ();
#else
g_assert_not_reached ();
#endif
}

MonoComInteropProxy*
ves_icall_Mono_Interop_ComInteropProxy_FindProxy (gpointer pUnk)
{
#ifndef DISABLE_COM
MonoComInteropProxy* proxy = NULL;
guint32 gchandle = 0;

mono_cominterop_lock ();
if (rcw_hash)
gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash, pUnk));
mono_cominterop_unlock ();
if (gchandle) {
proxy = (MonoComInteropProxy*)mono_gchandle_get_target (gchandle);
/* proxy is null means we need to free up old RCW */
if (!proxy) {
mono_gchandle_free (gchandle);
g_hash_table_remove (rcw_hash, pUnk);
}
}
return proxy;
#else
g_assert_not_reached ();
#endif
}

/**
* cominterop_get_ccw_object:
* @ccw_entry: a pointer to the CCWEntry
* @verify: verify ccw_entry is in fact a ccw
*
* Returns: the corresponding object for the CCW
*/
static MonoObject*
cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify)
{
MonoCCW *ccw = NULL;

/* no CCW's exist yet */
if (!ccw_interface_hash)
return NULL;

if (verify) {
ccw = g_hash_table_lookup (ccw_interface_hash, ccw_entry);
}
else {
ccw = ccw_entry->ccw;
g_assert (ccw);
}
if (ccw)
return mono_gchandle_get_target (ccw->gc_handle);
else
return NULL;
}

static void
cominterop_setup_marshal_context (EmitMarshalContext *m, MonoMethod *method)
{
MonoMethodSignature *sig, *csig;
sig = mono_method_signature (method);
/* we copy the signature, so that we can modify it */
/* FIXME: which to use? */
csig = mono_metadata_signature_dup_full (method->klass->image, sig);
/* csig = mono_metadata_signature_dup (sig); */

/* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
#ifdef HOST_WIN32
csig->call_convention = MONO_CALL_STDCALL;
#else
csig->call_convention = MONO_CALL_C;
#endif
csig->hasthis = 0;
csig->pinvoke = 1;

m->image = method->klass->image;
m->piinfo = NULL;
m->retobj_var = 0;
m->sig = sig;
m->csig = csig;
}

/**
* cominterop_get_ccw:
* @object: a pointer to the object
* @itf: interface type needed
*
* Returns: a value indicating if the object is a
* Runtime Callable Wrapper (RCW) for a COM object
*/
static gpointer
cominterop_get_ccw (MonoObject* object, MonoClass* itf)
{
int i;
MonoCCW *ccw = NULL;
MonoCCWInterface* ccw_entry = NULL;
gpointer *vtable = NULL;
static gpointer iunknown[3] = {NULL, NULL, NULL};
static gpointer idispatch[4] = {NULL, NULL, NULL, NULL};
MonoClass* iface = NULL;
MonoClass* klass = NULL;
EmitMarshalContext m;
int start_slot = 3;
int method_count = 0;
GList *ccw_list, *ccw_list_item;
MonoCustomAttrInfo *cinfo = NULL;

if (!object)
return NULL;

klass = mono_object_get_class (object);

mono_cominterop_lock ();
if (!ccw_hash)
ccw_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
if (!ccw_interface_hash)
ccw_interface_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);

ccw_list = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
mono_cominterop_unlock ();

ccw_list_item = ccw_list;
while (ccw_list_item) {
MonoCCW* ccw_iter = ccw_list_item->data;
if (mono_gchandle_get_target (ccw_iter->gc_handle) == object) {
ccw = ccw_iter;
break;
}
ccw_list_item = g_list_next(ccw_list_item);
}

if (!iunknown [0]) {
iunknown [0] = cominterop_ccw_queryinterface;
iunknown [1] = cominterop_ccw_addref;
iunknown [2] = cominterop_ccw_release;
}

if (!idispatch [0]) {
idispatch [0] = cominterop_ccw_get_type_info_count;
idispatch [1] = cominterop_ccw_get_type_info;
idispatch [2] = cominterop_ccw_get_ids_of_names;
idispatch [3] = cominterop_ccw_invoke;
}

if (!ccw) {
ccw = g_new0 (MonoCCW, 1);
#ifdef HOST_WIN32
ccw->free_marshaler = 0;
#endif
ccw->vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
ccw->ref_count = 0;
/* just alloc a weak handle until we are addref'd*/
ccw->gc_handle = mono_gchandle_new_weakref (object, FALSE);

if (!ccw_list) {
ccw_list = g_list_alloc ();
ccw_list->data = ccw;
}
else
ccw_list = g_list_append (ccw_list, ccw);
mono_cominterop_lock ();
g_hash_table_insert (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)), ccw_list);
mono_cominterop_unlock ();
/* register for finalization to clean up ccw */
mono_object_register_finalizer (object);
}

cinfo = mono_custom_attrs_from_class (itf);
if (cinfo) {
static MonoClass* coclass_attribute = NULL;
if (!coclass_attribute)
coclass_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "CoClassAttribute");
if (mono_custom_attrs_has_attr (cinfo, coclass_attribute)) {
g_assert(itf->interface_count && itf->interfaces[0]);
itf = itf->interfaces[0];
}
if (!cinfo->cached)
mono_custom_attrs_free (cinfo);
}

iface = itf;
if (iface == mono_class_get_iunknown_class ()) {
start_slot = 3;
}
else if (iface == mono_class_get_idispatch_class ()) {
start_slot = 7;
}
else {
method_count += iface->method.count;
start_slot = cominterop_get_com_slot_begin (iface);
iface = NULL;
}

ccw_entry = g_hash_table_lookup (ccw->vtable_hash, itf);

if (!ccw_entry) {
int vtable_index = method_count-1+start_slot;
vtable = mono_image_alloc0 (klass->image, sizeof (gpointer)*(method_count+start_slot));
memcpy (vtable, iunknown, sizeof (iunknown));
if (start_slot == 7)
memcpy (vtable+3, idispatch, sizeof (idispatch));

iface = itf;
for (i = iface->method.count-1; i >= 0;i--) {
int param_index = 0;
MonoMethodBuilder *mb;
MonoMarshalSpec ** mspecs;
MonoMethod *wrapper_method, *adjust_method;
MonoMethod *method = iface->methods [i];
MonoMethodSignature* sig_adjusted;
MonoMethodSignature* sig = mono_method_signature (method);
gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;


mb = mono_mb_new (iface, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
adjust_method = cominterop_get_managed_wrapper_adjusted (method);
sig_adjusted = mono_method_signature (adjust_method);

mspecs = g_new (MonoMarshalSpec*, sig_adjusted->param_count + 1);
mono_method_get_marshal_info (method, mspecs);


/* move managed args up one */
for (param_index = sig->param_count; param_index >= 1; param_index--) {
int mspec_index = param_index+1;
mspecs [mspec_index] = mspecs [param_index];

if (mspecs[mspec_index] == NULL) {
if (sig_adjusted->params[param_index]->type == MONO_TYPE_OBJECT) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
}
else if (sig_adjusted->params[param_index]->type == MONO_TYPE_STRING) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
}
else if (sig_adjusted->params[param_index]->type == MONO_TYPE_CLASS) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
}
else if (sig_adjusted->params[param_index]->type == MONO_TYPE_BOOLEAN) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_VARIANTBOOL;
}
} else {
/* increase SizeParamIndex since we've added a param */
if (sig_adjusted->params[param_index]->type == MONO_TYPE_ARRAY ||
sig_adjusted->params[param_index]->type == MONO_TYPE_SZARRAY)
if (mspecs[mspec_index]->data.array_data.param_num != -1)
mspecs[mspec_index]->data.array_data.param_num++;
}
}

/* first arg is IntPtr for interface */
mspecs [1] = NULL;

/* move return spec to last param */
if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret)) {
if (mspecs [0] == NULL) {
if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_OBJECT) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_STRUCT;
}
else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_STRING) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_BSTR;
}
else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_CLASS) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_INTERFACE;
}
else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_BOOLEAN) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_VARIANTBOOL;
}
}

mspecs [sig_adjusted->param_count] = mspecs [0];
mspecs [0] = NULL;
}

/* skip visiblity since we call internal methods */
mb->skip_visibility = TRUE;

cominterop_setup_marshal_context (&m, adjust_method);
m.mb = mb;
mono_marshal_emit_managed_wrapper (mb, sig_adjusted, mspecs, &m, adjust_method, 0);
mono_cominterop_lock ();
wrapper_method = mono_mb_create_method (mb, m.csig, m.csig->param_count + 16);
mono_cominterop_unlock ();

vtable [vtable_index--] = mono_compile_method (wrapper_method);


for (param_index = sig_adjusted->param_count; param_index >= 0; param_index--)
if (mspecs [param_index])
mono_metadata_free_marshal_spec (mspecs [param_index]);
g_free (mspecs);
}

ccw_entry = g_new0 (MonoCCWInterface, 1);
ccw_entry->ccw = ccw;
ccw_entry->vtable = vtable;
g_hash_table_insert (ccw->vtable_hash, itf, ccw_entry);
g_hash_table_insert (ccw_interface_hash, ccw_entry, ccw);
}

return ccw_entry;
}

static gboolean
mono_marshal_free_ccw_entry (gpointer key, gpointer value, gpointer user_data)
{
g_hash_table_remove (ccw_interface_hash, value);
g_assert (value);
g_free (value);
return TRUE;
}

/**
* mono_marshal_free_ccw:
* @object: the mono object
*
* Returns: whether the object had a CCW
*/
gboolean
mono_marshal_free_ccw (MonoObject* object)
{
GList *ccw_list, *ccw_list_orig, *ccw_list_item;
/* no ccw's were created */
if (!ccw_hash || g_hash_table_size (ccw_hash) == 0)
return FALSE;

/* need to cache orig list address to remove from hash_table if empty */
mono_cominterop_lock ();
ccw_list = ccw_list_orig = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
mono_cominterop_unlock ();

if (!ccw_list)
return FALSE;

ccw_list_item = ccw_list;
while (ccw_list_item) {
MonoCCW* ccw_iter = ccw_list_item->data;
MonoObject* handle_target = mono_gchandle_get_target (ccw_iter->gc_handle);

/* Looks like the GC NULLs the weakref handle target before running the
* finalizer. So if we get a NULL target, destroy the CCW as well. */
if (!handle_target || handle_target == object) {
/* remove all interfaces */
g_hash_table_foreach_remove (ccw_iter->vtable_hash, mono_marshal_free_ccw_entry, NULL);
g_hash_table_destroy (ccw_iter->vtable_hash);

/* get next before we delete */
ccw_list_item = g_list_next(ccw_list_item);

/* remove ccw from list */
ccw_list = g_list_remove (ccw_list, ccw_iter);

#ifdef HOST_WIN32
if (ccw_iter->free_marshaler)
ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (ccw_iter->free_marshaler);
#endif

g_free (ccw_iter);
}
else
ccw_list_item = g_list_next(ccw_list_item);
}

/* if list is empty remove original address from hash */
if (g_list_length (ccw_list) == 0)
g_hash_table_remove (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));


return TRUE;
}

/**
* cominterop_get_managed_wrapper_adjusted:
* @method: managed COM Interop method
*
* Returns: the generated method to call with signature matching
* the unmanaged COM Method signature
*/
static MonoMethod *
cominterop_get_managed_wrapper_adjusted (MonoMethod *method)
{
static MonoMethod *get_hr_for_exception = NULL;
MonoMethod *res = NULL;
MonoMethodBuilder *mb;
MonoMarshalSpec **mspecs;
MonoMethodSignature *sig, *sig_native;
MonoExceptionClause *main_clause = NULL;
int pos_leave;
int hr = 0;
int i;
gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;

if (!get_hr_for_exception)
get_hr_for_exception = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetHRForException", -1);

sig = mono_method_signature (method);

/* create unmanaged wrapper */
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);

sig_native = cominterop_method_signature (method);

mspecs = g_new0 (MonoMarshalSpec*, sig_native->param_count+1);

mono_method_get_marshal_info (method, mspecs);

/* move managed args up one */
for (i = sig->param_count; i >= 1; i--)
mspecs [i+1] = mspecs [i];

/* first arg is IntPtr for interface */
mspecs [1] = NULL;

/* move return spec to last param */
if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
mspecs [sig_native->param_count] = mspecs [0];

mspecs [0] = NULL;

if (!preserve_sig) {
hr = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
}
else if (!MONO_TYPE_IS_VOID (sig->ret))
hr = mono_mb_add_local (mb, sig->ret);

/* try */
main_clause = g_new0 (MonoExceptionClause, 1);
main_clause->try_offset = mono_mb_get_label (mb);

/* load last param to store result if not preserve_sig and not void */
if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
mono_mb_emit_ldarg (mb, sig_native->param_count-1);

/* the CCW -> object conversion */
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_icon (mb, FALSE);
mono_mb_emit_icall (mb, cominterop_get_ccw_object);

for (i = 0; i < sig->param_count; i++)
mono_mb_emit_ldarg (mb, i+1);

mono_mb_emit_managed_call (mb, method, NULL);

if (!MONO_TYPE_IS_VOID (sig->ret)) {
if (!preserve_sig) {
MonoClass *rclass = mono_class_from_mono_type (sig->ret);
if (rclass->valuetype) {
mono_mb_emit_op (mb, CEE_STOBJ, rclass);
} else {
mono_mb_emit_byte (mb, mono_type_to_stind (sig->ret));
}
} else
mono_mb_emit_stloc (mb, hr);
}

pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);

/* Main exception catch */
main_clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
main_clause->try_len = mono_mb_get_pos (mb) - main_clause->try_offset;
main_clause->data.catch_class = mono_defaults.object_class;

/* handler code */
main_clause->handler_offset = mono_mb_get_label (mb);

if (!preserve_sig || (sig->ret && !sig->ret->byref && (sig->ret->type == MONO_TYPE_U4 || sig->ret->type == MONO_TYPE_I4))) {
mono_mb_emit_managed_call (mb, get_hr_for_exception, NULL);
mono_mb_emit_stloc (mb, hr);
}
else {
mono_mb_emit_byte (mb, CEE_POP);
}

mono_mb_emit_branch (mb, CEE_LEAVE);
main_clause->handler_len = mono_mb_get_pos (mb) - main_clause->handler_offset;
/* end catch */

mono_mb_set_clauses (mb, 1, main_clause);

mono_mb_patch_branch (mb, pos_leave);

if (!preserve_sig || !MONO_TYPE_IS_VOID (sig->ret))
mono_mb_emit_ldloc (mb, hr);

mono_mb_emit_byte (mb, CEE_RET);

mono_cominterop_lock ();
res = mono_mb_create_method (mb, sig_native, sig_native->param_count + 16);
mono_cominterop_unlock ();

mono_mb_free (mb);

for (i = sig_native->param_count; i >= 0; i--)
if (mspecs [i])
mono_metadata_free_marshal_spec (mspecs [i]);
g_free (mspecs);

return res;
}

/**
* cominterop_mono_string_to_guid:
*
* Converts the standard string representation of a GUID
* to a 16 byte Microsoft GUID.
*/
static void
cominterop_mono_string_to_guid (MonoString* string, guint8 *guid) {
gunichar2 * chars = mono_string_chars (string);
int i = 0;
static guint8 indexes[16] = {7, 5, 3, 1, 12, 10, 17, 15, 20, 22, 25, 27, 29, 31, 33, 35};

for (i = 0; i < sizeof(indexes); i++)
guid [i] = g_unichar_xdigit_value (chars [indexes [i]]) + (g_unichar_xdigit_value (chars [indexes [i] - 1]) << 4);
}

static gboolean
cominterop_class_guid_equal (guint8* guid, MonoClass* klass)
{
guint8 klass_guid [16];
if (cominterop_class_guid (klass, klass_guid))
return !memcmp (guid, klass_guid, sizeof (klass_guid));
return FALSE;
}

static int STDCALL
cominterop_ccw_addref (MonoCCWInterface* ccwe)
{
gint32 ref_count = 0;
MonoCCW* ccw = ccwe->ccw;
g_assert (ccw);
g_assert (ccw->gc_handle);
ref_count = InterlockedIncrement ((gint32*)&ccw->ref_count);
if (ref_count == 1) {
guint32 oldhandle = ccw->gc_handle;
g_assert (oldhandle);
/* since we now have a ref count, alloc a strong handle*/
ccw->gc_handle = mono_gchandle_new (mono_gchandle_get_target (oldhandle), FALSE);
mono_gchandle_free (oldhandle);
}
return ref_count;
}

static int STDCALL
cominterop_ccw_release (MonoCCWInterface* ccwe)
{
gint32 ref_count = 0;
MonoCCW* ccw = ccwe->ccw;
g_assert (ccw);
g_assert (ccw->ref_count > 0);
ref_count = InterlockedDecrement ((gint32*)&ccw->ref_count);
if (ref_count == 0) {
/* allow gc of object */
guint32 oldhandle = ccw->gc_handle;
g_assert (oldhandle);
ccw->gc_handle = mono_gchandle_new_weakref (mono_gchandle_get_target (oldhandle), FALSE);
mono_gchandle_free (oldhandle);
}
return ref_count;
}

#ifdef HOST_WIN32
static const IID MONO_IID_IMarshal = {0x3, 0x0, 0x0, {0xC0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46}};
#endif

#ifdef HOST_WIN32
/* All ccw objects are free threaded */
static int
cominterop_ccw_getfreethreadedmarshaler (MonoCCW* ccw, MonoObject* object, gpointer* ppv)
{
#ifdef HOST_WIN32
if (!ccw->free_marshaler) {
int ret = 0;
gpointer tunk;
tunk = cominterop_get_ccw (object, mono_class_get_iunknown_class ());
ret = CoCreateFreeThreadedMarshaler (tunk, (LPUNKNOWN*)&ccw->free_marshaler);
}

if (!ccw->free_marshaler)
return MONO_E_NOINTERFACE;

return ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (ccw->free_marshaler, (IID*)&MONO_IID_IMarshal, ppv);
#else
return MONO_E_NOINTERFACE;
#endif
}
#endif

static int STDCALL
cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv)
{
MonoError error;
GPtrArray *ifaces;
MonoClass *itf = NULL;
int i;
MonoCCW* ccw = ccwe->ccw;
MonoClass* klass = NULL;
MonoClass* klass_iter = NULL;
MonoObject* object = mono_gchandle_get_target (ccw->gc_handle);

g_assert (object);
klass = mono_object_class (object);

if (ppv)
*ppv = NULL;

if (!mono_domain_get ())
mono_thread_attach (mono_get_root_domain ());

/* handle IUnknown special */
if (cominterop_class_guid_equal (riid, mono_class_get_iunknown_class ())) {
*ppv = cominterop_get_ccw (object, mono_class_get_iunknown_class ());
/* remember to addref on QI */
cominterop_ccw_addref (*ppv);
return MONO_S_OK;
}

/* handle IDispatch special */
if (cominterop_class_guid_equal (riid, mono_class_get_idispatch_class ())) {
if (!cominterop_can_support_dispatch (klass))
return MONO_E_NOINTERFACE;

*ppv = cominterop_get_ccw (object, mono_class_get_idispatch_class ());
/* remember to addref on QI */
cominterop_ccw_addref (*ppv);
return MONO_S_OK;
}

#ifdef HOST_WIN32
/* handle IMarshal special */
if (0 == memcmp (riid, &MONO_IID_IMarshal, sizeof (IID))) {
return cominterop_ccw_getfreethreadedmarshaler (ccw, object, ppv);
}
#endif
klass_iter = klass;
while (klass_iter && klass_iter != mono_defaults.object_class) {
ifaces = mono_class_get_implemented_interfaces (klass_iter, &error);
g_assert (mono_error_ok (&error));
if (ifaces) {
for (i = 0; i < ifaces->len; ++i) {
MonoClass *ic = NULL;
ic = g_ptr_array_index (ifaces, i);
if (cominterop_class_guid_equal (riid, ic)) {
itf = ic;
break;
}
}
g_ptr_array_free (ifaces, TRUE);
}

if (itf)
break;

klass_iter = klass_iter->parent;
}
if (itf) {
*ppv = cominterop_get_ccw (object, itf);
/* remember to addref on QI */
cominterop_ccw_addref (*ppv);
return MONO_S_OK;
}

return MONO_E_NOINTERFACE;
}

static int STDCALL
cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo)
{
if(!pctinfo)
return MONO_E_INVALIDARG;

*pctinfo = 1;

return MONO_S_OK;
}

static int STDCALL
cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo)
{
return MONO_E_NOTIMPL;
}

static int STDCALL
cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
gunichar2** rgszNames, guint32 cNames,
guint32 lcid, gint32 *rgDispId)
{
static MonoClass *ComDispIdAttribute = NULL;
MonoCustomAttrInfo *cinfo = NULL;
int i,ret = MONO_S_OK;
MonoMethod* method;
gchar* methodname;
MonoClass *klass = NULL;
MonoCCW* ccw = ccwe->ccw;
MonoObject* object = mono_gchandle_get_target (ccw->gc_handle);

/* Handle DispIdAttribute */
if (!ComDispIdAttribute)
ComDispIdAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "DispIdAttribute");

g_assert (object);
klass = mono_object_class (object);

if (!mono_domain_get ())
mono_thread_attach (mono_get_root_domain ());

for (i=0; i < cNames; i++) {
methodname = mono_unicode_to_external (rgszNames[i]);

method = mono_class_get_method_from_name(klass, methodname, -1);
if (method) {
cinfo = mono_custom_attrs_from_method (method);
if (cinfo) {
MonoError error;
MonoObject *result = mono_custom_attrs_get_attr_checked (cinfo, ComDispIdAttribute, &error);
g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/;

if (result)
rgDispId[i] = *(gint32*)mono_object_unbox (result);
else
rgDispId[i] = (gint32)method->token;

if (!cinfo->cached)
mono_custom_attrs_free (cinfo);
}
else
rgDispId[i] = (gint32)method->token;
} else {
rgDispId[i] = MONO_E_DISPID_UNKNOWN;
ret = MONO_E_DISP_E_UNKNOWNNAME;
}
}

return ret;
}

static int STDCALL
cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
gpointer riid, guint32 lcid,
guint16 wFlags, gpointer pDispParams,
gpointer pVarResult, gpointer pExcepInfo,
guint32 *puArgErr)
{
return MONO_E_NOTIMPL;
}

typedef gpointer (STDCALL *SysAllocStringLenFunc)(gunichar* str, guint32 len);
typedef guint32 (STDCALL *SysStringLenFunc)(gpointer bstr);
typedef void (STDCALL *SysFreeStringFunc)(gunichar* str);

static SysAllocStringLenFunc sys_alloc_string_len_ms = NULL;
static SysStringLenFunc sys_string_len_ms = NULL;
static SysFreeStringFunc sys_free_string_ms = NULL;

#ifndef HOST_WIN32

typedef struct tagSAFEARRAYBOUND {
ULONG cElements;
LONG lLbound;
}SAFEARRAYBOUND,*LPSAFEARRAYBOUND;
#define VT_VARIANT 12

#endif

typedef guint32 (STDCALL *SafeArrayGetDimFunc)(gpointer psa);
typedef int (STDCALL *SafeArrayGetLBoundFunc)(gpointer psa, guint32 nDim, glong* plLbound);
typedef int (STDCALL *SafeArrayGetUBoundFunc)(gpointer psa, guint32 nDim, glong* plUbound);
typedef int (STDCALL *SafeArrayPtrOfIndexFunc)(gpointer psa, glong* rgIndices, gpointer* ppvData);
typedef int (STDCALL *SafeArrayDestroyFunc)(gpointer psa);
typedef int (STDCALL *SafeArrayPutElementFunc)(gpointer psa, glong* rgIndices, gpointer* ppvData);
typedef gpointer (STDCALL *SafeArrayCreateFunc)(int vt, guint32 cDims, SAFEARRAYBOUND* rgsabound);

static SafeArrayGetDimFunc safe_array_get_dim_ms = NULL;
static SafeArrayGetLBoundFunc safe_array_get_lbound_ms = NULL;
static SafeArrayGetUBoundFunc safe_array_get_ubound_ms = NULL;
static SafeArrayPtrOfIndexFunc safe_array_ptr_of_index_ms = NULL;
static SafeArrayDestroyFunc safe_array_destroy_ms = NULL;
static SafeArrayPutElementFunc safe_array_put_element_ms = NULL;
static SafeArrayCreateFunc safe_array_create_ms = NULL;

static gboolean
init_com_provider_ms (void)
{
static gboolean initialized = FALSE;
char *error_msg;
MonoDl *module = NULL;
const char* scope = "liboleaut32.so";

if (initialized)
return TRUE;

module = mono_dl_open(scope, MONO_DL_LAZY, &error_msg);
if (error_msg) {
g_warning ("Error loading COM support library '%s': %s", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}
error_msg = mono_dl_symbol (module, "SysAllocStringLen", (gpointer*)&sys_alloc_string_len_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysAllocStringLen", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SysStringLen", (gpointer*)&sys_string_len_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysStringLen", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SysFreeString", (gpointer*)&sys_free_string_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysFreeString", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SafeArrayGetDim", (gpointer*)&safe_array_get_dim_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayGetDim", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SafeArrayGetLBound", (gpointer*)&safe_array_get_lbound_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayGetLBound", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SafeArrayGetUBound", (gpointer*)&safe_array_get_ubound_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayGetUBound", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SafeArrayPtrOfIndex", (gpointer*)&safe_array_ptr_of_index_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayPtrOfIndex", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SafeArrayDestroy", (gpointer*)&safe_array_destroy_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayDestroy", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SafeArrayPutElement", (gpointer*)&safe_array_put_element_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayPutElement", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

error_msg = mono_dl_symbol (module, "SafeArrayCreate", (gpointer*)&safe_array_create_ms);
if (error_msg) {
g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayCreate", scope, error_msg);
g_assert_not_reached ();
return FALSE;
}

initialized = TRUE;
return TRUE;
}

gpointer
mono_string_to_bstr (MonoString *string_obj)
{
if (!string_obj)
return NULL;
#ifdef HOST_WIN32
return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
#else
if (com_provider == MONO_COM_DEFAULT) {
int slen = mono_string_length (string_obj);
/* allocate len + 1 utf16 characters plus 4 byte integer for length*/
char *ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
if (ret == NULL)
return NULL;
memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
* ((guint32 *) ret) = slen * sizeof(gunichar2);
ret [4 + slen * sizeof(gunichar2)] = 0;
ret [5 + slen * sizeof(gunichar2)] = 0;

return ret + 4;
} else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
gpointer ret = NULL;
gunichar* str = NULL;
guint32 len;
len = mono_string_length (string_obj);
str = g_utf16_to_ucs4 (mono_string_chars (string_obj), len,
NULL, NULL, NULL);
ret = sys_alloc_string_len_ms (str, len);
g_free(str);
return ret;
} else {
g_assert_not_reached ();
}
#endif
}

MonoString *
mono_string_from_bstr (gpointer bstr)
{
if (!bstr)
return NULL;
#ifdef HOST_WIN32
return mono_string_new_utf16 (mono_domain_get (), bstr, SysStringLen (bstr));
#else
if (com_provider == MONO_COM_DEFAULT) {
return mono_string_new_utf16 (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2));
} else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
MonoString* str = NULL;
glong written = 0;
gunichar2* utf16 = NULL;

utf16 = g_ucs4_to_utf16 (bstr, sys_string_len_ms (bstr), NULL, &written, NULL);
str = mono_string_new_utf16 (mono_domain_get (), utf16, written);
g_free (utf16);
return str;
} else {
g_assert_not_reached ();
}

#endif
}

void
mono_free_bstr (gpointer bstr)
{
if (!bstr)
return;
#ifdef HOST_WIN32
SysFreeString ((BSTR)bstr);
#else
if (com_provider == MONO_COM_DEFAULT) {
g_free (((char *)bstr) - 4);
} else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
sys_free_string_ms (bstr);
} else {
g_assert_not_reached ();
}

#endif
}


/* SAFEARRAY marshalling */
int
mono_cominterop_emit_marshal_safearray (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
MonoMethodBuilder *mb = m->mb;

switch (action) {

case MARSHAL_ACTION_CONV_IN: {

if (t->attrs & PARAM_ATTRIBUTE_IN) {

/* Generates IL code for the following algorithm:

SafeArray safearray; // safearray_var
IntPtr indices; // indices_var
int empty; // empty_var
if (mono_marshal_safearray_create (array, out safearray, out indices, out empty)) {
if (!empty) {
int index=0; // index_var
do { // label3
variant elem = Marshal.GetNativeVariantForObject (array.GetValueImpl(index));
mono_marshal_safearray_set_value (safearray, indices, elem);
++index;
}
while (mono_marshal_safearray_next (safearray, indices));
} // label2
mono_marshal_safearray_free_indices (indices);
} // label1
*/

int safearray_var, indices_var, empty_var, elem_var, index_var;
guint32 label1 = 0, label2 = 0, label3 = 0;
static MonoMethod *get_native_variant_for_object = NULL;
static MonoMethod *get_value_impl = NULL;
static MonoMethod *variant_clear = NULL;

conv_arg = safearray_var = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
indices_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
empty_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);

if (t->byref) {
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
} else
mono_mb_emit_ldarg (mb, argnum);

mono_mb_emit_ldloc_addr (mb, safearray_var);
mono_mb_emit_ldloc_addr (mb, indices_var);
mono_mb_emit_ldloc_addr (mb, empty_var);
mono_mb_emit_icall (mb, mono_marshal_safearray_create);

label1 = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

mono_mb_emit_ldloc (mb, empty_var);

label2 = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);

index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
mono_mb_emit_byte (mb, CEE_LDC_I4_0);
mono_mb_emit_stloc (mb, index_var);

label3 = mono_mb_get_label (mb);

if (!get_value_impl)
get_value_impl = mono_class_get_method_from_name (mono_defaults.array_class, "GetValueImpl", 1);
g_assert (get_value_impl);

if (t->byref) {
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
} else
mono_mb_emit_ldarg (mb, argnum);

mono_mb_emit_ldloc (mb, index_var);

mono_mb_emit_managed_call (mb, get_value_impl, NULL);

if (!get_native_variant_for_object)
get_native_variant_for_object = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetNativeVariantForObject", 2);
g_assert (get_native_variant_for_object);

elem_var = mono_mb_add_local (mb, &mono_class_get_variant_class ()->byval_arg);
mono_mb_emit_ldloc_addr (mb, elem_var);

mono_mb_emit_managed_call (mb, get_native_variant_for_object, NULL);

mono_mb_emit_ldloc (mb, safearray_var);
mono_mb_emit_ldloc (mb, indices_var);
mono_mb_emit_ldloc_addr (mb, elem_var);
mono_mb_emit_icall (mb, mono_marshal_safearray_set_value);

if (!variant_clear)
variant_clear = mono_class_get_method_from_name (mono_class_get_variant_class (), "Clear", 0);

mono_mb_emit_ldloc_addr (mb, elem_var);
mono_mb_emit_managed_call (mb, variant_clear, NULL);

mono_mb_emit_add_to_local (mb, index_var, 1);

mono_mb_emit_ldloc (mb, safearray_var);
mono_mb_emit_ldloc (mb, indices_var);
mono_mb_emit_icall (mb, mono_marshal_safearray_next);
mono_mb_emit_branch_label (mb, CEE_BRTRUE, label3);

mono_mb_patch_short_branch (mb, label2);

mono_mb_emit_ldloc (mb, indices_var);
mono_mb_emit_icall (mb, mono_marshal_safearray_free_indices);

mono_mb_patch_short_branch (mb, label1);
}
break;
}

case MARSHAL_ACTION_PUSH:
if (t->byref)
mono_mb_emit_ldloc_addr (mb, conv_arg);
else
mono_mb_emit_ldloc (mb, conv_arg);
break;

case MARSHAL_ACTION_CONV_OUT: {

if (t->attrs & PARAM_ATTRIBUTE_OUT) {
/* Generates IL code for the following algorithm:

Array result; // result_var
IntPtr indices; // indices_var
int empty; // empty_var
bool byValue = !t->byref && (t->attrs & PARAM_ATTRIBUTE_IN);
if (mono_marshal_safearray_begin(safearray, out result, out indices, out empty, parameter, byValue)) {
if (!empty) {
int index=0; // index_var
do { // label3
if (!byValue || (index < parameter.Length)) {
object elem = Variant.GetObjectForNativeVariant(mono_marshal_safearray_get_value(safearray, indices));
result.SetValueImpl(elem, index);
}
++index;
}
while (mono_marshal_safearray_next(safearray, indices));
} // label2
mono_marshal_safearray_end(safearray, indices);
} // label1
if (!byValue)
return result;
*/

int result_var, indices_var, empty_var, elem_var, index_var;
guint32 label1 = 0, label2 = 0, label3 = 0, label4 = 0;
static MonoMethod *get_object_for_native_variant = NULL;
static MonoMethod *set_value_impl = NULL;
gboolean byValue = !t->byref && (t->attrs & PARAM_ATTRIBUTE_IN);

result_var = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
indices_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
empty_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);

mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_ldloc_addr (mb, result_var);
mono_mb_emit_ldloc_addr (mb, indices_var);
mono_mb_emit_ldloc_addr (mb, empty_var);
mono_mb_emit_ldarg (mb, argnum);
if (byValue)
mono_mb_emit_byte (mb, CEE_LDC_I4_0);
else
mono_mb_emit_byte (mb, CEE_LDC_I4_1);
mono_mb_emit_icall (mb, mono_marshal_safearray_begin);

label1 = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);

mono_mb_emit_ldloc (mb, empty_var);

label2 = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);

index_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
mono_mb_emit_byte (mb, CEE_LDC_I4_0);
mono_mb_emit_stloc (mb, index_var);

label3 = mono_mb_get_label (mb);

if (byValue) {
mono_mb_emit_ldloc (mb, index_var);
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_byte (mb, CEE_LDLEN);
label4 = mono_mb_emit_branch (mb, CEE_BGE);
}

mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_ldloc (mb, indices_var);
mono_mb_emit_icall (mb, mono_marshal_safearray_get_value);

if (!get_object_for_native_variant)
get_object_for_native_variant = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForNativeVariant", 1);
g_assert (get_object_for_native_variant);

if (!set_value_impl)
set_value_impl = mono_class_get_method_from_name (mono_defaults.array_class, "SetValueImpl", 2);
g_assert (set_value_impl);

elem_var = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);

mono_mb_emit_managed_call (mb, get_object_for_native_variant, NULL);
mono_mb_emit_stloc (mb, elem_var);

mono_mb_emit_ldloc (mb, result_var);
mono_mb_emit_ldloc (mb, elem_var);
mono_mb_emit_ldloc (mb, index_var);
mono_mb_emit_managed_call (mb, set_value_impl, NULL);

if (byValue)
mono_mb_patch_short_branch (mb, label4);

mono_mb_emit_add_to_local (mb, index_var, 1);

mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_ldloc (mb, indices_var);
mono_mb_emit_icall (mb, mono_marshal_safearray_next);
mono_mb_emit_branch_label (mb, CEE_BRTRUE, label3);

mono_mb_patch_short_branch (mb, label2);

mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_ldloc (mb, indices_var);
mono_mb_emit_icall (mb, mono_marshal_safearray_end);

mono_mb_patch_short_branch (mb, label1);

if (!byValue) {
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_ldloc (mb, result_var);
mono_mb_emit_byte (mb, CEE_STIND_REF);
}
}
break;
}

default:
g_assert_not_reached ();
}

return conv_arg;
}

static
guint32 mono_marshal_safearray_get_dim (gpointer safearray)
{
guint32 result=0;
#ifdef HOST_WIN32
result = SafeArrayGetDim (safearray);
#else
if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
result = safe_array_get_dim_ms (safearray);
} else {
g_assert_not_reached ();
}
#endif
return result;
}

static
int mono_marshal_safe_array_get_lbound (gpointer psa, guint nDim, glong* plLbound)
{
int result=MONO_S_OK;
#ifdef HOST_WIN32
result = SafeArrayGetLBound (psa, nDim, plLbound);
#else
if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
result = safe_array_get_lbound_ms (psa, nDim, plLbound);
} else {
g_assert_not_reached ();
}
#endif
return result;
}

static
int mono_marshal_safe_array_get_ubound (gpointer psa, guint nDim, glong* plUbound)
{
int result=MONO_S_OK;
#ifdef HOST_WIN32
result = SafeArrayGetUBound (psa, nDim, plUbound);
#else
if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
result = safe_array_get_ubound_ms (psa, nDim, plUbound);
} else {
g_assert_not_reached ();
}
#endif
return result;
}

static gboolean
mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *indices, gpointer empty, gpointer parameter, gboolean allocateNewArray)
{
int dim;
uintptr_t *sizes;
intptr_t *bounds;
MonoClass *aklass;
int i;
gboolean bounded = FALSE;

#ifndef HOST_WIN32
// If not on windows, check that the MS provider is used as it is
// required for SAFEARRAY support.
// If SAFEARRAYs are not supported, returning FALSE from this
// function will prevent the other mono_marshal_safearray_xxx functions
// from being called.
if ((com_provider != MONO_COM_MS) || !init_com_provider_ms ()) {
return FALSE;
}
#endif

(*(int*)empty) = TRUE;

if (safearray != NULL) {

dim = mono_marshal_safearray_get_dim (safearray);

if (dim > 0) {

*indices = g_malloc (dim * sizeof(int));

sizes = alloca (dim * sizeof(uintptr_t));
bounds = alloca (dim * sizeof(intptr_t));

for (i=0; i<dim; ++i) {
glong lbound, ubound;
int cursize;
int hr;

hr = mono_marshal_safe_array_get_lbound (safearray, i+1, &lbound);
if (hr < 0) {
cominterop_raise_hr_exception (hr);
}
if (lbound != 0)
bounded = TRUE;
hr = mono_marshal_safe_array_get_ubound (safearray, i+1, &ubound);
if (hr < 0) {
cominterop_raise_hr_exception (hr);
}
cursize = ubound-lbound+1;
sizes [i] = cursize;
bounds [i] = lbound;

((int*)*indices) [i] = lbound;

if (cursize != 0)
(*(int*)empty) = FALSE;
}

if (allocateNewArray) {
aklass = mono_bounded_array_class_get (mono_defaults.object_class, dim, bounded);
*result = mono_array_new_full (mono_domain_get (), aklass, sizes, bounds);
} else {
*result = parameter;
}
}
}
return TRUE;
}

static
gpointer mono_marshal_safearray_get_value (gpointer safearray, gpointer indices)
{
gpointer result;
#ifdef HOST_WIN32
int hr = SafeArrayPtrOfIndex (safearray, indices, &result);
if (hr < 0) {
cominterop_raise_hr_exception (hr);
}
#else
if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
int hr = safe_array_ptr_of_index_ms (safearray, indices, &result);
if (hr < 0) {
cominterop_raise_hr_exception (hr);
}
} else {
g_assert_not_reached ();
}
#endif
return result;
}

static
gboolean mono_marshal_safearray_next (gpointer safearray, gpointer indices)
{
int i;
int dim = mono_marshal_safearray_get_dim (safearray);
gboolean ret= TRUE;
int *pIndices = (int*) indices;
int hr;

for (i=dim-1; i>=0; --i)
{
glong lbound, ubound;

hr = mono_marshal_safe_array_get_ubound (safearray, i+1, &ubound);
if (hr < 0) {
cominterop_raise_hr_exception (hr);
}

if (++pIndices[i] <= ubound) {
break;
}

hr = mono_marshal_safe_array_get_lbound (safearray, i+1, &lbound);
if (hr < 0) {
cominterop_raise_hr_exception (hr);
}

pIndices[i] = lbound;

if (i == 0)
ret = FALSE;
}
return ret;
}

static
void mono_marshal_safearray_end (gpointer safearray, gpointer indices)
{
g_free(indices);
#ifdef HOST_WIN32
SafeArrayDestroy (safearray);
#else
if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
safe_array_destroy_ms (safearray);
} else {
g_assert_not_reached ();
}
#endif
}

static gboolean
mono_marshal_safearray_create (MonoArray *input, gpointer *newsafearray, gpointer *indices, gpointer empty)
{
int dim;
SAFEARRAYBOUND *bounds;
int i;
int max_array_length;

#ifndef HOST_WIN32
// If not on windows, check that the MS provider is used as it is
// required for SAFEARRAY support.
// If SAFEARRAYs are not supported, returning FALSE from this
// function will prevent the other mono_marshal_safearray_xxx functions
// from being called.
if ((com_provider != MONO_COM_MS) || !init_com_provider_ms ()) {
return FALSE;
}
#endif

max_array_length = mono_array_length (input);
dim = ((MonoObject *)input)->vtable->klass->rank;

*indices = g_malloc (dim * sizeof (int));
bounds = alloca (dim * sizeof (SAFEARRAYBOUND));
(*(int*)empty) = (max_array_length == 0);

if (dim > 1) {
for (i=0; i<dim; ++i) {
((int*)*indices) [i] = bounds [i].lLbound = input->bounds [i].lower_bound;
bounds [i].cElements = input->bounds [i].length;
}
} else {
((int*)*indices) [0] = 0;
bounds [0].cElements = max_array_length;
bounds [0].lLbound = 0;
}

#ifdef HOST_WIN32
*newsafearray = SafeArrayCreate (VT_VARIANT, dim, bounds);
#else
*newsafearray = safe_array_create_ms (VT_VARIANT, dim, bounds);
#endif

return TRUE;
}

static
void mono_marshal_safearray_set_value (gpointer safearray, gpointer indices, gpointer value)
{
#ifdef HOST_WIN32
int hr = SafeArrayPutElement (safearray, indices, value);
if (hr < 0)
cominterop_raise_hr_exception (hr);
#else
if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
int hr = safe_array_put_element_ms (safearray, indices, value);
if (hr < 0) {
cominterop_raise_hr_exception (hr);
}
} else
g_assert_not_reached ();
#endif
}

static
void mono_marshal_safearray_free_indices (gpointer indices)
{
g_free (indices);
}

#else /* DISABLE_COM */

void
mono_cominterop_init (void)
{
/*FIXME
This icalls are used by the marshal code when doing PtrToStructure and StructureToPtr and pinvoke.

If we leave them out and the FullAOT compiler finds the need to emit one of the above 3 wrappers it will
g_assert.

The proper fix would be to emit warning, remove them from marshal.c when DISABLE_COM is used and
emit an exception in the generated IL.
*/
register_icall (mono_string_to_bstr, "mono_string_to_bstr", "ptr obj", FALSE);
register_icall (mono_string_from_bstr, "mono_string_from_bstr", "obj ptr", FALSE);
register_icall (mono_free_bstr, "mono_free_bstr", "void ptr", FALSE);
}

void
mono_cominterop_cleanup (void)
{
}

void
cominterop_release_all_rcws (void)
{
}

gboolean
mono_marshal_free_ccw (MonoObject* object)
{
return FALSE;
}

gpointer
mono_string_to_bstr (MonoString *string_obj)
{
if (!string_obj)
return NULL;
#ifdef HOST_WIN32
return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
#else
{
int slen = mono_string_length (string_obj);
/* allocate len + 1 utf16 characters plus 4 byte integer for length*/
char *ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
if (ret == NULL)
return NULL;
memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
* ((guint32 *) ret) = slen * sizeof(gunichar2);
ret [4 + slen * sizeof(gunichar2)] = 0;
ret [5 + slen * sizeof(gunichar2)] = 0;

return ret + 4;
}
#endif
}

MonoString *
mono_string_from_bstr (gpointer bstr)
{
if (!bstr)
return NULL;
#ifdef HOST_WIN32
return mono_string_new_utf16 (mono_domain_get (), bstr, SysStringLen (bstr));
#else
return mono_string_new_utf16 (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2));
#endif
}

void
mono_free_bstr (gpointer bstr)
{
if (!bstr)
return;
#ifdef HOST_WIN32
SysFreeString ((BSTR)bstr);
#else
g_free (((char *)bstr) - 4);
#endif
}

int
ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk)
{
g_assert_not_reached ();
return 0;
}

int
ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk)
{
g_assert_not_reached ();
return 0;
}

int
ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk, gpointer riid, gpointer* ppv)
{
g_assert_not_reached ();
return 0;
}

#endif /* DISABLE_COM */

MonoString *
ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr)
{
MONO_ARCH_SAVE_REGS;

return mono_string_from_bstr(ptr);
}

gpointer
ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString* ptr)
{
MONO_ARCH_SAVE_REGS;

return mono_string_to_bstr(ptr);
}

void
ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (gpointer ptr)
{
MONO_ARCH_SAVE_REGS;

mono_free_bstr (ptr);
}
Something went wrong with that request. Please try again.