Recent

Author Topic: Calling, executing, and passing argument to external programs from a GUI  (Read 6745 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Hi

I am coding a GUI to help automate decryption of a particular encryption system (eCryptfs).

There is a Linux command called ecryptfs-unwrap-passphrase that resides in /usr/bin/. When it's ran, it asks the user for their login password, a bit like when you try and do something using sudo or when you try to su.

I am trying to call this program from mine. I have read http://www.freepascal.org/docs-html/fcl/process/tprocess.html and followed http://wiki.lazarus.freepascal.org/Executing_External_Programs.

Before I continue trying to make this work, I have two questions :

1) Can you call a none graphical command line program from within a GUI like I am trying to do? Though my program compiles, it hangs, and I can't work out if that because the answer to this question is "no, you can't", or if it's due to question 2

2) How do you call a program and also pass that program additional values? i.e. Assuming the answer to 1 above is "yes", and the reason my program is hanging is merely because ecryptfs-unwrap-passphrase is waiting for the user to input a password but it can't be displayed on screen, how do I pass both the command and the password that I want it to use? e.g. ecryptfs-unwrap-passphrase MyPassword

So far I have :

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  
  3. var
  4.   FileToAttack : String;
  5.   UnwrapPassphraseProcess: TProcess;
  6.   UnwrapPassphraseOutput: TStringList;
  7.  
  8. begin
  9.   if OpenDialog1.Execute then
  10.     begin
  11.     FileToAttack := OpenDialog1.FileName;
  12.     if FileExists(FileToAttack) then
  13.        begin
  14.        Edit1.Caption := FileToAttack;
  15.        UnwrapPassphraseProcess := TProcess.Create(nil);
  16.        UnwrapPassphraseOutput.Create;
  17.  
  18.        UnwrapPassphraseProcess.CommandLine := ('/usr/bin/ecryptfs-unwrap-passphrase ');
  19.        // Have also tried ..:= ('/usr/bin/ecryptfs-unwrap-passphrase ' + 'MyPassword')
  20.        UnwrapPassphraseProcess.Options := UnwrapPassphraseProcess.Options + [poWaitOnExit, poUsePipes];
  21.        UnwrapPassphraseProcess.Execute;
  22.        UnwrapPassphraseOutput.LoadFromStream(UnwrapPassphraseProcess.Output);
  23.        UnwrapPassphraseOutput.SaveToFile('/home/ted/testing.txt');
  24.  
  25.        UnwrapPassphraseProcess.Free;
  26.        UnwrapPassphraseOutput.Free;
  27.        OpenDialog1.Close;
  28.        end
  29.      else
  30.         ShowMessage('An error occurred opening the passphrase file.');
  31.     end;
  32. end;                                        
  33.  

Thanks

Ted


Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Quote
1) Can you call a none graphical command line program from within a GUI like I am trying to do?
Yes, if not, we won't have many GUI wrappers created with Lazarus.
Quote
2) How do you call a program and also pass that program additional values? i.e. Assuming the answer to 1 above is "yes", and the reason my program is hanging is merely because ecryptfs-unwrap-passphrase is waiting for the user to input a password but it can't be displayed on screen, how do I pass both the command and the password that I want it to use? e.g. ecryptfs-unwrap-passphrase MyPassword
Exploit the Input and Output (and probably StdErr) properties of TProcess. Use Input to write to the process (as if you type something with the keyboard) and Output for reading what the program outputs. Don't forget that some (or most?) programs requires EOLn to be sent before they process the given input. Try the attached example for concrete code.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
That is very useful...thanks! And thanks for taking the time to attach the example. I had read about the Input and Output properties of TProcess shortly before posting this but I couldn't work out exactly how it worked. Your example has clarified that so I will try again tonight when I get home.

Ted

 

TinyPortal © 2005-2018