MFC中获取指定打印机的打印队列的方法

void CPrintListTestDlg::OnBnClickedButton1()
{
    GetDlgItem(IDC_TxtList)->SetWindowText(L"");
    HANDLE   PrnHandle;   
    UpdateData(TRUE);
    LPTSTR strPrinter = m_StrPrinterName.GetBuffer();//如果网络打印机,则格式为\\Xp-pcname\CanonLBP or \\192.168.1.x\\PrinterName
    m_StrPrinterName.ReleaseBuffer(); 
    if   (OpenPrinter(strPrinter,&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) 
                    ;
                    //cout<<"a"<<endl;
                else if(pInfo->Status==PRINTER_STATUS_PENDING_DELETION)   
                    ;
                    //cout<<"b"<<endl;  
                ///////////以上pInfo->Status代码试验不成功,哪位知道原因请告知,谢谢
                if (pInfo->Attributes&PRINTER_ATTRIBUTE_WORK_OFFLINE)//测试成功
                {
                    AfxMessageBox(L"offline");                
                }
                else
                {
                    AfxMessageBox(L"online");
                }
        }   
        JOB_INFO_2  *pJobs;
        int         cJobs;
        DWORD       dwPrinterStatus;
        if (!GetJobs(PrnHandle, &pJobs, &cJobs, &dwPrinterStatus))
        {
            /*CString strPrint = pJobs->pPrinterName;
            cout<<pJobs->JobId<<"\n"<<strPrint<<endl;
            cout<<pJobs->pDocument<<endl;*/    
            ClosePrinter(PrnHandle);
            return;
        }
        else
        {
            //CString strPrint = pJobs->pPrinterName;
            if (cJobs > 0)
            {
                AppendText(IDC_TxtList, L"开始获取....");
                //    cout<<pJobs->JobId<<"\nPrint:"<<strPrint<<endl;

                CString strtemp ,strindex;
                for (int j = 0; j <cJobs; j ++)
                {
                    strindex.Format(L"%d", pJobs[j].JobId);
                    strtemp = pJobs[j].pDocument;
                    
                    strindex = L"打印文档"+strindex+L"\t";
                    strtemp = strindex + strtemp;

                    AppendText(IDC_TxtList,strtemp);
                }
            }
            else
                AppendText(IDC_TxtList, L"无打印队列");
        
        }
        ClosePrinter(PrnHandle);
    }
    else
    {
        CString strError;
        strError.Format(L"%d",GetLastError());
        strError = L"打开出错" + strError;
        AfxMessageBox(L"打开出错!");
    }

}
BOOL CPrintListTestDlg::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;

    /* Get the printer information. */ 
    if (!GetPrinter(hPrinter,
        2,
        (LPBYTE)pPrinterInfo,///////////////////////////lpstr
        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;
}

运行结果:

 

posted on 2013-02-16 17:02  火星大能猫  阅读(2045)  评论(0编辑  收藏  举报