[NTVDM]
[reactos.git] / subsystems / ntvdm / ntvdm.c
index e5ace37..6c7099d 100644 (file)
@@ -34,6 +34,7 @@ static HANDLE ConsoleOutput = INVALID_HANDLE_VALUE;
 static DWORD  OrgConsoleInputMode, OrgConsoleOutputMode;
 static CONSOLE_CURSOR_INFO         OrgConsoleCursorInfo;
 static CONSOLE_SCREEN_BUFFER_INFO  OrgConsoleBufferInfo;
+static HANDLE CommandThread = NULL;
 
 static HMENU hConsoleMenu  = NULL;
 static INT   VdmMenuPos    = -1;
@@ -196,6 +197,11 @@ BOOL WINAPI ConsoleCtrlHandler(DWORD ControlType)
             EmulatorInterrupt(0x23);
             break;
         }
+        case CTRL_LAST_CLOSE_EVENT:
+        {
+            if (CommandThread) TerminateThread(CommandThread, 0);
+            break;
+        }
         default:
         {
             /* Stop the VDM if the user logs out or closes the console */
@@ -279,6 +285,9 @@ BOOL ConsoleInit(VOID)
     /* Set the handler routine */
     SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
 
+    /* Enable the CTRL_LAST_CLOSE_EVENT */
+    SetLastConsoleEventActive();
+
     /* Get the input handle to the real console, and check for success */
     ConsoleInput = CreateFileW(L"CONIN$",
                                GENERIC_READ | GENERIC_WRITE,
@@ -368,28 +377,87 @@ VOID ConsoleCleanup(VOID)
     if (ConsoleInput  != INVALID_HANDLE_VALUE) CloseHandle(ConsoleInput);
 }
 
-INT wmain(INT argc, WCHAR *argv[])
+DWORD WINAPI CommandThreadProc(LPVOID Parameter)
 {
-#ifndef STANDALONE
-    wprintf(L"\nReactOS Virtual DOS Machine\n\n"
-            L"OS integration (BaseVDM) unimplemented\n");
+    VDM_COMMAND_INFO CommandInfo;
+    CHAR CmdLine[MAX_PATH];
+    CHAR AppName[MAX_PATH];
+    CHAR PifFile[MAX_PATH];
+    CHAR Desktop[MAX_PATH];
+    CHAR Title[MAX_PATH];
+    CHAR Env[MAX_PATH];
+
+    UNREFERENCED_PARAMETER(Parameter);
+
+    while (TRUE)
+    {
+        /* Clear the structure */
+        ZeroMemory(&CommandInfo, sizeof(CommandInfo));
+
+        /* Initialize the structure members */
+        CommandInfo.VDMState = VDM_NOT_LOADED;
+        CommandInfo.CmdLine = CmdLine;
+        CommandInfo.CmdLen = sizeof(CmdLine);
+        CommandInfo.AppName = AppName;
+        CommandInfo.AppLen = sizeof(AppName);
+        CommandInfo.PifFile = PifFile;
+        CommandInfo.PifLen = sizeof(PifFile);
+        CommandInfo.Desktop = Desktop;
+        CommandInfo.DesktopLen = sizeof(Desktop);
+        CommandInfo.Title = Title;
+        CommandInfo.TitleLen = sizeof(Title);
+        CommandInfo.Env = Env;
+        CommandInfo.EnvLen = sizeof(Env);
+
+        /* Wait for the next available VDM */
+        if (!GetNextVDMCommand(&CommandInfo)) break;
+
+        /* Start the process from the command line */
+        DPRINT1("Starting '%s'...\n", AppName);
+        if (DosLoadExecutable(DOS_LOAD_AND_EXECUTE,
+                              AppName,
+                              CmdLine,
+                              Env,
+                              NULL,
+                              NULL) != ERROR_SUCCESS)
+        {
+            DisplayMessage(L"Could not start '%S'", AppName);
+            break;
+        }
+
+        /* Start simulation */
+        EmulatorSimulate();
+
+        /* Perform another screen refresh */
+        VgaRefreshDisplay();
+    }
+
     return 0;
-#else
+}
 
+INT wmain(INT argc, WCHAR *argv[])
+{
+#ifdef STANDALONE
+    CHAR ApplicationName[MAX_PATH];
     CHAR CommandLine[DOS_CMDLINE_LENGTH];
 
-    if (argc == 2 && argv[1] != NULL)
+    if (argc >= 2)
     {
-        WideCharToMultiByte(CP_ACP, 0, argv[1], -1, CommandLine, sizeof(CommandLine), NULL, NULL);
+        WideCharToMultiByte(CP_ACP, 0, argv[1], -1, ApplicationName, sizeof(ApplicationName), NULL, NULL);
+
+        if (argc >= 3) WideCharToMultiByte(CP_ACP, 0, argv[2], -1, CommandLine, sizeof(CommandLine), NULL, NULL);
+        else strcpy(CommandLine, "");
     }
     else
     {
         wprintf(L"\nReactOS Virtual DOS Machine\n\n"
-                L"Usage: NTVDM <executable>\n");
+                L"Usage: NTVDM <executable> [<parameters>]\n");
         return 0;
     }
 
-    DPRINT1("\n\n\nNTVDM - Starting '%s'...\n\n\n", CommandLine);
+#endif
+
+    DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
 
     /* Initialize the console */
     if (!ConsoleInit())
@@ -419,10 +487,34 @@ INT wmain(INT argc, WCHAR *argv[])
         goto Cleanup;
     }
 
+#ifndef STANDALONE
+
+    /* Create the GetNextVDMCommand thread */
+    CommandThread = CreateThread(NULL, 0, &CommandThreadProc, NULL, 0, NULL);
+    if (CommandThread == NULL)
+    {
+        wprintf(L"FATAL: Failed to create the command processing thread: %d\n", GetLastError());
+        goto Cleanup;
+    }
+
+    /* Wait for the command thread to exit */
+    WaitForSingleObject(CommandThread, INFINITE);
+
+    /* Close the thread handle */
+    CloseHandle(CommandThread);
+
+#else
+
     /* Start the process from the command line */
-    if (!DosCreateProcess(CommandLine, 0))
+    DPRINT1("Starting '%s'...\n", CommandLine);
+    if (DosLoadExecutable(DOS_LOAD_AND_EXECUTE,
+                          ApplicationName,
+                          CommandLine,
+                          GetEnvironmentStrings(),
+                          NULL,
+                          NULL) != ERROR_SUCCESS)
     {
-        DisplayMessage(L"Could not start program: %S", CommandLine);
+        DisplayMessage(L"Could not start '%S'", ApplicationName);
         goto Cleanup;
     }
 
@@ -432,16 +524,21 @@ INT wmain(INT argc, WCHAR *argv[])
     /* Perform another screen refresh */
     VgaRefreshDisplay();
 
+#endif
+
 Cleanup:
     BiosCleanup();
     EmulatorCleanup();
     ConsoleCleanup();
 
+#ifndef STANDALONE
+    ExitVDM(FALSE, 0);
+#endif
+
+    /* Quit the VDM */
     DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");
 
     return 0;
-
-#endif
 }
 
 /* EOF */