Real Software Forums

Display printer properties
Page 1 of 1

Author:  spacewalker [ Thu Feb 23, 2012 8:15 am ]
Post subject:  Display printer properties


I try to display the properties for a printer
For this I use the WINAPI function "PrinterProperties"

My problem is that instead of the printerproperties I only get the printer version-information.
But I have an idea of what the problem might be. I use this API-Call :
iReturn=OpenPrinter(sPrintername, iPrinterHandle, 0)

I think that instead of 0 I need to pass a "PRINTER_DEFAULTS" structure\memoryblock to the function and set "DesiredAccess=PRINTER_ALL_ACCESS"

The "PRINTER_DEFAULTS" structure has reference to another structure: DEVMODE

So I need some input about how to pass the PRINTER_DEFAULTS / DEVMODE to OpenPrinter()

Here my code:
Declare Function OpenPrinter _
Lib "winspool.drv" Alias "OpenPrinterA" ( _
PrinterName As Cstring, _
byRef phPrinter As integer, _
pDefault As integer) As integer

Declare Function ClosePrinter _
Lib "winspool.drv" ( _
hPrinterhandle As integer ) As integer

Declare Function PrinterProperties _
Lib "winspool.drv" ( _
windowHandle As Integer, Printerhandle As Integer) As Integer

dim iReturn as integer
dim iPrinterHandle as Integer
dim sPrintername as String

sPrintername = EditField1.Text //"EPSON Stylus Series"
iReturn = OpenPrinter(sPrintername, iPrinterHandle, 0) // <------------------ the 0 here may be the problem

'**iReturn is <>0 so the OpenPrinter call works

'**this should open the printer properties dialogbox....but it only displays Printer/Driver Version
'iReturn = PrinterProperties (window1.Handle,iPrinterHandle)
iReturn = PrinterProperties (0,iPrinterHandle)

iReturn= closePrinter (iPrinterHandle)

I use XP


Author:  MonkeybreadSoftware [ Fri Feb 24, 2012 11:35 am ]
Post subject:  Re: Display printer properties

What are you doing?
I have a lot of print related classes for windows in our plugin and I could add you this function easily.


Author:  spacewalker [ Fri Feb 24, 2012 12:49 pm ]
Post subject:  Re: Display printer properties


I need to change printer properties like tray or papersize etc.
For this I want to display the "printer properties dialog" or optionally the "printer setup dialog"
(This is selectable with "PD_PRINTSETUP")

The changes to the properties should be for the next print job and optionally I need the changes to be saved permanently
(I'm not sure but here it seems that the DEVMODE structure comes into play?)

I've found "PrintDlg" API function :

But with PrintDlg the changes made in the printer properties are only for a particular print job and not permanent
("Displays a Print Dialog Box or a Print Setup dialog box. The Print dialog box enables the user to specify the properties of a particular print job.")

But I'd need an option that allows to save the changes permanently

There is another Function "PrintDlgEx" :

But like PrinterDlg the change made to the properties are not permanent but only for "a particular print job")
("Displays a Print property sheet that enables the user to specify the properties of a particular print job")

And there is also a function "DocumentProperties" which allows to change printer settings, too
Maybe this is the way to go ?
(at least in the description I can not find anything abgut a "particular print job")

maybe we need a combination of PrintDlg / PrintDlgEx and DocumentProperties ?


Author:  charonn0 [ Fri Feb 24, 2012 9:17 pm ]
Post subject:  Re: Display printer properties

Here's a function I had laying around. It gets the name of the default printer and opens the printer properties dialog for that printer. If you already have code to get the names of printers other than the default then you can easily modify this code to work with arbitrary printer names.

A few notes:
This function returns an integer. 0 = success; anything else is the last error code reported by Windows. For example, if the Print Spooler service isn't running, this function returns error 1722.

You can pass a window to this function. The passed window then serves as the parent window of the printer dialog window.
If no window is passed then the desktop serves as the parent window.

Declare Function GetLastError Lib "Kernel32" () As Integer
Declare Function GetDefaultPrinterW Lib "WinSpool.Drv" (buffer As Ptr, ByRef bufferSize As Integer) As Boolean
Declare Function OpenPrinterW Lib "WinSpool.Drv" (PrinterName As WString, ByRef PrinterHandle As Integer, defaults As Ptr) As Boolean
Declare Function PrinterProperties Lib "WinSpool.Drv" (ParentWindow As Integer, PrinterHandle As Integer) As Boolean
Declare Function ClosePrinter Lib "WinSpool.Drv" (PrinterHandle As Integer) As Boolean

Dim requiredSize As Integer
Dim parentHandle As Integer
If ParentWindow <> Nil Then parentHandle = ParentWindow.Handle
Call GetDefaultPrinterW(Nil, requiredSize) //First call GetDefaultPrinter with Nil to get the length of the printer's name
requiredSize = requiredSize * 2
Dim mb As New MemoryBlock(requiredSize) //Then make our buffer the right size
If GetDefaultPrinterW(mb, requiredSize) Then
Dim printerHandle As Integer
If OpenPrinterW(mb.WString(0), printerHandle, Nil) Then //Get a handle to the printer
Call PrinterProperties(parentHandle, printerHandle)
Call ClosePrinter(printerHandle)
Return 0
End If
End If

Return GetLastError()

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group