The usage of DocumentProperties
and SetPrinter
API functions gives an opportunity to change the printer settings. But, when using them, I could not have a stable method of changing settings on all Windows OS versions. Below, all possible methods of changing printer parameters will be described and also the solution of how to obtain the stabler behavior for different Windows OS versions. In this article, I will describe the method of setting the printer settings and the problems I faced on different OS versions.
Changing Printer Settings
You can change the printer settings with the help of API:
DocumentProperties
- retrieves and changes the printer parameters;SetPrinter
- determines data for printer, changes the state, and also can manage the printing and tasks.
Changing Settings Using the DocumentProperties Function
To use the DocumentProperties
function to change the printer settings, you should do as follows:
- Get the number of bytes required for the
DEVMODE
structure:
where
hPrinter
– a HANDLE to your local printer that can be obtained by calling OpenPrinter
or AddPrinter
;szPrinterName
- name of the local printer.
- Allocate memory for the
DEVMODE
structure:
- Get the current printer settings with the
DM_OUT_BUFFER
parameter:
DM_OUT_BUFFER
- points that the function will write the current settings to thepDevMode
structure.
- Change the required parameters:
- The next call of
DocumentProperties
withDM_IN_BUFFER
andDM_OUT_BUFFER
parameters configures settings for the printer. The returned structure can be used as an argument in the call of theCreateDC
function:
The utilizing of the DocumentProperties
function doesn’t always make it possible to change the printer settings on different OS versions. Let’s take a look at the solution of this problem below.
Changing Settings Using the SetPrinter Function
To use the SetPrinter
function to change the printer settings, do as follows:
- Call the
GetPrinter
function to get the size of thePRINTER_INFO_2
structure:
where
hPrinter
- a HANDLE to your local printer that can be obtained by calling OpenPrinter or AddPrinter;- 2 - a level, which indicates that the size of the
PRINTER_INFO_2
structure is inquired and that it contains the detailed information about the printer (for more detailed information about different structure types, see MSDN); dwNeeded
- size of thePRINTER_INFO_2
structure in bytes.
- Allocate memory for the
PRINTER_INFO_2
structure:
- Get the
PRINTER_INFO_2
structure:
- Change the required parameters:
- Update data:
- Update the printer information by calling the
SetPrinter
function:
On some operating systems, the call of the SetPrinter
function causes the program crash or this function do not work. The research of this problem brought me to the following results.
Before calling the SetPrinter
function, call the DocumentProperties
function with some additional actions to update the DEVMODE
structure. The following piece of code demonstrates the methods of settings updating for different operating systems:
- Let’s consider the function:
This function returns the modified DEVMODE
structure after the the pSrcDevMode
structure was used with the help of the DocumentProperties
API function.
Using the method described above I managed to obtain the stable method to apply the settings to local printers on different Windows OS versions.
Description of the Project Modules
PrinterUtils.h, PrinterUtils.cpp
The function that returns the DEVMODE
structure after applying the pSrcDevMode
structure:
The function that defines the DEVMODE printer structure for the current OS:
The function that performs copying from fromDevMode
to toDevMode
by fields:
The fucntion that sets the settings with devMode
to the printer with the szPrinterName
name:
WinVerUtils.h, WinVerUtils.cpp contain the functions for checking of the current OS version. For example:
WinErrorUtils.h contains the function of obtaining the error code description:
traceUtils.h contains the macros for the displaying of the text and error description in the console.