Read the STDIN program in Delphi

I have the following batch of script:

dir | myapp.exe 

And the program has this source (more or less):

 procedure TForm1.FormCreate(Sender: TObject); var buff: String; begin Read(buff); Memo1.Lines.Text:=buff; end; 

And the conclusion in the note:

The volume on drive C has no label.

I tried:

  • put the read part in a loop with eof as a condition - somehow causing an endless loop
  • write cycle to read before strlen(buff) is 0 - for some reason it goes out a second time
  • Reading in just 0.5 seconds (I was thinking about asynchronous writing to stdin), this also failed

By the way, starting the program directly, without stdin data, causes the EInputOutput (I / O Error) error code.

+7
source share
2 answers

GUI applications do not have automatically assigned stdin, stdout, or stderr. You can do something like:

 procedure TForm1.FormCreate(Sender: TObject); var Buffer: array[0..1000] of Byte; StdIn: TStream; Count: Integer; begin StdIn := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE)); Count := StdIn.Read(Buffer, 1000); StdIn.Free; ShowMessageFmt('%d', [Count]); end; 

If you do

 dir *.pas | myapp.exe 

You will see a message with a number> 0, and if you do:

 myapp.exe 

You will see message number 0. In both cases, a form will be displayed.

+10
source

try using a threaded approach instead of Read(buff)

 InputStream := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE)); 
+3
source

All Articles