Click to See Complete Forum and Search --> : How to change to windows Standard printer?


Markus Grossmann
April 21st, 1999, 07:01 AM
I need to change the standard windows printer from my VC++ (MFC)-programm. Does'n anybody know, how to do this?

Greetings
Markus Grossmann

Clive Walker
April 21st, 1999, 07:44 AM
Hello...

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.

Example:

// 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:

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

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.

Regards

Clive Walker

Markus Grossmann
April 21st, 1999, 08:46 AM
Thanks, that seems to help!

As far as I can see, I don't need to know the port (e.g. NE01), put I must not leave the print processor. And thats fine so, because I don't get the port with a EnumPrinters-call (Level 2), but I do get the processor.

What I really need is, to automaticaly print a CHtmlView on a specified printer without rising the PrinterDlg. I can do this now by changing the standard-printer to the specified one and then call an OleCommandTarget->Exec(NULL,OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER ,NULL,NULL).
Afterwards I rechange the standard printer.

Any better ideas?? :-)

Greatings
Markus Grossmann