Solved

help with print job monitor

Posted on 2007-03-27
3
1,981 Views
Last Modified: 2013-11-07
I'm trying to create a program that when I run it I can capture a print jobs name,total pages, owner, etc.. I'm trying to use wmi win32_printjob for this, but I just can't seem to get it. I would like to be able to capture the info into variables so that I can write them to a file, database, etc... I found the below code and it works, I just can't figure out how to get it not to return so much info, just return the fields I want and how to get the info into variables for each property. Looking to get this working in vb.net

Thank you

Public Class Form1
    WithEvents watcher As ManagementEventWatcher

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) _
    Handles Me.FormClosed
        watcher.Stop()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        Dim q As New WqlEventQuery("__InstanceCreationEvent")
        q.WithinInterval = New TimeSpan(0, 0, 1)
        q.Condition = "TargetInstance ISA 'Win32_PrintJob'"
        watcher = New ManagementEventWatcher(q)
        watcher.Start()
    End Sub

    Private Sub watcher_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) _
    Handles watcher.EventArrived
        Dim printjob As ManagementBaseObject = e.NewEvent.Item("TargetInstance")
        Dim sb As New System.Text.StringBuilder
        sb.AppendLine("This printjob just started:")
        For Each p As PropertyData In printjob.Properties
            p.Name
            sb.AppendFormat("{0}: {1}", p.Name, p.Value)
            sb.AppendLine()


        Next
        MsgBox(sb.ToString)


    End Sub
End Class
0
Comment
Question by:heydude
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 500 total points
ID: 18805958
this guy helped me and hence it will work for u too : http://www.experts-exchange.com/M_38421.html

bob said:
I have this WMI class that retrieves PrintJob information, but I can't test it right now:

' Add a reference to System.Management.dll.

Imports System.Management

Namespace My.Computer.WMI

  Public Class PrintJob

    Public DataType As String
    Public Description As String
    Public Document As String
    Public DriverName As String
    Public ElapsedTime As Date
    Public HostPrintQueue As String
    Public InstallDate As Date
    Public JobId As Integer    'uint32
    Public JobStatus As String
    Public Notify As String
    Public Owner As String
    Public PagesPrinted As Integer     'uint32
    Public Parameters As String
    Public PrintProcessor As String
    Public Priority As Integer     'uint32
    Public Size As Integer     'uint32
    Public StartTime As Date
    Public Status As String
    Public StatusMask As Integer     'uint32
    Public TimeSubmitted As Date
    Public TotalPages As Integer     'uint32
    Public UntilTime As Date


    Public Shared ReadOnly Property List() As PrintJob()
      Get
        Dim query As String = "Select * From Win32_PrintJob"

        Dim searcher As New ManagementObjectSearcher(query)

        Dim results As ManagementObjectCollection = searcher.Get

        Dim listPrintJobs(results.Count - 1) As PrintJob

        Dim index As Integer = 0

        For Each entryCurrent As ManagementObject In results

          Dim job As New PrintJob

          job.DataType = entryCurrent("DataType")
          job.Description = entryCurrent("Description")
          job.Document = entryCurrent("Document")
          job.DriverName = entryCurrent("DriverName")
          job.ElapsedTime = entryCurrent("ElapsedTime")
          job.HostPrintQueue = entryCurrent("HostPrintQueue")
          job.InstallDate = entryCurrent("InstallDate")
          job.JobId = entryCurrent("JobId")
          job.JobStatus = entryCurrent("JobStatus")
          job.Notify = entryCurrent("Notify")
          job.Owner = entryCurrent("Owner")
          job.PagesPrinted = Convert.ToInt32(entryCurrent("PagesPrinted"))
          job.Parameters = entryCurrent("Parameters")
          job.PrintProcessor = entryCurrent("PrintProcessor")
          job.Priority = entryCurrent("Priority")
          job.Size = entryCurrent("Size")
          job.StartTime = entryCurrent("StartTime")
          job.Status = entryCurrent("Status")
          job.StatusMask = entryCurrent("StatusMask")
          job.TimeSubmitted = entryCurrent("TimeSubmitted")
          job.TotalPages = Convert.ToInt32(entryCurrent("TotalPages"))
          job.UntilTime = entryCurrent("UntilTime")

          listPrintJobs(index) = job

          index += 1

        Next entryCurrent

        Return listPrintJobs
      End Get
    End Property    'List

  End Class

End Namespace

Sample usage:
  Public Shared Function PrintJobs() As String

    Dim builder As New StringBuilder
    Dim list As PrintJob() = PrintJob.List()

    If list.Length = 0 Then
      builder.Append("(None)")
    Else
      For Each job As PrintJob In list
        builder.Append(job.Document & Environment.NewLine)
      Next
    End If

    Return builder.ToString()

  End Function
0
 

Author Comment

by:heydude
ID: 18808485
didn't really help me?
0
 

Author Comment

by:heydude
ID: 18833873
I did end up coming up with a way to do it. I'm going to give you the points because I can probably use the code above for something.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now