home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
- #include "ConsoleOutput.h"
- #include "Config.h"
-
-
- CConsoleOutput::CConsoleOutput(bool bSaveList)
- {
- m_bSaveList=bSaveList;
- m_hFile=NULL;
- m_hFileMap=NULL;
- m_lpData=NULL;
- m_bDoOemConversion=true;
- }
- /*
- int CConsoleOutput::ParseNExecuteCommand(int idx,CArchiveDescription *pad, LPCSTR lpArcFile, LPCSTR lpFile)
- {
- LPCSTR lpFmtStr=pad->String(idx);
-
- if(!lpFmtStr)
- {
- MessageBox(GetFocus(),"Format string for this command is not defined. "
- "Current operation couldn't be processed. "
- "Check you multiarc.ini settings for this archiver type.","Warning",MB_ICONWARNING);
-
- return -1;
- }
-
- LPCSTR lpArchiver=pad->String(ARCHIVER_IDX);
-
- char szCommand[MAX_PATH*4];
- char szFormat[MAX_PATH];
- int iBP=lstrlen(lpFmtStr);
- char **pinta=new LPSTR[iBP+1];
- pinta[0]=0;
- va_list vl;
- va_start(vl,pinta[0]);
- iBP=1;
-
- strcpy(szFormat,lpFmtStr);
-
- char *p=szFormat;
- while(*p)
- {
- if(*p == '%')
- {
- switch(*(p+1))
- {
- case 'A':
- pinta[iBP++]=(char *)lpArcFile;
- p[1]='s';
- break;
- case 'F':
- pinta[iBP++]=(char *)lpFile;
- p[1]='s';
- break;
- case 'P':
- pinta[iBP++]=(char *)lpArchiver;
- p[1]='s';
- break;
- }
- }
- p++;
- }
-
- wvsprintf(szCommand,szFormat,vl);
-
- va_end(vl);
-
- int iRet=ExecuteCommand(szCommand);
-
- pad->DebugOutput(szCommand,GetOutputData(),iRet);
-
- delete[] pinta;
-
- return iRet;
- }
- */
- LPSTR GetErrorMessage()
- {
- LPVOID lpMsgBuf;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf,0,NULL);
- return (LPSTR)lpMsgBuf;
- }
-
-
- int CConsoleOutput::ExecuteCommand(LPCSTR lpCommand,LPCSTR lpInput)
- {
- SECURITY_ATTRIBUTES sa = {0};
- STARTUPINFO si = {0};
- PROCESS_INFORMATION pi = {0};
- HANDLE hPipeInputRead = NULL;
- HANDLE hPipeInputWrite = NULL;
- BOOL bTest = 0;
- DWORD dwNumberOfBytesRead = 0;
-
- ReleaseContents();
-
- sa.nLength = sizeof(sa);
- sa.bInheritHandle = TRUE;
- sa.lpSecurityDescriptor = NULL;
-
- char path[MAX_PATH];
- char name[MAX_PATH];
- GetTempPath(MAX_PATH,path);
- GetTempFileName(path,"zha",0,name);
- m_hFile=CreateFile(name,GENERIC_READ|GENERIC_WRITE,NULL,&sa,CREATE_ALWAYS,
- FILE_ATTRIBUTE_TEMPORARY|FILE_FLAG_RANDOM_ACCESS|
- FILE_FLAG_DELETE_ON_CLOSE,NULL);
-
- // Create pipe for standard input redirection.
- CreatePipe(&hPipeInputRead,&hPipeInputWrite,&sa,0);
-
- if(lpInput)
- {
- DWORD dw;
- WriteFile(hPipeInputWrite,lpInput,lstrlen(lpInput),&dw,0);
- }
-
- // Make child process use hFile as standard out,
- // and make sure it does not show on screen.
- si.cb = sizeof(si);
- si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
- si.wShowWindow = SW_HIDE;
- si.hStdInput = hPipeInputRead;
- si.hStdOutput = m_hFile;
- si.hStdError = m_hFile;
-
- char cmdbuf[1024];
- wsprintf(cmdbuf,"%sconspawn.pipe \"%s\"",g_szMultiarcPath,lpCommand);
-
- int ExitCode=0;
-
- if(!CreateProcess(NULL,cmdbuf,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
- {
- LPSTR lpError=GetErrorMessage();
- char sz[1024];
- wsprintf(sz,"CONSPAWN.PIPE running error : %s",lpError);
- MessageBox(GetFocus(),sz,"Error",MB_ICONSTOP);
- LocalFree(lpError);
- ExitCode=MAEL_CANT_CREATE_PROCESS;
- }
-
- // Now that handles have been inherited, close it to be safe.
- // You don't want to read or write to them accidentally.
- CloseHandle(hPipeInputRead);
-
- SetFilePointer(m_hFile,0,0,FILE_BEGIN);
-
- int iCon=100000;
- // Wait for CONSPAWN to finish.
- while(WaitForSingleObject (pi.hProcess, 100)==WAIT_TIMEOUT)
- if(pProcessDataProc && !pProcessDataProc(NULL,iCon=(iCon<0)?100000:iCon-1000))
- break;
-
- if(!ExitCode)
- GetExitCodeProcess(pi.hProcess,(LPDWORD)&ExitCode);
-
- // Close all remaining handles
- CloseHandle (pi.hProcess);
- CloseHandle (hPipeInputWrite);
-
- if(m_bSaveList)
- {
- DWORD dwSize=GetFileSize(m_hFile,NULL);
- m_hFileMap=CreateFileMapping(m_hFile,NULL,PAGE_READWRITE,0,dwSize+1,NULL);
- m_lpData=(LPSTR)MapViewOfFile(m_hFileMap,FILE_MAP_WRITE,0,0,0);
- m_lpData[dwSize]=0;
- if(m_bDoOemConversion)
- OemToChar(m_lpData,m_lpData);
- }
- else
- {
- CloseHandle(m_hFile);
- m_hFile=NULL;
- }
-
- return ExitCode;
- }
-
- void CConsoleOutput::ReleaseContents()
- {
- if(m_lpData)
- {
- UnmapViewOfFile(m_lpData);
- m_lpData=NULL;
- }
-
- if(m_hFileMap)
- {
- CloseHandle(m_hFileMap);
- m_hFileMap=NULL;
- }
-
- if(m_hFile)
- {
- CloseHandle(m_hFile);
- m_hFile=NULL;
- }
- }
-