自己动手写打印机监控程序 tuijian_1.gif
2010-10-27 22:25:27
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。
一、需求
      由于公司打印纸张浪费比较严重,领导要求对所有的打印进行记录。对比了几款商业软件有的功能比较强大但是价格比较贵而且好多功能并不需要,而有的功能又过于简单。我需要的只是对打印进行记录并不需要计费等功能,而且考虑最好可以和公司现有的OA系统整合在一起,让领导在OA的界面中就可以查询到打印记录,最后决定尝试自己动手进行开发。
二、查找资料
      由于平时用C#比较多一些所以首先查找了一些用C#写的开源程序,但是由于版本都比较老了根本无法运行。又找到一个C++写的可以运行但是C++实在是搞不定,也不是一两天能学会的所以放弃。虽然找了两天没有找到可以运行的完整的程序,但是确定了一个方向,就是调用Windows API函数Enumjobs来获得打印记录。并且还搜到了一份现成的用C#调用Enumjobs的代码,虽然不完整但是确实可以用。
三、规划
     1、首先程序需要安装到打印服务器所以最好是以服务的方式运行
     2、需要通过配置文件来确定需要监控的打印机
四、开始动手
     1、新建一个服务
          (1)首先打开Visual studio 2008,新建一个windows service
          (2)在Service1上点右键选Add Installer
p_w_picpath
        (3)在ProjectInstaller中设置好服务的运行账号和相关设置
            serviceProcessInstaller1 设置属性account为LocalSystem
            serviceInstaller1 设置属性starttype为Automatic
        (4)添加一个timer
            注意这里不能添加工具栏那个,要添加System.timer命名空间下的
       (5)设置服务运行时启动timer
 
 
        
  1. protected override void OnStart(string[] args) 
  2.     timer1.Enabled = true
(6)定时调用监控程序
 
 
        
  1. private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
  2.     pm.peekPrinterJobs(config.GetPrinterName()); 
(7)读取配置文件
 
 
        
  1. private static string ReadXml(string nodeName, string attr) 
  2.     XmlDocument xml = new XmlDocument(); 
  3.     xml.Load("config.xml"); 
  4.     return xml.SelectSingleNode("config/" + nodeName).Attributes[attr].Value; 
  5.  
  6. public static string GetPrinterName() 
  7.     return ReadXml("printer""name"); 
  8. }        public static string GetOutputPath() 
  9.     return ReadXml("output""path"); 
  10.  
    配置文件
 
 
        
  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <config> 
  3. <output path="c:\\monitor.log"></output> 
  4. <printer name="HP5100"></printer> 
  5. </config> 
      (8)新建一个类,核心的监控代码,主要就是通过调用peekPrinterJobs这个函数返回打印的记录
 
 
        
  1. public static int oldPrintId = 0; 
  2.  
  3.        [StructLayout(LayoutKind.Sequential)] 
  4.        public struct SYSTEMTIME 
  5.        { 
  6.            public short wYear; 
  7.            public short wMonth; 
  8.            public short wDayOfWeek; 
  9.            public short wDay; 
  10.            public short wHour; 
  11.            public short wMinute; 
  12.            public short wSecond; 
  13.            public short wMilliseconds; 
  14.        } 
  15.  
  16.        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 
  17.        public struct JOB_INFO_1 
  18.        { 
  19.            public int JobId; 
  20.            public string pPrinterName; 
  21.            public string pMachineName; 
  22.            public string pUserName; 
  23.            public string pDocument; 
  24.            public string pDatatype; 
  25.            public string pStatus; 
  26.            public int Status; 
  27.            public int Priority; 
  28.            public int Position; 
  29.            public int TotalPages; 
  30.            public int PagesPrinted; 
  31.            public SYSTEMTIME Submitted; 
  32.        } 
  33.  
  34.        [DllImport("winspool.drv", CharSet = CharSet.Auto)] 
  35.        public static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault); 
  36.  
  37.        [DllImport("winspool.drv", CharSet = CharSet.Auto)] 
  38.        public static extern bool ClosePrinter(IntPtr hPrinter); 
  39.  
  40.        [DllImport("winspool.drv", CharSet = CharSet.Auto)] 
  41.        public static extern int EnumJobs(IntPtr hPrinter, int FirstJob, int NoJobs, int Level, IntPtr pInfo, int cdBuf, 
  42.                                          out int pcbNeeded, out int pcReturned); 
  43.  
  44.        public static void peekPrinterJobs(string printerToPeek) 
  45.        { 
  46.            IntPtr handle; 
  47.            int FirstJob = 0; 
  48.            int NumJobs = 127; 
  49.            int pcbNeeded; 
  50.            int pcReturned; 
  51.  
  52.            // open printer  
  53.            OpenPrinter(printerToPeek, out handle, IntPtr.Zero); 
  54.  
  55.            // get num bytes required, here we assume the maxt job for the printer quest is 128 (0..127)  
  56.            EnumJobs(handle, FirstJob, NumJobs, 1, IntPtr.Zero, 0, out pcbNeeded, out pcReturned); 
  57.  
  58.            // allocate unmanaged memory  
  59.            IntPtr pData = Marshal.AllocHGlobal(pcbNeeded); 
  60.  
  61.            // get structs  
  62.            EnumJobs(handle, FirstJob, NumJobs, 1, pData, pcbNeeded, out pcbNeeded, out pcReturned); 
  63.  
  64.            // create array of managed job structs  
  65.            JOB_INFO_1[] jobs = new JOB_INFO_1[pcReturned]; 
  66.  
  67.            // marshal struct to managed  
  68.            int pTemp = pData.ToInt32(); //start pointer  
  69.            for (int i = 0; i < pcReturned; ++i) 
  70.            { 
  71.                jobs[i] = (JOB_INFO_1)Marshal.PtrToStructure(new IntPtr(pTemp), typeof(JOB_INFO_1)); 
  72.                if (jobs[i].Status != 16) break
  73.                if (jobs[i].JobId == oldPrintId) 
  74.                { 
  75.                    break
  76.                } 
  77.                else 
  78.                { 
  79.                    pTemp += Marshal.SizeOf(typeof(JOB_INFO_1)); 
  80.                                        //记录到日志,可以自己写方法写到数据库或者其他地方 
  81.                    RecordJobToLog(jobs[i]); 
  82.                    oldPrintId = jobs[i].JobId; 
  83.                } 
  84.  
  85.            } 
  86.  
  87.            Marshal.FreeHGlobal(pData); 
  88.  
  89.            ClosePrinter(handle); 
  90.  
  91.        } 
  92.  
  93.        private static void RecordJobToLog(JOB_INFO_1 job) 
  94.        { 
  95.            string logText = job.JobId + "-" + job.pMachineName + "-" + job.pUserName + "-" + job.pDocument + "-" + job.PagesPrinted + "-" + job.TotalPages + "-" + job.Status + "-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm"); 
  96.            WriteToLog(logText); 
  97.        } 
3、安装部署
 
           编译程序,把程序拷到服务器
           然后运行c:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil.exe installutil yourproject.exe 安装服务
           卸载服务c:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil.exe /u yourproject.exe
           打印一份文件,然后就可以到指定目录查看打印日志了
           服务的配置文件一定要拷贝到c:\windows\system32中
五、总结
      通过这个程序学习了服务的开发、读取xml和C#调用api函数,自己开发的优势是可以把日志存已自己需要的格式存放在需要的地方,比如配置文件或者数据库方便和现有平台的整合。JOB_INFO_1所得到的数据比较有限,比如无法取得打印份数等等,通过JOB_INFO_2可以取得更多的打印信息,原理都是一样的先能用慢慢在修改完善。
六、参考链接
本文出自 “ dqw” 博客,转载请与作者联系!