www.pudn.com > imgport.rar > Monitor.cpp, change:2009-03-26,size:14484b


/* 
     Monitor.cpp 
      
     Project Full Name: imageprinter 
     http://sourceforge.net/projects/imageprinter/ 
     http://code-industry.net 
     
     
     Copyright (C) 2007-2008    Ibadov Tariel   <tariel@code-industry.net> 
     
    * This program is free software; you can redistribute it and/or modify 
    * it under the terms of the GNU General Public License as published by 
    * the Free Software Foundation; either version 2 of the License, or 
    * (at your option) any later version. 
    * 
    * This program is distributed in the hope that it will be useful, 
    * but WITHOUT ANY WARRANTY; without even the implied warranty of 
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    * GNU General Public License for more details.      
      
      
*/ 
 
#define MONITOR_EXPORTS 
#include <fstream.h> 
#include <iostream.h> 
#include "PortList.h" 
#include "Monitor.h" 
#include "rpcl.h" 
#include "p2jpg.h" 
#include "utils.h" 
 
#define szLocalMonitor	_T("ImagePrinter Port") 
 
 
 
HINSTANCE hInst=NULL; 
HINSTANCE hSpoolssDll=NULL; 
 
CRITICAL_SECTION SpoolerSection; 
 
HANDLE	  m_hEvent=NULL; 
CPortList *m_portlist=NULL; 
TCHAR	  *m_sRegistryRoot=NULL; 
 
bool default_ext_app; 
bool select_ext_app; 
extern long PageNo; 
string  NamePrintFile; 
string  NameTempFile; 
TCHAR tempfilename[MAX_PATH]; 
TCHAR image_name[MAX_PATH]; 
bool   onefile=false;  
int    qjpg=100; 
bool   multipage_tiff=false; 
 
typedef BOOL (WINAPI *fpENUMPORTS) 
( 
	LPWSTR  pName, 
	DWORD   Level, 
	LPBYTE  pPorts, 
	DWORD   cbBuf, 
	LPDWORD pcbNeeded, 
	LPDWORD pcReturned 
); 
 
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD  dwReason, LPVOID lpRes) 
{ 
	switch (dwReason) 
	{ 
	case DLL_PROCESS_ATTACH: 
	case DLL_THREAD_ATTACH: 
	        hInst = hModule; 
	        InitializeCriticalSection(&SpoolerSection); 
        	DisableThreadLibraryCalls((HINSTANCE)hModule); 
			m_portlist=new CPortList(); 
	        return TRUE; 
	case DLL_PROCESS_DETACH: 
	case DLL_THREAD_DETACH: 
		if(m_hEvent) 
			CloseHandle(m_hEvent); 
		if(m_sRegistryRoot) 
			free((void*)m_sRegistryRoot); 
		if(hSpoolssDll) 
			FreeLibrary(hSpoolssDll); 
		delete m_portlist; 
	        return TRUE; 
    } 
    UNREFERENCED_PARAMETER( lpRes ); 
	return TRUE; 
} 
 
void EnterCritSection() 
{ 
	EnterCriticalSection(&SpoolerSection); 
} 
 
void LeaveCritSection() 
{ 
	LeaveCriticalSection(&SpoolerSection); 
} 
 
 
BOOL WINAPI iEnumPorts(LPWSTR pName,DWORD dwLevel,LPBYTE pPorts,DWORD cbBuf,LPDWORD pdwNeeded,LPDWORD pdwReturned) 
{ 
	EnterCritSection(); 
	BOOL bResult = m_portlist->EnumPorts(pName,dwLevel,pPorts,cbBuf,pdwNeeded,pdwReturned); 
	LeaveCritSection(); 
	return bResult; 
} 
 
BOOL WINAPI iOpenPortEx(LPWSTR   pName,LPWSTR pPrinterName,PHANDLE pHandle, struct _MONITOR *pMonitor) 
{ 
	EnterCritSection(); 
	PORT *pPort=m_portlist->FindPort(pName); 
	*pHandle=(HANDLE)pPort; 
	pPort->dwStatus |= PS_OPENED; 
	LeaveCritSection(); 
	return pPort!=NULL; 
} 
 
BOOL WINAPI iOpenPort(LPWSTR   pName,PHANDLE pHandle) 
{ 
	EnterCritSection(); 
	PORT *pPort=m_portlist->FindPort(pName); 
	*pHandle=(HANDLE)pPort; 
	pPort->dwStatus |= PS_OPENED; 
	LeaveCritSection(); 
	return pPort!=NULL; 
} 
 
 
BOOL WINAPI iStartDocPort(HANDLE  hPort,LPWSTR  pPrinterName,DWORD   JobId,	DWORD   Level,	LPBYTE  pDocInfo) 
{ 
    EnterCritSection(); 
    TCHAR *sRoot; 
    HKEY hKey; 
    TCHAR original_name[MAX_PATH] = _T("original_name"); 
    sRoot=_T("SOFTWARE\\ImagePrinter"); 
	PORT *pPort=(PORT*)hPort; 
    TCHAR one_file[MAX_PATH] = _T("one_file"); 
    TCHAR multipagetiff[MAX_PATH] = _T("multipage_tiff"); 
    TCHAR q_jpg[MAX_PATH] = _T("q_jpg"); 
    TCHAR ext_app_name[MAX_PATH] = _T("ext_app_name"); 
	TCHAR ext_app_default[MAX_PATH] = _T("ext_app_default"); 
    TCHAR extApp[MAX_PATH] = _T("ext_app"); 
      
	if(pPort->dwStatus & PS_STARTDOC){ 
		LeaveCritSection(); 
		return FALSE; 
	} 
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,sRoot,0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS){ 
  	    TCHAR sPath[MAX_PATH]=_T("path"); 
  		TCHAR sFormat[MAX_PATH]=_T("format"); 
		TCHAR sFormatEx[MAX_PATH]=_T("format_ext"); 
 
 
		 
		TCHAR ShowPr[MAX_PATH] = _T("ShowProgress"); 
        _tcscpy(image_name,_T("image_name")); 
         
        DWORD dwSize;  LONG ret; 
           
        dwSize=sizeof(sPath); 
        ret=RegQueryValueEx(hKey,sPath,0,NULL,(BYTE*)sPath,&dwSize); 
        if(ret==ERROR_SUCCESS)  _tcscpy(pPort->cPath,sPath);                           
        dwSize=sizeof(sFormat); 
        ret=RegQueryValueEx(hKey,sFormat,0,NULL,(BYTE*)sFormat,&dwSize); 
        if(ret==ERROR_SUCCESS)  _tcscpy(pPort->img_format,sFormat);          
        dwSize=sizeof(sFormatEx); 
        ret=RegQueryValueEx(hKey,sFormatEx,0,NULL,(BYTE*)sFormatEx,&dwSize); 
        if(ret==ERROR_SUCCESS)  _tcscpy(pPort->img_format_ext,sFormatEx); 
        dwSize=sizeof(extApp); 
        ret=RegQueryValueEx(hKey,extApp,0,NULL,(BYTE*)extApp,&dwSize); 
         dwSize=sizeof(ShowPr); 
        ret=RegQueryValueEx(hKey,ShowPr,0,NULL,(BYTE*)ShowPr,&dwSize); 
        if(ret==ERROR_SUCCESS)  _tcscpy(pPort->ShowPr,ShowPr);            
        dwSize=sizeof(original_name); 
        RegQueryValueEx(hKey,original_name,0,NULL,(BYTE*)original_name,&dwSize); 
        dwSize=sizeof(one_file); 
        RegQueryValueEx(hKey,one_file,0,NULL,(BYTE*)one_file,&dwSize); 
        dwSize=sizeof(q_jpg); 
        RegQueryValueEx(hKey,q_jpg,0,NULL,(BYTE*)q_jpg,&dwSize); 
 
        dwSize=sizeof(multipagetiff); 
        RegQueryValueEx(hKey,multipagetiff,0,NULL,(BYTE*)multipagetiff,&dwSize); 
 
        dwSize=sizeof(ext_app_default); 
        RegQueryValueEx(hKey,ext_app_default,0,NULL,(BYTE*)ext_app_default,&dwSize); 
 
        dwSize=sizeof(ext_app_name); 
        RegQueryValueEx(hKey,ext_app_name,0,NULL,(BYTE*)ext_app_name,&dwSize); 
        if(ret==ERROR_SUCCESS)  _tcscpy(pPort->extApp,ext_app_name);            
        dwSize=sizeof(image_name); 
        RegQueryValueEx(hKey,image_name,0,NULL,(BYTE*)image_name,&dwSize); 
 
        RegCloseKey(hKey); 
         
    } 
     
    if (_tcsncmp(one_file,_T("true"), 4) == 0) onefile=true; 
    else onefile=false;                               
    if (_tcsncmp(multipagetiff,_T("true"), 4) == 0) { 
        multipage_tiff=true; 
        if (_tcsncmp(pPort->img_format,_T("tif"), 3) == 0) onefile=false;                               
    } 
    else multipage_tiff=false;                               
         
         
    qjpg=_ttoi(q_jpg); 
     
    TCHAR filepath[MAX_PATH];  
    TCHAR filename[MAX_PATH]; 
   _tcscpy(filepath,pPort->cPath); 
	switch(Level){ 
	case 1:{ 
			DOC_INFO_1 *pDoc=(DOC_INFO_1*)pDocInfo; 
			_tcscpy(filename,pDoc->pDocName); 
			break; 
		} 
	case 2:{ 
			DOC_INFO_2 *pDoc=(DOC_INFO_2*)pDocInfo; 
			_tcscpy(filename,pDoc->pDocName); 
			break; 
		} 
	} 
    
    _tcscpy(tempfilename,filepath); 
 
     
   if (_tcsncmp(original_name,_T("true"), 4) == 0) { 
       _tcscat(filepath,NormalizeFileName(filename));     
    }else  _tcscat( filepath, image_name); 
     
   if ((_tcsncmp(ext_app_default,_T("true"), 4) == 0)&&(_tcsncmp(extApp,_T("true"), 4)) == 0) { 
        default_ext_app=true; 
        select_ext_app=false; 
    } 
   else 
   if ((_tcsncmp(ext_app_name,_T("none"), 4) != 0)&&(_tcsncmp(extApp,_T("true"), 4) == 0)&&(_tcslen(ext_app_name)>2)) { 
                                          
        default_ext_app=false; 
        select_ext_app=true; 
    } 
    else 
    { 
        default_ext_app=false; 
        select_ext_app=false; 
    } 
     
 
 
    if (_tcsncmp(pPort->img_format,_T("raw"), 3) == 0){ 
         PageNo++;                                               
         
        swprintf( tempfilename, _T("%s%d"), filepath, PageNo); 
        _tcscat( tempfilename, _T(".raw")); 
        _tcscpy(filepath,tempfilename); 
         
    }else     
        _tcscat( tempfilename, image_name); 
     
 
  	for (int i=0;i<=MAX_PATH;i++) 
	    	NameTempFile+=tempfilename[i]; 
    WideCharToMultiByte( CP_ACP, 0, tempfilename, -1, (char *)NameTempFile.c_str(), MAX_PATH-1, NULL, NULL); 
 
  	for (int i=0;i<=MAX_PATH;i++) 
	    	NamePrintFile+=filepath[i]; 
    WideCharToMultiByte( CP_ACP, 0, filepath, -1, (char *)NamePrintFile.c_str(), MAX_PATH-1, NULL, NULL); 
   
     
 	if(FileExists(tempfilename))DeleteFile(tempfilename); 
	pPort->hFile=::CreateFile(tempfilename,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL); 
 
    if(pPort->hFile==INVALID_HANDLE_VALUE)	{ 
        _tcscat(tempfilename,_T(" : ERROR Create File !!!"));    
      	MessageBox(HWND_DESKTOP,filepath,_T("ImagePrinter") ,MB_OK);			 
		pPort->hFile=0; 
		HANDLE hPrinter; 
		if(OpenPrinter(pPrinterName,&hPrinter,NULL)){ 
			SetJob(hPrinter,JobId,0,NULL,JOB_CONTROL_RESTART); 
			SetJob(hPrinter,JobId,0,NULL,JOB_CONTROL_PAUSE); 
			CloseHandle(hPrinter); 
			LeaveCritSection(); 
			return FALSE; 
		} 
	} 
	else{ 
		_tcscpy(pPort->sCurDocument,filepath); 
		_tcscpy(pPort->sPrinter,pPrinterName); 
		pPort->dwStatus|=PS_STARTDOC; 
		pPort->dwJobID=JobId; 
 	 
	} 
	LeaveCritSection(); 
	return TRUE; 
} 
 
 
 
 
BOOL WINAPI iWritePort(	HANDLE  hPort,	LPBYTE  pBuffer,DWORD   cbBuf,LPDWORD pcbWritten) 
{ 
	PORT *pPort=(PORT*)hPort; 
	if(pPort->hFile){ 
		EnterCritSection(); 
		BOOL bResult=WriteFile(pPort->hFile,pBuffer,cbBuf,pcbWritten,NULL); 
		LeaveCritSection(); 
		if(!bResult){ 
			HANDLE hPrinter; 
			if(OpenPrinter(pPort->sPrinter,&hPrinter,NULL)){ 
				SetJob(hPrinter,pPort->dwJobID,0,NULL,JOB_CONTROL_RESTART); 
				SetJob(hPrinter,pPort->dwJobID,0,NULL,JOB_CONTROL_PAUSE); 
				CloseHandle(hPrinter); 
			} 
		} 
		return TRUE; 
	} 
	return TRUE;	 
} 
 
BOOL WINAPI iReadPort(HANDLE hPort,LPBYTE pBuffer,DWORD  cbBuf,LPDWORD pcbRead) 
{ 
	return TRUE; 
} 
 
BOOL WINAPI iEndDocPort(HANDLE hPort) 
{ 
	PORT *pPort=(PORT*)hPort; 
        int icount = 0, compress = 0;; 
        int isFormat=0; 
	EnterCritSection(); 
	if(pPort->hFile){ 
		FlushFileBuffers(pPort->hFile); 
   		CloseHandle(pPort->hFile); 
        std::string  extApp; 
     	for( int i=0;i<MAX_PATH;i++ ) 
            extApp+=pPort->extApp[i]; 
                    if (_tcsncmp(pPort->img_format,_T("tif"), 3) == 0)  
                    { 
                       isFormat=1; 
                       if (_tcsicmp(pPort->img_format_ext,_T("G4")) == 0) 
                           compress = COMPRESSION_CCITTFAX4; 
                       else 
                           if (_tcsicmp(pPort->img_format_ext,_T("G3")) == 0) 
                               compress = COMPRESSION_CCITTFAX3;  
                       else 
                           compress = COMPRESSION_LZW;                        
                    } 
     
                    if (_tcsncmp(pPort->img_format,_T("png"), 3) == 0) isFormat=2;                   
                    if (_tcsncmp(pPort->img_format,_T("bmp"), 3) == 0) isFormat=3;       
					if (_tcsncmp(pPort->img_format,_T("pdf"), 3) == 0) isFormat=4;   
                    if (_tcsncmp(pPort->img_format,_T("raw"), 3) == 0) isFormat=5;   
	                bool ShowPr=false;     
    				if (_tcsncmp(pPort->ShowPr,_T("true"), 4) == 0)    ShowPr=true;   
 
        icount = start_convert( isFormat, compress, (char*)extApp.c_str(), ShowPr); 
 
        if (isFormat!=5) 
            	if(FileExists(tempfilename))DeleteFile(tempfilename);		 
 
		PulseEvent(m_hEvent); 
		_tcscpy(pPort->sCurDocument,_T("")); 
		pPort->dwStatus &= ~PS_STARTDOC; 
		pPort->hFile=0; 
		HANDLE hPrinter; 
		if(OpenPrinter(pPort->sPrinter,&hPrinter,NULL)){ 
			SetJob(hPrinter,pPort->dwJobID,0,NULL,JOB_CONTROL_DELETE); 
			CloseHandle(hPrinter); 
		} 
	} 
	LeaveCritSection(); 
	return TRUE; 
} 
 
BOOL WINAPI iClosePort(HANDLE hPort) 
{ 
	EnterCritSection(); 
	PORT *pPort=(PORT*)hPort; 
	pPort->dwStatus &= ~PS_OPENED; 
	LeaveCritSection(); 
	return TRUE; 
} 
 
BOOL WINAPI iAddPort(LPWSTR pName,HWND hWnd,LPWSTR pMonitorName) 
{ 
    TCHAR tempdir[256]; 
    GetTempPath( MAX_PATH,tempdir); 
    EnterCritSection(); 
	m_portlist->AddPort( _T("tif"), tempdir, _T("ImagePrinter Port"), 0, NULL, NULL); 
	m_portlist->Save(m_sRegistryRoot); 
	LeaveCritSection(); 
    return TRUE; 
} 
 
BOOL WINAPI iAddPortEx(	LPWSTR	pName,DWORD	Level,LPBYTE	pBuffer,LPWSTR	pMonitorName) 
{ 
	return TRUE; 
} 
 
BOOL WINAPI iConfigurePort(	LPWSTR	pName,HWND	hWnd,LPWSTR	pPortName) 
{ 
    TCHAR *sRoot; 
    HKEY hKey; 
    sRoot=_T("SOFTWARE\\ImagePrinter"); 
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,sRoot,0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS){      
                                                                       
        TCHAR appdir[MAX_PATH]=_T("appdir"); 
        TCHAR appDir[MAX_PATH]=_T(""); 
        DWORD dwSize;  LONG ret; 
        dwSize=sizeof(appdir); 
        ret=RegQueryValueEx(hKey,appdir,0,NULL,(BYTE*)appdir,&dwSize);                                                                       
        RegCloseKey(hKey); 
         
        if(ret==ERROR_SUCCESS){ 
          _tcscpy(appDir,appdir);          
          _tcscat(appDir,_T("Options.exe")); 
           ShellExecute( NULL, _T("open"), appDir,_T(""), NULL, SW_SHOW);                                                                                  
        } 
    } 
	return TRUE; 
} 
 
BOOL WINAPI iDeletePort(LPWSTR	pName,HWND	hWnd,LPWSTR	pPortName) 
{ 
	EnterCritSection(); 
	if(m_portlist->DeletePort(pPortName)) 
	{ 
		m_portlist->Save(m_sRegistryRoot); 
		LeaveCritSection(); 
		TCHAR buf[512]; 
        _stprintf(buf,_T("ImagePrinter port successfully removed."),pPortName); 
		MessageBox(hWnd,buf,_T("Port removed"),MB_OK); 
		return TRUE; 
	} 
	LeaveCritSection(); 
	MessageBox(hWnd,_T("An unknown error \n\n Couldn't remove the port."),_T("Error"),MB_OK); 
	return TRUE;	 
} 
 
MONITOREX MonitorEx = { 
    sizeof(MONITOR), 
    { 
        iEnumPorts, 
        iOpenPort, 
        iOpenPortEx,				 
        iStartDocPort, 
        iWritePort, 
		iReadPort, 
        iEndDocPort, 
        iClosePort, 
        iAddPort, 
        iAddPortEx, 
        iConfigurePort, 
        iDeletePort, 
        NULL,				//iGetPrinterDataFromPort 
        NULL				//iSetPortTimeOuts 
    } 
}; 
 
LPMONITOREX WINAPI InitializePrintMonitor(LPWSTR pRegistryRoot) 
{ 
	if(m_sRegistryRoot)	free((void*)m_sRegistryRoot); 
	m_sRegistryRoot=_tcsdup(pRegistryRoot); 
	m_hEvent=CreateEvent(NULL,FALSE,FALSE,szLocalMonitor); 
	EnterCritSection(); 
	m_portlist->Load(m_sRegistryRoot); 
	LeaveCritSection(); 
	return &MonitorEx; 
}