获取打印机连接状态及打印任务是否错误

获取打印机连接状态及打印任务是否错误
    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>

你可能感兴趣的:(获取打印机连接状态及打印任务是否错误)