posts - 13,  comments - 16,  trackbacks - 0
    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>

posted on 2009-07-15 17:49 似水之心 阅读(5875) 评论(3)  编辑 收藏 引用

FeedBack:
# re: 获取打印机连接状态及打印任务是否错误
2009-07-16 18:57 | 凡客诚品
来分享下  回复  更多评论
  
# re: 获取打印机连接状态及打印任务是否错误
2009-09-10 09:35 | vanlin
始终不好用,,   回复  更多评论
  
# re: 获取打印机连接状态及打印任务是否错误
2010-11-18 16:52 | dahai
isok=GetPrinter(PrnHandle,2,NULL,NULL,&dwSize);
if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER)
{
ClosePrinter(PrnHandle);
return false;
}

在你的GetPrinter函数前面再加上这些!  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2009年9月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

留言簿(2)

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜