Click here to Skip to main content
11,935,361 members (59,117 online)
Click here to Skip to main content
Add your own
alternative version


44 bookmarked

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


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) )
    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\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.


  • Feb 28th, 2003

    A broken link was corrected and also some typing errors.


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

You may also be interested in...

Comments and Discussions

Questionjava program for finding network printer ink status dynamically Pin
Member 1054008127-Jan-14 2:39
memberMember 1054008127-Jan-14 2:39 
GeneralHelp! Pin
Danny10000004-Jun-08 4:29
memberDanny10000004-Jun-08 4:29 
GeneralHere's a Nicer version, single function &quot;API&quot; Pin
LoganKale8-Jan-07 9:10
memberLoganKale8-Jan-07 9:10 
GeneralPlease Help How many pages the printer printed Pin
ThangaDharma6-Oct-05 8:49
memberThangaDharma6-Oct-05 8:49 
GeneralTo warm up default printer Pin
Anonymous16-Apr-04 12:59
sussAnonymous16-Apr-04 12:59 
GeneralNetwork Printers: finding hostname Pin
Chulips4-Mar-04 5:40
memberChulips4-Mar-04 5:40 
GeneralThanks. Pin
darshanjani1003-Mar-04 2:59
memberdarshanjani1003-Mar-04 2:59 
GeneralMemory Leak Pin
sdpper2-Aug-03 20:21
membersdpper2-Aug-03 20:21 

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 Pin
Ashutosh Bhatikar25-Mar-03 20:58
sussAshutosh Bhatikar25-Mar-03 20:58 
GeneralPrinter finding Pin
Wouter Dhondt28-Feb-03 6:46
memberWouter Dhondt28-Feb-03 6:46 
GeneralRe: Printer finding Pin
G. Steudtel28-Feb-03 7:25
memberG. Steudtel28-Feb-03 7:25 
GeneralRe: Printer finding Pin
Member 1054008127-Jan-14 2:34
memberMember 1054008127-Jan-14 2:34 
QuestionWrong category? Pin
Roger Allen28-Feb-03 4:38
memberRoger Allen28-Feb-03 4:38 
AnswerRe: Wrong category? Pin
G. Steudtel28-Feb-03 7:11
memberG. Steudtel28-Feb-03 7:11 
GeneralPlease fix the demo link Pin
Paul Belikian28-Feb-03 3:38
memberPaul Belikian28-Feb-03 3:38 
GeneralRe: Please fix the demo link Pin
G. Steudtel28-Feb-03 7:09
memberG. Steudtel28-Feb-03 7: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.

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