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

打印编程

开发平台:

Visual C++

  1. #include "PortList.h"
  2. CPortList::CPortList(TCHAR *sMonitorName,TCHAR *sPortDesc)
  3. {
  4.         if(sMonitorName) m_sMonitorName=_tcsdup(sMonitorName);
  5.         else m_sMonitorName=_tcsdup(_T(""));
  6.         if(sPortDesc)        m_sPortDesc=_tcsdup(sPortDesc);
  7.         else m_sPortDesc=_tcsdup(_T(""));
  8.         m_pFirst=new PORT;
  9.         m_pFirst->pNext=NULL;
  10.         m_pFirst->dwStatus=0;
  11.         m_pFirst->hFile=0;
  12.         m_pFirst->dwJobID=0;
  13.         m_pFirst->dither = FALSE;
  14.         _tcscpy(m_pFirst->sPath,_T(""));
  15.         _tcscpy(m_pFirst->sCurDocument,_T(""));
  16.         _tcscpy(m_pFirst->sPrinter,_T(""));
  17. }
  18. CPortList::~CPortList()
  19. {
  20.         free(m_sMonitorName);
  21.         free(m_sPortDesc);
  22.         PORT *pNext;
  23.         while(m_pFirst){
  24.                 pNext=m_pFirst->pNext;
  25.                 delete m_pFirst;
  26.                 m_pFirst=pNext;
  27.         }
  28. }
  29. BOOL CPortList::EnumPorts(LPWSTR   pName,DWORD   Level,LPBYTE  pPorts,DWORD   cbBuf,LPDWORD pcbNeeded,        LPDWORD pcReturned)
  30. {
  31.     LPBYTE  pEnd;
  32.     DWORD   LastError=0;
  33.     DWORD cb=0;
  34.         PORT *pPort=m_pFirst;
  35.     while (pPort->pNext){
  36.         cb+=GetPortSize(pPort->sPath, Level);
  37.         pPort=pPort->pNext;
  38.     }
  39.     *pcbNeeded=cb;
  40.     if (cb <= cbBuf){
  41.                 pEnd=pPorts+cbBuf;
  42.         *pcReturned=0;
  43.         pPort=m_pFirst;
  44.         while (pPort->pNext){
  45.             pEnd = CopyPortToBuffer(pPort, Level, pPorts, pEnd);
  46.             switch (Level)
  47.                         {
  48.             case 1:
  49.                 pPorts+=sizeof(PORT_INFO_1);
  50.                 break;
  51.             case 2:
  52.                 pPorts+=sizeof(PORT_INFO_2);
  53.                 break;
  54.             default:
  55.                 LastError = ERROR_INVALID_LEVEL;
  56.                 goto Cleanup;
  57.             }
  58.             (*pcReturned)++;
  59.             pPort=pPort->pNext;
  60.         }
  61.     }
  62.     else LastError = ERROR_INSUFFICIENT_BUFFER;
  63. Cleanup:
  64.     if (LastError)        {
  65.         SetLastError(LastError);
  66.         return FALSE;
  67.     }
  68.         else return TRUE;
  69. }
  70. DWORD CPortList::GetPortSize(TCHAR *pName,DWORD dwLevel)
  71. {
  72.     DWORD   cb;
  73.     switch (dwLevel) {
  74.     case 1:
  75.         cb=sizeof(PORT_INFO_1) +
  76.                         _tcslen(pName)*sizeof(TCHAR) +
  77.                         sizeof(TCHAR);
  78.         break;
  79.     case 2:
  80.         cb = _tcslen(pName) + 1 + _tcslen(m_sMonitorName) + 1 +_tcslen(m_sPortDesc) + 1;
  81.         cb *= sizeof(TCHAR);
  82.         cb += sizeof(PORT_INFO_2);
  83.         break;
  84.     default:
  85.         cb = 0;
  86.         break;
  87.     }
  88.     return cb;
  89. }
  90. LPBYTE CPortList::CopyPortToBuffer(PORT *pPort,DWORD dwLevel,LPBYTE pStart,LPBYTE pEnd)
  91. {
  92.         switch(dwLevel)
  93.         {
  94.         case 1:
  95.                 {
  96.                         PORT_INFO_1 *pPortInfo=(PORT_INFO_1*)pStart;
  97.                         pEnd-=_tcslen(pPort->sPath) * sizeof(TCHAR) + sizeof(TCHAR);
  98.                         pPortInfo->pName=_tcscpy((TCHAR*)pEnd,pPort->sPath);
  99.                         break;
  100.                 }
  101.         case 2:
  102.                 {
  103.                         PORT_INFO_2 *pPortInfo=(PORT_INFO_2*)pStart;
  104.                         pEnd-=_tcslen(m_sMonitorName) * sizeof(TCHAR) + sizeof(TCHAR);
  105.                         pPortInfo->pMonitorName=_tcscpy((TCHAR*)pEnd,m_sMonitorName);
  106.                         pEnd-=_tcslen(m_sPortDesc) * sizeof(TCHAR) + sizeof(TCHAR);
  107.                         pPortInfo->pDescription=_tcscpy((TCHAR*)pEnd,m_sPortDesc);
  108.                         pEnd-=_tcslen(pPort->sPath) * sizeof(TCHAR) + sizeof(TCHAR);
  109.                         pPortInfo->pPortName=_tcscpy((TCHAR*)pEnd,pPort->sPath);
  110.                         pPortInfo->fPortType=0;
  111.                         pPortInfo->Reserved=0;
  112.                         break;
  113.                 }
  114.         default:
  115.                 MessageBox(0,_T("Error"),_T("invalid level"),MB_OK);
  116.         }
  117.     return pEnd;
  118. }
  119. BOOL CPortList::AddPort(TCHAR *img_format, TCHAR *cPath, TCHAR *sPath, DWORD dwStatus, TCHAR* img_format_ext, BOOL dither, TCHAR* extApp)
  120. {
  121.         HKEY hKey;
  122.         bool tmp_bool;
  123.         PORT *pNew=new PORT;
  124.         if(!pNew){
  125.                   return FALSE;
  126.         }
  127.         pNew->pNext=m_pFirst;
  128.         pNew->dwStatus=dwStatus;
  129.         pNew->hFile=0;
  130.         pNew->dwJobID=0;
  131.         pNew->dither = dither;
  132.          _tcscpy(pNew->img_format,img_format);        
  133.          _tcscpy(pNew->img_format_ext,img_format_ext);
  134.          _tcscpy(pNew->extApp,extApp);        
  135.          _tcscpy(pNew->cPath,cPath);
  136.         _tcscpy(pNew->sPath,sPath);
  137.         _tcscpy(pNew->sCurDocument,_T(""));
  138.         _tcscpy(pNew->sPrinter,_T(""));
  139.         m_pFirst=pNew;
  140.         return TRUE;
  141. }
  142. void CPortList::Save(TCHAR *sRoot)
  143. {
  144.         HKEY hKey;
  145.         if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,sRoot,0,KEY_SET_VALUE | KEY_READ,&hKey) != ERROR_SUCCESS)
  146.                         return;
  147.         DWORD z;
  148.         TCHAR sPort[5];
  149.         z=1;
  150.         do{
  151.                 _ultot(z++,sPort,10);
  152.         }while(RegDeleteValue(hKey,sPort)==ERROR_SUCCESS);
  153.         PORT *pPort=m_pFirst;
  154.         z=Count();
  155.         while(pPort->pNext){
  156.                 _ultot(z--,sPort,10);
  157.                 RegSetValueEx(hKey,_T("path"),0,REG_SZ,(BYTE*)pPort->cPath,_tcslen(pPort->cPath)*sizeof(TCHAR));
  158.                 RegSetValueEx(hKey,_T("name"),0,REG_SZ,(BYTE*)_T("ImagePrinter Port"),_tcslen(_T("ImagePrinter Port"))*sizeof(TCHAR));                
  159.                 RegSetValueEx(hKey,_T("format"),0,REG_SZ,(BYTE*)_T("tif"),_tcslen(_T("tif"))*sizeof(TCHAR));                
  160.                 pPort=pPort->pNext;
  161.         }
  162.         RegCloseKey(hKey);
  163. }
  164. #define szLocalMonitor _T("ImagePrinter Port")
  165. void CPortList::Load2(PMONITORINIT pMi)
  166. {
  167.     if( NULL != pMi )
  168.     {
  169. HANDLE phkResult = NULL;
  170. TCHAR sPath[MAX_PATH]=_T("path");
  171. TCHAR sFormat[MAX_PATH]=_T("format");
  172. TCHAR sFormatEx[MAX_PATH]=_T("format_ext");
  173. TCHAR extApp[MAX_PATH] = _T("ext_app");
  174. DWORD dither = 0;
  175. DWORD dwSize;
  176. LONG ret;
  177. if( ERROR_SUCCESS != (ret = pMi->pMonitorReg->fpOpenKey( pMi->hckRegistryRoot, szLocalMonitor, KEY_READ, &phkResult, pMi->hSpooler ) ) )
  178. return;
  179. dwSize=sizeof(dither);
  180. if( ERROR_SUCCESS != (ret = pMi->pMonitorReg->fpQueryValue( phkResult, _T("dither"), 0, (BYTE*)&dither, &dwSize, pMi->hSpooler ) ) )
  181. {
  182. dither = 0;
  183. ret = ERROR_SUCCESS;
  184. }
  185. dwSize=sizeof(extApp);
  186. if( ERROR_SUCCESS != (ret = pMi->pMonitorReg->fpQueryValue( phkResult, extApp, 0, (BYTE*)extApp, &dwSize, pMi->hSpooler) ) )
  187. {
  188. _tcscpy(extApp,_T(""));
  189. ret = ERROR_SUCCESS;
  190. }
  191. dwSize=sizeof(sFormatEx);
  192. if( ERROR_SUCCESS != (ret = pMi->pMonitorReg->fpQueryValue( phkResult, sFormatEx, 0, (BYTE*)sFormatEx, &dwSize, pMi->hSpooler) ) )
  193. {
  194. _tcscpy(sFormatEx,_T(""));
  195. ret = ERROR_SUCCESS;
  196. }
  197. dwSize = sizeof(sPath);
  198. ret = pMi->pMonitorReg->fpQueryValue( phkResult, sPath, 0, (BYTE*)sPath, &dwSize, pMi->hSpooler );
  199. dwSize = sizeof(sFormat);
  200. if( ret == ERROR_SUCCESS )
  201. ret = pMi->pMonitorReg->fpQueryValue( phkResult, sFormat, 0, (BYTE*)sFormat, &dwSize, pMi->hSpooler );
  202. if( ret==ERROR_SUCCESS )
  203. AddPort( sFormat, sPath, _T("ImagePrinter Port"), 0, sFormatEx, (0!=dither), extApp);
  204. pMi->pMonitorReg->fpCloseKey( phkResult, pMi->hSpooler );
  205.     }
  206. }
  207. void CPortList::Load(TCHAR *sRoot)
  208. {
  209. HKEY hKey;
  210. if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,sRoot,0,KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS){
  211. return;
  212. }
  213. TCHAR sPath[MAX_PATH]=_T("path");
  214. TCHAR sFormat[MAX_PATH]=_T("format");
  215. TCHAR sFormatEx[MAX_PATH]=_T("format_ext");
  216. TCHAR extApp[MAX_PATH] = _T("ext_app");
  217. DWORD dither = 0;
  218. DWORD dwSize;
  219. LONG ret;
  220. dwSize=sizeof(dither);
  221. if( ERROR_SUCCESS != (ret=RegQueryValueEx(hKey,_T("dither"),0,NULL,(BYTE*)&dither,&dwSize) ) )
  222. {
  223. dither = 0;
  224. ret = ERROR_SUCCESS;
  225. }
  226. dwSize=sizeof(extApp);
  227. if( ERROR_SUCCESS != (ret=RegQueryValueEx(hKey,extApp,0,NULL,(BYTE*)extApp,&dwSize) ) )
  228. {
  229. _tcscpy(extApp,_T(""));
  230. ret = ERROR_SUCCESS;
  231. }
  232. dwSize=sizeof(sFormatEx);
  233. if( ERROR_SUCCESS != (ret=RegQueryValueEx(hKey,sFormatEx,0,NULL,(BYTE*)sFormatEx,&dwSize) ) )
  234. {
  235. _tcscpy(sFormatEx,_T(""));
  236. ret = ERROR_SUCCESS;
  237. }
  238. dwSize=sizeof(sPath);
  239. ret=RegQueryValueEx(hKey,sPath,0,NULL,(BYTE*)sPath,&dwSize);
  240. dwSize=sizeof(sFormat);
  241. if(ret==ERROR_SUCCESS)                
  242. ret=RegQueryValueEx(hKey,sFormat,0,NULL,(BYTE*)sFormat,&dwSize);
  243. if(ret==ERROR_SUCCESS){
  244. AddPort( sFormat, sPath, _T("ImagePrinter Port"), 0, sFormatEx, (0!=dither), extApp);
  245. }
  246. RegCloseKey(hKey);
  247. }
  248. PORT *CPortList::FindPort(TCHAR *sPath)
  249. {
  250.         PORT *pPort=m_pFirst;
  251.         while(pPort->pNext){
  252.                 if(_tcsicmp(sPath,pPort->sPath)==0)
  253.                         return pPort;
  254.                 pPort=pPort->pNext;
  255.         }
  256.         return NULL;
  257. }
  258. BOOL CPortList::DeletePort(TCHAR *sPath)
  259. {
  260.         PORT *pPort=m_pFirst;
  261.         PORT *pPrevPort=m_pFirst;
  262.         while(pPort->pNext)        {
  263.                 if(_tcsicmp(sPath,pPort->sPath)==0)                {
  264.                         if(pPort==m_pFirst)                        {
  265.                                 m_pFirst=m_pFirst->pNext;
  266.                                 delete pPort;
  267.                         }
  268.                         else{
  269.                                 pPrevPort->pNext=pPort->pNext;
  270.                                 delete pPort;
  271.                         }
  272.                         return TRUE;
  273.                 }
  274.                 pPrevPort=pPort;
  275.                 pPort=pPort->pNext;
  276.         }
  277.         return FALSE;
  278. }
  279. DWORD CPortList::Count()
  280. {
  281.         DWORD dwCount=0;
  282.         PORT *pPort=m_pFirst;
  283.         while(pPort->pNext){
  284.                 ++dwCount;
  285.                 pPort=pPort->pNext;
  286.         }
  287.         return dwCount;
  288. }