clamchoda
clamchoda

Reputation: 4941

Using ManagementObjectSearcher and Win32_Printer to check Status

I have been forced to use the RawPrinterHelper class to print to a thermal printer because the PrintDocument method has proven unfit for POS printing.

I now need to check the status of the printer prior to printing to be sure that it is, online and ready to print. I have been able to successful check attributes from Win32_Printer. I can see properties such as PrinterStatus, changing from 3, to a 2 or a 1 when out of paper or tray is open. This is great.

My question is, which properties should indicate it is O.K. to print? There must be more than just checking if PrinterStatus is idle (3).

    private bool ReadyCheck(string printerName)
    {
        bool ready = false;
        string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);

        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        using (ManagementObjectCollection coll = searcher.Get())
        {
            try
            {
                foreach (ManagementObject printer in coll)
                {
                    // Print status = 3, idle and ready to print
                    string status = printer.Properties["PrinterStatus"].Value.ToString();
                    string extendedPrinterStatus = printer.Properties["ExtendedPrinterStatus"].Value.ToString();

                    // What else means printer is ready?
                    if (status.Trim() == "3")
                        ready = true;

                }
            }
            catch (ManagementException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        return ready;

    }

Edit: @vendettamit Sorry I didn't ask very well. For instance, Idle (3) indicates it's OK to print. I am wondering if there are more which also indicate it's OK to print such as; Printing (4), WarmUp (5), ect, or is Idle (3) the only time you should send the next print job? Thanks

Upvotes: 2

Views: 11613

Answers (2)

Milan Madubasha
Milan Madubasha

Reputation: 641

**//check printer is online**
private static bool IsOnline(ManagementBaseObject printer)
        {
            bool isOnlineprinter = true;
            PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
            var PrinterName = printerNative.GetPrinterName();
            var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();
            var ResultPrinter01 = printer.Properties["ExtendedPrinterStatus"].Value.ToString();
            var ResultPrinter02 = printer.Properties["PrinterState"].Value.ToString();

            if (PrinterNameProperty == PrinterName)
            {
                //(no internet connection or printer switched off):PrinterState
                if (ResultPrinter02 == "128"|| ResultPrinter02=="4096")
                {
                    isOnlineprinter = false;
                }



                ////printer is initializing....
                //if (ResultPrinter02 == "16")
                //{
                //    isOnlineprinter = false;
                //}

                //(no internet connection or printer switched off):ExtendedPrinterStatus
                if (ResultPrinter01 == "7")
                {
                    isOnlineprinter = false;
                }

            }

            return isOnlineprinter;
         }
         
         
         
         
  **//check for out of paper**
private static bool IspaperOK(ManagementBaseObject printer)
        {
            bool PaperOK = true;
            
            PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
            var PrinterName = printerNative.GetPrinterName();
            var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();

            var PaperStatus = printer.Properties["PrinterState"].Value.ToString();
            
            if (PrinterNameProperty == PrinterName)
            {
                //(PrinterState)16 = Out of Paper
                //(PrinterState)5 = Out of paper
                //(PrinterState)4 = paperjam
                //(PrinterState)144 = Out of paper
                if ((PaperStatus == "5") || (PaperStatus == "16")||(PaperStatus=="144"))
                {
                    PaperOK = false;
                }

            }

            return PaperOK;
        }
        
        
        
**//Verify still printing state or not**
private static bool Isprinting(ManagementBaseObject printer)
        {
            bool Isprintingnow = false;
            PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
            var PrinterName = printerNative.GetPrinterName();
            var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();
            var printing01 = printer.Properties["PrinterState"].Value.ToString();
            var printing02 = printer.Properties["PrinterStatus"].Value.ToString();
            if (PrinterNameProperty == PrinterName)
            {
                //(PrinterState)11 = Printing
                //(PrinterState)1024 = printing
                //(PrinterStatus)4 = printing
                if (printing01 == "11" || printing01 == "1024" || printing02=="4")
                {
                    Isprintingnow = true;
                }

            }

            return Isprintingnow;
        }

**//check for error (Printer)**
 private static bool IsPrinterError(ManagementBaseObject printer)
        {
            bool PrinterOK = true;

            PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
            var PrinterName = printerNative.GetPrinterName();
            var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();

            var PrinterSpecificError = printer.Properties["PrinterState"].Value.ToString();
            var otherError = printer.Properties["ExtendedPrinterStatus"].Value.ToString();
            if (PrinterNameProperty == PrinterName)
            {
                //(PrinterState)2 - error of printer
                //(PrinterState)131072 - Toner Low
                //(PrinterState)18 - Toner Low
                //(PrinterState)19 - No Toner

                if ((PrinterSpecificError == "131072")||(PrinterSpecificError == "18")||(PrinterSpecificError == "19")||(PrinterSpecificError == "2")||(PrinterSpecificError == "7"))
                {
                    PrinterOK = false;
                }

                //(ExtendedPrinterStatus) 2 - no error
                if (otherError=="2")
                {
                    PrinterOK = true;
                }
                else
                {
                    PrinterOK = false;
                }

            }
            return PrinterOK;
        }

    **//check Network or USB**
private static bool IsNetworkPrinter(ManagementBaseObject printer)
    {
        bool IsNetwork = true;
        PrinterNative.PrinterNative.PrinterNative printerNative = new PrinterNative.PrinterNative.PrinterNative();
        var PrinterName = printerNative.GetPrinterName();
        var PrinterNameProperty = printer.Properties["DeviceId"].Value.ToString();
      
        var network = printer.Properties["Network"].Value.ToString();                   
        var local = printer.Properties["Local"].Value.ToString();

                    if (PrinterNameProperty == PrinterName)
                    {
                        if (network == "True")
                        {
                            IsNetwork = true;
                        }

                        if (network == "True" && local == "True")
                        {
                            IsNetwork = true;
                        }

                        if (local == "True" && network=="False")
                        {
                            IsNetwork = false;
                        }
                    }

        return IsNetwork;
    }

         

            //(PrinterState)16 = Out of Paper
            //(PrinterState)5 = Out of paper
            //(PrinterState)4 = paperjam
            //(PrinterState)144 = Out of paper
            //(PrinterState)4194432 = Lid Open        
            //(PrinterState)4194448 = Out of paper/Lid open 
            //(PrinterState)4096= Offline   
            //(PrinterState)1024= Printing
            //(PrinterState)128= Printer is offline

Upvotes: 2

vendettamit
vendettamit

Reputation: 14677

Below are the values that you can take into account to check if It's ready to print:

Other (1)
Unknown (2)
Idle (3) 
Printing (4)
Warmup (5)
Stopped Printing (6)
Offline (7)

Also a note from Remarks on MSDN documentation for Win32_Printer-

If you are retrieving PrinterStatus = 3 or PrinterState = 0, the printer driver may not be feeding accurate information into WMI. WMI retrieves the printer information from the spoolsv.exe process. It is possible the printer driver does not report its status to the spooler. In this case, Win32_Printer reports the printer as Idle.

Upvotes: 2

Related Questions