1

I'm querying to Win32_PrintJob WMI class every time there is a change with ManagementEventWatcher, I obtained data about it, such as: Document, HostPrintQueue, JobId, JobStatus, TotalPages, etc. But TotalPages is not representing the real number of page to print, Seems at the moment to obtain these data still the printjob doesn't finished to process and devolving a number of pages to print in that moment but the real total is other number, How to get the real number of a print job when finished it to process? Here is my code:

 ManagementEventWatcher createPrintJobWatcher;
        String strComputerName = ".";
        // Create event query to be notified within 1 milli second of a change in a service
        WqlEventQuery createPrintJobQuery = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 0.001 WHERE TargetInstance ISA \"Win32_PrintJob\"");

        createPrintJobWatcher = new ManagementEventWatcher();
        createPrintJobWatcher.Scope = new ManagementScope("\\\\" + strComputerName + "\\root\\CIMV2");
        createPrintJobWatcher.Query = createPrintJobQuery;
        // times out watcher.WaitForNextEvent in 1 seconds
        createPrintJobWatcher.Options.Timeout = new TimeSpan(0, 0, 1);
        //set the print event handler
        createPrintJobWatcher.EventArrived += new EventArrivedEventHandler(createPrintJobListener);

        createPrintJobWatcher.Start();

        Console.WriteLine("Listening...");

        Console.ReadLine();

createPrintJobListener method:

        static void createPrintJobListener(object sender, EventArrivedEventArgs e)
    {

        SelectQuery query = new SelectQuery("Win32_PrintJob");
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        using (ManagementObjectCollection printJobs = searcher.Get())
            foreach (ManagementObject printJob in printJobs)
            {
                Console.WriteLine("c1:", printJob);
                Console.WriteLine("ID: {0}", printJob.GetPropertyValue("JobId").ToString());
                Console.WriteLine("name: {0}", printJob.GetPropertyValue("name").ToString());
                Console.WriteLine("status: {0}", printJob.GetPropertyValue("status").ToString());
                if (printJob.GetPropertyValue("JobStatus") != null)
                {
                    Console.WriteLine("JobStatus: {0}", printJob.GetPropertyValue("JobStatus").ToString());
                }
                else
                {
                    Console.WriteLine("JobStatus: NULLLLLL");
                }

                Console.WriteLine("PC: {0}", printJob.GetPropertyValue("HostPrintQueue").ToString());
                Console.WriteLine("TOTOAL PAGES: {0}", printJob.GetPropertyValue("TotalPages").ToString());                    
            }
    }
karelp90
  • 319
  • 2
  • 7
  • Does this answer your question? [WMI to get current printer job (including number of copies)](https://stackoverflow.com/questions/74967456/wmi-to-get-current-printer-job-including-number-of-copies) – karel Jan 03 '23 at 12:42

1 Answers1

1

WMI is probably not sufficient to do this.

Windows doesn't reliably provide the page count (or copies etc), so the only way to get accurate info is to pause the job and parse it. This is a non-trivial task, but here's a little more info.

Nick Westgate
  • 3,088
  • 2
  • 34
  • 41
  • thanks for your answer. I already found the solution for this, the problem was I asking for the total pages of print job at the wrong moment. When print job status is "Printing" is the moment for get this value, at least that's how it worked for me. Now the problem is with dmCopies value in the scenery when I print a word document, always shows 1. By the way, there is a way for get the total historic count of a printer with c#? – karelp90 Nov 02 '17 at 13:48
  • Windows doesn't keep historic page count info. High-end printers do if you want to play with [SNMP](https://tools.ietf.org/html/rfc3805). – Nick Westgate Nov 03 '17 at 01:36
  • what is dmCopies @karelp90....? Can you ellaborate it more clearly, is that the 'Number of copies' given beforer user clicking Print (button) ? If so, can WMI obtain that number too...? – gumuruh Jan 01 '23 at 10:21