How to Spawn a Console App and Redirect Standard Handles

Last reviewed: August 5, 1996
Article ID: Q126628
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

This article discusses spawning a console application with CreateProcess() and redirecting its output. The standard handles are controlled with the STARTUPINFO fields hStdInput, hStdOutput, and hStdError.

In Windows NT version 3.1, if a windowed application spawned a console application, you could:

  • Redirect none of its standard handles (don't use STARTF_USESTDHANDLES).

    -or-

  • Redirect all of its standard handles (use STARTF_USESTDHANDLES).

For example, if you redirected hStdInput and hStdOutput, but left hStdError as 0 or INVALID_HANDLE_VALUE, the console application would fail if it tried to write to stderr. This is not a problem for a console application spawning another console application.

In Windows NT version 3.5 and later and in Windows 95, if you set any of these fields to INVALID_HANDLE_VALUE, Windows NT will assign the default value to that handle in the console application, rather than leaving it an invalid value. Therefore, if you set STARTF_USESTDHANDLES, but fail to set one of the handle fields, this will not cause a problem for the console application. You can now redirect standard input, but not standard output, and so forth.


Additional reference words: 3.50 4.00
KBCategory: kbprg kbwebcontent
KBSubcategory: BseProcThrd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" 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.

Last reviewed: August 5, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.