c++, give input to dos prompt with command 'system'

hi,
i'm using c++ builder 6 and i'm writing a program here at work. this problem came up:
i want to decrypt a rsa private key in my program. now i can give one dos-command to the promptand will be executed fine:

AnsiString command.sprintf("openssl rsa -in %s -out %s",inputfile,outputfile);
system(command.c_str());

after this line the command prompt waits for the password so i have to enter the password using the keyboard. i don(t want this i want to be able to pass the password to the command prompt so that my key will be decrypted without having to interfere manually.

if this cannot be done in C++ plz advise on a batch file that does the same...


greetz, drizzz
moedermariaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ExceterCommented:
>> if this cannot be done in C++ plz advise on a batch file that does the same...

Sure, this can be done in C/C++. Simply call the program with popen() instead of system. This allows you to write to the executed program's input stream. For example,

FILE *chkdsk;

if( (chkdsk = popen( command.c_str(), "wt" )) == NULL )
   exit( 1 );
fputs( "password", chkdsk );
printf( "\nProcess returned %d\r\n", pclose( chkdsk ) );

Replace "password" with the text you wish to enter.

Exceter
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZalexZCommented:
Hi,

wrote this already in the duplicated question;-)

you can create a answer file and redirect stdin!!

TRy something like this(but i dont know if it will work with pwd):

echo testpwd > answer.txt
myapp.exe < answer.txt

Then when your prog read from stdin it will get the first line, second read->second line and so on!

Regards
AZ
0
ExceterCommented:
>> wrote this already in the duplicated question;-)

Actually, you wrote it in the duplicate question, this one is the original. :-)

Besides, assuming this does work, it is still much simpler to use popen() because it returns a FILE pointer and you can write DIRECTLY into the sub process' stdin stream. :-)

Exceter
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ZalexZCommented:
OK, Exceter your completly right, but ...

I tried to build a little Prog with popen, and the linker can't find this function!
So i tried it on a UNIX-Machine and it worked!

Are you sure this function is avaiable to DOS???

Regards
AZ
0
ExceterCommented:
>> ...and the linker can't find this function!

popen() is found in stdio.h and, unless you are using C++, I doubt that you forgot to include this as you would not be able to write to stdout. What compiler are you using? If you are using a Microsoft compiler try placing an underscore before the function name. For example,

if( (chkdsk = _popen( command.c_str(), "wt" )) == NULL )

...

printf( "\nProcess returned %d\r\n", _pclose( chkdsk ) );

Exceter
0
CleanupPingCommented:
moedermaria:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
ExceterCommented:
Points to: Exceter.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.