Monitor.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:10k
- /*
- Copyright (C) 2007 Ibadov Tariel <itk@bk.ru>
- */
- #define MONITOR_EXPORTS
- #include <fstream.h>
- #include <iostream.h>
- #include "PortList.h"
- #include "Monitor.h"
- #include "rpcl.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;
- typedef BOOL (WINAPI *fpENUMPORTS)
- (
- LPWSTR pName,
- DWORD Level,
- LPBYTE pPorts,
- DWORD cbBuf,
- LPDWORD pcbNeeded,
- LPDWORD pcReturned
- );
- BOOL PortExists(TCHAR *pServerName,TCHAR *pPortName,DWORD *pError)
- {
- BOOL Found = TRUE;
- fpENUMPORTS pfnSpoolssEnumPorts;
- DWORD cbNeeded;
- DWORD cReturned;
- DWORD cbPorts;
- LPPORT_INFO_1 pPorts;
- *pError = NO_ERROR;
- if (!hSpoolssDll)
- hSpoolssDll = LoadLibrary(L"SPOOLSS.DLL");
- if(!hSpoolssDll){
- *pError=GetLastError();
- }
- else
- {
- pfnSpoolssEnumPorts = (fpENUMPORTS)GetProcAddress(hSpoolssDll,"EnumPortsW");
- if(!pfnSpoolssEnumPorts){
- *pError = GetLastError();
- FreeLibrary(hSpoolssDll);
- hSpoolssDll = NULL;
- }
- }
- if (!pfnSpoolssEnumPorts)
- Found=TRUE;
- (*pfnSpoolssEnumPorts)(pServerName, 1, NULL, 0, &cbNeeded, &cReturned);
- if (GetLastError() != ERROR_INSUFFICIENT_BUFFER){
- Found=TRUE;
- }
- else{
- cbPorts = cbNeeded;
- pPorts = (LPPORT_INFO_1)new BYTE[cbPorts];
- if(!pPorts){
- Found=TRUE;
- }
- else{
- if(!(*pfnSpoolssEnumPorts)(pServerName,1,(LPBYTE)pPorts,cbPorts,&cbNeeded,&cReturned)){
- Found=TRUE;
- }
- else{
- Found = FALSE;
- for (DWORD z = 0; z < cReturned; z++){
- if(_tcsicmp(pPorts[z].pName, pPortName)==0)
- Found = TRUE;
- }
- }
- }
- delete pPorts;
- }
- return Found;
- }
- BOOL FileExists(TCHAR *sFilePath)
- {
- WIN32_FIND_DATA w32fd;
- HANDLE hFile=FindFirstFile(sFilePath,&w32fd);
- if(hFile!=INVALID_HANDLE_VALUE){
- FindClose(hFile);
- return TRUE;
- }
- return FALSE;
- }
- BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpRes)
- {
- switch (dwReason)
- {
- case DLL_PROCESS_ATTACH:
- init_log( _T("imgport"), &hModule, 1 );
- 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();
- PORT *pPort=(PORT*)hPort;
- TCHAR filepath[MAX_PATH];
- TCHAR filename[MAX_PATH];
- LPTSTR buff = NULL;
-
- if(pPort->dwStatus & PS_STARTDOC)
- {
- LeaveCritSection();
- return FALSE;
- }
- _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;
- }
- }
- //_tcscat( filepath, filename);
- _tcscpy( filepath, _T("fax_image"));
- if( FileExists(filepath) )
- DeleteFile(filepath);
- pPort->hFile = ::CreateFile( filepath, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- write_log( 3, _T("iStartDocPort"), _T("%s"), filepath );
- if(pPort->hFile==INVALID_HANDLE_VALUE)
- {
- _tcscat(filepath,_T(" : ERROR CreateFile !!!"));
- 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;
- EnterCritSection();
- if(pPort->hFile){
- FlushFileBuffers(pPort->hFile);
- CloseHandle(pPort->hFile);
- std::string s_tmp;
- std::string extApp;
- for (int i=0;i<MAX_PATH;i++)
- s_tmp+=pPort->sCurDocument[i];
- WideCharToMultiByte( CP_ACP, 0, pPort->sCurDocument, -1, (char *)s_tmp.c_str(), MAX_PATH-1, NULL, NULL);
- //s_tmp = pPort->sCurDocument;
-
- for (int i=0;i<MAX_PATH;i++)
- extApp+=pPort->extApp[i];
- int icount = 0, compress = 0;;
- int isFormat=0;
- 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;
- write_log( 3, "iEndDocPort", "%s %s %d %s", pPort->img_format, pPort->img_format_ext, pPort->dither, s_tmp.c_str() );
- icount = start_convert( (char *)s_tmp.c_str(), (char *)s_tmp.c_str(), isFormat, compress, pPort->dither, (char*)extApp.c_str());
- if(FileExists(pPort->sCurDocument))DeleteFile(pPort->sCurDocument);
- 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[MAX_PATH] = _T("");
- GetTempPath( MAX_PATH,tempdir);
- EnterCritSection();
- m_portlist->AddPort( _T("tif"), tempdir, _T("ImagePrinter Port"), 0, NULL, FALSE, 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)
- {
- MessageBox(hWnd,_T("This port cannot be configured."),szLocalMonitor,MB_OK);
- 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 nn Couldn't remove the port."),_T("Error"),MB_OK);
- return TRUE;
- }
- BOOL IsFileReady(TCHAR *sPath)
- {
- HANDLE hFile=CreateFile(sPath,GENERIC_READ,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hFile==INVALID_HANDLE_VALUE){
- return FALSE;
- }
- else{
- CloseHandle(hFile);
- 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;
- }