You are here:

C++/Setting Printer

Advertisement


Question
Hi Ralph,
  Thanks for the helpful reply of yours.It was quite educating.The irony is that i have all those methods.My problem is that i cant use Startpage/endpage since i m using the webbrwoser's execWb method to print a page using the particular printer which uses the default printer of the system.In this case i m unable to use the metohs mentioned by you.Can i anyway use SetPrinter() to set the printer programitcaly for the application?

Thanks again.

-------------------------
Followup To
Question -
Hi!
 I have more than one printer installe don my PC.through visucall c++ ,i want to print a job to a prticular printer, in windows 2000.How can i do that using Setprinter().

thanks
Answer -
I presume you mean that you wish to do this using the Win32 GDI printer API functions, such as SetPrinter. I am not sure from your question if you know what SetPrinter does or not, or how much you know about printing under the MS Windows GDI. I shall assume that you have very little knowledge and apologise now if you already know some or all of what follows.

First let's deal with SetPrinter. SetPrinter sets the data or state of a particular printer (such as pause, resume, clear). It does _not_ set which printer to use for a print job. If you read the documentation on SetPrinter you will note that you have to pass it a HANDLE to a previously opened or added printer - and you use the functions OpenPrinter and AddPrinter to achieve these tasks. The only way SetPrinter might be used in the way you wish is to set a particular printer as the default printer. However, this seems to be a waste of time to me as you would then know the name of the printer and could use it directly anyway! Also it would modify your system unless of course you reset the original default printer afterwards...

Next I shall point you in the direction of the main MS Windows GDI printing and print spooler documentation and I suggest you read through it if you have not done so already. You will notice that before you get to the reference material on SetPrinter etc... there are sections leading you through the general philosophy behind GDI printing and how to go about printing. If you have a local copy of the MSDN Library then you can find the GDI printing reference material in the Windows GDI section under Printing and Print Spooler - for the July 2004 edition this is on the documentation path Graphics and Multimedia / Windows GDI / SDK Documentation / Windows GDI / Printing and Print Spooler. If you do not have a local copy of the MSDN library to hand then you can find it online at http://www.msdn.microsoft.com/library/default.asp - the printing documentation is on the documentation path Win32 and COM Development / Graphics and Multimedia / GDI / Windows GDI / Printing and Print Spooler.

Now GDI 101: GDI was the original MS Windows graphics sub-system. It stands for Graphics Device Interface. To perform any drawing on any device you require a device context or DC. Windows keeps a cache of DCs to the display device, but if you want to draw on something else - such as a printer - then you need to create your own, and delete it when finished. The GDI API functions to do this are CreateDC and DeleteDC and are documented in the MSDN library in the section Device Contexts - at the same level in the documentation tree as (the root) Printing and Print Spooler. It is to CreateDC that you provide the printer name information. Note that generally you would use the common printer dialog boxes provided by Windows. These are available using the PrintDlgEx or PrintDlg API functions (documented under User Interface Design and Development / Windows Management / User Input / Common Dialog Box Library / Common Dialog Box Reference / Functions - or similar documentation path). They allow you to set up the printer, its settings and the print job settings and can return a DC handle to the selected printer.

Having obtained a handle to a printer DC you next have to set up a print job abort call-back function and modeless print job cancel dialog box.

Finally you get to start printing the document. You need to call StartDoc and EndDoc functions at the beginning and end of the print job and StartPage and EndPage for each page. Finally you use GDI drawing functions to print the document. All this is covered in the Printing and Print Spooler documentation - with some simple sample C code.

Now you may need to programmatically find the names of available printers installed on your system. In MS Windows functions that iterate through sets of things are prefixed with the letters Enum (for Enumerate). Generally you call the Enum function and either provide a call back function which is called for each item in the set, or pass output parameters which the function fills in with the data for all enumerated items. In this case the function you require is EnumPrinters, and it is of the type that you pass a buffer which is filled in by EnumPrinter with PRINTER_INFO_n structures where n is 1, 2, 4 or 5, depending on the requested printer information level (err, this being 1, 2, 4 or 5). From a quick peek level 2 and PRINTER_INFO_2 structures look good for Windows 2000.

You might like to investigate using the MFC framework and its support for printing - see the Visual Studio .NET documentation for Visual C++ / Adding Functionality / Visual C++ Libraries / MFC / User Interface / Printing and Print Preview or locate the relevant material for your version of Visual C++.

Hope this helped a little.  

Answer
I have not done so, but the documentation (go read it!) states that this is one of those things you can do with SetPrinter for Windows 200/XP – aren't you lucky...However you should perhaps look at SetDefaultPrinter instead as this is a lot less complex – it just takes the name of the printer as a LPCTSTR. I suggest you obtain the name of the previous default printer _before_ you switch over so you can swap back after you are done. Like to guess the name of the function to get the default printer name? If you thought GetDefaultPrinter then award yourself a large something nice...

Hope this is what you need. You might also like to check for further information on printing using IWebBrowser or related interfaces. For example you might like to look at the article “Beyond Print Preview: Print Customization for Internet Explorer 5.5” . Part 2 says it will allow customisation of the Print dialog... (See also the related Print Template documentation in the Internet Explorer documentation). This may or may not be of help - I only did a quick search of the MSDN library and I could find no easy way to select the printer other than displaying the print dialog – which may not be what you wish.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.