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
Solved

CreateProcess and redirect output

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 42
bobThere java chalenge 45 94
Separate into files by filename 12 87
Message not shown 5 67
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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