Redmon & capture stdin with Delphi



我正在使用redmon将postscript重定向到delphi进行处理。

我使用以下代码将stdin读取到文件中:

var
  Stdin: THandleStream;
  FStream: TFileStream;
  BytesRead:Int64;
  Buffer: array[0..1023] of Byte; 
StdIn := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));
try
  tempps:=GetTempFile('.ps');
  FStream:=tfilestream.Create(tempps,fmCreate or fmOpenReadWrite);
  StdIn.Seek(0,0);
  try
    repeat
      BytesRead:=StdIn.Read(Buffer,1024);
      FStream.Write(Buffer,BytesRead);
    until bytesread<SizeOf(Buffer);
  finally
    InputSize:=FStream.Size;
    FStream.Free;
  end;
finally
  StdIn.Free;
end;

这适用于大多数情况,除了redmon日志文件显示的情况:

REDMON WritePort: OK  count=65536 written=65536
REDMON WritePort: Process not running. Returning TRUE.
    Ignoring 65536 bytes

是65536只是一个转移注意力的事实,是我没有正确阅读标准,还是我忽略了一些奇怪的限制?

提前感谢。

<标题>编辑1

65536是一个红鲱鱼- redmon在日志中每64k打印此消息,整个文件是688759字节,然而看起来redmon提前关闭输出,但随后仍然继续输出更多的文本。

我不知道RedMon是如何工作的,但我不会依赖bytesread<SizeOf(Buffer)作为EOF条件,因为我认为你实际上是从管道中读取的,而MSDN文档所说的ReadFile函数可以返回读取的字节数小于读取的字节数,如果你从管道中读取。

BytesRead <= 0条件更可靠(只有当RedMon在管道的另一边写0字节时才会失败,我想它不应该这样做):

repeat
  BytesRead:=StdIn.Read(Buffer,1024);
  if BytesRead > 0 then
    FStream.WriteBuffer(Buffer,BytesRead);
until BytesRead <= 0;

最新更新