Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / WTL
Article

WTL Installed Printers List

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Nov 2002 74.4K   1.7K   16   8
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



Comments and Discussions

 
Generalfile atlmisc.h Pin
polunga25-Jan-05 19:35
polunga25-Jan-05 19:35 
GeneralRe: file atlmisc.h Pin
Rob Caldecott25-Jan-05 21:38
Rob Caldecott25-Jan-05 21:38 
GeneralNetwork printers Pin
jerko22-Mar-04 2:14
jerko22-Mar-04 2:14 
GeneralRe: Network printers Pin
jerko22-Mar-04 3:02
jerko22-Mar-04 3:02 
GeneralRe: Network printers Pin
Steve S22-Mar-04 3:03
Steve S22-Mar-04 3:03 
GeneralChoosing a printer default from the list Pin
Jo Fredrickson10-Oct-03 2:18
Jo Fredrickson10-Oct-03 2:18 
GeneralRe: Choosing a printer default from the list Pin
willcoxson20-Feb-04 11:25
willcoxson20-Feb-04 11:25 
GeneralRe: Choosing a printer default from the list Pin
Christopher Stratmann20-Feb-06 9:33
Christopher Stratmann20-Feb-06 9:33 
I have come across the same situation and this is what I came up with...(Ofcourse most of the code came from this page which I take no credit for.)

Step1: Record the Current Default Printer.
Step2: Change the default Printer to the one which I want to print on.
Step3: ShellExecute(..,"Print",...);
Step4: Change the default Printer back to the original.

typedef std::vector<std::string>  VString;
bool SetCurrentDefaultPrinter					(const std::string& s_printer_name)
{
	SetDefaultPrinter(s_printer_name.c_str());

	return true;
}

bool GetDefaultPrinter							(std::string& s_printer_name)
{
	s_printer_name = "";
	CPrintDialog cpd(FALSE);
	if(!cpd.GetDefaults())
	{
		return false;
	}

	s_printer_name = cpd.GetDeviceName();
	return true;
}

bool GetPrinterList								(VString& v_printers)
{
	v_printers.clear();
  
	DWORD dwSize = 0;
	DWORD dwPrinters = 0;
	// Enumerate all installed printers
	if (!::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, 
		NULL, 4, NULL, 0, &dwSize, &dwPrinters))
	{
		// Check for ERROR_INSUFFICIENT_BUFFER
		// If something else, then quit
		if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
			return false;
	// Fall through
	}

	// Allocate some buffer memory
	LPBYTE pBuffer = new BYTE [dwSize];
	if (pBuffer == NULL)
		return false;

	// Fill the buffer
	// Again, this depends on the O/S
	if (!::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS ,
		NULL, 4, pBuffer, dwSize, &dwSize, &dwPrinters))
	{
		// Error - unlikely though
		// as first call to EnumPrinters
		// succeeded!
		return false;
	}

	// Do we have any printers?
	if (dwPrinters == 0)
		return false;

	// Cast to PRINTER_INFO_2
	PRINTER_INFO_4* pInfo = (PRINTER_INFO_4*)pBuffer;
	// Loop adding the printers to the list
	for (DWORD i = 0; i < dwPrinters; i++, pInfo++)
	{
		std::string s_printer_name = pInfo->pPrinterName;
		v_printers.push_back(s_printer_name);
	}

	return true;
}

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.