WindowsPrinterInfoMBS.Status not working?

WindowsPrinterInfoMBS.Status returns a value of 0 for each WindowsPrinterInfoMBS in WindowsPrinterInfoMBS.LocalPrinters regardless of what the real status is.

I’m trying to found out if the printer for which I’ve stored a setup string is actually online and available. OpenPrinter(setupstring) crashes the whole app silently if the printer is not online.

If I unplug my target printer from the USB, the printer still comes up in WindowsPrinterInfoMBS.LocalPrinters, so I can’t use its presence in LocalPrinters to determine whether it’s actually online or not.

WindowsPrinterInfoMBS.Status looks like it should do exactly what I need but it always returns 0 for every printer.

Windows10 64-bit, Xojo 2021r2.1, MBS 220pr9

I just built my Windows Printer List with a new label to show status.
I have several printers on a PC and some show 0 and some 128 for status.

So let’s lookup PRINTER_INFO_2 structure:

and I found the constants:

#define PRINTER_STATUS_PAUSED            0x00000001
#define PRINTER_STATUS_ERROR             0x00000002
#define PRINTER_STATUS_PENDING_DELETION  0x00000004
#define PRINTER_STATUS_PAPER_JAM         0x00000008
#define PRINTER_STATUS_PAPER_OUT         0x00000010
#define PRINTER_STATUS_MANUAL_FEED       0x00000020
#define PRINTER_STATUS_PAPER_PROBLEM     0x00000040
#define PRINTER_STATUS_OFFLINE           0x00000080
#define PRINTER_STATUS_IO_ACTIVE         0x00000100
#define PRINTER_STATUS_BUSY              0x00000200
#define PRINTER_STATUS_PRINTING          0x00000400
#define PRINTER_STATUS_OUTPUT_BIN_FULL   0x00000800
#define PRINTER_STATUS_NOT_AVAILABLE     0x00001000
#define PRINTER_STATUS_WAITING           0x00002000
#define PRINTER_STATUS_PROCESSING        0x00004000
#define PRINTER_STATUS_INITIALIZING      0x00008000
#define PRINTER_STATUS_WARMING_UP        0x00010000
#define PRINTER_STATUS_TONER_LOW         0x00020000
#define PRINTER_STATUS_NO_TONER          0x00040000
#define PRINTER_STATUS_PAGE_PUNT         0x00080000
#define PRINTER_STATUS_USER_INTERVENTION 0x00100000
#define PRINTER_STATUS_OUT_OF_MEMORY     0x00200000
#define PRINTER_STATUS_DOOR_OPEN         0x00400000
#define PRINTER_STATUS_SERVER_UNKNOWN    0x00800000
#define PRINTER_STATUS_POWER_SAVE        0x01000000
#define PRINTER_STATUS_SERVER_OFFLINE    0x02000000
#define PRINTER_STATUS_DRIVER_UPDATE_NEEDED    0x04000000

So 128 is offline.

@Christian_Schmitz yes, I see the constants in the MBS documentation. But all of my printers return status of 0 regardless of whether they are online or offline or completely disconnected from the PC.

Strange, but sorry, I have no idea why this happens for you.

Try this, offline being provided to status depends on the driver so read it from attribute which checks against the usb connection status if the printer is usb. Other attributes can be found here if you search for PRINTER_ATTRIBUTE. Tested against my usb dymo labelwriter 450 on my desk here and it worked.

Const PRINTER_ATTRIBUTE_WORK_OFFLINE = &h00000400

Dim printers(-1) As WindowsPrinterInfoMBS = WindowsPrinterInfoMBS.LocalPrinters

For Each p As WindowsPrinterInfoMBS In printers
  Dim offline As Boolean = CType((p.AttributeFlags And PRINTER_ATTRIBUTE_WORK_OFFLINE), Boolean)
  system.debuglog(p.PrinterName + " Status=" + p.Status.ToString + " Attribute=" + p.AttributeFlags.ToString + " offline=" + offline.ToString)
Next
1 Like

Thanks, Julian, that works!

1 Like