
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,
NULL,
1,
pPrInfo4,
dwBytes,
&dwNeeded,
&dwReturned
);
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
- 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.