File size: 126,316 Bytes
8c763fb | 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using Dbg = System.Diagnostics.Debug;
namespace System.Management.Automation.Remoting.Client
{
internal static class WSManNativeApi
{
internal const uint INFINITE = 0xFFFFFFFF;
internal const string PS_CREATION_XML_TAG = "creationXml";
internal const string PS_CONNECT_XML_TAG = "connectXml";
internal const string PS_CONNECTRESPONSE_XML_TAG = "connectResponseXml";
internal const string PS_XML_NAMESPACE = "http://schemas.microsoft.com/powershell";
internal const string WSMAN_STREAM_ID_STDOUT = "stdout";
internal const string WSMAN_STREAM_ID_PROMPTRESPONSE = "pr";
internal const string WSMAN_STREAM_ID_STDIN = "stdin";
internal const string ResourceURIPrefix = @"http://schemas.microsoft.com/powershell/";
internal const string NoProfile = "WINRS_NOPROFILE";
internal const string CodePage = "WINRS_CODEPAGE";
internal static readonly Version WSMAN_STACK_VERSION = new Version(3, 0);
internal const int WSMAN_FLAG_REQUESTED_API_VERSION_1_1 = 1;
// WSMan's default max env size in V2
internal const int WSMAN_DEFAULT_MAX_ENVELOPE_SIZE_KB_V2 = 150;
// WSMan's default max env size in V3
internal const int WSMAN_DEFAULT_MAX_ENVELOPE_SIZE_KB_V3 = 500;
#region WSMan errors
/// <summary>
/// The WinRM service cannot process the request because the request needs to be sent
/// to a different machine.
/// Use the redirect information to send the request to a new machine.
/// 0x8033819B from sdk\inc\wsmerror.h.
/// </summary>
internal const int ERROR_WSMAN_REDIRECT_REQUESTED = -2144108135;
/// <summary>
/// The WS-Management service cannot process the request. The resource URI is missing or
/// it has an incorrect format. Check the documentation or use the following command for
/// information on how to construct a resource URI: "winrm help uris".
/// </summary>
internal const int ERROR_WSMAN_INVALID_RESOURCE_URI = -2144108485;
/// <summary>
/// The WinRM service cannon re-connect the session because the session is no longer
/// associated with this transportmanager object.
/// </summary>
internal const int ERROR_WSMAN_INUSE_CANNOT_RECONNECT = -2144108083;
/// <summary>
/// Sending data to a remote command failed with the following error message: The client
/// cannot connect to the destination specified in the request. Verify that the service on
/// the destination is running and is accepting requests. Consult the logs and documentation
/// for the WS-Management service running on the destination, most commonly IIS or WinRM.
/// If the destination is the WinRM service, run the following command on the destination to
/// analyze and configure the WinRM service:
/// </summary>
internal const int ERROR_WSMAN_SENDDATA_CANNOT_CONNECT = -2144108526;
/// <summary>
/// Sending data to a remote command failed with the following error message: The WinRM client
/// cannot complete the operation within the time specified. Check if the machine name is valid
/// and is reachable over the network and firewall exception for Windows Remote Management service
/// is enabled.
/// </summary>
internal const int ERROR_WSMAN_SENDDATA_CANNOT_COMPLETE = -2144108250;
internal const int ERROR_WSMAN_ACCESS_DENIED = 5;
internal const int ERROR_WSMAN_OUTOF_MEMORY = 14;
internal const int ERROR_WSMAN_NETWORKPATH_NOTFOUND = 53;
internal const int ERROR_WSMAN_OPERATION_ABORTED = 995;
internal const int ERROR_WSMAN_SHUTDOWN_INPROGRESS = 1115;
internal const int ERROR_WSMAN_AUTHENTICATION_FAILED = 1311;
internal const int ERROR_WSMAN_NO_LOGON_SESSION_EXIST = 1312;
internal const int ERROR_WSMAN_LOGON_FAILURE = 1326;
internal const int ERROR_WSMAN_IMPROPER_RESPONSE = 1722;
internal const int ERROR_WSMAN_INCORRECT_PROTOCOLVERSION = -2141974624;
internal const int ERROR_WSMAN_URL_NOTAVAILABLE = -2144108269;
internal const int ERROR_WSMAN_INVALID_AUTHENTICATION = -2144108274;
internal const int ERROR_WSMAN_CANNOT_CONNECT_INVALID = -2144108080;
internal const int ERROR_WSMAN_CANNOT_CONNECT_MISMATCH = -2144108090;
internal const int ERROR_WSMAN_CANNOT_CONNECT_RUNASFAILED = -2144108065;
internal const int ERROR_WSMAN_CREATEFAILED_INVALIDNAME = -2144108094;
internal const int ERROR_WSMAN_TARGETSESSION_DOESNOTEXIST = -2144108453;
internal const int ERROR_WSMAN_REMOTESESSION_DISALLOWED = -2144108116;
internal const int ERROR_WSMAN_REMOTECONNECTION_DISALLOWED = -2144108061;
internal const int ERROR_WSMAN_INVALID_RESOURCE_URI2 = -2144108542;
internal const int ERROR_WSMAN_CORRUPTED_CONFIG = -2144108539;
internal const int ERROR_WSMAN_URI_LIMIT = -2144108499;
internal const int ERROR_WSMAN_CLIENT_KERBEROS_DISABLED = -2144108318;
internal const int ERROR_WSMAN_SERVER_NOTTRUSTED = -2144108316;
internal const int ERROR_WSMAN_WORKGROUP_NO_KERBEROS = -2144108276;
internal const int ERROR_WSMAN_EXPLICIT_CREDENTIALS_REQUIRED = -2144108315;
internal const int ERROR_WSMAN_REDIRECT_LOCATION_INVALID = -2144108105;
internal const int ERROR_WSMAN_BAD_METHOD = -2144108428;
internal const int ERROR_WSMAN_HTTP_SERVICE_UNAVAILABLE = -2144108270;
internal const int ERROR_WSMAN_HTTP_SERVICE_ERROR = -2144108176;
internal const int ERROR_WSMAN_COMPUTER_NOTFOUND = -2144108103;
internal const int ERROR_WSMAN_TARGET_UNKNOWN = -2146893053;
internal const int ERROR_WSMAN_CANNOTUSE_IP = -2144108101;
#endregion
#region MarshalledObject
/// <summary>
/// A struct holding marshalled data (IntPtr). This is
/// created to supply IDisposable pattern to safely
/// release the unmanaged pointer.
/// </summary>
internal struct MarshalledObject : IDisposable
{
private IntPtr _dataPtr;
/// <summary>
/// Constructs a MarshalledObject with the supplied
/// ptr.
/// </summary>
/// <param name="dataPtr"></param>
internal MarshalledObject(IntPtr dataPtr)
{
_dataPtr = dataPtr;
}
/// <summary>
/// Gets the unmanaged ptr.
/// </summary>
internal IntPtr DataPtr { get { return _dataPtr; } }
/// <summary>
/// Creates a MarshalledObject for the specified object.
/// </summary>
/// <typeparam name="T">
/// Must be a value type.
/// </typeparam>
/// <param name="obj"></param>
/// <returns>MarshalledObject.</returns>
internal static MarshalledObject Create<T>(T obj)
{
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf<T>());
Marshal.StructureToPtr(obj, ptr, false);
// Now create the MarshalledObject and return.
MarshalledObject result = new MarshalledObject();
result._dataPtr = ptr;
return result;
}
/// <summary>
/// Dispose the unmanaged IntPtr.
/// </summary>
public void Dispose()
{
if (_dataPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(_dataPtr);
_dataPtr = IntPtr.Zero;
}
}
/// <summary>
/// Implicit cast to IntPtr.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static implicit operator IntPtr(MarshalledObject obj)
{
return obj._dataPtr;
}
}
#endregion
#region WSMan_Authentication_Credentials
/// <summary>
/// Different Authentication Mechanisms supported by WSMan.
/// TODO: By the look of it, this appears like a Flags enum.
/// Need to confirm the behavior with WSMan.
/// </summary>
/// <remarks>
/// Please keep in sync with WSManAuthenticationMechanism
/// from C:\e\win7_powershell\admin\monad\nttargets\assemblies\logging\ETW\Manifests\Microsoft-Windows-PowerShell-Instrumentation.man
/// </remarks>
[Flags]
internal enum WSManAuthenticationMechanism : int
{
/// <summary>
/// Use the default authentication.
/// </summary>
WSMAN_FLAG_DEFAULT_AUTHENTICATION = 0x0,
/// <summary>
/// Use no authentication for a remote operation.
/// </summary>
WSMAN_FLAG_NO_AUTHENTICATION = 0x1,
/// <summary>
/// Use digest authentication for a remote operation.
/// </summary>
WSMAN_FLAG_AUTH_DIGEST = 0x2,
/// <summary>
/// Use negotiate authentication for a remote operation (may use kerberos or ntlm)
/// </summary>
WSMAN_FLAG_AUTH_NEGOTIATE = 0x4,
/// <summary>
/// Use basic authentication for a remote operation.
/// </summary>
WSMAN_FLAG_AUTH_BASIC = 0x8,
/// <summary>
/// Use kerberos authentication for a remote operation.
/// </summary>
WSMAN_FLAG_AUTH_KERBEROS = 0x10,
/// <summary>
/// Use client certificate authentication for a remote operation.
/// </summary>
WSMAN_FLAG_AUTH_CLIENT_CERTIFICATE = 0x20,
/// <summary>
/// Use CredSSP authentication for a remote operation.
/// </summary>
WSMAN_FLAG_AUTH_CREDSSP = 0x80,
}
/// <summary>
/// This is used to represent _WSMAN_AUTHENTICATION_CREDENTIALS
/// native structure. _WSMAN_AUTHENTICATION_CREDENTIALS has a union
/// member which cannot be easily represented in managed code.
/// So created an interface and each union member is represented
/// with a different structure.
/// </summary>
internal abstract class BaseWSManAuthenticationCredentials : IDisposable
{
// used to get Marshalled data of the class.
public abstract MarshalledObject GetMarshalledObject();
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool isDisposing)
{
}
}
/// <summary>
/// Used to supply _WSMAN_USERNAME_PASSWORD_CREDS type credentials for
/// WSManCreateSession.
/// </summary>
internal class WSManUserNameAuthenticationCredentials : BaseWSManAuthenticationCredentials
{
/// <summary>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct WSManUserNameCredentialStruct
{
/// <summary>
/// </summary>
internal WSManAuthenticationMechanism authenticationMechanism;
/// <summary>
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string userName;
/// <summary>
/// Making password secure.
/// </summary>
internal IntPtr password;
}
private WSManUserNameCredentialStruct _cred;
private MarshalledObject _data;
/// <summary>
/// Default constructor.
/// </summary>
internal WSManUserNameAuthenticationCredentials()
{
_cred = new WSManUserNameCredentialStruct();
_data = MarshalledObject.Create<WSManUserNameCredentialStruct>(_cred);
}
/// <summary>
/// Constructs an WSManUserNameAuthenticationCredentials object.
/// It is upto the caller to verify if <paramref name="name"/>
/// and <paramref name="pwd"/> are valid. This API wont complain
/// if they are Empty or Null.
/// </summary>
/// <param name="name">
/// user name.
/// </param>
/// <param name="pwd">
/// password.
/// </param>
/// <param name="authMechanism">
/// can be 0 (the user did not specify an authentication mechanism,
/// WSMan client will choose between Kerberos and Negotiate only);
/// if it is not 0, it must be one of the values from
/// WSManAuthenticationMechanism enumeration.
/// </param>
internal WSManUserNameAuthenticationCredentials(string name,
System.Security.SecureString pwd, WSManAuthenticationMechanism authMechanism)
{
_cred = new WSManUserNameCredentialStruct();
_cred.authenticationMechanism = authMechanism;
_cred.userName = name;
if (pwd != null)
{
_cred.password = Marshal.SecureStringToCoTaskMemUnicode(pwd);
}
_data = MarshalledObject.Create<WSManUserNameCredentialStruct>(_cred);
}
/// <summary>
/// Gets a structure representation (used for marshalling)
/// </summary>
internal WSManUserNameCredentialStruct CredentialStruct
{
get { return _cred; }
}
/// <summary>
/// Marshalled Data.
/// </summary>
/// <returns></returns>
public override MarshalledObject GetMarshalledObject()
{
return _data;
}
/// <summary>
/// Dispose of the resources.
/// </summary>
/// <param name="isDisposing"></param>
protected override void Dispose(bool isDisposing)
{
if (_cred.password != IntPtr.Zero)
{
Marshal.ZeroFreeCoTaskMemUnicode(_cred.password);
_cred.password = IntPtr.Zero;
}
_data.Dispose();
}
}
/// <summary>
/// </summary>
internal class WSManCertificateThumbprintCredentials : BaseWSManAuthenticationCredentials
{
/// <summary>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct WSManThumbprintStruct
{
/// <summary>
/// </summary>
internal WSManAuthenticationMechanism authenticationMechanism;
/// <summary>
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string certificateThumbprint;
/// <summary>
/// This is provided for padding as underlying WSMan's implementation
/// uses a union, we need to pad up unused fields.
/// </summary>
internal IntPtr reserved;
}
private MarshalledObject _data;
/// <summary>
/// Constructs an WSManCertificateThumbprintCredentials object.
/// It is upto the caller to verify if <paramref name="thumbPrint"/>
/// is valid. This API wont complain if it is Empty or Null.
/// </summary>
/// <param name="thumbPrint"></param>
internal WSManCertificateThumbprintCredentials(string thumbPrint)
{
WSManThumbprintStruct cred = new WSManThumbprintStruct();
cred.authenticationMechanism = WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_CLIENT_CERTIFICATE;
cred.certificateThumbprint = thumbPrint;
cred.reserved = IntPtr.Zero;
_data = MarshalledObject.Create<WSManThumbprintStruct>(cred);
}
/// <summary>
/// Marshalled Data.
/// </summary>
/// <returns></returns>
public override MarshalledObject GetMarshalledObject()
{
return _data;
}
/// <summary>
/// Dispose of the resources.
/// </summary>
/// <param name="isDisposing"></param>
protected override void Dispose(bool isDisposing)
{
// data is of struct type..so there is no need to set it to null..
_data.Dispose();
}
}
#endregion
#region WSMan Session Options
/// <summary>
/// Enum representing native WSManSessionOption enum.
/// </summary>
internal enum WSManSessionOption : int
{
#region TimeOuts
/// <summary>
/// Int - default timeout in ms that applies to all operations on the client side.
/// </summary>
WSMAN_OPTION_DEFAULT_OPERATION_TIMEOUTMS = 1,
/// <summary>
/// Int - Robust connection maximum retry time in minutes.
/// </summary>
WSMAN_OPTION_MAX_RETRY_TIME = 11,
/// <summary>
/// Int - timeout in ms for WSManCreateShellEx operations.
/// </summary>
WSMAN_OPTION_TIMEOUTMS_CREATE_SHELL = 12,
/// <summary>
/// Int - timeout in ms for WSManReceiveShellOutputEx operations.
/// </summary>
WSMAN_OPTION_TIMEOUTMS_RECEIVE_SHELL_OUTPUT = 14,
/// <summary>
/// Int - timeout in ms for WSManSendShellInputEx operations.
/// </summary>
WSMAN_OPTION_TIMEOUTMS_SEND_SHELL_INPUT = 15,
/// <summary>
/// Int - timeout in ms for WSManSignalShellEx operations.
/// </summary>
WSMAN_OPTION_TIMEOUTMS_SIGNAL_SHELL = 16,
/// <summary>
/// Int - timeout in ms for WSManCloseShellOperationEx operations.
/// </summary>
WSMAN_OPTION_TIMEOUTMS_CLOSE_SHELL_OPERATION = 17,
#endregion
#region Connection Options
/// <summary>
/// Int - 1 to not validate the CA on the server certificate; 0 - default.
/// </summary>
WSMAN_OPTION_SKIP_CA_CHECK = 18,
/// <summary>
/// Int - 1 to not validate the CN on the server certificate; 0 - default.
/// </summary>
WSMAN_OPTION_SKIP_CN_CHECK = 19,
/// <summary>
/// Int - 1 to not encrypt the messages; 0 - default.
/// </summary>
WSMAN_OPTION_UNENCRYPTED_MESSAGES = 20,
/// <summary>
/// Int - 1 Send all network packets for remote operations in UTF16; 0 - default is UTF8.
/// </summary>
WSMAN_OPTION_UTF16 = 21,
/// <summary>
/// Int - 1 When using negotiate, include port number in the connection SPN; 0 - default.
/// </summary>
WSMAN_OPTION_ENABLE_SPN_SERVER_PORT = 22,
/// <summary>
/// Int - Used when not talking to the main OS on a machine but, for instance, a BMC
/// 1 Identify this machine to the server by including the MachineID header; 0 - default.
/// </summary>
WSMAN_OPTION_MACHINE_ID = 23,
/// <summary>
/// Int -1 Enables host process to be created with interactive token.
/// </summary>
WSMAN_OPTION_USE_INTERACTIVE_TOKEN = 34,
#endregion
#region Locale
/// <summary>
/// String - RFC 3066 language code.
/// </summary>
WSMAN_OPTION_LOCALE = 25,
/// <summary>
/// String - RFC 3066 language code.
/// </summary>
WSMAN_OPTION_UI_LANGUAGE = 26,
#endregion
#region Other
/// <summary>
/// Int - max SOAP envelope size (kb) - default 150kb from winrm config
/// (see 'winrm help config' for more details); the client SOAP packet size cannot surpass
/// this value; this value will be also sent to the server in the SOAP request as a
/// MaxEnvelopeSize header; the server will use min(MaxEnvelopeSizeKb from server configuration,
/// MaxEnvelopeSize value from SOAP).
/// </summary>
WSMAN_OPTION_MAX_ENVELOPE_SIZE_KB = 28,
/// <summary>
/// Int (read only) - max data size (kb) provided by the client, guaranteed by
/// the winrm client implementation to fit into one SOAP packet; this is an
/// approximate value calculated based on the WSMAN_OPTION_MAX_ENVELOPE_SIZE_KB (default 150kb),
/// the maximum possible size of the SOAP headers and the overhead of the base64
/// encoding which is specific to WSManSendShellInput API; this option can be used
/// with WSManGetSessionOptionAsDword API; it cannot be used with WSManSetSessionOption API.
/// </summary>
WSMAN_OPTION_SHELL_MAX_DATA_SIZE_PER_MESSAGE_KB = 29,
/// <summary>
/// String -
/// </summary>
WSMAN_OPTION_REDIRECT_LOCATION = 30,
/// <summary>
/// DWORD - 1 to not validate the revocation status on the server certificate; 0 - default.
/// </summary>
WSMAN_OPTION_SKIP_REVOCATION_CHECK = 31,
/// <summary>
/// DWORD - 1 to allow default credentials for Negotiate (this is for SSL only); 0 - default.
/// </summary>
WSMAN_OPTION_ALLOW_NEGOTIATE_IMPLICIT_CREDENTIALS = 32,
/// <summary>
/// DWORD - When using just a machine name in the connection string use an SSL connection.
/// 0 means HTTP, 1 means HTTPS. Default is 0.
/// </summary>
WSMAN_OPTION_USE_SSL = 33
#endregion
}
/// <summary>
/// Enum representing WSMan Shell specific options.
/// </summary>
internal enum WSManShellFlag : int
{
/// <summary>
/// Turn off compression for Send/Receive operations. By default compression is
/// turned on, but if communicating with a down-level box it may be necessary to
/// do this. Other reasons for turning it off is due to the extra memory consumption
/// and CPU utilization that is used as a result of compression.
/// </summary>
WSMAN_FLAG_NO_COMPRESSION = 1,
/// <summary>
/// Enable the service to drop operation output when running disconnected.
/// </summary>
WSMAN_FLAG_SERVER_BUFFERING_MODE_DROP = 0x4,
/// <summary>
/// Enable the service to block operation progress when output buffers are full.
/// </summary>
WSMAN_FLAG_SERVER_BUFFERING_MODE_BLOCK = 0x8,
/// <summary>
/// Enable receive call to not immediately retrieve results. Only applicable for Receive calls on commands.
/// </summary>
WSMAN_FLAG_RECEIVE_DELAY_OUTPUT_STREAM = 0X10
}
#endregion
#region WSManData
/// <summary>
/// Types of supported WSMan data.
/// PowerShell uses only Text and DWORD (in some places).
/// </summary>
internal enum WSManDataType : uint
{
WSMAN_DATA_NONE = 0,
WSMAN_DATA_TYPE_TEXT = 1,
WSMAN_DATA_TYPE_BINARY = 2,
WSMAN_DATA_TYPE_WS_XML_READER = 3,
WSMAN_DATA_TYPE_DWORD = 4
}
[StructLayout(LayoutKind.Sequential)]
internal class WSManDataStruct
{
internal uint type;
internal WSManBinaryOrTextDataStruct binaryOrTextData;
}
[StructLayout(LayoutKind.Sequential)]
internal class WSManBinaryOrTextDataStruct
{
internal int bufferLength;
internal IntPtr data;
}
/// <summary>
/// Used to supply WSMAN_DATA_BINARY/WSMAN_DATA_TEXT type in place of _WSMAN_DATA.
/// </summary>
internal class WSManData_ManToUn : IDisposable
{
private readonly WSManDataStruct _internalData;
private IntPtr _marshalledObject = IntPtr.Zero;
private IntPtr _marshalledBuffer = IntPtr.Zero;
/// <summary>
/// Constructs a WSMAN_DATA_BINARY object. This is used to send
/// data to remote end.
/// </summary>
/// <param name="data"></param>
internal WSManData_ManToUn(byte[] data)
{
Dbg.Assert(data != null, "Data cannot be null");
_internalData = new WSManDataStruct();
_internalData.binaryOrTextData = new WSManBinaryOrTextDataStruct();
_internalData.binaryOrTextData.bufferLength = data.Length;
_internalData.type = (uint)WSManDataType.WSMAN_DATA_TYPE_BINARY;
IntPtr dataToSendPtr = Marshal.AllocHGlobal(_internalData.binaryOrTextData.bufferLength);
_internalData.binaryOrTextData.data = dataToSendPtr;
_marshalledBuffer = dataToSendPtr; // Stored directly to enable graceful clean up during finalizer scenarios
Marshal.Copy(data, 0, _internalData.binaryOrTextData.data, _internalData.binaryOrTextData.bufferLength);
_marshalledObject = Marshal.AllocHGlobal(Marshal.SizeOf<WSManDataStruct>());
Marshal.StructureToPtr(_internalData, _marshalledObject, false);
}
/// <summary>
/// Constructs a WSMAN_DATA_TEXT object. This is used to send data
/// to remote end.
/// </summary>
/// <param name="data"></param>
internal WSManData_ManToUn(string data)
{
Dbg.Assert(data != null, "Data cannot be null");
_internalData = new WSManDataStruct();
_internalData.binaryOrTextData = new WSManBinaryOrTextDataStruct();
_internalData.binaryOrTextData.bufferLength = data.Length;
_internalData.type = (uint)WSManDataType.WSMAN_DATA_TYPE_TEXT;
// marshal text data
_internalData.binaryOrTextData.data = Marshal.StringToHGlobalUni(data);
_marshalledBuffer = _internalData.binaryOrTextData.data; // Stored directly to enable graceful clean up during finalizer scenarios
_marshalledObject = Marshal.AllocHGlobal(Marshal.SizeOf<WSManDataStruct>());
Marshal.StructureToPtr(_internalData, _marshalledObject, false);
}
/// <summary>
/// Gets the type of data.
/// </summary>
internal uint Type
{
get { return _internalData.type; }
set { _internalData.type = value; }
}
/// <summary>
/// Gets the buffer length of data.
/// </summary>
internal int BufferLength
{
get { return _internalData.binaryOrTextData.bufferLength; }
set { _internalData.binaryOrTextData.bufferLength = value; }
}
/// <summary>
/// Free unmanaged resources. All users of this class should call Dispose rather than
/// depending on the finalizer to clean it up.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool isDisposing)
{
// Managed objects should not be deleted when this is called via the finalizer
// because they may have been collected already. To prevent leaking the marshalledBuffer
// pointer, we are storing its value as a private member of the class, just like marshalledObject.
if (_marshalledBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(_marshalledBuffer);
_marshalledBuffer = IntPtr.Zero;
}
if (_marshalledObject != IntPtr.Zero)
{
Marshal.FreeHGlobal(_marshalledObject);
_marshalledObject = IntPtr.Zero;
}
}
/// <summary>
/// Finalizes an instance of the <see cref="WSManData_ManToUn"/> class.
/// </summary>
~WSManData_ManToUn()
{
Dispose(false);
}
/// <summary>
/// Implicit IntPtr conversion.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static implicit operator IntPtr(WSManData_ManToUn data)
{
if (data != null)
{
return data._marshalledObject;
}
else
{
return IntPtr.Zero;
}
}
}
internal class WSManData_UnToMan
{
/// <summary>
/// Gets the type of data.
/// </summary>
private uint _type;
internal uint Type
{
get { return _type; }
set { _type = value; }
}
/// <summary>
/// Gets the buffer length of data.
/// </summary>
private int _bufferLength;
internal int BufferLength
{
get { return _bufferLength; }
set { _bufferLength = value; }
}
private string _text;
internal string Text
{
get
{
if (this.Type == (uint)WSManDataType.WSMAN_DATA_TYPE_TEXT)
return _text;
else
return string.Empty;
}
}
private byte[] _data;
internal byte[] Data
{
get
{
if (this.Type == (uint)WSManDataType.WSMAN_DATA_TYPE_BINARY)
return _data;
else
return Array.Empty<byte>();
}
}
/// <summary>
/// Converts the unmanaged structure to a managed class object.
/// </summary>
/// <param name="dataStruct"></param>
/// <returns></returns>
internal static WSManData_UnToMan UnMarshal(WSManDataStruct dataStruct)
{
WSManData_UnToMan newData = new WSManData_UnToMan();
newData._type = dataStruct.type;
newData._bufferLength = dataStruct.binaryOrTextData.bufferLength;
switch (dataStruct.type)
{
case (uint)WSManNativeApi.WSManDataType.WSMAN_DATA_TYPE_TEXT:
if (dataStruct.binaryOrTextData.bufferLength > 0)
{
string tempText = Marshal.PtrToStringUni(dataStruct.binaryOrTextData.data, dataStruct.binaryOrTextData.bufferLength);
newData._text = tempText;
}
break;
case (uint)WSManNativeApi.WSManDataType.WSMAN_DATA_TYPE_BINARY:
if (dataStruct.binaryOrTextData.bufferLength > 0)
{
// copy data from unmanaged heap to managed heap.
byte[] dataRecvd = new byte[dataStruct.binaryOrTextData.bufferLength];
Marshal.Copy(
dataStruct.binaryOrTextData.data,
dataRecvd,
0,
dataStruct.binaryOrTextData.bufferLength);
newData._data = dataRecvd;
}
break;
default:
throw new NotSupportedException();
}
return newData;
}
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManData_UnToMan UnMarshal(IntPtr unmanagedData)
{
WSManData_UnToMan result = null;
if (unmanagedData != IntPtr.Zero)
{
WSManDataStruct resultInternal = Marshal.PtrToStructure<WSManDataStruct>(unmanagedData);
result = WSManData_UnToMan.UnMarshal(resultInternal);
}
return result;
}
}
/// <summary>
/// Used to supply a DWORD data in place of _WSMAN_DATA.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct WSManDataDWord
{
private readonly WSManDataType _type;
private WSManDWordDataInternal _dwordData;
/// <summary>
/// Constructs a WSMAN_DATA_DWORD object.
/// </summary>
/// <param name="data"></param>
internal WSManDataDWord(int data)
{
_dwordData = new WSManDWordDataInternal();
_dwordData.number = data;
_type = WSManDataType.WSMAN_DATA_TYPE_DWORD;
}
/// <summary>
/// Creates an unmanaged ptr which holds the class data.
/// This unmanaged ptr can be used with WSMan native API.
/// </summary>
/// <returns></returns>
internal MarshalledObject Marshal()
{
return MarshalledObject.Create<WSManDataDWord>(this);
}
/// <summary>
/// This struct is created to honor struct boundaries between
/// x86,amd64 and ia64. WSMan defines a generic WSMAN_DATA
/// structure that addresses DWORD, binary, text data.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct WSManDWordDataInternal
{
internal int number;
internal IntPtr reserved;
}
}
#endregion
#region WSManShellStartupInfo / WSManOptionSet / WSManCommandArgSet / WSManProxyInfo
/// <summary>
/// WSMan allows multiple streams within a shell but powershell is
/// using only 1 stream for input and 1 stream for output to allow
/// sequencing of data. Because of this the following structure will
/// have only one string to hold stream information.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManStreamIDSetStruct
{
internal int streamIDsCount;
internal IntPtr streamIDs;
}
internal struct WSManStreamIDSet_ManToUn
{
private WSManStreamIDSetStruct _streamSetInfo;
private MarshalledObject _data;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="streamIds"></param>
internal WSManStreamIDSet_ManToUn(string[] streamIds)
{
Dbg.Assert(streamIds != null, "stream ids cannot be null or empty");
int sizeOfIntPtr = Marshal.SizeOf<IntPtr>();
_streamSetInfo = new WSManStreamIDSetStruct();
_streamSetInfo.streamIDsCount = streamIds.Length;
_streamSetInfo.streamIDs = Marshal.AllocHGlobal(sizeOfIntPtr * streamIds.Length);
for (int index = 0; index < streamIds.Length; index++)
{
IntPtr streamAddress = Marshal.StringToHGlobalUni(streamIds[index]);
Marshal.WriteIntPtr(_streamSetInfo.streamIDs, index * sizeOfIntPtr, streamAddress);
}
_data = MarshalledObject.Create<WSManStreamIDSetStruct>(_streamSetInfo);
}
/// <summary>
/// Free resources.
/// </summary>
internal void Dispose()
{
if (_streamSetInfo.streamIDs != IntPtr.Zero)
{
int sizeOfIntPtr = Marshal.SizeOf<IntPtr>();
for (int index = 0; index < _streamSetInfo.streamIDsCount; index++)
{
IntPtr streamAddress = IntPtr.Zero;
streamAddress = Marshal.ReadIntPtr(_streamSetInfo.streamIDs, index * sizeOfIntPtr);
if (streamAddress != IntPtr.Zero)
{
Marshal.FreeHGlobal(streamAddress);
streamAddress = IntPtr.Zero;
}
}
Marshal.FreeHGlobal(_streamSetInfo.streamIDs);
_streamSetInfo.streamIDs = IntPtr.Zero;
}
_data.Dispose();
}
/// <summary>
/// Implicit cast to IntPtr.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static implicit operator IntPtr(WSManStreamIDSet_ManToUn obj)
{
return obj._data.DataPtr;
}
}
internal class WSManStreamIDSet_UnToMan
{
internal string[] streamIDs;
internal int streamIDsCount;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManStreamIDSet_UnToMan UnMarshal(IntPtr unmanagedData)
{
WSManStreamIDSet_UnToMan result = null;
if (unmanagedData != IntPtr.Zero)
{
WSManStreamIDSetStruct resultInternal = Marshal.PtrToStructure<WSManStreamIDSetStruct>(unmanagedData);
result = new WSManStreamIDSet_UnToMan();
string[] idsArray = null;
if (resultInternal.streamIDsCount > 0)
{
idsArray = new string[resultInternal.streamIDsCount];
IntPtr[] ptrs = new IntPtr[resultInternal.streamIDsCount];
Marshal.Copy(resultInternal.streamIDs, ptrs, 0, resultInternal.streamIDsCount); // Marshal the array of string pointers
for (int i = 0; i < resultInternal.streamIDsCount; i++)
{
idsArray[i] = Marshal.PtrToStringUni(ptrs[i]); // Marshal the string pointers into strings
}
/*
* // TODO: Why didn't this work? It looks more efficient
idsArray = new string[resultInternal.streamIDsCount];
int sizeInBytes = Marshal.SizeOf<IntPtr>();
IntPtr perElementPtr = resultInternal.streamIDs;
for (int i = 0; i < resultInternal.streamIDsCount; i++)
{
IntPtr p = IntPtr.Add(perElementPtr, (i * sizeInBytes));
idsArray[i] = Marshal.PtrToStringUni(p);
}
*/
}
result.streamIDs = idsArray;
result.streamIDsCount = resultInternal.streamIDsCount;
}
return result;
}
}
/// <summary>
/// Managed to Unmanaged: Option struct used to pass optional information with WSManCreateShellEx .
/// Unmanaged to Managed: Included in WSManPluginRequest.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManOption
{
/// <summary>
/// Underlying type = PCWSTR.
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string name;
/// <summary>
/// Underlying type = PCWSTR.
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string value;
/// <summary>
/// Underlying type = BOOL.
/// </summary>
internal bool mustComply;
}
/// <summary>
/// Unmanaged to Managed: WSMAN_OPERATION_INFO includes the struct directly, so this cannot be made internal to WsmanOptionSet.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManOptionSetStruct
{
internal int optionsCount;
/// <summary>
/// Pointer to an array of WSManOption objects.
/// </summary>
internal IntPtr options;
internal bool optionsMustUnderstand;
}
/// <summary>
/// Option set struct used to pass optional information
/// with WSManCreateShellEx.
/// </summary>
internal struct WSManOptionSet : IDisposable
{
#region Managed to Unmanaged
private WSManOptionSetStruct _optionSet;
private MarshalledObject _data;
/// <summary>
/// Options to construct this OptionSet with.
/// </summary>
/// <param name="options"></param>
internal WSManOptionSet(WSManOption[] options)
{
Dbg.Assert(options != null, "options cannot be null");
int sizeOfOption = Marshal.SizeOf<WSManOption>();
_optionSet = new WSManOptionSetStruct();
_optionSet.optionsCount = options.Length;
_optionSet.optionsMustUnderstand = true;
_optionSet.options = Marshal.AllocHGlobal(sizeOfOption * options.Length);
for (int index = 0; index < options.Length; index++)
{
// Look at the structure of native WSManOptionSet.. Options is a pointer..
// In C-Style array individual elements are continuous..so I am building
// continuous array elements here.
Marshal.StructureToPtr(options[index], (IntPtr)(_optionSet.options.ToInt64() + (sizeOfOption * index)), false);
}
_data = MarshalledObject.Create<WSManOptionSetStruct>(_optionSet);
// For conformity:
this.optionsCount = 0;
this.options = null;
this.optionsMustUnderstand = false;
}
/// <summary>
/// </summary>
public void Dispose()
{
if (_optionSet.options != IntPtr.Zero)
{
Marshal.FreeHGlobal(_optionSet.options);
_optionSet.options = IntPtr.Zero;
}
// dispose option set
_data.Dispose();
}
/// <summary>
/// Implicit IntPtr cast.
/// </summary>
/// <param name="optionSet"></param>
/// <returns></returns>
public static implicit operator IntPtr(WSManOptionSet optionSet)
{
return optionSet._data.DataPtr;
}
#endregion
#region Unmanaged to Managed
internal int optionsCount;
internal WSManOption[] options;
internal bool optionsMustUnderstand;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManOptionSet UnMarshal(IntPtr unmanagedData)
{
if (unmanagedData == IntPtr.Zero)
{
return new WSManOptionSet();
}
else
{
WSManOptionSetStruct resultInternal = Marshal.PtrToStructure<WSManOptionSetStruct>(unmanagedData);
return UnMarshal(resultInternal);
}
}
/// <summary>
/// Converts the unmanaged structure to a managed class object.
/// </summary>
/// <param name="resultInternal"></param>
/// <returns></returns>
internal static WSManOptionSet UnMarshal(WSManOptionSetStruct resultInternal)
{
WSManOption[] tempOptions = null;
if (resultInternal.optionsCount > 0)
{
tempOptions = new WSManOption[resultInternal.optionsCount];
int sizeInBytes = Marshal.SizeOf<WSManOption>();
IntPtr perElementPtr = resultInternal.options;
for (int i = 0; i < resultInternal.optionsCount; i++)
{
IntPtr p = IntPtr.Add(perElementPtr, (i * sizeInBytes));
tempOptions[i] = Marshal.PtrToStructure<WSManOption>(p);
}
}
WSManOptionSet result = new WSManOptionSet();
result.optionsCount = resultInternal.optionsCount;
result.options = tempOptions;
result.optionsMustUnderstand = resultInternal.optionsMustUnderstand;
return result;
}
#endregion
}
/// <summary>
/// </summary>
internal struct WSManCommandArgSet : IDisposable
{
[StructLayout(LayoutKind.Sequential)]
internal struct WSManCommandArgSetInternal
{
internal int argsCount;
internal IntPtr args;
}
private WSManCommandArgSetInternal _internalData;
private MarshalledObject _data;
#region Managed to Unmanaged
internal WSManCommandArgSet(byte[] firstArgument)
{
_internalData = new WSManCommandArgSetInternal();
_internalData.argsCount = 1;
_internalData.args = Marshal.AllocHGlobal(Marshal.SizeOf<IntPtr>());
// argument set takes only strings..but powershell's serialized pipeline might contain
// \0 (null characters) which are unacceptable in WSMan. So we are converting to Base64
// here. The server will convert this back to original string.
string base64EncodedArgument = Convert.ToBase64String(firstArgument);
IntPtr firstArgAddress = Marshal.StringToHGlobalUni(base64EncodedArgument);
Marshal.WriteIntPtr(_internalData.args, firstArgAddress);
_data = MarshalledObject.Create<WSManCommandArgSet.WSManCommandArgSetInternal>(_internalData);
// For conformity:
this.args = null;
this.argsCount = 0;
}
/// <summary>
/// Free resources.
/// </summary>
public void Dispose()
{
IntPtr firstArgAddress = Marshal.ReadIntPtr(_internalData.args);
if (firstArgAddress != IntPtr.Zero)
{
Marshal.FreeHGlobal(firstArgAddress);
}
Marshal.FreeHGlobal(_internalData.args);
_data.Dispose();
}
/// <summary>
/// Implicit cast to IntPtr.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static implicit operator IntPtr(WSManCommandArgSet obj)
{
return obj._data.DataPtr;
}
#endregion
#region Unmanaged to Managed
internal string[] args;
internal int argsCount;
/// <summary>
/// Since this is a structure, it must be non-null. This differs in behavior from all the other UnMarshals
/// that are classes since they can be null.
/// TODO: Do I need to worry about intermediate null characters in the arguments? The managed to unmanaged does!
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManCommandArgSet UnMarshal(IntPtr unmanagedData)
{
WSManCommandArgSet result = new WSManCommandArgSet();
if (unmanagedData != IntPtr.Zero)
{
WSManCommandArgSetInternal resultInternal = Marshal.PtrToStructure<WSManCommandArgSetInternal>(unmanagedData);
string[] tempArgs = null;
if (resultInternal.argsCount > 0)
{
tempArgs = new string[resultInternal.argsCount];
IntPtr[] ptrs = new IntPtr[resultInternal.argsCount];
Marshal.Copy(resultInternal.args, ptrs, 0, resultInternal.argsCount); // Marshal the array of string pointers
for (int i = 0; i < resultInternal.argsCount; i++)
{
tempArgs[i] = Marshal.PtrToStringUni(ptrs[i]); // Marshal the string pointers into strings
}
}
result.argsCount = resultInternal.argsCount;
result.args = tempArgs;
}
return result;
}
#endregion
}
internal struct WSManShellDisconnectInfo : IDisposable
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WSManShellDisconnectInfoInternal
{
/// <summary>
/// New idletimeout for the server shell that overrides the original idletimeout specified in WSManCreateShell.
/// </summary>
internal uint idleTimeoutMs;
}
private WSManShellDisconnectInfoInternal _internalInfo;
internal MarshalledObject data;
#region Constructor / Other methods
internal WSManShellDisconnectInfo(uint serverIdleTimeOut)
{
_internalInfo = new WSManShellDisconnectInfoInternal();
_internalInfo.idleTimeoutMs = serverIdleTimeOut;
data = MarshalledObject.Create<WSManShellDisconnectInfoInternal>(_internalInfo);
}
/// <summary>
/// Disposes the object.
/// </summary>
public void Dispose()
{
data.Dispose();
}
/// <summary>
/// Implicit IntPtr.
/// </summary>
/// <param name="disconnectInfo"></param>
/// <returns></returns>
public static implicit operator IntPtr(WSManShellDisconnectInfo disconnectInfo)
{
return disconnectInfo.data.DataPtr;
}
#endregion
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManShellStartupInfoStruct
{
/// <summary>
/// PowerShell always uses one stream. So no need to expand this.
/// Maps to WSManStreamIDSet.
/// </summary>
internal IntPtr inputStreamSet;
/// <summary>
/// PowerShell always uses one stream. So no need to expand this.
/// Maps to WSManStreamIDSet.
/// </summary>
internal IntPtr outputStreamSet;
/// <summary>
/// Idle timeout.
/// </summary>
internal uint idleTimeoutMs;
/// <summary>
/// Working directory of the shell.
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string workingDirectory;
/// <summary>
/// Environment variables available to the shell.
/// Maps to WSManEnvironmentVariableSet.
/// </summary>
internal IntPtr environmentVariableSet;
/// <summary>
/// Environment variables available to the shell.
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string name;
}
/// <summary>
/// Managed to unmanaged representation of WSMAN_SHELL_STARTUP_INFO.
/// It converts managed values into an unmanaged compatible WSManShellStartupInfoStruct that
/// is marshalled into unmanaged memory.
/// </summary>
internal struct WSManShellStartupInfo_ManToUn : IDisposable
{
private WSManShellStartupInfoStruct _internalInfo;
internal MarshalledObject data;
#region Constructor / Other methods
/// <summary>
/// Creates a startup info with 1 startup option.
/// The startup option is intended to specify the version.
/// </summary>
/// <param name="inputStreamSet">
/// </param>
/// <param name="outputStreamSet">
/// </param>
/// <param name="serverIdleTimeOut">
/// </param>
/// <param name="name">
/// </param>
internal WSManShellStartupInfo_ManToUn(WSManStreamIDSet_ManToUn inputStreamSet, WSManStreamIDSet_ManToUn outputStreamSet, uint serverIdleTimeOut, string name)
{
_internalInfo = new WSManShellStartupInfoStruct();
_internalInfo.inputStreamSet = inputStreamSet;
_internalInfo.outputStreamSet = outputStreamSet;
_internalInfo.idleTimeoutMs = serverIdleTimeOut;
// WSMan uses %USER_PROFILE% as the default working directory.
_internalInfo.workingDirectory = null;
_internalInfo.environmentVariableSet = IntPtr.Zero;
_internalInfo.name = name;
data = MarshalledObject.Create<WSManShellStartupInfoStruct>(_internalInfo);
}
/// <summary>
/// Disposes the object.
/// </summary>
public void Dispose()
{
data.Dispose();
}
/// <summary>
/// Implicit IntPtr.
/// </summary>
/// <param name="startupInfo"></param>
/// <returns></returns>
public static implicit operator IntPtr(WSManShellStartupInfo_ManToUn startupInfo)
{
return startupInfo.data.DataPtr;
}
#endregion
}
/// <summary>
/// Unmanaged to managed representation of WSMAN_SHELL_STARTUP_INFO.
/// It unmarshals the unmanaged struct into this object for use by managed code.
/// </summary>
internal class WSManShellStartupInfo_UnToMan
{
internal WSManStreamIDSet_UnToMan inputStreamSet;
internal WSManStreamIDSet_UnToMan outputStreamSet;
internal uint idleTimeoutMS;
internal string workingDirectory;
internal WSManEnvironmentVariableSet environmentVariableSet;
internal string name;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManShellStartupInfo_UnToMan UnMarshal(IntPtr unmanagedData)
{
WSManShellStartupInfo_UnToMan result = null;
if (unmanagedData != IntPtr.Zero)
{
WSManShellStartupInfoStruct resultInternal = Marshal.PtrToStructure<WSManShellStartupInfoStruct>(unmanagedData);
result = new WSManShellStartupInfo_UnToMan();
result.inputStreamSet = WSManStreamIDSet_UnToMan.UnMarshal(resultInternal.inputStreamSet);
result.outputStreamSet = WSManStreamIDSet_UnToMan.UnMarshal(resultInternal.outputStreamSet);
result.idleTimeoutMS = resultInternal.idleTimeoutMs;
result.workingDirectory = resultInternal.workingDirectory; // TODO: Special marshaling required here?
result.environmentVariableSet = WSManEnvironmentVariableSet.UnMarshal(resultInternal.environmentVariableSet);
result.name = resultInternal.name;
}
return result;
}
}
/// <summary>
/// Managed representation of WSMAN_ENVIRONMENT_VARIABLE_SET.
/// It wraps WSManEnvironmentVariableSetInternal and UnMarshals the unmanaged
/// data into the object.
/// </summary>
internal class WSManEnvironmentVariableSet
{
internal uint varsCount;
internal WSManEnvironmentVariableInternal[] vars;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManEnvironmentVariableSet UnMarshal(IntPtr unmanagedData)
{
WSManEnvironmentVariableSet result = null;
if (unmanagedData != IntPtr.Zero)
{
WSManEnvironmentVariableSetInternal resultInternal = Marshal.PtrToStructure<WSManEnvironmentVariableSetInternal>(unmanagedData);
result = new WSManEnvironmentVariableSet();
WSManEnvironmentVariableInternal[] varsArray = null;
if (resultInternal.varsCount > 0)
{
varsArray = new WSManEnvironmentVariableInternal[resultInternal.varsCount];
int sizeInBytes = Marshal.SizeOf<WSManEnvironmentVariableInternal>();
IntPtr perElementPtr = resultInternal.vars;
for (int i = 0; i < resultInternal.varsCount; i++)
{
IntPtr p = IntPtr.Add(perElementPtr, (i * sizeInBytes));
varsArray[i] = Marshal.PtrToStructure<WSManEnvironmentVariableInternal>(p);
}
}
result.vars = varsArray;
result.varsCount = resultInternal.varsCount;
}
return result;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WSManEnvironmentVariableSetInternal
{
internal uint varsCount;
internal IntPtr vars; // Array of WSManEnvironmentVariableInternal structs
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManEnvironmentVariableInternal
{
[MarshalAs(UnmanagedType.LPWStr)]
internal string name;
[MarshalAs(UnmanagedType.LPWStr)]
internal string value;
}
}
/// <summary>
/// Proxy Info used with WSManCreateSession.
/// </summary>
internal class WSManProxyInfo : IDisposable
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WSManProxyInfoInternal
{
public int proxyAccessType;
public WSManUserNameAuthenticationCredentials.WSManUserNameCredentialStruct proxyAuthCredentialsStruct;
}
private MarshalledObject _data;
/// <summary>
/// </summary>
/// <param name="proxyAccessType"></param>
/// <param name="authCredentials"></param>
internal WSManProxyInfo(ProxyAccessType proxyAccessType,
WSManUserNameAuthenticationCredentials authCredentials)
{
WSManProxyInfoInternal internalInfo = new WSManProxyInfoInternal();
internalInfo.proxyAccessType = (int)proxyAccessType;
internalInfo.proxyAuthCredentialsStruct = new WSManUserNameAuthenticationCredentials.WSManUserNameCredentialStruct();
internalInfo.proxyAuthCredentialsStruct.authenticationMechanism = WSManAuthenticationMechanism.WSMAN_FLAG_DEFAULT_AUTHENTICATION;
if (authCredentials != null)
{
internalInfo.proxyAuthCredentialsStruct = authCredentials.CredentialStruct;
}
_data = MarshalledObject.Create<WSManProxyInfoInternal>(internalInfo);
}
public void Dispose()
{
// data is of struct type..so no need to set it to null.
_data.Dispose();
GC.SuppressFinalize(this);
}
/// <summary>
/// Implicit IntPtr.
/// </summary>
/// <param name="proxyInfo"></param>
/// <returns></returns>
public static implicit operator IntPtr(WSManProxyInfo proxyInfo)
{
return proxyInfo._data.DataPtr;
}
}
#endregion
#region WSMan Shell Async
/// <summary>
/// Flags used by all callback functions: WSMAN_COMPLETION_FUNCTION,
/// WSMAN_SUBSCRIPTION_COMPLETION_FUNCTION and WSMAN_SHELL_COMPLETION_FUNCTION.
/// </summary>
internal enum WSManCallbackFlags
{
//
// Flag that marks the end of any single step of multistep operation
//
WSMAN_FLAG_CALLBACK_END_OF_OPERATION = 0x1,
//
// WSMAN_SHELL_COMPLETION_FUNCTION API specific flags
// end of a particular stream; it is used for optimization purposes if the shell
// knows that no more output will occur for this stream; in some conditions this
// cannot be determined.
//
WSMAN_FLAG_CALLBACK_END_OF_STREAM = 0x8,
//
// Flag that if present on CreateShell callback indicates that it supports disconnect
//
WSMAN_FLAG_CALLBACK_SHELL_SUPPORTS_DISCONNECT = 0x20,
//
// Marks the end of an auto-disconnect operation.
//
WSMAN_FLAG_CALLBACK_SHELL_AUTODISCONNECTED = 0x40,
//
// Network failure notification.
//
WSMAN_FLAG_CALLBACK_NETWORK_FAILURE_DETECTED = 0x100,
//
// Network connection retry notification.
//
WSMAN_FLAG_CALLBACK_RETRYING_AFTER_NETWORK_FAILURE = 0x200,
//
// Network retry succeeded, connection re-established notification.
//
WSMAN_FLAG_CALLBACK_RECONNECTED_AFTER_NETWORK_FAILURE = 0x400,
//
// Retries failed, now auto-disconnecting.
//
WSMAN_FLAG_CALLBACK_SHELL_AUTODISCONNECTING = 0x800,
//
// Internal error during retries. Cannot auto-disconnect. Shell failure.
//
WSMAN_FLAG_CALLBACK_RETRY_ABORTED_DUE_TO_INTERNAL_ERROR = 0x1000,
//
// Flag that indicates for a receive operation that a delay stream request has been processed
//
WSMAN_FLAG_RECEIVE_DELAY_STREAM_REQUEST_PROCESSED = 0X2000
}
/// <summary>
/// Completion function used by all Shell functions. Returns error->code != 0 upon error;
/// use error->errorDetail structure for extended error informations; the callback is
/// called for each shell operation; after a WSManReceiveShellOutput operation is initiated,
/// the callback is called for each output stream element or if error; the underlying
/// implementation handles the polling of stream data from the command or shell.
/// If WSMAN_COMMAND_STATE_DONE state is received, no more streams will be received from the command,
/// so the command can be closed using WSManCloseShellOperationEx(command).
/// If error->code != 0, the result is guaranteed to be NULL. The error and result objects are
/// allocated and owned by the WSMan client stack; they are valid during the callback only; the user
/// has to synchronously copy the data in the callback. This callback function will use the current
/// access token, whether it is a process or impersonation token.
/// </summary>
/// <param name="operationContext">
/// user supplied operation context.
/// </param>
/// <param name="flags">
/// one or more flags from WSManCallbackFlags
/// </param>
/// <param name="error">
/// error allocated and owned by the winrm stack; valid in the callback only;
/// </param>
/// <param name="shellOperationHandle">
/// shell handle associated with the user context
/// </param>
/// <param name="commandOperationHandle">
/// command handle associated with the user context
/// </param>
/// <param name="operationHandle">
/// operation handle associated with the user context
/// </param>
/// <param name="data">
/// output data from command/shell; allocated internally and owned by the winrm stack.
/// valid only within this function.
/// See WSManReceiveDataResult.
/// </param>
internal delegate void WSManShellCompletionFunction(
IntPtr operationContext,
int flags,
IntPtr error,
IntPtr shellOperationHandle,
IntPtr commandOperationHandle,
IntPtr operationHandle,
IntPtr data
);
/// <summary>
/// Struct which holds reference to the callback(delegate) passed to WSMan
/// API.
/// </summary>
internal struct WSManShellAsyncCallback
{
// GC handle which prevents garbage collector from collecting this delegate.
private GCHandle _gcHandle;
private readonly IntPtr _asyncCallback;
internal WSManShellAsyncCallback(WSManShellCompletionFunction callback)
{
// if a delegate is re-located by a garbage collection, it will not affect
// the underlaying managed callback, so Alloc is used to add a reference
// to the delegate, allowing relocation of the delegate, but preventing
// disposal. Using GCHandle without pinning reduces fragmentation potential
// of the managed heap.
_gcHandle = GCHandle.Alloc(callback);
_asyncCallback = Marshal.GetFunctionPointerForDelegate(callback);
}
public static implicit operator IntPtr(WSManShellAsyncCallback callback)
{
return callback._asyncCallback;
}
}
/// <summary>
/// Used in different WSMan functions to supply async callback.
/// </summary>
internal class WSManShellAsync
{
[StructLayout(LayoutKind.Sequential)]
internal struct WSManShellAsyncInternal
{
internal IntPtr operationContext;
internal IntPtr asyncCallback;
}
private MarshalledObject _data;
private WSManShellAsyncInternal _internalData;
internal WSManShellAsync(IntPtr context, WSManShellAsyncCallback callback)
{
_internalData = new WSManShellAsyncInternal();
_internalData.operationContext = context;
_internalData.asyncCallback = callback;
_data = MarshalledObject.Create<WSManShellAsyncInternal>(_internalData);
}
public void Dispose()
{
_data.Dispose();
}
public static implicit operator IntPtr(WSManShellAsync async)
{
return async._data;
}
}
/// <summary>
/// Used in the shell completion function delegate to refer to error.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManError
{
internal int errorCode;
/// <summary>
/// Extended error description from the fault;
/// </summary>
internal string errorDetail;
/// <summary>
/// Language for error description (RFC 3066 language code); it can be NULL.
/// </summary>
internal string language;
/// <summary>
/// Machine id; it can be NULL.
/// </summary>
internal string machineName;
/// <summary>
/// Constructs a WSManError from the unmanaged pointer.
/// This involves copying data from unmanaged memory to managed heap.
/// </summary>
/// <param name="unmanagedData">
/// Pointer to unmanaged data.
/// </param>
/// <returns>
/// </returns>
internal static WSManError UnMarshal(IntPtr unmanagedData)
{
return Marshal.PtrToStructure<WSManError>(unmanagedData);
}
}
internal class WSManCreateShellDataResult
{
[StructLayout(LayoutKind.Sequential)]
private struct WSManCreateShellDataResultInternal
{
internal WSManDataStruct data;
}
internal string data;
internal static WSManCreateShellDataResult UnMarshal(IntPtr unmanagedData)
{
WSManCreateShellDataResult result = new WSManCreateShellDataResult();
if (unmanagedData != IntPtr.Zero)
{
WSManCreateShellDataResultInternal resultInternal = Marshal.PtrToStructure<WSManCreateShellDataResultInternal>(unmanagedData);
string connectData = null;
if (resultInternal.data.textData.textLength > 0)
{
connectData = Marshal.PtrToStringUni(resultInternal.data.textData.text, resultInternal.data.textData.textLength);
}
result.data = connectData;
}
return result;
}
// The following structures are created to honor struct boundaries
// on x86,amd64 and ia64
[StructLayout(LayoutKind.Sequential)]
private struct WSManDataStruct
{
internal uint type;
internal WSManTextDataInternal textData;
}
[StructLayout(LayoutKind.Sequential)]
private struct WSManTextDataInternal
{
internal int textLength;
internal IntPtr text;
}
}
internal class WSManConnectDataResult
{
[StructLayout(LayoutKind.Sequential)]
private struct WSManConnectDataResultInternal
{
internal WSManDataStruct data;
}
internal string data;
internal static WSManConnectDataResult UnMarshal(IntPtr unmanagedData)
{
WSManConnectDataResultInternal resultInternal = Marshal.PtrToStructure<WSManConnectDataResultInternal>(unmanagedData);
string connectData = null;
if (resultInternal.data.textData.textLength > 0)
{
connectData = Marshal.PtrToStringUni(resultInternal.data.textData.text, resultInternal.data.textData.textLength);
}
WSManConnectDataResult result = new WSManConnectDataResult();
result.data = connectData;
return result;
}
// The following structures are created to honor struct boundaries
// on x86,amd64 and ia64
[StructLayout(LayoutKind.Sequential)]
private struct WSManDataStruct
{
internal uint type;
internal WSManTextDataInternal textData;
}
[StructLayout(LayoutKind.Sequential)]
private struct WSManTextDataInternal
{
internal int textLength;
internal IntPtr text;
}
}
/// <summary>
/// Used in the shell completion function delegate to refer to the data.
/// </summary>
internal class WSManReceiveDataResult
{
/// <summary>
/// The actual data.
/// </summary>
internal byte[] data;
/// <summary>
/// Stream the data belongs to.
/// </summary>
internal string stream;
/// <summary>
/// Constructs a WSManReceiveDataResult from the unmanaged pointer.
/// This involves copying data from unmanaged memory to managed heap.
/// Currently PowerShell supports only binary data on the wire, so this
/// method asserts if the data is not binary.
/// </summary>
/// <param name="unmanagedData">
/// Pointer to unmanaged data.
/// </param>
/// <returns>
/// </returns>
internal static WSManReceiveDataResult UnMarshal(IntPtr unmanagedData)
{
WSManReceiveDataResultInternal result1 =
Marshal.PtrToStructure<WSManReceiveDataResultInternal>(unmanagedData);
// copy data from unmanaged heap to managed heap.
byte[] dataRecvd = null;
if (result1.data.binaryData.bufferLength > 0)
{
dataRecvd = new byte[result1.data.binaryData.bufferLength];
Marshal.Copy(result1.data.binaryData.buffer,
dataRecvd,
0,
result1.data.binaryData.bufferLength);
}
#if !UNIX
Dbg.Assert(result1.data.type == (uint)WSManDataType.WSMAN_DATA_TYPE_BINARY,
"ReceiveDataResult can receive only binary data");
#endif
WSManReceiveDataResult result = new WSManReceiveDataResult();
result.data = dataRecvd;
result.stream = result1.streamId;
return result;
}
[StructLayout(LayoutKind.Sequential)]
private struct WSManReceiveDataResultInternal
{
[MarshalAs(UnmanagedType.LPWStr)]
internal string streamId;
internal WSManDataStruct data;
[MarshalAs(UnmanagedType.LPWStr)]
internal string commandState;
internal int exitCode;
}
// The following structures are created to honor struct boundaries
// on x86,amd64 and ia64
[StructLayout(LayoutKind.Sequential)]
private struct WSManDataStruct
{
internal uint type;
internal WSManBinaryDataInternal binaryData;
}
[StructLayout(LayoutKind.Sequential)]
private struct WSManBinaryDataInternal
{
internal int bufferLength;
internal IntPtr buffer;
}
}
#endregion
#region Plugin API Structure Definitions
/// <summary>
/// This is the managed representation of the WSMAN_PLUGIN_REQUEST struct.
/// </summary>
internal class WSManPluginRequest
{
/// <summary>
/// Unmarshalled WSMAN_SENDER_DETAILS struct.
/// </summary>
internal WSManSenderDetails senderDetails;
internal string locale;
internal string resourceUri;
/// <summary>
/// Unmarshalled WSMAN_OPERATION_INFO struct.
/// </summary>
internal WSManOperationInfo operationInfo;
/// <summary>
/// Kept around to allow direct access to shutdownNotification and its handle.
/// </summary>
private WSManPluginRequestInternal _internalDetails;
/// <summary>
/// Volatile value that should be read directly from its unmanaged location.
/// TODO: Does "volatile" still apply when accessing it in managed code.
/// </summary>
internal bool shutdownNotification
{
get { return _internalDetails.shutdownNotification; }
}
/// <summary>
/// Left untouched in unmanaged memory because it is passed directly to
/// RegisterWaitForSingleObject().
/// </summary>
internal IntPtr shutdownNotificationHandle
{
get { return _internalDetails.shutdownNotificationHandle; }
}
/// <summary>
/// Copy of the unmanagedData value used to create the structure.
/// </summary>
internal IntPtr unmanagedHandle;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManPluginRequest UnMarshal(IntPtr unmanagedData)
{
// Dbg.Assert(IntPtr.Zero != unmanagedData, "unmanagedData must be non-null. This means WinRM sent a bad pointer.");
WSManPluginRequest result = null;
if (unmanagedData != IntPtr.Zero)
{
WSManPluginRequestInternal resultInternal = Marshal.PtrToStructure<WSManPluginRequestInternal>(unmanagedData);
result = new WSManPluginRequest();
result.senderDetails = WSManSenderDetails.UnMarshal(resultInternal.senderDetails);
result.locale = resultInternal.locale;
result.resourceUri = resultInternal.resourceUri;
result.operationInfo = WSManOperationInfo.UnMarshal(resultInternal.operationInfo);
result._internalDetails = resultInternal;
result.unmanagedHandle = unmanagedData;
}
return result;
}
/// <summary>
/// Representation of WSMAN_PLUGIN_REQUEST.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WSManPluginRequestInternal
{
/// <summary>
/// WSManSenderDetails.
/// </summary>
internal IntPtr senderDetails;
[MarshalAs(UnmanagedType.LPWStr)]
internal string locale;
[MarshalAs(UnmanagedType.LPWStr)]
internal string resourceUri;
/// <summary>
/// WSManOperationInfo.
/// </summary>
internal IntPtr operationInfo;
internal bool shutdownNotification;
internal IntPtr shutdownNotificationHandle;
}
}
internal class WSManSenderDetails
{
internal string senderName;
internal string authenticationMechanism;
internal WSManCertificateDetails certificateDetails;
internal IntPtr clientToken; // TODO: How should this be marshalled?????
internal string httpUrl;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManSenderDetails UnMarshal(IntPtr unmanagedData)
{
WSManSenderDetails result = null;
if (unmanagedData != IntPtr.Zero)
{
WSManSenderDetailsInternal resultInternal = Marshal.PtrToStructure<WSManSenderDetailsInternal>(unmanagedData);
result = new WSManSenderDetails();
result.senderName = resultInternal.senderName;
result.authenticationMechanism = resultInternal.authenticationMechanism;
result.certificateDetails = WSManCertificateDetails.UnMarshal(resultInternal.certificateDetails);
result.clientToken = resultInternal.clientToken; // TODO: UnMarshaling needed here!!!!
result.httpUrl = resultInternal.httpUrl;
}
return result;
}
/// <summary>
/// Managed representation of WSMAN_SENDER_DETAILS.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WSManSenderDetailsInternal
{
[MarshalAs(UnmanagedType.LPWStr)]
internal string senderName;
[MarshalAs(UnmanagedType.LPWStr)]
internal string authenticationMechanism;
/// <summary>
/// WSManCertificateDetails.
/// </summary>
internal IntPtr certificateDetails;
internal IntPtr clientToken;
[MarshalAs(UnmanagedType.LPWStr)]
internal string httpUrl;
}
}
internal class WSManCertificateDetails
{
internal string subject;
internal string issuerName;
internal string issuerThumbprint;
internal string subjectName;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManCertificateDetails UnMarshal(IntPtr unmanagedData)
{
WSManCertificateDetails result = null;
if (unmanagedData != IntPtr.Zero)
{
WSManCertificateDetailsInternal resultInternal = Marshal.PtrToStructure<WSManCertificateDetailsInternal>(unmanagedData);
result = new WSManCertificateDetails();
result.subject = resultInternal.subject;
result.issuerName = resultInternal.issuerName;
result.issuerThumbprint = resultInternal.issuerThumbprint;
result.subjectName = resultInternal.subjectName;
}
return result;
}
/// <summary>
/// Managed representation of WSMAN_CERTIFICATE_DETAILS.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WSManCertificateDetailsInternal
{
[MarshalAs(UnmanagedType.LPWStr)]
internal string subject;
[MarshalAs(UnmanagedType.LPWStr)]
internal string issuerName;
[MarshalAs(UnmanagedType.LPWStr)]
internal string issuerThumbprint;
[MarshalAs(UnmanagedType.LPWStr)]
internal string subjectName;
}
}
internal class WSManOperationInfo
{
internal WSManFragmentInternal fragment;
internal WSManFilterInternal filter;
internal WSManSelectorSet selectorSet;
internal WSManOptionSet optionSet;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="unmanagedData"></param>
/// <returns></returns>
internal static WSManOperationInfo UnMarshal(IntPtr unmanagedData)
{
WSManOperationInfo result = null;
if (unmanagedData != IntPtr.Zero)
{
WSManOperationInfoInternal resultInternal = Marshal.PtrToStructure<WSManOperationInfoInternal>(unmanagedData);
result = new WSManOperationInfo();
result.fragment = resultInternal.fragment;
result.filter = resultInternal.filter;
result.selectorSet = WSManSelectorSet.UnMarshal(resultInternal.selectorSet);
result.optionSet = WSManOptionSet.UnMarshal(resultInternal.optionSet);
}
return result;
}
/// <summary>
/// Managed representation of WSMAN_OPERATION_INFO.
/// selectorSet and optionSet are handled differently because they are structs that contain pointers to arrays of structs.
/// Most other data structures in the API point to structures using IntPtr rather than including the actual structure.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WSManOperationInfoInternal
{
internal WSManFragmentInternal fragment;
internal WSManFilterInternal filter;
internal WSManSelectorSet.WSManSelectorSetStruct selectorSet;
internal WSManOptionSetStruct optionSet;
}
/// <summary>
/// Managed representation of WSMAN_FRAGMENT.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManFragmentInternal
{
[MarshalAs(UnmanagedType.LPWStr)]
internal string path;
[MarshalAs(UnmanagedType.LPWStr)]
internal string dialect;
}
/// <summary>
/// Managed representation of WSMAN_FILTER.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManFilterInternal
{
[MarshalAs(UnmanagedType.LPWStr)]
internal string filter;
[MarshalAs(UnmanagedType.LPWStr)]
internal string dialect;
}
}
internal class WSManSelectorSet
{
internal int numberKeys;
internal WSManKeyStruct[] keys;
/// <summary>
/// Converts the unmanaged pointer to a managed class object.
/// </summary>
/// <param name="resultInternal"></param>
/// <returns></returns>
internal static WSManSelectorSet UnMarshal(WSManSelectorSetStruct resultInternal)
{
WSManKeyStruct[] tempKeys = null;
if (resultInternal.numberKeys > 0)
{
tempKeys = new WSManKeyStruct[resultInternal.numberKeys];
int sizeInBytes = Marshal.SizeOf<WSManKeyStruct>();
IntPtr perElementPtr = resultInternal.keys;
for (int i = 0; i < resultInternal.numberKeys; i++)
{
IntPtr p = IntPtr.Add(perElementPtr, (i * sizeInBytes));
tempKeys[i] = Marshal.PtrToStructure<WSManKeyStruct>(p);
}
}
WSManSelectorSet result = new WSManSelectorSet();
result.numberKeys = resultInternal.numberKeys;
result.keys = tempKeys;
return result;
}
/// <summary>
/// Managed representation of WSMAN_SELECTOR_SET.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManSelectorSetStruct
{
internal int numberKeys;
/// <summary>
/// Array of WSManKeyStruct structures.
/// </summary>
internal IntPtr keys;
}
/// <summary>
/// Managed representation of WSMAN_OPTION_SET.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WSManKeyStruct
{
[MarshalAs(UnmanagedType.LPWStr)]
internal string key;
[MarshalAs(UnmanagedType.LPWStr)]
internal string value;
}
}
#endregion
#region DllImports ClientAPI
#if !UNIX
internal const string WSManClientApiDll = @"WsmSvc.dll";
internal const string WSManProviderApiDll = @"WsmSvc.dll";
#else
internal const string WSManClientApiDll = @"libpsrpclient";
internal const string WSManProviderApiDll = @"libpsrpomiprov";
#endif
/// <summary>
/// This API is used to initialize the WinRM client;
/// It can be used by different clients on the same process, ie svchost.exe.
/// Returns a nonzero error code upon failure.
/// </summary>
/// <param name="flags">
/// </param>
/// <param name="wsManAPIHandle">
/// </param>
/// <returns>
/// </returns>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManInitialize(int flags,
[In, Out] ref IntPtr wsManAPIHandle);
/// <summary>
/// This API deinitializes the Winrm client stack; all operations will
/// finish before this API will return; this is a sync call;
/// it is highly recommended that all operations are explicitly cancelled
/// and all sessions are closed before calling this API
/// Returns non zero error code upon failure.
/// </summary>
/// <param name="wsManAPIHandle"></param>
/// <param name="flags"></param>
/// <returns></returns>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManDeinitialize(IntPtr wsManAPIHandle, int flags);
/// <summary>
/// Creates a session which can be used to perform subsequent operations
/// Returns a non zero error code upon failure.
/// </summary>
/// <param name="wsManAPIHandle"></param>
/// <param name="connection">
/// if NULL, then connection will default to 127.0.0.1
/// </param>
/// <param name="flags"></param>
/// <param name="authenticationCredentials">
/// can be null.
/// </param>
/// <param name="proxyInfo">
/// </param>
/// <param name="wsManSessionHandle"></param>
/// <returns></returns>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManCreateSession(IntPtr wsManAPIHandle,
[MarshalAs(UnmanagedType.LPWStr)] string connection,
int flags,
IntPtr authenticationCredentials,
IntPtr proxyInfo,
[In, Out] ref IntPtr wsManSessionHandle);
/// <summary>
/// Frees memory of session and closes all related operations before returning;
/// this is sync call it is recommended that all pending operations are either
/// completed or cancelled before calling this API. Returns a non zero error
/// code upon failure.
/// </summary>
/// <param name="wsManSessionHandle"></param>
/// <param name="flags"></param>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManCloseSession(IntPtr wsManSessionHandle,
int flags);
/// <summary>
/// WSManSetSessionOption API - set session options
/// Returns a non zero error code upon failure.
/// </summary>
/// <param name="wsManSessionHandle"></param>
/// <param name="option"></param>
/// <param name="data">
/// An int (DWORD) data.
/// </param>
/// <returns></returns>
internal static int WSManSetSessionOption(IntPtr wsManSessionHandle,
WSManSessionOption option,
WSManDataDWord data)
{
MarshalledObject marshalObj = data.Marshal();
using (marshalObj)
{
return WSManSetSessionOption(wsManSessionHandle, option, marshalObj.DataPtr);
}
}
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManSetSessionOption(IntPtr wsManSessionHandle,
WSManSessionOption option,
IntPtr data);
/// <summary>
/// WSManGetSessionOptionAsDword API - get a session option. Returns a non
/// zero error code upon failure.
/// </summary>
/// <param name="wsManSessionHandle"></param>
/// <param name="option"></param>
/// <param name="value">
/// An int (DWORD) data.
/// </param>
/// <returns>Zero on success, otherwise the error code.</returns>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManGetSessionOptionAsDword(IntPtr wsManSessionHandle,
WSManSessionOption option,
out int value);
/// <summary>
/// Function that retrieves a WSMan session option as string. Thread.CurrentUICulture
/// will be used as the language code to get the error message in.
/// </summary>
/// <param name="wsManAPIHandle"></param>
/// <param name="option">Session option to get.</param>
/// <returns></returns>
internal static string WSManGetSessionOptionAsString(IntPtr wsManAPIHandle,
WSManSessionOption option)
{
Dbg.Assert(wsManAPIHandle != IntPtr.Zero, "wsManAPIHandle cannot be null.");
// The error code taken from winerror.h used for getting buffer length.
const int ERROR_INSUFFICIENT_BUFFER = 122;
string returnval = string.Empty;
int bufferSize = 0;
// calculate buffer size required
if (WSManGetSessionOptionAsString(wsManAPIHandle,
option, 0, null, out bufferSize) != ERROR_INSUFFICIENT_BUFFER)
{
return returnval;
}
// calculate space required to store output.
// StringBuilder will not work for this case as CLR
// does not copy the entire string if there are delimiters ('\0')
// in the middle of a string.
int bufferSizeInBytes = bufferSize * 2;
byte[] msgBufferPtr = new byte[bufferSizeInBytes];
// Now get the actual value
int messageLength;
if (WSManGetSessionOptionAsString(wsManAPIHandle,
option, bufferSizeInBytes, msgBufferPtr, out messageLength) != 0)
{
return returnval;
}
try
{
returnval = Encoding.Unicode.GetString(msgBufferPtr, 0, bufferSizeInBytes);
}
catch (ArgumentNullException)
{
}
catch (System.Text.DecoderFallbackException)
{
}
return returnval;
}
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
private static extern int WSManGetSessionOptionAsString(IntPtr wsManSessionHandle,
WSManSessionOption option,
int optionLength,
byte[] optionAsString,
out int optionLengthUsed);
/// <summary>
/// Creates a shell on the remote end.
/// </summary>
/// <param name="wsManSessionHandle">
/// Session in which the shell is created.
/// </param>
/// <param name="flags">
/// </param>
/// <param name="resourceUri">
/// The resource Uri to use to create the shell.
/// </param>
/// <param name="shellId"></param>
/// <param name="startupInfo">
/// startup information to be passed to the shell.
/// </param>
/// <param name="optionSet">
/// Options to be passed with CreateShell
/// </param>
/// <param name="openContent">
/// any content that is used by the remote shell to startup.
/// </param>
/// <param name="asyncCallback">
/// callback to notify when the create operation completes.
/// </param>
/// <param name="shellOperationHandle">
/// An out parameter referencing a WSMan shell operation handle
/// for this shell.
/// </param>
/// <returns></returns>
internal static void WSManCreateShellEx(IntPtr wsManSessionHandle,
int flags,
string resourceUri,
string shellId,
WSManShellStartupInfo_ManToUn startupInfo,
WSManOptionSet optionSet,
WSManData_ManToUn openContent,
IntPtr asyncCallback,
ref IntPtr shellOperationHandle)
{
WSManCreateShellExInternal(wsManSessionHandle, flags, resourceUri, shellId, startupInfo, optionSet,
openContent, asyncCallback, ref shellOperationHandle);
}
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManCreateShellEx", SetLastError = false, CharSet = CharSet.Unicode)]
private static extern void WSManCreateShellExInternal(IntPtr wsManSessionHandle,
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string resourceUri,
[MarshalAs(UnmanagedType.LPWStr)] string shellId,
IntPtr startupInfo,
IntPtr optionSet,
IntPtr openContent,
IntPtr asyncCallback,
[In, Out] ref IntPtr shellOperationHandle);
/// <summary>
/// </summary>
/// <param name="wsManSessionHandle"></param>
/// <param name="flags"></param>
/// <param name="resourceUri"></param>
/// <param name="shellId"></param>
/// <param name="optionSet"></param>
/// <param name="connectXml"></param>
/// <param name="asyncCallback"></param>
/// <param name="shellOperationHandle"></param>
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManConnectShell", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManConnectShellEx(IntPtr wsManSessionHandle,
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string resourceUri,
[MarshalAs(UnmanagedType.LPWStr)] string shellId,
IntPtr optionSet,
IntPtr connectXml,
IntPtr asyncCallback,
[In, Out] ref IntPtr shellOperationHandle);
/// <summary>
/// </summary>
/// <param name="wsManSessionHandle"></param>
/// <param name="flags"></param>
/// <param name="disconnectInfo"></param>
/// <param name="asyncCallback"></param>
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManDisconnectShell", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManDisconnectShellEx(IntPtr wsManSessionHandle,
int flags,
IntPtr disconnectInfo,
IntPtr asyncCallback);
/// <summary>
/// </summary>
/// <param name="wsManSessionHandle"></param>
/// <param name="flags"></param>
/// <param name="asyncCallback"></param>
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManReconnectShell", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManReconnectShellEx(IntPtr wsManSessionHandle,
int flags,
IntPtr asyncCallback);
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManReconnectShellCommand", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManReconnectShellCommandEx(IntPtr wsManCommandHandle,
int flags,
IntPtr asyncCallback);
/// <summary>
/// Starts a command on the remote end.
/// </summary>
/// <param name="shellOperationHandle">
/// Shell handle in which the command is created and run.
/// </param>
/// <param name="flags"></param>
/// <param name="commandId"></param>
/// <param name="commandLine">
/// command line for the command.
/// </param>
/// <param name="commandArgSet">
/// arguments for the command.
/// </param>
/// <param name="optionSet">
/// options.
/// </param>
/// <param name="asyncCallback">
/// callback to notify when the operation completes.
/// </param>
/// <param name="commandOperationHandle">
/// An out parameter referencing a WSMan shell operation handle
/// for this command.
/// </param>
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManRunShellCommandEx", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManRunShellCommandEx(IntPtr shellOperationHandle,
int flags,
[MarshalAs(UnmanagedType.LPWStr)]
string commandId,
[MarshalAs(UnmanagedType.LPWStr)]
string commandLine,
IntPtr commandArgSet,
IntPtr optionSet,
IntPtr asyncCallback,
ref IntPtr commandOperationHandle);
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManConnectShellCommand", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManConnectShellCommandEx(IntPtr shellOperationHandle,
int flags,
[MarshalAs(UnmanagedType.LPWStr)]
string commandID,
IntPtr optionSet,
IntPtr connectXml,
IntPtr asyncCallback,
ref IntPtr commandOperationHandle);
/// <summary>
/// Registers a callback with WSMan to receive output from the remote end.
/// If commandOperationHandle is null, then the receive callback is registered
/// for shell. It is enough to register the callback only once. WSMan will
/// keep on calling this callback as and when it has data for a particular
/// command + shell. There will be only 1 callback active per command or per shell.
/// So if there are multiple commands active, then there can be 1 callback active
/// for each of them.
/// TODO: How to unregister the callback.
/// </summary>
/// <param name="shellOperationHandle">
/// Shell Operation Handle.
/// </param>
/// <param name="commandOperationHandle">
/// Command Operation Handle. If null, the receive request corresponds
/// to the shell.
/// </param>
/// <param name="flags"></param>
/// <param name="desiredStreamSet"></param>
/// <param name="asyncCallback">
/// callback which receives the data asynchronously.
/// </param>
/// <param name="receiveOperationHandle">
/// handle to use to cancel the operation.
/// </param>
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManReceiveShellOutput", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManReceiveShellOutputEx(IntPtr shellOperationHandle,
IntPtr commandOperationHandle,
int flags,
IntPtr desiredStreamSet,
IntPtr asyncCallback,
[In, Out] ref IntPtr receiveOperationHandle);
/// <summary>
/// Send data to the remote end.
/// </summary>
/// <param name="shellOperationHandle">
/// Shell Operation Handle.
/// </param>
/// <param name="commandOperationHandle">
/// Command Operation Handle. If null, the send request corresponds
/// to the shell.
/// </param>
/// <param name="flags"></param>
/// <param name="streamId"></param>
/// <param name="streamData"></param>
/// <param name="asyncCallback">
/// callback to notify when the operation completes.
/// </param>
/// <param name="sendOperationHandle">
/// handle to use to cancel the operation.
/// </param>
internal static void WSManSendShellInputEx(IntPtr shellOperationHandle,
IntPtr commandOperationHandle,
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string streamId,
WSManData_ManToUn streamData,
IntPtr asyncCallback,
ref IntPtr sendOperationHandle)
{
WSManSendShellInputExInternal(shellOperationHandle, commandOperationHandle, flags, streamId,
streamData, false, asyncCallback, ref sendOperationHandle);
}
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManSendShellInput", SetLastError = false, CharSet = CharSet.Unicode)]
private static extern void WSManSendShellInputExInternal(IntPtr shellOperationHandle,
IntPtr commandOperationHandle,
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string streamId,
IntPtr streamData,
bool endOfStream,
IntPtr asyncCallback,
[In, Out] ref IntPtr sendOperationHandle);
/// <summary>
/// Closes a shell or a command; if the callback associated with the operation
/// is pending and have not completed when WSManCloseShellOperationEx is called,
/// the function waits for the callback to finish; If the operation was not finished,
/// the operation is cancelled and the operation callback is called with
/// WSMAN_ERROR_OPERATION_ABORTED error; then the WSManCloseShellOperationEx callback
/// is called with WSMAN_FLAG_CALLBACK_END_OF_OPERATION flag as result of this operation.
/// </summary>
/// <param name="shellHandle">
/// Shell handle to Close.
/// </param>
/// <param name="flags"></param>
/// <param name="asyncCallback">
/// callback to notify when the operation completes.
/// </param>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManCloseShell(IntPtr shellHandle,
int flags,
IntPtr asyncCallback);
/// <summary>
/// Closes a command (signals the termination of a command); the WSManCloseCommand callback
/// is called with WSMAN_FLAG_CALLBACK_END_OF_OPERATION flag as result of this operation.
/// </summary>
/// <param name="cmdHandle">
/// Command handle to Close.
/// </param>
/// <param name="flags"></param>
/// <param name="asyncCallback">
/// callback to notify when the operation completes.
/// </param>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManCloseCommand(IntPtr cmdHandle,
int flags,
IntPtr asyncCallback);
/// <summary>
/// Sends a signal. If <paramref name="cmdOperationHandle"/> is null, then the signal will
/// be sent to shell.
/// </summary>
/// <param name="shellOperationHandle"></param>
/// <param name="cmdOperationHandle"></param>
/// <param name="flags"></param>
/// <param name="code"></param>
/// <param name="asyncCallback"></param>
/// <param name="signalOperationHandle"></param>
[DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManSignalShell", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManSignalShellEx(IntPtr shellOperationHandle,
IntPtr cmdOperationHandle,
int flags,
string code,
IntPtr asyncCallback,
[In, Out] ref IntPtr signalOperationHandle);
/// <summary>
/// Closes an asynchronous operation; if the callback associated with the operation
/// is pending and have not completed when WSManCloseOperation is called, then
/// the function marks the operation for deletion and returns; If the callback was not called,
/// the operation is cancelled and the operation callback is called with
/// WSMAN_ERROR_OPERATION_ABORTED error; the operation handle is freed in all cases
/// after the callback returns.
/// </summary>
/// <param name="operationHandle"></param>
/// <param name="flags"></param>
/// <returns></returns>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManCloseOperation(IntPtr operationHandle, int flags);
/// <summary>
/// Function that retrieves WSMan error messages with a particular error code. Thread.CurrentUICulture
/// will be used as the language code to get the error message in.
/// </summary>
/// <param name="wsManAPIHandle"></param>
/// <param name="errorCode"></param>
/// <returns></returns>
internal static string WSManGetErrorMessage(IntPtr wsManAPIHandle, int errorCode)
{
Dbg.Assert(wsManAPIHandle != IntPtr.Zero, "wsManAPIHandle cannot be null.");
// The error code taken from winerror.h used for getting buffer length.
const int ERROR_INSUFFICIENT_BUFFER = 122;
// get language code.
string langCode = CultureInfo.CurrentUICulture.Name;
string returnval = string.Empty;
int bufferSize = 0;
// calculate buffer size required
if (WSManGetErrorMessage(wsManAPIHandle,
0, langCode, errorCode, 0, null, out bufferSize) != ERROR_INSUFFICIENT_BUFFER)
{
return returnval;
}
// calculate space required to store output.
// StringBuilder will not work for this case as CLR
// does not copy the entire string if there are delimiters ('\0')
// in the middle of a string.
int bufferSizeInBytes = bufferSize * 2;
byte[] msgBufferPtr = new byte[bufferSizeInBytes];
// Now get the actual value
int messageLength;
if (WSManGetErrorMessage(wsManAPIHandle,
0, langCode, errorCode, bufferSizeInBytes, msgBufferPtr, out messageLength) != 0)
{
return returnval;
}
try
{
returnval = Encoding.Unicode.GetString(msgBufferPtr, 0, bufferSizeInBytes);
}
catch (ArgumentNullException)
{
}
catch (System.Text.DecoderFallbackException)
{
}
catch (ArgumentOutOfRangeException)
{
}
return returnval;
}
/// <summary>
/// Function that retrieves WSMan error messages with a particular error code and a language code.
/// </summary>
/// <param name="wsManAPIHandle">
/// The handle returned by WSManInitialize API call. It cannot be NULL.
/// </param>
/// <param name="flags">
/// Reserved for future use. It must be 0.
/// </param>
/// <param name="languageCode">
/// Defines the RFC 3066 language code name that should be used to localize the error. It can be NULL.
/// if not specified, the thread's UI language will be used.
/// </param>
/// <param name="errorCode">
/// Represents the error code for the requested error message. This error code can be a hexadecimal or
/// decimal component from WSManagement component, WinHttp component or other Windows operating system
/// components.
/// </param>
/// <param name="messageLength">
/// Represents the size of the output message buffer in characters, including the NULL terminator.
/// If 0, then the "message" parameter must be NULL; in this case the function will return
/// ERROR_INSUFFICIENT_BUFFER error and the "messageLengthUsed" parameter will be set to the number
/// of characters needed, including NULL terminator.
/// </param>
/// <param name="message">
/// Represents the output buffer to store the message in. It must be allocated/deallocated by the client.
/// The buffer must be big enough to store the message plus the NULL terminator otherwise an
/// ERROR_INSUFFICIENT_BUFFER error will be returned and the "messageLengthUsed" parameter will be set
/// to the number of characters needed, including NULL terminator. If NULL, then the "messageLength" parameter
/// must be NULL; in this case the function will return ERROR_INSUFFICIENT_BUFFER error and the "messageLengthUsed"
/// parameter will be set to the number of characters needed, including NULL terminator.
/// </param>
/// <param name="messageLengthUsed">
/// Represents the effective number of characters written to the output buffer, including the NULL terminator.
/// It cannot be NULL. If both "messageLength" and "message" parameters are 0, the function will return ERROR_INSUFFICIENT_BUFFER
/// and "messageLengthUsed" parameter will be set to the number of characters needed, including NULL terminator
/// </param>
[DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManGetErrorMessage(IntPtr wsManAPIHandle,
int flags,
string languageCode,
int errorCode,
int messageLength,
byte[] message,
out int messageLengthUsed);
#endregion
#region DllImports PluginAPI
/// <summary>
/// Gets operational information for items such as time-outs and data restrictions that
/// are associated with the operation.
/// </summary>
/// <param name="requestDetails">Specifies the resource URI, options, locale, shutdown flag, and handle for the request.</param>
/// <param name="flags">Specifies the options that are available for retrieval.</param>
/// <param name="data">Specifies the result object (WSMAN_DATA).</param>
/// <returns></returns>
[DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManPluginGetOperationParameters(
IntPtr requestDetails,
int flags,
[In, Out, MarshalAs(UnmanagedType.LPStruct)] WSManDataStruct data);
// [In, Out] ref IntPtr data);
/// <summary>
/// Reports the completion of an operation by all operation entry points except for the
/// WSManPluginStartup and WSManPluginShutdown methods.
/// </summary>
/// <param name="requestDetails">Specifies the resource URI, options, locale, shutdown flag, and handle for the request.</param>
/// <param name="flags"></param>
/// <param name="errorCode">Reports any failure in the operation. Terminates on non-NO_ERROR status.</param>
/// <param name="extendedInformation">XML document containing extra error information.</param>
/// <returns></returns>
[DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManPluginOperationComplete(
IntPtr requestDetails,
int flags,
int errorCode,
[MarshalAs(UnmanagedType.LPWStr)] string extendedInformation);
internal enum WSManFlagReceive : int
{
/// <summary>
/// No more data on this stream. Only valid when a stream is specified.
/// </summary>
WSMAN_FLAG_RECEIVE_RESULT_NO_MORE_DATA = 1,
/// <summary>
/// Send the data as soon as possible. Normally data is held onto in
/// order to maximise the size of the response packet. This should
/// only be used if a request/response style of data is needed between
/// the send and receive data streams.
/// </summary>
WSMAN_FLAG_RECEIVE_FLUSH = 2,
/// <summary>
/// Data reported is at a boundary. Plugins usually serialize and fragment
/// output data objects and push them along the receive byte stream.
/// If the current data chunk being reported is an end fragment of the
/// data object current processed, plugins would set this flag.
/// </summary>
WSMAN_FLAG_RECEIVE_RESULT_DATA_BOUNDARY = 4
}
internal const string WSMAN_SHELL_NAMESPACE = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell";
internal const string WSMAN_COMMAND_STATE_DONE = WSMAN_SHELL_NAMESPACE + "/CommandState/Done";
internal const string WSMAN_COMMAND_STATE_PENDING = WSMAN_SHELL_NAMESPACE + "/CommandState/Pending";
internal const string WSMAN_COMMAND_STATE_RUNNING = WSMAN_SHELL_NAMESPACE + "/CommandState/Running";
/// <summary>
/// Reports results for the WSMAN_PLUGIN_RECEIVE plug-in call and is used by most shell
/// plug-ins that return results. After all of the data is received, the
/// WSManPluginOperationComplete method must be called.
/// </summary>
/// <param name="requestDetails">Specifies the resource URI, options, locale, shutdown flag, and handle for the request.</param>
/// <param name="flags"></param>
/// <param name="stream">Specifies the stream that the data is associated with.</param>
/// <param name="streamResult">A pointer to a WSMAN_DATA structure that specifies the result object that is returned to the client.</param>
/// <param name="commandState">Specifies the state of the command. It must be set to a value specified by the plugin.</param>
/// <param name="exitCode">Only set when the commandState is terminating.</param>
/// <returns></returns>
[DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManPluginReceiveResult(
IntPtr requestDetails,
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string stream,
IntPtr streamResult,
[MarshalAs(UnmanagedType.LPWStr)] string commandState,
int exitCode);
/// <summary>
/// Reports shell and command context back to the Windows Remote Management (WinRM)
/// infrastructure so that further operations can be performed against the shell and/or
/// command. This method is called only for WSManPluginShell and WSManPluginCommand plug-in
/// entry points.
/// </summary>
/// <param name="requestDetails">Specifies the resource URI, options, locale, shutdown flag, and handle for the request.</param>
/// <param name="flags"></param>
/// <param name="context">Defines the value to pass into all future shell and command operations. Represents either the shell or the command.</param>
/// <returns></returns>
[DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int WSManPluginReportContext(
IntPtr requestDetails,
int flags,
IntPtr context);
#if UNIX
/// <summary>
/// Registers the shutdown callback.
/// </summary>
/// <param name="requestDetails">Specifies the resource URI, options, locale, shutdown flag, and handle for the request.</param>
/// <param name="shutdownCallback">Callback to be executed on shutdown.</param>
/// <param name="shutdownContext"></param>
/// <returns></returns>
[DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern void WSManPluginRegisterShutdownCallback(
IntPtr requestDetails,
IntPtr shutdownCallback,
IntPtr shutdownContext);
#endif
#endregion
}
/// <summary>
/// Interface to enable stubbing of the WSManNativeApi PInvoke calls for
/// unit testing.
/// Note: It is implemented as a class to avoid exposing it outside the module.
/// </summary>
#nullable enable
internal interface IWSManNativeApiFacade
{
// TODO: Expand this to cover the rest of the API once I prove that it works!
int WSManPluginGetOperationParameters(
IntPtr requestDetails,
int flags,
WSManNativeApi.WSManDataStruct data);
int WSManPluginOperationComplete(
IntPtr requestDetails,
int flags,
int errorCode,
string extendedInformation);
int WSManPluginReceiveResult(
IntPtr requestDetails,
int flags,
string? stream,
IntPtr streamResult,
string commandState,
int exitCode);
int WSManPluginReportContext(
IntPtr requestDetails,
int flags,
IntPtr context);
void WSManPluginRegisterShutdownCallback(
IntPtr requestDetails,
IntPtr shutdownCallback,
IntPtr shutdownContext);
}
#nullable restore
/// <summary>
/// Concrete implementation of the PInvoke facade for use in the production code.
/// </summary>
internal class WSManNativeApiFacade : IWSManNativeApiFacade
{
int IWSManNativeApiFacade.WSManPluginGetOperationParameters(
IntPtr requestDetails,
int flags,
WSManNativeApi.WSManDataStruct data)
{
return WSManNativeApi.WSManPluginGetOperationParameters(requestDetails, flags, data);
}
int IWSManNativeApiFacade.WSManPluginOperationComplete(
IntPtr requestDetails,
int flags,
int errorCode,
string extendedInformation)
{
return WSManNativeApi.WSManPluginOperationComplete(requestDetails, flags, errorCode, extendedInformation);
}
int IWSManNativeApiFacade.WSManPluginReceiveResult(
IntPtr requestDetails,
int flags,
string stream,
IntPtr streamResult,
string commandState,
int exitCode)
{
return WSManNativeApi.WSManPluginReceiveResult(requestDetails, flags, stream, streamResult, commandState, exitCode);
}
int IWSManNativeApiFacade.WSManPluginReportContext(
IntPtr requestDetails,
int flags,
IntPtr context)
{
return WSManNativeApi.WSManPluginReportContext(requestDetails, flags, context);
}
void IWSManNativeApiFacade.WSManPluginRegisterShutdownCallback(
IntPtr requestDetails,
IntPtr shutdownCallback,
IntPtr shutdownContext)
{
#if UNIX
WSManNativeApi.WSManPluginRegisterShutdownCallback(requestDetails, shutdownCallback, shutdownContext);
#endif
}
}
}
|