Microsoft KB Archive/135387

-

The information in this article applies to:


 * Microsoft Windows Software Development Kit (SDK) 3.1
 * Microsoft Win32 Application Programming Interface (API), used with:
 * Microsoft Windows NT, versions 3.5, 3.51, 4.0
 * 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.

Notes to Keep in Mind
  The Device value you get or set actually contains three elements separated by commas as follows:  , ,

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 in the Microsoft Knowledge Base:

"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