HOWTO: Get and Set the Default Printer in WindowsID: Q135387
|
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.
<printer name>,<driver name>,<port>
For example:
My Printer,HPPCL5MS,lpt1:
// 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:
Q140560 How to Set the Default Printer Programmatically in Windows 95
Additional query words: 3.11
Keywords : kbcode kbprint kbNTOS350 kbNTOS351 kbNTOS400 kbSDKWin32 kbDSupport
Version : WINDOWS:3.1,95; winnt:3.5,3.51,4.0
Platform : WINDOWS winnt
Issue type : kbhowto
|
Last Reviewed: August 24, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |