[KERNEL32]
[reactos.git] / dll / win32 / kernel32 / client / proc.c
index 0c4f36b..fee3215 100644 (file)
@@ -12,7 +12,7 @@
 
 #include <k32.h>
 
-// #define NDEBUG
+#define NDEBUG
 #include <debug.h>
 
 /* GLOBALS *******************************************************************/
@@ -49,6 +49,7 @@ VOID WINAPI
 RegisterWaitForInputIdle(WaitForInputIdleType lpfnRegisterWaitForInputIdle);
 
 #define CMD_STRING L"cmd /c "
+#define NTVDM_STRING L"\\ntvdm.exe"
 
 /* FUNCTIONS ****************************************************************/
 
@@ -182,6 +183,22 @@ BasepConfigureAppCertDlls(IN PWSTR ValueName,
     return BasepSaveAppCertRegistryValue(Context, ValueName, ValueData);
 }
 
+
+BOOLEAN
+NTAPI
+BasepCheckDosApp(IN PUNICODE_STRING ApplicationName)
+{
+    PWCHAR Extension;
+    
+    /* Get the extension from the file name */
+    Extension = &ApplicationName->Buffer[ApplicationName->Length /
+                                         sizeof(WCHAR) - 4];
+
+    /* Check if the extension is .COM */
+    if (_wcsnicmp(Extension, L".com", 4) == 0) return TRUE;
+    else return FALSE;
+}
+
 NTSTATUS
 WINAPI
 BasepIsProcessAllowed(IN PCHAR ApplicationName)
@@ -509,10 +526,10 @@ BasepNotifyCsrOfThread(IN HANDLE ThreadHandle,
                                  NULL,
                                  CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepCreateThread),
                                  sizeof(BASE_CREATE_THREAD));
-    if (!NT_SUCCESS(Status) || !NT_SUCCESS(ApiMessage.Status))
+    if (!NT_SUCCESS(Status))
     {
-        DPRINT1("Failed to tell csrss about new thread: %lx %lx\n", Status, ApiMessage.Status);
-        return ApiMessage.Status;
+        DPRINT1("Failed to tell CSRSS about new thread: %lx\n", Status);
+        return Status;
     }
 
     /* Return Success */
@@ -528,7 +545,6 @@ BasepCreateFirstThread(HANDLE ProcessHandle,
                        LPSECURITY_ATTRIBUTES lpThreadAttributes,
                        PSECTION_IMAGE_INFORMATION SectionImageInfo,
                        PCLIENT_ID ClientId,
-                       BOOLEAN InheritHandles,
                        DWORD dwCreationFlags)
 {
     NTSTATUS Status;
@@ -579,17 +595,25 @@ BasepCreateFirstThread(HANDLE ProcessHandle,
     CreateProcessRequest->ProcessHandle = ProcessHandle;
     CreateProcessRequest->ThreadHandle = hThread;
     CreateProcessRequest->CreationFlags = dwCreationFlags;
-    CreateProcessRequest->bInheritHandles = InheritHandles;
+
+    /*
+     * For GUI applications we turn on the 2nd bit. This also allows
+     * us to know whether or not this is a GUI or a TUI application.
+     */
+    if (IMAGE_SUBSYSTEM_WINDOWS_GUI == SectionImageInfo->SubSystemType)
+    {
+        CreateProcessRequest->ProcessHandle = (HANDLE)
+            ((ULONG_PTR)CreateProcessRequest->ProcessHandle | 2);
+    }
 
     /* Call CSR */
-    DPRINT1("Calling CsrClientCallServer from BasepCreateFirstThread...\n");
     Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
                                  NULL,
                                  CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepCreateProcess),
                                  sizeof(BASE_CREATE_PROCESS));
-    if (!NT_SUCCESS(Status) || !NT_SUCCESS(ApiMessage.Status))
+    if (!NT_SUCCESS(Status))
     {
-        DPRINT1("Failed to tell csrss about new process: %lx %lx\n", Status, ApiMessage.Status);
+        DPRINT1("Failed to tell CSRSS about new process: %lx\n", Status);
         return NULL;
     }
 
@@ -937,19 +961,19 @@ BasePushProcessParameters(IN ULONG ParameterFlags,
         ProcessParameters->StandardError = StartupInfo->hStdError;
     }
 
-    /* Use Special Flags for ConDllInitialize in Kernel32 */
+    /* Use Special Flags for BasepInitConsole in Kernel32 */
     if (CreationFlags & DETACHED_PROCESS)
     {
         ProcessParameters->ConsoleHandle = HANDLE_DETACHED_PROCESS;
     }
-    else if (CreationFlags & CREATE_NO_WINDOW)
-    {
-        ProcessParameters->ConsoleHandle = HANDLE_CREATE_NO_WINDOW;
-    }
     else if (CreationFlags & CREATE_NEW_CONSOLE)
     {
         ProcessParameters->ConsoleHandle = HANDLE_CREATE_NEW_CONSOLE;
     }
+    else if (CreationFlags & CREATE_NO_WINDOW)
+    {
+        ProcessParameters->ConsoleHandle = HANDLE_CREATE_NO_WINDOW;
+    }
     else
     {
         /* Inherit our Console Handle */
@@ -1186,14 +1210,14 @@ GetProcessShutdownParameters(OUT LPDWORD lpdwLevel,
                                  NULL,
                                  CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepGetProcessShutdownParam),
                                  sizeof(BASE_GET_PROCESS_SHUTDOWN_PARAMS));
-    if (!(NT_SUCCESS(Status)) || !(NT_SUCCESS(ApiMessage.Status)))
+    if (!NT_SUCCESS(Status))
     {
         /* Return the failure from CSRSS */
-        BaseSetLastNTError(ApiMessage.Status);
+        BaseSetLastNTError(Status);
         return FALSE;
     }
 
-    /* Get the data out of the LCP reply */
+    /* Get the data back */
     *lpdwLevel = GetShutdownParametersRequest->Level;
     *lpdwFlags = GetShutdownParametersRequest->Flags;
     return TRUE;
@@ -1218,10 +1242,10 @@ SetProcessShutdownParameters(IN DWORD dwLevel,
                                  NULL,
                                  CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX, BasepSetProcessShutdownParam),
                                  sizeof(BASE_SET_PROCESS_SHUTDOWN_PARAMS));
-    if (!NT_SUCCESS(Status) || !NT_SUCCESS(ApiMessage.Status))
+    if (!NT_SUCCESS(Status))
     {
         /* Return the failure from CSRSS */
-        BaseSetLastNTError(ApiMessage.Status);
+        BaseSetLastNTError(Status);
         return FALSE;
     }
 
@@ -1849,7 +1873,7 @@ FatalAppExitW(IN UINT uAction,
     ULONG Response;
     NTSTATUS Status;
 
-    /* Setup the stirng to print out */
+    /* Setup the string to print out */
     RtlInitUnicodeString(&UnicodeString, lpMessageText);
 
     /* Display the hard error no matter what */
@@ -2517,6 +2541,7 @@ CreateProcessInternalW(HANDLE hToken,
     WCHAR SaveChar = 0;
     ULONG RetVal;
     UINT Error = 0;
+    UINT Length;
     BOOLEAN SearchDone = FALSE;
     BOOLEAN Escape = FALSE;
     CLIENT_ID ClientId;
@@ -2524,6 +2549,7 @@ CreateProcessInternalW(HANDLE hToken,
     PPEB RemotePeb;
     SIZE_T EnvSize = 0;
     BOOL Ret = FALSE;
+    WCHAR VdmPath[MAX_PATH];
 
     /* FIXME should process
      * HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
@@ -2604,6 +2630,16 @@ CreateProcessInternalW(HANDLE hToken,
         }
     }
 
+    /* Get the path to the VDM host */
+    Length = GetSystemDirectoryW(VdmPath, MAX_PATH - wcslen(NTVDM_STRING));
+    if ((Length == 0) || (Length >= MAX_PATH - wcslen(NTVDM_STRING)))
+    {
+        /* System path not found for some reason, fail */
+        SetLastError(ERROR_INVALID_NAME);
+        return FALSE;
+    }
+    wcscat(VdmPath, NTVDM_STRING);
+
     /*
      * According to some sites, ShellExecuteEx uses an undocumented flag to
      * send private handle data (such as HMONITOR or HICON). See:
@@ -2630,7 +2666,7 @@ CreateProcessInternalW(HANDLE hToken,
         while (NULL != (ScanString = wcschr(ScanString, L'^')))
         {
             ScanString++;
-            if (*ScanString == L'\"' || *ScanString == L'^' || *ScanString == L'\"')
+            if (*ScanString == L'\"' || *ScanString == L'^' || *ScanString == L'\\')
             {
                 Escape = TRUE;
                 break;
@@ -2840,14 +2876,13 @@ GetAppName:
             case STATUS_INVALID_IMAGE_PROTECT:
             case STATUS_INVALID_IMAGE_NOT_MZ:
 
-#if 0
             /* If it's a DOS app, use VDM */
             if ((BasepCheckDosApp(&ApplicationName)))
             {
                 DPRINT1("Launching VDM...\n");
                 RtlFreeHeap(RtlGetProcessHeap(), 0, NameBuffer);
                 RtlFreeHeap(RtlGetProcessHeap(), 0, ApplicationName.Buffer);
-                return CreateProcessW(L"ntvdm.exe",
+                return CreateProcessW(VdmPath,
                                       (LPWSTR)((ULONG_PTR)lpApplicationName), /* FIXME: Buffer must be writable!!! */
                                       lpProcessAttributes,
                                       lpThreadAttributes,
@@ -2858,7 +2893,6 @@ GetAppName:
                                       &StartupInfo,
                                       lpProcessInformation);
             }
-#endif
             /* It's a batch file */
             Extension = &ApplicationName.Buffer[ApplicationName.Length /
                                                 sizeof(WCHAR) - 4];
@@ -2918,7 +2952,7 @@ GetAppName:
                 DPRINT1("Launching VDM...\n");
                 RtlFreeHeap(RtlGetProcessHeap(), 0, NameBuffer);
                 RtlFreeHeap(RtlGetProcessHeap(), 0, ApplicationName.Buffer);
-                return CreateProcessW(L"ntvdm.exe",
+                return CreateProcessW(VdmPath,
                                       (LPWSTR)((ULONG_PTR)lpApplicationName), /* FIXME: Buffer must be writable!!! */
                                       lpProcessAttributes,
                                       lpThreadAttributes,
@@ -3241,7 +3275,6 @@ GetAppName:
                                      lpThreadAttributes,
                                      &SectionImageInfo,
                                      &ClientId,
-                                     bInheritHandles,
                                      dwCreationFlags);
 
     if (hThread == NULL)