Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > alt.comp.lang.pascal > #18
| From | "Carlos E.R." <robin_listas@es.invalid> |
|---|---|
| Newsgroups | alt.comp.lang.pascal |
| Subject | Re: Help - TProcess |
| Date | 2018-08-23 16:18 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <eas25f-r0l.ln1@Telcontar.valinor> (permalink) |
| References | <vc4tndh5eqq931rjir6duo9laumfitg3jl@4ax.com> |
On 2018-08-23 13:14, Shadow wrote:
> Using Lazarus - Free Pascal - Windows and Linux
>
> Wanted to write a GUI program that retrieves (URL_link
> :=Edit1.Text;) an URL and downloads the file, and writes the log to a
> downloads.txt, so I have a record of where I downloaded things. It's
> useful for when I archive my downloads, I keep the downloads.txt with
> the files.
>
> So I used ShellExecute, and that worked fine, but only for
> Windows.
> BUT I can't include any switches, ie, if I want check the date
> of a download, there is no way I can do this (example wraps because of
> my newsreader):
>
> <wget --server-response --spider
> http://downloads.pdf-xchange.com/PDFX_Vwr_Port.zip>
>
> and then this
>
> <wget -c http://downloads.pdf-xchange.com/PDFX_Vwr_Port.zip -O
> PDFX_Vwr_Port_v2.5.322.9.zip>
>
> If I could include switches, I could add them via radio
> buttons and get the output in a text window.
>
> SO, I read up on external programs and switches:
>
> http://wiki.freepascal.org/Executing_External_Programs
>
> And saw that TProcess accepts switches and is also cross
> platform. But NONE of the examples the guy provides compile. Every
> single one errors out.
>
> The "man page" wasn't much better, it looks like gibberish to me:
>
> https://www.freepascal.org/docs-html/fcl/process/tprocess.html
>
> Anyone familiar with TProcess help me out ? Maybe write a very
> simple program that takes switches and adds them to the command line
> and then writes the output to a file ?
> I need an example that works to start with.
> TIA
> []'s
>
This is a function that executes a program in Linux in a code of mine. I
can not now write a sample code, so instead I excerpted it.
procedure TMyApplication.DoRebootRouter;
// See
<http://wiki.freepascal.org/Executing_External_Programs#TProcess> for
the doc on calling a program and getting its output.
const
BUF_SIZE = 2048; // Buffer size for reading the output in chunks
var
AProcess : TProcess;
OutputStream : TStream;
BytesRead : longint;
Buffer : array[1..BUF_SIZE] of byte;
ExitCode,
ExitStatus,
I: Integer;
S: String;
F: Text;
begin
ExitCode := -1;
AProcess := TProcess.Create(nil);
try
AProcess.Executable := '/usr/local/bin/egctl';
{$ifDEF Dummy}
AProcess.Parameters.Add('Regleta');
AProcess.Options := [poWaitOnExit, poUsePipes, poStderrToOutPut];
// Don't wait.
{$else}
AProcess.Options := [poWaitOnExit, poUsePipes, poStderrToOutPut];
// Don't wait.
AProcess.Parameters.Add('Regleta');
AProcess.Parameters.Add('off');
AProcess.Parameters.Add('off');
AProcess.Parameters.Add('off');
AProcess.Parameters.Add('off');
{$endIf}
AProcess.Execute;
OutputStream := TMemoryStream.Create;
repeat
BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);
OutputStream.Write(Buffer, BytesRead)
until BytesRead = 0;
ExitCode:= AProcess.ExitStatus;
ExitStatus:= AProcess.ExitStatus
finally
AProcess.Free;
end;
syslog(log_info, 'Called %s, got EC: %d ED: %d'#10, ['egctl',
ExitCode, ExitStatus]);
Writeln('Called egctl, got exitcode ', AProcess.ExitCode, ' and
exitstatus ', AProcess.ExitStatus);
// Or the data can be shown on screen
// Or the data can be shown on screen
with TStringList.Create do
begin
OutputStream.Position := 0; // Required to make sure all data is
copied from the start
LoadFromStream(OutputStream);
writeln('ยทยท ', Text); // It seems to only get stderr, no stdout
writeln('--- Number of lines = ', Count, ' ----');
Free
end;
...
// Clean up
OutputStream.Free;
end;
Watchout, some lines have wrapped. Sorry I'm busy now, can not
generalize the code.
I remember that the AProcess.Options were important: wrong ones and
things failed.
Despite the comment, the code does wait, because it captures the program
output text. It has to wait for all of it.
The equivalent terminal command would be:
/usr/local/bin/egctl Regleta off off off off
or on dummy mode:
/usr/local/bin/egctl Regleta
But the command itself is irrelevant for you. Just easier for me not to
edit it now for posting ;-)
--
Cheers, Carlos.
Back to alt.comp.lang.pascal | Previous | Next — Previous in thread | Next in thread | Find similar
Help - TProcess Shadow <Sh@dow.br> - 2018-08-23 08:14 -0300
Re: Help - TProcess "Carlos E.R." <robin_listas@es.invalid> - 2018-08-23 16:18 +0200
Re: Help - TProcess Shadow <Sh@dow.br> - 2018-08-23 15:10 -0300
Re: Help - TProcess Shadow <Sh@dow.br> - 2018-08-23 22:18 -0300
Re: Help - TProcess "Carlos E.R." <robin_listas@es.invalid> - 2018-11-23 12:57 +0100
Re: Help - TProcess Shadow <Sh@dow.br> - 2018-11-30 22:59 -0200
csiph-web