Click here to Skip to main content
Licence 
First Posted 7 Nov 2002
Views 51,948
Bookmarked 12 times

WTL Installed Printers List

By | 7 Nov 2002 | Article
Use this class in your WTL apps to retrieve a list of installed printers

Sample Image - WTLInstalledPrinters.jpg

Introduction

Want to fetch an array of installed printers for use in your WTL apps? Easy - just use this class.

First, include the header:

#include "installedprinters.h"

Next, simply create a CInstalledPrinters object and the array will be populated automatically. This class is derived from CSimpleArray<CString>, so you can, for example, fill a listbox using the following code:

CListBox listbox = GetDlgItem(IDC_LIST1);
// Get the list of installed printers
CInstalledPrinters list;
// Fill listbox
for (int i = 0; i < list.GetSize(); i++)
    listbox.AddString(list[i]);

It's as easy as that, The class will use the Win32 EnumPrinters API call, using PRINTER_INFO_5 structures - which is probably the fastest way to enumerate printers (no attempt is made to actually open the printer, as this can be slow if you have network printers installed).

CInstalledPrinters

#pragma once

#include <atlmisc.h>

class CInstalledPrinters : public CSimpleArray<CString>
{
public:
    CInstalledPrinters(void)
    {
        GetPrinters();
    }

    void GetPrinters(void)
    {        
        DWORD dwSize = 0;
        DWORD dwPrinters = 0;
        // Enumerate all installed printers
        if (!::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, 
               NULL, 5, NULL, 0, &dwSize, &dwPrinters))
        {
            // Check for ERROR_INSUFFICIENT_BUFFER
            // If something else, then quit
            if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
                return;
            // Fall through
        }
        // Allocate some buffer memory
        LPBYTE pBuffer = new BYTE [dwSize];
        if (pBuffer == NULL)
            return;
        // Fill the buffer
        // Again, this depends on the O/S
        if (!::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
              NULL, 5, pBuffer, dwSize, &dwSize, &dwPrinters))
        {
            // Error - unlikely though
            // as first call to EnumPrinters
            // succeeded!
            return;
        }
        // Do we have any printers?
        if (dwPrinters == 0)
            return;
        // Cast to PRINTER_INFO_2
        PRINTER_INFO_5* pInfo = (PRINTER_INFO_5*)pBuffer;
        // Loop adding the printers to the list
        for (DWORD i = 0; i < dwPrinters; i++, pInfo++)
            Add(CString(pInfo->pPrinterName));
        delete [] pBuffer;
    }
};

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

Rob Caldecott

Architect

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalfile atlmisc.h Pinmemberpolunga19:35 25 Jan '05  
GeneralRe: file atlmisc.h PinmemberRobert Edward Caldecott21:38 25 Jan '05  
GeneralNetwork printers Pinmemberjerko2:14 22 Mar '04  
GeneralRe: Network printers Pinmemberjerko3:02 22 Mar '04  
GeneralRe: Network printers PinmemberSteve S3:03 22 Mar '04  
GeneralChoosing a printer default from the list Pinmemberjoanne_fredrickson2:18 10 Oct '03  
GeneralRe: Choosing a printer default from the list Pinmembermoronikos11:25 20 Feb '04  
GeneralRe: Choosing a printer default from the list Pinmemberchris1759:33 20 Feb '06  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 8 Nov 2002
Article Copyright 2002 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid