console - How to write binary data into stdout in Delphi? -
How do I write from the Delphi console application to stdout?
Here's what I've tried I've processed this simple test app as infos, I can read a file from the disk and can output it to the stdout console:
Program console auto; {$ APPTYPE CONSOLE} uses classes, Windows, SUUSUtils; Var S: TMemoryStream; OutputStream: Thandalstream; SS: string; Buffer: pibet; I: integer; Start S: = TMemoryStream.Create; S.LoadFromFile ('1.jpg'); s. Shake (0, Sophrambegging); // Am I right that the output stream will now be written to stdout? OutputStream: = Thunderstream. Create (GetStdHandle (STD_OUTPUT_HANDLE)); GetMem (Buffer, S. Size); Eseridbuffer (buffer, S. size); I: = Outputstream. Write (buffer ^, S. size); // i = 0 here for some reasons (buffer, s. Size); Written (I, 'Byte Written on Output'); Readln (SS); // Do not leave the app to read the previous line S.Free; End. But for some reason it fails, can you direct me to write standout?
Your approach is sound However, if stdout is connected to the console, your code fails . Error code ERROR_NOT_ENOUGH_MEMORY :
There is not enough storage available to process this order after writing the stream. If you redirect stdout to a file, your code will work properly. And of course you really do not want to migrate binary data into the console. It's going to keep unreadable content on the console and the computer is beeped by the beep!
If you have to output to the console, then how big is the console device buffer and it must be typed in the right size portion, I have to admit that I am not sure how to do this You can use trial and error, but this does not appeal to me. Perhaps there is a way to inquire with the console to know the information.
Considering the documentation, it seems that 64K is the upper border and in fact if I write the blank space in your handle stream, then I can write about 64K at a time though, if I use raw jpeg binary I write the data, so it first tells. So I think that is also part of the problem ??? Do not dump JPEG to console.
Another comment Since you read the contents of the file in the memory stream, there is no need to allocate intermediate buffer. You s Memory ^ can write directly.
Comments
Post a Comment