// Current Printer info sample code
//
// Dave Polaschek and David Hayward
// Developer Technical Support
// AppleLink: DEVSUPPORT
//
// Copyright 1995, Apple Computer,Inc
//
// Check the default printer under GX or old printing architecture.
// Print out the name, entityType, and zone.
//
// davep 8/16/95 initial cut
#include <stdio.h>
#define gestaltGXVersion 'qdgx'
// in PrintingManager.h (old) or GXPrinting.h (new)
#define gestaltGXPrintingMgrVersion 'pmgr'
// in PrintingManager.h (old) or GXPrinting.h (new)
#define gestaltAliasMgrAttr 'alis'
// in Aliases.h
#define printingResourceID 0xE000 // 0xE000 = -8192
OSErr GetCurrentPrinter(Str255 printer);
OSErr test(void);
Boolean gxInstalled(void);
Boolean validPrinter(void);
/*---------------------------------------------------------------------------*\
This function returns true if QuickDraw GX printing is available
\*---------------------------------------------------------------------------*/
Boolean gxInstalled(void)
{
long version;
if (Gestalt(gestaltGXVersion, &version) == noErr)
if (Gestalt(gestaltGXPrintingMgrVersion, &version) == noErr)
return(true);
return(false);
}
/*---------------------------------------------------------------------------*\
This function returns the network entity of the currently
selected printer, and as an added bonus, you get an error code, too!
\*---------------------------------------------------------------------------*/
OSErr GetCurrentPrinter(Str255 printer)
{
StringHandle theDriver;
Handle thePrinter;
OSErr theErr;
short resFileRefnum;
theDriver = GetString(printingResourceID);
if (!theDriver) goto BADDriver;
if (gxInstalled()) {
short vRefNum;
long dirID;
FSSpec theFile;
long resSize;
theErr=FindFolder(kOnSystemDisk,kDesktopFolderType,kDontCreateFolder,
&vRefNum,&dirID);
if (theErr != noErr) goto BADDriver;
theErr=FSMakeFSSpec(vRefNum,dirID,*theDriver,&theFile);
if (theErr != noErr) goto BADDriver;
resFileRefnum = FSpOpenResFile(&theFile,fsRdPerm);
if (resFileRefnum == -1) goto BADResFile;
thePrinter = Get1Resource('comm', 0);
if (!thePrinter) goto BADPrinter;
if (*((long *)*thePrinter) != 'PPTL') goto BADPrinter;
resSize = GetHandleSize(thePrinter)-6;
BlockMoveData((*thePrinter)+6,printer,resSize);
} else {
Boolean aliasCallsPresent;
Boolean aliasResourcePresent;
long resSize,response;
AliasHandle theAlias;
aliasCallsPresent = ((Gestalt(gestaltAliasMgrAttr,&response) == noErr)
&&
(response & 1));
theAlias = (AliasHandle) GetResource('alis',printingResourceID);
aliasResourcePresent = !!(theAlias);
if (aliasCallsPresent && aliasResourcePresent) {
FSSpec fileSpec;
Boolean wasChanged;
theErr = ResolveAlias(NULL, theAlias,&fileSpec,&wasChanged);
if (theErr != noErr) goto BADDriver;
resFileRefnum = FSpOpenResFile(&fileSpec,fsRdPerm);
if (resFileRefnum == -1) goto BADResFile;
} else {
resFileRefnum = OpenResFile(*theDriver);
if (resFileRefnum == -1) goto BADResFile;
}
thePrinter = Get1Resource('PAPA', printingResourceID);
if (!thePrinter) goto BADPrinter;
resSize = GetHandleSize(thePrinter);
BlockMoveData(*thePrinter,printer,resSize);
}
ReleaseResource(thePrinter);
CloseResFile(resFileRefnum);
return(noErr);
// Error returns
BADPrinter:
CloseResFile(resFileRefnum);
BADResFile:
theErr = ResError();
BADDriver:
return(theErr);
}
/*---------------------------------------------------------------------------*\
This function is called by the test harness. Doesn't do much
except prove that things work without crashing.
\*---------------------------------------------------------------------------*/
OSErr test(void)
{
OSErr testValue;
Str255 printerEntity;
testValue = GetCurrentPrinter(printerEntity);
if (testValue)
return testValue;
else {
Str31 printer,type,zone;
unsigned char *currentString;
currentString = printerEntity;
BlockMove(currentString,printer,Length(currentString)+1);
p2cstr(printer);
currentString += Length(currentString) + 1;
BlockMove(currentString,type,Length(currentString) + 1);
p2cstr(type);
currentString += Length(currentString) + 1;
BlockMove(currentString,zone,Length(currentString) + 1);
p2cstr(zone);
printf("Current Printer name = %s\n"
"EntityType = %s\n"
"Zone = %s\n",printer,type,zone);
return noErr;
}
}
/*---------------------------------------------------------------------------*\
This function tells if there's a valid printer selected (i.e. not
aimed at a driver that's since been deleted or no printer selected
since last System sw install) in the chooser
\*---------------------------------------------------------------------------*/
Boolean validPrinter(void)
{
Str255 printerEntity;
if (GetCurrentPrinter(printerEntity) != noErr)
return false;
else if (Length(printerEntity) == 0)
return false;
else
return true;
}
// Simple testbed for Macintosh sample code
//
// Dave Polaschek
// Developer Technical Support
// AppleLink: DEVSUPPORT
//
// Copyright 1995, Apple Computer,Inc
//
// This file contains main, and nothing else.
// dave 8/15/95 initial cut
#include <stdio.h>
extern OSErr test(void); // in CurrentPrinter.c
void main(void)
{
OSErr testResult;
testResult = test();
printf("Return value from test() = %d\n",testResult);
}
|