如何在C ++中以编程方式检索以下信息:
这是Mac OSX中的终端命令:
ioreg -c IOPlatformExpertDevice | awk '/board-id/ {print $4}' | awk -F '\"' '{print $2}'
我使用IOKit库来检索信息,例如IOPlatformSerialNumber和PlatformUUID信息。但是我找不到“ board-id”的任何对应密钥。
如果您在C ++代码中具有的io_service_t
句柄,则IOPlatformExpertDevice
可以使用该IORegistryEntryCreateCFProperty()
函数获取“ board-id”属性。期望接收到一个CFData
对象,但是请确保检查null和正确的类型ID。然后,使用常规CFData
方法以所需的形式提取数据。
如果还没有IOService句柄,则应该可以使用以下任一方法到达那里IOServiceGetMatchingService()
(我希望可以安全地假设只有一个IOPlatformExpertDevice
实例。),或者使用它IORegistryGetRootEntry()
来获取根,然后遍历IORegistry图到具有IORegistryEntryGetChildEntry()
或类似功能的平台专家设备。
由于该board-id
属性没有命名的符号常量,因此只需对其进行硬编码:
CFTypeRef board_id_property = IORegistryEntryCreateCFProperty(
platform_expert_device, CFSTR("board-id"), kCFAllocatorDefault, 0);
请注意,属性值可以采取不同的类型,包括CFNumber
,CFBoolean
,CFString
,CFData
,CFArray
,和CFDictionary
,你需要准备处理,其中类型不匹配您期望,或返回NULL时(如果财产没有一个的情况下存在)。使用检查类型CFGetTypeID()
,例如:
if (board_id_property != NULL && CFGetTypeID(board_id_property) == CFDataGetTypeID())
{
CFDataRef board_id_data = (CFDataRef)board_id_property;
// safe to use CFData* functions now
...
CFRelease(board_id_property);
}
else
{
// Unexpected, do error handling.
...
if (board_id_property != NULL)
CFRelease(board_id_property);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句