Saturday, January 10, 2009

Printer status monitoring

One of the features I want my component to be able to support is the ability for applications to use it to monitor the status of printers and print jobs.  The easiest way to work out how to do this was to write a small application which gathered and displayed the required information. 

It turned out to be a fairly enjoyable little bit of work which provided a print monitoring class ready for inclusion in my component.

Obtaining printer status, for example detecting out of paper conditions, turned out to be trickier than just asking the printer how it's doing.

Printer status under Windows isn't really a regularly updated and accurate property on the printer itself and can't be reliably obtained by just querying the printer information.

The status of the printer is updated and made available when jobs are actively spooling and printing.

The solution was to iterate over the available printers, using the EnumPrinters API, and for each printer returned look at the information for queued jobs using the EnumJobs API.

Then it was just a case of inspecting information from both the printer (using the PRINTER_INFO_2 structure) and the jobs (using the JOB_INFO_2 structure).  Both of these structures contain status information but I found that the jobs reported more accurately.

The small test application I developed to work through the solution has turned out to be a handy little thing.  When started, it just sits quietly in the corner reporting how the printers it can see, including network attached printers, are doing and how many jobs are in each printer's queue.  So handy in fact that I may just add a little help file and throw it up on the website as a free user downloadable utility application.

No comments: