HOWTO: Get and Set the Default Printer in Windows

Last reviewed: March 4, 1998
Article ID: Q135387
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows version 3.1
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.5, 3.51 - Microsoft Windows 95

SUMMARY

In all versions of Windows, the appropriate way to get the default printer is to use GetProfileString, and the appropriate way to set the default printer is to use WriteProfileString. This works whether the default printer information is stored in the WIN.INI file or in the registry.

MORE INFORMATION

Notes to Keep in Mind

  • The Device value you get or set actually contains three elements separated by commas as follows:

          <printer name>,<driver name>,<port>
    

    For example:

          My Printer,HPPCL5MS,lpt1:
    
  • When setting the default printer, you must specify valid names for these elements. That is, you must specify a valid printer, driver, and port. If not, programs such as Print Manager may set the printer back to the previous valid printer, and other programs may become very confused. You can use the EnumPrinters API to retrieve the printer name, driver name, and port name of all available printers.
  • Windows 95 and Windows NT map most .INI file references to the registry. Because of this mapping, GetProfileString and WriteProfileString still function as they do under 16-bit Windows (Microsoft Windows and Windows for Workgroups).
  • After setting the default printer, notify all other applications of the change by broadcasting the WM_WININICHANGE message.

Sample Code

   // This code uses a sample profile string of "My Printer,HPPCL5MS,lpt1:"
   // To get the default printer for Windows 3.1, Windows 3.11,
   // Windows 95, and Windows NT use:
   GetProfileString("windows", "device", ",,,", buffer, sizeof(buffer));

   -----

   // To set the default printer for Windows 3.1 and Windows 3.11 use:
   WriteProfileString("windows", "device", "My Printer,HPPCL5MS,lpt1:");
   SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, 0L);

   -----

   // To set the default printer for Windows 95 use:
   WriteProfileString("windows", "device", "My Printer,HPPCL5MS,lpt1:");
   SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0L,
   (LPARAM)(LPCTSTR)"windows", SMTO_NORMAL, 1000, NULL);

   -----

   // To set the default printer for Windows NT use:
   /* Note printer driver is usually WINSPOOL under Windows NT */
   WriteProfileString("windows", "device", "My Printer,WINSPOOL,lpt1:");
  SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0L, 0L,
  SMTO_NORMAL, 1000, NULL);

There are two circumstances where the code won't work:

  1. If the customer leaves out the SendMessage, no other application will recognize the change in the .INI settings.

  2. If a different 32-bit application does not handle the WIN.INI change message, then it will appear to that application as if the default printer has not been changed. The user will need to exit and re-enter Windows 95 to have the other application recognize the printer change.

This is the preferred method of changing the printer if the code is to be platform independent; this method will work on Windows 3.1, Windows 95 and Windows NT.

For additional information, please see the following article(s) in the Microsoft Knowledge Base:

   ARTICLE-ID: Q140560
   TITLE     : How to Set the Default Printer Programmatically in Windows
               95


Additional query words: 3.11
Keywords : GdiPrn kbcode kbprint
Version : WINNT:3.1,3.5,3.51,4.0; WIN95
Platform : Win95 winnt
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 4, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.