Setting default printer via DEVMODE struct and EnumPrinters 
Author Message
 Setting default printer via DEVMODE struct and EnumPrinters

I searched the newsgroups for an answer to a problem I had when trying
to select a new default printer in an MFC app.  My method was based on
Microsoft doc...  Q166129 HOWTO: Print Directly to a Non-Default
Printer in MFC.

Problem was I was getting a NULL DEVMODE pointer returned - so
couldn't proceed, code crashed etc.

I then tried using DocumentPRoperties to get DEVMODE, like in
Q167345 HOWTO: Modify Printer Settings with DocumentProperties()

Still no luck.  OpenPrinter call was working for all our network
printers here, but the first call to DocumentProperties to ask how
many bytes is required returned -1.

Finally, assisted by some code kindly emailed to me from another
newsgroup poster I figured out what the problem was - hence I'm
posting this to advise others that may hit the same problem.

It turns out that this is what was happening....

I was using a printer pathname as per what you get when you browse our
network for printers eg when installing a driver on a newly built
Windows PC.
These are names like    \\boronia\SSDLaser
When I use a string like that it does the OpenPrinter call
successfully, but I can't get a DEVMODE struct from subsequent calls
as per code in Q166129.  And when I call DocumentProperties as in
Q167345, it returns a -1 failure and I still can't get a DEVMODE
structure.

However, if I use EnumPrinters to get the printer name string, it
gives a friendlier name string (like  "HP LaserJet 6P on Boronia").
And if I hand in that string then everything works.

Hmmmm.  I'm a bit annoyed at the MS docs - in the sample code of the
Q166129 HOWTO, it actually uses
         (_T("\\\\RED-PRN-25\\PRIV0006")
as its example when calling the code routine I was struggling with....
so my use of  \\boronia\SSDLaser  for a printer name string was not
unnatural.  Specially confusing because it works in OpenPrinter, where
a random
string that isn't a valid printer fails.  Somehow, however, the handle
returned by OpenPrinter is not as good as the one you get for the same
printer when calling OpenPrinter with the enum'd name string.

Anyway ... be warned.. and if anyone from MS is reading this, please
update Q166129 to advise of this show stopping gotcha.

Enjoy
DaveB



Tue, 09 Nov 2004 09:44:45 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. EnumPrinters, DocumentProperties & default DevModes

2. EnumPrinters, DocumentProperties & default DevModes

3. Changing printer settings, DEVMODE And C#

4. Windows Default Printers and Default Printer Setups

5. Set Default Printer

6. Set default printer

7. how to Set Default Printer

8. Q: How to set default printer bin?

9. Problems Setting the Default printer with Win32 API SDK

10. Help : How to set the default printer

11. Setting the Default Printer

12. how to Set Default Printer

 

 
Powered by phpBB® Forum Software