Surveiller un spooler

Zodiark26 Messages postés 2 Date d'inscription mardi 31 mai 2011 Statut Membre Dernière intervention 21 juin 2011 - 17 juin 2011 à 16:49
seb4stien13 Messages postés 31 Date d'inscription lundi 17 novembre 2008 Statut Membre Dernière intervention 11 septembre 2011 - 4 sept. 2011 à 22:34
Bonjour, a la communauté ,

Je me présente, j'ai 23ans je suis apprenti développeur (en BTS), et habitué au surf sur ce forum car d'habitude je me débrouille seul dans ce que j'ai a faire = ) d'habitude hein..

Mais la je craque ^^ impossible de réaliser ce que je veux, c'est a dire vérifier en permanence un si quelqu'un demande une impression, une nouvelle ligne au spooler (qu'elle soit conservé ou non), bref " l'évènement impression " en quelques sorte.
J'ai vu quelque chose qui ressemblais à ce que je voulais ici =>http://www.vbfrance.com/codes/MONITEUR-FILE-IMPRESSION-SPOOL-WATCHER_40520.aspx

Mais je veux un exe (d'ou l'utilisation du vb avec visual studio 2010) qui lance l'appli en tache de fond et qui fasse mes traitements quand l'évènement se produit.

J'avais commencé avec ça, mais je bloque :
Imports System
Imports System.Management
Imports System.Management.Instrumentation
Imports System.IO
Imports System.Collections
Imports System.Reflection
Imports System.MarshalByRefObject
Imports System.ComponentModel.Component
Imports System.Drawing.Printing.PrintDocument

Public Class oGate
    Sub Main()
        scruter_imprimante("PDFCreator", "adesouza", "0.1")
    End Sub

    Private Sub scruter_imprimante(ByVal drivername As String, ByVal user As String, ByVal detectTime As String)

        Dim nomImprimante As String = "PDFCreator"

        Dim queryprinter As New ManagementObjectSearcher()
        Dim management_object As New ManagementObject()
        Dim WmiQueryResult As ManagementObjectCollection

        queryprinter.Query.QueryString = "SELECT * FROM Win32_Printer WHERE Name='" & nomImprimante & "'"
        'queryprinter.Query.QueryString = "SELECT * FROM __instancecreationevent WITHIN" & detectTime & "WHERE TargetInstance isa 'Win32_PrintJob' AND TargetInstance.DriverName='" & nomImprimante & "'"

        Do While True
            WmiQueryResult = queryprinter.Get
            Try
                For Each management_object In WmiQueryResult
                    If management_object.GetPropertyValue("Name") = "PDFCreator" Then
                        'imprimante = management_object.GetPropertyValue("Name")
                        'MsgBox(management_object.GetPropertyValue("JobCountSinceLastReset"))
                        'For Each p In management_object.Properties
                        '    MsgBox(p.Name)
                        'Next
                        'MsgBox(management_object.Properties)
                    End If
                    'MsgBox("e")
                Next management_object

            Catch ex As Exception
                MsgBox("Erreur lors de la création de la liste des imprimantes installées : " & ex.ToString)
            End Try
        Loop

    End Sub

End Class


J'ai également testé de faire fonctionné les WqlEventQuery et de vielle dll comme PrintWatchQueu mais rien a faire.

2 réponses

Zodiark26 Messages postés 2 Date d'inscription mardi 31 mai 2011 Statut Membre Dernière intervention 21 juin 2011
21 juin 2011 à 09:37
En fait j'ai "presque" résolu mon soucis... ^^

Avec ce code ci-dessous une adaptation de celui trouvé sur ce site.

Je recherche maintenant à masquer la fenetre (si possible).
Et lancer la commande shell sans que la fenetre de cmd s'ouvre.
Mais je pense avoir fait le plus dur => donc résolu !

Imports PrinterQueueWatch
'Référence a System.Management
' + 
Imports System.Management

Public Class nomAppli
    Dim imprimante As String = "PDFCreator"
    Dim info As New PrinterInformation(imprimante, SpoolerApiConstantEnumerations.PrinterAccessRights.PRINTER_ALL_ACCESS, True)
    Dim urlFinal As String = ""
    Dim urlBase As String = ""
    Dim driverName As String = ""

    Private Sub nomAppli_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        PrinterMonitorComponent1.RemovePrinter(imprimante)
    End Sub
    Private Sub nomAppli_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        Try
            nomAppli.CheckForIllegalCrossThreadCalls = False
            'On définit l'imprimante
            PrinterMonitorComponent1.DeviceName = imprimante

            'Flusher l'imprimante déjà connectée puis connecté la nouvelle
            PrinterMonitorComponent1.RemovePrinter(imprimante)
            'On connecte l'imprimante a surveiller
            PrinterMonitorComponent1.AddPrinter(imprimante)

            'MsgBox( PrinterMonitorComponent1.DeviceName)
            Me.Text = " Imprimante surveillée : " & imprimante

        Catch ex As Exception
            MsgBox("Erreur dans l'initialisation du Moniteur d'Impression : " & ex.ToString)
        End Try
    End Sub

    ' '  REFRESH DU SPOOL ET OUVERTURE DU NAVIGATEUR  ==============
    ' ================================================================
    Private Sub PrinterMonitorComponent1_JobAdded(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrinterMonitorComponent1.JobAdded
        refresh_actif()
        Shell("cmd /c start " & urlFinal, AppWinStyle.MaximizedFocus)
        MsgBox("mes actions")
    End Sub

    Private Sub PrinterMonitorComponent1_JobDeleted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrinterMonitorComponent1.JobDeleted
        refresh_actif()
    End Sub

    Private Sub refresh_actif()
        Dim jobcol As New PrinterQueueWatch.PrintJobCollection(imprimante, info.JobCount)
        Dim job As New PrintJob
        Dim currentDate As String
        Dim pattern As String = "YYmmdd"
        Try
            For Each job In jobcol
                'On recup la date format yyyymmdd
                currentDate = Now()
                currentDate = Mid(currentDate, 7, 4) & Mid(currentDate, 4, 2) & Mid(currentDate, 1, 2)
                urlFinal = urlBase & " traitement sur url"
                'MsgBox(job.JobId)
            Next job
        Catch ex As Exception
            MsgBox("Erreur pendant la recherche des tâches d'impression sur : " & PrinterMonitorComponent1.DeviceName & " Message : " & ex.ToString & ex.Message)
        End Try

    End Sub

End Class
0
seb4stien13 Messages postés 31 Date d'inscription lundi 17 novembre 2008 Statut Membre Dernière intervention 11 septembre 2011
4 sept. 2011 à 22:34
POur lancer une commande shell sans que la fenêtre ne s'ouvre :

Sub HiddenCMDCommand(ByVal Command As String)
        
        Dim startme As New Diagnostics.ProcessStartInfo()
       
        startme.Arguments = "/C " & Command
        
        startme.FileName = "cmd.exe"
        
        startme.WindowStyle = Diagnostics.ProcessWindowStyle.Hidden
        
        Process.Start(startme)
    End Sub

J'éspere que c'est ce que tu cherches
0
Rejoignez-nous