Recent

Author Topic: [Solved] How is the pipeline transferred?  (Read 3170 times)

loaded

  • Hero Member
  • *****
  • Posts: 578
[Solved] How is the pipeline transferred?
« on: January 28, 2022, 02:26:03 pm »
Hi All,
How can we import the pipeline used in the command line in windows not into a file but into a memo?
« Last Edit: January 29, 2022, 07:45:28 am by loaded »
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

paweld

  • Sr. Member
  • ****
  • Posts: 385
Re: How is the pipeline transferred?
« Reply #1 on: January 28, 2022, 03:13:58 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   BufSize = 256;
  4. var
  5.   cmdprocess: TProcessUTF8;
  6.   ms: TMemoryStream;
  7.   NumBytes, BytesRead: Longint;
  8. begin
  9.   BytesRead := 0;
  10.   NumBytes := 0;
  11.   ms := TMemoryStream.Create;
  12.   cmdprocess := TProcessUTF8.Create(nil);
  13.   cmdprocess.Options := [poUsePipes, poNoConsole];
  14.   cmdprocess.Executable := 'cmd';
  15.   cmdprocess.Parameters.Add('/c');
  16.   cmdprocess.Parameters.Add('dir');
  17.   cmdprocess.Execute;
  18.   while cmdprocess.Running do
  19.   begin
  20.     ms.SetSize(BytesRead + BufSize);
  21.     NumBytes := cmdprocess.Output.Read((ms.Memory + BytesRead)^, BufSize);
  22.     if NumBytes > 0 then
  23.       Inc(BytesRead, NumBytes)
  24.     else
  25.       break;
  26.   end;
  27.   ms.SetSize(BytesRead);
  28.   memo1.Lines.LoadFromStream(ms);
  29.   ms.Free;
  30.   cmdprocess.Free;
  31. end;      
More info: https://wiki.freepascal.org/Executing_External_Programs#Reading_large_output
You may also use: https://github.com/t-edson/UnTerminal
Best regards
paweld
---
Lazarus trunk / FPC stable

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How is the pipeline transferred?
« Reply #2 on: January 28, 2022, 03:21:38 pm »
The command is:

Dir |yourApp

Simply check StdInputHandle in your app.
When used as above, StdInputHandle is not going to be 0.

To read the data, use Windows API

Or

use THandleStream.

Or

Assign the handle to a text variable, and use ReadLn

Edit:
Sample code:
Code: Pascal  [Select][+][-]
  1. var
  2.   s:string;
  3.   hs: THandleStream;
  4. begin
  5.   if StdInputHandle>0 then
  6.   begin
  7.     hs:=THandleStream.Create(StdInputHandle);
  8.     SetLength(s,hs.Size);
  9.     hs.Read(s[1],hs.Size);
  10.     memo1.Text:=s;
  11.  
« Last Edit: January 28, 2022, 03:28:11 pm by engkin »

MarkMLl

  • Hero Member
  • *****
  • Posts: 4402
Re: How is the pipeline transferred?
« Reply #3 on: January 28, 2022, 03:24:12 pm »
The easiest way is to read what's available from Input (i.e. the preopened text device). I've done that in the past but would need to check the detail.

The worst bit is getting your program to decide whether anything's actually being piped to it, or if it should e.g. rely on the standard dialog(u)e etc. to open a file.

MarkMLl


MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

loaded

  • Hero Member
  • *****
  • Posts: 578
Re: How is the pipeline transferred?
« Reply #4 on: January 28, 2022, 06:08:06 pm »
Thank you very much paweld and engkin and MarkMLl each for your replies.
Inspired by the code and ideas you sent, I did a trial run. It works fine for the dos environment.
I don't know if you have used it before!!! but not working with Ghostscript app !!!
Is there anything else I can do about this? I would be glad if you can help. Respects.
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How is the pipeline transferred?
« Reply #5 on: January 28, 2022, 06:42:01 pm »
Use the command line version.

I used GhostScript a few years ago, specifically I used the DLL.

Edit:
The command line version ends with the letter c.
« Last Edit: January 28, 2022, 06:55:25 pm by engkin »

loaded

  • Hero Member
  • *****
  • Posts: 578
Re: How is the pipeline transferred?
« Reply #6 on: January 28, 2022, 07:30:21 pm »
The command line version ends with the letter c.
Perfect spot, thank you very much engkin
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

ASBzone

  • Hero Member
  • *****
  • Posts: 633
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: How is the pipeline transferred?
« Reply #7 on: February 07, 2022, 10:37:24 pm »
When used as above, StdInputHandle is not going to be 0.

The problem is that StdInputHandle is often not 0, even when no piping is taking place.

Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3.  
  4. program OpenPipe;
  5.  
  6. begin
  7.   WriteLn('Standard Input Handle Value is: ', StdInputHandle);
  8. end.
  9.  

Try this in Windows with the following commands:

MyExe.exe
DIR C:\ | MyExe.exe
-ASB: https://www.BrainWaveCC.com/

OLD: Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055
NEW: Lazarus v2_2_0-82-g6aebd4d4ae / FPC v3.2.3-587-g65733801e5
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

ASBzone

  • Hero Member
  • *****
  • Posts: 633
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: [Solved] How is the pipeline transferred?
« Reply #8 on: February 07, 2022, 10:59:07 pm »
-ASB: https://www.BrainWaveCC.com/

OLD: Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055
NEW: Lazarus v2_2_0-82-g6aebd4d4ae / FPC v3.2.3-587-g65733801e5
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How is the pipeline transferred?
« Reply #9 on: February 08, 2022, 12:20:05 am »
When used as above, StdInputHandle is not going to be 0.

The problem is that StdInputHandle is often not 0, even when no piping is taking place.

Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3.  
  4. program OpenPipe;
  5.  
  6. begin
  7.   WriteLn('Standard Input Handle Value is: ', StdInputHandle);
  8. end.
  9.  

Try this in Windows with the following commands:

MyExe.exe
DIR C:\ | MyExe.exe


Did you try with GUI apps?

ASBzone

  • Hero Member
  • *****
  • Posts: 633
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: How is the pipeline transferred?
« Reply #10 on: February 08, 2022, 04:42:15 am »
-ASB: https://www.BrainWaveCC.com/

OLD: Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055
NEW: Lazarus v2_2_0-82-g6aebd4d4ae / FPC v3.2.3-587-g65733801e5
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

jcmontherock

  • Full Member
  • ***
  • Posts: 126
Re: [Solved] How is the pipeline transferred?
« Reply #11 on: February 08, 2022, 10:51:11 am »
What I am using:
Code: Pascal  [Select][+][-]
  1. var
  2.   StringArray: TStringArray;
  3.   Line:         AnsiString;
  4.   FileStream:   TFileStream;
  5.   rc:           Boolean;
  6. ....
  7.   StringArray := TStringArray.Create('/c', 'dir');
  8.   rc := RunCommand('cmd.exe', StringArray, Line, [poStderrToOutPut]);
  9.   if rc = False then WriteLn('Some error has been happend !...' + sLineBreak);
  10.   WriteLn('Command: ' + Line);
  11.  
  12.   Memo1.Text := Line;
  13.  
  14.   FileStream := TFileStream.Create('output.txt', fmCreate);
  15.   FileStream.Write(Line[1], Length(Line));
  16.   FileStream.Free;
  17.  

 

TinyPortal © 2005-2018