LookupDrivers is used to iterate through the contents of the unit table.
OSErr LookupDrivers(
UnitNumber beginningUnit,
UnitNumber endingUnit,
Boolean emptyUnits,
ItemCount *returnedRefNums,
DriverRefNum *refNums);
Given the first and last unit numbers to scan, LookupDrivers returns the reference numbers of both native and 68K drivers. The emptyUnits parameter tells it to return either available or allocated units, and returnedRefNums tells it the maximum number of reference numbers to return. When LookupDrivers finishes, returnedRefNums contains the actual number of reference numbers returned.
The sample code shown in Listing 9-3 uses HighestUnitNumber and LookupDrivers to print out the reference numbers of all installed drivers and obtain driver information.
noErr
|
0 | No error |
badUnitErr
|
-21 | Bad unit number |
paramErr
|
-50 | Bad parameter |
Listing 9-3 Using the LookupDrivers function
OSStatus FindAllDrivers (void)
{
ItemCount theCount = 1;
UnitNumber theUnit = 0;
DriverRefNum theRefNum, *fullSizedRefNumBuffer;
/* method #1: iterate with a small output buffer */
while ( (theUnit <= HighestUnitNumber()) &&
(LookupDrivers (theUnit, theUnit, false, &theCount, &theRefNum) ==noErr))
{
if (theCount == 1) printf ("Refnum #%d is allocated.\n",theRefNum);
theCount = 1;
theUnit++;
}
/* method #2: get all refnums with one call */
fullSizedRefNumBuffer = NewPtr ((HighestUnitNumber() + 1) *
sizeof(DriverRefNum));
theCount = (HighestUnitNumber() + 1);
LookupDrivers (0, HighestUnitNumber(), false, &theCount,
fullSizedRefNumBuffer);
for(theUnit=0,theUnit <theCount;theUnit++)
{
printf("Refnum #%d is allocated.\n", fullSizedRefNumBuffer [theUnit]);
ShowDriverInfo (fullSizedRefNumBuffer [theUnit]);
}
DisposePtr(fullSizedRefNumBuffer);
return noErr;
}
ShowDriverInfo (DriverRefNum *refNum)
{
UnitNumber theUnit;
DriverRefNum aRefNum;
DriverFlags theFlags;
FSSpec driverFileSpec;
RegEntryID theDevice;
CFragHFSLocator theLoc;
Str255 theName;
CFragConnectionID fragmentConnID;
DriverOpenCount theOpenCount;
DriverEntryPointPtr fragmentMain;
DriverDescription theDriverDescription;
theLoc.u.onDisk.fileSpec = &driverFileSpec; /* See note below */
GetDriverInformation ( aRefNum,
&theUnit,
&theFlags,
&theOpenCount,
theName,
&theDevice,
&theLoc,
&fragmentConnID,
&fragmentMain,
&theDriverDescription);
printf ("Driver's flags are: %x\n", theFlags);
}
When calling GetDriverInformation, always supply an FSSpec file specification as shown in the preceding sample. Failure to do so may cause the DLL or the CFM to crash the system or overwrite the system heap.
You can also use the DLL to load a native driver without any associated hardware device. Just pass nil in RegEntryIDPtr to the DLL installation service.