delphi 超精简无卡顿调用dos命令输出结果


本文整理自网络,侵删。

 
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 + word = 数据库 + 公文处理

Delphi 缓冲文件流-tbufferedfilestream tfilestream 性能测试

Delphi cxgrid:动态设计统计功能

Delphi 为 service application 添加描述文本的方法

Delphi列表控件tlistview定位到某一行

Delphi程序支持外部参数

Delphi 用拼音首字符检索汉字的源代码

Delphi application.messagebox 详解

Delphi idhttp用法详解

Delphi 捕捉全局异常错误的方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...