Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / CodeWarrior / February 2004



Tip: Looking for answers? Try searching our database.

How to get the local printer name?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pealow Ng LY - 03 Feb 2004 08:56 GMT
Hi there,

   May i know how do I get the printer list and the current printer name
from a print dialog? I searched thru the documentation, managed to find
these 2 APIs:PMSessionCreatePrinterList, PMSessionGetCurrentPrinter.
However, i don't know how to use it to retrieve the chosen printer name from
a print dialog and i have no idea how to create a printer list.

  Can  someone enlighten me? Thanks in advance.

regards, pealow
Kaydell - 03 Feb 2004 23:06 GMT
> Hi there,
>
[quoted text clipped - 7 lines]
>
> regards, pealow

Hi,

I've been working on the same problem.  At first I tried using
PMSessionCreatePrinterList and PMSessionGetCurrentPrinter.  The first
problem that I ran into is that the documentation on Apple's web-site
says that they are available for OS 9.  This is not really true.  The
function are defined but they don't do anything useful.  They always
return an error.  It does say that if you carefully read the
documentation under function result.  it says:

If you call the function PMSessionCreatePrinterList in Mac OS 8 and 9,
the result code kPMNotImplemented is returned.

It is possible to get and set the current printer under OS 9 using a
hack that I found a long time ago in MacTech, but I wanted to do it
the Apple approved way using the new carbon function, but they don't
work under OS 9.

I don't know if you're supporting OS 9 and OS X in your software or
not.

I'm using Pascal, but you should be able to translate this to C++ if
you're like most people and use that.

    procedure GetPrinterNames(listItem:TListItem; var
currentPrinterDriver:str255);
   
        procedure TestErr(status:OSStatus);
        begin
            if status <> noErr then begin
                DoOKAlert2('Error listing available printers
^1',MacErrorStr(status));
                Exit(GetPrinterNames);
            end;
        end;
       
    var
        printSession:PMPrintSession;
        status1,status2:OSStatus;
        theCFArrayRef:CFArrayRef;
        currentIndex:CFIndex;
        currentPrinter:PMPrinter;
        i,n:integer;
        aCFStringRef:CFStringRef;
        st:str255;
        success:boolean;
    begin
        currentPrinterDriver := '';
        printSession := nil;
        currentIndex := 0;
          TestErr(PMCreateSession(printSession));
        theCFArrayRef := MyCFArrayCreate;
        TestErr(PMSessionCreatePrinterList(printSession,theCFArrayRef,currentIndex,currentPrinter));
        n := CFArrayGetCount(theCFArrayRef);
        status1 := kPMNoError;
        for i := 1 to n do begin
            aCFStringRef := CFStringRef(CFArrayGetValueAtIndex(theCFArrayRef,i-1));
            success := CFStringGetPascalString(aCFStringRef,@st,SizeOf(st),kCFStringEncodingMacRoman);
            if success then begin
                if listItem <> nil then
                    listItem.Add(st,false);
                if currentIndex = i-1 then
                    currentPrinterDriver := st;
            end;
        end;          
        CFRelease(CFTypeRef(theCFArrayRef));
        status2 := PMRelease(PMObject(printSession));
        TestErr(status1);
    end;

Some of my code needs a little explaining.

The function, GetPrinterNames, returns both a list of available
printers, and the name of the printer that is the default printer.

1) MacErrorStr simply converts an OSErr or OSStatus to a string.  I
didn't include its code because it only recognizes a handful of the
most common error codes.

2) MyCFArrayCreate is a function that I defined in one of my units
that is written in C++.  I mostly program in Pascal, but Apple's
documentation said that you could simply pass nil for one of the
parameters to CFArrayCreate, but I couln't do that in Pascal because
it was a "var" parameter.

pascal CFArrayRef MyCFArrayCreate(void) {
    return(CFArrayCreate(nil,nil,0,nil));
}

3) DoOKAlert2 is a function that I wrote that  puts up a simple alert
and takes two parameters.

4) TListItem is a class that I implemented.  It ties into my window
class code.  You'd just replace this with something of your own to add
the print driver names to.

I believe that in OS X, there is both a default printer and a printer
associated with a session.  I believe that PMSessionGetCurrentPrinter
returns the printer object that is associated with the session that
you pass  to it as a parameter.  I believe that this printer would be
the default printer unless you changed the printer yourself for a
session.

I couldn't figure out how to use PMSessionGetCurrentPrinter.  I use my
function GetPrinterNames to give me both a list of all available OS X
printer drivers and to return the name of the default OS X printer
driver.

Kaydell
Pealow Ng LY - 25 Feb 2004 03:48 GMT
Hello guys,    I refer to my question posted on 3rd Feb 2004. Thanks to kaydell, i manage to get the printer list. It works well in OS X. My concern here is it does not work in OS 9. Yes, Kaydell has mentioned this in his reply. So I wonder if anybody else know how to go about getting the printer list in OS 9?

Thanks again for your help and kindness in advance.

regards,pealow
> Hi there,
>
[quoted text clipped - 7 lines]
>
> regards, pealow

Hi,

I've been working on the same problem.  At first I tried using
PMSessionCreatePrinterList and PMSessionGetCurrentPrinter.  The first
problem that I ran into is that the documentation on Apple's web-site
says that they are available for OS 9.  This is not really true.  The
function are defined but they don't do anything useful.  They always
return an error.  It does say that if you carefully read the
documentation under function result.  it says:

If you call the function PMSessionCreatePrinterList in Mac OS 8 and 9,
the result code kPMNotImplemented is returned.

It is possible to get and set the current printer under OS 9 using a
hack that I found a long time ago in MacTech, but I wanted to do it
the Apple approved way using the new carbon function, but they don't
work under OS 9.

I don't know if you're supporting OS 9 and OS X in your software or
not.

I'm using Pascal, but you should be able to translate this to C++ if
you're like most people and use that.

procedure GetPrinterNames(listItem:TListItem; var
currentPrinterDriver:str255);

 procedure TestErr(status:OSStatus);
 begin
  if status <> noErr then begin
   DoOKAlert2('Error listing available printers
^1',MacErrorStr(status));
   Exit(GetPrinterNames);
  end;
 end;
 
var
 printSession:PMPrintSession;
 status1,status2:OSStatus;
 theCFArrayRef:CFArrayRef;
 currentIndex:CFIndex;
 currentPrinter:PMPrinter;
 i,n:integer;
 aCFStringRef:CFStringRef;
 st:str255;
 success:boolean;
begin
 currentPrinterDriver := '';
 printSession := nil;
 currentIndex := 0;
    TestErr(PMCreateSession(printSession));
 theCFArrayRef := MyCFArrayCreate;
 TestErr(PMSessionCreatePrinterList(printSession,theCFArrayRef,currentIndex,currentPrinter));
 n := CFArrayGetCount(theCFArrayRef);
 status1 := kPMNoError;
 for i := 1 to n do begin
  aCFStringRef := CFStringRef(CFArrayGetValueAtIndex(theCFArrayRef,i-1));
  success := CFStringGetPascalString(aCFStringRef,@st,SizeOf(st),kCFStringEncodingMacRoman);
  if success then begin
   if listItem <> nil then
    listItem.Add(st,false);
   if currentIndex = i-1 then
    currentPrinterDriver := st;
  end;
 end;    
 CFRelease(CFTypeRef(theCFArrayRef));
 status2 := PMRelease(PMObject(printSession));
 TestErr(status1);
end;

Some of my code needs a little explaining.

The function, GetPrinterNames, returns both a list of available
printers, and the name of the printer that is the default printer.

1) MacErrorStr simply converts an OSErr or OSStatus to a string.  I
didn't include its code because it only recognizes a handful of the
most common error codes.

2) MyCFArrayCreate is a function that I defined in one of my units
that is written in C++.  I mostly program in Pascal, but Apple's
documentation said that you could simply pass nil for one of the
parameters to CFArrayCreate, but I couln't do that in Pascal because
it was a "var" parameter.

pascal CFArrayRef MyCFArrayCreate(void) {
return(CFArrayCreate(nil,nil,0,nil));
}

3) DoOKAlert2 is a function that I wrote that  puts up a simple alert
and takes two parameters.

4) TListItem is a class that I implemented.  It ties into my window
class code.  You'd just replace this with something of your own to add
the print driver names to.

I believe that in OS X, there is both a default printer and a printer
associated with a session.  I believe that PMSessionGetCurrentPrinter
returns the printer object that is associated with the session that
you pass  to it as a parameter.  I believe that this printer would be
the default printer unless you changed the printer yourself for a
session.

I couldn't figure out how to use PMSessionGetCurrentPrinter.  I use my
function GetPrinterNames to give me both a list of all available OS X
printer drivers and to return the name of the default OS X printer
driver.

Kaydell
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.