redirecting stdio for sequential programs in an MFC dialog

Dennis Foreman 0 Reputation points

My C++ MFC dialog creates 2 child processes sequentially. I redirect STDOUT to separate files for each of the programs (and restore to default after each one runs). STDOUT from program 1 gets sent to its file as expected. Program 2 STDOUT disappears. Both programs return 0. I know Program 2 runs because a MessageBox it creates for debugging appears. To remove any other effects, I have reduced Program 2 to just: std::cout<<"aha"<<std::endl;

Is there anything I should know about doing this? PS. I have no access to the source code for the REAL programs 1 and 2.

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,749 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 30,101 Reputation points

    Although the CRT will create inheritable files, the STARTUPINFO struct uses Win32 HANDLE types, not FILE* pointers returned from CRT functions.

    For example, to obtain the low-level HANDLE for use with STARTUPINFO from a CRT FILE* pointer -

    FILE *fp = _tfopen(_T("Created by ConsoleApplication1.txt"), _T("w"));
    if (fp)
        hFile = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp)));
    if (hFile != INVALID_HANDLE_VALUE)
    //   CreateProcess code using hFile in STARTUPINFO