如何用命名管道实现CreatePipe类似功能?

baidu一下有很多啊

下面是我参考网上程序写的
delphi的
希望能帮到你

{执行命令行程序------------------------------}

 
procedure commond_ex();
const  num_read = 256;
var
          hReadPipe,hWritePipe:THandle;
          si:STARTUPINFO;
          lsa:SECURITY_ATTRIBUTES;
          pi:PROCESS_INformATION;

          cchReadBuffer:DWORD;

          ph:PChar;
          fname:PChar;
          Apprunning   :   Dword;

          Label out;
begin
En_dis_butt(False);

          fname:=allocmem(255);
          ph:=AllocMem(5000);
          lsa.nLength  :=sizeof(SECURITY_ATTRIBUTES);
          lsa.lpSecurityDescriptor  :=nil;
          lsa.bInheritHandle  :=True;

          if  CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false  then
            begin
            Displaymassage('Can  not  create  pipe!');
            Goto out;
            end;
          fillchar(si,sizeof(STARTUPINFO),0);
          si.cb  :=sizeof(STARTUPINFO);
          GetStartupInfo(&si);
          si.dwFlags  :=(STARTF_USESTDHANDLES  or  STARTF_USESHOWWINDOW);
          si.wShowWindow  :=SW_HIDE;   //隐藏dos窗口
          //si.hStdInput := hReadPipe;
          si.hStdOutput  :=hWritePipe; //重设输出接口
          si.hStdError := hWritePipe;
          StrPCopy(fname,tmp_comm);  
          //创建子进程
          if  CreateProcess(  nil,  fname,  nil,  nil,  true, NORMAL_PRIORITY_CLASS,  nil,  nil,  &si,  &pi)  =  False            then
            begin
              Displaymassage('can  not  create  process');
              FreeMem(ph);
              FreeMem(fname);
              Goto out;
           end;
          CloseHandle(hWritePipe);

          sleep(10);

          Form1.Timer1.Enabled := True; //开启定时器,显示进度

          //等待执行完毕
          repeat
            Apprunning   :=   WaitForSingleObject(pi.hProcess,100);
            Application.ProcessMessages;
          until   (Apprunning   <>   WAIT_TIMEOUT);

          Form1.Timer1.Enabled := False;

          tmp_content :='';
          //读取dos窗口输出
          while True do
            begin
            if  not ReadFile(hReadPipe,ph[0],num_read,cchReadBuffer,nil) then break;
            ph[cchReadbuffer]:=#0;
            //OemToChar(ph, ph);
            tmp_content := tmp_content +ph;
            end;
           
          CloseHandle(hReadPipe);
          CloseHandle(pi.hThread);
          CloseHandle(pi.hProcess);
          //CloseHandle(hWritePipe);
          FreeMem(ph);
          FreeMem(fname);

 out:
 En_dis_butt(True);
 Form1.Timer1.Enabled := False;

 Displaymassage(tmp_content);

end;

转自:http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=1273737

posted @ 2012-01-27 10:01  stma  阅读(572)  评论(0)    收藏  举报