///返回 JOB_INFO_1 指针,和任务记录数 pLength
JOB_INFO_1* CControlPrinterDlg::ListJobsArray(LPTSTR szPrinterName,LPDWORD pLength) //CControlPrinterDlg::你的类名 { HANDLE hPrinter; DWORD dwNeeded,dwReturned,i; JOB_INFO_1* pJobInfo; if(!OpenPrinter(szPrinterName,&hPrinter,NULL)) //打开打印机 { return NULL; } // if(!EnumJobs(hPrinter,0,0xFFFFFFFF,1,NULL,0,&dwNeeded,&dwReturned)) { if(GetLastError()!=ERROR_INSUFFICIENT_BUFFER) { ClosePrinter(hPrinter); return NULL; } } if((pJobInfo=(JOB_INFO_1*)malloc(dwNeeded))==NULL) { ClosePrinter(hPrinter); return NULL; } if(!EnumJobs(hPrinter,0,0xFFFFFFFF,1,(LPBYTE)pJobInfo,dwNeeded,&dwNeeded,&dwReturned)) { ClosePrinter(hPrinter); free(pJobInfo); return NULL; } ClosePrinter(hPrinter); *pLength=dwReturned; return pJobInfo; }
调用:
void CControlPrinterDlg::OnSelchangePrinters() { // TODO: Add your control notification handler code here int nIndex=m_listbox.GetCurSel(); CString strPrinterName; m_listBox_job.ResetContent(); if(nIndex!=CB_ERR) { m_listbox.GetText(nIndex,strPrinterName); if(!strPrinterName.IsEmpty()) { DWORD length; JOB_INFO_1* jobs=ListJobsArray((LPTSTR)(LPCSTR)strPrinterName,&length); //这里调用 if(jobs!=NULL) { for(int i=0;i<length;i++) { m_listBox_job.AddString(jobs[i].pDocument); } } free(jobs); //释放 } } }