Void cprintlisttestdlg: onbnclickedbutton1 () {getdlgitem (idc_txtlist)-> setwindowtext (L ""); handle prnhandle; updatedata (true); lptstr strprinter = Response (); // for a network printer, the format is \ XP-pcname \ canonlbor \ 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 (Bu F), & dwsize) {printer_info_2 * pinfo; pinfo = (printer_info_2 *) BUF; // pinfo-> Status indicates the printer status. For detailed code, see winspool. if (pinfo-> Status = printer_status_paused); // cout <"A" <Endl; else if (pinfo-> Status = printer_status_pending_deletion); // cout <"B" <Endl; //////// the above pinfo-> Status Code test fails. Which one knows the reason? Thank you if (pinfo-> attributes & printer_attribute_work_offline) // test successful {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 "start to get .... "); // 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 "printed document" + strindex + L "\ t"; strtemp = strindex + strtemp; appendtext (idc_txtlist, strtemp);} else appendtext (idc_txtlist, L "no print queue");} closeprinter (prnhandle);} else {cstring strerror; strerror. format (L "% d", getlasterror (); strerror = l "Open error" + strerror; afxmessagebox (L "Open error! ");}}
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;}
Running result: