Re[6]: Перенаправление ввода-вывода консоли
От: ua1zcl Россия www.alexklm.ru
Дата: 21.05.04 13:00
Оценка:
Здравствуйте, Oxy, Вы писали:

Oxy>А я работаю под вынь2к. И вообще я хотел создать редирект который бы работал под любой из ОС Win9x/ME/2k/XP.

Oxy>Может кто то знает другие варианты? Помогите, плиз!
Листая поиск по этой теме, я нашёл этот поток. Я тоже упёрся в проблему вывода в Win2k/XP (Win9x/ME — работает).
Прошло два года, может что-нибудь изменилось? Меня интереует код на API.
Привожу отрезок кода



static bool RunProcess(PVOID pv)
{
 if(!pRcvBuf)pRcvBuf= new char[100];
 if(!pTsmBuf)pTsmBuf= new char[100];
 bool fSuccess;
 int iFailures=0;
 
 if(ptl) 
 { 
  sprintf(str,"Running %s   '%s' { %s} for {%s}\r\n",
             ptl->szTlType[0]?ptl->szTlType:"CommandLine tool",
                 ptl->szTlAlias[0]?ptl->szTlAlias:"",
                 pszThisToolFPath,pszTargetFile);
 }
 else sprintf(str,"Running...  {tool: %s} for {%s}\r\n",pszThisToolFPath,pszTargetFile);
 Lm(str);

 dwNbytesRead=0; dwReadSoFar=0; dwWritn=0;
 DWORD dwRes;
 DWORD dwRead=1;
 bool bRead=1,bRes=0;
 SECURITY_ATTRIBUTES sa;

 if(sg.uOsVersion==W2000 || sg.uOsVersion==WXP || sg.uOsVersion==WNT4)
 {
  SECURITY_DESCRIPTOR sd={0};
  SECURITY_DESCRIPTOR_CONTROL sc={0};
  DWORD dwRevision=1;
  BOOL b=GetSecurityDescriptorControl(&sd,&sc,&dwRevision);
  //if(!b)Gle();
 }
 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
 sa.lpSecurityDescriptor = NULL;
 sa.bInheritHandle = TRUE;

 hChildsInpPipe=0; hMyOutPipe=0; hChildsOutPipe=0; hMyReadPipe=0;
 OVERLAPPED ov={0};
 memset(&pi,0,sizeof pi);
 STARTUPINFO si={0};
 BOOL bProc;
 HANDLE hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);
 HANDLE hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);
 HANDLE hCurProc=GetCurrentProcess();

 memset(&si, 0, sizeof(STARTUPINFO));

 if(!CreatePipe(&hMyReadPipe, &hChildsOutPipe, &sa,0x10000)){ goto end;}
 if(!CreatePipe(&hChildsInpPipe, &hMyOutPipe, &sa,0x10000)){ goto end;}

  // Set a write handle to the pipe to be STDOUT.
 if(!SetStdHandle(STD_OUTPUT_HANDLE, hChildsOutPipe))
  { Mbox("Redirecting STDOUT failed");  goto end;}
   // Set a read handle to the pipe to be STDIN.
 if(!SetStdHandle(STD_INPUT_HANDLE, hChildsInpPipe))
  {  Mbox("Redirecting Stdin failed");  goto end;}

 si.cb = sizeof(STARTUPINFO);
 si.dwFlags = STARTF_USESTDHANDLES;


  fSuccess = DuplicateHandle(hCurProc,hChildsOutPipe,
                             hCurProc, &hChildsOutPipeDup,0,
                             FALSE,DUPLICATE_SAME_ACCESS);
  if( !fSuccess ) Mbox("DuplicateHandle failed");

  fSuccess = DuplicateHandle(hCurProc,hChildsInpPipe,
                             hCurProc, &hChildsInpPipeDup,0,
                             FALSE,DUPLICATE_SAME_ACCESS);
  if( !fSuccess ) Mbox("DuplicateHandle failed");

 //si.hStdError = hChildsOutPipe;
 //si.hStdInput = hChildsInpPipe;
 //si.hStdOutput = hChildsOutPipe;/
 si.hStdError = hChildsOutPipeDup;
 si.hStdInput = hChildsInpPipeDup;
 si.hStdOutput =hChildsOutPipeDup;
 si.wShowWindow=SW_HIDE;
 si.dwFillAttribute=0;//BACKGROUND_BLUE|FOREGROUND_GREEN;
 si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;//|STARTF_USEFILLATTRIBUTE;//    |


 SetCurrentDirectory(pszCurrDir);// Чтобы компилятор знал откуда плясать


 #ifdef DBUG
 SaveShortFile("_CmdLineReal.txt",pszFullCmdLine,strlen(pszFullCmdLine));
 #endif

 bProc=CreateProcess(NULL,pszFullCmdLine,0,0,TRUE,CREATE_SUSPENDED|CREATE_NO_WINDOW,0,0, &si, &pi);
 if(!bProc || (!pi.hProcess || !pi.hThread || !pi.dwProcessId || !pi.dwThreadId))
 { Mbox("No Process created"); Gle(); goto end;}

 CloseHandle(hChildsOutPipeDup); hChildsOutPipeDup=0;
 CloseHandle(hChildsInpPipeDup); hChildsInpPipeDup=0;
 CloseHandle(hChildsOutPipe); hChildsOutPipe=0;
 CloseHandle(hChildsInpPipe); hChildsInpPipe=0;
 
 dwTicksStarted=GetTickCount();
 
 dwRes=ResumeThread(pi.hThread);
 //sprintf(str,"ResumeThread return %d\r\n",dwRes); Lm(str);
 SetLastError(0);
 
 while((bRead && dwRead) || iFailures < 10)
 { dwRead=0;
   dwTicksLastRead=GetTickCount();
   bRead=ReadFile(hMyReadPipe,pRcvBuf+dwNbytesRead,2,&dwRead,0);

   if(bRead && dwRead)
   { dwNbytesRead+=dwRead;  
     if( dwNbytesRead+1>=50)
     { *(pRcvBuf+dwNbytesRead)=0; Lm(pRcvBuf); 
       dwReadSoFar+=dwNbytesRead; dwNbytesRead=0;
     }
   }
   else iFailures++;
    //else Gle();
 }
 //sprintf(str,"dwReadSoFar %d\r\n",dwReadSoFar); Lm(str);
 bRes=CompleteReading();
 SaveShortFile("_pszLe.txt",pszLe,iCurErrStrlen);
 if(ptl)FindAndAddWarnsErrorMessage(ptl,pv);

end:
 if(hChildsInpPipe)CloseHandle(hChildsInpPipe);
 if(hMyOutPipe)CloseHandle(hMyOutPipe);
 if(hChildsOutPipe)CloseHandle(hChildsOutPipe);
 if(hMyReadPipe)CloseHandle(hMyReadPipe);
 if(ov.hEvent)CloseHandle(ov.hEvent);
 SetStdHandle(STD_INPUT_HANDLE, hSaveStdin);
 SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout);
 dwTicksFinished=GetTickCount();
 return bRes;
}
Александр
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.