Microsoft KB Archive/38308

{|
 * width="100%"|

PRB: Spawn Does Not Pass Redirection to Child
'Article ID: Q38308

Creation Date: 22-NOV-1988

Revision Date: 19-JAN-1996' The information in this article applies to:

  The C Run-time (CRT), included with: - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and 1.52 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, and 4.0 SYMPTOMS

Using the program below to spawn SORT.EXE on MS-DOS, the following error message is generated: SORT: Too many parameters Running the same program on Windows NT causes the following error message to be generated: SORT: Invalid Switch CAUSE

This is the expected behavior as COMMAND.COM or CMD.EXE (the command interpreter for Windows NT), not the EXEC loader, handles indirection. Child processes inherit the handles of their parents; therefore, to redirect the input and output of the child, first change the definitions of STDIN and STDOUT in the parent process. RESOLUTION

The proper way to redirect input for a filter is described starting on page 441 of the &quot;MS-DOS Encyclopedia,&quot; along with a complete MASM example. Note: The dup, dup2, _dup, and _dup2 functions in the MS-DOS C run-time library are the same as the Interrupt 21h functions 45h and 46h, respectively. There also are partial examples of this technique included with the C run-time library reference documentation for the _dup related functions and on page 353 of &quot;Advanced MS-DOS Programming&quot; (by Duncan, published by Microsoft Press). It is possible to use freopen to redefine STDIN and STDOUT; however, doing so loses any redirection that may have been performed on the parent process. The easiest workaround is to use the system function to spawn a copy of COMMAND.COM or CMD.EXE that runs SORT.EXE, as follows: system(&quot;sort.exe outfile.dat&quot;); MORE INFORMATION

The following code sample demonstrates the problem. Sample Code

/* Compile options needed: none */ char *args[6] ; args[0] = &quot;sort&quot; ; args[1] = &quot;<&quot; ; args[2] = &quot;infile.dat&quot; ; /* exists */ args[3] = &quot;>&quot; ;        /* direct output to a disk file */ args[4] = &quot;outfile.dat&quot; ; args[5] = NULL ;
 * 1) include  #include  main {

spawnvp (P_WAIT, &quot;sort.exe&quot;, args) ; } 
 * }

-

"THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY." '' ©1997 Microsoft Corporation. All rights reserved. Legal Notices.

''

Additional reference words: 1.00 1.50 2.00 2.10 4.00 6.00 6.00a 6.00ax 7.00

KBCategory: kbprg kbprb

KBSubcategory: CRTIss