Mega Code Archive

 
Categories / Delphi / Printing
 

Get the Number of Printer Jobs

Title: Get the Number of Printer Jobs Question: How to get the number of jobs in the windows spooler for a specific printer. You may want your Application to limit the number of jobs in the spooler to avoid the loss of data. Answer: uses Windows, SysUtils, Printers, WinSpool; {************************************************************************** * NAME: PrinterJobsCount * DESC: Get the number of jobs in the windows printer spooler * PARAMS: PrinterName * RESULT: Number of printer jobs * CREATED: 27-05-2002/Andreas Schmidt *************************************************************************} function PrinterJobsCount(const PrinterName : String) : Integer; var hPrinter : THandle; PPI2 : PPrinterInfo2; dwNeeded : DWORD; begin Result := -1; // open the printer if NOT OpenPrinter(PChar(PrinterName), hPrinter, Nil) then raise EPrinter.Create('Can''t open printer: '+PrinterName+ #13+SysErrorMessage(GetLastError)); try // just get the amount of needed memory GetPrinter(hPrinter, 2, Nil, 0, @dwNeeded); // allocate the memory GetMem(PPI2, dwNeeded); try // get the printer information if NOT GetPrinter( hPrinter, // handle to printer 2, // information level PPI2, // printer information buffer dwNeeded, // size of buffer @dwNeeded // bytes received or required ) then raise EPrinter.Create('Error getting information for printer: '+ PrinterName+#13+SysErrorMessage(GetLastError)); Result := PPI2^.cJobs; finally FreeMem(PPI2); end; finally ClosePrinter(hPrinter); end; end;