How to get printer list?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MartinSfromB
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Nov 24, 2004 9:04 am
Location: Berlin, Germany
Contact:

How to get printer list?

Post by MartinSfromB »

Hello Group!
I hhope, someone can help. The printing functionality in wxWindows is great but am I to blind to see how to get a list of system printers.
I can set the used printer by it's name in wx... but the names of the printers in my program i get with a little delphi-dll under MSW. There must be another way!? And what's the way under linux?

Thanx for help
Jasper
Earned a small fee
Earned a small fee
Posts: 18
Joined: Tue Sep 21, 2004 1:49 pm
Contact:

Post by Jasper »

I'd definitely like to now this too, at the moment I just show a print dialog to let the user select a printer, which isn't particularly nice.
NinjaNL
Moderator
Moderator
Posts: 899
Joined: Sun Oct 03, 2004 10:33 am
Location: Oosterwolde, Netherlands

Re: How to get printer list?

Post by NinjaNL »

MartinSfromB wrote:am I to blind to see how to get a list of system printers.
Do you mean how do you present the user with a list of installed printers? Like the dialog which sits under the "Printers..." button on Print Preview?

From th eprinting sample 2.5.3

Code: Select all

    wxPrintDialogData printDialogData(* g_printData);
    wxPrintDialog printerDialog(this, & printDialogData);

    printerDialog.GetPrintDialogData().SetSetupDialog(true /*show print setup dialog*/);
    printerDialog.ShowModal();

    (*g_printData) = printerDialog.GetPrintDialogData().GetPrintData();
Or is this not what you mean?
Follow the development of my screenplay authoring program at http://wxscreenplaywriter.blogspot.com/
Jasper
Earned a small fee
Earned a small fee
Posts: 18
Joined: Tue Sep 21, 2004 1:49 pm
Contact:

Post by Jasper »

I don't know about the original poster, but in my case I'd like to be able to retrieve a list of printers to show in a combobox for example (I currently use more or less what you posted, but that isn't very natural in my case).
MartinSfromB
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Nov 24, 2004 9:04 am
Location: Berlin, Germany
Contact:

Post by MartinSfromB »

What i mean: I want to draw on a printer. I can set the printer by it's name with wxPrintData::SetPrinterName(..). But how to get the list of (installed) printer(names)??? A user should choose a specific printer for a specific job in my programm.
HansR
Earned a small fee
Earned a small fee
Posts: 14
Joined: Wed Apr 14, 2010 4:41 am
Contact:

Re: How to get printer list?

Post by HansR »

I know this is an old thread, but I have the same problem. Neither the standard print dialog or the page setup dialog make any sense with the application that I am developing. As a result, I'd like to create my own custom print dialog.

In order to create a custom print dialog, I need to be able to get a list of available printers. I'd also need to be able to open the system printer settings dialog (i.e., have a "Preferences" button next to the printer selection dialog), and preferably also be able to query which page types the currently selected printer supports.

Does wxWidgets support this?

Hans
HansR
Earned a small fee
Earned a small fee
Posts: 14
Joined: Wed Apr 14, 2010 4:41 am
Contact:

Post by HansR »

Seeing as this wasn't currently supported, I wrote something that provides this feature myself.

Hans
rogue
In need of some credit
In need of some credit
Posts: 6
Joined: Sun May 24, 2009 5:36 am

This snippet quickly gets msw printers local and remote

Post by rogue »

The problem with all the versions I've seen to date is that they only get local printers. If I've set up a remote printer, it didn't show up.

I finally found the magic invocation. One secret is to use PRINTER_INFO_4, the other is to use PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS.

With that, the net printer I'd set up on this machine showed up.

As the #defines and #errors say, this is Windows only.

Enjoy.

void CPrinterSetupDialog::GetPrinterList()
{
#if defined(__WXMSW__)

// When Name is NULL, setting Flags to PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS
// enumerates printers that are installed on the local machine. These printers include
// those that are physically attached to the local machine as well as remote printers to
// which it has a network connection.
DWORD dwFlags = PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL;
LPTSTR Name = NULL;
DWORD dwNeeded = 0;
DWORD dwReturned = 0;

// First, find out how much space we need for the return buffers
if(::EnumPrinters(dwFlags, Name, 4, 0, 0, & dwNeeded, & dwReturned)) {
throw CLogicError(ERR_POS);
}

DWORD LastError = ::GetLastError();
if(LastError != ERROR_INSUFFICIENT_BUFFER) {
throw CLogicError(ERR_POS);
}

// Allocate the buffer
PRINTER_INFO_4 * PrinterInfo = (PRINTER_INFO_4 *)malloc(dwNeeded);

if( ! PrinterInfo) {
throw CLogicError(ERR_POS);
}

if( ! ::EnumPrinters(dwFlags, Name, 4, (unsigned char *)PrinterInfo, dwNeeded, & dwNeeded, & dwReturned)) {
throw CLogicError(ERR_POS);
}

for(int i = 0; i < (int)dwReturned; i++) {
m_PrinterName.push_back(PrinterInfo.pPrinterName);
}

free(PrinterInfo);

#else
#error CPrinterSetupDialog::GetPrinterList() has not been implemented for this platform
#endif
}
HansR
Earned a small fee
Earned a small fee
Posts: 14
Joined: Wed Apr 14, 2010 4:41 am
Contact:

Post by HansR »

Thanks rogue. I'm a little surprised that your installed network printers didn't show, as mine did. It might be something to do with how I set mine up. Anyway, I have updated the code (and the archive on my website) to use PRINTER_INFO_4 as your code snippet shows.

Perhaps you could test the new version to confirm that it shows your remote printers.

Hans
Rohit Agarwal
Earned some good credits
Earned some good credits
Posts: 119
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: How to get printer list?

Post by Rohit Agarwal »

Given a specific printer,
how do you query its capabilities?

The equivalent of wxSystemSettings::GetMetrics
which is for querying the system.

For example the dpi for each of its quality settings?
Post Reply