Solved

CreateProcess and redirect output

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

729 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