For example, in the following example from IOKit documentation, each entry for this device has different types, such as Data, Number, etc
How can I use IOKit to read the type of it? I know that we can use IORegistryEntryCreateCFProperties
to create a dictionary for an entry and read the property as void pointer
and cast it to the type we know it is, but how can I know its type without taking a look using IORegistry Explorer?
Note that you can use a similar function, IORegistryEntryCreateCFProperty
to query only one property, which is usually more useful unless you're actually trying to build something similar to IORegistryExplorer itself and don't yet know the names of the properties you want to inspect.
In both cases, this really boils down to a Core Foundation question. You'll notice that rather than a truly opaque void*
, IORegistryEntryCreateCFProperty()
actually returns a CFTypeRef
, and likewise, IORegistryEntryCreateCFProperties()
promises to only return CoreFoundation-type objects in its dictionary. This means you can query the object's type using CFGetTypeID()
. The exact value of this is meaningless, but it can be compared to the type IDs for the expected set of types, e.g. CFStringGetTypeID()
for CFString
, CFDataGetTypeID()
for CFData
and so on.
If you're using Objective-C, you can also perform a bridging cast and treat the property values as NSObject
- and then find out the specific type using e.g. [object isKindOfClass:[NSString class]]
.