本文介绍了如何获取网络打印机的状态? (我的意思是缺纸,卡纸等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从c ++更改为c#,今天早上我写下了以下代码,试图获取HP打印机的详细状态,但PRINTER_INFO_2.status始终为零,即使打印机没有有任何纸张。

I'm just changing from c++ to c#, and I wrote down the following code this morning trying to get the detailed status of a HP printer, but PRINTER_INFO_2.status is always zero even when the printer didn't have any paper.

代码:


    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool OpenPrinter(string printer, out IntPtr handle, ref structPrinterDefaults pDefault);
    [DllImport("winspool.drv")]
    public static extern bool ClosePrinter(IntPtr handle);
    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool GetPrinter(IntPtr handle, Int32 level, IntPtr buffer, Int32 size, out Int32 sizeNeeded);

    // it doesn't work!!
    public static int GetPrinterStatusInt(string PrinterName)
    {
      int intRet = 0;
      IntPtr hPrinter;

      structPrinterDefaults defaults = new structPrinterDefaults();

      if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
      {
        int cbNeeded = 0;
        bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
        if (cbNeeded > 0)
        {
          IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
          bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
          if (bolRet)
          {
            PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();

            Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));

            intRet = System.Convert.ToInt32(Info2.Status);
          }
          Marshal.FreeHGlobal(pAddr);
        }
        ClosePrinter(hPrinter);
      }

      return intRet;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct structPrinterDefaults
    {
      [MarshalAs(UnmanagedType.LPTStr)]
      public String pDatatype;
      public IntPtr pDevMode;
      [MarshalAs(UnmanagedType.I4)]
      public int DesiredAccess;
    };

    [FlagsAttribute]
    public enum PrinterStatus
    {
      PRINTER_STATUS_BUSY = 0x00000200,
      PRINTER_STATUS_DOOR_OPEN = 0x00400000,
      PRINTER_STATUS_ERROR = 0x00000002,
      PRINTER_STATUS_INITIALIZING = 0x00008000,
      PRINTER_STATUS_IO_ACTIVE = 0x00000100,
      PRINTER_STATUS_MANUAL_FEED = 0x00000020,
      PRINTER_STATUS_NO_TONER = 0x00040000,
      PRINTER_STATUS_NOT_AVAILABLE = 0x00001000,
      PRINTER_STATUS_OFFLINE = 0x00000080,
      PRINTER_STATUS_OUT_OF_MEMORY = 0x00200000,
      PRINTER_STATUS_OUTPUT_BIN_FULL = 0x00000800,
      PRINTER_STATUS_PAGE_PUNT = 0x00080000,
      PRINTER_STATUS_PAPER_JAM = 0x00000008,
      PRINTER_STATUS_PAPER_OUT = 0x00000010,
      PRINTER_STATUS_PAPER_PROBLEM = 0x00000040,
      PRINTER_STATUS_PAUSED = 0x00000001,
      PRINTER_STATUS_PENDING_DELETION = 0x00000004,
      PRINTER_STATUS_PRINTING = 0x00000400,
      PRINTER_STATUS_PROCESSING = 0x00004000,
      PRINTER_STATUS_TONER_LOW = 0x00020000,
      PRINTER_STATUS_USER_INTERVENTION = 0x00100000,
      PRINTER_STATUS_WAITING = 0x20000000,
      PRINTER_STATUS_WARMING_UP = 0x00010000
    }

    public struct PRINTER_INFO_2
    {
      public string pServerName;
      public string pPrinterName;
      public string pShareName;
      public string pPortName;
      public string pDriverName;
      public string pComment;
      public string pLocation;
      public IntPtr pDevMode;
      public string pSepFile;
      public string pPrintProcessor;
      public string pDatatype;
      public string pParameters;
      public IntPtr pSecurityDescriptor;
      public UInt32 Attributes;
      public UInt32 Priority;
      public UInt32 DefaultPriority;
      public UInt32 StartTime;
      public UInt32 UntilTime;
      public UInt32 Status;
      public UInt32 cJobs;
      public UInt32 AveragePPM;
    }

推荐答案

参考System.Printing和System.Drawing,并使用如下代码:

Reference System.Printing and System.Drawing, and use code like this:


var server = new LocalPrintServer();
foreach (var printer in PrinterSettings.InstalledPrinters.OfType<string>())
{
  var queue = new PrintQueue(server, printer);
  Console.WriteLine(queue.IsOffline);
}


这篇关于如何获取网络打印机的状态? (我的意思是缺纸,卡纸等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:15