프린터 상태 가져오기

17034 단어
얼마 전에 한 항목에 접촉하여 프린터의 작업 상태를 요구하고 dll로 봉인하여 코드를 다음과 같이 실현한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

using System.Security;

namespace TestDll
{
    public interface ITestClass
    {

         string GetPrinterStatusByName(string PrinterName);
    }

    [ClassInterface(ClassInterfaceType.None)]
    public class TestClass : ITestClass
    {
        public string GetPrinterStatusByName(string PrinterName)
        {
            int intValue = GetPrinterStatusInt(PrinterName);
            string strRet = string.Empty;
            switch (intValue)
            {
                case 0:
                    strRet = "    (Ready)";
                    break;
                case 0x00000200:
                    strRet = " (Busy)";
                    break;
                case 0x00400000:
                    strRet = "   (Printer Door Open)";
                    break;
                case 0x00000002:
                    strRet = "  (Printer Error)";
                    break;
                case 0x0008000:
                    strRet = "   (Initializing)";
                    break;
                case 0x00000100:
                    strRet = "    ,  (I/O Active)";
                    break;
                case 0x00000020:
                    strRet = "    (Manual Feed)";
                    break;
                case 0x00040000:
                    strRet = "   (No Toner)";
                    break;
                case 0x00001000:
                    strRet = "   (Not Available)";
                    break;
                case 0x00000080:
                    strRet = "  (Off Line)";
                    break;
                case 0x00200000:
                    strRet = "    (Out of Memory)";
                    break;
                case 0x00000800:
                    strRet = "     (Output Bin Full)";
                    break;
                case 0x00080000:
                    strRet = "       (Page Punt)";
                    break;
                case 0x00000008:
                    strRet = "  (Paper Jam)";
                    break;
                case 0x00000010:
                    strRet = "     (Paper Out)";
                    break;
                case 0x00000040:
                    strRet = "    (Page Problem)";
                    break;
                case 0x00000001:
                    strRet = "  (Paused)";
                    break;
                case 0x00000004:
                    strRet = "    (Pending Deletion)";
                    break;
                case 0x00000400:
                    strRet = "    (Printing)";
                    break;
                case 0x00004000:
                    strRet = "    (Processing)";
                    break;
                case 0x00020000:
                    strRet = "    (Toner Low)";
                    break;
                case 0x00100000:
                    strRet = "      (User Intervention)";
                    break;
                case 0x20000000:
                    strRet = "  (Waiting)";
                    break;
                case 0x00010000:
                    strRet = "   (Warming Up)";
                    break;
                default:
                    strRet = "    (Unknown Status)";
                    break;
            }
            return strRet;
        }
        [DllImport("winspool.Drv", EntryPoint = "OpenPrinter", SetLastError = true,
    CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall),
    SuppressUnmanagedCodeSecurityAttribute()]
        internal static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPTStr)]
   string printerName,
    out IntPtr phPrinter,
    ref structPrinterDefaults pd);

        [DllImport("winspool.Drv", EntryPoint = "GetPrinterA", SetLastError = true,
    ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern bool GetPrinter(
    IntPtr hPrinter,
    int dwLevel,
    IntPtr pPrinter,
    int dwBuf,
    out int dwNeeded
    );

        [StructLayout(LayoutKind.Sequential)]
        internal 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 uint Attributes;
            public uint Priority;
            public uint DefaultPriority;
            public uint StartTime;
            public uint UntilTime;
            public uint Status;
            public uint cJobs;
            public uint AveragePPM;
        }

        internal 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;
        }

        [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true,
    CharSet = CharSet.Unicode, ExactSpelling = false,
    CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
        internal static extern bool ClosePrinter(IntPtr phPrinter);

        //      
        [FlagsAttribute]
        internal 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
        }


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

git 소스 참조:https://github.com/knowledge0603/getPrinterStatus

좋은 웹페이지 즐겨찾기