Click here to Skip to main content
15,885,365 members
Articles / Desktop Programming / MFC

Finding Printers in a Network

Rate me:
Please Sign up or sign in to vote.
4.22/5 (15 votes)
26 Feb 20033 min read 128.2K   5.6K   46   16
Enumerate printers in a network, sending/printing a file on them

Sample Image - DirektDruck.gif

Introduction

In a project, I was forced to find the printers in a network and display the results. The finding of printers in a network is not challenging, just cumbersome. Only work, no inspiration.

At first, you must look up all local printers, then all network printers and at the end, all remote printers. That's about it. Here is the code for the local printers, after getting the required size of the PrinterInfoArray. The code for the network printers and remote printers is similar.

C++
if( dwNeeded != 0)
  { pPrInfo4 = new unsigned char [dwNeeded+128];
    dwBytes  = dwNeeded+128;
    ::EnumPrinters( PRINTER_ENUM_LOCAL, // types of
                                        // printer objects to enumerate
             NULL,          // name of printer object
             1,             // specifies type of printer info structure
             pPrInfo4,      // pointer to buffer to receive
                            // printer info structures
             dwBytes,       // size, in bytes, of array
             &dwNeeded,     // pointer to variable with
                            // no. of bytes copied (or required)
             &dwReturned    // pointer to variable with
                            // no. of printer info. structures copied
             );
    for( i=0; i<(int)dwReturned; i++ )
       { strStr = (LPCSTR)((PRINTER_INFO_1*)pPrInfo4)[i].pName;
         if( -1 == strStr.Find((LPCTSTR)(_T("!!"))) )
           if( CB_ERR == m_Ctrl_Windowsdrucker.FindStringExact(0,strStr) )
              m_Ctrl_Windowsdrucker.AddString(strStr);
       }
    delete pPrInfo4;
    pPrInfo4 = NULL;
  }

The only "creative" part in this whole fragment is the casting of the printer name out of the bytes forming the info array. The ifs assure, that only different printers, and no printer providers, are added. The names of printer providers all start with !!.

This becomes important in the remote printers section. There you have to quest the printer provider for the printer it provides; the else-part of the if( -1 == strStr.Find((LPCTSTR)(_T("!!"))) ). Because it repeats almost the code above, I ask you to have a closer look at the source code. The most remarkable change is, that you have to supply the name for the printer provider instead of a NULL pointer as second parameter in the EnumPrinter function.

Later on, I merged the code into a small project, simulating part of the UNIX print command, with a graphic user interface. This tool came in quite handy, when we needed to print files directly on various printers all over the intranet.

You can use this tool to copy files to a printer anywhere, found by your computer. So you can print a document to a file, and later on send it to different printers. The only drawback is, all printers must use the same printer driver. No modification on the file is done during printing. It's just like good old DOS copy <filename> prn. If you have a link to this tool on your desktop, you also may drag a file directly on this icon. While this tool is running, you also may drag a file directly in its window. The code for doing this was adopted from VIEWRICH.cpp from the MFC-source. A colleague of mine uses this tool now to send configuration files to the printers.

On start up, it may take some time until all printers are found. That depends on the size of the network and its speed. So give it some seconds or minutes.

What you can't is finding printers like http:www.codeproject.com\1stFloor\2ndRoom\FancyfulColorLaserJet.

I put all text in string resources, so you may adapt them to your own local. Just copy the string resource with your local into the rc-file and edit the text to meet your requirements. Sorry, for the folks who don't speak English or German. But I only know three languages, and my French is only good enough not to starve in France or for a small talk not exceeding 1 millimeter.

This code will not run under Win95. Maybe under Win95 SP2, but I don't know.

History

  • 28th February, 2003: A broken link was corrected and also some typing errors

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionjava program for finding network printer ink status dynamically Pin
Member 1054008127-Jan-14 1:39
Member 1054008127-Jan-14 1:39 
GeneralHelp! Pin
Danny10000004-Jun-08 3:29
Danny10000004-Jun-08 3:29 
GeneralHere's a Nicer version, single function &quot;API&quot; Pin
LoganKale8-Jan-07 8:10
LoganKale8-Jan-07 8:10 
GeneralPlease Help How many pages the printer printed Pin
ThangaDharma6-Oct-05 7:49
ThangaDharma6-Oct-05 7:49 
GeneralTo warm up default printer Pin
Anonymous16-Apr-04 11:59
Anonymous16-Apr-04 11:59 
GeneralNetwork Printers: finding hostname Pin
Chulips4-Mar-04 4:40
Chulips4-Mar-04 4:40 
GeneralThanks. Pin
darshanjani1003-Mar-04 1:59
darshanjani1003-Mar-04 1:59 
GeneralMemory Leak Pin
sdpper2-Aug-03 19:21
sdpper2-Aug-03 19:21 
GeneralA better way given in MSDN Pin
Ashutosh R. Bhatikar25-Mar-03 19:58
Ashutosh R. Bhatikar25-Mar-03 19:58 
GeneralPrinter finding Pin
Wouter Dhondt28-Feb-03 5:46
Wouter Dhondt28-Feb-03 5:46 
GeneralRe: Printer finding Pin
G. Steudtel28-Feb-03 6:25
G. Steudtel28-Feb-03 6:25 
GeneralRe: Printer finding Pin
Member 1054008127-Jan-14 1:34
Member 1054008127-Jan-14 1:34 
QuestionWrong category? Pin
Roger Allen28-Feb-03 3:38
Roger Allen28-Feb-03 3:38 
AnswerRe: Wrong category? Pin
G. Steudtel28-Feb-03 6:11
G. Steudtel28-Feb-03 6:11 
GeneralPlease fix the demo link Pin
Paul Belikian28-Feb-03 2:38
Paul Belikian28-Feb-03 2:38 
GeneralRe: Please fix the demo link Pin
G. Steudtel28-Feb-03 6:09
G. Steudtel28-Feb-03 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.