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 android platform uses-permission[2] androidmanifest.xml 配置

Delphi 比较俩组ip地址是否一样

Delphi 文本列数值排序从小到到

Delphi 让控制台窗口不现实出来

Delphi中使用自定义字体

Delphi windows 编程[11] - wm_size 消息

Delphi treeview添加背景图片

Delphi 文件夹拷贝子文件及文件夹

Delphi在目录中搜索文件

Delphi xe android platform uses-permission[1] 权限列表

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



打赏

取消

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

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

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

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

评论

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