Recent

Author Topic: TProcess  (Read 10291 times)

rjmatm

  • New Member
  • *
  • Posts: 11
TProcess
« on: July 13, 2004, 05:04:32 pm »
I'm trying to run an external application, just an 'ls' from my Lazarus application.  I was trying the TProcess component but I have run into a couple issues.

1) When I try to set the TProcess to active I get a runtime error - 'Access Violation'

2) I'm not quite sure how to handle the output stream from the TProcess so I can make it a readable string and send it to something like a TMemo.

Any help or even a point to relavent documention would be greatly apprciated.

Thanks

Drewski

  • Jr. Member
  • **
  • Posts: 55
TProcess
« Reply #1 on: July 13, 2004, 08:43:21 pm »
add "dos" to your uses section.

procedure RunCommand(Exec: String; Args: String);
var
fExec: String;
begin
 // you cant use just 'ls' you have to have the full path: '/bin/ls' which is what this does.
 fExec := FileSearch(Exec, GetEnv('PATH'));
 if Length(fExec) > 0 then begin
   with TProcess.Create(self) do begin
     CommandLine := fExec +' ' + Args;
     Execute;
   end;
 end
 else ShowMessage('Couldn''t find the ' +Exec+ ' executable!');
end;

As for the stream I think you can use Lines.LoadFromStream, but I am pretty sure that will block until the TProcess ends.

HTH
Andrew

rjmatm

  • New Member
  • *
  • Posts: 11
TProcess
« Reply #2 on: July 14, 2004, 03:54:33 am »
Thanks Andrew.

The command seems to work, now I'm just fighting with the output stream.

rjmatm

  • New Member
  • *
  • Posts: 11
TProcess
« Reply #3 on: July 14, 2004, 05:18:04 am »
First, thanks again Andrew, you got me going in the right direction.

After fighting with the streams and getting access violation messages for a couple of hours I found an old post in the mailing list archives.  It was about setting an option for the TProcess to [poUseStreams], will that didn't work.  I found that the new component implementation of TProcess uses [poUsePipes], that did it!  

For the sake of everyone trying to do this and not finding an answer I'm posting my code below.  As of this post my IDE about is dated '29-5-2004', so this should work if you have the same one.

I just have a form with a button and a TMemo.

// CODE
procedure TForm1.Button1Click(Sender: TObject);
  var
  fExec: String;
  Args: String;
  WorkingDir: String;
 
begin

  // Directory to ls
  WorkingDir := '/etc';
  // Search listings in your profile $PATH to find the command.  In this case it's ls
  fExec := FileSearch('ls', GetEnv('PATH'));
  // Arguments for the command
  Args := '-la';

  if Length(fExec) > 0 then begin

    with TProcess.Create(self) do begin
      CurrentDirectory := WorkingDir;
      Options := [poUsePipes];
      CommandLine := fExec + ' ' + Args;
      Execute;
      Memo1.Lines.LoadFromStream(OutPut);
    end;

  end
  else

    begin
      ShowMessage('Couldn not find the executable!');
    end;

end;

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2706
TProcess
« Reply #4 on: July 14, 2004, 05:24:27 pm »
Quote from: "andyman"
add "dos" to your uses section.

procedure RunCommand(Exec: String; Args: String);
....


Just for the record, the unit dos is only needed for the example and not for using tprocess
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Anonymous

  • Guest
TProcess
« Reply #5 on: October 11, 2004, 04:54:23 pm »
exactly what I was looking for :)

But I have another question:
How is it possible to use a pipe within a command ? I mean for instance the command call:
cat filename.xyz | grep abc



Thx for helping me out

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2706
TProcess
« Reply #6 on: October 12, 2004, 11:56:49 am »
you need a kind of shell for it.  something like sh cat filename.xyz |grep abc
(look in the manpages for details)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Rocky

  • Guest
TProcess
« Reply #7 on: October 22, 2004, 11:32:36 am »
Again me.
I am not able to view the documentation to tprocess, because I dont get it working and dont really look through, how to create the documentation.

But I have a question about TProcess, I hope someone can answer it.

I am using this code
Code: [Select]

 with TProcess.Create(self) do begin
CurrentDirectory := WorkingDir;
Options := [poUsePipes];
CommandLine := fExec + ' ' + Args;
Execute;
Memo1.Lines.LoadFromStream(OutPut);
end;


to run a binary I built a frontend for. But there is a big problem. At the first startup of this binary, it shows the license and asks for a yes.

Is there any way to pipe that yes - or does anyone have an idea, how I could resolve that issue ?
Is it possible to show the output (the license) of this binary (its displayed in the bash) in the frontend using Lazarus, and pushing the yes of the frontend to the binary back ?

I would be really happy if someone could help me out, giving me ideas how to solve that.

Thx a lot

Rocky

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2706
TProcess
« Reply #8 on: October 25, 2004, 11:49:58 am »
like you can read from the output pipe, you can write to the input pipe.
Somethinglike Process.Input.Write( ...
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018