How to execute commands using Free Pascal/Lazarus

You can execute OS commands in Free Pascal / Lazarus by using TProcess component that exists in System component palatte.

TProcess also can open external applications from your Lazarus project.

Example:

1. Create new application, put Edit box, a button, TProcess and a Memo in main form:

Image

2. Change UsePiples property to True in TProcess.Options.

3. Write this code in button’s OnClick:


procedure TForm1.Button1Click(Sender: TObject);
begin
  Process1.CommandLine:= Edit1.Text;
  Process1.Execute;
  Memo1.Lines.LoadFromStream(Process1.Output);
end;

4. Run the application and write any commands like (ls in Linux, dir in Windows) and click the button. You will get the result of text commands in the memo:

You can also run applications, like calculator, text editor, or any other application.

11 thoughts on “How to execute commands using Free Pascal/Lazarus

  1. في الحقيقة لا أعرف آلية عملها، وربما تستهلك المعالج في حالة إنتظار إنتهاء البرنامج اﻵخر. لكن لم تحدث لي مشاكل معها، إستخدمها مرة واحدة في برنامج تسجيل عندما يتم تحديث البرنامج.
    وربما تختلف من نظام تشغيل إلى آخر
    على كل حال يُمكنك تجربة الخيارات ومقارنة إستهلاك الذاكرة والمعالج مع تغيير تلك الخيارات

  2. أستاذي الفاضل …
    ويكي لازاريس يوفر مثالا كاملا للتعامل مع البرامج التي لها مخرجات وهذا هو الكود

    program procoutlarge;
    {
    Copyright (c) 2004-2011 by Marc Weustink and contributors

    This example is created in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    }
    // This is a
    // WORKING
    // demo program that shows
    // how to launch an external program
    // and read from its output.

    uses
    Classes, Process, SysUtils;

    const
    READ_BYTES = 2048;

    var
    OurCommand: String;
    OutputLines: TStringList;
    MemStream: TMemoryStream;
    OurProcess: TProcess;
    NumBytes: LongInt;
    BytesRead: LongInt;

    begin
    // A temp Memorystream is used to buffer the output
    MemStream := TMemoryStream.Create;
    BytesRead := 0;

    OurProcess := TProcess.Create(nil);
    // Recursive dir is a good example.
    OurCommand:=’invalid command, please fix the IFDEFS.’;
    {$IFDEF Windows}
    //Can’t use dir directly, it’s built in
    //so we just use the shell:
    OurCommand:=’cmd.exe /c “dir /s c:\windows\”‘;
    {$ENDIF Windows}
    {$IFDEF Unix}
    //Needs to be tested on Linux/Unix:
    OurCommand := ‘ls –recursive –all -l /’;
    {$ENDIF Unix}
    writeln(‘– Going to run: ‘ + OurCommand);
    OurProcess.CommandLine := OurCommand;

    // We cannot use poWaitOnExit here since we don’t
    // know the size of the output. On Linux the size of the
    // output pipe is 2 kB; if the output data is more, we
    // need to read the data. This isn’t possible since we are
    // waiting. So we get a deadlock here if we use poWaitOnExit.
    OurProcess.Options := [poUsePipes];
    WriteLn(‘– External program run started’);
    OurProcess.Execute;
    while OurProcess.Running do
    begin
    // make sure we have room
    MemStream.SetSize(BytesRead + READ_BYTES);

    // try reading it
    NumBytes := OurProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
    if NumBytes > 0
    then begin
    Inc(BytesRead, NumBytes);
    Write(‘.’) //Output progress to screen.
    end
    else begin
    // no data, wait 100 ms
    Sleep(100);
    end;
    end;
    // read last part
    repeat
    // make sure we have room
    MemStream.SetSize(BytesRead + READ_BYTES);
    // try reading it
    NumBytes := OurProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
    if NumBytes > 0
    then begin
    Inc(BytesRead, NumBytes);
    Write(‘.’);
    end;
    until NumBytes 0 then WriteLn;
    MemStream.SetSize(BytesRead);
    WriteLn(‘– External program run complete’);

    OutputLines := TStringList.Create;
    OutputLines.LoadFromStream(MemStream);
    WriteLn(‘– External program output line count = ‘, OutputLines.Count, ‘ –‘);
    for NumBytes := 0 to OutputLines.Count – 1 do
    begin
    WriteLn(OutputLines[NumBytes]);
    end;
    WriteLn(‘– Program end’);
    OutputLines.Free;
    OurProcess.Free;
    MemStream.Free;
    end.

    المشكلة أنه عند تشغيل برنامج مثل
    wget
    لا نجد أي مخرجات على التارمينال
    فهل من خبير أو عارف بحل هذه المشكلة ؟

  3. سلام عليك أستاذ وصبحك الله بكل خير
    البرنامج
    wget
    يعمل في الخلفية دون مشاكل ولكن مخرجاته التي نعرفها لما ننفذه من التارمينال لا تظهر في برنامجنا الذي أنشأناها
    أعني بالمخرجات مثل سرعة التحميل
    دمت بود

  4. تتم قراءة المخرجات عند إنغلاق البرنامج الذي تم ندائه. ولا تتم قراءة المخرجات أثناء البرنامج يعمل

  5. البرنامج عمل بنجاح. لكن لم يُخبرني أن التحميل إنتهى. كذلك عند إعطائه ملف رابط غير موجود يعلق

    >>while OurProcess.Running do

    نعم مادام البرنامج الخارجي مازال يعمل فإن هذا الشرط يتحقق وسيبقى برنامج لازاراس في هذه الحلقة لايخرج منها إلى أن ينتهي البرنامج الثاني

Leave a comment