procedure CaptureConsoleOutput(const ACommand, AParameters: String; AMemo: TMemo);
    const
    CReadBuffer = 2400;
    var
    saSecurity: TSecurityAttributes;
    hRead: THandle;
    hWrite: THandle;
    suiStartup: TStartupInfo;
    piProcess: TProcessInformation;
    pBuffer: array[0..CReadBuffer] of Char;
    dRead: DWord;
    dRunning: DWord;
    inS: THandleStream;
    sRet: TStrings;
    begin
    saSecurity.nLength := SizeOf(TSecurityAttributes);
    saSecurity.bInheritHandle := True;
    saSecurity.lpSecurityDescriptor := nil;
    if CreatePipe(hRead, hWrite, @saSecurity, 0) then
    begin
    FillChar(suiStartup, SizeOf(TStartupInfo), #0);
    suiStartup.cb := SizeOf(TStartupInfo);
    suiStartup.hStdInput := hRead;
    suiStartup.hStdOutput := hWrite;
    suiStartup.hStdError := hWrite;
    suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
    suiStartup.wShowWindow := SW_HIDE;
    if CreateProcess(nil, PChar(ACommand + ‘ ‘ + AParameters), @saSecurity,
    @saSecurity, True, NORMAL_PRIORITY_CLASS, nil, nil, suiStartup, piProcess)
    then
    begin
    repeat
    dRunning := WaitForSingleObject(piProcess.hProcess, 100);
    Application.ProcessMessages();
    repeat
    //
    inS := THandleStream.Create(hRead);
    if inS.Size > 0 then
    begin
    sRet := TStringList.Create;
    sRet.LoadFromStream(inS);
    AMemo.Lines.Add(sRet.Text);
    sRet.Free;
    end;
    inS.Free;
    // www.delphitop.com
    until (dRead < CReadBuffer);
    until (dRunning <> WAIT_TIMEOUT);
    CloseHandle(piProcess.hProcess);
    CloseHandle(piProcess.hThread);
    end;
    CloseHandle(hRead);
    CloseHandle(hWrite);
    end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    CaptureConsoleOutput(‘cmd.exe /c ‘,’ping www.baidu.com’,memo1);
    end;
    delphi调用dos命令
    博客分类:
    DELPHI随笔

    unit RunDos;
    interface
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;
    Type
    TRunDos=class(TObject)
    public
    procedure CheckResult(b:Boolean);
    function RunDOS(CommandLine: String): String;
    end;
    implementation
    { TRunDos }
    procedure TRunDos.CheckResult(b: Boolean);
    begin
    if not b then
    Raise Exception.Create(SysErrorMessage(GetLastError));
    end;
    function TRunDos.RunDOS(CommandLine: String): String;
    var
    HRead,HWrite:THandle;
    StartInfo:TStartupInfo;
    ProceInfo:TProcessInformation;
    b:Boolean;
    sa:TSecurityAttributes;
    inS:THandleStream;
    sRet:TStrings;
    begin
    Result := ‘’;
    FillChar(sa,sizeof(sa),0);
    //设置允许继承,否则在NT和2000下无法取得输出结果
    sa.nLength := sizeof(sa);
    sa.bInheritHandle := True;
    sa.lpSecurityDescriptor := nil;
    b := CreatePipe(HRead,HWrite,@sa,0);
    CheckResult(b);
    FillChar(StartInfo,SizeOf(StartInfo),0);
    StartInfo.cb := SizeOf(StartInfo);
    StartInfo.wShowWindow := SW_HIDE;
    //使用指定的句柄作为标准输入输出的文件句柄,使用指定的显示方式
    StartInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
    StartInfo.hStdError := HWrite;
    StartInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE);//HRead;
    StartInfo.hStdOutput := HWrite;
    b := CreateProcess(nil,
    PChar(CommandLine),
    nil,
    nil,
    True,
    CREATE_NEW_CONSOLE,
    nil,
    nil,
    StartInfo,
    ProceInfo );

    CheckResult(b);
    WaitForSingleObject(ProceInfo.hProcess,INFINITE);
    inS := THandleStream.Create(HRead);
    if inS.Size>0 then
    begin
    sRet := TStringList.Create;
    sRet.LoadFromStream(inS);
    showmessage(sRet.Text);
    Result := sRet.Text;
    sRet.Free;
    end;
    inS.Free;

    CloseHandle(HRead);
    CloseHandle(HWrite);
    end;
    end.