Listing 2-8 – Apple Network Setup User Manual
Page 36

C H A P T E R 2
Using Network Setup
36
Reading and Writing Preferences
Note
The sample shown in Listing 2-7, which opens and closes
the entity before reading each preference, is implemented
in an inefficient manner for the sake of clarity. If you are
reading multiple preferences, it is more efficient to open the
entity once. Then read the preferences by calling
OTCfgGetPrefs
or
OTCfgSetPrefs
multiple times and close
the entity when you’re done.
◆
A noteworthy point about reading preferences is that the
OTCfgOpenPrefs
function does not take an area parameter. This is because the
CfgEntityRef
itself
implicitly includes the area. The significant of this point is demonstrated in the
section “Working with Sets” (page 42).
You can use the
MyReadFixedSizePref
routine shown in Listing 2-7 to read
specific preferences within an entity. For example, Listing 2-8 shows how to
read the DHCP lease information from a TCP/IP network connection entity.
The routine calls
MyReadFixedSizePref
, passing it the appropriate preference
type (
kOTCfgTCPDHCPLeaseInfoPref
), a pointer to the corresponding C structure,
and the size of the structure.
Listing 2-8
Reading the DHCP lease info preference in a TCP/IP network
connection entity
static OSStatus MyReadDHCPLeaseInfo(CfgDatabaseRef dbRef,
const CfgEntityRef *entity,
OTCfgTCPDHCPLeaseInfo *dhcpInfo)
{
OSStatus err;
assert(dbRef != nil);
assert(entity != nil);
assert(dhcpInfo != nil);
err = MyReadFixedSizePref(dbRef, entity, kOTCfgTCPDHCPLeaseInfoPref,
dhcpInfo, sizeof(*dhcpInfo));
return err;
}