Click here to Skip to main content
12,816,775 members (33,896 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 2 May 2002

Setting the default printer programmatically in an MFC application

, 11 Jun 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
How to change the default printer in MFC and save/restore this setting too/from the registry


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 initialisation, 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 printers 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


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 applications 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 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

    12 Jun 2002 - Initial Revision


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


    About the Author

    Roger Allen
    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][/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

    You may also be interested in...


    Comments and Discussions

    GeneralEnumPrinters identifier not found Pin
    bhat vinay20-Oct-10 3:11
    memberbhat vinay20-Oct-10 3:11 
    QuestionWork with job ? Pin
    PAOS26-Feb-07 3:48
    memberPAOS26-Feb-07 3:48 
    GeneralSet app to use non-default printer Pin
    techjeff21-May-06 11:33
    membertechjeff21-May-06 11:33 
    Thanks for the write up and the code. I have a challenge: I can't control the source code for the application. I want to set this application so that it will print to a "virtual printer" I installed in Windows. The virtual printer is something my company co-develops.

    - My printer X is my Windows default printer
    - My printer Y is my weird dot matrix printer I use only with one application
    - I want my one application to use only the Y printer, to make it easier for computer illiterates to just "print", and they do so with the right printer.

    What do you recommend?
    - Are there any utilities that can start with my one program to force it to use only this printer (the non-default dot matrix printer)?
    - Can a utility be created to run and scan for a windows print event coming from the one program?
    - Are there any .INI modifications I could use?
    - Are there any other tweaks I can make in Windows?

    Your help is much appreciated. I Googled for 3 hours yesterday and couldn't find what I was looking for. I am not an expert in printing technology. Frown | :(

    Run to the hills!
    GeneralGreat Stuff !!! Pin
    Warren Stevens21-Aug-05 9:05
    memberWarren Stevens21-Aug-05 9:05 
    Generalneed to handle WM_SETTINGCHANGE Pin
    fredwobus23-May-05 5:37
    sussfredwobus23-May-05 5:37 
    GeneralDoesn't work in Unicode :( Pin
    VitalyTomilov31-Mar-05 4:27
    memberVitalyTomilov31-Mar-05 4:27 
    GeneralUse Without Document/View Framwork Pin
    rtek3-Mar-05 7:30
    memberrtek3-Mar-05 7:30 
    GeneralSet Default/Global Printer Pin
    DanYELL21-Sep-04 6:08
    memberDanYELL21-Sep-04 6:08 
    GeneralChanging printers. Pin
    Jim Barrett5-Jul-04 3:00
    memberJim Barrett5-Jul-04 3:00 
    GeneralCalling SetPrintOrientation Pin
    RtekRyan15-May-04 15:34
    memberRtekRyan15-May-04 15:34 
    GeneralRe: Calling SetPrintOrientation Pin
    Roger Allen16-May-04 23:24
    memberRoger Allen16-May-04 23:24 
    GeneralRe: Calling SetPrintOrientation Pin
    rtek17-May-04 7:39
    memberrtek17-May-04 7:39 
    GeneralRe: Calling SetPrintOrientation Pin
    Mr-X26-Nov-05 15:47
    memberMr-X26-Nov-05 15:47 
    GeneralGreat Job! Pin
    Frank Kobs8-Mar-04 23:47
    memberFrank Kobs8-Mar-04 23:47 
    GeneralGreat Stuff !! Pin
    Vijay Bansal5-Feb-04 6:30
    memberVijay Bansal5-Feb-04 6:30 
    GeneralGreat Job Pin
    JohnLI3-Oct-03 6:55
    memberJohnLI3-Oct-03 6:55 
    GeneralGod job Pin
    GodSaveMe3-Jul-03 4:07
    memberGodSaveMe3-Jul-03 4:07 
    GeneralGeting max paper Pin
    Pezhvak30-Jan-03 0:39
    memberPezhvak30-Jan-03 0:39 
    GeneralUNICODE support Pin
    erand19-Dec-02 6:49
    membererand19-Dec-02 6:49 
    Questionset pagesize? Pin 17:20 17:20 
    GeneralEnumJobs cannot see multiple copies in Word2000 Pin
    rasho13-Oct-02 22:47
    sussrasho13-Oct-02 22:47 
    Generalservant of printers Pin
    Xjenny_62-Oct-02 2:31
    memberXjenny_62-Oct-02 2:31 
    GeneralAddional functionality Pin
    Roger Allen30-May-02 1:26
    memberRoger Allen30-May-02 1:26 
    QuestionNetworked Printers? Pin
    Paresh Solanki8-May-02 1:35
    memberParesh Solanki8-May-02 1:35 
    AnswerRe: Networked Printers? Pin
    Paresh Solanki8-May-02 1:37
    memberParesh Solanki8-May-02 1:37 

    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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.170308.1 | Last Updated 12 Jun 2002
    Article Copyright 2002 by Roger Allen
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid