Reputation: 3440
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:
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!
Upvotes: 1
Views: 1953
Reputation: 11878
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:
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.
Upvotes: 1