Solved

help with print job monitor

Posted on 2007-03-27
3
1,973 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

12 Experts available now in Live!

Get 1:1 Help Now