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 xe6 android 实现base64字符串的解析

Delphi对access数据库安全加密方法

Delphi android 启动卡死处理方法

Delphi trunc转换函数

Delphi中format与formatdatetime函数详解

Delphi for 循环 to和downto的理解

Delphi 获取系统的硬盘分区及使用信息

Delphi 如何获取桌面图标方法

Delphi版的创建高权限进程

Delphi toscilloscope 仿windows任务管理器cpu使用记录组件

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



打赏

取消

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

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

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

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

评论

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