PortList.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:10k
- #include "PortList.h"
- CPortList::CPortList(TCHAR *sMonitorName,TCHAR *sPortDesc)
- {
- if(sMonitorName) m_sMonitorName=_tcsdup(sMonitorName);
- else m_sMonitorName=_tcsdup(_T(""));
- if(sPortDesc) m_sPortDesc=_tcsdup(sPortDesc);
- else m_sPortDesc=_tcsdup(_T(""));
- m_pFirst=new PORT;
- m_pFirst->pNext=NULL;
- m_pFirst->dwStatus=0;
- m_pFirst->hFile=0;
- m_pFirst->dwJobID=0;
- m_pFirst->dither = FALSE;
- _tcscpy(m_pFirst->sPath,_T(""));
- _tcscpy(m_pFirst->sCurDocument,_T(""));
- _tcscpy(m_pFirst->sPrinter,_T(""));
- }
- CPortList::~CPortList()
- {
- free(m_sMonitorName);
- free(m_sPortDesc);
- PORT *pNext;
- while(m_pFirst){
- pNext=m_pFirst->pNext;
- delete m_pFirst;
- m_pFirst=pNext;
- }
- }
- BOOL CPortList::EnumPorts(LPWSTR pName,DWORD Level,LPBYTE pPorts,DWORD cbBuf,LPDWORD pcbNeeded, LPDWORD pcReturned)
- {
- LPBYTE pEnd;
- DWORD LastError=0;
- DWORD cb=0;
- PORT *pPort=m_pFirst;
- while (pPort->pNext){
- cb+=GetPortSize(pPort->sPath, Level);
- pPort=pPort->pNext;
- }
- *pcbNeeded=cb;
- if (cb <= cbBuf){
- pEnd=pPorts+cbBuf;
- *pcReturned=0;
- pPort=m_pFirst;
- while (pPort->pNext){
- pEnd = CopyPortToBuffer(pPort, Level, pPorts, pEnd);
- switch (Level)
- {
- case 1:
- pPorts+=sizeof(PORT_INFO_1);
- break;
- case 2:
- pPorts+=sizeof(PORT_INFO_2);
- break;
- default:
- LastError = ERROR_INVALID_LEVEL;
- goto Cleanup;
- }
- (*pcReturned)++;
- pPort=pPort->pNext;
- }
- }
- else LastError = ERROR_INSUFFICIENT_BUFFER;
- Cleanup:
- if (LastError) {
- SetLastError(LastError);
- return FALSE;
- }
- else return TRUE;
- }
- DWORD CPortList::GetPortSize(TCHAR *pName,DWORD dwLevel)
- {
- DWORD cb;
- switch (dwLevel) {
- case 1:
- cb=sizeof(PORT_INFO_1) +
- _tcslen(pName)*sizeof(TCHAR) +
- sizeof(TCHAR);
- break;
- case 2:
- cb = _tcslen(pName) + 1 + _tcslen(m_sMonitorName) + 1 +_tcslen(m_sPortDesc) + 1;
- cb *= sizeof(TCHAR);
- cb += sizeof(PORT_INFO_2);
- break;
- default:
- cb = 0;
- break;
- }
- return cb;
- }
- LPBYTE CPortList::CopyPortToBuffer(PORT *pPort,DWORD dwLevel,LPBYTE pStart,LPBYTE pEnd)
- {
- switch(dwLevel)
- {
- case 1:
- {
- PORT_INFO_1 *pPortInfo=(PORT_INFO_1*)pStart;
- pEnd-=_tcslen(pPort->sPath) * sizeof(TCHAR) + sizeof(TCHAR);
- pPortInfo->pName=_tcscpy((TCHAR*)pEnd,pPort->sPath);
- break;
- }
- case 2:
- {
- PORT_INFO_2 *pPortInfo=(PORT_INFO_2*)pStart;
- pEnd-=_tcslen(m_sMonitorName) * sizeof(TCHAR) + sizeof(TCHAR);
- pPortInfo->pMonitorName=_tcscpy((TCHAR*)pEnd,m_sMonitorName);
- pEnd-=_tcslen(m_sPortDesc) * sizeof(TCHAR) + sizeof(TCHAR);
- pPortInfo->pDescription=_tcscpy((TCHAR*)pEnd,m_sPortDesc);
- pEnd-=_tcslen(pPort->sPath) * sizeof(TCHAR) + sizeof(TCHAR);
- pPortInfo->pPortName=_tcscpy((TCHAR*)pEnd,pPort->sPath);
- pPortInfo->fPortType=0;
- pPortInfo->Reserved=0;
- break;
- }
- default:
- MessageBox(0,_T("Error"),_T("invalid level"),MB_OK);
- }
- return pEnd;
- }
- BOOL CPortList::AddPort(TCHAR *img_format, TCHAR *cPath, TCHAR *sPath, DWORD dwStatus, TCHAR* img_format_ext, BOOL dither, TCHAR* extApp)
- {
- HKEY hKey;
- bool tmp_bool;
- PORT *pNew=new PORT;
- if(!pNew){
- return FALSE;
- }
- pNew->pNext=m_pFirst;
- pNew->dwStatus=dwStatus;
- pNew->hFile=0;
- pNew->dwJobID=0;
- pNew->dither = dither;
- _tcscpy(pNew->img_format,img_format);
- _tcscpy(pNew->img_format_ext,img_format_ext);
- _tcscpy(pNew->extApp,extApp);
- _tcscpy(pNew->cPath,cPath);
- _tcscpy(pNew->sPath,sPath);
- _tcscpy(pNew->sCurDocument,_T(""));
- _tcscpy(pNew->sPrinter,_T(""));
- m_pFirst=pNew;
- return TRUE;
- }
- void CPortList::Save(TCHAR *sRoot)
- {
- HKEY hKey;
- if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,sRoot,0,KEY_SET_VALUE | KEY_READ,&hKey) != ERROR_SUCCESS)
- return;
- DWORD z;
- TCHAR sPort[5];
- z=1;
- do{
- _ultot(z++,sPort,10);
- }while(RegDeleteValue(hKey,sPort)==ERROR_SUCCESS);
- PORT *pPort=m_pFirst;
- z=Count();
- while(pPort->pNext){
- _ultot(z--,sPort,10);
- RegSetValueEx(hKey,_T("path"),0,REG_SZ,(BYTE*)pPort->cPath,_tcslen(pPort->cPath)*sizeof(TCHAR));
- RegSetValueEx(hKey,_T("name"),0,REG_SZ,(BYTE*)_T("ImagePrinter Port"),_tcslen(_T("ImagePrinter Port"))*sizeof(TCHAR));
- RegSetValueEx(hKey,_T("format"),0,REG_SZ,(BYTE*)_T("tif"),_tcslen(_T("tif"))*sizeof(TCHAR));
- pPort=pPort->pNext;
- }
- RegCloseKey(hKey);
- }
- #define szLocalMonitor _T("ImagePrinter Port")
- void CPortList::Load2(PMONITORINIT pMi)
- {
- if( NULL != pMi )
- {
- HANDLE phkResult = NULL;
- TCHAR sPath[MAX_PATH]=_T("path");
- TCHAR sFormat[MAX_PATH]=_T("format");
- TCHAR sFormatEx[MAX_PATH]=_T("format_ext");
- TCHAR extApp[MAX_PATH] = _T("ext_app");
- DWORD dither = 0;
- DWORD dwSize;
- LONG ret;
- if( ERROR_SUCCESS != (ret = pMi->pMonitorReg->fpOpenKey( pMi->hckRegistryRoot, szLocalMonitor, KEY_READ, &phkResult, pMi->hSpooler ) ) )
- return;
- dwSize=sizeof(dither);
- if( ERROR_SUCCESS != (ret = pMi->pMonitorReg->fpQueryValue( phkResult, _T("dither"), 0, (BYTE*)&dither, &dwSize, pMi->hSpooler ) ) )
- {
- dither = 0;
- ret = ERROR_SUCCESS;
- }
- dwSize=sizeof(extApp);
- if( ERROR_SUCCESS != (ret = pMi->pMonitorReg->fpQueryValue( phkResult, extApp, 0, (BYTE*)extApp, &dwSize, pMi->hSpooler) ) )
- {
- _tcscpy(extApp,_T(""));
- ret = ERROR_SUCCESS;
- }
- dwSize=sizeof(sFormatEx);
- if( ERROR_SUCCESS != (ret = pMi->pMonitorReg->fpQueryValue( phkResult, sFormatEx, 0, (BYTE*)sFormatEx, &dwSize, pMi->hSpooler) ) )
- {
- _tcscpy(sFormatEx,_T(""));
- ret = ERROR_SUCCESS;
- }
- dwSize = sizeof(sPath);
- ret = pMi->pMonitorReg->fpQueryValue( phkResult, sPath, 0, (BYTE*)sPath, &dwSize, pMi->hSpooler );
- dwSize = sizeof(sFormat);
- if( ret == ERROR_SUCCESS )
- ret = pMi->pMonitorReg->fpQueryValue( phkResult, sFormat, 0, (BYTE*)sFormat, &dwSize, pMi->hSpooler );
- if( ret==ERROR_SUCCESS )
- AddPort( sFormat, sPath, _T("ImagePrinter Port"), 0, sFormatEx, (0!=dither), extApp);
- pMi->pMonitorReg->fpCloseKey( phkResult, pMi->hSpooler );
- }
- }
- void CPortList::Load(TCHAR *sRoot)
- {
- HKEY hKey;
- if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,sRoot,0,KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS){
- return;
- }
- TCHAR sPath[MAX_PATH]=_T("path");
- TCHAR sFormat[MAX_PATH]=_T("format");
- TCHAR sFormatEx[MAX_PATH]=_T("format_ext");
- TCHAR extApp[MAX_PATH] = _T("ext_app");
- DWORD dither = 0;
- DWORD dwSize;
- LONG ret;
- dwSize=sizeof(dither);
- if( ERROR_SUCCESS != (ret=RegQueryValueEx(hKey,_T("dither"),0,NULL,(BYTE*)&dither,&dwSize) ) )
- {
- dither = 0;
- ret = ERROR_SUCCESS;
- }
- dwSize=sizeof(extApp);
- if( ERROR_SUCCESS != (ret=RegQueryValueEx(hKey,extApp,0,NULL,(BYTE*)extApp,&dwSize) ) )
- {
- _tcscpy(extApp,_T(""));
- ret = ERROR_SUCCESS;
- }
- dwSize=sizeof(sFormatEx);
- if( ERROR_SUCCESS != (ret=RegQueryValueEx(hKey,sFormatEx,0,NULL,(BYTE*)sFormatEx,&dwSize) ) )
- {
- _tcscpy(sFormatEx,_T(""));
- ret = ERROR_SUCCESS;
- }
- dwSize=sizeof(sPath);
- ret=RegQueryValueEx(hKey,sPath,0,NULL,(BYTE*)sPath,&dwSize);
- dwSize=sizeof(sFormat);
- if(ret==ERROR_SUCCESS)
- ret=RegQueryValueEx(hKey,sFormat,0,NULL,(BYTE*)sFormat,&dwSize);
- if(ret==ERROR_SUCCESS){
- AddPort( sFormat, sPath, _T("ImagePrinter Port"), 0, sFormatEx, (0!=dither), extApp);
- }
- RegCloseKey(hKey);
- }
- PORT *CPortList::FindPort(TCHAR *sPath)
- {
- PORT *pPort=m_pFirst;
- while(pPort->pNext){
- if(_tcsicmp(sPath,pPort->sPath)==0)
- return pPort;
- pPort=pPort->pNext;
- }
- return NULL;
- }
- BOOL CPortList::DeletePort(TCHAR *sPath)
- {
- PORT *pPort=m_pFirst;
- PORT *pPrevPort=m_pFirst;
- while(pPort->pNext) {
- if(_tcsicmp(sPath,pPort->sPath)==0) {
- if(pPort==m_pFirst) {
- m_pFirst=m_pFirst->pNext;
- delete pPort;
- }
- else{
- pPrevPort->pNext=pPort->pNext;
- delete pPort;
- }
- return TRUE;
- }
- pPrevPort=pPort;
- pPort=pPort->pNext;
- }
- return FALSE;
- }
- DWORD CPortList::Count()
- {
- DWORD dwCount=0;
- PORT *pPort=m_pFirst;
- while(pPort->pNext){
- ++dwCount;
- pPort=pPort->pNext;
- }
- return dwCount;
- }