function DosCommand(CommandLine: String): String;
var
cmdBuffer: array [0..MAX_PATH] of char;
Buffer: array [0..4096] of char;
pipeRead, pipeWrite: THandle;
sa: SECURITY_ATTRIBUTES;
si: STARTUPINFO;
pi: PROCESS_INFORMATION;
dwRead: DWORD;
begin
GetEnvironmentVariable('COMSPEC', cmdBuffer, sizeof(cmdBuffer));
CommandLine:= String(cmdBuffer) + ' /C ' + CommandLine;
Result:= '';
sa.nLength:= sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle:= TRUE;
if CreatePipe(pipeRead, pipeWrite, @sa, 0) then
begin
si.cb:= sizeof(STARTUPINFO);
ZeroMemory(@pi, sizeof(PROCESS_INFORMATION));
si.hStdOutput:= pipeWrite;
si.hStdError := pipeWrite;
si.hStdInput := pipeWrite;
si.dwFlags:= STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
si.wShowWindow:= SW_HIDE;
if CreateProcess(nil, PCHAR(CommandLine), nil, nil, TRUE, 0, nil, nil, si, pi) then
begin
CloseHandle(pi.hThread);
if WaitForSingleObject(pi.hProcess, 9000) = WAIT_OBJECT_0 then
begin
dwRead:= 0;
WriteFile(pipeWrite, '', 1, dwRead, 0);
repeat
ZeroMemory(@buffer, sizeof(buffer));
ReadFile(pipeRead, buffer, sizeof(buffer), dwRead, 0);
OemToCharBuffA(buffer, buffer, dwRead);
Result:= Result + #13 + #10 + String(buffer);
until dwRead < sizeof(buffer);
end;
CloseHandle(pi.hProcess);
end;
CloseHandle(pipeRead);
CloseHandle(pipeWrite);
end;
end;