Monitor.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:10k
源码类别:

打印编程

开发平台:

Visual C++

  1. /*
  2.    Copyright (C) 2007    Ibadov Tariel   <itk@bk.ru>
  3. */
  4. #define MONITOR_EXPORTS
  5. #include <fstream.h>
  6. #include <iostream.h>
  7. #include "PortList.h"
  8. #include "Monitor.h"
  9. #include "rpcl.h"
  10. #define szLocalMonitor _T("ImagePrinter Port")
  11. HINSTANCE hInst=NULL;
  12. HINSTANCE hSpoolssDll=NULL;
  13. CRITICAL_SECTION SpoolerSection;
  14. HANDLE m_hEvent=NULL;
  15. CPortList *m_portlist=NULL;
  16. TCHAR *m_sRegistryRoot=NULL;
  17. typedef BOOL (WINAPI *fpENUMPORTS)
  18. (
  19. LPWSTR  pName,
  20. DWORD   Level,
  21. LPBYTE  pPorts,
  22. DWORD   cbBuf,
  23. LPDWORD pcbNeeded,
  24. LPDWORD pcReturned
  25. );
  26. BOOL PortExists(TCHAR *pServerName,TCHAR *pPortName,DWORD *pError)
  27. {
  28. BOOL Found = TRUE;
  29. fpENUMPORTS pfnSpoolssEnumPorts;
  30. DWORD cbNeeded;
  31.     DWORD cReturned;
  32.     DWORD cbPorts;
  33.     LPPORT_INFO_1 pPorts;
  34.     *pError = NO_ERROR;
  35.     if (!hSpoolssDll)
  36.         hSpoolssDll = LoadLibrary(L"SPOOLSS.DLL");
  37. if(!hSpoolssDll){
  38. *pError=GetLastError();
  39. }
  40. else
  41. {
  42. pfnSpoolssEnumPorts = (fpENUMPORTS)GetProcAddress(hSpoolssDll,"EnumPortsW");
  43.         if(!pfnSpoolssEnumPorts){
  44. *pError = GetLastError();
  45. FreeLibrary(hSpoolssDll);
  46. hSpoolssDll = NULL;
  47. }
  48. }
  49.     if (!pfnSpoolssEnumPorts)
  50.         Found=TRUE;
  51.     (*pfnSpoolssEnumPorts)(pServerName, 1, NULL, 0, &cbNeeded, &cReturned);
  52.     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER){
  53. Found=TRUE;
  54. }
  55. else{
  56.         cbPorts = cbNeeded;
  57.         pPorts = (LPPORT_INFO_1)new BYTE[cbPorts];
  58.         if(!pPorts){
  59. Found=TRUE;
  60. }
  61. else{
  62.             if(!(*pfnSpoolssEnumPorts)(pServerName,1,(LPBYTE)pPorts,cbPorts,&cbNeeded,&cReturned)){
  63. Found=TRUE;
  64. }
  65. else{
  66.                 Found = FALSE;
  67.                 for (DWORD z = 0; z < cReturned; z++){
  68.                     if(_tcsicmp(pPorts[z].pName, pPortName)==0)
  69.                         Found = TRUE;
  70.                 }
  71.             }
  72.         }
  73.         delete pPorts;
  74.     }
  75.     return Found;
  76. }
  77. BOOL FileExists(TCHAR *sFilePath)
  78. {
  79. WIN32_FIND_DATA w32fd;
  80. HANDLE hFile=FindFirstFile(sFilePath,&w32fd);
  81. if(hFile!=INVALID_HANDLE_VALUE){
  82. FindClose(hFile);
  83. return TRUE;
  84. }
  85. return FALSE; 
  86. }
  87. BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD  dwReason, LPVOID lpRes)
  88. {
  89. switch (dwReason)
  90. {
  91. case DLL_PROCESS_ATTACH:
  92.                 init_log( _T("imgport"), &hModule, 1 ); 
  93. case DLL_THREAD_ATTACH:
  94.         hInst = hModule;
  95.         InitializeCriticalSection(&SpoolerSection);
  96.          DisableThreadLibraryCalls((HINSTANCE)hModule);
  97. m_portlist=new CPortList();
  98.         return TRUE;
  99. case DLL_PROCESS_DETACH:
  100. case DLL_THREAD_DETACH:
  101. if(m_hEvent)
  102. CloseHandle(m_hEvent);
  103. if(m_sRegistryRoot)
  104. free((void*)m_sRegistryRoot);
  105. if(hSpoolssDll)
  106. FreeLibrary(hSpoolssDll);
  107. delete m_portlist;
  108.         return TRUE;
  109.     }
  110.     UNREFERENCED_PARAMETER( lpRes );
  111. return TRUE;
  112. }
  113. void EnterCritSection()
  114. {
  115. EnterCriticalSection(&SpoolerSection);
  116. }
  117. void LeaveCritSection()
  118. {
  119. LeaveCriticalSection(&SpoolerSection);
  120. }
  121. BOOL WINAPI iEnumPorts(LPWSTR pName,DWORD dwLevel,LPBYTE pPorts,DWORD cbBuf,LPDWORD pdwNeeded,LPDWORD pdwReturned)
  122. {
  123. EnterCritSection();
  124. BOOL bResult = m_portlist->EnumPorts(pName,dwLevel,pPorts,cbBuf,pdwNeeded,pdwReturned);
  125. LeaveCritSection();
  126. return bResult;
  127. }
  128. BOOL WINAPI iOpenPortEx(LPWSTR   pName,LPWSTR pPrinterName,PHANDLE pHandle, struct _MONITOR *pMonitor)
  129. {
  130. EnterCritSection();
  131. PORT *pPort=m_portlist->FindPort(pName);
  132. *pHandle=(HANDLE)pPort;
  133. pPort->dwStatus |= PS_OPENED;
  134. LeaveCritSection();
  135. return pPort!=NULL;
  136. }
  137. BOOL WINAPI iOpenPort(LPWSTR   pName,PHANDLE pHandle)
  138. {
  139. EnterCritSection();
  140. PORT *pPort=m_portlist->FindPort(pName);
  141. *pHandle=(HANDLE)pPort;
  142. pPort->dwStatus |= PS_OPENED;
  143. LeaveCritSection();
  144. return pPort!=NULL;
  145. }
  146. BOOL WINAPI iStartDocPort(HANDLE  hPort,LPWSTR  pPrinterName,DWORD   JobId, DWORD   Level, LPBYTE  pDocInfo)
  147. {
  148. EnterCritSection();
  149. PORT *pPort=(PORT*)hPort;
  150. TCHAR filepath[MAX_PATH];
  151.     TCHAR filename[MAX_PATH];
  152. LPTSTR buff = NULL;
  153. if(pPort->dwStatus & PS_STARTDOC)
  154. {
  155. LeaveCritSection();
  156. return FALSE;
  157. }
  158. _tcscpy( filepath, pPort->cPath);
  159. switch(Level)
  160. {
  161. case 1:{
  162. DOC_INFO_1 *pDoc=(DOC_INFO_1*)pDocInfo;
  163. _tcscpy(filename,pDoc->pDocName);
  164. break;
  165. }
  166. case 2:{
  167. DOC_INFO_2 *pDoc=(DOC_INFO_2*)pDocInfo;
  168. _tcscpy(filename,pDoc->pDocName);
  169. break;
  170. }
  171. }
  172. //_tcscat( filepath, filename);
  173. _tcscpy( filepath, _T("fax_image"));
  174.   if( FileExists(filepath) ) 
  175. DeleteFile(filepath);
  176. pPort->hFile = ::CreateFile( filepath, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  177.         write_log( 3, _T("iStartDocPort"), _T("%s"), filepath );
  178.     if(pPort->hFile==INVALID_HANDLE_VALUE)
  179. {
  180.         _tcscat(filepath,_T(" : ERROR CreateFile !!!"));                                                       
  181.        MessageBox(HWND_DESKTOP,filepath,_T("ImagePrinter") ,MB_OK);
  182. pPort->hFile=0;
  183. HANDLE hPrinter;
  184. if(OpenPrinter(pPrinterName,&hPrinter,NULL))
  185. {
  186. SetJob(hPrinter,JobId,0,NULL,JOB_CONTROL_RESTART);
  187. SetJob(hPrinter,JobId,0,NULL,JOB_CONTROL_PAUSE);
  188. CloseHandle(hPrinter);
  189. LeaveCritSection();
  190. return FALSE;
  191. }
  192. }
  193. else
  194. {
  195. _tcscpy(pPort->sCurDocument,filepath);
  196. _tcscpy(pPort->sPrinter,pPrinterName);
  197. pPort->dwStatus|=PS_STARTDOC;
  198. pPort->dwJobID=JobId;
  199. }
  200. LeaveCritSection();
  201. return TRUE;
  202. }
  203. BOOL WINAPI iWritePort( HANDLE  hPort, LPBYTE  pBuffer,DWORD   cbBuf,LPDWORD pcbWritten)
  204. {
  205. PORT *pPort=(PORT*)hPort;
  206. if(pPort->hFile){
  207. EnterCritSection();
  208. BOOL bResult=WriteFile(pPort->hFile,pBuffer,cbBuf,pcbWritten,NULL);
  209. LeaveCritSection();
  210. if(!bResult){
  211. HANDLE hPrinter;
  212. if(OpenPrinter(pPort->sPrinter,&hPrinter,NULL)){
  213. SetJob(hPrinter,pPort->dwJobID,0,NULL,JOB_CONTROL_RESTART);
  214. SetJob(hPrinter,pPort->dwJobID,0,NULL,JOB_CONTROL_PAUSE);
  215. CloseHandle(hPrinter);
  216. }
  217. }
  218. return TRUE;
  219. }
  220. return TRUE;
  221. }
  222. BOOL WINAPI iReadPort(HANDLE hPort,LPBYTE pBuffer,DWORD  cbBuf,LPDWORD pcbRead)
  223. {
  224. return TRUE;
  225. }
  226. BOOL WINAPI iEndDocPort(HANDLE hPort)
  227. {
  228. PORT *pPort=(PORT*)hPort;
  229. EnterCritSection();
  230. if(pPort->hFile){
  231. FlushFileBuffers(pPort->hFile);
  232.     CloseHandle(pPort->hFile);
  233.         std::string  s_tmp;
  234.         std::string  extApp;
  235.       for (int i=0;i<MAX_PATH;i++)
  236.      s_tmp+=pPort->sCurDocument[i];
  237.         WideCharToMultiByte( CP_ACP, 0, pPort->sCurDocument, -1, (char *)s_tmp.c_str(), MAX_PATH-1, NULL, NULL);
  238.         //s_tmp = pPort->sCurDocument;
  239.         
  240.       for (int i=0;i<MAX_PATH;i++)
  241.      extApp+=pPort->extApp[i];
  242.         int icount = 0, compress = 0;;
  243.         int isFormat=0;
  244.                     if (_tcsncmp(pPort->img_format,_T("tif"), 3) == 0) 
  245.                     {
  246.                        isFormat=1;
  247.                        if (_tcsicmp(pPort->img_format_ext,_T("G4")) == 0)
  248.                            compress = COMPRESSION_CCITTFAX4;
  249.                        else
  250.                            if (_tcsicmp(pPort->img_format_ext,_T("G3")) == 0)
  251.                                compress = COMPRESSION_CCITTFAX3; 
  252.                        else
  253.                            compress = COMPRESSION_LZW;                       
  254.                     }
  255.                     if (_tcsncmp(pPort->img_format,_T("png"), 3) == 0) isFormat=2;                  
  256.                     if (_tcsncmp(pPort->img_format,_T("bmp"), 3) == 0) isFormat=3;      
  257. if (_tcsncmp(pPort->img_format,_T("pdf"), 3) == 0) isFormat=4;      
  258.                 write_log( 3, "iEndDocPort", "%s %s %d %s", pPort->img_format, pPort->img_format_ext, pPort->dither, s_tmp.c_str() );  
  259.            icount = start_convert( (char *)s_tmp.c_str(), (char *)s_tmp.c_str(), isFormat, compress, pPort->dither, (char*)extApp.c_str());
  260.              if(FileExists(pPort->sCurDocument))DeleteFile(pPort->sCurDocument);
  261. PulseEvent(m_hEvent);
  262. _tcscpy(pPort->sCurDocument,_T(""));
  263. pPort->dwStatus &= ~PS_STARTDOC;
  264. pPort->hFile=0;
  265. HANDLE hPrinter;
  266. if(OpenPrinter(pPort->sPrinter,&hPrinter,NULL)){
  267. SetJob(hPrinter,pPort->dwJobID,0,NULL,JOB_CONTROL_DELETE);
  268. CloseHandle(hPrinter);
  269. }
  270. }
  271. LeaveCritSection();
  272. return TRUE;
  273. }
  274. BOOL WINAPI iClosePort(HANDLE hPort)
  275. {
  276. EnterCritSection();
  277. PORT *pPort=(PORT*)hPort;
  278. pPort->dwStatus &= ~PS_OPENED;
  279. LeaveCritSection();
  280. return TRUE;
  281. }
  282. BOOL WINAPI iAddPort(LPWSTR pName,HWND hWnd,LPWSTR pMonitorName)
  283. {
  284.     TCHAR tempdir[MAX_PATH] = _T("");
  285.     GetTempPath( MAX_PATH,tempdir);
  286.     EnterCritSection();
  287. m_portlist->AddPort( _T("tif"), tempdir, _T("ImagePrinter Port"), 0, NULL, FALSE, NULL);
  288. m_portlist->Save(m_sRegistryRoot);
  289. LeaveCritSection();
  290.     return TRUE;
  291. }
  292. BOOL WINAPI iAddPortEx( LPWSTR pName,DWORD Level,LPBYTE pBuffer,LPWSTR pMonitorName)
  293. {
  294. return TRUE;
  295. }
  296. BOOL WINAPI iConfigurePort( LPWSTR pName,HWND hWnd,LPWSTR pPortName)
  297. {
  298. MessageBox(hWnd,_T("This port cannot be configured."),szLocalMonitor,MB_OK);
  299. return TRUE;
  300. }
  301. BOOL WINAPI iDeletePort(LPWSTR pName,HWND hWnd,LPWSTR pPortName)
  302. {
  303. EnterCritSection();
  304. if(m_portlist->DeletePort(pPortName))
  305. {
  306. m_portlist->Save(m_sRegistryRoot);
  307. LeaveCritSection();
  308. TCHAR buf[512];
  309.         _stprintf(buf,_T("ImagePrinter port successfully removed."),pPortName);
  310. MessageBox(hWnd,buf,_T("Port removed"),MB_OK);
  311. return TRUE;
  312. }
  313. LeaveCritSection();
  314. MessageBox(hWnd,_T("An unknown error nn Couldn't remove the port."),_T("Error"),MB_OK);
  315. return TRUE;
  316. }
  317. BOOL IsFileReady(TCHAR *sPath)
  318. {
  319. HANDLE hFile=CreateFile(sPath,GENERIC_READ,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  320. if(hFile==INVALID_HANDLE_VALUE){
  321. return FALSE;
  322. }
  323. else{
  324. CloseHandle(hFile);
  325. return TRUE;
  326. }
  327. }
  328. MONITOREX MonitorEx = {
  329.     sizeof(MONITOR),
  330.     {
  331.         iEnumPorts,
  332.         iOpenPort,
  333.         iOpenPortEx,
  334.         iStartDocPort,
  335.         iWritePort,
  336. iReadPort,
  337.         iEndDocPort,
  338.         iClosePort,
  339.         iAddPort,
  340.         iAddPortEx,
  341.         iConfigurePort,
  342.         iDeletePort,
  343.         NULL, //iGetPrinterDataFromPort
  344.         NULL //iSetPortTimeOuts
  345.     }
  346. };
  347. LPMONITOREX WINAPI InitializePrintMonitor( LPWSTR pRegistryRoot)
  348. {
  349. if(m_sRegistryRoot) free((void*)m_sRegistryRoot);
  350. m_sRegistryRoot=_tcsdup(pRegistryRoot);
  351. m_hEvent=CreateEvent(NULL,FALSE,FALSE,szLocalMonitor);
  352. EnterCritSection();
  353. m_portlist->Load(m_sRegistryRoot);
  354. LeaveCritSection();
  355. return &MonitorEx;
  356. }