Changing Printer settings using the Windows API

I have been working on a program that prints any document you provide to it, as long as there is an application installed on your computer that can handle that document. One issue that we have with it is Duplex printing, i.e. getting the documents to print double-sided. This is a story of how we solved this issue.

The way this problem used to be handled is to install two printers, one set to always print single-sided and another to always print double-sided. Actually a third would be needed to allow the user to keep custom settings for their own preferences. The application would then switch the default printer in Windows to the appropriate printer, depending on the user choice and duplex requirement. We needed to keep the same user interface, but would have liked to have been able to use just the one printer and programmatically set duplex on or off. In this case I was using Visual Basic 6 although it could just as well have been in C#. Neither VB6 nor the .NET framework seem to provide an in-built way to change the Windows printer settings, they let you change the settings for the current session but not persisted.

I found some examples for how to do change a printer’s settings using calls to the Windows API:

How to set duplex printing for Microsoft Word Automation clients
http://support.microsoft.com/kb/828638

Controlling the Printer from Word VBA
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=116

The examples call WINSPOOL.DRV to set the printer properties. When making these calls, one needs to specify the security level required, and in all the examples I found they would use PRINTER_NORMAL_ACCESS:

Const STANDARD_RIGHTS_REQUIRED = 0xF0000
Const PRINTER_ACCESS_USE = 0x00008
Const PRINTER_NORMAL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_USE)

In my testing, this worked fine on a local printer but when trying to change the settings on a network printer I was getting “access denied” errors. After some investigation, I got an explanation for what was happening. The following two articles provided the needed insight:

If you ask for STANDARD_RIGHTS_REQUIRED, you may as well ask for the moon
http://blogs.msdn.com/oldnewthing/archive/2008/02/27/7912126.aspx

Changing printer settings using C# (also see one of the first comments in the thread)
http://www.codeproject.com/KB/dotnet/NET_Printer_Library.aspx?display=PrintAll

In short, the problem was I didn’t have full administrative access to the network printers in question; I could only change local print preferences. To accomodate this, a lower access level needs to be passed to the function call. Here’s the full range of available levels:

'Access levels for interacting with a device
Const DELETE = &H10000
Const READ_CONTROL = &H20000          ' Read device information
Const WRITE_DAC = &H40000             ' Write Device Access Control info
Const WRITE_OWNER = &H80000           ' Change the object owner
' Combining these for full access to the device
Const STANDARD_RIGHTS_REQUIRED = &HF0000

'Access rights to print servers
Const SERVER_ACCESS_ADMINISTER = &H1
Const SERVER_ACCESS_ENUMERATE = &H2
'Access rights for printers
Const PRINTER_ACCESS_ADMINISTER = &H4
Const PRINTER_ACCESS_USE = &H8

For me the following seemed to work:

' Access which allows you to set duplex on or off
Const PRINTER_NORMAL_ACCESS = (READ_CONTROL Or PRINTER_ACCESS_USE)

This would be useful to know when making calls to other API functions where the security levels need to be taken into account, in a network environment.

Here’s the full code, based on the example provided in the article above. I only tested the SetDuplex function.

Option Explicit

Private Type PRINTER_DEFAULTS
   pDatatype As Long
   pDevmode As Long
   DesiredAccess As Long
End Type

Private Type PRINTER_INFO_2
   pServerName As Long
   pPrinterName As Long
   pShareName As Long
   pPortName As Long
   pDriverName As Long
   pComment As Long
   pLocation As Long
   pDevmode As Long               ' Pointer to DEVMODE
   pSepFile As Long
   pPrintProcessor As Long
   pDatatype As Long
   pParameters As Long
   pSecurityDescriptor As Long    ' Pointer to SECURITY_DESCRIPTOR
   Attributes As Long
   Priority As Long
   DefaultPriority As Long
   StartTime As Long
   UntilTime As Long
   Status As Long
   cJobs As Long
   AveragePPM As Long
End Type

Private Type DEVMODE
   dmDeviceName As String * 32
   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 * 32
   dmUnusedPadding As Integer
   dmBitsPerPel As Integer
   dmPelsWidth As Long
   dmPelsHeight As Long
   dmDisplayFlags As Long
   dmDisplayFrequency As Long
   dmICMMethod As Long
   dmICMIntent As Long
   dmMediaType As Long
   dmDitherType As Long
   dmReserved1 As Long
   dmReserved2 As Long
End Type

Private Const DM_ORIENTATION = &H1
Private Const DM_PAPERSIZE = &H2
Private Const DM_PAPERLENGTH = &H4
Private Const DM_PAPERWIDTH = &H8
Private Const DM_DEFAULTSOURCE = &H200
Private Const DM_PRINTQUALITY = &H400
Private Const DM_COLOR = &H800
Private Const DM_DUPLEX = &H1000

Private Const DM_IN_BUFFER = 8
Private Const DM_OUT_BUFFER = 2

Private Const DELETE = &H10000
Private Const READ_CONTROL = &H20000          ' Allowed to read device information
Private Const WRITE_DAC = &H40000             ' Allowed to write device access control info
Private Const WRITE_OWNER = &H80000           ' Allowed to change the object owner
' Combining these for full access to a device  (DELETE + READ_CONTROL + WRITE_DAC + WRITE_OWNER):
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000

Private Const SERVER_ACCESS_ADMINISTER = &H1  ' Access rights to administer print servers.
Private Const SERVER_ACCESS_ENUMERATE = &H2   ' Access rights to enumerate print servers.
Private Const PRINTER_ACCESS_ADMINISTER = &H4 ' Access rights for printers to perform administrative tasks.
Private Const PRINTER_ACCESS_USE = &H8        ' Access rights for printers for general use (printing, querying).

' Access which allows you to set duplex on or off
Private Const PRINTER_NORMAL_ACCESS = (READ_CONTROL Or PRINTER_ACCESS_USE)

Private Const PRINTER_ENUM_CONNECTIONS = &H4
Private Const PRINTER_ENUM_LOCAL = &H2

Private Declare Function ClosePrinter Lib "winspool.drv" _
      (ByVal hPrinter As Long) As Long
Private Declare Function DocumentProperties Lib "winspool.drv" _
      Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
      ByVal hPrinter As Long, ByVal pDeviceName As String, _
      ByVal pDevModeOutput As Long, ByVal pDevModeInput As Long, _
      ByVal fMode As Long) As Long
Private Declare Function GetPrinter Lib "winspool.drv" Alias _
      "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
      pPrinter As Byte, ByVal cbBuf As Long, pcbNeeded As Long) As Long
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 SetPrinter Lib "winspool.drv" Alias _
      "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
      pPrinter As Byte, ByVal Command As Long) As Long
Private Declare Function EnumPrinters Lib "winspool.drv" _
      Alias "EnumPrintersA" _
      (ByVal flags As Long, ByVal name As String, ByVal Level As Long, _
      pPrinterEnum As Long, ByVal cdBuf As Long, pcbNeeded As Long, _
      pcReturned As Long) As Long

Private Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyA" _
      (ByVal RetVal As String, ByVal Ptr As Long) As Long

Private Declare Function StrLen Lib "kernel32" Alias "lstrlenA" _
      (ByVal Ptr As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
      (pDest As Any, pSource As Any, ByVal cbLength As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Declare Function DeviceCapabilities Lib "winspool.drv" _
      Alias "DeviceCapabilitiesA" (ByVal lpDeviceName As String, _
      ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, _
      ByVal dev As Long) As Long

Public Sub SetColorMode(ByVal sPrinterName As String, iColorMode As Long)
   SetPrinterProperty sPrinterName, DM_COLOR, iColorMode
End Sub

Public Function GetColorMode(ByVal sPrinterName As String) As Long
  GetColorMode = GetPrinterProperty(sPrinterName, DM_COLOR)
End Function

Public Sub SetDuplex(ByVal sPrinterName As String, iDuplex As Long)
   SetPrinterProperty sPrinterName, DM_DUPLEX, iDuplex
End Sub

Public Function GetDuplex(ByVal sPrinterName As String) As Long
   GetDuplex = GetPrinterProperty(sPrinterName, DM_DUPLEX)
End Function

Public Sub SetPrintQuality(ByVal sPrinterName As String, iQuality As Long)
   SetPrinterProperty sPrinterName, DM_PRINTQUALITY, iQuality
End Sub

Public Function GetPrintQuality(ByVal sPrinterName As String) As Long
   GetPrintQuality = GetPrinterProperty(sPrinterName, DM_PRINTQUALITY)
End Function

Private Function SetPrinterProperty(ByVal sPrinterName As String, ByVal iPropertyType As Long, _
      ByVal iPropertyValue As Long) As Boolean

   'Code adapted from Microsoft KB article Q230743

    Dim hPrinter As Long          'handle for the current printer
    Dim pd As PRINTER_DEFAULTS
    Dim pinfo As PRINTER_INFO_2
    Dim dm As DEVMODE

    Dim yDevModeData() As Byte        'Byte array to hold contents
                                      'of DEVMODE structure
    Dim yPInfoMemory() As Byte        'Byte array to hold contents
                                      'of PRINTER_INFO_2 structure
    Dim iBytesNeeded As Long
    Dim iRet As Long
    Dim iJunk As Long
    Dim iCount As Long
      
    On Error GoTo cleanup

    pd.DesiredAccess = PRINTER_NORMAL_ACCESS
    iRet = OpenPrinter(sPrinterName, hPrinter, pd)
    If (iRet = 0) Or (hPrinter = 0) Then
       'Can't access current printer. Bail out doing nothing
       Exit Function
    End If

    'Get the size of the DEVMODE structure to be loaded
    iRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
    If (iRet < 0) Then
       'Can't access printer properties.
       GoTo cleanup
    End If

    'Make sure the byte array is large enough
    'Some printer drivers lie about the size of the DEVMODE structure they
    'return, so an extra 100 bytes is provided just in case!
    ReDim yDevModeData(0 To iRet + 100) As Byte
      
    'Load the byte array
    iRet = DocumentProperties(0, hPrinter, sPrinterName, _
                VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)
    If (iRet < 0) Then
       GoTo cleanup
    End If

    'Copy the byte array into a structure so it can be manipulated
    Call CopyMemory(dm, yDevModeData(0), Len(dm))

    If dm.dmFields And iPropertyType = 0 Then
       'Wanted property not available. Bail out.
       GoTo cleanup
    End If

    'Set the property to the appropriate value
    Select Case iPropertyType
    Case DM_ORIENTATION
       dm.dmOrientation = iPropertyValue
    Case DM_PAPERSIZE
       dm.dmPaperSize = iPropertyValue
    Case DM_PAPERLENGTH
       dm.dmPaperLength = iPropertyValue
    Case DM_PAPERWIDTH
       dm.dmPaperWidth = iPropertyValue
    Case DM_DEFAULTSOURCE
       dm.dmDefaultSource = iPropertyValue
    Case DM_PRINTQUALITY
       dm.dmPrintQuality = iPropertyValue
    Case DM_COLOR
       dm.dmColor = iPropertyValue
    Case DM_DUPLEX
       dm.dmDuplex = iPropertyValue
    End Select
      
    'Load the structure back into the byte array
    Call CopyMemory(yDevModeData(0), dm, Len(dm))

    'Tell the printer about the new property
    iRet = DocumentProperties(0, hPrinter, sPrinterName, _
          VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _
          DM_IN_BUFFER Or DM_OUT_BUFFER)

    If (iRet < 0) Then
       GoTo cleanup
    End If

    'The code above *ought* to be sufficient to set the property
    'correctly. Unfortunately some brands of Postscript printer don't
    'seem to respond correctly. The following code is used to make
    'sure they also respond correctly.
    Call GetPrinter(hPrinter, 2, 0, 0, iBytesNeeded)
    If (iBytesNeeded = 0) Then
       'Couldn't access shared printer settings
       GoTo cleanup
    End If
      
    'Set byte array large enough for PRINTER_INFO_2 structure
    ReDim yPInfoMemory(0 To iBytesNeeded + 100) As Byte

    'Load the PRINTER_INFO_2 structure into byte array
    iRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), iBytesNeeded, iJunk)
    If (iRet = 0) Then
       'Couldn't access shared printer settings
       GoTo cleanup
    End If

    'Copy byte array into the structured type
    Call CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))

    'Load the DEVMODE structure with byte array containing
    'the new property value
    pinfo.pDevmode = VarPtr(yDevModeData(0))
      
    'Set security descriptor to null
    pinfo.pSecurityDescriptor = 0
     
    'Copy the PRINTER_INFO_2 structure back into byte array
    Call CopyMemory(yPInfoMemory(0), pinfo, Len(pinfo))

    'Send the new details to the printer
    iRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)

    'Indicate whether it all worked or not!
    SetPrinterProperty = CBool(iRet)

cleanup:
   'Release the printer handle
   If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)
      
   'Flush the message queue. If you don't do this,
   'you can get page fault errors when you try to
   'print a document immediately after setting a printer property.
   For iCount = 1 To 20
      DoEvents
   Next iCount
   End Function

Private Function GetPrinterProperty(ByVal sPrinterName As String, ByVal iPropertyType As Long) As Long

  'Code adapted from Microsoft KB article Q230743

  Dim hPrinter As Long
  Dim pd As PRINTER_DEFAULTS
  Dim dm As DEVMODE

  Dim yDevModeData() As Byte
  Dim iRet As Long
      
  On Error GoTo cleanup
      
  pd.DesiredAccess = PRINTER_NORMAL_ACCESS
      
  'Get the printer handle
  iRet = OpenPrinter(sPrinterName, hPrinter, pd)
  If (iRet = 0) Or (hPrinter = 0) Then
     'Couldn't access the printer
      Exit Function
  End If

  'Find out how many bytes needed for the printer properties
  iRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
  If (iRet < 0) Then
     'Couldn't access printer properties
      GoTo cleanup
  End If

  'Make sure the byte array is large enough, including the
  '100 bytes extra in case the printer driver is lying.
  ReDim yDevModeData(0 To iRet + 100) As Byte
      
  'Load the printer properties into the byte array
  iRet = DocumentProperties(0, hPrinter, sPrinterName, _
              VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)
  If (iRet < 0) Then
     'Couldn't access printer properties
     GoTo cleanup
  End If



  'Copy the byte array to the DEVMODE structure
  Call CopyMemory(dm, yDevModeData(0), Len(dm))

  If Not dm.dmFields And iPropertyType = 0 Then
     'Requested property not available on this printer.
     GoTo cleanup
  End If

  'Get the value of the requested property
  Select Case iPropertyType
  Case DM_ORIENTATION
     GetPrinterProperty = dm.dmOrientation
  Case DM_PAPERSIZE
     GetPrinterProperty = dm.dmPaperSize
  Case DM_PAPERLENGTH
     GetPrinterProperty = dm.dmPaperLength
  Case DM_PAPERWIDTH
     GetPrinterProperty = dm.dmPaperWidth
  Case DM_DEFAULTSOURCE
     GetPrinterProperty = dm.dmDefaultSource
  Case DM_PRINTQUALITY
     GetPrinterProperty = dm.dmPrintQuality
  Case DM_COLOR
     GetPrinterProperty = dm.dmColor
  Case DM_DUPLEX
     GetPrinterProperty = dm.dmDuplex
  End Select
      
cleanup:
   'Release the printer handle
   If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)

End Function
This entry was posted in Technology and tagged , , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

12 Comments

  1. Posted 2009/11/03 at 6:44 pm | Permalink

    Thank you so much! This was exactly what I was looking for!

  2. Vikram
    Posted 2011/07/24 at 3:07 pm | Permalink

    Hi,
    I am trying to write this in VB.Net, but I keep getting an access denied error if I don’t use PRINTER_ALL_ACCESS. However, with VBA, I can use (READ_CONTROL Or PRINTER_ACCESS_USE), and it works like a charm. Any ideas?

    Thanks
    Vikram

  3. Ylitka Ul'yana
    Posted 2011/10/03 at 10:09 am | Permalink

    GetProperty function actually returns the value, i’ve set for duplex printing, but it for some reason doesn’t change anything while using Application.ActiveWorkBook.PrintOut in Excel. What’s wrong?

  4. Ylitka Yl'jana
    Posted 2011/10/03 at 12:42 pm | Permalink

    So it works with MS Word documents, when using PrintOut with named parameter Background:=False
    But there is no such parameter in Excel PrintOut…. Any ideas what to do?

  5. Ylitka Yl'jana
    Posted 2011/10/03 at 2:48 pm | Permalink

    The only working idea is to avoid using ActivePrinnter in Excel, it’s settings are being saved by the application just after it’s start and there is no way to make Excell accept new settings for further prints.

  6. Ylitka Yl'jana
    Posted 2011/10/04 at 2:31 pm | Permalink

    Now I’m trying to write this in С#, and I also get Access Denied Error. Has anybody solved the problem?

  7. Ylitka Yl'jana
    Posted 2011/10/05 at 12:25 pm | Permalink

    It’s interesting, that I achieved success in vba with the help of this article. But while debugging SetPrinter function returns zero or false, however the duplex property of my local printer driver changes to needed value and everything works as I expect.

    I copied the code from http://www.codeproject.com/KB/dotnet/NET_Printer_Library.aspx?display=PrintAll
    for C# and rewrote it following the same structure as in VBA. I deleted a check of the SetPrinter result value but for some reason that didn’t influenced my local driver duplex property. Then I compared values of PRINTER_NORMAL_ACCESS in VBA and С#, they turned to be different. In VBA PRINTER_NORMAL_ACCESS equels 131080 as Int. I took this value and placed it as a number to С# code and everything started working. The last problem is to understand how to count PRINTER_NORMAL_ACCESS in C# and how to make SetPrinter function return TRUE. That would make the code look better.

  8. coucou78
    Posted 2012/03/02 at 1:35 pm | Permalink

    YOUR code is working , sure. (i’m working with PRINTER_INFO_8 in order to have printer settings by default)

    BUT :

    i cannot explain why this shity printer setting work like that, is there an official how to ?

  9. Posted 2012/08/01 at 11:06 pm | Permalink

    It’s unbelievably frustrating to install these printers. I’m going to copy the code from above and hopefully I don’t have anymore issues. I just cant’ understand what i’m doing wrong?

  10. Bagir
    Posted 2012/10/04 at 1:36 pm | Permalink

    This code changes the general printer settings. And I would like to change the settings of the current session, which live is still open Excel.
    Ylitka Yl’jana you do it?

  11. Michael Asteriou
    Posted 2014/03/27 at 12:25 pm | Permalink

    Excellent!
    This was exactly the information I was looking for!

    Thank you very much!

    Michael

  12. Ludo Soete
    Posted 2017/10/19 at 5:32 pm | Permalink

    Hi,
    Exellent code, but I cant change the paperorientation to print in Landscape mode. it keep on going in Portrait.
    I’m using a Ricoh printer.


    Sub ChangePaperOrientation(PrinterName As String, lPaperOrientation As Long)

    If lPaperOrientation = Landscape_ Then 'Landscape_ = 1
    SetPaperOrientation PrinterName, Landscape_
    Else 'Portrait_ = 2
    SetPaperOrientation PrinterName, Portrait_
    End If

    ' Orientation

    End Sub

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
*

Time limit is exhausted. Please reload the CAPTCHA.