如何创建一个程序来列出所有的USB设备在Mac? [英] How to create a program to list all the USB devices in a Mac?

查看:378
本文介绍了如何创建一个程序来列出所有的USB设备在Mac?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Mac OS X操作系统的曝光有限,现在我已经开始使用Xcode并且正在研究I / O工具包。我需要在命令行工具下创建一个程序在Xcode中,以列出所有USB设备连接在Mac系统中。那些以前有经验的人,请帮助我。

解决方案

如果任何人可以向我提供示例代码,那么它将是非常有用的。可以适应 USBPrivateDataSample 以满足您的需求,样本设置通知程序,列出当前连接的设备,然后等待设备连接/分离。如果您这样做,您将需要删除 usbVendor usbProduct 匹配字典,因此所有USB设备都匹配。 / p>

或者,您可以使用 IOServiceGetMatchingServices 获取所有当前匹配服务的迭代器,使用 IOServiceMatching (kIOUSBDeviceClassName)



以下是一个简短示例(我从未运行过):


$ b b

  #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;

/ *为类设置一个匹配字典* /
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if(matchingDict == NULL)
{
return -1; // fail
}

/ *现在我们有一个字典,得到一个迭代器。* /
kr = IOServiceGetMatchingServices(kIOMasterPortDefault,matchingDict,& iter);
if(kr!= KERN_SUCCESS)
{
return -1;
}

/ * iterate * /
while((device = IOIteratorNext(iter)))
{
/ 。检查属性* /
/ * ... * /
/ *并在继续下一个项目之前释放引用* /
IOObjectRelease(device);
}

/ *完成,释放迭代器* /
IOObjectRelease(iter);
return 0;
}


I have a limited exposure to the Mac OS X operating system and now I have started using Xcode and am studying about I/O kit. I need to create a program in Xcode under command line tool in order to list all USB devices connected in a Mac system. Those who have previous experience under this, please help me. If anyone could provide me with sample code then it will be of great use, as I am looking for starting point.

解决方案

You can adapt USBPrivateDataSample to your needs, the sample sets up a notifier, lists the currently attached devices, then waits for device attach/detach. If you do, you will want to remove the usbVendor and usbProduct matching dictionaries, so all USB devices are matched.

Alternately, you can use IOServiceGetMatchingServices to get an iterator for all current matching services, using a dictionary created by IOServiceMatching(kIOUSBDeviceClassName).

Here's a short sample (which I've never run):

#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;
}

这篇关于如何创建一个程序来列出所有的USB设备在Mac?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆