Microsoft KB Archive/150956

{|
 * width="100%"|

INFO: Redirection Issues on Windows 95 MS-DOS Applications

 * }

Q150956

-

The information in this article applies to:


 * Microsoft Win32 Application Programming Interface (API), used with:
 * Microsoft Windows 95
 * Microsoft Windows 98

-

SUMMARY
Due to implementation differences on the Microsoft Windows 95, Windows 98, and Microsoft Windows NT platforms, programs need an intermediate Win32 Console Application to successfully redirect the output of 16-bit console based applications (MS-DOS applications and batch files) on Windows 95 and Windows 98.

MORE INFORMATION
Microsoft Knowledge Base article Q190351 HOWTO: Spawn Console Processes with Redirected Standard Handles demonstrates redirection of console based applications.

On Windows 95 and Windows 98 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 Q190351 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; 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 and Windows 98.

/*++ 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 and Windows 98. 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.

--*/
 * 1) include 

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); } }