Recent

Author Topic: [SOLVED]How to 'echo' before running TProcess executable?  (Read 609 times)

laznewb

  • New Member
  • *
  • Posts: 20
[SOLVED]How to 'echo' before running TProcess executable?
« on: February 16, 2020, 04:40:27 pm »
I'm trying to run OpenSSL using TProcess.Create.Executable and pass a string to OpenSSL instead of a filename. If I were to run the command in CMD, it would be look like this:

Quote
echo 'SomeTextToSecure' | openssl enc -a -out file.txt -pass pass:password123

But how would I run an executable using TProcess.Create.Executable, all while including the echo part? Obviously I could use RunCommand, but this is not preferable in this situation. Or is there another way to pass a string when the parameter expects a filename in CMD? Any ideas are much appreciated!  :)
« Last Edit: February 16, 2020, 05:37:33 pm by laznewb »

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: How to 'echo' before running TProcess executable?
« Reply #1 on: February 16, 2020, 04:48:35 pm »
You need to run the Command.exe or CMD.exe for that to happen..

or simply using WRITELN or something before that..
The only true wisdom is knowing you know nothing

laznewb

  • New Member
  • *
  • Posts: 20
Re: How to 'echo' before running TProcess executable?
« Reply #2 on: February 16, 2020, 05:36:57 pm »
Thank you jamie, I thought as much. Will just have to write to the file the string then delete the file straight after - will be secure enough for my purposes.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to 'echo' before running TProcess executable?
« Reply #3 on: February 16, 2020, 05:52:51 pm »
If the process expects something in the standard input (as seems to be the case) the easiest way to give it is to write it to the TProcess.Input stream  after it starts running. A very basic example:

Code: Pascal  [Select][+][-]
  1. AnString := 'SomeTextToSecure' + LineEnding; {Add other needed terminator(s)}
  2. AProcess := TProcess.Create(Nil);
  3. try
  4.   AProcess.Executable := 'openssl';
  5.   AProcess.Options := [poUsePipes]; {add other options}
  6.   {Set Parameters, etc.}
  7.   AProcess.Execute;
  8.   AProcess.Input.Write(AnString[1], Length(AnString));
  9.   if AProcess.WaitForExit then
  10.     {Do whatever when the process ends normally}
  11.   else
  12.     {Do whatever when the process errors out}
  13. finally
  14.   AProcess.Free;
  15. end;

You can find more info on this topic in the wiki: Executing External Programs
« Last Edit: February 16, 2020, 06:00:12 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018