Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC

Setting the Default Printer Programmatically in an MFC Application

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
11 Jun 2002CPOL2 min read 240.8K   4.1K   50   41
How to change the default printer in MFC and save/restore this setting to/from the registry

Overview

In a standard MFC application, the system wide default printer will be setup as default each time your application starts. This is done during your CWinApp objects initialization, when it calls the function CWinApp::UpdatePrinterSelection(). This sets up the m_hDevMode and m_hDevNames global handles (as in global memory handles used with GlobalLock/Unlock) which CWinApp uses to manage the selected printer.

I have provided a wrapper class for enumPrinters, which is used to gather information on the locally installed printers which is needed when switching printers. It can also set the printers when called by a function in your CWinApp derived class and passing through m_hDevMode and m_hDevNames as references, so they can be changed.

When switching printers programmatically, you need to know:

  • The printer's name, e.g., HP Laserjet 4L
  • The port the printer is installed on, e.g., LPT1:
  • The spooler that will dump the printout to the printer, usually winspool

Details

The class CEnumPrinters encapsulates the required functionality needed to query for the locally setup printers. This list can then be read/used to select new printers and set them as the new default printer to be used by the application.

Example list of printers

Restoring the Selected Printer

You can save the selected printer to the registry and restore it also. To do this, you need to place calls to CEnumPrinters::SavePrinterSelection() and CEnumPrinters::RestorePrinterSelection() in your application's ExitInstance and InitInstance procedures.

The information saved is:

  • The printer name
  • The spooling device
  • The port name
  • The page orientation (portrait or landscape)

Class Interface

The following functions are used in the class:

  • int GetPrinterCount() ;
  • CString GetPrinterName(int index) ;
  • CString GetPrinterLocation(int index) ;
  • CString GetPrinterShareName(int index) ;
  • CString GetPrinterPortName(int index) ;
  • void ReadLocalPrinters() ;

The above functions enumerate the list of locally installed printers - automatically called in class constructor.

  • bool SetNewPrinter(HANDLE& hDevMode, HANDLE& hDevNames, const CString& PrinterName, const CString& PrinterSpooler, const CString& PrinterPort) ;

Use the function above when switching to a printer by Name, driver, port

  • bool SetNewPrinter(HANDLE& hDevMode, HANDLE& hDevNames, int index) ;

Use the function above when switching to a printer in the local list by index

  • bool SetPrintOrientation(HANDLE &hDevMode, int mode) ;

Use the above function to switch between portrait and landscape page orientations.

  • bool SavePrinterSelection(HANDLE &hDevMode, HANDLE& hDevNames) ;
  • bool RestorePrinterSelection(HANDLE &hDevMode, HANDLE& hDevNames) ;

These procedures save/restore the selected printer from the registry (key PrinterConfig)

Future Enhancements or Coding Projects

  • Add functionality to query network printers
  • Add functionality to add a new printer

Enjoy!

Revision History

  • 12th June, 2002 - Initial revision

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

Comments and Discussions

 
QuestionThis was very helpful - thanks Pin
DavidBaird11-Sep-18 20:25
DavidBaird11-Sep-18 20:25 
GeneralEnumPrinters identifier not found Pin
bhat vinay20-Oct-10 2:11
bhat vinay20-Oct-10 2:11 
QuestionWork with job ? Pin
PAOS26-Feb-07 2:48
PAOS26-Feb-07 2:48 
GeneralSet app to use non-default printer Pin
techjeff21-May-06 10:33
techjeff21-May-06 10:33 
GeneralGreat Stuff !!! Pin
Warren Stevens21-Aug-05 8:05
Warren Stevens21-Aug-05 8:05 
Generalneed to handle WM_SETTINGCHANGE Pin
freddycam23-May-05 4:37
freddycam23-May-05 4:37 
GeneralDoesn't work in Unicode :( Pin
Vitaly Tomilov31-Mar-05 3:27
Vitaly Tomilov31-Mar-05 3:27 
GeneralUse Without Document/View Framwork Pin
rtek3-Mar-05 6:30
rtek3-Mar-05 6:30 
GeneralSet Default/Global Printer Pin
DanYELL21-Sep-04 5:08
DanYELL21-Sep-04 5:08 
GeneralChanging printers. Pin
Jim Barrett5-Jul-04 2:00
Jim Barrett5-Jul-04 2:00 
GeneralCalling SetPrintOrientation Pin
Member 110107815-May-04 14:34
Member 110107815-May-04 14:34 
GeneralRe: Calling SetPrintOrientation Pin
Roger Allen16-May-04 22:24
Roger Allen16-May-04 22:24 
GeneralRe: Calling SetPrintOrientation Pin
rtek17-May-04 6:39
rtek17-May-04 6:39 
GeneralRe: Calling SetPrintOrientation Pin
Mr-X26-Nov-05 14:47
Mr-X26-Nov-05 14:47 
GeneralGreat Job! Pin
Frank Kobs8-Mar-04 22:47
Frank Kobs8-Mar-04 22:47 
Just what I needed! It's really a plug & play class.
include it, compile, and it works.
Just had to add some NULL handle checks.

Keep up your good articles about printing Smile | :)


GeneralGreat Stuff !! Pin
Vijay Bansal5-Feb-04 5:30
Vijay Bansal5-Feb-04 5:30 
GeneralGreat Job Pin
JohnLI3-Oct-03 5:55
JohnLI3-Oct-03 5:55 
GeneralGod job Pin
GodSaveMe3-Jul-03 3:07
GodSaveMe3-Jul-03 3:07 
GeneralGeting max paper Pin
Pezhvak Khoshbakht29-Jan-03 23:39
Pezhvak Khoshbakht29-Jan-03 23:39 
GeneralUNICODE support Pin
erand19-Dec-02 5:49
erand19-Dec-02 5:49 
Questionset pagesize? Pin
wxzhao12-Dec-02 16:20
wxzhao12-Dec-02 16:20 
GeneralEnumJobs cannot see multiple copies in Word2000 Pin
Rasho13-Oct-02 21:47
Rasho13-Oct-02 21:47 
Generalservant of printers Pin
Xjenny_62-Oct-02 1:31
Xjenny_62-Oct-02 1:31 
GeneralAddional functionality Pin
Roger Allen30-May-02 0:26
Roger Allen30-May-02 0:26 
QuestionNetworked Printers? Pin
Paresh Solanki8-May-02 0:35
Paresh Solanki8-May-02 0:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.