📄 portlist.cpp
字号:
// PortList.cpp
//
//////////////////////////////////////////////////////////////////////
#include "PortList.h"
#include "logger.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
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->hPrinterPhysics=0;
_tcscpy(m_pFirst->sPrinterPhysics,_T(""));
_tcscpy(m_pFirst->sUser,_T(""));
//孙刚增加结束
_tcscpy(m_pFirst->sPath,_T(""));
_tcscpy(m_pFirst->sCurDocument,_T(""));
_tcscpy(m_pFirst->sPrinterLocalDirectory,_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;
}//switch
(*pcReturned)++;
pPort=pPort->pNext;
}//while 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("错误"),_T("非法级别!"),MB_OK);
}//switch
return pEnd;
}
BOOL CPortList::AddPort(TCHAR *sPath, TCHAR*sPrinterPhysics,DWORD dwStatus)
{
PORT *pNew=new PORT;
if(!pNew)
return FALSE;
pNew->pNext=m_pFirst;
pNew->dwStatus=dwStatus;
pNew->hFile=0;
pNew->dwJobID=0;
//孙刚增加:
_tcscpy(pNew->sPrinterPhysics,sPrinterPhysics);
_tcscpy(pNew->sUser,_T(""));
pNew->hPrinterPhysics=NULL;
pNew->dwJobIdPhysics=0;
//孙刚增加结束
_tcscpy(pNew->sPath,sPath);
_tcscpy(pNew->sCurDocument,_T(""));
_tcscpy(pNew->sPrinterLocalDirectory,_T(""));
m_pFirst=pNew;
return TRUE;
}
void CPortList::Save(TCHAR *sRoot)
{
HKEY hKey;
// Registry-Key oeffnen
if(RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
sRoot,
0,
KEY_SET_VALUE | KEY_READ,
&hKey) != ERROR_SUCCESS)
return;
// Alle vorhandenen Ports loeschen
DWORD z;
TCHAR sPort[15];
TCHAR sPortPrinterPhysics[15];
z=1;
BOOL haveMore=true;
while(haveMore)
{
_ultot(z,sPort,10);
haveMore=(RegDeleteValue(hKey,sPort)==ERROR_SUCCESS);
_ultot(z,sPortPrinterPhysics,10);
_tcscat(sPortPrinterPhysics,_T("P"));
haveMore=haveMore||(ERROR_SUCCESS==RegDeleteValue(hKey,sPortPrinterPhysics));
z++;
}
// Ports schreiben
PORT *pPort=m_pFirst;
z=Count();
while(pPort->pNext)
{
_ultot(z,sPort,10);
RegSetValueEx(
hKey,
sPort, //value name
0, //reserved
REG_SZ, //typ
(BYTE*)pPort->sPath, //value
_tcslen(pPort->sPath)*sizeof(TCHAR));
//孙刚增加:
_ultot(z,sPortPrinterPhysics,10);
_tcscat(sPortPrinterPhysics,_T("P"));
//z--;
RegSetValueEx(
hKey,
sPortPrinterPhysics, //value name
0, //reserved
REG_SZ, //typ
(BYTE*)pPort->sPrinterPhysics, //value
_tcslen(pPort->sPrinterPhysics)*sizeof(TCHAR));
z--;
//孙刚增加结束
pPort=pPort->pNext;
}
// Registry-Key schliessen
RegCloseKey(hKey);
}
void CPortList::Load(TCHAR *sRoot)
{
TCHAR sPath[MAX_PATH];
TCHAR sPort[5];
DWORD dwSize;
TCHAR s[MAX_PATH];
TCHAR s1[200];
HKEY hKey;
// Registry-Key oeffnen
if(RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
sRoot,
0,
KEY_QUERY_VALUE,
&hKey) != ERROR_SUCCESS)
{
_tcscpy(s,_T("打开注册表键"));
_tcscat(s,sRoot);
_tcscat(s,_T("错误"));
_tcscat(s,formatLastError(s1));
::Error(s);
return;
}
// Pfade der Ports lesen
LONG retPath=ERROR_SUCCESS;
LONG retPrinter=ERROR_SUCCESS;
DWORD z=1;
while(retPath==ERROR_SUCCESS||retPrinter==ERROR_SUCCESS)
{
if(z>1000)break;
_ultot(z,sPort,10);
sPath[0]=0;
dwSize=sizeof(sPath);
retPath=RegQueryValueEx(
hKey,
sPort, //value name
0, //reserved
NULL, //Typ
(BYTE*)sPath, //value
&dwSize); //buffer size
//孙刚增加:
TCHAR sPrinterPhysics[MAX_PATH];
sPrinterPhysics[0]=0;
TCHAR sPortPrinterPhysics[5];
_ultot(z,sPortPrinterPhysics,10);
_tcscat(sPortPrinterPhysics,_T("P"));
dwSize=sizeof(sPrinterPhysics);
// ::MessageBox(NULL,sPortPrinterPhysics,sPortPrinterPhysics,MB_OK);
retPrinter=RegQueryValueEx(
hKey,
sPortPrinterPhysics, //value name
0, //reserved
NULL, //Typ
(BYTE*)sPrinterPhysics, //value
&dwSize); //buffer size
if(retPrinter==ERROR_SUCCESS&&retPrinter!=retPath)
{
_tcscpy(s,_T("取"));
_tcscat(s,sPort);
_tcscat(s,_T("号代理端口的保存路径错误"));
_tcscat(s,formatLastError(s1,retPath));
::Error(s);
}
else if(retPath==ERROR_SUCCESS&&retPrinter!=retPath)
{
_tcscpy(s,_T("取"));
_tcscat(s,sPortPrinterPhysics);
_tcscat(s,_T("号代理端口的物理打印机错误"));
_tcscat(s,formatLastError(s1,retPrinter));
::Error(s);
}
else if(retPath!=ERROR_SUCCESS)
{
_tcscpy(s,_T("取"));
_tcscat(s,sPortPrinterPhysics);
_tcscat(s,_T("号代理端口的物理打印机错误"));
_tcscat(s,formatLastError(s1,retPrinter));
::Error(s);
}
// ::MessageBox(NULL,sPrinterPhysics,sPrinterPhysics,MB_OK);
//孙刚结束
if((sPath[0]!=0)&&(sPrinterPhysics[0]!=0))
{
//孙刚修改:
AddPort(sPath,sPrinterPhysics,0);
//孙刚修改结束
}
z++;
}
// Registry-Key schliessen
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -