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
/*
 * PROJECT:         ReactOS Win32 Base API
 * LICENSE:         GPL - See COPYING in the top level directory
 * FILE:            dll/win32/kernel32/client/vdm.c
 * PURPOSE:         Virtual DOS Machines (VDM) Support
 * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
 */

/* INCLUDES *******************************************************************/

#include <k32.h>

#define NDEBUG
#include <debug.h>

/* TYPES **********************************************************************/

#define BINARY_UNKNOWN  (0)
#define BINARY_PE_EXE32 (1)
#define BINARY_PE_DLL32 (2)
#define BINARY_PE_EXE64 (3)
#define BINARY_PE_DLL64 (4)
#define BINARY_WIN16    (5)
#define BINARY_OS216    (6)
#define BINARY_DOS      (7)
#define BINARY_UNIX_EXE (8)
#define BINARY_UNIX_LIB (9)


typedef enum _ENV_NAME_TYPE
{
    EnvNameNotAPath = 1,
    EnvNameSinglePath ,
    EnvNameMultiplePath
} ENV_NAME_TYPE;

typedef struct _ENV_INFO
{
    ENV_NAME_TYPE NameType;
    ULONG  NameLength;
    PWCHAR Name;
} ENV_INFO, *PENV_INFO;

/* GLOBALS ********************************************************************/

#define ENV_NAME_ENTRY(type, name)  \
    {(type), _ARRAYSIZE(name) - 1, (name)}

static ENV_INFO BasepEnvNameType[] =
{
    ENV_NAME_ENTRY(EnvNameMultiplePath, L"PATH"),
    ENV_NAME_ENTRY(EnvNameSinglePath  , L"WINDIR"),
    ENV_NAME_ENTRY(EnvNameSinglePath  , L"SYSTEMROOT"),
    ENV_NAME_ENTRY(EnvNameMultiplePath, L"TEMP"),
    ENV_NAME_ENTRY(EnvNameMultiplePath, L"TMP"),
};

static UNICODE_STRING BaseDotComSuffixName = RTL_CONSTANT_STRING(L".com");
static UNICODE_STRING BaseDotPifSuffixName = RTL_CONSTANT_STRING(L".pif");
static UNICODE_STRING BaseDotExeSuffixName = RTL_CONSTANT_STRING(L".exe");

/* FUNCTIONS ******************************************************************/

ULONG
WINAPI
BaseIsDosApplication(IN PUNICODE_STRING PathName,
                     IN NTSTATUS Status)
{
    UNICODE_STRING String;

    /* Is it a .com? */
    String.Length = BaseDotComSuffixName.Length;
    String.Buffer = &PathName->Buffer[(PathName->Length - String.Length) / sizeof(WCHAR)];
    if (RtlEqualUnicodeString(&String, &BaseDotComSuffixName, TRUE)) return BINARY_TYPE_COM;

    /* Is it a .pif? */
    String.Length = BaseDotPifSuffixName.Length;
    String.Buffer = &PathName->Buffer[(PathName->Length - String.Length) / sizeof(WCHAR)];
    if (RtlEqualUnicodeString(&String, &BaseDotPifSuffixName, TRUE)) return BINARY_TYPE_PIF;

    /* Is it an exe? */
    String.Length = BaseDotExeSuffixName.Length;
    String.Buffer = &PathName->Buffer[(PathName->Length - String.Length) / sizeof(WCHAR)];
    if (RtlEqualUnicodeString(&String, &BaseDotExeSuffixName, TRUE)) return BINARY_TYPE_EXE;

    return 0;
}

NTSTATUS
WINAPI
BaseCheckVDM(IN ULONG BinaryType,
             IN PCWCH ApplicationName,
             IN PCWCH CommandLine,
             IN PCWCH CurrentDirectory,
             IN PANSI_STRING AnsiEnvironment,
             IN PBASE_API_MESSAGE ApiMessage,
             IN OUT PULONG iTask,
             IN DWORD CreationFlags,
             IN LPSTARTUPINFOW StartupInfo,
             IN HANDLE hUserToken OPTIONAL)
{
    NTSTATUS Status;
    PBASE_CHECK_VDM CheckVdm = &ApiMessage->Data.CheckVDMRequest;
    PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
    PWCHAR CurrentDir = NULL;
    PWCHAR ShortAppName = NULL;
    PWCHAR ShortCurrentDir = NULL;
    ULONG Length;
    PCHAR AnsiCmdLine = NULL;
    PCHAR AnsiAppName = NULL;
    PCHAR AnsiCurDirectory = NULL;
    PCHAR AnsiDesktop = NULL;
    PCHAR AnsiTitle = NULL;
    PCHAR AnsiReserved = NULL;
    STARTUPINFOA AnsiStartupInfo;
    ULONG NumStrings = 5;

    /* Parameters validation */
    if (ApplicationName == NULL || CommandLine == NULL)
    {
        return STATUS_INVALID_PARAMETER;
    }

    /* Trim leading whitespace from ApplicationName */
    while (*ApplicationName == L' ' || *ApplicationName == L'\t')
        ++ApplicationName;

    /* Calculate the size of the short application name */
    Length = GetShortPathNameW(ApplicationName, NULL, 0);
    if (Length == 0)
    {
        Status = STATUS_OBJECT_PATH_INVALID;
        goto Cleanup;
    }

    /* Allocate memory for the short application name */
    ShortAppName = (PWCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
                                           HEAP_ZERO_MEMORY,
                                           Length * sizeof(WCHAR));
    if (!ShortAppName)
    {
        Status = STATUS_NO_MEMORY;
        goto Cleanup;
    }

    /* Get the short application name */
    if (GetShortPathNameW(ApplicationName, ShortAppName, Length) == 0)
    {
        /* Try to determine which error occurred */
        switch (GetLastError())
        {
            case ERROR_NOT_ENOUGH_MEMORY:
            {
                Status = STATUS_NO_MEMORY;
                break;
            }

            case ERROR_INVALID_PARAMETER:
            {
                Status = STATUS_INVALID_PARAMETER;
                break;
            }

            default:
            {
                Status = STATUS_OBJECT_PATH_INVALID;
            }
        }

        goto Cleanup;
    }

    /* Trim leading whitespace from CommandLine */
    while (*CommandLine == L' ' || *CommandLine == L'\t')
        ++CommandLine;

    /*
     * CommandLine is usually formatted as: 'ApplicationName param0 ...'.
     * So we want to strip the first token (ApplicationName) from it.
     * Two cases are in fact possible:
     * - either the first token is indeed ApplicationName, so we just skip it;
     * - or the first token is not exactly ApplicationName, because it happened
     *   that somebody else already preprocessed CommandLine. Therefore we
     *   suppose that the first token corresponds to an application name and
     *   we skip it. Care should be taken when quotes are present in this token.
     */
     if (*CommandLine)
     {
        /* The first part of CommandLine should be the ApplicationName... */
        Length = wcslen(ApplicationName);
        if (Length <= wcslen(CommandLine) &&
            _wcsnicmp(ApplicationName, CommandLine, Length) == 0)
        {
            /* Skip it */
            CommandLine += Length;
        }
        /*
         * ... but it is not, however we still have a token. We suppose that
         * it corresponds to some sort of application name, so we skip it too.
         */
        else
        {
            /* Get rid of the first token. We stop when we see whitespace. */
            while (*CommandLine && !(*CommandLine == L' ' || *CommandLine == L'\t'))
            {
                if (*CommandLine == L'\"')
                {
                    /* We enter a quoted part, skip it */
                    ++CommandLine;
                    while (*CommandLine && *CommandLine++ != L'\"') ;
                }
                else
                {
                    /* Go to the next character */
                    ++CommandLine;
                }
            }
        }
    }

    /*
     * Trim remaining whitespace from CommandLine that may be
     * present between the application name and the parameters.
     */
    while (*CommandLine == L' ' || *CommandLine == L'\t')
        ++CommandLine;

    /* Get the current directory */
    if (CurrentDirectory == NULL)
    {
        /* Allocate memory for the current directory path */
        Length = GetCurrentDirectoryW(0, NULL);
        CurrentDir = (PWCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
                                             HEAP_ZERO_MEMORY,
                                             Length * sizeof(WCHAR));
        if (CurrentDir == NULL)
        {
            Status = STATUS_NO_MEMORY;
            goto Cleanup;
        }

        /* Get the current directory */
        GetCurrentDirectoryW(Length, CurrentDir);
        CurrentDirectory = CurrentDir;
    }

    /* Calculate the size of the short current directory path */
    Length = GetShortPathNameW(CurrentDirectory, NULL, 0);

    /* Allocate memory for the short current directory path */
    ShortCurrentDir = (PWCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
                                              HEAP_ZERO_MEMORY,
                                              Length * sizeof(WCHAR));
    if (!ShortCurrentDir)
    {
        Status = STATUS_NO_MEMORY;
        goto Cleanup;
    }

    /* Get the short current directory path */
    if (!GetShortPathNameW(CurrentDirectory, ShortCurrentDir, Length))
    {
        /* Try to determine which error occurred */
        switch (GetLastError())
        {
            case ERROR_NOT_ENOUGH_MEMORY:
            {
                Status = STATUS_NO_MEMORY;
                break;
            }

            case ERROR_INVALID_PARAMETER:
            {
                Status = STATUS_INVALID_PARAMETER;
                break;
            }

            default:
            {
                Status = STATUS_OBJECT_PATH_INVALID;
            }
        }
        goto Cleanup;
    }

    /* Setup the input parameters */
    CheckVdm->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
    CheckVdm->BinaryType = BinaryType;
    CheckVdm->CodePage = CP_ACP;
    CheckVdm->dwCreationFlags = CreationFlags;
    CheckVdm->CurDrive = CurrentDirectory[0] - L'A';
    CheckVdm->CmdLen = wcslen(CommandLine) + 1;
    CheckVdm->AppLen = wcslen(ShortAppName) + 1;
    CheckVdm->PifLen = 0; // TODO: PIF file support!
    CheckVdm->CurDirectoryLen = wcslen(ShortCurrentDir) + 1;
    CheckVdm->EnvLen = AnsiEnvironment->Length;
    CheckVdm->DesktopLen = (StartupInfo->lpDesktop != NULL) ? (wcslen(StartupInfo->lpDesktop) + 1) : 0;
    CheckVdm->TitleLen = (StartupInfo->lpTitle != NULL) ? (wcslen(StartupInfo->lpTitle) + 1) : 0;
    CheckVdm->ReservedLen = (StartupInfo->lpReserved != NULL) ? (wcslen(StartupInfo->lpReserved) + 1) : 0;

    if (StartupInfo->dwFlags & STARTF_USESTDHANDLES)
    {
        /* Set the standard handles */
        CheckVdm->StdIn  = StartupInfo->hStdInput;
        CheckVdm->StdOut = StartupInfo->hStdOutput;
        CheckVdm->StdErr = StartupInfo->hStdError;
    }

    /* Allocate memory for the ANSI strings */
    // We need to add the newline characters '\r\n' to the command line
    AnsiCmdLine = (PCHAR)RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, CheckVdm->CmdLen + 2);
    AnsiAppName = (PCHAR)RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, CheckVdm->AppLen);
    AnsiCurDirectory = (PCHAR)RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, CheckVdm->CurDirectoryLen);
    if (StartupInfo->lpDesktop)
        AnsiDesktop = (PCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
                                             HEAP_ZERO_MEMORY,
                                             CheckVdm->DesktopLen);
    if (StartupInfo->lpTitle)
        AnsiTitle = (PCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
                                           HEAP_ZERO_MEMORY,
                                           CheckVdm->TitleLen);
    if (StartupInfo->lpReserved)
        AnsiReserved = (PCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
                                              HEAP_ZERO_MEMORY,
                                              CheckVdm->ReservedLen);

    if (!AnsiCmdLine
        || !AnsiAppName
        || !AnsiCurDirectory
        || (StartupInfo->lpDesktop && !AnsiDesktop)
        || (StartupInfo->lpTitle && !AnsiTitle)
        || (StartupInfo->lpReserved && !AnsiReserved))
    {
        Status = STATUS_NO_MEMORY;
        goto Cleanup;
    }

    /* Convert the command line into an ANSI string */
    WideCharToMultiByte(CP_ACP,
                        0,
                        CommandLine,
                        CheckVdm->CmdLen,
                        AnsiCmdLine,
                        CheckVdm->CmdLen,
                        NULL,
                        NULL);
    /* Add the needed newline and NULL-terminate */
    CheckVdm->CmdLen--; // Rewind back to the NULL character
    AnsiCmdLine[CheckVdm->CmdLen++] = '\r';
    AnsiCmdLine[CheckVdm->CmdLen++] = '\n';
    AnsiCmdLine[CheckVdm->CmdLen++] = 0;

    /* Convert the short application name into an ANSI string */
    WideCharToMultiByte(CP_ACP,
                        0,
                        ShortAppName,
                        CheckVdm->AppLen,
                        AnsiAppName,
                        CheckVdm->AppLen,
                        NULL,
                        NULL);

    /* Convert the short current directory path into an ANSI string */
    WideCharToMultiByte(CP_ACP,
                        0,
                        ShortCurrentDir,
                        CheckVdm->CurDirectoryLen,
                        AnsiCurDirectory,
                        CheckVdm->CurDirectoryLen,
                        NULL,
                        NULL);

    if (StartupInfo->lpDesktop)
    {
        /* Convert the desktop name into an ANSI string */
        WideCharToMultiByte(CP_ACP,
                            0,
                            StartupInfo->lpDesktop,
                            CheckVdm->DesktopLen,
                            AnsiDesktop,
                            CheckVdm->DesktopLen,
                            NULL,
                            NULL);
        NumStrings++;
    }

    if (StartupInfo->lpTitle)
    {
        /* Convert the title into an ANSI string */
        WideCharToMultiByte(CP_ACP,
                            0,
                            StartupInfo->lpTitle,
                            CheckVdm->TitleLen,
                            AnsiTitle,
                            CheckVdm->TitleLen,
                            NULL,
                            NULL);
        NumStrings++;
    }

    if (StartupInfo->lpReserved)
    {
        /* Convert the reserved value into an ANSI string */
        WideCharToMultiByte(CP_ACP,
                            0,
                            StartupInfo->lpReserved,
                            CheckVdm->ReservedLen,
                            AnsiReserved,
                            CheckVdm->ReservedLen,
                            NULL,
                            NULL);
        NumStrings++;
    }

    /* Fill the ANSI startup info structure */
    RtlCopyMemory(&AnsiStartupInfo, StartupInfo, sizeof(AnsiStartupInfo));
    AnsiStartupInfo.lpReserved = AnsiReserved;
    AnsiStartupInfo.lpDesktop = AnsiDesktop;
    AnsiStartupInfo.lpTitle = AnsiTitle;

    /* Allocate the capture buffer */
    CaptureBuffer = CsrAllocateCaptureBuffer(NumStrings,
                                             CheckVdm->CmdLen
                                             + CheckVdm->AppLen
                                             + CheckVdm->PifLen
                                             + CheckVdm->CurDirectoryLen
                                             + CheckVdm->DesktopLen
                                             + CheckVdm->TitleLen
                                             + CheckVdm->ReservedLen
                                             + CheckVdm->EnvLen
                                             + sizeof(*CheckVdm->StartupInfo));
    if (CaptureBuffer == NULL)
    {
        Status = STATUS_NO_MEMORY;
        goto Cleanup;
    }

    /* Capture the command line */
    CsrCaptureMessageBuffer(CaptureBuffer,
                            AnsiCmdLine,
                            CheckVdm->CmdLen,
                            (PVOID*)&CheckVdm->CmdLine);

    /* Capture the application name */
    CsrCaptureMessageBuffer(CaptureBuffer,
                            AnsiAppName,
                            CheckVdm->AppLen,
                            (PVOID*)&CheckVdm->AppName);

    CheckVdm->PifFile = NULL; // TODO: PIF file support!

    /* Capture the current directory */
    CsrCaptureMessageBuffer(CaptureBuffer,
                            AnsiCurDirectory,
                            CheckVdm->CurDirectoryLen,
                            (PVOID*)&CheckVdm->CurDirectory);

    /* Capture the environment */
    CsrCaptureMessageBuffer(CaptureBuffer,
                            AnsiEnvironment->Buffer,
                            CheckVdm->EnvLen,
                            (PVOID*)&CheckVdm->Env);

    /* Capture the startup info structure */
    CsrCaptureMessageBuffer(CaptureBuffer,
                            &AnsiStartupInfo,
                            sizeof(*CheckVdm->StartupInfo),
                            (PVOID*)&CheckVdm->StartupInfo);

    if (StartupInfo->lpDesktop)
    {
        /* Capture the desktop name */
        CsrCaptureMessageBuffer(CaptureBuffer,
                                AnsiDesktop,
                                CheckVdm->DesktopLen,
                                (PVOID*)&CheckVdm->Desktop);
    }
    else CheckVdm->Desktop = NULL;

    if (StartupInfo->lpTitle)
    {
        /* Capture the title */
        CsrCaptureMessageBuffer(CaptureBuffer,
                                AnsiTitle,
                                CheckVdm->TitleLen,
                                (PVOID*)&CheckVdm->Title);
    }
    else CheckVdm->Title = NULL;

    if (StartupInfo->lpReserved)
    {
        /* Capture the reserved parameter */
        CsrCaptureMessageBuffer(CaptureBuffer,
                                AnsiReserved,
                                CheckVdm->ReservedLen,
                                (PVOID*)&CheckVdm->Reserved);
    }
    else CheckVdm->Reserved = NULL;

    /* Send the message to CSRSS */
    Status = CsrClientCallServer((PCSR_API_MESSAGE)ApiMessage,
                                 CaptureBuffer,
                                 CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepCheckVDM),
                                 sizeof(*CheckVdm));

    /* Write back the task ID */
    *iTask = CheckVdm->iTask;

Cleanup:

    /* Free the ANSI strings */
    if (AnsiCmdLine) RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiCmdLine);
    if (AnsiAppName) RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiAppName);
    if (AnsiCurDirectory) RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiCurDirectory);
    if (AnsiDesktop) RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiDesktop);
    if (AnsiTitle) RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiTitle);
    if (AnsiReserved) RtlFreeHeap(RtlGetProcessHeap(), 0, AnsiReserved);

    /* Free the capture buffer */
    if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);

    /* Free the current directory, if it was allocated here, and its short path */
    if (ShortCurrentDir) RtlFreeHeap(RtlGetProcessHeap(), 0, ShortCurrentDir);
    if (CurrentDir) RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentDir);

    /* Free the short app name */
    if (ShortAppName) RtlFreeHeap(RtlGetProcessHeap(), 0, ShortAppName);

    return Status;
}

BOOL
WINAPI
BaseUpdateVDMEntry(IN ULONG UpdateIndex,
                   IN OUT PHANDLE WaitHandle,
                   IN ULONG IndexInfo,
                   IN ULONG BinaryType)
{
    BASE_API_MESSAGE ApiMessage;
    PBASE_UPDATE_VDM_ENTRY UpdateVdmEntry = &ApiMessage.Data.UpdateVDMEntryRequest;

    /* Check what update is being sent */
    switch (UpdateIndex)
    {
        /* VDM is being undone */
        case VdmEntryUndo:
        {
            /* Tell the server how far we had gotten along */
            UpdateVdmEntry->iTask = HandleToUlong(*WaitHandle);
            UpdateVdmEntry->VDMCreationState = IndexInfo;
            break;
        }

        /* VDM is ready with a new process handle */
        case VdmEntryUpdateProcess:
        {
            /* Send it the process handle */
            UpdateVdmEntry->VDMProcessHandle = *WaitHandle;
            UpdateVdmEntry->iTask = IndexInfo;
            break;
        }
    }

    /* Also check what kind of binary this is for the console handle */
    if (BinaryType == BINARY_TYPE_WOW)
    {
        /* Magic value for 16-bit apps */
        UpdateVdmEntry->ConsoleHandle = (HANDLE)-1;
    }
    else if (UpdateVdmEntry->iTask)
    {
        /* No handle for true VDM */
        UpdateVdmEntry->ConsoleHandle = NULL;
    }
    else
    {
        /* Otherwise, use the regular console handle */
        UpdateVdmEntry->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
    }

    /* Finally write the index and binary type */
    UpdateVdmEntry->EntryIndex = UpdateIndex;
    UpdateVdmEntry->BinaryType = BinaryType;

    /* Send the message to CSRSS */
    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                        NULL,
                        CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepUpdateVDMEntry),
                        sizeof(*UpdateVdmEntry));
    if (!NT_SUCCESS(ApiMessage.Status))
    {
        /* Handle failure */
        BaseSetLastNTError(ApiMessage.Status);
        return FALSE;
    }

    /* If this was an update, CSRSS returns a new wait handle */
    if (UpdateIndex == VdmEntryUpdateProcess)
    {
        /* Return it to the caller */
        *WaitHandle = UpdateVdmEntry->WaitObjectForParent;
    }

    /* We made it */
    return TRUE;
}

BOOL
WINAPI
BaseCheckForVDM(IN HANDLE ProcessHandle,
                OUT LPDWORD ExitCode)
{
    NTSTATUS Status;
    EVENT_BASIC_INFORMATION EventBasicInfo;
    BASE_API_MESSAGE ApiMessage;
    PBASE_GET_VDM_EXIT_CODE GetVdmExitCode = &ApiMessage.Data.GetVDMExitCodeRequest;

    /* It's VDM if the process is actually a wait handle (an event) */
    Status = NtQueryEvent(ProcessHandle,
                          EventBasicInformation,
                          &EventBasicInfo,
                          sizeof(EventBasicInfo),
                          NULL);
    if (!NT_SUCCESS(Status)) return FALSE;

    /* Setup the input parameters */
    GetVdmExitCode->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
    GetVdmExitCode->hParent = ProcessHandle;

    /* Call CSRSS */
    Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                                 NULL,
                                 CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepGetVDMExitCode),
                                 sizeof(*GetVdmExitCode));
    if (!NT_SUCCESS(Status)) return FALSE;

    /* Get the exit code from the reply */
    *ExitCode = GetVdmExitCode->ExitCode;
    return TRUE;
}

BOOL
WINAPI
BaseGetVdmConfigInfo(IN LPCWSTR CommandLineReserved,
                     IN ULONG DosSeqId,
                     IN ULONG BinaryType,
                     IN PUNICODE_STRING CmdLineString,
                     OUT PULONG VdmSize)
{
    WCHAR Buffer[MAX_PATH];
    WCHAR CommandLine[MAX_PATH * 2];
    ULONG Length;

    /* Clear the buffer in case we fail */
    CmdLineString->Buffer = 0;

    /* Always return the same size: 16 Mb */
    *VdmSize = 0x1000000;

    /* Get the system directory */
    Length = GetSystemDirectoryW(Buffer, MAX_PATH);
    if (!(Length) || (Length >= MAX_PATH))
    {
        /* Eliminate no path or path too big */
        SetLastError(ERROR_INVALID_NAME);
        return FALSE;
    }

    /* Check if this is VDM with a DOS Sequence ID */
    if (DosSeqId)
    {
        /*
         * Build the VDM string for it:
         * -i%lx : Gives the DOS Sequence ID;
         * %s%c  : Nothing if DOS VDM, -w if WoW VDM, -ws if separate WoW VDM.
         */
        _snwprintf(CommandLine,
                   ARRAYSIZE(CommandLine),
                   L"\"%s\\ntvdm.exe\" -i%lx %s%c",
                   Buffer,
                   DosSeqId,
                   (BinaryType == BINARY_TYPE_DOS) ? L" " : L"-w",
                   (BinaryType == BINARY_TYPE_SEPARATE_WOW) ? L's' : L' ');
    }
    else
    {
        /*
         * Build the string for it without the DOS Sequence ID:
         * %s%c  : Nothing if DOS VDM, -w if WoW VDM, -ws if separate WoW VDM.
         */
        _snwprintf(CommandLine,
                   ARRAYSIZE(CommandLine),
                   L"\"%s\\ntvdm.exe\" %s%c",
                   Buffer,
                   (BinaryType == BINARY_TYPE_DOS) ? L" " : L"-w",
                   (BinaryType == BINARY_TYPE_SEPARATE_WOW) ? L's' : L' ');
    }

    /* Create the actual string */
    return RtlCreateUnicodeString(CmdLineString, CommandLine);
}

ENV_NAME_TYPE
WINAPI
BaseGetEnvNameType_U(IN PWCHAR Name,
                     IN ULONG NameLength)
{
    PENV_INFO EnvInfo;
    ENV_NAME_TYPE NameType;
    ULONG i;

    /* Start by assuming the environment variable doesn't describe paths */
    NameType = EnvNameNotAPath;

    /* Loop all the environment names */
    for (i = 0; i < ARRAYSIZE(BasepEnvNameType); i++)
    {
        /* Get this entry */
        EnvInfo = &BasepEnvNameType[i];

        /* Check if it matches the name */
        if ((EnvInfo->NameLength == NameLength) &&
            (_wcsnicmp(EnvInfo->Name, Name, NameLength) == 0))
        {
            /* It does, return the type */
            NameType = EnvInfo->NameType;
            break;
        }
    }

    return NameType;
}

BOOL
NTAPI
BaseCreateVDMEnvironment(IN PWCHAR lpEnvironment,
                         OUT PANSI_STRING AnsiEnv,
                         OUT PUNICODE_STRING UnicodeEnv)
{
#define IS_ALPHA(x)   \
    ( ((x) >= L'A' && (x) <= L'Z') || ((x) >= L'a' && (x) <= L'z') )

// From lib/rtl/path.c :
// Can be put in some .h ??
#define IS_PATH_SEPARATOR(x)    ((x) == L'\\' || (x) == L'/')

    BOOL Success = FALSE;
    NTSTATUS Status;
    ULONG EnvironmentSize = 0;
    SIZE_T RegionSize;
    PWCHAR Environment, NewEnvironment = NULL;
    ENV_NAME_TYPE NameType;
    ULONG NameLength, NumChars, Remaining;
    PWCHAR SourcePtr, DestPtr, StartPtr;

    /* Make sure we have both strings */
    if (!AnsiEnv || !UnicodeEnv)
    {
        /* Fail */
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    /* Check if an environment was passed in */
    if (!lpEnvironment)
    {
        /* Nope, create one */
        Status = RtlCreateEnvironment(TRUE, &Environment);
        if (!NT_SUCCESS(Status)) goto Cleanup;
    }
    else
    {
        /* Use the one we got */
        Environment = lpEnvironment;
    }

    /* Do we have something now ? */
    if (!Environment)
    {
        /* Still not, bail out */
        SetLastError(ERROR_BAD_ENVIRONMENT);
        goto Cleanup;
    }

    /*
     * Count how much space the whole environment takes. The environment block is
     * doubly NULL-terminated (NULL from last string and final NULL terminator).
     */
    SourcePtr = Environment;
    while (!(*SourcePtr++ == UNICODE_NULL && *SourcePtr == UNICODE_NULL))
        ++EnvironmentSize;
    EnvironmentSize += 2; // Add the two terminating NULLs

    /*
     * Allocate a new copy large enough to hold all the environment with paths
     * in their short form. Since the short form of a path can be a bit longer
     * than its long form, for example in the case where characters that are
     * invalid in the 8.3 representation are present in the long path name:
     *   'C:\\a+b' --> 'C:\\A_B~1', or:
     *   'C:\\a b' --> 'C:\\AB2761~1' (with checksum inserted),
     * we suppose that the possible total number of extra characters needed to
     * convert the long paths into their short form is at most equal to MAX_PATH.
     */
    RegionSize = (EnvironmentSize + MAX_PATH) * sizeof(WCHAR);
    Status = NtAllocateVirtualMemory(NtCurrentProcess(),
                                     (PVOID*)&NewEnvironment,
                                     0,
                                     &RegionSize,
                                     MEM_COMMIT,
                                     PAGE_READWRITE);
    if (!NT_SUCCESS(Status))
    {
        /* We failed, bail out */
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        NewEnvironment = NULL;
        goto Cleanup;
    }

    /* Parse the environment block */
    Remaining = MAX_PATH - 2; // '-2': remove the last two NULLs. FIXME: is it really needed??
    SourcePtr = Environment;
    DestPtr   = NewEnvironment;

    /* Loop through all the environment strings */
    while (*SourcePtr != UNICODE_NULL)
    {
        /*
         * 1. Check the type of the environment variable and copy its name.
         */

        /* Regular environment variable */
        if (*SourcePtr != L'=')
        {
            StartPtr = SourcePtr;

            /* Copy the environment variable name, including the '=' */
            while (*SourcePtr != UNICODE_NULL)
            {
                *DestPtr++ = *SourcePtr;
                if (*SourcePtr++ == L'=') break;
            }

            /* Guess the type of the environment variable */
            NameType = BaseGetEnvNameType_U(StartPtr, SourcePtr - StartPtr - 1);
        }
        /* 'Current directory' environment variable (i.e. of '=X:=' form) */
        else // if (*SourcePtr == L'=')
        {
            /* First assume we have a possibly malformed environment variable */
            NameType = EnvNameNotAPath;

            /* Check for a valid 'Current directory' environment variable */
            if (IS_ALPHA(SourcePtr[1]) && SourcePtr[2] == L':' && SourcePtr[3] == L'=')
            {
                /*
                 * Small optimization: convert the path to short form only if
                 * the current directory is not the root directory (i.e. not
                 * of the '=X:=Y:\' form), otherwise just do a simple copy.
                 */
                if ( wcslen(SourcePtr) >= ARRAYSIZE("=X:=Y:\\")-1 &&
                     !( IS_ALPHA(SourcePtr[4]) && SourcePtr[5] == L':' &&
                        IS_PATH_SEPARATOR(SourcePtr[6]) && SourcePtr[7] == UNICODE_NULL ) )
                {
                    NameType = EnvNameSinglePath;

                    /* Copy the '=X:=' prefix */
                    *DestPtr++ = SourcePtr[0];
                    *DestPtr++ = SourcePtr[1];
                    *DestPtr++ = SourcePtr[2];
                    *DestPtr++ = SourcePtr[3];
                    SourcePtr += 4;
                }
            }
            else
            {
                /*
                 * Invalid stuff starting with '=', i.e.:
                 * =? (with '?' not being a letter)
                 * =X??? (with '?' not being ":=" and not followed by something longer than 3 characters)
                 * =X:=??? (with '?' not being "X:\\")
                 *
                 * 'NameType' is already set to 'EnvNameNotAPath'.
                 */
            }
        }


        /*
         * 2. Copy the environment value and perform conversions accordingly.
         */

        if (NameType == EnvNameNotAPath)
        {
            /* Copy everything, including the NULL terminator */
            do
            {
                *DestPtr++ = *SourcePtr;
            } while (*SourcePtr++ != UNICODE_NULL);
        }
        else if (NameType == EnvNameSinglePath)
        {
            /* Convert the path to its short form */
            NameLength = wcslen(SourcePtr);
            NumChars = GetShortPathNameW(SourcePtr, DestPtr, NameLength + 1 + Remaining);
            if (NumChars == 0 || NumChars > NameLength + Remaining)
            {
                /* If the conversion failed, just copy the original value */
                RtlCopyMemory(DestPtr, SourcePtr, NameLength * sizeof(WCHAR));
                NumChars = NameLength;
            }
            DestPtr += NumChars;
            if (NumChars > NameLength)
                Remaining -= (NumChars - NameLength);

            SourcePtr += NameLength;

            /* Copy the NULL terminator */
            *DestPtr++ = *SourcePtr++;
        }
        else // if (NameType == EnvNameMultiplePath)
        {
            WCHAR Delimiter;

            /* Loop through the list of paths (delimited by ';') and convert each path to its short form */
            do
            {
                /* Copy any trailing ';' before going to the next path */
                while (*SourcePtr == L';')
                {
                    *DestPtr++ = *SourcePtr++;
                }

                StartPtr = SourcePtr;

                /* Find the next path list delimiter or the NULL terminator */
                while (*SourcePtr != UNICODE_NULL && *SourcePtr != L';')
                {
                    ++SourcePtr;
                }
                Delimiter = *SourcePtr;

                NameLength = SourcePtr - StartPtr;
                if (NameLength)
                {
                    /*
                     * Temporarily replace the possible path list delimiter by NULL.
                     * 'lpEnvironment' must point to a read+write memory buffer!
                     */
                    *SourcePtr = UNICODE_NULL;

                    NumChars = GetShortPathNameW(StartPtr, DestPtr, NameLength + 1 + Remaining);
                    if ( NumChars == 0 ||
                        (Delimiter == L';' ? NumChars > NameLength + Remaining
                                           : NumChars > NameLength /* + Remaining ?? */) )
                    {
                        /* If the conversion failed, just copy the original value */
                        RtlCopyMemory(DestPtr, StartPtr, NameLength * sizeof(WCHAR));
                        NumChars = NameLength;
                    }
                    DestPtr += NumChars;
                    if (NumChars > NameLength)
                        Remaining -= (NumChars - NameLength);

                    /* If removed, restore the path list delimiter in the source environment value and copy it */
                    if (Delimiter != UNICODE_NULL)
                    {
                        *SourcePtr = Delimiter;
                        *DestPtr++ = *SourcePtr++;
                    }
                }
            } while (*SourcePtr != UNICODE_NULL);

            /* Copy the NULL terminator */
            *DestPtr++ = *SourcePtr++;
        }
    }

    /* NULL-terminate the environment block */
    *DestPtr++ = UNICODE_NULL;

    /* Initialize the Unicode string to hold it */
    RtlInitEmptyUnicodeString(UnicodeEnv, NewEnvironment,
                              (DestPtr - NewEnvironment) * sizeof(WCHAR));
    UnicodeEnv->Length = UnicodeEnv->MaximumLength;

    /* Create its ANSI version */
    Status = RtlUnicodeStringToAnsiString(AnsiEnv, UnicodeEnv, TRUE);
    if (!NT_SUCCESS(Status))
    {
        /* Set last error if conversion failure */
        BaseSetLastNTError(Status);
    }
    else
    {
        /* Everything went okay, so return success */
        Success = TRUE;
        NewEnvironment = NULL;
    }

Cleanup:
    /* Cleanup path starts here, start by destroying the environment copy */
    if (!lpEnvironment && Environment) RtlDestroyEnvironment(Environment);

    /* See if we are here due to failure */
    if (NewEnvironment)
    {
        /* Initialize the paths to be empty */
        RtlInitEmptyUnicodeString(UnicodeEnv, NULL, 0);
        RtlInitEmptyAnsiString(AnsiEnv, NULL, 0);

        /* Free the environment copy */
        RegionSize = 0;
        Status = NtFreeVirtualMemory(NtCurrentProcess(),
                                     (PVOID*)&NewEnvironment,
                                     &RegionSize,
                                     MEM_RELEASE);
        ASSERT(NT_SUCCESS(Status));
    }

    /* Return the result */
    return Success;
}

BOOL
NTAPI
BaseDestroyVDMEnvironment(IN PANSI_STRING AnsiEnv,
                          IN PUNICODE_STRING UnicodeEnv)
{
    SIZE_T Dummy = 0;

    /* Clear the ANSI buffer since Rtl creates this for us */
    if (AnsiEnv->Buffer) RtlFreeAnsiString(AnsiEnv);

    /* The Unicode buffer is build by hand, though */
    if (UnicodeEnv->Buffer)
    {
        /* So clear it through the API */
        NtFreeVirtualMemory(NtCurrentProcess(),
                            (PVOID*)&UnicodeEnv->Buffer,
                            &Dummy,
                            MEM_RELEASE);
    }

    /* All done */
    return TRUE;
}


/* Check whether a file is an OS/2 or a very old Windows executable
 * by testing on import of KERNEL.
 *
 * FIXME: is reading the module imports the only way of discerning
 *        old Windows binaries from OS/2 ones ? At least it seems so...
 */
static DWORD WINAPI
InternalIsOS2OrOldWin(HANDLE hFile, IMAGE_DOS_HEADER *mz, IMAGE_OS2_HEADER *ne)
{
  DWORD CurPos;
  LPWORD modtab = NULL;
  LPSTR nametab = NULL;
  DWORD Read, Ret;
  int i;

  Ret = BINARY_OS216;
  CurPos = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);

  /* read modref table */
  if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_modtab, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) ||
     (!(modtab = HeapAlloc(GetProcessHeap(), 0, ne->ne_cmod * sizeof(WORD)))) ||
     (!(ReadFile(hFile, modtab, ne->ne_cmod * sizeof(WORD), &Read, NULL))) ||
     (Read != (DWORD)ne->ne_cmod * sizeof(WORD)))
  {
    goto broken;
  }

  /* read imported names table */
  if((SetFilePointer(hFile, mz->e_lfanew + ne->ne_imptab, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) ||
     (!(nametab = HeapAlloc(GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab))) ||
     (!(ReadFile(hFile, nametab, ne->ne_enttab - ne->ne_imptab, &Read, NULL))) ||
     (Read != (DWORD)ne->ne_enttab - ne->ne_imptab))
  {
    goto broken;
  }

  for(i = 0; i < ne->ne_cmod; i++)
  {
    LPSTR module;
    module = &nametab[modtab[i]];
    if(!strncmp(&module[1], "KERNEL", module[0]))
    {
      /* very old windows file */
      Ret = BINARY_WIN16;
      goto done;
    }
  }

  broken:
  DPRINT1("InternalIsOS2OrOldWin(): Binary file seems to be broken\n");

  done:
  HeapFree(GetProcessHeap(), 0, modtab);
  HeapFree(GetProcessHeap(), 0, nametab);
  SetFilePointer(hFile, CurPos, NULL, FILE_BEGIN);
  return Ret;
}

static DWORD WINAPI
InternalGetBinaryType(HANDLE hFile)
{
  union
  {
    struct
    {
      unsigned char magic[4];
      unsigned char ignored[12];
      unsigned short type;
    } elf;
    struct
    {
      unsigned long magic;
      unsigned long cputype;
      unsigned long cpusubtype;
      unsigned long filetype;
    } macho;
    IMAGE_DOS_HEADER mz;
  } Header;
  char magic[4];
  DWORD Read;

  if((SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) ||
     (!ReadFile(hFile, &Header, sizeof(Header), &Read, NULL) ||
      (Read != sizeof(Header))))
  {
    return BINARY_UNKNOWN;
  }

  if(!memcmp(Header.elf.magic, "\177ELF", sizeof(Header.elf.magic)))
  {
    /* FIXME: we don't bother to check byte order, architecture, etc. */
    switch(Header.elf.type)
    {
      case 2:
        return BINARY_UNIX_EXE;
      case 3:
        return BINARY_UNIX_LIB;
    }
    return BINARY_UNKNOWN;
  }

  /* Mach-o File with Endian set to Big Endian or Little Endian*/
  if(Header.macho.magic == 0xFEEDFACE ||
     Header.macho.magic == 0xCEFAEDFE)
  {
    switch(Header.macho.filetype)
    {
      case 0x8:
        /* MH_BUNDLE */
        return BINARY_UNIX_LIB;
    }
    return BINARY_UNKNOWN;
  }

  /* Not ELF, try DOS */
  if(Header.mz.e_magic == IMAGE_DOS_SIGNATURE)
  {
    /* We do have a DOS image so we will now try to seek into
     * the file by the amount indicated by the field
     * "Offset to extended header" and read in the
     * "magic" field information at that location.
     * This will tell us if there is more header information
     * to read or not.
     */
    if((SetFilePointer(hFile, Header.mz.e_lfanew, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) ||
       (!ReadFile(hFile, magic, sizeof(magic), &Read, NULL) ||
        (Read != sizeof(magic))))
    {
      return BINARY_DOS;
    }

    /* Reading the magic field succeeded so
     * we will try to determine what type it is.
     */
    if(!memcmp(magic, "PE\0\0", sizeof(magic)))
    {
      IMAGE_FILE_HEADER FileHeader;
      if(!ReadFile(hFile, &FileHeader, sizeof(IMAGE_FILE_HEADER), &Read, NULL) ||
         (Read != sizeof(IMAGE_FILE_HEADER)))
      {
        return BINARY_DOS;
      }

      /* FIXME - detect 32/64 bit */

      if(FileHeader.Characteristics & IMAGE_FILE_DLL)
        return BINARY_PE_DLL32;
      return BINARY_PE_EXE32;
    }

    if(!memcmp(magic, "NE", 2))
    {
      /* This is a Windows executable (NE) header.  This can
       * mean either a 16-bit OS/2 or a 16-bit Windows or even a
       * DOS program (running under a DOS extender).  To decide
       * which, we'll have to read the NE header.
       */
      IMAGE_OS2_HEADER ne;
      if((SetFilePointer(hFile, Header.mz.e_lfanew, NULL, FILE_BEGIN) == 1) ||
         !ReadFile(hFile, &ne, sizeof(IMAGE_OS2_HEADER), &Read, NULL) ||
         (Read != sizeof(IMAGE_OS2_HEADER)))
      {
        /* Couldn't read header, so abort. */
        return BINARY_DOS;
      }

      switch(ne.ne_exetyp)
      {
        case 2:
          return BINARY_WIN16;
        case 5:
          return BINARY_DOS;
        default:
          return InternalIsOS2OrOldWin(hFile, &Header.mz, &ne);
      }
    }
    return BINARY_DOS;
  }
  return BINARY_UNKNOWN;
}

/*
 * @implemented
 */
BOOL
WINAPI
GetBinaryTypeW (
    LPCWSTR lpApplicationName,
    LPDWORD lpBinaryType
    )
{
  HANDLE hFile;
  DWORD BinType;

  if(!lpApplicationName || !lpBinaryType)
  {
    SetLastError(ERROR_INVALID_PARAMETER);
    return FALSE;
  }

  hFile = CreateFileW(lpApplicationName, GENERIC_READ, FILE_SHARE_READ, NULL,
                      OPEN_EXISTING, 0, 0);
  if(hFile == INVALID_HANDLE_VALUE)
  {
    return FALSE;
  }

  BinType = InternalGetBinaryType(hFile);
  CloseHandle(hFile);

  switch(BinType)
  {
    case BINARY_UNKNOWN:
    {
      WCHAR *dot;

      /*
       * guess from filename
       */
      if(!(dot = wcsrchr(lpApplicationName, L'.')))
      {
        return FALSE;
      }
      if(!lstrcmpiW(dot, L".COM"))
      {
        *lpBinaryType = SCS_DOS_BINARY;
        return TRUE;
      }
      if(!lstrcmpiW(dot, L".PIF"))
      {
        *lpBinaryType = SCS_PIF_BINARY;
        return TRUE;
      }
      return FALSE;
    }
    case BINARY_PE_EXE32:
    case BINARY_PE_DLL32:
    {
      *lpBinaryType = SCS_32BIT_BINARY;
      return TRUE;
    }
    case BINARY_PE_EXE64:
    case BINARY_PE_DLL64:
    {
      *lpBinaryType = SCS_64BIT_BINARY;
      return TRUE;
    }
    case BINARY_WIN16:
    {
      *lpBinaryType = SCS_WOW_BINARY;
      return TRUE;
    }
    case BINARY_OS216:
    {
      *lpBinaryType = SCS_OS216_BINARY;
      return TRUE;
    }
    case BINARY_DOS:
    {
      *lpBinaryType = SCS_DOS_BINARY;
      return TRUE;
    }
    case BINARY_UNIX_EXE:
    case BINARY_UNIX_LIB:
    {
      return FALSE;
    }
  }

  DPRINT1("Invalid binary type %lu returned!\n", BinType);
  return FALSE;
}

/*
 * @implemented
 */
BOOL
WINAPI
GetBinaryTypeA(IN LPCSTR lpApplicationName,
               OUT LPDWORD lpBinaryType)
{
    ANSI_STRING ApplicationNameString;
    UNICODE_STRING ApplicationNameW;
    BOOL StringAllocated = FALSE, Result;
    NTSTATUS Status;

    RtlInitAnsiString(&ApplicationNameString, lpApplicationName);

    if (ApplicationNameString.Length * sizeof(WCHAR) >= NtCurrentTeb()->StaticUnicodeString.MaximumLength)
    {
        StringAllocated = TRUE;
        Status = RtlAnsiStringToUnicodeString(&ApplicationNameW, &ApplicationNameString, TRUE);
    }
    else
    {
        Status = RtlAnsiStringToUnicodeString(&(NtCurrentTeb()->StaticUnicodeString), &ApplicationNameString, FALSE);
    }

    if (!NT_SUCCESS(Status))
    {
        BaseSetLastNTError(Status);
        return FALSE;
    }

    if (StringAllocated)
    {
        Result = GetBinaryTypeW(ApplicationNameW.Buffer, lpBinaryType);
        RtlFreeUnicodeString(&ApplicationNameW);
    }
    else
    {
        Result = GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
    }

    return Result;
}

/*
 * @unimplemented
 */
BOOL
WINAPI
CmdBatNotification (
    DWORD   Unknown
    )
{
    STUB;
    return FALSE;
}

/*
 * @implemented
 */
VOID
WINAPI
ExitVDM(BOOL IsWow, ULONG iWowTask)
{
    BASE_API_MESSAGE ApiMessage;
    PBASE_EXIT_VDM ExitVdm = &ApiMessage.Data.ExitVDMRequest;

    /* Setup the input parameters */
    ExitVdm->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
    ExitVdm->iWowTask = IsWow ? iWowTask : 0; /* Always zero for DOS tasks */
    ExitVdm->WaitObjectForVDM = NULL;

    /* Call CSRSS */
    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                        NULL,
                        CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepExitVDM),
                        sizeof(*ExitVdm));

    /* Close the returned wait object handle, if any */
    if (NT_SUCCESS(ApiMessage.Status) && (ExitVdm->WaitObjectForVDM != NULL))
    {
        CloseHandle(ExitVdm->WaitObjectForVDM);
    }
}

/*
 * @implemented
 */
BOOL
WINAPI
GetNextVDMCommand(PVDM_COMMAND_INFO CommandData)
{
    BOOL Success = FALSE;
    NTSTATUS Status;
    BASE_API_MESSAGE ApiMessage;
    PBASE_GET_NEXT_VDM_COMMAND GetNextVdmCommand = &ApiMessage.Data.GetNextVDMCommandRequest;
    PBASE_IS_FIRST_VDM IsFirstVdm = &ApiMessage.Data.IsFirstVDMRequest;
    PBASE_SET_REENTER_COUNT SetReenterCount = &ApiMessage.Data.SetReenterCountRequest;
    PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
    ULONG NumStrings = 0;

    /*
     * Special case to test whether the VDM is the first one.
     */
    if (CommandData == NULL)
    {
        /* Call CSRSS */
        CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                            NULL,
                            CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepIsFirstVDM),
                            sizeof(*IsFirstVdm));
        if (!NT_SUCCESS(ApiMessage.Status))
        {
            BaseSetLastNTError(ApiMessage.Status);
            return FALSE;
        }

        /* Return TRUE if this is the first VDM */
        return IsFirstVdm->FirstVDM;
    }

    /* CommandData != NULL */

    /*
     * Special case to increment or decrement the reentrancy count.
     */
    if ((CommandData->VDMState == VDM_INC_REENTER_COUNT) ||
        (CommandData->VDMState == VDM_DEC_REENTER_COUNT))
    {
        /* Setup the input parameters */
        SetReenterCount->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
        SetReenterCount->fIncDec = CommandData->VDMState;

        /* Call CSRSS */
        CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                            NULL,
                            CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepSetReenterCount),
                            sizeof(*SetReenterCount));
        if (!NT_SUCCESS(ApiMessage.Status))
        {
            BaseSetLastNTError(ApiMessage.Status);
            return FALSE;
        }

        return TRUE;
    }

    /*
     * TODO!
     * Special case to retrieve or set WOW information.
     */
    // TODO: if CommandData->VDMState & (VDM_LIST_WOW_PROCESSES | VDM_LIST_WOW_TASKS | VDM_ADD_WOW_TASK)
    // then call BasepGetNextVDMCommand in a simpler way!

    /*
     * Regular case.
     */

    /* Clear the structure */
    RtlZeroMemory(GetNextVdmCommand, sizeof(*GetNextVdmCommand));

    /* Setup the input parameters */
    GetNextVdmCommand->iTask = CommandData->TaskId;
    GetNextVdmCommand->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
    GetNextVdmCommand->CmdLen = CommandData->CmdLen;
    GetNextVdmCommand->AppLen = CommandData->AppLen;
    GetNextVdmCommand->PifLen = CommandData->PifLen;
    GetNextVdmCommand->CurDirectoryLen = CommandData->CurDirectoryLen;
    GetNextVdmCommand->EnvLen = CommandData->EnvLen;
    GetNextVdmCommand->DesktopLen = CommandData->DesktopLen;
    GetNextVdmCommand->TitleLen = CommandData->TitleLen;
    GetNextVdmCommand->ReservedLen = CommandData->ReservedLen;
    GetNextVdmCommand->VDMState = CommandData->VDMState;

    /* Count the number of strings */
    if (CommandData->CmdLen) NumStrings++;
    if (CommandData->AppLen) NumStrings++;
    if (CommandData->PifLen) NumStrings++;
    if (CommandData->CurDirectoryLen) NumStrings++;
    if (CommandData->EnvLen) NumStrings++;
    if (CommandData->DesktopLen) NumStrings++;
    if (CommandData->TitleLen) NumStrings++;
    if (CommandData->ReservedLen) NumStrings++;

    /* Allocate the capture buffer */
    CaptureBuffer = CsrAllocateCaptureBuffer(NumStrings + 1,
                                             GetNextVdmCommand->CmdLen
                                             + GetNextVdmCommand->AppLen
                                             + GetNextVdmCommand->PifLen
                                             + GetNextVdmCommand->CurDirectoryLen
                                             + GetNextVdmCommand->EnvLen
                                             + GetNextVdmCommand->DesktopLen
                                             + GetNextVdmCommand->TitleLen
                                             + GetNextVdmCommand->ReservedLen
                                             + sizeof(*GetNextVdmCommand->StartupInfo));
    if (CaptureBuffer == NULL)
    {
        BaseSetLastNTError(STATUS_NO_MEMORY);
        goto Cleanup;
    }

    /* Capture the data */

    CsrAllocateMessagePointer(CaptureBuffer,
                              sizeof(*GetNextVdmCommand->StartupInfo),
                              (PVOID*)&GetNextVdmCommand->StartupInfo);

    if (CommandData->CmdLen)
    {
        CsrAllocateMessagePointer(CaptureBuffer,
                                  CommandData->CmdLen,
                                  (PVOID*)&GetNextVdmCommand->CmdLine);
    }

    if (CommandData->AppLen)
    {
        CsrAllocateMessagePointer(CaptureBuffer,
                                  CommandData->AppLen,
                                  (PVOID*)&GetNextVdmCommand->AppName);
    }

    if (CommandData->PifLen)
    {
        CsrAllocateMessagePointer(CaptureBuffer,
                                  CommandData->PifLen,
                                  (PVOID*)&GetNextVdmCommand->PifFile);
    }

    if (CommandData->CurDirectoryLen)
    {
        CsrAllocateMessagePointer(CaptureBuffer,
                                  CommandData->CurDirectoryLen,
                                  (PVOID*)&GetNextVdmCommand->CurDirectory);
    }

    if (CommandData->EnvLen)
    {
        CsrAllocateMessagePointer(CaptureBuffer,
                                  CommandData->EnvLen,
                                  (PVOID*)&GetNextVdmCommand->Env);
    }

    if (CommandData->DesktopLen)
    {
        CsrAllocateMessagePointer(CaptureBuffer,
                                  CommandData->DesktopLen,
                                  (PVOID*)&GetNextVdmCommand->Desktop);
    }

    if (CommandData->TitleLen)
    {
        CsrAllocateMessagePointer(CaptureBuffer,
                                  CommandData->TitleLen,
                                  (PVOID*)&GetNextVdmCommand->Title);
    }

    if (CommandData->ReservedLen)
    {
        CsrAllocateMessagePointer(CaptureBuffer,
                                  CommandData->ReservedLen,
                                  (PVOID*)&GetNextVdmCommand->Reserved);
    }

    while (TRUE)
    {
        /* Call CSRSS */
        Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,<--- Status is assigned
                                     CaptureBuffer,
                                     CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepGetNextVDMCommand),
                                     sizeof(*GetNextVdmCommand));

        /* Exit the waiting loop if we did not receive any event handle */
        if (GetNextVdmCommand->WaitObjectForVDM == NULL)
            break;

        /* Wait for the event to become signaled and try again */
        Status = NtWaitForSingleObject(GetNextVdmCommand->WaitObjectForVDM,<--- Status is overwritten
                                       FALSE, NULL);
        if (Status != STATUS_SUCCESS)
        {
            /* Fail if we timed out, or if some other error happened */
            BaseSetLastNTError(Status);
            goto Cleanup;
        }

        /* Set the retry flag, clear the exit code, and retry a query */
        GetNextVdmCommand->VDMState |= VDM_FLAG_RETRY;
        GetNextVdmCommand->ExitCode = 0;
    }

    if (!NT_SUCCESS(Status))
    {
        if (Status == STATUS_INVALID_PARAMETER)
        {
            /*
             * One of the buffer lengths was less than required. Store the correct ones.
             * Note that the status code is not STATUS_BUFFER_TOO_SMALL as one would expect,
             * in order to keep compatibility with Windows 2003 BASESRV.DLL.
             */
            CommandData->CmdLen = GetNextVdmCommand->CmdLen;
            CommandData->AppLen = GetNextVdmCommand->AppLen;
            CommandData->PifLen = GetNextVdmCommand->PifLen;
            CommandData->CurDirectoryLen = GetNextVdmCommand->CurDirectoryLen;
            CommandData->EnvLen      = GetNextVdmCommand->EnvLen;
            CommandData->DesktopLen  = GetNextVdmCommand->DesktopLen;
            CommandData->TitleLen    = GetNextVdmCommand->TitleLen;
            CommandData->ReservedLen = GetNextVdmCommand->ReservedLen;
        }
        else
        {
            /* Any other failure */
            CommandData->CmdLen = 0;
            CommandData->AppLen = 0;
            CommandData->PifLen = 0;
            CommandData->CurDirectoryLen = 0;
            CommandData->EnvLen      = 0;
            CommandData->DesktopLen  = 0;
            CommandData->TitleLen    = 0;
            CommandData->ReservedLen = 0;
        }

        BaseSetLastNTError(Status);
        goto Cleanup;
    }

    /* Write back the standard handles */
    CommandData->StdIn  = GetNextVdmCommand->StdIn;
    CommandData->StdOut = GetNextVdmCommand->StdOut;
    CommandData->StdErr = GetNextVdmCommand->StdErr;

    /* Write back the startup info */
    RtlMoveMemory(&CommandData->StartupInfo,
                  GetNextVdmCommand->StartupInfo,
                  sizeof(*GetNextVdmCommand->StartupInfo));

    if (CommandData->CmdLen)
    {
        /* Write back the command line */
        RtlMoveMemory(CommandData->CmdLine,
                      GetNextVdmCommand->CmdLine,
                      GetNextVdmCommand->CmdLen);

        /* Set the actual length */
        CommandData->CmdLen = GetNextVdmCommand->CmdLen;
    }

    if (CommandData->AppLen)
    {
        /* Write back the application name */
        RtlMoveMemory(CommandData->AppName,
                      GetNextVdmCommand->AppName,
                      GetNextVdmCommand->AppLen);

        /* Set the actual length */
        CommandData->AppLen = GetNextVdmCommand->AppLen;
    }

    if (CommandData->PifLen)
    {
        /* Write back the PIF file name */
        RtlMoveMemory(CommandData->PifFile,
                      GetNextVdmCommand->PifFile,
                      GetNextVdmCommand->PifLen);

        /* Set the actual length */
        CommandData->PifLen = GetNextVdmCommand->PifLen;
    }

    if (CommandData->CurDirectoryLen)
    {
        /* Write back the current directory */
        RtlMoveMemory(CommandData->CurDirectory,
                      GetNextVdmCommand->CurDirectory,
                      GetNextVdmCommand->CurDirectoryLen);

        /* Set the actual length */
        CommandData->CurDirectoryLen = GetNextVdmCommand->CurDirectoryLen;
    }

    if (CommandData->EnvLen)
    {
        /* Write back the environment */
        RtlMoveMemory(CommandData->Env,
                      GetNextVdmCommand->Env,
                      GetNextVdmCommand->EnvLen);

        /* Set the actual length */
        CommandData->EnvLen = GetNextVdmCommand->EnvLen;
    }

    if (CommandData->DesktopLen)
    {
        /* Write back the desktop name */
        RtlMoveMemory(CommandData->Desktop,
                      GetNextVdmCommand->Desktop,
                      GetNextVdmCommand->DesktopLen);

        /* Set the actual length */
        CommandData->DesktopLen = GetNextVdmCommand->DesktopLen;
    }

    if (CommandData->TitleLen)
    {
        /* Write back the title */
        RtlMoveMemory(CommandData->Title,
                      GetNextVdmCommand->Title,
                      GetNextVdmCommand->TitleLen);

        /* Set the actual length */
        CommandData->TitleLen = GetNextVdmCommand->TitleLen;
    }

    if (CommandData->ReservedLen)
    {
        /* Write back the reserved parameter */
        RtlMoveMemory(CommandData->Reserved,
                      GetNextVdmCommand->Reserved,
                      GetNextVdmCommand->ReservedLen);

        /* Set the actual length */
        CommandData->ReservedLen = GetNextVdmCommand->ReservedLen;
    }

    /* Write the remaining output parameters */
    CommandData->TaskId        = GetNextVdmCommand->iTask;
    CommandData->CreationFlags = GetNextVdmCommand->dwCreationFlags;
    CommandData->CodePage      = GetNextVdmCommand->CodePage;
    CommandData->ExitCode      = GetNextVdmCommand->ExitCode;
    CommandData->CurrentDrive  = GetNextVdmCommand->CurrentDrive;
    CommandData->VDMState      = GetNextVdmCommand->VDMState;
    CommandData->ComingFromBat = GetNextVdmCommand->fComingFromBat;

    /* It was successful */
    Success = TRUE;

Cleanup:
    if (CaptureBuffer != NULL) CsrFreeCaptureBuffer(CaptureBuffer); 
    return Success;
}


/*
 * @implemented
 */
DWORD
WINAPI
GetVDMCurrentDirectories(DWORD cchCurDirs, PCHAR lpszzCurDirs)
{
    BASE_API_MESSAGE ApiMessage;
    PBASE_GETSET_VDM_CURDIRS VDMCurrentDirsRequest = &ApiMessage.Data.VDMCurrentDirsRequest;
    PCSR_CAPTURE_BUFFER CaptureBuffer;

    /* Allocate the capture buffer */
    CaptureBuffer = CsrAllocateCaptureBuffer(1, cchCurDirs);
    if (CaptureBuffer == NULL)
    {
        BaseSetLastNTError(STATUS_NO_MEMORY);
        return 0;
    }

    /* Setup the input parameters */
    VDMCurrentDirsRequest->cchCurDirs = cchCurDirs;
    CsrAllocateMessagePointer(CaptureBuffer,
                              cchCurDirs,
                              (PVOID*)&VDMCurrentDirsRequest->lpszzCurDirs);

    /* Call CSRSS */
    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                        CaptureBuffer,
                        CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepGetVDMCurDirs),
                        sizeof(*VDMCurrentDirsRequest));

    /* Set the last error */
    BaseSetLastNTError(ApiMessage.Status);

    if (NT_SUCCESS(ApiMessage.Status))
    {
        /* Copy the result */
        RtlMoveMemory(lpszzCurDirs, VDMCurrentDirsRequest->lpszzCurDirs, cchCurDirs);
    }

    /* Free the capture buffer */
    CsrFreeCaptureBuffer(CaptureBuffer);

    /* Return the size if it was successful, or if the buffer was too small */
    return (NT_SUCCESS(ApiMessage.Status) || (ApiMessage.Status == STATUS_BUFFER_TOO_SMALL))
           ? VDMCurrentDirsRequest->cchCurDirs : 0;
}


/*
 * @implemented (undocumented)
 */
BOOL
WINAPI
RegisterConsoleVDM(IN DWORD dwRegisterFlags,
                   IN HANDLE hStartHardwareEvent,
                   IN HANDLE hEndHardwareEvent,
                   IN HANDLE hErrorHardwareEvent,
                   IN DWORD dwUnusedVar,
                   OUT LPDWORD lpVideoStateLength,
                   OUT PVOID* lpVideoState, // PVIDEO_HARDWARE_STATE_HEADER*
                   IN PVOID lpUnusedBuffer,
                   IN DWORD dwUnusedBufferLength,
                   IN COORD dwVDMBufferSize,
                   OUT PVOID* lpVDMBuffer)
{
    BOOL Success;
    CONSOLE_API_MESSAGE ApiMessage;
    PCONSOLE_REGISTERVDM RegisterVDMRequest = &ApiMessage.Data.RegisterVDMRequest;
    PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;

    /* Set up the data to send to the Console Server */
    RegisterVDMRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
    RegisterVDMRequest->RegisterFlags = dwRegisterFlags;

    if (dwRegisterFlags != 0)
    {
        RegisterVDMRequest->StartHardwareEvent = hStartHardwareEvent;
        RegisterVDMRequest->EndHardwareEvent   = hEndHardwareEvent;
        RegisterVDMRequest->ErrorHardwareEvent = hErrorHardwareEvent;

        RegisterVDMRequest->VDMBufferSize = dwVDMBufferSize;

#if 0
        RegisterVDMRequest->UnusedBufferLength = dwUnusedBufferLength;

        /* Allocate a Capture Buffer */
        CaptureBuffer = CsrAllocateCaptureBuffer(1, dwUnusedBufferLength);
        if (CaptureBuffer == NULL)
        {
            DPRINT1("CsrAllocateCaptureBuffer failed!\n");
            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
            return FALSE;
        }

        /* Capture the buffer to write */
        CsrCaptureMessageBuffer(CaptureBuffer,
                                (PVOID)lpUnusedBuffer,
                                dwUnusedBufferLength,
                                (PVOID*)&RegisterVDMRequest->UnusedBuffer);
#endif
    }
    else
    {
        // CaptureBuffer = NULL;
    }

    /* Call the server */
    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                        CaptureBuffer,
                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepRegisterVDM),
                        sizeof(*RegisterVDMRequest));

    /* Check for success */
    Success = NT_SUCCESS(ApiMessage.Status);

    /* Release the capture buffer if needed */
    if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);

    /* Retrieve the results */
    if (Success)
    {
        if (dwRegisterFlags != 0)
        {
            _SEH2_TRY
            {
                *lpVideoStateLength = RegisterVDMRequest->VideoStateLength;
                *lpVideoState       = RegisterVDMRequest->VideoState;
                *lpVDMBuffer        = RegisterVDMRequest->VDMBuffer;
            }
            _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
            {
                SetLastError(ERROR_INVALID_ACCESS);
                Success = FALSE;
            }
            _SEH2_END;
        }
    }
    else
    {
        BaseSetLastNTError(ApiMessage.Status);
    }

    /* Return success status */
    return Success;
}


/*
 * @unimplemented
 */
BOOL
WINAPI
RegisterWowBaseHandlers (
    DWORD   Unknown0
    )
{
    STUB;
    return FALSE;
}


/*
 * @unimplemented
 */
BOOL
WINAPI
RegisterWowExec (
    DWORD   Unknown0
    )
{
    STUB;
    return FALSE;
}


/*
 * @implemented
 */
BOOL
WINAPI
SetVDMCurrentDirectories(DWORD cchCurDirs, PCHAR lpszzCurDirs)
{
    BASE_API_MESSAGE ApiMessage;
    PBASE_GETSET_VDM_CURDIRS VDMCurrentDirsRequest = &ApiMessage.Data.VDMCurrentDirsRequest;
    PCSR_CAPTURE_BUFFER CaptureBuffer;

    /* Allocate the capture buffer */
    CaptureBuffer = CsrAllocateCaptureBuffer(1, cchCurDirs);
    if (CaptureBuffer == NULL)
    {
        BaseSetLastNTError(STATUS_NO_MEMORY);
        return FALSE;
    }

    /* Setup the input parameters */
    VDMCurrentDirsRequest->cchCurDirs = cchCurDirs;
    CsrCaptureMessageBuffer(CaptureBuffer,
                            lpszzCurDirs,
                            cchCurDirs,
                            (PVOID*)&VDMCurrentDirsRequest->lpszzCurDirs);

    /* Call CSRSS */
    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                        CaptureBuffer,
                        CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepSetVDMCurDirs),
                        sizeof(*VDMCurrentDirsRequest));

    /* Free the capture buffer */
    CsrFreeCaptureBuffer(CaptureBuffer);

    /* Set the last error */
    BaseSetLastNTError(ApiMessage.Status);

    return NT_SUCCESS(ApiMessage.Status) ? TRUE : FALSE;
}

/*
 * @unimplemented
 */
DWORD
WINAPI
VDMConsoleOperation (
    DWORD   Unknown0,
    DWORD   Unknown1
    )
{
    STUB;
    return 0;
}


/*
 * @unimplemented
 */
BOOL
WINAPI
VDMOperationStarted(IN ULONG Unknown0)
{
    DPRINT1("VDMOperationStarted(%d)\n", Unknown0);

    return BaseUpdateVDMEntry(VdmEntryUpdateControlCHandler,
                              NULL,
                              0,
                              Unknown0);
}