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

  • SEARCH
  • Home
  • 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 8715283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:48:33+00:00 2026-06-13T05:48:33+00:00

I use this to read a widestring out of the cmd (Windows Shell). var

  • 0

I use this to read a widestring out of the cmd (Windows Shell).

var
 pBuffer       : array [0..250] of WideChar;
 aBuffer       : array [0..250] of Char;
 RealUnicode   : Integer;
 ExtendedAscii : Integer;
begin
 RealUnicode   := 2;
 ExtendedAscii := 1;
 // ... pipes etc...
 CreateProcessW(nil, pwidechar(ComSpec + ' /U'), nil, nil, TRUE, (CREATE_NEW_CONSOLE or CREATE_BREAKAWAY_FROM_JOB), nil, nil, StartupInfo, ProcessInfo);
 // ...
 while true do begin
  sleep (10); // Reduce CPU Usage
  GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
  if ExitCode <> STILL_ACTIVE then Break;
  FillChar(pBuffer,SizeOf(PBuffer), #0);
  ReadFile(hoRead, pBuffer[0], 250, BytesRead, nil);    
  if BytesRead > 0 then begin
   if (IsTextUnicode(@pBuffer, BytesRead, @RealUNICODE) or IsTextUnicode(@pBuffer, BytesRead, @ExtendedAscii) then begin
    MessagBoxW(0,dbuffer,'',0);
   end else begin
    FillChar    (aBuffer,SizeOf(aBuffer ), #0);
    CopyMemory  (@aBuffer , @pBuffer, BytesRead * 2);
    MessageBoxA (0, aBuffer, '', 0);
   end;
  end;
 end;
end;

This snippet works actually pretty good. It makes sure that if ansi strings/chars get written into the console (for example – ping.exe) that it gets the ANSI output later.
Unfortunately, there is one little glitch. I use ping.exe and it works without problems until it returns to the Unicode part. It’s actually hard to explain but I hope you guys know what I mean.
Thank you for your help.

EDIT:
When ping.exe is finished, the snippet returns empty strings for some reason. Altough the readbytes > 0

EDIT2:
enter image description here

Explanation:
I started the cmd with CreateProcesW, set the pipes etc, and then read the first buffer bytes (in Unicode). Then I typed in ipconfig and it switched back to ANSI. Then it reads bytes and they are empty ANSI strings. After that, the “program” (not cmd) sometimes crashes.

EDIT3:
I have an example here (with sourcecode and binary). It’s compiled with delphi7 and tntcontrols. If you don’t have tntcontrols just put a memo (name : Memo1) in the form. and change the widestrings to strings and/or try to debug it with messageboxW.
http://dl.dropbox.com/u/349314/UNICODE%20Shell%20Example.rar
This example does NOT care about the ansi input!

  • 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-13T05:48:34+00:00Added an answer on June 13, 2026 at 5:48 am

    Console can’t display all Unicode characters correctly. This is because of Console window limitations. The output of console process is in OEM encoding (or DOS encoding), not ANSI.

    You can check it by redirecting the output to a file. Usually, the file would not display correctly in Notepad if it contains non-English characters.

    When you run cmd.exe with /u you can expect that it writes Unicode characters to the pipe. In this case, you should always treat the output as Unicode.


    You create process by calling Wide-version. But it does not mean the started process uses Unicode functions to do the output. Moreover, since Windows 2000 all Ansi-versions of Windows API functions are wrappers around the Wide-versions:

    1. Ansi-function converts all text in input parameters to Wide-strings using the default code page.
    2. Then it calls the Wide-version.
    3. Converts the results, if any, from Wide- to Ansi and returns.

    However, this is not quite true for console windows. Programs usually need to convert their Unicode stings to OEM-encoding (DOS encoding), and then write the result to stdout. Only in this case, it will be displayed correctly by the console window.

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

Sidebar

Related Questions

I use this code to read data from sqlite database: keyFromSql = [NSString stringWithCString:(char
I use this function to read information from xls (2003) file var connectionString =
In my open-source plain C code I use this simple structure to read and
I have to read 100.000 rows from database, I use this code : List<elementClass>
I read this article on how to use keytool to generate CSRs for your
i've read this answer but i don't know how to use that sample in
So basiclly I read this, http://www.gdcl.co.uk/2011/June/UnregisteredFilters.htm . Which tells you how to use filters
I've seen people use many-to-one mappings to represent one-to-one relationships. I've also read this
I use this function for transforming URLs to images on output. function InsertLink(T){ var
I use this function to read file to string function LoadFile(const FileName: TFileName): string;

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.