Click here to Skip to main content
Click here to Skip to main content

Finding Printers in a Network

, 26 Feb 2003
Rate this:
Please Sign up or sign in to vote.
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.

  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 if's 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

  • Feb 28th, 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

About the Author

G. Steudtel
Software Developer (Senior)
Germany Germany
No Biography provided

Comments and Discussions

 
Questionjava program for finding network printer ink status dynamically PinmemberMember 1054008127-Jan-14 1:39 
GeneralHelp! PinmemberDanny10000004-Jun-08 3:29 
GeneralHere's a Nicer version, single function &quot;API&quot; PinmemberLoganKale8-Jan-07 8:10 
GeneralPlease Help How many pages the printer printed PinmemberThangaDharma6-Oct-05 7:49 
GeneralTo warm up default printer PinsussAnonymous16-Apr-04 11:59 
GeneralNetwork Printers: finding hostname PinmemberChulips4-Mar-04 4:40 
GeneralThanks. Pinmemberdarshanjani1003-Mar-04 1:59 
GeneralMemory Leak Pinmembersdpper2-Aug-03 19:21 
Hi,
 
While perusing your code I noticed a memory leak. Allocating memory as in "pPrInfo4 = new unsigned char [dwNeeded+128];" should be freed by "delete [] pPrInfo4;" and NOT "delete pPrInfo4;". I believe the way you are doing it is only deallocating 4 bytes instead of the array [dwNeeded+128].
 
Nice demo! Smile | :)
 
Steve D. Perkins
 


GeneralA better way given in MSDN PinsussAshutosh Bhatikar25-Mar-03 19:58 
GeneralPrinter finding PinmemberWouter Dhondt28-Feb-03 5:46 
GeneralRe: Printer finding PinmemberG. Steudtel28-Feb-03 6:25 
GeneralRe: Printer finding PinmemberMember 1054008127-Jan-14 1:34 
QuestionWrong category? PinmemberRoger Allen28-Feb-03 3:38 
AnswerRe: Wrong category? PinmemberG. Steudtel28-Feb-03 6:11 
GeneralPlease fix the demo link PinmemberPaul Belikian28-Feb-03 2:38 
GeneralRe: Please fix the demo link PinmemberG. Steudtel28-Feb-03 6:09 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 27 Feb 2003
Article Copyright 2003 by G. Steudtel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid