Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-04
8
Medium Priority
?
347 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
  • 4
  • 2
7 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Screencast - Getting to Know the Pipeline

575 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