CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Printer status

  1. #1
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Printer status

    How do I get the printer's status - busy, printing, out of paper etc?

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Printer status


    'this may give an idea about jobs in the queue.
    'However, about being out of paper or other similar "errors", I do not know
    ' how to handle. Maybe looking at printer.errors ?

    private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (byval pPrinterName as string, phPrinter as Long, pDefault as Any) as Long
    private Declare Function ClosePrinter Lib "winspool.drv" (byval hPrinter as Long) as Long
    private Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (byval hPrinter as Long, byval FirstJob as Long, byval NoJobs as Long, byval Level as Long, pJob as Any, byval cdBuf as Long, pcbNeeded as Long, pcReturned as Long) as Long
    private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    Dim hPrinter as Long, lNeeded as Long, lReturned as Long
    Dim lJobCount as Long
    OpenPrinter Printer.DeviceName, hPrinter, byval 0&
    EnumJobs hPrinter, 0, 99, 1, byval 0&, 0, lNeeded, lReturned
    If lNeeded &gt; 0 then
    ReDim byteJobsBuffer(lNeeded - 1) as Byte
    EnumJobs hPrinter, 0, 99, 1, byteJobsBuffer(0), lNeeded, lNeeded, lReturned
    If lReturned &gt; 0 then
    lJobCount = lReturned
    else
    lJobCount = 0
    End If
    else
    lJobCount = 0
    End If
    ClosePrinter hPrinter
    MsgBox "Jobs in printer queue: " + CStr(lJobCount), vbInformation
    End Sub




    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Printer status

    I got it - but it's a lot of stuff....

    see http://www.merrioncomputing.com/Prog...rintStatus.htm

    ..and now I'm off on holiday....
    Sláinte,
    D.

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  4. #4
    Join Date
    Apr 2000
    Posts
    737

    Re: Printer status

    do you realise that this wouldn't work if you got no print job ?? i.e. it always return OK status if you don't have any print job ??


    HTH

    cksiow
    http://vblib.virtualave.net - share our codes

  5. #5
    Join Date
    Oct 2001
    Posts
    1

    Re: Printer status

    The code below works. I have only some problems with the status. It seems not correspond with microsoft constante declaration. I'm still working.

    It's certainly not the best way to code...
    so comments are welcome and thanks for people who will help me to become better ;-)

    Option Explicit

    '*************************************************************************************************
    'constants declaration
    '*************************************************************************************************
    'Constants for DEVMODE structure
    Private Const CCHDEVICENAME = 32
    Private Const CCHFORMNAME = 32

    'Constants for DesiredAccess member of PRINTER_DEFAULTS
    Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Private Const PRINTER_ACCESS_ADMINISTER = &H4
    Private Const PRINTER_ACCESS_USE = &H8
    Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

    'Constant that goes into PRINTER_INFO_5 Attributes member
    'to set it as default
    Private Const PRINTER_ATTRIBUTE_DEFAULT = 4
    Private Const ERROR_INSUFFICIENT_BUFFER = &H122

    'constante pour l'état des job
    Private Const PRINTER_STATUS_BUSY = &H200
    Private Const PRINTER_STATUS_DOOR_OPEN = &H400000
    Private Const PRINTER_STATUS_ERROR = &H2
    Private Const PRINTER_STATUS_INITIALIZING = &H8000
    Private Const PRINTER_STATUS_IO_ACTIVE = &H100
    Private Const PRINTER_STATUS_MANUAL_FEED = &H20
    Private Const PRINTER_STATUS_NO_TONER = &H40000
    Private Const PRINTER_STATUS_NOT_AVAILABLE = &H1000
    Private Const PRINTER_STATUS_OFFLINE = &H80
    Private Const PRINTER_STATUS_OUT_OF_MEMORY = &H200000
    Private Const PRINTER_STATUS_OUTPUT_BIN_FULL = &H800
    Private Const PRINTER_STATUS_PAGE_PUNT = &H80000
    Private Const PRINTER_STATUS_PAPER_JAM = &H8
    Private Const PRINTER_STATUS_PAPER_OUT = &H10
    Private Const PRINTER_STATUS_PAPER_PROBLEM = &H40
    Private Const PRINTER_STATUS_PAUSED = &H1
    Private Const PRINTER_STATUS_PENDING_DELETION = &H4
    Private Const PRINTER_STATUS_PRINTING = &H400
    Private Const PRINTER_STATUS_PROCESSING = &H4000
    Private Const PRINTER_STATUS_TONER_LOW = &H20000
    Private Const PRINTER_STATUS_USER_INTERVENTION = &H100000
    Private Const PRINTER_STATUS_WAITING = &H2000
    Private Const PRINTER_STATUS_WARMING_UP = &H10000


    '*************************************************************************************************
    'Data structure
    '*************************************************************************************************
    'Data structure contains information about the device initialization
    'and environment of a printer
    Private Type DEVMODE
    dmDeviceName As String * CCHDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCHFORMNAME
    dmLogPixels As Integer
    dmBitsPerPel As Long
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
    dmICMMethod As Long 'Windows 95 only
    dmICMIntent As Long 'Windows 95 only
    dmMediaType As Long 'Windows 95 only
    dmDitherType As Long 'Windows 95 only
    dmReserved1 As Long 'Windows 95 only
    dmReserved2 As Long 'Windows 95 only
    End Type

    'Data structure specifies detailed printer information.
    Private Type PRINTER_INFO_5
    pPrinterName As String
    pPortName As String
    Attributes As Long
    DeviceNotSelectedTimeout As Long
    TransmissionRetryTimeout As Long
    End Type

    'Data structure specifies the default data type, environment,
    'initialization data, and access rights for a printer.
    Private Type PRINTER_DEFAULTS
    pDatatype As Long
    pDevMode As DEVMODE
    DesiredAccess As Long
    End Type

    Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
    End Type

    Private Type TIME_ZONE_INFORMATION
    Bias As Long
    StandardName(32) As Integer
    StandardDate As SYSTEMTIME
    StandardBias As Long
    DaylightName(32) As Integer
    DaylightDate As SYSTEMTIME
    DaylightBias As Long
    End Type

    '*************************************************************************************************
    'Windows Win32 APIs declaration section
    '*************************************************************************************************
    'Function retrieves a handle identifying the specified printer or print server
    Private Declare Function OpenPrinter Lib "winspool.drv" _
    Alias "OpenPrinterA" _
    (ByVal pPrinterName As String, _
    phPrinter As Long, _
    pDefault As PRINTER_DEFAULTS) As Long

    Private Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" _
    (ByVal hPrinter As Long, _
    ByVal FirstJob As Long, _
    ByVal NoJobs As Long, _
    ByVal Level As Long, _
    pJob As Long, _
    ByVal cdBuf As Long, _
    pcbNeeded As Long, _
    pcReturned As Long) As Long


    'Function closes the specified printer object
    Private Declare Function ClosePrinter Lib "winspool.drv" _
    (ByVal hPrinter As Long) As Long

    'Function copies a string to a buffer
    Private Declare Function lstrcpy Lib "kernel32" _
    Alias "lstrcpyA" _
    (ByVal lpString1 As String, _
    ByVal lpString2 As Any) As Long

    Private Declare Function SystemTimeToTzSpecificLocalTime Lib "kernel32" _
    (lpTimeZoneInformation As TIME_ZONE_INFORMATION, _
    lpUniversalTime As SYSTEMTIME, _
    lpLocalTime As SYSTEMTIME) As Long

    Private Declare Function GetTimeZoneInformation Lib "kernel32" _
    (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long


    '*************************************************************************************************
    'private variables
    '*************************************************************************************************
    Private JI1() As Variant
    Private nombreJobSpooles As Long

    '*************************************************************************************************
    'Class properties section !
    '*************************************************************************************************
    Public Property Get JobsInfo() As Variant()
    JobsInfo = JI1
    End Property

    Public Property Get NbJobs() As Long
    NbJobs = nombreJobSpooles
    End Property
    '*************************************************************************************************
    'user functions
    '*************************************************************************************************
    'copy long (pointer) to string
    Private Function PtrCtoVbString(Add As Long) As String
    Dim sTemp As String * 512, X As Long

    X = lstrcpy(sTemp, Add)
    If (InStr(1, sTemp, Chr(0)) = 0) Then
    PtrCtoVbString = ""
    Else
    PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)
    End If
    End Function


    '*****************************************************
    'get printer jobs
    '*****************************************************
    Public Function getjobsPrinter(ByVal deviceName As String) As Boolean
    Dim X As Long
    Dim pcbNeeded
    Dim t() As Long
    Dim i As Integer
    Dim handle As Long
    Dim pd As PRINTER_DEFAULTS

    ' set the PRINTER_DEFAULTS members
    pd.pDatatype = 0&
    pd.DesiredAccess = PRINTER_ACCESS_USE

    'Printer handle
    X = OpenPrinter(deviceName, handle, pd)
    If X = False Then
    getjobsPrinter = False
    Exit Function
    End If

    'space needs
    X = EnumJobs(handle, 0, &HFFFFFFFF, 2, ByVal 0&, pcbNeeded, pcbNeeded, nombreJobSpooles)
    If Err.LastDllError &lt;&gt; 122 Then
    ClosePrinter (handle)
    getjobsPrinter = False
    Exit Function
    End If

    'there is some jobs in spool?
    If pcbNeeded &gt; 0 Then
    'allocate space
    ReDim t(0 To (pcbNeeded / 4) + 3)
    'récupération des job
    X = EnumJobs(handle, 0, &HFFFFFFFF, 2, t(0), pcbNeeded, pcbNeeded, nombreJobSpooles)
    If X = False Then
    ClosePrinter (handle)
    getjobsPrinter = False
    Exit Function
    End If
    End If

    ReDim JI1(nombreJobSpooles, 26)

    For i = 0 To nombreJobSpooles - 1
    JI1(i, 0) = t(0 + (i * 26)) 'jobid
    JI1(i, 1) = PtrCtoVbString(t(1 + (i * 26))) 'pprintername
    JI1(i, 2) = PtrCtoVbString(t(2 + (i * 26))) 'pmachineame
    JI1(i, 3) = PtrCtoVbString(t(3 + (i * 26))) 'pusername
    JI1(i, 4) = PtrCtoVbString(t(4 + (i * 26))) 'pdocument
    JI1(i, 5) = PtrCtoVbString(t(5 + (i * 26))) 'pnotifyname
    JI1(i, 6) = PtrCtoVbString(t(6 + (i * 26))) 'pdatatype
    JI1(i, 7) = PtrCtoVbString(t(7 + (i * 26))) 'pPrintProcessor
    JI1(i, 8) = PtrCtoVbString(t(8 + (i * 26))) 'pParameters
    JI1(i, 9) = PtrCtoVbString(t(9 + (i * 26))) 'pDrivername
    JI1(i, 10) = "" 'PtrCtoVbString(t(10 + (i * 26))) 'pDevMode
    JI1(i, 11) = PtrCtoVbString(t(11 + (i * 26))) 'pstatus
    JI1(i, 12) = "" 'PtrCtoVbString(t(12 + (i * 26))) 'psécurity
    'set Printer Status
    Select Case t(13 + (i * 26))
    Case PRINTER_STATUS_BUSY
    JI1(i, 13) = "Occupé"
    Case PRINTER_STATUS_DOOR_OPEN
    JI1(i, 13) = "Porte Ouverte"
    Case PRINTER_STATUS_ERROR
    JI1(i, 13) = "Erreur"
    Case PRINTER_STATUS_INITIALIZING
    JI1(i, 13) = "Initialisation"
    Case PRINTER_STATUS_IO_ACTIVE
    JI1(i, 13) = "E/S Actif"
    Case PRINTER_STATUS_MANUAL_FEED
    JI1(i, 13) = "Manual Feed"
    Case PRINTER_STATUS_NO_TONER
    JI1(i, 13) = "Pas de toner"
    Case PRINTER_STATUS_NOT_AVAILABLE
    JI1(i, 13) = "Non disponible"
    Case PRINTER_STATUS_OFFLINE
    JI1(i, 13) = "Hors Ligne"
    Case PRINTER_STATUS_OUT_OF_MEMORY
    JI1(i, 13) = "Manque Memoire"
    Case PRINTER_STATUS_OUTPUT_BIN_FULL
    JI1(i, 13) = "Back Plein"
    Case PRINTER_STATUS_PAGE_PUNT
    JI1(i, 13) = "PAGE PUNT"
    Case PRINTER_STATUS_PAPER_JAM
    JI1(i, 13) = "Bourrage Papier"
    Case PRINTER_STATUS_PAPER_OUT
    JI1(i, 13) = "Manque Papier"
    Case PRINTER_STATUS_PAPER_PROBLEM
    JI1(i, 13) = "Problème Papier"
    Case PRINTER_STATUS_PAUSED
    JI1(i, 13) = "Pause"
    Case Else
    JI1(i, 13) = ""
    End Select
    JI1(i, 14) = t(14 + (i * 26)) 'priority
    JI1(i, 15) = t(15 + (i * 26)) 'position
    JI1(i, 16) = t(16 + (i * 26)) 'starttime
    JI1(i, 17) = t(17 + (i * 26)) 'untilTime
    JI1(i, 18) = t(18 + (i * 26)) 'totalpages
    JI1(i, 19) = convertOctet(t(19 + (i * 26))) 'size
    'type systeme time --&gt; 16octets donc 4 long !
    JI1(i, 20) = convertSystemTime2VbDate(t(20 + (i * 26)), _
    t(21 + (i * 26)), _
    t(22 + (i * 26)), _
    t(23 + (i * 26))) 'submitted

    JI1(i, 21) = t(24 + (i * 26)) 'time
    JI1(i, 22) = t(25 + (i * 26)) 'pages printed
    Next i
    getjobsPrinter = True
    End Function

    'fonction de conversion en octet vers l'unité la plus facile * lire
    Private Function convertOctet(taille As Long) As String
    On Error Resume Next

    Const LibelleMo = " Mo"
    Const LibellekO = " Ko"
    Const libelleo = " Oct"
    Dim ko As Long
    Dim Mo As Long
    Dim nbKo As Single
    Dim nbMo As Single

    ko = 2 ^ 10
    Mo = 2 ^ 20

    'conversion
    nbMo = Round(taille / Mo, 2)
    nbKo = Round(taille / ko, 2)
    'affichage sous forme de chaine
    If nbMo &gt;= 1 Then
    convertOctet = nbMo & LibelleMo
    ElseIf nbKo &gt;= 1 Then
    convertOctet = nbKo & LibellekO
    Else
    convertOctet = taille & libelleo
    End If
    End Function

    'fonction de conversion d'un type systemetime codé sur 4 long --&gt; 8 integer
    ' wYear As Integer
    ' wMonth As Integer
    ' wDayOfWeek As Integer
    ' wDay As Integer
    ' wHour As Integer
    ' wMinute As Integer
    ' wSecond As Integer
    ' wMilliseconds As Integer
    ' Attention * l'inversion des 2 octets de poids fort et des 2 octets de poids faibles
    Private Function convertSystemTime2VbDate(ByRef long1 As Long, ByRef long2 As Long, ByRef long3 As Long, ByRef long4 As Long) As Variant
    Dim dateGMT As SYSTEMTIME
    Dim dateLocale As SYSTEMTIME
    Dim annee As Variant
    Dim jour As Variant
    Dim mois As Variant
    Dim heure As Variant
    Dim minute As Variant
    Dim seconde As Variant
    Dim datejour As Variant
    Dim heurejour As Variant
    Dim timeZone As TIME_ZONE_INFORMATION

    LongToInt long1, dateGMT.wYear, dateGMT.wMonth

    LongToInt long2, dateGMT.wDayOfWeek, dateGMT.wDay

    LongToInt long3, dateGMT.wHour, dateGMT.wMinute

    LongToInt long4, dateGMT.wSecond, dateGMT.wMilliseconds

    '* ce niveau le temps est GMT, il faut le passer en heure locale --&gt; appel fonction API windows
    'récupération du timeZone local
    GetTimeZoneInformation timeZone
    SystemTimeToTzSpecificLocalTime timeZone, dateGMT, dateLocale

    'annee
    annee = "" & dateLocale.wYear
    'mois
    If dateLocale.wMonth &lt; 10 Then
    mois = "0" & dateLocale.wMonth
    Else
    mois = "" & dateLocale.wMonth
    End If
    'jour
    If dateLocale.wDay &lt; 10 Then
    jour = "0" & dateLocale.wDay
    Else
    jour = "" & dateLocale.wDay
    End If
    'heure
    If dateLocale.wHour &lt; 10 Then
    heure = "0" & dateLocale.wHour
    Else
    heure = "" & dateLocale.wHour
    End If
    'minute
    If dateLocale.wMinute &lt; 10 Then
    minute = "0" & dateLocale.wMinute
    Else
    minute = "" & dateLocale.wMinute
    End If
    'seconde
    If dateLocale.wSecond &lt; 10 Then
    seconde = "0" & dateLocale.wSecond
    Else
    seconde = "" & dateLocale.wSecond
    End If

    'reconstitution de la date
    datejour = jour & "/" & mois & "/" & annee
    'reconstitution de l'heure
    heurejour = heure & ":" & minute & ":" & seconde

    'si date du jour alors renvoie que l'heure
    If datejour = FormatDateTime(Now, vbShortDate) Then
    convertSystemTime2VbDate = heurejour
    Else
    convertSystemTime2VbDate = datejour & " " & heurejour
    End If
    End Function

    Private Function LongToInt(nombre As Long, ByRef poidsFaible As Integer, ByRef poidsFort As Integer) As Boolean
    poidsFaible = nombre Mod 65536
    'gestion de l'arrondi
    If poidsFaible &gt; 32768 Then
    poidsFort = Round(nombre / 65536) - 1
    Else
    poidsFort = Round(nombre / 65536)
    End If
    End Function



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured