home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / wincom / download / mltwcx.1.0.0.8-src.cab / mltwcx-src / ConsoleOutput.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-16  |  4.6 KB  |  202 lines

  1. #include "stdafx.h"
  2. #include "ConsoleOutput.h"
  3. #include "Config.h"
  4.  
  5.  
  6. CConsoleOutput::CConsoleOutput(bool bSaveList)
  7. {
  8.     m_bSaveList=bSaveList;
  9.     m_hFile=NULL;
  10.     m_hFileMap=NULL;
  11.     m_lpData=NULL;
  12.     m_bDoOemConversion=true;
  13. }
  14. /*
  15. int CConsoleOutput::ParseNExecuteCommand(int idx,CArchiveDescription *pad, LPCSTR lpArcFile, LPCSTR lpFile)
  16. {
  17.     LPCSTR lpFmtStr=pad->String(idx);
  18.  
  19.     if(!lpFmtStr)
  20.     {
  21.         MessageBox(GetFocus(),"Format string for this command is not defined. "
  22.             "Current operation couldn't be processed. "
  23.             "Check you multiarc.ini settings for this archiver type.","Warning",MB_ICONWARNING);
  24.  
  25.         return -1;
  26.     }
  27.  
  28.     LPCSTR lpArchiver=pad->String(ARCHIVER_IDX);
  29.  
  30.     char szCommand[MAX_PATH*4];
  31.     char szFormat[MAX_PATH];
  32.     int iBP=lstrlen(lpFmtStr);
  33.     char **pinta=new LPSTR[iBP+1];
  34.     pinta[0]=0;
  35.     va_list vl;
  36.     va_start(vl,pinta[0]);
  37.     iBP=1;
  38.  
  39.     strcpy(szFormat,lpFmtStr);
  40.  
  41.     char *p=szFormat;
  42.     while(*p)
  43.     {
  44.         if(*p == '%')
  45.         {
  46.             switch(*(p+1))
  47.             {
  48.             case 'A':
  49.                 pinta[iBP++]=(char *)lpArcFile;
  50.                 p[1]='s';
  51.                 break;
  52.             case 'F':
  53.                 pinta[iBP++]=(char *)lpFile;
  54.                 p[1]='s';
  55.                 break;
  56.             case 'P':
  57.                 pinta[iBP++]=(char *)lpArchiver;
  58.                 p[1]='s';
  59.                 break;
  60.             }
  61.         }
  62.         p++;
  63.     }
  64.     
  65.     wvsprintf(szCommand,szFormat,vl);
  66.  
  67.     va_end(vl);
  68.  
  69.     int iRet=ExecuteCommand(szCommand);
  70.  
  71.     pad->DebugOutput(szCommand,GetOutputData(),iRet);
  72.  
  73.     delete[] pinta;
  74.  
  75.     return iRet;
  76. }
  77. */
  78. LPSTR GetErrorMessage()
  79. {
  80.     LPVOID lpMsgBuf;
  81.     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  82.         NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  83.         (LPTSTR) &lpMsgBuf,0,NULL);
  84.     return (LPSTR)lpMsgBuf;
  85. }
  86.  
  87.  
  88. int CConsoleOutput::ExecuteCommand(LPCSTR lpCommand,LPCSTR lpInput)
  89. {
  90.     SECURITY_ATTRIBUTES sa          = {0};
  91.     STARTUPINFO         si          = {0};
  92.     PROCESS_INFORMATION pi          = {0};
  93.     HANDLE              hPipeInputRead   = NULL;
  94.     HANDLE              hPipeInputWrite  = NULL;
  95.     BOOL                bTest = 0;
  96.     DWORD               dwNumberOfBytesRead = 0;
  97.  
  98.     ReleaseContents();
  99.  
  100.     sa.nLength = sizeof(sa);
  101.     sa.bInheritHandle = TRUE;
  102.     sa.lpSecurityDescriptor = NULL;
  103.  
  104.     char path[MAX_PATH];
  105.     char name[MAX_PATH];
  106.     GetTempPath(MAX_PATH,path);
  107.     GetTempFileName(path,"zha",0,name);
  108.     m_hFile=CreateFile(name,GENERIC_READ|GENERIC_WRITE,NULL,&sa,CREATE_ALWAYS,
  109.                                 FILE_ATTRIBUTE_TEMPORARY|FILE_FLAG_RANDOM_ACCESS|
  110.                                 FILE_FLAG_DELETE_ON_CLOSE,NULL);
  111.  
  112.       // Create pipe for standard input redirection.
  113.     CreatePipe(&hPipeInputRead,&hPipeInputWrite,&sa,0);
  114.  
  115.     if(lpInput)
  116.     {
  117.         DWORD dw;
  118.         WriteFile(hPipeInputWrite,lpInput,lstrlen(lpInput),&dw,0);
  119.     }
  120.  
  121.       // Make child process use hFile as standard out,
  122.       // and make sure it does not show on screen.
  123.     si.cb = sizeof(si);
  124.     si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  125.     si.wShowWindow = SW_HIDE;
  126.     si.hStdInput   = hPipeInputRead;
  127.     si.hStdOutput  = m_hFile;
  128.     si.hStdError   = m_hFile;
  129.  
  130.     char cmdbuf[1024];
  131.     wsprintf(cmdbuf,"%sconspawn.pipe \"%s\"",g_szMultiarcPath,lpCommand);
  132.  
  133.     int ExitCode=0;
  134.  
  135.     if(!CreateProcess(NULL,cmdbuf,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
  136.     {
  137.         LPSTR lpError=GetErrorMessage();
  138.         char sz[1024];
  139.         wsprintf(sz,"CONSPAWN.PIPE running error : %s",lpError);
  140.         MessageBox(GetFocus(),sz,"Error",MB_ICONSTOP);
  141.         LocalFree(lpError);
  142.         ExitCode=MAEL_CANT_CREATE_PROCESS;
  143.     }
  144.  
  145.       // Now that handles have been inherited, close it to be safe.
  146.       // You don't want to read or write to them accidentally.
  147.     CloseHandle(hPipeInputRead);
  148.  
  149.     SetFilePointer(m_hFile,0,0,FILE_BEGIN);
  150.  
  151.     int iCon=100000;
  152.       // Wait for CONSPAWN to finish.
  153.     while(WaitForSingleObject (pi.hProcess, 100)==WAIT_TIMEOUT)
  154.         if(pProcessDataProc && !pProcessDataProc(NULL,iCon=(iCon<0)?100000:iCon-1000))
  155.             break;
  156.  
  157.     if(!ExitCode)
  158.         GetExitCodeProcess(pi.hProcess,(LPDWORD)&ExitCode);
  159.  
  160.       // Close all remaining handles
  161.     CloseHandle (pi.hProcess);
  162.     CloseHandle (hPipeInputWrite);
  163.  
  164.     if(m_bSaveList)
  165.     {
  166.         DWORD dwSize=GetFileSize(m_hFile,NULL);
  167.         m_hFileMap=CreateFileMapping(m_hFile,NULL,PAGE_READWRITE,0,dwSize+1,NULL);
  168.         m_lpData=(LPSTR)MapViewOfFile(m_hFileMap,FILE_MAP_WRITE,0,0,0);
  169.         m_lpData[dwSize]=0;
  170.         if(m_bDoOemConversion)
  171.             OemToChar(m_lpData,m_lpData);
  172.     }
  173.     else
  174.     {
  175.         CloseHandle(m_hFile);
  176.         m_hFile=NULL;
  177.     }
  178.  
  179.     return ExitCode;
  180. }
  181.  
  182. void CConsoleOutput::ReleaseContents()
  183. {
  184.     if(m_lpData)
  185.     {
  186.         UnmapViewOfFile(m_lpData);
  187.         m_lpData=NULL;
  188.     }
  189.  
  190.     if(m_hFileMap)
  191.     {
  192.         CloseHandle(m_hFileMap);
  193.         m_hFileMap=NULL;
  194.     }
  195.  
  196.     if(m_hFile)
  197.     {
  198.         CloseHandle(m_hFile);
  199.         m_hFile=NULL;
  200.     }
  201. }
  202.