1. Andrew Davis
  2. PowerBuilder
  3. Tuesday, 20 November 2018 10:44 AM UTC

Hi All

Can anyone help, I need to be able to query the printer and know whether it is offline or out of paper or a jam etc.

How do i do this, using PB2017r2/3 currently

 

regards and thanks in advance

 

Andrew

David Peace (Powersoft) Accepted Answer Pending Moderation
  1. Tuesday, 20 November 2018 14:16 PM UTC
  2. PowerBuilder
  3. # 1

Hi Andrew

I think you would need to make a windows API call,

try looking at this article https://docs.microsoft.com/en-us/windows/desktop/printdocs/printing-and-print-spooler-functions

Hope that helps

Regards

David

Comment
  1. Roland Smith
  2. Tuesday, 20 November 2018 15:02 PM UTC
It looks like GetPrinter with the PRINTER_INFO_6 structure will return the status. The status codes are:



#define PRINTER_STATUS_BUSY 0x00000200

#define PRINTER_STATUS_DOOR_OPEN 0x00400000

#define PRINTER_STATUS_ERROR 0x00000002

#define PRINTER_STATUS_INITIALIZING 0x00008000

#define PRINTER_STATUS_IO_ACTIVE 0x00000100

#define PRINTER_STATUS_MANUAL_FEED 0x00000020

#define PRINTER_STATUS_NO_TONER 0x00040000

#define PRINTER_STATUS_NOT_AVAILABLE 0x00001000

#define PRINTER_STATUS_OFFLINE 0x00000080

#define PRINTER_STATUS_OUT_OF_MEMORY 0x00200000

#define PRINTER_STATUS_OUTPUT_BIN_FULL 0x00000800

#define PRINTER_STATUS_PAGE_PUNT 0x00080000

#define PRINTER_STATUS_PAPER_JAM 0x00000008

#define PRINTER_STATUS_PAPER_OUT 0x00000010

#define PRINTER_STATUS_PAPER_PROBLEM 0x00000040

#define PRINTER_STATUS_PAUSED 0x00000001

#define PRINTER_STATUS_PENDING_DELETION 0x00000004

#define PRINTER_STATUS_POWER_SAVE 0x01000000

#define PRINTER_STATUS_PRINTING 0x00000400

#define PRINTER_STATUS_PROCESSING 0x00004000

#define PRINTER_STATUS_SERVER_UNKNOWN 0x00800000

#define PRINTER_STATUS_TONER_LOW 0x00020000

#define PRINTER_STATUS_USER_INTERVENTION 0x00100000

#define PRINTER_STATUS_WAITING 0x00002000

#define PRINTER_STATUS_WARMING_UP 0x00010000



Just convert the hex numbers to ulong and make some constants.

  1. Helpful
  1. David Peace (Powersoft)
  2. Tuesday, 20 November 2018 15:48 PM UTC
Thanks Roland, I had not got enough time to read all the functions. I think that would work perfectly :)
  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Tuesday, 20 November 2018 22:03 PM UTC
  2. PowerBuilder
  3. # 2

Here is a window which I think will work, I am still testing. The case statement is meant to convert the value constants to a string.

forward
global type w_main from window
end type
type cb_process from commandbutton within w_main
end type
type cb_cancel from commandbutton within w_main
end type
type printer_info_6 from structure within w_main
end type
end forward

type printer_info_6 from structure
unsignedlong dwstatus
unsignedlong reserved
end type

global type w_main from window
integer width = 2098
integer height = 1460
boolean titlebar = true
string title = "Printer Status"
boolean controlmenu = true
long backcolor = 67108864
string icon = "AppIcon!"
boolean center = true
cb_process cb_process
cb_cancel cb_cancel
end type
global w_main w_main

type prototypes
Function boolean GetDefaultPrinter ( &
Ref string pszBuffer, &
Ref ulong pcchBuffer ) &
Library "winspool.drv" Alias For "GetDefaultPrinterW"

Function boolean OpenPrinter ( &
string pPrinterName, &
Ref long phPrinter, &
ulong pDefault ) &
Library "winspool.drv" Alias For "OpenPrinterW"

Function boolean ClosePrinter ( &
long hPrinter ) &
Library "winspool.drv"

Function boolean GetPrinter ( &
long hPrinter, &
ulong Level, &
Ref structure pPrinter, &
ulong cbBuf, &
Ref ulong pcbNeeded ) &
Library "winspool.drv" Alias For "GetPrinterW"

end prototypes

type variables
Constant ULong PRINTER_STATUS_BUSY = 512 // 0x00000200
Constant ULong PRINTER_STATUS_DOOR_OPEN = 4194304 // 0x00400000
Constant ULong PRINTER_STATUS_ERROR = 2 // 0x00000002
Constant ULong PRINTER_STATUS_INITIALIZING = 32768 // 0x00008000
Constant ULong PRINTER_STATUS_IO_ACTIVE = 256 // 0x00000100
Constant ULong PRINTER_STATUS_MANUAL_FEED = 32 // 0x00000020
Constant ULong PRINTER_STATUS_NO_TONER = 262144 // 0x00040000
Constant ULong PRINTER_STATUS_NOT_AVAILABLE = 4096 // 0x00001000
Constant ULong PRINTER_STATUS_OFFLINE = 132 // 0x00000080
Constant ULong PRINTER_STATUS_OUT_OF_MEMORY = 2097152 // 0x00200000
Constant ULong PRINTER_STATUS_OUTPUT_BIN_FULL = 2048 // 0x00000800
Constant ULong PRINTER_STATUS_PAGE_PUNT = 524288 // 0x00080000
Constant ULong PRINTER_STATUS_PAPER_JAM = 8 // 0x00000008
Constant ULong PRINTER_STATUS_PAPER_OUT = 16 // 0x00000010
Constant ULong PRINTER_STATUS_PAPER_PROBLEM = 64 // 0x00000040
Constant ULong PRINTER_STATUS_PAUSED = 1 // 0x00000001
Constant ULong PRINTER_STATUS_PENDING_DELETION = 4 // 0x00000004
Constant ULong PRINTER_STATUS_POWER_SAVE = 16777216 // 0x01000000
Constant ULong PRINTER_STATUS_PRINTING = 1024 // 0x00000400
Constant ULong PRINTER_STATUS_PROCESSING = 16384 // 0x00004000
Constant ULong PRINTER_STATUS_READY = 0 // 0x00000000
Constant ULong PRINTER_STATUS_SERVER_UNKNOWN = 8388608 // 0x00800000
Constant ULong PRINTER_STATUS_TONER_LOW = 131072 // 0x00020000
Constant ULong PRINTER_STATUS_USER_INTERVENTION = 1048576 // 0x00100000
Constant ULong PRINTER_STATUS_WAITING = 8192 // 0x00002000
Constant ULong PRINTER_STATUS_WARMING_UP = 65536 // 0x00010000

end variables
on w_main.create
this.cb_process=create cb_process
this.cb_cancel=create cb_cancel
this.Control[]={this.cb_process,&
this.cb_cancel}
end on

on w_main.destroy
destroy(this.cb_process)
destroy(this.cb_cancel)
end on

type cb_process from commandbutton within w_main
integer x = 73
integer y = 64
integer width = 370
integer height = 132
integer taborder = 10
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "Process"
end type

event clicked;PRINTER_INFO_6 lstr_info
Boolean lb_return
Long ll_hPrinter
String ls_null, ls_DefaultPrinter, ls_Status
ULong lul_buffer, lul_needed

// Size the buffer
SetNull(ls_null)
GetDefaultPrinter(ls_null, lul_buffer)
ls_DefaultPrinter = Space(lul_buffer)

// Get the Windows default printer
lb_return = GetDefaultPrinter(ls_DefaultPrinter, lul_buffer)

// open the printer
lb_return = OpenPrinter(ls_DefaultPrinter, ll_hPrinter, 0)

// get the printer status
lb_return = GetPrinter(ll_hPrinter, 6, lstr_info, 8, lul_needed)

choose case lstr_info.dwStatus
case PRINTER_STATUS_READY
ls_Status = "Ready"
case else
ls_Status = "Unknown Status Code: " + String(lstr_info.dwStatus)
end choose

// close the printer
ClosePrinter(ll_hPrinter)

end event

type cb_cancel from commandbutton within w_main
integer x = 1682
integer y = 1216
integer width = 334
integer height = 100
integer taborder = 10
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "Cancel"
boolean cancel = true
end type

event clicked;Close(Parent)

end event

 

Comment
  1. Sivaprakash BKR
  2. Saturday, 24 November 2018 04:53 AM UTC
I get Ready status only, even when out of paper conditions....
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 21 November 2018 11:28 AM UTC
  2. PowerBuilder
  3. # 3

Great stuff Roland, very usefull!

Comment
There are no comments made yet.
Andrew Davis Accepted Answer Pending Moderation
  1. Thursday, 29 November 2018 17:20 PM UTC
  2. PowerBuilder
  3. # 4

Roland

The function is nearly there - I get the default printer

I have a generic text printer which is showing with an exclamation mark on the windows printers ie showing as error then it returns status =2 which is great

However my HP laserjet is showing on its screen out of paper ( I pulled the paper tray out ) - but the function still says ready - I tried putting the printer offline and it still says ready - even though there are several jobs in the queue

Any ideas ?

as always any help is truly appreciated.

 

Andrew

 

Comment
  1. Roland Smith
  2. Thursday, 29 November 2018 19:54 PM UTC
As far as I know the functions I posted code for only talk to the printer queue on your computer, not the printer itself.

To communicate with the printer you would have to use a totally different method. A post I found on StackOverflow suggest using SNMP but that may be for more high end printers. Each printer manufacturer should have there own API for that.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.