Recent

Author Topic: TProcess + CreatePipes  (Read 6156 times)

almp1

  • New Member
  • *
  • Posts: 13
TProcess + CreatePipes
« on: March 23, 2017, 11:04:31 pm »
Hello,
I wonder if it is possible to retrieve a string from another application. For example
I have application 1 of console type APP1 which has the following code

Code: Pascal  [Select][+][-]
  1. procedure GetMessage;
  2. var
  3.   AProcess: TProcess;
  4.   OutputLines: TStringList;
  5. begin
  6.   AProcess := TProcess.Create(nil);
  7.   AProcess.Executable := 'APP2.exe';
  8.   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes, poNoConsole];
  9.   AProcess.Execute;
  10.   OutputLines := TStringList.Create;
  11.   OutputLines.LoadFromStream(AProcess.Output);
  12.   write(OutputLines.Text);
  13.   AProcess.Free;
  14. end;
  15.  

I have the application of GUI type APP2 which has the following code ....

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   InPipe : TInputPipeStream;
  4.   OutPipe : TOutputPipeStream;
  5.   s: String;
  6. begin
  7.   InPipe := TInputPipeStream.Create(Handle);
  8.   OutPipe := TOutputPipeStream.Create(Handle);
  9.   s:='my text';
  10.   OutPipe.Write(s[1], Length(s));
  11.   CreatePipeStreams(InPipe, OutPipe);
  12.   Close;
  13.   Application.Terminate;
  14. end;
  15.  

The above codes are compiled and run, but I do not get the value of the second application.

How to retrieve the string generated in APP2 through APP1?

Tanks

André

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: TProcess + CreatePipes
« Reply #1 on: March 23, 2017, 11:45:46 pm »
In the first code snippet, line 8, you don't want the option poWaitOnExit at the same time as poUsePipes.

almp1

  • New Member
  • *
  • Posts: 13
Re: TProcess + CreatePipes
« Reply #2 on: March 24, 2017, 12:45:24 am »
Hi eric

I tried it like that
Code: Pascal  [Select][+][-]
  1.   AProcess.Options := AProcess.Options + [poUsePipes, poNoConsole];
  2.  

 but it did not work out

tks

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: TProcess + CreatePipes
« Reply #3 on: March 24, 2017, 08:21:45 am »
I'm doing something very similar in a program of mine, and I use
Code: [Select]
AProcess.Options := [poUsePipes];
which works fine.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: TProcess + CreatePipes
« Reply #4 on: March 24, 2017, 10:24:44 am »
This scenario is prepared in the runcommand() utils, which uses TProcess internally.

Your examples will fail for larger outputs, or when the application hesitates before it starts giving output. Try

Code: Pascal  [Select][+][-]
  1. if runcommand('app2.exe',[],s) then
  2.    writeln(s):

See also http://wiki.freepascal.org/Executing_External_Programs#.28Process..29RunCommand

The case your above code doesn't prepare for is also described on that page:

http://wiki.freepascal.org/Executing_External_Programs#Reading_large_output

Note that it has a loop to read output.
« Last Edit: March 24, 2017, 10:26:23 am by marcov »

almp1

  • New Member
  • *
  • Posts: 13
Re: TProcess + CreatePipes
« Reply #5 on: March 24, 2017, 08:51:50 pm »
Hi marcov

Even tracing to the Runcommand, I continue without receiving the return.
I believe the problem is in APP1, which is responsible for giving feedback.
Thank you very much for the tip

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: TProcess + CreatePipes
« Reply #6 on: March 24, 2017, 09:07:33 pm »
I believe the problem is in APP1, which is responsible for giving feedback.

Not sure what you're trying to do. Hopefully more than just return a simple string.

Consider other possibilities?

- Put the code you want to run from another app into a dynamic library and call it from the first app.

- (Windows only) Add capability to the called app so it can act like an Automation server and then call from first app (client) via COM. MS Office and other apps can be operated this way under "remote control". Examples:

http://wiki.lazarus.freepascal.org/Office_Automation



Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: TProcess + CreatePipes
« Reply #7 on: March 24, 2017, 09:27:48 pm »
You can only redirect and write/read to/from stdout, stdin and stderr pipes. Unnamed pipes won't work but named ones do.

See  this link for more info : https://msdn.microsoft.com/en-us/library/windows/desktop/aa365590(v=vs.85).aspx

almp1

  • New Member
  • *
  • Posts: 13
Re: TProcess + CreatePipes
« Reply #8 on: March 24, 2017, 10:53:17 pm »
Hi friends !

At the beginning is a change of strings between the applications.

APP1 is a CGI and (console mode).
APP2 is a GUI application. I need this because of LazReport which generates a PDF.

My desire in APP2 is to generate the PDF and return it with a String (Base64) for APP1.

However, I would like to do this without involving external files. In my tests, I realized that it is possible to use APP2 to generate the PDF. Then use APP1 to rescue the PDF written to disk. But I would like this to be done in a direct way
APP1 <---> APP2

Thank you

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: TProcess + CreatePipes
« Reply #9 on: March 24, 2017, 11:10:42 pm »
You could use the TCP/IP stack for that. It would be less hassle than pipes and more cross platform friendly.

APP2 is the server and APP1 is the client which connects to the server and issues needed commands to it.

almp1

  • New Member
  • *
  • Posts: 13
Re: TProcess + CreatePipes
« Reply #10 on: March 24, 2017, 11:19:31 pm »
Hello friends

I will follow the with TCP / IP.

Thank you very much

 

TinyPortal © 2005-2018