Solved

CreateProcess and redirect output

Posted on 1998-10-03
5
935 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now