?
Solved

CreateProcess and redirect to file using hStdOutput

Posted on 2006-10-30
7
Medium Priority
?
6,400 Views
Last Modified: 2013-11-20
Hi Experts,
I would like to redirect the output of a command line program to a file using hStdOutput.
However, while the program happily displays its output in its dos window, the file is not written.
What am I doing wrong?

here is what I am doing:
      STARTUPINFO si;
      ZeroMemory( &si, sizeof(si) );
      si.cb = sizeof(si);
      si.dwFlags = STARTF_USESHOWWINDOW;
      si.wShowWindow = SW_SHOW;
      CFile file;
      file.Open(outFile.c_str(),CFile::modeWrite);
      ofstream out(outFile.c_str());

//Both approaches don't work
      si.hStdOutput = out;
                si.hStdOutput = file;
//ofstream is at least creating the file

      if( !CreateProcess(
            0,//nExeLoc.c_str(),            // Location of executable
            //Cannot take full path (spaces) MAybe with \"\"
            commandLine.GetBuffer(),// Command line parameters.
            NULL,           // Process handle not inheritable.
            NULL,           // Thread handle not inheritable.
            FALSE,          // Set handle inheritance to FALSE.
            0,              // No creation flags.
            NULL,           // Use parent's environment block.
            wd.GetBuffer(),        // Use parent's starting directory.
            &si,            // Pointer to STARTUPINFO structure.
            &procInfo )           // Pointer to PROCESS_INFORMATION structure.
      ) {
            //Error occured!
            return(false);
      }

This is urgent for me.
Thanks,
Jens
0
Comment
Question by:allmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17832326
Instead of CFile use CreateFile function which returns HANDLE. Use this handle to fill members of STARTUPINFO structure for redirecting program output.
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 1000 total points
ID: 17832455
I think you need to specify STARTUPINFO.dwFlags = STARTF_USESTDHANDLES

As per docs :

dwFlags :
STARTF_USESTDHANDLES If this value is specified, sets the standard input of the process, standard output, and standard error handles to the handles specified in the hStdInput, hStdOutput, and hStdError members of the STARTUPINFO structure. The CreateProcess function's fInheritHandles parameter must be set to TRUE for this to work properly.

If this value is not specified, the hStdInput, hStdOutput, and hStdError members of the STARTUPINFO structure are ignored.  <==

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/startupinfo_str.asp


Here is full sample code :
How to spawn console processes with redirected standard handles : http://support.microsoft.com/kb/190351 <=======

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17832470
as per bove which means you also need STARTUPINFO.bInheritHandle = TRUE

STARTUPINFO si;

si.dwFlags = STARTF_USESTDHANDLES;
si.bInheritHandle = TRUE;

...

Refer above link for full example code.

-MAHESH
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 total points
ID: 17834337
AlexFM is correct about 'CreateProcess()', however some constraints have to be met regarding how to set it up. Here's a code snippet (taken from 'production' code) which does that:

    SECURITY_ATTRIBUTES sa;
    STARTUPINFO         si;
    PROCESS_INFORMATION pi;
    HANDLE              hStdOut;
    BOOL                bRes;

    sa.nLength              =   sizeof  (   SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor =   NULL;
    sa.bInheritHandle       =   TRUE;

    dwFlags =       FILE_ATTRIBUTE_NORMAL;

    hStdOut =   CreateFile  (   "c:\\test.out",
                                GENERIC_WRITE,,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                &sa,
                                CREATE_ALWAYS,
                                dwFlags,
                                NULL
                            );

    ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

    si.cb           =   sizeof  (   STARTUPINFO);
    si.dwFlags      =   STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.wShowWindow  =   SW_HIDE;
    si.hStdInput    =   NULL;
    si.hStdOutput   =   hStdOut; // output file handle
    si.hStdError    =   NULL;

    bRes    =   CreateProcess   (   "sample.exe",
                                    NULL,
                                    NULL,
                                    NULL,
                                    TRUE,
                                    NORMAL_PRIORITY_CLASS,
                                    GetEnvironmentStrings   (),
                                    "C:\\somedir",
                                    &si,
                                    &pi
                                );

    // wait with patience .....
    WaitForSingleObject (   pi.hProcess,    INFINITE);
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question