Delphi 匿名管道


本文整理自网络,侵删。

 

 

function CallCmd(CommandStr: string): string;

const

  ReadBuffer = 4096;

var

  Security: TSecurityAttributes;

  si: TStartUpInfo;

  pi: PROCESS_INFORMATION;

  hRead, hWrite: THandle;

  len: DWORD;

  ExitCode: DWORD;

  Buffer: PAnsiChar;

  hProcess: Integer;

begin

  with Security do

  begin

    nlength := SizeOf(TSecurityAttributes);

    binherithandle := true;

    lpsecuritydescriptor := nil;

  end;

  if not Createpipe(hRead, hWrite, @Security, 0) then

  begin

    ShowMessage('Pipe Create Error 0x01');

  end;

  //GetStartupInfo(si);

  FillChar(si,Sizeof(si), #0);

  with si do

  begin

    cb := SizeOf(si);

    dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;

    wShowWindow := SW_HIDE;

    hStdOutput := hWrite;

    hStdError := hWrite;

  end;

  if not CreateProcess(nil, PWideChar('cmd.exe /c ' + CommandStr), 0, 0, TRUE, 0, 0, 0, si, pi) then

  begin

    ShowMessage('Create Process Error');

  end;

 

  CloseHandle(hWrite);

  Buffer := AllocMem(ReadBuffer + 1);

  while True do

  begin

    PeekNamedPipe(hRead, 0, 0, 0, @len, 0);

    if len <> 0 then

    begin

      ReadFile(hRead, Buffer[0], ReadBuffer, dword(len), nil);

      {out date}

      with Form1.mmo1 do

      begin

        Lines.Text := Lines.Text + Buffer;

        SelStart := Length(Lines.Text);

        SelLength := 0;

      end;

    end

    else

    begin

      //Break;

      GetExitCodeProcess(pi.hProcess, ExitCode);

      if ExitCode <> STILL_ACTIVE then

      begin

        CloseHandle(hRead);

        Break;

      end;

    end;

  end;

end;

 

https://www.cnblogs.com/YiShen/p/9750507.html

相关阅读 >>

Delphi xe 制作的萤光时钟,可改作屏保

Delphi xe firemonkey的stylebook皮肤控件的使用

Delphi 采集功能代码getstr

Delphi 自动升级组件autoupgrader简单教程

Delphi webbroker iis cgi 的配置

Delphi 判断图像格式bmp jpg gif pcx png psd ras sgi tiff err

Delphi 读取文本文件的最后一行

Delphi 获取安卓手机wifi信息(xe8)

Delphi常用获取系统信息的函数单元

汇编数据宽度和字节序

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



打赏

取消

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

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

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

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

评论

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