?
Solved

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

Posted on 2003-03-04
8
Medium Priority
?
336 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:moedermaria
[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
  • 4
  • 2
8 Comments
 
LVL 8

Accepted Solution

by:
Exceter earned 400 total points
ID: 8068281
>> 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
 

Expert Comment

by:ZalexZ
ID: 8073406
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
 
LVL 8

Expert Comment

by:Exceter
ID: 8074210
>> 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Expert Comment

by:ZalexZ
ID: 8087086
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
 
LVL 8

Expert Comment

by:Exceter
ID: 8090523
>> ...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
 

Expert Comment

by:CleanupPing
ID: 9447377
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
 
LVL 8

Expert Comment

by:Exceter
ID: 9450939
Points to: Exceter.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Progress

777 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