Page 1 of 1

Capturing print job information

Posted: Wed May 23, 2007 5:34 pm
by rjara
Hi All,

Does anyone have an example of how to capture pring job information so it creates a log entry every time anything is sent to any printer(any printer or any print job ie date time job size etc). I think there are some APIs that can be used but i haven been able to find any information on the PB forums. Any ideas.

Thanks in advance.
Rick

Posted: Sat Jun 09, 2007 12:33 pm
by rjara
I thought there was some code gurus here. I guess this question is to hard!

Posted: Sat Jun 09, 2007 12:47 pm
by PB
> I guess this question is to hard

Try converting this: http://support.microsoft.com/kb/228769/EN-US/

Posted: Sun Jun 10, 2007 12:10 am
by ABBKlaus
had some free time :oops:

Code: Select all

#MAX_PRINTERS=255
dwFlags=#PRINTER_ENUM_CONNECTIONS | #PRINTER_ENUM_LOCAL
Dim hPrinter(#MAX_PRINTERS)
pcbNeeded=0
pcReturned=0
dwBytesNeeded=0
dwReturned=0
*pJobInfo.JOB_INFO_2
*pinfo4.PRINTER_INFO_4
Dim previous(#MAX_PRINTERS)

For i=0 To #MAX_PRINTERS
  previous(i)=0
Next

While Not EnumPrinters_(dwFlags,0,4,*pinfo4,dwBytesNeeded,@dwBytesNeeded,@dwReturned)
  If GetLastError_()=#ERROR_INSUFFICIENT_BUFFER
    If *pinfo4
      FreeMemory(*pinfo4)
    EndIf
    *pinfo4=AllocateMemory(dwBytesNeeded * SizeOf(PRINTER_INFO_4))
  Else
    Debug "Enumprinters failed: "+Str(GetLastError_())
    Goto Cleanup
  EndIf
Wend

Debug "*******************************************************"
Debug "# OF PRINTERS (Local/Networked) INSTALLED ON YOUR MACHINE ="+Str(dwReturned)
If dwReturned
  *pinfo4temp.PRINTER_INFO_4=*pinfo4
  For i = 0 To dwReturned-1
    Debug "Printer #"+Str(i+1)+": "+PeekS(*pinfo4temp\pPrinterName)
    *pinfo4temp+SizeOf(PRINTER_INFO_4)
  Next
  
  *pinfo4temp.PRINTER_INFO_4=*pinfo4
  For i = 0 To dwReturned-1
    If Not OpenPrinter_(PeekS(*pinfo4temp\pPrinterName),@hPrinter(i),0)
      Debug "OpenPrinter failed: "+Str(GetLastError_())
      Goto Cleanup
    EndIf
    *pinfo4temp+SizeOf(PRINTER_INFO_4)
  Next
  
  Repeat ; Keep polling ForEver And dump the info whenever a job is submitted To any printer.
    *pinfo4temp.PRINTER_INFO_4=*pinfo4
    For i = 0 To dwReturned-1
      While Not EnumJobs_(hPrinter(i), 0, 1, 2, *pJobInfo, pcbNeeded, @pcbNeeded, @pcReturned)
        If GetLastError_()=#ERROR_INSUFFICIENT_BUFFER
          If *pJobInfo
            FreeMemory(*pJobInfo)
            *pJobInfo=0
          EndIf
          *pJobInfo = AllocateMemory(pcbNeeded * SizeOf(JOB_INFO_2))
        Else
          Debug "EnumJobs on printer "+Str(i+1)+" failed: "+Str(GetLastError_())
          Goto Cleanup
        EndIf
      Wend
      
      If pcReturned>0
        If *pJobInfo\JobId Not previous(i)
          previous(i) = *pJobInfo\JobId
          Debug "PRINTER # "+Str(i+1)+"  IS PRINTING ************"
          Debug "DUMP THE INFO FROM THE CURRENT JOB - devmode"
          ;******************************************************
          *pDevMode.DEVMODE=*pJobInfo\pDevMode
          If *pDevMode\dmFields & #DM_YRESOLUTION
            Debug "dmYResolution: "+Str(*pDevMode\dmYResolution)+" dpi"
          EndIf
          If *pDevMode\dmPrintQuality & (#DMRES_HIGH | #DMRES_MEDIUM | #DMRES_LOW | #DMRES_DRAFT)
            Debug "Print Quality: "+Str(*pDevMode\dmPrintQuality)+" dpi"
          EndIf
          ;******************************************************
          ;*   Check if Orientation is Portrait or Landscape    *
          ;******************************************************
          If *pDevMode\dmOrientation = #DMORIENT_PORTRAIT
            Debug "Orientation:Portrait"
          EndIf
          If *pDevMode\dmOrientation = #DMORIENT_LANDSCAPE
            Debug "Orientation:Landscape"
          EndIf
          ;******************************************************
          If *pDevMode\dmFields & #DM_COPIES
            Debug "Num of Copies: "+Str(*pDevMode\dmCopies)
          EndIf            
          If *pDevMode\dmFields & #DM_PAPERLENGTH
            Debug "Paper Length: "+Str(*pDevMode\dmPaperLength)
          EndIf
          If *pDevMode\dmFields & #DM_PAPERWIDTH
            Debug "Paper Width: "+Str(*pDevMode\dmPaperWidth)
          EndIf
          ;******************************************************/
          If *pDevMode\dmDuplex & #DM_DUPLEX
            Debug "Duplex Mode ON"
          EndIf
          If *pDevMode\dmColor = #DMCOLOR_COLOR
            Debug "Color Image"
          Else
            Debug "Monochrome Image"
          EndIf
          If *pDevMode\dmFields & #DM_BITSPERPEL
            Debug "Bits Per Pel: "+Str(*pDevMode\dmBitsPerPel)
          EndIf
          Debug "JOB INFO-2 dump      **************"
          Debug "JobId           : "+Str(*pJobInfo\JobId)
          Debug "Printer Name    : "+PeekS(*pJobInfo\pPrinterName)
          Debug "Machine Name    : "+PeekS(*pJobInfo\pMachineName)
          Debug "User Name       : "+PeekS(*pJobInfo\pUserName)
          Debug "Document        : "+PeekS(*pJobInfo\pDocument)
          Debug "Notify Name     : "+PeekS(*pJobInfo\pNotifyName)
          Debug "Datatype        : "+PeekS(*pJobInfo\pDatatype)
          Debug "Print Processor : "+PeekS(*pJobInfo\pPrintProcessor)
          Debug "Parameters      : "+Str(*pJobInfo\pParameters)
          Debug "Driver Name     : "+PeekS(*pJobInfo\pDriverName)
          
          Debug "TotalPages      : "+Str(*pJobInfo\TotalPages)
          Debug "Size            : "+Str(*pJobInfo\iSize)
          
          SystemTimeToTzSpecificLocalTime_(0,*pJobInfo\Submitted,LocalTime.SYSTEMTIME)
          Debug "Date Submitted  : "+Str(LocalTime\wYear)+"/"+RSet(Str(LocalTime\wMonth),2,"0")+"/"+RSet(Str(LocalTime\wDay),2,"0")
          Debug "Time Submitted  : "+RSet(Str(LocalTime\wHour),2,"0")+":"+RSet(Str(LocalTime\wMinute),2,"0")+":"+RSet(Str(LocalTime\wSecond),2,"0")
        EndIf
      EndIf
    Next
    Delay(10)
  ForEver
EndIf

Cleanup:
For i = 0 To dwReturned-1
  If Not ClosePrinter_(hPrinter(i))
    Debug "ClosePrinter failed: "+Str(GetLastError_())
  EndIf
Next
If *pJobInfo
  FreeMemory(*pJobInfo)
EndIf
If *pinfo4
  FreeMemory(*pinfo4)
EndIf
Regards Klaus

Posted: Sun Jun 10, 2007 2:28 am
by PB

Code: Select all

Line 134: GetLastError() is not a function, array, macro or linked list
The above line needs to be changed to:

Code: Select all

Debug "ClosePrinter failed: "+Str(GetLastError_())

Posted: Sun Jun 10, 2007 2:41 am
by GeoTrail
@ABBKlaus, if you have some more, could you send me some free time please? :lol:

Posted: Sun Jun 10, 2007 12:55 pm
by ABBKlaus
no i had droopy lib installed :oops: but you are right PB :wink:
Line 134: GetLastError() is not a function, array, macro or linked list
Regards Klaus

Posted: Sat Jun 16, 2007 10:33 am
by rjara
Thanks ABBKlaus for your effort in coding this sample. I will modifying the code to write to a log file. Again thanks.

Re:

Posted: Thu Mar 12, 2015 4:34 pm
by bbanelli
ABBKlaus wrote:had some free time :oops:
Sorry for raising such an old thread, but this is a fantastic piece of code example.

Is it possible to obtain (raw?) data from these structures so one could use DOC_INFO_1 structure to set output to file and than print it out with WritePrinter() which requires pointer to data in order to work? I was skimming through documentation but can't seem to find anything useful so far.

So basically, once print job is detected, I would like to obtain data that is being printed and save it somewhere else apart from them being printed out; perhaps in that case, I don't even have to use WritePrinter() put can save data as a raw document with inbuilt PureBasic functions... Is that even theoretically possible with API?

Thank you in advance for any clues whatsoever!

With my best,

Bruno