Getting printer list on MAC OSX  [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.
User avatar
cutecode
Earned some good credits
Earned some good credits
Posts: 103
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Getting printer list on MAC OSX

Postby cutecode » Thu Oct 12, 2017 2:06 pm

How to get a list of installed printers on MAC?
regards,
Alexander Saprykin

User avatar
doublemax
Moderator
Moderator
Posts: 11384
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Getting printer list on MAC OSX

Postby doublemax » Thu Oct 12, 2017 9:12 pm

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
Earned some good credits
Earned some good credits
Posts: 103
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Getting printer list on MAC OSX

Postby cutecode » Thu Oct 12, 2017 9:59 pm

this ticket is still in opened state.
regards,
Alexander Saprykin

User avatar
doublemax
Moderator
Moderator
Posts: 11384
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Getting printer list on MAC OSX

Postby doublemax » Thu Oct 12, 2017 10:17 pm

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: 110
Joined: Tue Nov 01, 2016 2:46 pm

Re: Getting printer list on MAC OSX  [SOLVED]

Postby coderrc » Fri Oct 13, 2017 5:28 pm

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
Earned some good credits
Earned some good credits
Posts: 103
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Getting printer list on MAC OSX

Postby cutecode » Fri Oct 13, 2017 6:57 pm

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?
regards,
Alexander Saprykin

User avatar
cutecode
Earned some good credits
Earned some good credits
Posts: 103
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Getting printer list on MAC OSX

Postby cutecode » Fri Oct 13, 2017 7:50 pm

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
regards,
Alexander Saprykin

User avatar
cutecode
Earned some good credits
Earned some good credits
Posts: 103
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Getting printer list on MAC OSX

Postby cutecode » Fri Oct 13, 2017 8:04 pm

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
regards,
Alexander Saprykin


Return to “Platform Related Issues”

Who is online

Users browsing this forum: No registered users and 2 guests