Given a handle (hStdOut here) to the standard output device, I use the following 2 procedures to write an arbitrary string from a console application:
Excerpt:
procedure Send(const s: string);
var
len: cardinal;
begin
len:=Length(s);
WriteFile(hStdOut,s[1],len,len,nil);
end;
procedure SendLn(const s: string);
begin
Send(s + #13#10);
end;
My trouble:
This statement doesn’t render correctely the string as I expected:
SendLn('The harder they come...');
My Question:
Is there a “WideString” overload of WriteFile or should I consider another Unicode-aware function that access the console screen buffer?
One problem is that you need to specify the length in bytes rather than characters. So use
ByteLengthrather thanLength. At the moment what you are passing inlenis half the byte size of the buffer.I also believe that you should not use the same variable for the
nNumberOfBytesToWriteandlpNumberOfBytesWrittenparameters.The above is fine if your
stdoutis expecting UTF-16 encoded text. If not, and if it is expecting ANSI text then you should switch to AnsiString.Exactly what you need to send to the standard output device depends on what text encoding it is expecting and I don’t know that.
Finally, if this is a console that you are writing to then you should simply use
WriteConsole.