獲取印表機連線狀態及列印任務是否錯誤
阿新 • • 發佈:2019-02-19
HANDLE PrnHandle;
if (OpenPrinter("EPSON Stylus Photo R270 Series",&PrnHandle,NULL))
{
unsigned char buf[8192];
DWORD dwSize;
if (GetPrinter(PrnHandle,2,buf,sizeof(buf),
&dwSize)) {
PRINTER_INFO_2* pInfo;
pInfo = (PRINTER_INFO_2*)buf;
//pInfo->Status 就是印表機的狀態,詳細的程式碼可以
//參見winspool.h中以PRINTER_STATUS開頭的巨集定義
if(pInfo->Status==PRINTER_STATUS_PAUSED)
AfxMessageBox("a");
else if(pInfo->Status==PRINTER_STATUS_PENDING_DELETION)
AfxMessageBox("b");
///////////以上pInfo->Status程式碼試驗不成功,哪位知道原因請告知,謝謝
if (pInfo->Attributes&PRINTER_ATTRIBUTE_WORK_OFFLINE)//測試成功
{
AfxMessageBox("offline");
}
else
{
AfxMessageBox("online");
}
}
ClosePrinter(PrnHandle);
}
上面是判斷印表機是否連機的。
下面是判斷列印任務是否正常完成的:
SetTimer(1,500,NULL);//用定時器來完成
響應函式:
switch(nIDEvent)
{
case 1:
{
JOB_INFO_2 *pJobs;
int cJobs,
i;
DWORD dwPrinterStatus;
if (!GetJobs(hPrinter, &pJobs, &cJobs, &dwPrinterStatus))
return ;
for (i=0; i < cJobs; i++)
{
if (pJobs[i].Status &
(JOB_STATUS_ERROR |
JOB_STATUS_OFFLINE |
JOB_STATUS_PAPEROUT |
JOB_STATUS_BLOCKED_DEVQ))
{
KillTimer(1);
AfxMessageBox(pJobs[i].pDocument);
free( pJobs );
return ;
}
}
free( pJobs );
}
break;
default:
break;
}
用到的方法:
BOOL GetJobs(HANDLE hPrinter, /* Handle to the printer. */
JOB_INFO_2 **ppJobInfo, /* Pointer to be filled. */
int *pcJobs, /* Count of jobs filled. */
DWORD *pStatus) /* Print Queue status. */
{
DWORD cByteNeeded,
nReturned,
cByteUsed;
JOB_INFO_2 *pJobStorage = NULL;
PRINTER_INFO_2 *pPrinterInfo = NULL;
/* Get the buffer size needed. */
if (!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return FALSE;
}
pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
if (!(pPrinterInfo))
/* Failure to allocate memory. */
return FALSE;
LPCSTR str="sssssssssss";
/* Get the printer information. */
if (!GetPrinter(hPrinter,2,(unsigned char *)(LPSTR)pPrinterInfo,cByteNeeded,&cByteUsed))
{
/* Failure to access the printer. */
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
/* Get job storage space. */
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)
{
/* Failure to allocate Job storage space. */
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
ZeroMemory(pJobStorage, cByteNeeded);
/* Get the list of jobs. */
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.
*/
*pcJobs = nReturned;
*pStatus = pPrinterInfo->Status;
*ppJobInfo = pJobStorage;
free(pPrinterInfo);
return TRUE;
}
需要插入標頭檔案#include <winspool.h>
unsigned char buf[8192];
DWORD dwSize;
if (GetPrinter(PrnHandle,2,buf,sizeof(buf),
&dwSize)) {
PRINTER_INFO_2* pInfo;
pInfo = (PRINTER_INFO_2*)buf;
//pInfo->Status 就是印表機的狀態,詳細的程式碼可以
//參見winspool.h中以PRINTER_STATUS開頭的巨集定義
if(pInfo->Status==PRINTER_STATUS_PAUSED)
AfxMessageBox("a");
else if(pInfo->Status==PRINTER_STATUS_PENDING_DELETION)
AfxMessageBox("b");
///////////以上pInfo->Status程式碼試驗不成功,哪位知道原因請告知,謝謝
if (pInfo->Attributes&PRINTER_ATTRIBUTE_WORK_OFFLINE)//測試成功
{
AfxMessageBox("offline");
}
else
{
AfxMessageBox("online");
}
}
ClosePrinter(PrnHandle);
}
上面是判斷印表機是否連機的。
下面是判斷列印任務是否正常完成的:
SetTimer(1,500,NULL);//用定時器來完成
響應函式:
switch(nIDEvent)
{
case 1:
{
JOB_INFO_2 *pJobs;
int cJobs,
i;
DWORD dwPrinterStatus;
if (!GetJobs(hPrinter, &pJobs, &cJobs, &dwPrinterStatus))
return ;
for (i=0; i < cJobs; i++)
{
if (pJobs[i].Status &
(JOB_STATUS_ERROR |
JOB_STATUS_OFFLINE |
JOB_STATUS_PAPEROUT |
JOB_STATUS_BLOCKED_DEVQ))
{
KillTimer(1);
AfxMessageBox(pJobs[i].pDocument);
free( pJobs );
return ;
}
}
free( pJobs );
}
break;
default:
break;
}
用到的方法:
BOOL GetJobs(HANDLE hPrinter, /* Handle to the printer. */
JOB_INFO_2 **ppJobInfo, /* Pointer to be filled. */
int *pcJobs, /* Count of jobs filled. */
DWORD *pStatus) /* Print Queue status. */
{
DWORD cByteNeeded,
nReturned,
cByteUsed;
JOB_INFO_2 *pJobStorage = NULL;
PRINTER_INFO_2 *pPrinterInfo = NULL;
/* Get the buffer size needed. */
if (!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return FALSE;
}
pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
if (!(pPrinterInfo))
/* Failure to allocate memory. */
return FALSE;
LPCSTR str="sssssssssss";
/* Get the printer information. */
if (!GetPrinter(hPrinter,2,(unsigned char *)(LPSTR)pPrinterInfo,cByteNeeded,&cByteUsed))
{
/* Failure to access the printer. */
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
/* Get job storage space. */
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)
{
/* Failure to allocate Job storage space. */
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
ZeroMemory(pJobStorage, cByteNeeded);
/* Get the list of jobs. */
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.
*/
*pcJobs = nReturned;
*pStatus = pPrinterInfo->Status;
*ppJobInfo = pJobStorage;
free(pPrinterInfo);
return TRUE;
}
需要插入標頭檔案#include <winspool.h>