Capturing print job information

Just starting out? Need help? Post your questions and find answers here.
rjara
User
User
Posts: 14
Joined: Wed May 23, 2007 5:17 pm

Capturing print job information

Post 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
rjara
User
User
Posts: 14
Joined: Wed May 23, 2007 5:17 pm

Post by rjara »

I thought there was some code gurus here. I guess this question is to hard!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I guess this question is to hard

Try converting this: http://support.microsoft.com/kb/228769/EN-US/
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post 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
Last edited by ABBKlaus on Sun Jun 10, 2007 12:39 pm, edited 1 time in total.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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_())
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

@ABBKlaus, if you have some more, could you send me some free time please? :lol:
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post 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
rjara
User
User
Posts: 14
Joined: Wed May 23, 2007 5:17 pm

Post by rjara »

Thanks ABBKlaus for your effort in coding this sample. I will modifying the code to write to a log file. Again thanks.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re:

Post 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
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Post Reply