15,471
社区成员
发帖
与我相关
我的任务
分享
{
//文档名
CString documentName;
DWORD dStatus;
//序号
int dindex;
};
//获取打印队列详细信息 返回vector
vector<CPrintDealSrv::PRINTER_JOB_INFO>* CPrintDealSrv::GetPrintJobList(void)
{
vector<PRINTER_JOB_INFO>* vlist = new vector<PRINTER_JOB_INFO>;
CString strPrinterName = _T("Microsoft XPS Document Writer");
HANDLE hPrintHandle;
LPTSTR lpPrinerName = strPrinterName.GetBuffer();
strPrinterName.ReleaseBuffer();
JOB_INFO_2 *pJobs;
int cJobs;
DWORD dwPrinterStatus;
PRINTER_JOB_INFO jobSimpleInfo;
jobSimpleInfo.documentName = pJobs[i].pDocument;
jobSimpleInfo.dindex = (i+1);
jobSimpleInfo.dStatus = pJobs[i].Status;
vlist->push_back(jobSimpleInfo);
return vlist;
}
//线程方法
UINT CPrintDealSrv::thStartPrintSrv(LPVOID lparm)
{
CPrintDealSrv * pPrintDeal = (CPrintDealSrv*)lparm;
vector<PRINTER_JOB_INFO>* vlist;
while(true)
{
vlist = pPrintDeal->GetPrintJobList();
Sleep(10);
delete vlist;
}
}
//获取成功
else{
if (cJobs > 0)
{
//队列中有待打印文档
for (int i = 0;i < cJobs; i ++)
{
PRINTER_JOB_INFO jobSimpleInfo;
jobSimpleInfo.documentName = pJobs[i].pDocument;
jobSimpleInfo.dindex = (i+1);
jobSimpleInfo.dStatus = pJobs[i].Status;
vlistPrintJobInfo->push_back(jobSimpleInfo);
}
}
//释放内存
free pJobs;
}
//获取打印队列详细信息
vector<CPrintDealSrv::PRINTER_JOB_INFO>* CPrintDealSrv::GetPrintJobList(void)
{
vlistPrintJobInfo = new vector<PRINTER_JOB_INFO>;
//打印机名称
CString strPrinterName = _T("Microsoft XPS Document Writer");
HANDLE hPrintHandle;
LPTSTR lpPrinerName = strPrinterName.GetBuffer();
strPrinterName.ReleaseBuffer();
if(OpenPrinter(lpPrinerName,&hPrintHandle,NULL))
{
unsigned char buf[8192];
DWORD dwSize;
if(GetPrinter(hPrintHandle, 2, buf, sizeof(buf),&dwSize))
{
PRINTER_INFO_2* pInfo;
pInfo = (PRINTER_INFO_2*)buf;
if(pInfo->Status == PRINTER_STATUS_PAUSED)//暂停
AfxMessageBox(L"##打印机暂停");
else if(pInfo->Status == PRINTER_STATUS_PENDING_DELETION)//等待删除
AfxMessageBox(L"##打印机删除中");
if(pInfo->Attributes& PRINTER_ATTRIBUTE_WORK_OFFLINE)
AfxMessageBox(L"##打印机离线!");
else
;//在线
}
}
JOB_INFO_2 *pJobs;
int cJobs;
DWORD dwPrinterStatus;
if(!GetPrintJobs(hPrintHandle,&pJobs,&cJobs,&dwPrinterStatus))
{
ClosePrinter(hPrintHandle);
return vlistPrintJobInfo;
}
//获取成功
else{
if (cJobs > 0)
{
//队列中有待打印文档
for (int i = 0;i < cJobs; i ++)
{
PRINTER_JOB_INFO jobSimpleInfo;
jobSimpleInfo.documentName = pJobs[i].pDocument;
jobSimpleInfo.dindex = (i+1);
jobSimpleInfo.dStatus = pJobs[i].Status;
vlistPrintJobInfo->push_back(jobSimpleInfo);
}
}
}
return vlistPrintJobInfo;
}
// 获取系统打印信息
BOOL CPrintDealSrv::GetPrintJobs(HANDLE hPrinter, JOB_INFO_2 ** ppJobInfo, int* jobCount, DWORD * pStatus)
{
DWORD cByteNeeded,
nReturned,
cByteUsed;
JOB_INFO_2 *pJobStorage = NULL;
PRINTER_INFO_2 *pPrinterInfo = NULL;
//获取缓冲区大小
if(!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)//缓冲区太小
{
return FALSE;
}
}
//分配所需的内存
pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
if(!pPrinterInfo)
{
return FALSE; //分配内存失败!
}
//获取打印机信息
if(!GetPrinter(hPrinter, 2, (LPBYTE)pPrinterInfo, cByteNeeded, &cByteUsed))
{
free(pPrinterInfo);
pPrinterInfo = NULL;
ASSERT(FALSE);//没有权限连接打印机
return FALSE;
}
//获取打印工作队列的存储空间
if (!EnumJobs(hPrinter, 0, pPrinterInfo->cJobs, 2, NULL, 0,
(LPDWORD)&cByteNeeded, (LPDWORD)&nReturned))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)//缓冲区太小
{
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
}
pJobStorage = (JOB_INFO_2 *)malloc(cByteNeeded);
if (!pJobStorage)//分配打印工作空间失败
{
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
ZeroMemory(pJobStorage, cByteNeeded);
//获取打印工作队列
if(!EnumJobs(hPrinter,0,pPrinterInfo->cJobs,2,(LPBYTE)pJobStorage,
cByteNeeded,(LPDWORD)&cByteUsed,(LPDWORD)&nReturned))
{
free(pPrinterInfo);
free(pJobStorage);
pJobStorage = NULL;
pPrinterInfo = NULL;
return FALSE;
}
/*
* Return the information.
*/
*jobCount = nReturned;
*pStatus = pPrinterInfo->Status;
*ppJobInfo = pJobStorage;
free(pPrinterInfo);
return TRUE;
}