Recent

Author Topic: [SOLVED]How to work with Process1:TProcess  (Read 6856 times)

winnie

  • New member
  • *
  • Posts: 8
[SOLVED]How to work with Process1:TProcess
« on: June 01, 2005, 08:16:00 pm »
How can I send the output of an command into a memo?

in the objectinspector is a option: poUsePipes, but how does can I use this option?

Can someone make a little example or show me a page where I cann read this?

thanks
Patrick

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
[SOLVED]How to work with Process1:TProcess
« Reply #1 on: June 02, 2005, 09:25:54 am »
See this example on Lazarus CCR. Remember that the Lines property of a TMemo is a TStrings. Maybe you want to extend the page and add an example of adding the output to a TMemo.

winnie

  • New member
  • *
  • Posts: 8
[SOLVED]How to work with Process1:TProcess
« Reply #2 on: June 02, 2005, 11:26:31 am »
i have seen this help in the wiki... bit it doesn't work properly.
when i want to compile this unit as a class under lazarus he doesn't accept the syntax of pOpen in this unit
only when I compile this unit on console with fpc he accept the syntax of pOpen.
Do anybody know why??

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
[SOLVED]How to work with Process1:TProcess
« Reply #3 on: June 02, 2005, 11:32:31 am »
Where is pOpen mentioned in the wiki?

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2584
[SOLVED]How to work with Process1:TProcess
« Reply #4 on: June 02, 2005, 11:36:13 am »
Where on this wiki page do yuou find pOpen ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

winnie

  • New member
  • *
  • Posts: 8
[SOLVED]How to work with Process1:TProcess
« Reply #5 on: June 02, 2005, 11:44:33 am »
ups...sry.. i have mixed up diverent pages... :S

but pOpen and system doesn't work in a lazarus programm... even when I include the Libc

Code: [Select]

uses ..., Libc;

winnie

  • New member
  • *
  • Posts: 8
[SOLVED]How to work with Process1:TProcess
« Reply #6 on: June 02, 2005, 01:23:04 pm »
I have write a class with the last example, but when i want to start another programm my lazarus program crashed... :S

here is the sourcecode of the class:
Code: [Select]

unit cprozess;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Process, StdCtrls, Dialogs;
 
const
  READ_BYTES = 2048;
 
 
type TProzessdata = class(TObject)
 
     private

     public
           function execute_programm(prog:String):TStringlist;
     
     end;
     

implementation

function TProzessdata.execute_programm(prog:String):TStringList;
var
  S: TStringList;
  M: TMemoryStream;
  P: TProcess;
  n: LongInt;
  BytesRead: LongInt;
begin;
      M := TMemoryStream.Create;
      BytesRead := 0;

      P := TProcess.Create(nil);
      P.CommandLine := 'halt';
      P.Options := [poUsePipes];
      P.Execute;
      while P.Running do
            begin
                 M.SetSize(BytesRead + READ_BYTES);
                 n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
                 if n > 0 then
                         Inc(BytesRead, n)
                 else
                     Sleep(100);
            end;
      repeat
            M.SetSize(BytesRead + READ_BYTES);
            n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
            if n > 0 then
               Inc(BytesRead, n);
      until n <= 0;
      M.SetSize(BytesRead);
      S := TStringList.Create;
      S.LoadFromStream(M);
      S.Free;
      P.Free;
      M.Free;
      execute_programm:=S;
end;
end.


Code: [Select]

uses ..., cprozess;
...
var
  Form1: TForm1;
  startop:TStartup;
  prozessop:TProzessdata;
...
procedure TForm1.Button1Click(Sender: TObject);
Var local: TSTringlist;
begin
     local:=prozessop.execute_programm('');
     memo1.lines:=local;
end;


what is wrong?

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
[SOLVED]How to work with Process1:TProcess
« Reply #7 on: June 02, 2005, 01:38:55 pm »
Two things:
Code: [Select]
S.Free;
If you want to return the stringlist and use it, you shouldn't free it.

Code: [Select]
memo1.lines:=local;
I am not sure assigning it like this, is going to work. Maybe memo1.lines.Assign(local); is a better idea.

winnie

  • New member
  • *
  • Posts: 8
[SOLVED]How to work with Process1:TProcess
« Reply #8 on: June 02, 2005, 03:16:16 pm »
*g* thanks it works :)
i will upload this class to the wiki tomorrow.

 

TinyPortal © 2005-2018