using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace Print { class PrintStatus { public static int oldPrintId = 0; [StructLayout(LayoutKind.Sequential)] public struct SYSTEMTIME { public short wYear; public short wMonth; public short wDayOfWeek; public short wDay; public short wHour; public short wMinute; public short wSecond; public short wMilliseconds; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct JOB_INFO_1 { public int JobId; public string pPrinterName; public string pMachineName; public string pUserName; public string pDocument; public string pDatatype; public string pStatus; public int Status; public int Priority; public int Position; public int TotalPages; public int PagesPrinted; public SYSTEMTIME Submitted; } [DllImport("winspool.drv", CharSet = CharSet.Auto)] public static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault); [DllImport("winspool.drv", CharSet = CharSet.Auto)] public static extern bool ClosePrinter(IntPtr hPrinter); [DllImport("winspool.drv", CharSet = CharSet.Auto)] public static extern int EnumJobs(IntPtr hPrinter, int FirstJob, int NoJobs, int Level, IntPtr pInfo, int cdBuf, out int pcbNeeded, out int pcReturned); public static void peekPrinterJobs(string printerToPeek) { IntPtr handle; int FirstJob = 0; int NumJobs = 127; int pcbNeeded; int pcReturned; // open printer OpenPrinter(printerToPeek, out handle, IntPtr.Zero); // get num bytes required, here we assume the maxt job for the printer quest is 128 (0..127) EnumJobs(handle, FirstJob, NumJobs, 1, IntPtr.Zero, 0, out pcbNeeded, out pcReturned); // allocate unmanaged memory IntPtr pData = Marshal.AllocHGlobal(pcbNeeded); // get structs EnumJobs(handle, FirstJob, NumJobs, 1, pData, pcbNeeded, out pcbNeeded, out pcReturned); // create array of managed job structs JOB_INFO_1[] jobs = new JOB_INFO_1[pcReturned]; // marshal struct to managed int pTemp = pData.ToInt32(); //start pointer for (int i = 0; i < pcReturned; ++i) { jobs[i] = (JOB_INFO_1)Marshal.PtrToStructure(new IntPtr(pTemp), typeof(JOB_INFO_1)); string s = jobs[i].pPrinterName + " " + jobs[i].pDocument + " 状态码:" + jobs[i].Status; //if (jobs[i].Status != 16) break; if (jobs[i].JobId == oldPrintId) { // MessageBox.Show("bbb"); break; } else { // MessageBox.Show("ccc"); pTemp += Marshal.SizeOf(typeof(JOB_INFO_1)); //记录到日志,可以自己写方法写到数据库或者其他地方 RecordJobToLog(jobs[i]); oldPrintId = jobs[i].JobId; } } Marshal.FreeHGlobal(pData); ClosePrinter(handle); } /// <summary> /// 获取打印任务 /// </summary> /// <param name="printerToPeek">要查询的打印机任务的名称</param> /// <returns></returns> public static List<string> GetPrinterTask(string printerToPeek) { List<string> nameList = new List<string>(); IntPtr handle; int FirstJob = 0; int NumJobs = 127; int pcbNeeded; int pcReturned; // open printer OpenPrinter(printerToPeek, out handle, IntPtr.Zero); // get num bytes required, here we assume the maxt job for the printer quest is 128 (0..127) EnumJobs(handle, FirstJob, NumJobs, 1, IntPtr.Zero, 0, out pcbNeeded, out pcReturned); // allocate unmanaged memory IntPtr pData = Marshal.AllocHGlobal(pcbNeeded); // get structs EnumJobs(handle, FirstJob, NumJobs, 1, pData, pcbNeeded, out pcbNeeded, out pcReturned); // create array of managed job structs JOB_INFO_1[] jobs = new JOB_INFO_1[pcReturned]; // marshal struct to managed int pTemp = pData.ToInt32(); //start pointer for (int i = 0; i < pcReturned; ++i) { jobs[i] = (JOB_INFO_1)Marshal.PtrToStructure(new IntPtr(pTemp), typeof(JOB_INFO_1)); string s = jobs[i].pPrinterName + " " + jobs[i].pDocument + " 状态码:" + jobs[i].Status; nameList.Add(s); //if (jobs[i].Status != 16) break; if (jobs[i].JobId == oldPrintId) { // MessageBox.Show("bbb"); break; } else { // MessageBox.Show("ccc"); pTemp += Marshal.SizeOf(typeof(JOB_INFO_1)); //记录到日志,可以自己写方法写到数据库或者其他地方 RecordJobToLog(jobs[i]); oldPrintId = jobs[i].JobId; } } Marshal.FreeHGlobal(pData); ClosePrinter(handle); return nameList; } private static void RecordJobToLog(JOB_INFO_1 job) { string logText = job.JobId + "-" + job.pMachineName + "-" + job.pUserName + "-" + job.pDocument + "-" + job.PagesPrinted + "-" + job.TotalPages + "-" + job.Status + "-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm"); Console.WriteLine(logText); } } }