两个都有源代码,控制台是个长时间的循环,并且不关闭,不断有信息输出.这个信息如何让delphi程序接受------重谢

解决方案 »

  1.   

    >>输出重定向。。控制台是不结束的一个长时间的..
      

  2.   

    socket,消息,回调函数都可以啊.
      

  3.   

    控制台的窗口已经是Console 好像没有句柄的 不能用消息吧如果不行的话 就改成SDK的吧。。
      

  4.   

    這個可以實現你要的, 看下:
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=199752管道的例子:  
    unit  Unit1;  
     
    interface  
     
    uses  
       Windows,  Messages,  SysUtils,  Variants,  Classes,  Graphics,  Controls,  Forms,  
       Dialogs,  StdCtrls;  
     
    type  
       TPipeForm  =  class(TForm)  
           Editor:  TMemo;  
           procedure  EditorKeyPress(Sender:  TObject;  var  Key:  Char);  
           procedure  FormDestroy(Sender:  TObject);  
           procedure  FormCreate(Sender:  TObject);  
           procedure  EditorKeyDown(Sender:  TObject;  var  Key:  Word;  
               Shift:  TShiftState);  
           procedure  EditorMouseDown(Sender:  TObject;  Button:  TMouseButton;  
               Shift:  TShiftState;  X,  Y:  Integer);  
       private  
           {  Private  declarations  }  
           CreateOk:  Boolean;  
                           WPos:  TPoint;  
           hReadPipe,  hWritePipe,  hWriteFile,  hReadFile:  THandle;  
           processinfo:  PROCESS_INFORMATION;  
           procedure  SendCmdToShell(Const  CmdStr:  String);  
           function  GetCmdStr:  string;  
       public  
           {  Public  declarations  }  
       end;  
     
    var  
       PipeForm:  TPipeForm;  
     
    implementation  
     
    {$R  *.dfm}  
     
    procedure  TPipeForm.EditorKeyPress(Sender:  TObject;  var  Key:  Char);  
    var  
       ECharPos:  TPoint;  
    begin  
               if  Key  =  Chr(VK_RETURN)  then  
       begin  
    //        ShowMessage(GetCmdStr);  
                           SendCmdToShell(GetCmdStr);  
       end  else  if  Key  =  Chr(VK_BACK)  then  
       begin  
                   ECharPos  :=  Editor.CaretPos;  
                   if  ECharPos.X  =  WPos.X  +  1  then  
                       Key  :=  #0;  
       end;  
    end;  
     
    procedure  TPipeForm.SendCmdToShell(Const  CmdStr:  String);  
    var  
               ShellCmdStr:  array[0..256]  of  char;  
       RBuffer:  array[0..25000]  of  char;  
       nByteToWrite:  DWORD;  
       nByteWritten:  DWORD;  
       nByteReaden:  DWORD;  
    begin  
               if  CreateOK  then  
       begin  
           StrPCopy(ShellCmdStr,  CmdStr);  
           nByteToWrite  :=  StrLen(ShellCmdStr);  
           ShellCmdStr[nByteToWrite]  :=  #13;  
           ShellCmdStr[nByteToWrite+1]  :=  #10;  
                           ShellCmdStr[nByteToWrite+2]  :=  #0;  
           Inc(nByteToWrite,  2);  
           WriteFile(hWriteFile,  ShellCmdStr,  nByteToWrite,  nByteWritten,  nil);  
           Sleep(400);  
           Editor.Lines.Clear;  
           FillChar(RBuffer,  Sizeof(RBuffer),  #0);  
           ReadFile(hReadFile,  RBuffer,  25000,  nByteReaden,  nil);  
           Editor.Lines.Add(StrPas(RBuffer));  
           WPos.Y  :=  Editor.Lines.Count-1;  
           WPos.X  :=  Length(Editor.Lines[WPos.Y])-1;  
       end;  
    end;  
     
    procedure  TPipeForm.FormDestroy(Sender:  TObject);  
    var  
               shellexitcode:  Cardinal;  
    begin  
               if  GetExitCodeProcess(processinfo.hProcess,  shellexitcode)  then  
       begin  
                   if  shellexitcode  =  STILL_ACTIVE  then  
                                       TerminateProcess(processinfo.hProcess,  0);  
       end;  
       if  hWriteFile  <>  0  then  
                   CloseHandle(hWriteFile);  
       if  hReadFile  <>  0  then  
                   CloseHandle(hReadFile);  
    end;  
     
    procedure  TPipeForm.FormCreate(Sender:  TObject);  
    var  
               Pipeattr:  SECURITY_ATTRIBUTES;  
       ShellStartInfo:  STARTUPINFO;  
       shellstr:  array  [0..256]  of  char;  
       RBuffer:  array[0..25000]  of  char;  
       I:  Integer;  
       nByteReaden:  DWORD;  
    begin  
               CreateOK  :=  False;  
       I  :=  0;  
       Editor.ReadOnly  :=  False;  
       Wpos.X  :=  0;  
       WPos.Y  :=  0;  
       with  Pipeattr  do  
       begin  
                           nLength  :=  Sizeof(SECURITY_ATTRIBUTES);  
                           lpSecurityDescriptor  :=  nil;  
                           bInheritHandle  :=  true;  
       end;  
     
               if  CreatePipe(hReadPipe,  hWriteFile,  @Pipeattr,  0)  then  
                   Inc(i);  
               if  CreatePipe(hReadFile,  hWritePipe,  @pipeattr,  0)  then  
                   Inc(i);  
     
       GetStartupInfo(ShellStartInfo);  
       with  ShellStartInfo  do  
       begin  
           dwFlags  :=  STARTF_USESHOWWINDOW  or  STARTF_USESTDHANDLES;  
           hStdInput  :=  hReadPipe;  
           hStdError  :=  hWritePipe;  
           hStdOutput  :=  hWritePipe;  
           wShowWindow  :=  SW_HIDE;  
       end;  
               GetSystemDirectory(@Shellstr,  MAX_PATH+1);  
       StrCat(@ShellStr,  Pchar('\\cmd.exe'));  
       if  CreateProcess(Shellstr,  nil,  nil,  nil,  True,  0,  
                                                                                       nil,  nil,  ShellStartInfo,  processinfo)  then  
               begin  
           Inc(i);  
       end  else  begin  
                   MessageBox(Handle,  Pchar('调用Shell错误!'),  Pchar('错误'),  (MB_OK  or  MB_ICONERROR));  
               end;  
       if  i  =  3  then  
       begin  
                   CreateOK  :=  True;  
           Editor.Lines.Clear;  
           sleep(250);  
                           ReadFile(hReadFile,  RBuffer,  25000,  nByteReaden,  nil);  
                   Editor.Lines.Add(StrPas(RBuffer));  
                           WPos.Y  :=  Editor.Lines.Count-1;  
           WPos.X  :=  Length(Editor.Lines[WPos.Y])-1;  
       end;  
    end;  
     
    procedure  TPipeForm.EditorKeyDown(Sender:  TObject;  var  Key:  Word;  
       Shift:  TShiftState);  
    var  
       ECharPos:  TPoint;  
    begin  
       ECharPos  :=  Editor.CaretPos;  
       if  ECharPos.Y  >  WPos.Y  then  
                   Editor.ReadOnly  :=  False  
       else  if  (ECharPos.Y  =  WPos.Y)  and  (ECharPos.X  >  WPos.X)  then  
       begin  
                   Editor.ReadOnly  :=  False;  
       end  else  
                   Editor.ReadOnly  :=  True;  
    end;  
     
    function  TPipeForm.GetCmdStr:  string;  
    var  
               LastLine:  Integer;  
    begin  
               LastLine  :=  Editor.Lines.Count  -  1;  
       if  LastLine  >  WPos.Y  then  
       begin  
                           result  :=  Editor.Lines[LastLine];  
       end  else  if  LastLine  =  WPos.Y  then  
       begin  
                   result  :=  Editor.Lines[LastLine];  
           result  :=  Copy(result,  WPos.X+2,  Length(result));  
       end  else  
       begin  
                   result  :=  '  ';  
       end;  
    end;  
     
    procedure  TPipeForm.EditorMouseDown(Sender:  TObject;  Button:  TMouseButton;  
       Shift:  TShiftState;  X,  Y:  Integer);  
    var  
       ECharPos:  TPoint;  
    begin  
       ECharPos  :=  Editor.CaretPos;  
       if  ECharPos.Y  >  WPos.Y  then  
                   Editor.ReadOnly  :=  False  
       else  if  (ECharPos.Y  =  WPos.Y)  and  (ECharPos.X  >  WPos.X)  then  
                   Editor.ReadOnly  :=  False  
       else  
                   Editor.ReadOnly  :=  True;  
    end;  
     
    end.
      

  5.   

    arr老大,关键是控制台程序不结束,而且要接受管道不可以啊,下面的命令也会死掉Microsoft Windows 2000 [Version 5.00.2195]
    (C) 版权所有 1985-2000 Microsoft Corp.C:\Documents and Settings\Administrator>copy con c:\a.txt
    123^Z
    改写 c:\a.txt 吗? (Yes/No/All): y
    已复制         1 个文件。C:\Documents and Settings\Administrator>
      

  6.   

    >>arr老大,关键是控制台程序不结束,而且要接受
    我上面的代碼,  只要你的delphi 不退出, 就沒問題啊!或者不執行 if  shellexitcode  =  STILL_ACTIVE  then  
                                       TerminateProcess(processinfo.hProcess,  
    就不會終于!
      

  7.   

    >>关键是控制台程序不结束,而且要接受
    我覺得上面我貼的代碼, 應該可以的
      

  8.   

    to arr:
    在这之前我已经试过pipe了
    这段代码虽然好,仍然没有帮上我的忙,可能是控制台代码中有WaitForSingleObject函数,导致无法响应,不过还是谢了另外snmp已经用delphi搞定了,这个问题先不去研究了,揭帖