如何从 Delphi 控制台应用程序写入标准输出?
这是我尝试过的。我根据我能找到的信息操纵了这个简单的测试应用程序,从磁盘读取文件并将其输出到控制台标准输出:
program ConsoleOut;
{$APPTYPE CONSOLE}
uses
Classes, Windows, SysUtils;
var
S: TMemoryStream;
OutputStream: THandleStream;
ss: string;
Buffer: PByte;
i: Integer;
begin
S := TMemoryStream.Create;
S.LoadFromFile('1.jpg');
S.Seek(0, soFromBeginning);
//Am I right that from now on writing to OutputStream will write to stdout?
OutputStream := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE));
GetMem(Buffer, S.Size);
S.ReadBuffer(Buffer^, S.Size);
i := OutputStream.Write(Buffer^, S.Size); //i = 0 here for some reason
FreeMem(Buffer, S.Size);
Writeln(i, ' byte written to output');
Readln(ss); //Don't exit app to read previous line
S.Free;
end.
但由于某种原因它失败了。您能否指导我以正确的方式写入标准输出?