Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7773911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:14:46+00:00 2026-06-01T17:14:46+00:00

Given a handle (hStdOut here) to the standard output device , I use the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:14:47+00:00Added an answer on June 1, 2026 at 5:14 pm

    One problem is that you need to specify the length in bytes rather than characters. So use ByteLength rather than Length. At the moment what you are passing in len is half the byte size of the buffer.

    I also believe that you should not use the same variable for the nNumberOfBytesToWrite and lpNumberOfBytesWritten parameters.

    procedure Send(const s: string);
    var
      NumberOfBytesToWrite, NumberOfBytesWritten: DWORD;
    begin
      NumberOfBytesToWrite := ByteLength(s);
      if NumberOfBytesToWrite>0 then
        WriteFile(hStdOut, s[1], NumberOfBytesToWrite, NumberOfBytesWritten, nil);
    end;
    

    The above is fine if your stdout is expecting UTF-16 encoded text. If not, and if it is expecting ANSI text then you should switch to AnsiString.

    procedure Send(const s: AnsiString);
    var
      NumberOfBytesToWrite, NumberOfBytesWritten: DWORD;
    begin
      NumberOfBytesToWrite := ByteLength(s);
      if NumberOfBytesToWrite>0 then
        WriteFile(hStdOut, s[1], NumberOfBytesToWrite, NumberOfBytesWritten, nil);
    end;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following class, what is your opinion on the best way to handle
Given the following basic grammar I want to understand how I can handle comment
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given a HANDLE (or ProcessID) to process 'A' is it possible on Win32 for
In the following, it is said that an I/O handle must be associated with
Given that GDI+ is a wrapper around GDI, how does it handle floating point
I'm writing a module to handle dice rolling. Given x die of y sides,
Is there any Win32 API to check if a given handle belongs to the
How can I enumerate all of the threads in a process given a HANDLE
When a window handle is given, how can i exactly resize a window sending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.