Knowledge Base

INFO: Redirection Issues on Windows 95 MS-DOS Applications

PSS ID Number: 150956

Article Last Modified on 5/12/2003


The information in this article applies to:


This article was previously published under Q150956

SUMMARY

On Windows 95, Windows 98, and Windows Millennium Edition (Me), programs need an intermediate Win32 Console Application to successfully redirect the output of 16-bit console based applications (MS-DOS applications and batch files) due to implementation differences on the following operating systems:
  • Microsoft Windows 95, Windows 98, or Windows Me
  • Microsoft Windows NT, Windows 2000, or Windows XP

MORE INFORMATION

For additional information about redirection of console-based applications, click the following article number to view the article in the Microsoft Knowledge Base:

190351 HOWTO: Spawn Console Processes with Redirected Standard Handles


On Windows 95, Windows 98, and Windows Me programs can exhibit blocking when using the ReadFile() API to read from the redirected handles of 16-bit console based applications (including console based MS-DOS applications and batch files.) In this situation ReadFile() remains blocked even after the redirected 16-bit console based application has terminated.

Programs can avoid this blocking situation by launching an intermediate Win32 Console application as a stub process between the Win32 parent and the 16-bit console based child. The creation of the stub application results in a Win32 console. The stub application then spawns the 16-bit console based application in the same console, forwarding it's redirected standard handles. As a result, the 16-bit console based application inherits the hidden console and the redirected standard handles.

To launch the stub application with redirected standard handles, use the technique demonstrated in the following article:

190351 HOWTO: Spawn Console Processes with Redirected Standard Handles

To make the stub console hidden set the following flags in the STARTUPINFO structure:
Startupinfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;<BR/>
Startupinfo.wShowWindow = SW_HIDE;
				
NOTE: If the original parent application is a Win32 Console application, and the 16-console based application being launched uses the parent's console, then this work around is not needed.

Sample Code

The following code sample demonstrates an intermediate stub application for redirecting 16-bit console based applications on Windows 95, Windows 98, and Windows Me.
/*++
         Module Name:

            CONSPAWN.C

         Description:

            Serves as an intermediate stub Win32 console application to
            avoid a hanging pipe when redirecting 16-bit console based
            programs (including MS-DOS console based programs and batch
            files) on Window 95, Windows 98, and Windows Me.
            This program is to be launched with redirected standard
            handles. It will launch the command line specified 16-bit
            console based application in the same console, forwarding
            it's own redirected standard handles to the 16-bit child.

--*/    
#include <windows.h>

void main (int argc, char *argv[])
{
   BOOL                bRet = FALSE;
   STARTUPINFO         si   = {0};
   PROCESS_INFORMATION pi   = {0};

   // Make child process use this app's standard files.
   si.cb = sizeof(si);
   si.dwFlags    = STARTF_USESTDHANDLES;
   si.hStdInput  = GetStdHandle (STD_INPUT_HANDLE);
   si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
   si.hStdError  = GetStdHandle (STD_ERROR_HANDLE);

   bRet = CreateProcess (NULL, argv[1],
                         NULL, NULL,
                         TRUE, 0,
                         NULL, NULL,
                         &si, &pi
                         );
   if (bRet)
   {
      WaitForSingleObject (pi.hProcess, INFINITE);
      CloseHandle (pi.hProcess);
      CloseHandle (pi.hThread);
   }
}
				

REFERENCES

For additional information about redirection of console-based applications, click the following article number to view the article in the Microsoft Knowledge Base:

190351 HOWTO: Spawn Console Processes with Redirected Standard Handles


Additional query words: win95 winnt inherit std redirect

Keywords: kbConsole kbinfo kbKernBase KB150956
Technology: kbAudDeveloper kbOSWin2000 kbOSWin95 kbOSWin98 kbOSWinME kbOSWinNTSearch kbOSWinSearch kbOSWinXP kbOSWinXPSearch kbWin32API kbWin32sSearch