我对Mac OS X操作系统有限的接触,现在开始使用Xcode并正在学习I/O kit。我需要在Xcode下创建一个命令行工具程序,以便在Mac系统中列出所有连接的USB设备。如果有此方面的经验,请帮助我。如果有人能提供样本代码,那将非常有用,因为我正在寻找起点。
usbVendor
和 usbProduct
匹配字典,这样所有的 USB 设备都会被匹配。IOServiceGetMatchingServices
通过由 IOServiceMatching(kIOUSBDeviceClassName)
创建的字典获取所有当前匹配服务的迭代器。#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
int main(int argc, const char *argv[])
{
CFMutableDictionaryRef matchingDict;
io_iterator_t iter;
kern_return_t kr;
io_service_t device;
/* set up a matching dictionary for the class */
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if (matchingDict == NULL)
{
return -1; // fail
}
/* Now we have a dictionary, get an iterator.*/
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
if (kr != KERN_SUCCESS)
{
return -1;
}
/* iterate */
while ((device = IOIteratorNext(iter)))
{
/* do something with device, eg. check properties */
/* ... */
/* And free the reference taken before continuing to the next item */
IOObjectRelease(device);
}
/* Done, release the iterator */
IOObjectRelease(iter);
return 0;
}
$ man ioreg
获取USB设备列表:
$ ioreg -Src IOUSBDevice
system()
或 popen()
在代码中执行 ioreg 工具的部分? - Paul Rsystem_profiler SPUSBDataType
,它将列出连接到系统的所有USB设备,然后您可以将数据转储到文本文件中或从命令中读取并在应用程序中处理。
IOKit.framework
)和Core Foundation框架,否则它将无法链接。 - Hasturkun