我打算用一个MFC的控制窗口,控制一个基于DOS的应用程序,但是不知道怎么能够将DOS的应用程序,全部输出到MFC程序。
并且可以接受MFC的一些命令非常感谢

解决方案 »

  1.   

    输入输出重定向
    http://www.sqqt.cn/article.asp?id=6488  这里有个例子可以看看怎样重定向
      

  2.   

    定义两个管道 来重定向cmd的输入和输出。
     HANDLE   hReadPipe,hWritePipe;   
      CString   cmd="command";   
      SECURITY_ATTRIBUTES   sa;   
      SECURITY_DESCRIPTOR   sd;   
        
      LPSECURITY_ATTRIBUTES   lpsa   =   NULL;   
        
      InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);   
      SetSecurityDescriptorDacl(&sd,TRUE,NULL,TRUE);   
      sa.nLength   =   sizeof(SECURITY_ATTRIBUTES);   
      sa.bInheritHandle=TRUE;   
      sa.lpSecurityDescriptor=&sd;   
      lpsa   =   &sa;   
      cmd="cmd";   
        
      sa.nLength=sizeof(SECURITY_ATTRIBUTES);   
      sa.bInheritHandle=TRUE;   
        
      if(!CreatePipe(&hReadPipe,&hWritePipe,&sa,0))   
      return   FALSE;   
        
      STARTUPINFO   si;   
      PROCESS_INFORMATION   ProcInfo;   
      ZeroMemory(&si,sizeof(si));   
      si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;   
      si.wShowWindow=SW_HIDE;   
      si.hStdInput=hReadPipe;   
      si.hStdOutput=si.hStdError=hWritePipe;   
        
      char*   windir=getenv("windir");   
      if(!CreateProcess(NULL,cmd.LockBuffer(),NULL,NULL,1,CREATE_NEW_CONSOLE,NULL,   
      windir,&si,&ProcInfo))   
      return   FALSE;   
      WaitForSingleObject(ProcInfo.hProcess,3000);   
        
      CString   str;   
              assert(hReadPipe);   
              DWORD   BytesRead;   
              char   dest[100];   
              bool   RdLoopDone   =   false;   
              while   (!RdLoopDone)   {   
      static   int   a=0;   
                      memset(dest,   0,   100);   
                      assert(ReadFile(hReadPipe,   &dest,   sizeof(dest),   &BytesRead,   NULL));   
      str+=dest;   
                      if   (BytesRead   <   100)   RdLoopDone   =   true;   
                      if   (a   >   20)   RdLoopDone   =   true;   
      a++;//this   while   loop   seems   infinit   if   without   any   control.   
              }   
              CloseHandle(hReadPipe);   
              CloseHandle(hWritePipe);   
              CloseHandle(ProcInfo.hProcess);   
      AfxMessageBox(str);//a   big   messagebox   with   long   text   appear!!   
              return   TRUE;