Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a console (Win) application (not mine so no source code available).
The application doesn't support command line input parameters. :sigh:
The application asks, when open, some number and strings from console, like:

OPENFILE filename1
GETOPTION filename2
SETVALUE optionvalue numericalvalue
SAVEOPTION filename2
RUNFUNCTION fname1
RUNFUNCTION fname2
SAVEOUTPUT filename3
EXIT


Because the strings to be used are always quite similar I was thinking to prepare an helper program (c++) that uses for me this console application.
So the question is:

How can I redirect stdin of that application putting there what I desire and let the application run the wanted commands?

I know that with DOS it is possible to redirect input output with > and <, but they looks works only to manage command line parameters.

The best solution is to do not save any file but to manage the 'script' anly with code.

Any helps?
Posted

Solved:
simply need to build a pipe with _popen

Bye
 
Share this answer
 
Alternatively, you can write a wrapper program that launches this app using CreateProcess. Specify a STARTUPINFO struct with the hStdInput and hStdOutput handles specified to your own choosing and set dwFlags to STARTF_USESTDHANDLES. See msdn docs for CreateProcess and STARTUPINFO for details.
 
Share this answer
 
Here is a link with an example program about redirection: http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx[^]
Of course you have to modify that according to your needs, you need to redirect only stdin. Note that a mistake that is easy to make is not marking the pipe handle as inheritable, and not closing the pipe handle on the parent side after the child process inherited it. In addition to this you have to decide whether to make this prog safe. If you go the easy way than you use just 1 thread, create the process, write its stdin (this may block), and then wait for the process to terminate. In this case your program may freeze (for example if the child process does not consume all your std input). Another solution is to use 2 threads. 1 thread can be busy by writing the stdin, the other thread can wait for the process to terminate. This way you can put in additional wait timeout to make your prog safe (WaitForSingleObject() on the process handle). If the process waiter thread returns from wait then it can terminate the other thread as well if that is still hanging on the write handle of the redirected stdin.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900