Just wondering if anyone has experience with reading the print status of windows printers.
I’ve obtained the data ok for status etc, but windows seems to only update the status when something is attempted to be printed. I’m wondering if anyone has ever invented some kind of trick where you sort of print something or fire something into the print queue to force the status check without actually printing out a physical page.
You should be able to see it in the “res” parameter here:
sModel : string;
h : nativeuint;
res : int64;
needed : dword;
if OpenPrinter(pchar(sModel), h, nil) then
if GetPrinter(h, 6, @res, 8, @needed) then
Seems to work reliably for me, in advance of printing. Res would contain flags, like PRINTER_STATUS_DOOR_OPEN, that can be interpreted as you require. It should all be in Windows and/or Printers unit.
Has it worked for you?
The following might be of value.
A handle obtained for a remote printer by a call to OpenPrinter for a remote printer accesses the printer through a local cache in the print spooler service. This cache isn’t real-time accurate. To obtain accurate data, replace the OpenPrinter call with OpenPrinter2 with pOptions.dwFlags set to PRINTER_OPTION_NO_CACHE. Note that only OpenPrinter2W is functional. The function returns a printer handle that uses other Printing API calls and bypasses the local cache. This method blocks while waiting for network communication with the remote printer, so it might not return immediately. How quickly this function returns depends on run-time factors such as network status, print server configuration, and printer driver implementation factors that are difficult to predict when writing an application. Calling this function from a thread that manages user interface interaction might make the application appear unresponsive.
If printer is remote, you could maybe extract its IP address, and ping it or something before using OpenPrinter2 (Might still block if turned on, but has some other obscure fault…)
Apologies to you both for the slow reply. Was a week of meetings last week…
@ap2021 When I looked deeper into why I wasn’t happy with the status being up-to-date it related to the cache concept that @Roger_Plant discussed in his post. Combining both your responses together will hopefully help me detect when a network printer is ‘offline’ but windows hasn’t actually received that information yet and the notes @Roger_Plant is making will help me ensure I allow for perhaps his version to be inside a ‘diagnose further’ function so as not to delay the initial status list by minutes whilst windows figures stuff out like the device isn’t there.
In our use case the customers are printing from a web application, but they have no visibility of the printer queue on the server that does the printing. Many support calls come in says ‘your print app is broken’ and actually its a network issue, printer offline/new IP address, out of paper etc.
Thanks so much to you both!