Getting printer list on MAC OSX Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Getting printer list on MAC OSX

Post by cutecode »

How to get a list of installed printers on MAC?
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Getting printer list on MAC OSX

Post by doublemax »

I'm not 100% sure, but i think there is no API for that in wxWidgets.

http://trac.wxwidgets.org/ticket/12879
Use the source, Luke!
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Getting printer list on MAC OSX

Post by cutecode »

this ticket is still in opened state.
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Getting printer list on MAC OSX

Post by doublemax »

I know. I just posted it as an indicator that this might not be possible.

As the ticket hasn't been touched for 4 years, it probably has been forgotten. The link to the code by one of the commenters is dead, too.
Use the source, Luke!
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: Getting printer list on MAC OSX

Post by coderrc »

doesnt mac use cups?
if it does then cups_dest_t and cupsGetDests should get you where you need to go

eg to get the default printer

Code: Select all

cups_dest_t* dests;
	int num_dests = cupsGetDests(&dests);
	wxString retval = "";

	for (int i = 0; i < num_dests; i++) {
		if (dests[i].is_default) {
			if (dests[i].instance) {
				retval = wxString::Format("%s%s", dests[i].name, dests[i].instance);
			}
			else {
				retval = wxString::Format("%s", dests[i].name);
			}
		}
	}

	//free memory from cups data
	cupsFreeDests(num_dests, dests);

	return retval;
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Getting printer list on MAC OSX

Post by cutecode »

Thanks for a hint.
I think this code should work under Linux and MAC, since I see printer list on both OSs by following this adress http://127.0.0.1:631/printers
I downloaded cups folder with include files, but cannot figure out which lib to include into project.
Could you help me?
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Getting printer list on MAC OSX

Post by cutecode »

gotcha...

/usr/lib/libcups.dylib for MAC
and
/usr/lib/x86_64-linux-gnu/libcups.so.2 for UBUNTU

the code works as I wanted

Thank you
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Getting printer list on MAC OSX

Post by cutecode »

That's my code

Code: Select all

bool GetPrinterList(wxArrayString &ar, wxString &szDefaultPrinterName)
{
    ar.Empty();
    szDefaultPrinterName.Empty();

#ifdef __WXMSW__
	DWORD dwSize, dwPrinters;
	BYTE *pBuffer;

	DWORD szz = 255;
	WCHAR c[256];
	GetDefaultPrinter(&c[0], &szz);
	szDefaultPrinterName = c;

	::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwSize, &dwPrinters);
	if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || dwSize == 0)
		return false;
	pBuffer = new BYTE[dwSize];;
	::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, pBuffer, dwSize, &dwSize, &dwPrinters);
	if (dwPrinters != 0)
	{
		PRINTER_INFO_5 *pPrnInfo = (PRINTER_INFO_5 *)pBuffer;
		for (UINT i = 0; i < dwPrinters; i++)
		{
			ar.Add(pPrnInfo->pPrinterName);
			pPrnInfo++;
		}
	}
	if (pBuffer)
	{
		delete[] pBuffer;
		pBuffer = NULL;
	}

	return true;
#else
//#if defined( __WXOSX__) || defined( __WXGTK__)
    cups_dest_t* dests;
    int num_dests = cupsGetDests(&dests);

    for (int i = 0; i < num_dests; i++)
    {
        wxString sz;

        if (dests[i].instance)
            sz = wxString::Format("%s%s", dests[i].name, dests[i].instance);
        else
            sz = wxString::Format("%s", dests[i].name);

        if (dests[i].is_default)
            szDefaultPrinterName = sz;

       ar.Add(sz);
    }

   //free memory from cups data
   cupsFreeDests(num_dests, dests);

   if(ar.GetCount())
        return true;
        
      //do some other methods  
#endif
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
Post Reply