Opening a TStream on stdin/stdout in a Delphi console app
Asked Answered
K

1

30

I'm trying to write a Delphi console application that creates a TStream for its standard input, and another TStream for its standard output.

(It will be launched by a host app with its input and output redirected to pipes, and will be passing binary data to/from that host app, so TStream will be much better-suited to the task than ReadLn/WriteLn.)

How do I go about opening a TStream on standard input or standard output?

Kelbee answered 29/6, 2009 at 21:13 Comment(0)
P
41

Off the top of my head:

  InputStream := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));
  OutputStream := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE));

Give that a go..

Prosperity answered 29/6, 2009 at 22:38 Comment(3)
Indeed it does, very nicely. Thanks!Kelbee
Note that you'll need the Windows unit in your uses clause for the GetStdHandle function.Damselfish
Is there a cross-platform way to do the same?Seine

© 2022 - 2024 — McMap. All rights reserved.