CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2002
    Location
    New Zealand
    Posts
    32

    Capturing Console Output

    A lot of IDE's capture the output from some command line utility, displaying it in a window on the screen. Any hints on how to go about 'capturing' the output?


  2. #2
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658

    Re: Capturing Console Output


    BOOL PipeResult( const CString& CommandLine, CString& strOutFile )
    {
    STARTUPINFO si;
    memset( &si, 0, sizeof( si ) );
    si.cb = sizeof( si );
    // Hide DOS window
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    // Prepare pipe handles for standard output redirection
    SECURITY_ATTRIBUTES saAttr;
    // Set the bInheritHandle flag so pipe handles are inherited.
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;
    HANDLE hReadPipe, hWritePipe;
    BOOL res = CreatePipe( &hReadPipe, &hWritePipe, &saAttr, 0 );
    if ( !res )
    {
    AfxMessageBox(_T("Problem pipe creation"));
    }
    else
    {
    si.dwFlags |= STARTF_USESTDHANDLES;
    si.hStdOutput = hWritePipe;
    PROCESS_INFORMATION pi;
    res = CreateProcess( NULL, (LPTSTR)(LPCTSTR)CommandLine, NULL, NULL, TRUE,
    0, NULL, NULL, &si, &pi );
    if (!res)
    {
    // process error of rsh
    AfxMessageBox(_T("Problem optimizer running"));
    }
    else
    {
    // Close write pipe
    CloseHandle( hWritePipe );
    DWORD NumberOfBytesRead;
    TCHAR Buffer[ 257 ];
    while(::ReadFile( hReadPipe, Buffer, sizeof(Buffer)/sizeof(Buffer[ 0 ])-1,
    &NumberOfBytesRead, NULL ) )
    {
    if (NumberOfBytesRead)
    {
    Buffer[ NumberOfBytesRead ] = TCHAR( 0 );
    strOutFile += Buffer;
    }
    else
    break;
    };
    // Close read pipe and wait for the finish
    CloseHandle( hReadPipe );
    WaitForSingleObject( pi.hProcess, INFINITE );
    res = TRUE;
    }
    }
    return res;
    }






    Rating isn't important...But gurus respect it and keep high

  3. #3
    Join Date
    Jul 2003
    Posts
    25
    Thanks for the code.

    I tried it and everything works fine.

    I dont know but I am reaching the
    Code:
    if (NumberOfBytesRead)
    {
    Buffer[ NumberOfBytesRead ] = TCHAR( 0 );
    strOutFile += Buffer;
    }
    else
    break;
    section only when the child process ( it is a console app ) is done with and it has outputted everything in the stdout. Therefore, I can show output only after the whole process is over.

    What I require is that I get a call whenever the exe writes something in the console, so that i can get the text and add it to my edit box.

    Something like VC++, which gives information about compiling of each and every .cpp as it is compiled? I mean...one line after another?

    Any help?

  4. #4
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    Now MSDN offers better code under the title

    "Creating a Child Process with Redirected Input and Output"

    Usage PeekNamedPipe could help as well

  5. #5
    Join Date
    Jul 2003
    Posts
    25
    I have tried that. Same problem. It is outputting after the child process gets over.

    I tried google groups..and none of the sample codes behave in an interactive manner where they show the output as they get it.

    I will try PeekNamedPipe.....havnt used Named Pipes till now? Any code sample to try out...

    Thanks for your response.

  6. #6
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    1. PeekNamedPipe is applicable to anonymous pipe as well
    2. Probably the problem is your child process

  7. #7
    Join Date
    Jul 2003
    Posts
    25
    The child process is doing around 10 same type of jobs. Each job is computationally complex and it takes around 2min to finish one job. After that it outputs the result of each job on stdout using *printf* function.

    After the process is over say after 20 minutes..all the result comes in a hurry...?

    Any guesses?

  8. #8
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    I assume that you output job results upon finishing job.
    Try to use fflush(NULL) for child process at the end of job

  9. #9
    Join Date
    Jul 2003
    Posts
    25
    THANK YOU THANK YOU THANK YOU.

    fflush ( NULL ) didi the trick?

    Well i am not a consol guy? Can you tell me the reason?

  10. #10
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    Because console output( as well as other file output ) is buffered

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured