Solved

CreateProcess and redirect output

Posted on 1998-10-03
5
942 Views
Last Modified: 2013-11-20
I am trying to use CreateProcess to execute a program like:
Myapp.exe -all > test.ini
-all works but the redirection does not work. Any solutions?
I don't use WinExec or system to avoid console window.
0
Comment
Question by:ames
5 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1322898
Redirection is a property of the shell not the underlying OS calls (like CreateProcess) that's why it doesn't work.

Your best option is:

- Add a command line option to MyApp.EXE that will tell MyApp.EXE should write to a file rather the stdout

There are two ways to implement

(a) Hardwork : implement in MyApp.EXE code to check the command line option and send output to a file rather stdout.  For example by replacing calls to printf with fprintf.

(b) Easier : using DuplicateHandle API in MyApp.EXE.  If command line option to redirect standard output is present you can then globally redirect MyApp.EXE's output to a different destination (the file), without making changes through MyApp.EXE.  If you need an example of how to do this look in the source for CFile::Duplicate which is in the MFC source code supplied with Visual C++.

Having said the above, I would pick option (a) unless there are tonnes of printf/cout calls scattered throughout MyApp.EXE.  The reason for picking (b) is they you don't want to, or can't change hundreds of calls to printf/cout.

0
 

Author Comment

by:ames
ID: 1322899
I do not have access to the source code of myapp.exe. I guess that takes out option (a). There is no command line option to redirect either. It takes out option (b). Am I out of luck? The goal is to execute a program and redirect output to a buffer.
0
 

Expert Comment

by:umarama
ID: 1322900
>>>> I don't use WinExec or system to avoid console window.

Well, I do know how to use WinExec to avoid console window.
I had used it to run a batch command. Check if the following works for you.

UINT uiRet = WinExec( Your CmdLine /*strBatchFile*/,
                      SW_SHOWMINNOACTIVE);

0
 

Expert Comment

by:umarama
ID: 1322901
In my project, I created a batch file which contained the execution steps. And then used WinExec with the SW_SHOWMINNOACTIVE flag to run the batch file.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 1322902
The problem with 'Myapp.exe -all > test.ini' is that the '>' redirection is done by the command shell and not by the called application (it would have to evaluate '>' as a parameter). You could either use the 'stdout' redirection by providing a valid (inheritable) file handle in the 'hStdOutput' member of 'STARTUPINFO' or simply use execute the command processor which will redirect the output: 'command.com /c myapp.exe -all > test.ini' (replace command.com with cmd.exe on NT)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

786 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