Buenas noches a todos,
Tengo la siguiente función desarrollada (probando en Windows 10) y me devuelve a la VCL (TMemo) el resultado de la salida de comandos como caracteres chinos.
Código Delphi
[-]
function ExecuteCommandCMD(aCommandLine: String; aReturnOutput: Boolean = False): String;
var
objWindowsVersion: TWindowsVersion;
typStartUpInfo: STARTUPINFO;
typSecAttr: SECURITY_ATTRIBUTES;
typSecDescrip: SECURITY_DESCRIPTOR;
typProcInformation: PROCESS_INFORMATION;
objNewStdIn: THandle;
objNewStdOut: THandle;
objReadStdOut: THandle;
objWriteStdIn: THandle;
carExitCode: Cardinal;
caByteRead: Cardinal;
carAvailable: Cardinal;
arrBuffer: array[0..MAX_PATH] of Char;
begin
Result := '';
objWindowsVersion := TWindowsVersion.Create;
try
if objWindowsVersion.IsPlatformNT then
begin
InitializeSecurityDescriptor(@typSecDescrip, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@typSecDescrip, True, nil, False);
typSecAttr.lpSecurityDescriptor := @typSecDescrip;
end
else
typSecAttr.lpSecurityDescriptor := nil;
finally
FreeAndNil(objWindowsVersion);
end;
typSecAttr.nLength := SizeOf(SECURITY_ATTRIBUTES);
typSecAttr.bInheritHandle := True;
if CreatePipe(objNewStdIn, objWriteStdIn, @typSecAttr, 0) then
begin
if CreatePipe(objReadStdOut, objNewStdOut, @typSecAttr, 0) then
begin
GetStartupInfo(typStartUpInfo);
with typStartUpInfo do
begin
dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
wShowWindow := SW_SHOW; hStdOutput := objNewStdOut;
hStdError := objNewStdOut;
hStdInput := objNewStdIn;
end;
Fillchar(arrBuffer, SizeOf(arrBuffer), 0);
GetEnvironmentVariable('COMSPEC', arrBuffer, SizeOf(arrBuffer) - 1);
StrCat(arrBuffer, PChar(' /C ' + aCommandLine));
if CreateProcess(nil, arrBuffer, nil, nil, True, CREATE_NEW_CONSOLE, nil, nil, typStartUpInfo, typProcInformation) then
begin
repeat
PeekNamedPipe(objReadStdOut, @arrBuffer, SizeOf(arrBuffer) - 1, @caByteRead, @carAvailable, nil);
if caByteRead > 0 then
begin
Fillchar(arrBuffer, SizeOf(arrBuffer), 0);
ReadFile(objReadStdOut, arrBuffer, SizeOf(arrBuffer), caByteRead, 0);
if aReturnOutput then Result := Result + #13#10 + String(arrBuffer);
end;
Application.ProcessMessages;
GetExitCodeProcess(typProcInformation.hProcess, carExitCode);
until (carExitCode <> STILL_ACTIVE) and (caByteRead = 0);
end
else
MessageError(ERR_DOS_COMMAND);
CloseHandle(objReadStdOut);
CloseHandle(objNewStdOut);
end;
CloseHandle(objNewStdIn);
CloseHandle(objWriteStdIn);
end;
end;
No entiendo en donde tengo que corregir para que lo que devuelva sea en un idioma legible tal cual lo vemos en la ventana de comandos cunado ejecutamos algo.
Saludos,
El Rayo