線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2065
推到 Plurk!
推到 Facebook!

How do I redirect a console program to my own prog

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-12-13 21:56:29 IP:61.218.xxx.xxx 未訂閱

How do I redirect a console program to my own program?.

資料來源: http://marten.hypermart.net/examples/redirect.htm
procedure ReDirect(filename:String);
var StartupInfo:TStartupInfo;
    pi:PROCESS_INFORMATION;
    Buffer,INBuf:array[0..255] of char;
    tmp:String;
    InRead,InWrite,OutRead,OutWrite:THandle;
    Env:array[0..255] of char;
    buf:String;
    BytesRead:longInt;
begin
  if not CreatePipe(InRead,InWrite,nil,0) then                //* Create pipeline
     Exit;         //* Could not create pipe
  if not CreatePipe(OutRead,OutWrite,nil,0) then        //* Create pipeline
     Exit;          //* Could not create pipe      StartupInfo.cb := sizeof(tstartupinfo);                //* Create startup information
  StartupInfo.lpReserved := nil;
  StartupInfo.lpDesktop := nil;
  StartupInfo.lpTitle := nil;
  StartupInfo.dwX := 0;
  StartupInfo.dwY := 0;
  StartupInfo.dwXSize := 0;
  StartupInfo.dwYSize := 0;
  StartupInfo.dwXCountChars := 0;
  StartupInfo.dwYCountChars := 0;
  StartupInfo.dwFillAttribute := 0;
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;        //* To use wShowWindow and the in/output windows
  StartupInfo.cbReserved2 := 0;
  StartupInfo.lpReserved2 := nil;
  StartupInfo.hStdInput := InRead;        //* The input handle
  StartupInfo.hStdOutput := OutWrite;        //* The output handle
  StartupInfo.hStdError := OutWrite;
  StartupInfo.wShowWindow := SW_Hide;        //* to hide the window      FillChar(Env,512,0);
  env := 'mart=test' #0 'Hoi=Hallo' #0 #0;        //* You can pass and environment string      if not CreateProcess(pchar(filename),pchar(tmp),nil,nil,false,0,@Env,nil,StartupInfo,pi) then        //* Start the program
  begin
   CloseHandle( InWrite  );        // The function failed!
   CloseHandle( InRead   );
   CloseHandle( OutWrite );
   CloseHandle( OutRead  );
  end else
  begin
   OutStream := THandleStream.Create(OutRead);        //* It workt, this can also be done with fileread() and filewrite()
   InStream := THandleStream.Create(InWrite);        //* but this works easyer
   InBuf := 'Lets write something to the program!' #13#10;
   InStream.Write(InBuf,SizeOf(InBuf));                //* Send something thru the pipeline to the StdInput       WaitForSingleObject(pi.hProcess,INFINITE);        //* Wait for the process to end (you can read from it before, but now your sure to get everything)       Buf := '';
   while OutStream.Read(Buffer,SizeOf(Buffer)) = SizeOf(Buffer) do        //* Do the reading
   begin
    Buf := Buf   Buffer;
   end;       while pos(#13#10,buf) > 0 do                //* Send the output for example to a Memo-control
   begin
    Memo1.Lines.Add(Copy(buf,1,pos(#13#10,Buf)-1));
    Delete(Buf,1,pos(#13#10,buf) 1);
   end;       CloseHandle(pi.hProcess);                //* Be sure to close the handles!
   CloseHandle(InRead);
   CloseHandle(OutWrite);
   CloseHandle( InWrite );
   CloseHandle( OutRead );
  end;
end;    
範例下載: http://marten.hypermart.net/examples/redirect.zip 聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]---
系統時間:2024-05-06 12:40:07
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!