Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CreateProcess and redirect output

Posted on 1998-10-03
5
Medium Priority
?
957 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 600 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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