关于EnumJobs...枚举打印机任务的API详细用法!急求。

解决方案 »

  1.   

    我找到了一个例子,大家可以一起看看:http://www.codeproject.com/vb/net/printwatchvbnet.asp?target=print%7CjobHow to monitor a printer queue from VB.NET
    By Merrion
      

  2.   

    bool CPrintList::OperateJob(int index)
    {
    HANDLE handle;
    JOB_INFO_2          *pJobStorage = NULL;
    PRINTER_INFO_2       *pPrinterInfo = NULL;
    DWORD  cByteNeeded,cByteUsed,nReturned; char buf[256];
    DWORD dw = 256;
    BOOL isadd = false; if (!m_bselected) return false;    isadd =GetDefaultPrinter(buf,&dw);
        if (!isadd) 
    return false ;
        if(!OpenPrinter(buf,&handle,NULL))
        {
              MessageBox(_T("Could not open printer"),_T("Error"),  MB_ICONINFORMATION);
              return false;
        }
    if (!GetPrinter(handle, 2, NULL, 0, &cByteNeeded))
    {
    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
                return false;
    }
    pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
           if (!(pPrinterInfo))
               return false;   /* Failure to allocate memory. */    if (!GetPrinter(handle, 2,(LPBYTE)pPrinterInfo, cByteNeeded, &cByteUsed))
           {
               /* Failure to access the printer. */ 
               free(pPrinterInfo);
               pPrinterInfo = NULL;
               return false;
           } if (!EnumJobs(handle, 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);  if (!EnumJobs(handle,0,pPrinterInfo->cJobs, 2,(LPBYTE)pJobStorage, cByteNeeded,(LPDWORD)&cByteUsed,(LPDWORD)&nReturned))
           {
               free(pPrinterInfo);
               free(pJobStorage);
               pJobStorage = NULL;
               pPrinterInfo = NULL;
               return false;
           }    if (!nReturned)   //打印队列中没有数据
       {
       free(pPrinterInfo);
               free(pJobStorage);
               pJobStorage = NULL;
               pPrinterInfo = NULL;
       return true; 
       } for (int i = 0; i < (int)nReturned; i++)

    if (m_selectjob.JobID == pJobStorage[i].JobId ) 
    {
    if (index == 0)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_PAUSE);
    else if (index == 1)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_RESTART);
    else if (index == 2)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_DELETE);
    else if (index == 3)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_RESUME);
    break;
    }
    }
            free(pPrinterInfo);
            free(pJobStorage);
    pPrinterInfo = NULL;
    pJobStorage = NULL;
    m_bselected  = false;
       return true;